engineering_lib_model.js 6.8 KB

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