engineering_lib_model.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. let result = false;
  57. data = this.filterLibData(data);
  58. try {
  59. // 查找计价规则表中是否有对应工程专业标准库的数据
  60. let compilationModel = new CompilationModel();
  61. let engineeringLib = await compilationModel.getEngineeringLib(valuationId, data.section, data.engineering);
  62. let result = false;
  63. if (engineeringLib === null) {
  64. // 不存在则插入
  65. result = await this.db.create(data);
  66. } else {
  67. engineeringLib = engineeringLib.length > 0 ? engineeringLib[0] : {};
  68. // 存在则直接更新
  69. let condition = {_id: engineeringLib.engineering_id};
  70. result = await this.db.update(condition, data);
  71. result = result.ok === 1;
  72. }
  73. // 失败直接返回
  74. if (!result) {
  75. throw '操作失败';
  76. }
  77. // 新增后更新编办数据表中专业工程字段
  78. if (result && engineeringLib === null) {
  79. let insertData = {
  80. engineering: data.engineering,
  81. engineering_id: result._id
  82. };
  83. let updateResult = await compilationModel.addEngineeringLib(valuationId, data.section, insertData);
  84. if (!updateResult) {
  85. throw '新增编办数据中的专业工程失败!';
  86. }
  87. }
  88. } catch (error) {
  89. console.log(error);
  90. result = false;
  91. }
  92. return result;
  93. }
  94. /**
  95. * 过滤计价数据
  96. *
  97. * @param {Object} data
  98. * @return {Object}
  99. */
  100. filterLibData(data) {
  101. if (Object.keys(data).length <= 0 || data.section === undefined) {
  102. throw '数据有误';
  103. }
  104. // 检测专业工程是否合法
  105. data.engineering = parseInt(data.engineering);
  106. let match = false;
  107. for(let index in EngineeringConst) {
  108. if (EngineeringConst[index] === data.engineering) {
  109. match = true;
  110. break;
  111. }
  112. }
  113. if (!match) {
  114. throw '工程专业错误';
  115. }
  116. // 判断标准清单
  117. data.bill_lib = this._validLib(data.bill_lib);
  118. // 判断定额库
  119. data.ration_lib = this._validLib(data.ration_lib);
  120. // 判断工料机库
  121. data.glj_lib = this._validLib(data.glj_lib);
  122. // 判断费率库
  123. data.fee_lib = this._validLib(data.fee_lib);
  124. return data;
  125. }
  126. /**
  127. * 校验库数据
  128. *
  129. * @param {Object} libData
  130. * @return {Object}
  131. */
  132. _validLib(libData) {
  133. let result = [];
  134. // 判断费率库
  135. if (libData === undefined || libData === '') {
  136. throw '判断费率库不能为空';
  137. }
  138. libData = libData instanceof Array ? libData : [libData];
  139. for(let tmp in libData) {
  140. result[tmp] = JSON.parse(libData[tmp]);
  141. }
  142. return result;
  143. }
  144. }
  145. export default EngineeringLibModel;