engineering_lib_model.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. if (engineeringLib === null) {
  68. // 不存在则插入
  69. result = await this.db.create(data);
  70. } else {
  71. delete data.id;
  72. delete data.section;
  73. delete data.engineering;
  74. // 存在则直接更新
  75. let condition = {_id: engineeringLib.engineering_id};
  76. result = await this.db.update(condition, data);
  77. result = result.ok === 1;
  78. }
  79. // 失败直接返回
  80. if (!result) {
  81. throw '操作失败';
  82. }
  83. // 新增后更新编办数据表中专业工程字段
  84. if (result && engineeringLib === null) {
  85. let insertData = {
  86. engineering: data.engineering,
  87. engineering_id: result._id
  88. };
  89. let updateResult = await compilationModel.addEngineeringLib(valuationId, data.section, insertData);
  90. if (!updateResult) {
  91. throw '新增编办数据中的专业工程失败!';
  92. }
  93. }
  94. } catch (error) {
  95. console.log(error);
  96. result = false;
  97. }
  98. return result;
  99. }
  100. /**
  101. * 过滤计价数据
  102. *
  103. * @param {Object} data
  104. * @return {Object}
  105. */
  106. filterLibData(data) {
  107. if (Object.keys(data).length <= 0 || data.section === undefined) {
  108. console.log('1');
  109. throw '数据有误';
  110. }
  111. // 检测专业工程是否合法
  112. data.engineering = parseInt(data.engineering);
  113. let match = false;
  114. for(let index in EngineeringConst) {
  115. if (EngineeringConst[index] === data.engineering) {
  116. match = true;
  117. break;
  118. }
  119. }
  120. if (!match) {
  121. throw '工程专业错误';
  122. }
  123. // 判断标准清单
  124. data.bill_lib = this._validLib(data.bill_lib);
  125. // 判断定额库
  126. data.ration_lib = this._validLib(data.ration_lib);
  127. // 判断工料机库
  128. data.glj_lib = this._validLib(data.glj_lib);
  129. // 判断费率标准
  130. data.fee_lib = this._validLib(data.fee_lib);
  131. // 判断人工系数
  132. data.artificial_lib = this._validLib(data.artificial_lib);
  133. return data;
  134. }
  135. /**
  136. * 校验库数据
  137. *
  138. * @param {Object} libData
  139. * @return {Object}
  140. */
  141. _validLib(libData) {
  142. let result = [];
  143. // 判断标准库
  144. if (libData === undefined || libData === '') {
  145. throw '标准库不能为空';
  146. }
  147. libData = libData instanceof Array ? libData : [libData];
  148. for(let tmp in libData) {
  149. result[tmp] = JSON.parse(libData[tmp]);
  150. }
  151. return result;
  152. }
  153. /**
  154. * 获取对应标准库数量
  155. *
  156. * @param {Object} valuationData
  157. * @return {Object}
  158. */
  159. async getLibCount(valuationData) {
  160. let result = {};
  161. if (valuationData.engineering_list === undefined || valuationData.engineering_list.length <= 0) {
  162. return result;
  163. }
  164. // 整理需要查找的数据
  165. let findIdList = [];
  166. for(let engineering of valuationData.engineering_list) {
  167. findIdList.push(engineering.engineering_id);
  168. }
  169. let condition = {_id: {$in: findIdList}};
  170. let libData = await this.findDataByCondition(condition, null, false);
  171. if (libData === null) {
  172. return result;
  173. }
  174. // 整理数据
  175. let countData = {};
  176. for(let tmp of libData) {
  177. countData[tmp._id] = {
  178. bill_count: tmp.bill_lib.length,
  179. ration_count: tmp.ration_lib.length,
  180. glj_count: tmp.glj_lib.length,
  181. fee_count: tmp.fee_lib.length,
  182. artificial_count: tmp.artificial_lib.length,
  183. };
  184. }
  185. for(let engineering of valuationData.engineering_list) {
  186. if (countData[engineering.engineering_id] !== undefined) {
  187. result[engineering.engineering] = countData[engineering.engineering_id];
  188. }
  189. }
  190. return result;
  191. }
  192. }
  193. export default EngineeringLibModel;