compilation_model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. import uuidV1 from 'uuid/v1';
  11. class CompilationModel extends BaseModel {
  12. /**
  13. * 允许的块
  14. *
  15. * @var {Array}
  16. */
  17. sectionList = ['bill', 'ration'];
  18. /**
  19. * 构造函数
  20. *
  21. * @return {void}
  22. */
  23. constructor() {
  24. let parent = super();
  25. parent.model = mongoose.model('compilation');
  26. parent.init();
  27. }
  28. /**
  29. * 获取编办列表
  30. *
  31. * @return {Promise}
  32. */
  33. async getCompilationList(fields = null) {
  34. // 筛选字段
  35. let field = fields == null ?{_id: 1, name: 1, is_release: 1, categoryID: 1, description: 1,overWriteUrl: 1,example: 1, "ration_valuation.id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
  36. "bill_valuation.id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1}:fields;
  37. let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);
  38. return compilationData === null ? [] : compilationData;
  39. }
  40. /**
  41. * 设置场景
  42. *
  43. * @param {string} scene
  44. * @return {void}
  45. */
  46. setScene(scene = '') {
  47. switch (scene) {
  48. // 新增
  49. case 'add':
  50. this.model.schema.path('name').required(true);
  51. this.model.schema.path('creator').required(true);
  52. this.model.schema.path('create_time').required(true);
  53. break;
  54. }
  55. }
  56. /**
  57. * 新增编办
  58. *
  59. * @param {Object} data
  60. * @return {Promise}
  61. */
  62. async add(data) {
  63. let result = false;
  64. if (Object.keys(data).length <= 0) {
  65. return result;
  66. }
  67. this.setScene('add');
  68. data.create_time = new Date().getTime();
  69. result = this.db.create(data);
  70. return result;
  71. }
  72. /*
  73. * 设置版本描述
  74. *
  75. * @param {String} compilationId
  76. * @param {String} description
  77. * @return {Promise}
  78. * */
  79. async setDescription(compilationId, description){
  80. return await this.updateById(compilationId, {description: description});
  81. }
  82. /*
  83. 设置代码覆盖路径
  84. */
  85. async setOverWriteUrl(compilationId, overWriteUrl, priceProp, consumeAmtProp){
  86. return await this.updateById(compilationId, {overWriteUrl: overWriteUrl, priceProperties: priceProp, consumeAmtProperties: consumeAmtProp});
  87. }
  88. /*
  89. * 设置例题
  90. * @param {String} compilationId
  91. * @param {Array} example
  92. * @return {Promise}
  93. * */
  94. async setExample(compilationId, example) {
  95. let data = [];
  96. for (let projId of example) {
  97. data.push(parseInt(projId));
  98. }
  99. return await this.updateById(compilationId, {example: data});
  100. }
  101. /**
  102. * 新增计价规则
  103. *
  104. * @param {String} id
  105. * @param {String} section
  106. * @param {Object} data
  107. * @return {Promise}
  108. */
  109. async addValuation(id, section, data) {
  110. let condition = {_id: id};
  111. let compilationData = await this.findDataByCondition(condition);
  112. if (compilationData === null || compilationData.name === undefined) {
  113. throw '没有找到对应的数据';
  114. }
  115. if (data.name === undefined || data.name === '') {
  116. throw '计价规则名称为空';
  117. }
  118. if (this.sectionList.indexOf(section) < 0) {
  119. console.log('2');
  120. throw '数据有误';
  121. }
  122. let insertData = {};
  123. data.id = uuidV1();
  124. insertData[section + '_valuation'] = data;
  125. let result = await this.db.addToSet(condition, insertData);
  126. return result.ok === undefined ? false : data.id;
  127. }
  128. /**
  129. * 保存计价数据
  130. *
  131. * @param {String} valuationId
  132. * @param {Object} data
  133. * @return {Promise}
  134. */
  135. async saveValuation(valuationId, data) {
  136. data = this._filterValuationData(data);
  137. let sectionString = data.section + "_valuation";
  138. let condition = {};
  139. condition[sectionString + ".id"] = valuationId;
  140. let updateData = {};
  141. updateData[sectionString + ".$.name"] = data.valuationName;
  142. let result = await this.db.update(condition, updateData);
  143. return result !== null && result.ok === 1;
  144. };
  145. /**
  146. * 更改启用/禁用
  147. *
  148. * @param {String} valuationId
  149. * @param {String} section
  150. * @param {String} enable
  151. * @return {Promise}
  152. */
  153. async switchEnable(valuationId, section, enable) {
  154. let sectionString = section + "_valuation";
  155. let condition = {};
  156. condition[sectionString + ".id"] = valuationId;
  157. let updateData = {};
  158. updateData[sectionString + ".$.enable"] = enable === "true";
  159. let result = await this.db.update(condition, updateData);
  160. return result !== null && result.ok === 1;
  161. }
  162. /**
  163. * 过滤计价数据
  164. *
  165. * @param {Object} data
  166. * @return {Object}
  167. */
  168. _filterValuationData(data) {
  169. if (Object.keys(data).length <= 0) {
  170. console.log('3');
  171. throw '数据有误';
  172. }
  173. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  174. throw '类型错误';
  175. }
  176. // 判断名称
  177. if (data.valuationName === undefined || data.valuationName === '') {
  178. throw '名称不能为空';
  179. }
  180. return data;
  181. }
  182. /**
  183. * 获取计价规则数据
  184. *
  185. * @param {String} compilationId
  186. * @param {String} id
  187. * @param {String} section
  188. * @return {Promise|Array}
  189. */
  190. async getValuation(compilationId, id, section) {
  191. if (this.sectionList.indexOf(section) < 0) {
  192. throw '数据有误';
  193. }
  194. let compilationData = await this.findDataByCondition({_id: compilationId});
  195. if (Object.keys(compilationData).length <= 0) {
  196. throw '编办数据有误';
  197. }
  198. let result = {};
  199. let sectionString = section + '_valuation';
  200. for(let valuation of compilationData[sectionString]) {
  201. if (valuation.id.toString() === id) {
  202. result = valuation;
  203. break;
  204. }
  205. }
  206. return [result, compilationData[sectionString]];
  207. /* 数据库获取编办
  208. let condition = {_id: versionId};
  209. let childCondition = {};
  210. childCondition[sectionString] = {$elemMatch: {_id: id}};
  211. let result = await this.db.findOne(condition, childCondition);
  212. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  213. */
  214. }
  215. /**
  216. * 删除计价规则
  217. *
  218. * @param {String} compilationId
  219. * @param {String} valuationId
  220. * @param {String} section
  221. * @return {Promise}
  222. */
  223. async deleteValuation(compilationId, valuationId, section) {
  224. let condition = {_id: compilationId};
  225. let sectionString = section + '_valuation';
  226. let deleteData = {};
  227. deleteData[sectionString] = {id: valuationId};
  228. // 利用pull删除嵌套数据
  229. let result = await this.db.deleteSet(condition, deleteData);
  230. return result !== null && result.ok === 1;
  231. }
  232. /**
  233. * 发布编办
  234. *
  235. * @param {String} id
  236. * @param {Number} status
  237. * @return {Promise}
  238. */
  239. async release(id, status) {
  240. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  241. if (status) {
  242. let compilationData = await this.findDataByCondition({_id: id});
  243. // 最少需要有一个计价规则存在
  244. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  245. throw '至少需要一个计价规则';
  246. }
  247. // 判断是否全部禁止
  248. let hasEnable = false;
  249. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  250. for (let valuation of valuationList) {
  251. if (valuation.enable) {
  252. hasEnable = true;
  253. break;
  254. }
  255. }
  256. if (!hasEnable) {
  257. throw '至少需要一个可用的计价规则';
  258. }
  259. }
  260. let updateData = {
  261. is_release: status === 1,
  262. release_time: new Date().getTime()
  263. };
  264. return this.updateById(id, updateData);
  265. }
  266. /**
  267. * 根据专业工程获取对应专业工程标准库id
  268. *
  269. * @param {String} valuationId
  270. * @param {String} section
  271. * @param {Number} engineering
  272. * @return {Promise}
  273. */
  274. async getEngineeringLib(valuationId, section, engineering) {
  275. let sectionString = section + "_valuation";
  276. let condition = {};
  277. condition[sectionString + ".id"] = valuationId;
  278. let compilationData = await this.findDataByCondition(condition);
  279. if (compilationData === null) {
  280. throw '不存在对应编办';
  281. }
  282. let valuationData = null;
  283. for(let valuation of compilationData[sectionString]) {
  284. if(valuation.id === valuationId) {
  285. valuationData = valuation;
  286. }
  287. }
  288. if (valuationData === null) {
  289. throw '不存在对应计价规则';
  290. }
  291. // 判断是否已有对应数据
  292. let engineeringList = valuationData.engineering_list;
  293. let engineeringLib = null;
  294. for(let tmpEngineering of engineeringList) {
  295. if (tmpEngineering.engineering === engineering) {
  296. engineeringLib = tmpEngineering;
  297. break;
  298. }
  299. }
  300. return engineeringLib;
  301. }
  302. /**
  303. * 新增专业工程标准库
  304. *
  305. * @param {String} valuationId
  306. * @param {String} section
  307. * @param {Object} data
  308. * @return {Promise}
  309. */
  310. async addEngineeringLib(valuationId, section, data) {
  311. let sectionString = section + "_valuation";
  312. let condition = {};
  313. condition[sectionString + ".id"] = valuationId;
  314. let insertData = {};
  315. insertData[sectionString + ".$.engineering_list"] = data;
  316. let result = await this.db.addToSet(condition, insertData);
  317. return result.ok === 1;
  318. }
  319. /*
  320. * 设置CLD办事处信息
  321. *
  322. * @param {String} compilationId
  323. * @param {int} category
  324. * @return {Promise}
  325. * */
  326. async updateCategory(compilationId, category) {
  327. return await this.updateById(compilationId, {categoryID: category});
  328. }
  329. }
  330. export default CompilationModel;