engineering_lib_model.js 6.4 KB

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