compilation_model.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. result = this.db.create(data);
  69. return result;
  70. }
  71. /**
  72. * 新增计价规则
  73. *
  74. * @param {String} id
  75. * @param {String} section
  76. * @param {Object} data
  77. * @return {Promise}
  78. */
  79. async addValuation(id, section, data) {
  80. let condition = {_id: id};
  81. let compilationData = await this.findDataByCondition(condition);
  82. if (compilationData === null || compilationData.name === undefined) {
  83. throw '没有找到对应的数据';
  84. }
  85. if (data.name === undefined || data.name === '') {
  86. throw '计价规则名称为空';
  87. }
  88. if (this.sectionList.indexOf(section) < 0) {
  89. throw '数据有误';
  90. }
  91. let insertData = {};
  92. insertData[section + '_valuation'] = data;
  93. let result = await this.db.addToSet(condition, insertData);
  94. return result.ok === undefined ? false : result.ok;
  95. }
  96. /**
  97. * 保存计价数据
  98. *
  99. * @param {String} valuationId
  100. * @param {Object} data
  101. * @return {Promise}
  102. */
  103. async saveValuation(valuationId, data) {
  104. data = this._filterValuationData(data);
  105. let sectionString = data.section + "_valuation";
  106. let condition = {};
  107. condition[sectionString + "._id"] = valuationId;
  108. let updateData = {};
  109. updateData[sectionString + ".$.bill_lib"] = data.bill_lib;
  110. updateData[sectionString + ".$.ration_lib"] = data.ration_lib;
  111. updateData[sectionString + ".$.name"] = data.name;
  112. updateData[sectionString + ".$.engineering"] = data.engineering;
  113. updateData[sectionString + ".$.main_tree_col"] = JSON.parse(data.main_tree_col);
  114. let result = await this.db.update(condition, updateData);
  115. return result !== null && result.ok === 1;
  116. };
  117. /**
  118. * 更改启用/禁用
  119. *
  120. * @param {String} valuationId
  121. * @param {String} section
  122. * @param {String} enable
  123. * @return {Promise}
  124. */
  125. async switchEnable(valuationId, section, enable) {
  126. let sectionString = section + "_valuation";
  127. let condition = {};
  128. condition[sectionString + "._id"] = valuationId;
  129. let updateData = {};
  130. updateData[sectionString + ".$.enable"] = enable === "true";
  131. let result = await this.db.update(condition, updateData);
  132. return result !== null && result.ok === 1;
  133. }
  134. /**
  135. * 过滤计价数据
  136. *
  137. * @param {Object} data
  138. * @return {Object}
  139. */
  140. _filterValuationData(data) {
  141. if (Object.keys(data).length <= 0) {
  142. throw '数据有误';
  143. }
  144. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  145. throw '类型错误';
  146. }
  147. // 判断名称
  148. if (data.name === undefined || data.name === '') {
  149. throw '名称不能为空';
  150. }
  151. // 判断工程专业
  152. if (data.engineering === undefined || data.engineering === '') {
  153. throw '名称不能为空';
  154. }
  155. data.engineering = parseInt(data.engineering);
  156. // 判断标准清单
  157. if (data.bill_lib === undefined || data.bill_lib === '') {
  158. throw '判断标准清单不能为空';
  159. }
  160. data.bill_lib = data.bill_lib instanceof Array ? data.bill_lib : [data.bill_lib];
  161. for(let tmp in data.bill_lib) {
  162. data.bill_lib[tmp] = JSON.parse(data.bill_lib[tmp]);
  163. }
  164. // 判断定额清单
  165. if (data.ration_lib === undefined || data.ration_lib === '') {
  166. throw '判断标准清单不能为空';
  167. }
  168. data.ration_lib = data.ration_lib instanceof Array ? data.ration_lib : [data.ration_lib];
  169. for(let tmp in data.ration_lib) {
  170. data.ration_lib[tmp] = JSON.parse(data.ration_lib[tmp]);
  171. }
  172. return data;
  173. }
  174. /**
  175. * 获取计价规则数据
  176. *
  177. * @param {String} compilationId
  178. * @param {String} id
  179. * @param {String} section
  180. * @return {Promise|Array}
  181. */
  182. async getValuation(compilationId, id, section) {
  183. if (this.sectionList.indexOf(section) < 0) {
  184. throw '数据有误';
  185. }
  186. let compilationData = await this.findDataByCondition({_id: compilationId});
  187. if (Object.keys(compilationData).length <= 0) {
  188. throw '编办数据有误';
  189. }
  190. let result = {};
  191. let sectionString = section + '_valuation';
  192. for(let valuation of compilationData[sectionString]) {
  193. if (valuation._id.toString() === id) {
  194. result = valuation;
  195. break;
  196. }
  197. }
  198. return [result, compilationData[sectionString]];
  199. /* 数据库获取编办
  200. let condition = {_id: versionId};
  201. let childCondition = {};
  202. childCondition[sectionString] = {$elemMatch: {_id: id}};
  203. let result = await this.db.findOne(condition, childCondition);
  204. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  205. */
  206. }
  207. /**
  208. * 删除计价规则
  209. *
  210. * @param {String} compilationId
  211. * @param {String} valuationId
  212. * @param {String} section
  213. * @return {Promise}
  214. */
  215. async deleteValuation(compilationId, valuationId, section) {
  216. let condition = {_id: compilationId};
  217. let sectionString = section + '_valuation';
  218. let deleteData = {};
  219. deleteData[sectionString] = {_id: valuationId};
  220. // 利用pull删除嵌套数据
  221. let result = await this.db.deleteSet(condition, deleteData);
  222. return result !== null && result.ok === 1;
  223. }
  224. /**
  225. * 发布编办
  226. *
  227. * @param {String} id
  228. * @param {Number} status
  229. * @return {Promise}
  230. */
  231. async release(id, status) {
  232. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  233. if (status) {
  234. let compilationData = await this.findDataByCondition({_id: id});
  235. // 最少需要有一个计价规则存在
  236. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  237. throw '至少需要一个计价规则';
  238. }
  239. // 判断是否全部禁止
  240. let hasEnable = false;
  241. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  242. for (let valuation of valuationList) {
  243. if (valuation.enable) {
  244. hasEnable = true;
  245. break;
  246. }
  247. }
  248. if (!hasEnable) {
  249. throw '至少需要一个可用的计价规则';
  250. }
  251. }
  252. let updateData = {
  253. is_release: status === 1,
  254. release_time: new Date().getTime()
  255. };
  256. return this.updateById(id, updateData);
  257. }
  258. }
  259. export default CompilationModel;