engineering_lib_model.js 6.5 KB

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