engineering_lib_model.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * 计价规则标准库业务逻辑
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/8/31
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model";
  9. import EngineeringLibSchema from "./schemas/engineering_lib";
  10. import CompilationModel from "./compilation_model";
  11. import {default as EngineeringConst, List as EngineeringList} from "../../common/const/engineering";
  12. class EngineeringLibModel extends BaseModel {
  13. /**
  14. * 构造函数
  15. *
  16. * @return {void}
  17. */
  18. constructor() {
  19. let parent = super();
  20. parent.model = EngineeringLibSchema;
  21. parent.init();
  22. }
  23. /**
  24. * 获取标准库数据
  25. *
  26. * @param {Object} data
  27. * @param {Number} engineering
  28. * @return {Promise}
  29. */
  30. async getLib(data, engineering) {
  31. let result = {};
  32. if (data.length <= 0) {
  33. return result;
  34. }
  35. let id = '';
  36. for(let tmp of data) {
  37. if (tmp.engineering === engineering) {
  38. id = tmp.engineering_id;
  39. break;
  40. }
  41. }
  42. if (id === '') {
  43. return result;
  44. }
  45. let condition = {_id: id};
  46. return this.findDataByCondition(condition);
  47. }
  48. /**
  49. * 新增标准库
  50. *
  51. * @param {String} valuationId
  52. * @param {Object} data
  53. * @return {Promise}
  54. */
  55. async addLib(valuationId, data) {
  56. if (data.main_tree_col) {
  57. data.main_tree_col = JSON.parse(data.main_tree_col);
  58. } else {
  59. delete data['main_tree_col'];
  60. }
  61. let result = false;
  62. data = this.filterLibData(data);
  63. try {
  64. // 查找计价规则表中是否有对应工程专业标准库的数据
  65. let compilationModel = new CompilationModel();
  66. let engineeringLib = await compilationModel.getEngineeringLib(valuationId, data.section, data.engineering);
  67. let result = false;
  68. if (engineeringLib === null) {
  69. // 不存在则插入
  70. result = await this.db.create(data);
  71. } else {
  72. engineeringLib = engineeringLib.length > 0 ? engineeringLib[0] : {};
  73. // 存在则直接更新
  74. let condition = {_id: engineeringLib.engineering_id};
  75. result = await this.db.update(condition, data);
  76. result = result.ok === 1;
  77. }
  78. // 失败直接返回
  79. if (!result) {
  80. throw '操作失败';
  81. }
  82. // 新增后更新编办数据表中专业工程字段
  83. if (result && engineeringLib === null) {
  84. let insertData = {
  85. engineering: data.engineering,
  86. engineering_id: result._id
  87. };
  88. let updateResult = await compilationModel.addEngineeringLib(valuationId, data.section, insertData);
  89. if (!updateResult) {
  90. throw '新增编办数据中的专业工程失败!';
  91. }
  92. }
  93. } catch (error) {
  94. console.log(error);
  95. result = false;
  96. }
  97. return result;
  98. }
  99. /**
  100. * 过滤计价数据
  101. *
  102. * @param {Object} data
  103. * @return {Object}
  104. */
  105. filterLibData(data) {
  106. if (Object.keys(data).length <= 0 || data.section === undefined) {
  107. console.log('1');
  108. throw '数据有误';
  109. }
  110. // 检测专业工程是否合法
  111. data.engineering = parseInt(data.engineering);
  112. let match = false;
  113. for(let index in EngineeringConst) {
  114. if (EngineeringConst[index] === data.engineering) {
  115. match = true;
  116. break;
  117. }
  118. }
  119. if (!match) {
  120. throw '工程专业错误';
  121. }
  122. // 判断标准清单
  123. data.bill_lib = this._validLib(data.bill_lib);
  124. // 判断定额库
  125. data.ration_lib = this._validLib(data.ration_lib);
  126. // 判断工料机库
  127. data.glj_lib = this._validLib(data.glj_lib);
  128. // 判断费率标准
  129. data.fee_lib = this._validLib(data.fee_lib);
  130. // 判断人工系数
  131. data.artificial_lib = this._validLib(data.artificial_lib);
  132. return data;
  133. }
  134. /**
  135. * 校验库数据
  136. *
  137. * @param {Object} libData
  138. * @return {Object}
  139. */
  140. _validLib(libData) {
  141. let result = [];
  142. // 判断标准库
  143. if (libData === undefined || libData === '') {
  144. throw '标准库不能为空';
  145. }
  146. libData = libData instanceof Array ? libData : [libData];
  147. for(let tmp in libData) {
  148. result[tmp] = JSON.parse(libData[tmp]);
  149. }
  150. return result;
  151. }
  152. /**
  153. * 获取对应标准库数量
  154. *
  155. * @param {Object} valuationData
  156. * @return {Object}
  157. */
  158. async getLibCount(valuationData) {
  159. let result = {};
  160. if (valuationData.engineering_list === undefined || valuationData.engineering_list.length <= 0) {
  161. return result;
  162. }
  163. // 整理需要查找的数据
  164. let findIdList = [];
  165. for(let engineering of valuationData.engineering_list) {
  166. findIdList.push(engineering.engineering_id);
  167. }
  168. let condition = {_id: {$in: findIdList}};
  169. let libData = await this.findDataByCondition(condition, null, false);
  170. if (libData === null) {
  171. return result;
  172. }
  173. // 整理数据
  174. let countData = {};
  175. for(let tmp of libData) {
  176. countData[tmp._id] = {
  177. bill_count: tmp.bill_lib.length,
  178. ration_count: tmp.ration_lib.length,
  179. glj_count: tmp.glj_lib.length,
  180. fee_count: tmp.fee_lib.length,
  181. artificial_count: tmp.artificial_lib.length,
  182. };
  183. }
  184. for(let engineering of valuationData.engineering_list) {
  185. if (countData[engineering.engineering_id] !== undefined) {
  186. result[engineering.engineering] = countData[engineering.engineering_id];
  187. }
  188. }
  189. return result;
  190. }
  191. }
  192. export default EngineeringLibModel;