compilation_model.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * 编办管理业务逻辑模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import BaseModel from "../../common/base/base_model";
  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 = mongoose.model('compilation');
  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, description: 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} compilationId
  75. * @param {String} description
  76. * @return {Promise}
  77. * */
  78. async setDescription(compilationId, description){
  79. return await this.updateById(compilationId, {description: description});
  80. }
  81. /**
  82. * 新增计价规则
  83. *
  84. * @param {String} id
  85. * @param {String} section
  86. * @param {Object} data
  87. * @return {Promise}
  88. */
  89. async addValuation(id, section, data) {
  90. let condition = {_id: id};
  91. let compilationData = await this.findDataByCondition(condition);
  92. if (compilationData === null || compilationData.name === undefined) {
  93. throw '没有找到对应的数据';
  94. }
  95. if (data.name === undefined || data.name === '') {
  96. throw '计价规则名称为空';
  97. }
  98. if (this.sectionList.indexOf(section) < 0) {
  99. console.log('2');
  100. throw '数据有误';
  101. }
  102. let insertData = {};
  103. insertData[section + '_valuation'] = data;
  104. let result = await this.db.addToSet(condition, insertData);
  105. return result.ok === undefined ? false : result.ok;
  106. }
  107. /**
  108. * 保存计价数据
  109. *
  110. * @param {String} valuationId
  111. * @param {Object} data
  112. * @return {Promise}
  113. */
  114. async saveValuation(valuationId, data) {
  115. data = this._filterValuationData(data);
  116. let sectionString = data.section + "_valuation";
  117. let condition = {};
  118. condition[sectionString + "._id"] = valuationId;
  119. let updateData = {};
  120. updateData[sectionString + ".$.name"] = data.name;
  121. let result = await this.db.update(condition, updateData);
  122. return result !== null && result.ok === 1;
  123. };
  124. /**
  125. * 更改启用/禁用
  126. *
  127. * @param {String} valuationId
  128. * @param {String} section
  129. * @param {String} enable
  130. * @return {Promise}
  131. */
  132. async switchEnable(valuationId, section, enable) {
  133. let sectionString = section + "_valuation";
  134. let condition = {};
  135. condition[sectionString + "._id"] = valuationId;
  136. let updateData = {};
  137. updateData[sectionString + ".$.enable"] = enable === "true";
  138. let result = await this.db.update(condition, updateData);
  139. return result !== null && result.ok === 1;
  140. }
  141. /**
  142. * 过滤计价数据
  143. *
  144. * @param {Object} data
  145. * @return {Object}
  146. */
  147. _filterValuationData(data) {
  148. if (Object.keys(data).length <= 0) {
  149. console.log('3');
  150. throw '数据有误';
  151. }
  152. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  153. throw '类型错误';
  154. }
  155. // 判断名称
  156. if (data.name === undefined || data.name === '') {
  157. throw '名称不能为空';
  158. }
  159. return data;
  160. }
  161. /**
  162. * 获取计价规则数据
  163. *
  164. * @param {String} compilationId
  165. * @param {String} id
  166. * @param {String} section
  167. * @return {Promise|Array}
  168. */
  169. async getValuation(compilationId, id, section) {
  170. if (this.sectionList.indexOf(section) < 0) {
  171. throw '数据有误';
  172. }
  173. let compilationData = await this.findDataByCondition({_id: compilationId});
  174. if (Object.keys(compilationData).length <= 0) {
  175. throw '编办数据有误';
  176. }
  177. let result = {};
  178. let sectionString = section + '_valuation';
  179. for(let valuation of compilationData[sectionString]) {
  180. if (valuation._id.toString() === id) {
  181. result = valuation;
  182. break;
  183. }
  184. }
  185. return [result, compilationData[sectionString]];
  186. /* 数据库获取编办
  187. let condition = {_id: versionId};
  188. let childCondition = {};
  189. childCondition[sectionString] = {$elemMatch: {_id: id}};
  190. let result = await this.db.findOne(condition, childCondition);
  191. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  192. */
  193. }
  194. /**
  195. * 删除计价规则
  196. *
  197. * @param {String} compilationId
  198. * @param {String} valuationId
  199. * @param {String} section
  200. * @return {Promise}
  201. */
  202. async deleteValuation(compilationId, valuationId, section) {
  203. let condition = {_id: compilationId};
  204. let sectionString = section + '_valuation';
  205. let deleteData = {};
  206. deleteData[sectionString] = {_id: valuationId};
  207. // 利用pull删除嵌套数据
  208. let result = await this.db.deleteSet(condition, deleteData);
  209. return result !== null && result.ok === 1;
  210. }
  211. /**
  212. * 发布编办
  213. *
  214. * @param {String} id
  215. * @param {Number} status
  216. * @return {Promise}
  217. */
  218. async release(id, status) {
  219. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  220. if (status) {
  221. let compilationData = await this.findDataByCondition({_id: id});
  222. // 最少需要有一个计价规则存在
  223. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  224. throw '至少需要一个计价规则';
  225. }
  226. // 判断是否全部禁止
  227. let hasEnable = false;
  228. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  229. for (let valuation of valuationList) {
  230. if (valuation.enable) {
  231. hasEnable = true;
  232. break;
  233. }
  234. }
  235. if (!hasEnable) {
  236. throw '至少需要一个可用的计价规则';
  237. }
  238. }
  239. let updateData = {
  240. is_release: status === 1,
  241. release_time: new Date().getTime()
  242. };
  243. return this.updateById(id, updateData);
  244. }
  245. /**
  246. * 根据专业工程获取对应专业工程标准库id
  247. *
  248. * @param {String} valuationId
  249. * @param {String} section
  250. * @param {Number} engineering
  251. * @return {Promise}
  252. */
  253. async getEngineeringLib(valuationId, section, engineering) {
  254. let sectionString = section + "_valuation";
  255. let condition = {};
  256. condition[sectionString + "._id"] = valuationId;
  257. let compilationData = await this.findDataByCondition(condition);
  258. if (compilationData === null) {
  259. throw '不存在对应编办';
  260. }
  261. let valuationData = null;
  262. for(let valuation of compilationData[sectionString]) {
  263. if(valuation._id.toString() === valuationId) {
  264. valuationData = valuation;
  265. }
  266. }
  267. if (valuationData === null) {
  268. throw '不存在对应计价规则';
  269. }
  270. // 判断是否已有对应数据
  271. let engineeringList = valuationData.engineering_list;
  272. let engineeringLib = null;
  273. for(let tmpEngineering of engineeringList) {
  274. if (tmpEngineering.engineering === engineering) {
  275. engineeringLib = tmpEngineering;
  276. break;
  277. }
  278. }
  279. return engineeringLib;
  280. }
  281. /**
  282. * 新增专业工程标准库
  283. *
  284. * @param {String} valuationId
  285. * @param {String} section
  286. * @param {Object} data
  287. * @return {Promise}
  288. */
  289. async addEngineeringLib(valuationId, section, data) {
  290. let sectionString = section + "_valuation";
  291. let condition = {};
  292. condition[sectionString + "._id"] = valuationId;
  293. let insertData = {};
  294. insertData[sectionString + ".$.engineering_list"] = data;
  295. let result = await this.db.addToSet(condition, insertData);
  296. return result.ok === 1;
  297. }
  298. }
  299. export default CompilationModel;