compilation_model.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * 编办管理业务逻辑模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model";
  9. import CompilationSchema from "./schemas/compilation";
  10. class CompilationModel extends BaseModel {
  11. /**
  12. * 允许的块
  13. *
  14. * @var {Array}
  15. */
  16. sectionList = ['bill', 'ration'];
  17. /**
  18. * 构造函数
  19. *
  20. * @return {void}
  21. */
  22. constructor() {
  23. let parent = super();
  24. parent.model = CompilationSchema;
  25. parent.init();
  26. }
  27. /**
  28. * 获取编办列表
  29. *
  30. * @return {Promise}
  31. */
  32. async getCompilationList() {
  33. // 筛选字段
  34. let field = {_id: 1, name: 1, is_release: 1, "ration_valuation._id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
  35. "bill_valuation._id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1};
  36. let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);
  37. return compilationData === null ? [] : compilationData;
  38. }
  39. /**
  40. * 设置场景
  41. *
  42. * @param {string} scene
  43. * @return {void}
  44. */
  45. setScene(scene = '') {
  46. switch (scene) {
  47. // 新增
  48. case 'add':
  49. this.model.schema.path('name').required(true);
  50. this.model.schema.path('creator').required(true);
  51. this.model.schema.path('create_time').required(true);
  52. break;
  53. }
  54. }
  55. /**
  56. * 新增编办
  57. *
  58. * @param {Object} data
  59. * @return {Promise}
  60. */
  61. async add(data) {
  62. let result = false;
  63. if (Object.keys(data).length <= 0) {
  64. return result;
  65. }
  66. this.setScene('add');
  67. data.create_time = new Date().getTime();
  68. console.log(data);
  69. result = this.db.create(data);
  70. return result;
  71. }
  72. /**
  73. * 新增计价规则
  74. *
  75. * @param {String} id
  76. * @param {String} section
  77. * @param {Object} data
  78. * @return {Promise}
  79. */
  80. async addValuation(id, section, data) {
  81. let condition = {_id: id};
  82. let compilationData = await this.findDataByCondition(condition);
  83. if (compilationData === null || compilationData.name === undefined) {
  84. throw '没有找到对应的数据';
  85. }
  86. if (data.name === undefined || data.name === '') {
  87. throw '计价规则名称为空';
  88. }
  89. if (this.sectionList.indexOf(section) < 0) {
  90. throw '数据有误';
  91. }
  92. let insertData = {};
  93. insertData[section + '_valuation'] = data;
  94. let result = await this.db.addToSet(condition, insertData);
  95. return result.ok === undefined ? false : result.ok;
  96. }
  97. /**
  98. * 保存计价数据
  99. *
  100. * @param {String} valuationId
  101. * @param {Object} data
  102. * @return {Promise}
  103. */
  104. async saveValuation(valuationId, data) {
  105. data = this._filterValuationData(data);
  106. let sectionString = data.section + "_valuation";
  107. let condition = {};
  108. condition[sectionString + "._id"] = valuationId;
  109. let updateData = {};
  110. updateData[sectionString + ".$.bill_lib"] = data.bill_lib;
  111. updateData[sectionString + ".$.ration_lib"] = data.ration_lib;
  112. updateData[sectionString + ".$.glj_lib"] = data.glj_lib;
  113. updateData[sectionString + ".$.fee_lib"] = data.fee_lib;
  114. updateData[sectionString + ".$.name"] = data.name;
  115. updateData[sectionString + ".$.engineering"] = data.engineering;
  116. updateData[sectionString + ".$.main_tree_col"] = JSON.parse(data.main_tree_col);
  117. let result = await this.db.update(condition, updateData);
  118. return result !== null && result.ok === 1;
  119. };
  120. /**
  121. * 更改启用/禁用
  122. *
  123. * @param {String} valuationId
  124. * @param {String} section
  125. * @param {String} enable
  126. * @return {Promise}
  127. */
  128. async switchEnable(valuationId, section, enable) {
  129. let sectionString = section + "_valuation";
  130. let condition = {};
  131. condition[sectionString + "._id"] = valuationId;
  132. let updateData = {};
  133. updateData[sectionString + ".$.enable"] = enable === "true";
  134. let result = await this.db.update(condition, updateData);
  135. return result !== null && result.ok === 1;
  136. }
  137. /**
  138. * 过滤计价数据
  139. *
  140. * @param {Object} data
  141. * @return {Object}
  142. */
  143. _filterValuationData(data) {
  144. if (Object.keys(data).length <= 0) {
  145. throw '数据有误';
  146. }
  147. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  148. throw '类型错误';
  149. }
  150. // 判断名称
  151. if (data.name === undefined || data.name === '') {
  152. throw '名称不能为空';
  153. }
  154. // 判断工程专业
  155. if (data.engineering === undefined || data.engineering === '') {
  156. throw '名称不能为空';
  157. }
  158. data.engineering = parseInt(data.engineering);
  159. // 判断标准清单
  160. data.bill_lib = this._validLib(data.bill_lib);
  161. // 判断定额库
  162. data.ration_lib = this._validLib(data.ration_lib);
  163. // 判断工料机库
  164. data.glj_lib = this._validLib(data.glj_lib);
  165. // 判断费率库
  166. data.fee_lib = this._validLib(data.fee_lib);
  167. return data;
  168. }
  169. /**
  170. * 校验库数据
  171. *
  172. * @param {Object} libData
  173. * @return {Object}
  174. */
  175. _validLib(libData) {
  176. let result = [];
  177. // 判断费率库
  178. if (libData === undefined || libData === '') {
  179. throw '判断费率库不能为空';
  180. }
  181. libData = libData instanceof Array ? libData : [libData];
  182. for(let tmp in libData) {
  183. result[tmp] = JSON.parse(libData[tmp]);
  184. }
  185. return result;
  186. }
  187. /**
  188. * 获取计价规则数据
  189. *
  190. * @param {String} compilationId
  191. * @param {String} id
  192. * @param {String} section
  193. * @return {Promise|Array}
  194. */
  195. async getValuation(compilationId, id, section) {
  196. if (this.sectionList.indexOf(section) < 0) {
  197. throw '数据有误';
  198. }
  199. let compilationData = await this.findDataByCondition({_id: compilationId});
  200. if (Object.keys(compilationData).length <= 0) {
  201. throw '编办数据有误';
  202. }
  203. let result = {};
  204. let sectionString = section + '_valuation';
  205. for(let valuation of compilationData[sectionString]) {
  206. if (valuation._id.toString() === id) {
  207. result = valuation;
  208. break;
  209. }
  210. }
  211. return [result, compilationData[sectionString]];
  212. /* 数据库获取编办
  213. let condition = {_id: versionId};
  214. let childCondition = {};
  215. childCondition[sectionString] = {$elemMatch: {_id: id}};
  216. let result = await this.db.findOne(condition, childCondition);
  217. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  218. */
  219. }
  220. /**
  221. * 删除计价规则
  222. *
  223. * @param {String} compilationId
  224. * @param {String} valuationId
  225. * @param {String} section
  226. * @return {Promise}
  227. */
  228. async deleteValuation(compilationId, valuationId, section) {
  229. let condition = {_id: compilationId};
  230. let sectionString = section + '_valuation';
  231. let deleteData = {};
  232. deleteData[sectionString] = {_id: valuationId};
  233. // 利用pull删除嵌套数据
  234. let result = await this.db.deleteSet(condition, deleteData);
  235. return result !== null && result.ok === 1;
  236. }
  237. /**
  238. * 发布编办
  239. *
  240. * @param {String} id
  241. * @param {Number} status
  242. * @return {Promise}
  243. */
  244. async release(id, status) {
  245. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  246. if (status) {
  247. let compilationData = await this.findDataByCondition({_id: id});
  248. // 最少需要有一个计价规则存在
  249. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  250. throw '至少需要一个计价规则';
  251. }
  252. // 判断是否全部禁止
  253. let hasEnable = false;
  254. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  255. for (let valuation of valuationList) {
  256. if (valuation.enable) {
  257. hasEnable = true;
  258. break;
  259. }
  260. }
  261. if (!hasEnable) {
  262. throw '至少需要一个可用的计价规则';
  263. }
  264. }
  265. let updateData = {
  266. is_release: status === 1,
  267. release_time: new Date().getTime()
  268. };
  269. return this.updateById(id, updateData);
  270. }
  271. }
  272. export default CompilationModel;