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