compilation_model.js 8.6 KB

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