compilation_model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. const engineeringModel = mongoose.model('engineering_lib');
  12. const compilationModel = mongoose.model('compilation');
  13. class CompilationModel extends BaseModel {
  14. /**
  15. * 允许的块
  16. *
  17. * @var {Array}
  18. */
  19. sectionList = ['bill', 'ration'];
  20. /**
  21. * 构造函数
  22. *
  23. * @return {void}
  24. */
  25. constructor() {
  26. let parent = super();
  27. parent.model = mongoose.model('compilation');
  28. parent.init();
  29. }
  30. /**
  31. * 获取编办列表
  32. *
  33. * @return {Promise}
  34. */
  35. async getCompilationList(fields = null) {
  36. // 筛选字段
  37. let field = fields == null ?{_id: 1, name: 1, is_release: 1, release_time:1,categoryID: 1, description: 1,overWriteUrl: 1,example: 1,edition: 1, "ration_valuation.id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
  38. "bill_valuation.id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1, "bill_valuation.fileTypes": 1}:fields;
  39. let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);
  40. return compilationData === null ? [] : compilationData;
  41. }
  42. /**
  43. * 获取编办列表
  44. *
  45. * @return {Promise}
  46. */
  47. async getList() {
  48. // 筛选字段
  49. let field = {_id: 1, name: 1, is_release: 1, description: 1, categoryID: 1};
  50. let compilationData = await this.findDataByCondition({name: {$ne: ''}, is_release: true}, field, false);
  51. return compilationData === null ? [] : compilationData;
  52. }
  53. /**
  54. * 根据id获取可用的编办数据
  55. *
  56. * @param {String} id
  57. * @return {Promise}
  58. */
  59. async getCompilationById(id) {
  60. let condition = {_id: id, is_release: true};
  61. let compilationData = await this.findDataByCondition(condition);
  62. if (!compilationData || compilationData.bill_valuation === undefined) {
  63. return compilationData;
  64. }
  65. if (compilationData.bill_valuation.length > 0) {
  66. let enableValuation = [];
  67. for (let index in compilationData.bill_valuation) {
  68. if (compilationData.bill_valuation[index].enable) {
  69. enableValuation.push(compilationData.bill_valuation[index]);
  70. }
  71. }
  72. compilationData.bill_valuation = enableValuation;
  73. }
  74. if (compilationData.ration_valuation.length > 0) {
  75. let enableValuation = [];
  76. for (let index in compilationData.ration_valuation) {
  77. if (compilationData.ration_valuation[index].enable) {
  78. enableValuation.push(compilationData.bill_valuation[index]);
  79. }
  80. }
  81. compilationData.ration_valuation = enableValuation;
  82. }
  83. return compilationData;
  84. }
  85. /**
  86. * 设置场景
  87. *
  88. * @param {string} scene
  89. * @return {void}
  90. */
  91. setScene(scene = '') {
  92. switch (scene) {
  93. // 新增
  94. case 'add':
  95. this.model.schema.path('name').required(true);
  96. this.model.schema.path('creator').required(true);
  97. this.model.schema.path('create_time').required(true);
  98. break;
  99. }
  100. }
  101. /**
  102. * 新增编办
  103. *
  104. * @param {Object} data
  105. * @return {Promise}
  106. */
  107. async add(data) {
  108. let result = false;
  109. if (Object.keys(data).length <= 0) {
  110. return result;
  111. }
  112. this.setScene('add');
  113. data.create_time = new Date().getTime();
  114. result = this.db.create(data);
  115. return result;
  116. }
  117. /*
  118. * 设置版本描述
  119. *
  120. * @param {String} compilationId
  121. * @param {String} description
  122. * @return {Promise}
  123. * */
  124. async setDescription(compilationId, description){
  125. return await this.updateById(compilationId, {description: description});
  126. }
  127. /*
  128. * 设置版本号
  129. *
  130. * @param {String} compilationId
  131. * @param {String} edition
  132. * @return {Promise}
  133. * */
  134. async setEdition(compilationId, edition){
  135. return await this.updateById(compilationId, {edition: edition});
  136. }
  137. /*
  138. 设置代码覆盖路径
  139. */
  140. async setOverWriteUrl(compilationId, overWriteUrl, priceProp, consumeAmtProp){
  141. return await this.updateById(compilationId, {overWriteUrl: overWriteUrl, priceProperties: priceProp, consumeAmtProperties: consumeAmtProp});
  142. }
  143. /*
  144. * 设置例题
  145. * @param {String} compilationId
  146. * @param {Array} example
  147. * @return {Promise}
  148. * */
  149. async setExample(compilationId, example) {
  150. let data = [];
  151. if (example) {
  152. for (let projId of example) {
  153. if (/^[0-9]+$/.test(projId)) {
  154. data.push(parseInt(projId));
  155. } else if (projId) {
  156. data.push(projId);
  157. }
  158. }
  159. }
  160. return await this.updateById(compilationId, {example: data});
  161. }
  162. /**
  163. * 新增计价规则
  164. *
  165. * @param {String} id
  166. * @param {String} section
  167. * @param {Object} data
  168. * @return {Promise}
  169. */
  170. async addValuation(id, section, data) {
  171. let condition = {_id: id};
  172. let compilationData = await this.findDataByCondition(condition);
  173. if (compilationData === null || compilationData.name === undefined) {
  174. throw '没有找到对应的数据';
  175. }
  176. if (data.name === undefined || data.name === '') {
  177. throw '计价规则名称为空';
  178. }
  179. if (this.sectionList.indexOf(section) < 0) {
  180. console.log('2');
  181. throw '数据有误';
  182. }
  183. let insertData = {};
  184. data.id = uuidV1();
  185. insertData[section + '_valuation'] = data;
  186. let result = await this.db.addToSet(condition, insertData);
  187. return result.ok === undefined ? false : data.id;
  188. }
  189. /**
  190. * 保存计价数据
  191. *
  192. * @param {String} valuationId
  193. * @param {Object} data
  194. * @return {Promise}
  195. */
  196. async saveValuation(valuationId, data) {
  197. data = this._filterValuationData(data);
  198. let sectionString = data.section + "_valuation";
  199. let condition = {};
  200. condition[sectionString + ".id"] = valuationId;
  201. let updateData = {};
  202. updateData[sectionString + ".$.name"] = data.valuationName;
  203. let result = await this.db.update(condition, updateData);
  204. return result !== null && result.ok === 1;
  205. };
  206. /**
  207. * 更改启用/禁用
  208. *
  209. * @param {String} valuationId
  210. * @param {String} section
  211. * @param {String} enable
  212. * @return {Promise}
  213. */
  214. async switchEnable(valuationId, section, enable) {
  215. let sectionString = section + "_valuation";
  216. let condition = {};
  217. condition[sectionString + ".id"] = valuationId;
  218. let updateData = {};
  219. updateData[sectionString + ".$.enable"] = enable === "true";
  220. let result = await this.db.update(condition, updateData);
  221. return result !== null && result.ok === 1;
  222. }
  223. /**
  224. * 设置计价规则适用类型
  225. *
  226. * @param {String} valuationId
  227. * @param {String} section
  228. * @param {String} setFileTypes
  229. * @return {Promise}
  230. */
  231. async setFileTypes(valuationId, section, fileTypes) {
  232. let sectionString = section + "_valuation";
  233. let condition = {};
  234. condition[sectionString + ".id"] = valuationId;
  235. let updateData = {};
  236. updateData[sectionString + ".$.fileTypes"] = fileTypes;
  237. let result = await this.db.update(condition, updateData);
  238. return result !== null && result.ok === 1;
  239. }
  240. /**
  241. * 过滤计价数据
  242. *
  243. * @param {Object} data
  244. * @return {Object}
  245. */
  246. _filterValuationData(data) {
  247. if (Object.keys(data).length <= 0) {
  248. console.log('3');
  249. throw '数据有误';
  250. }
  251. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  252. throw '类型错误';
  253. }
  254. // 判断名称
  255. if (data.valuationName === undefined || data.valuationName === '') {
  256. throw '名称不能为空';
  257. }
  258. return data;
  259. }
  260. /**
  261. * 获取计价规则数据
  262. *
  263. * @param {String} compilationId
  264. * @param {String} id
  265. * @param {String} section
  266. * @return {Promise|Array}
  267. */
  268. async getValuation(compilationId, id, section) {
  269. if (this.sectionList.indexOf(section) < 0) {
  270. throw '数据有误';
  271. }
  272. let compilationData = await this.findDataByCondition({_id: compilationId});
  273. if (Object.keys(compilationData).length <= 0) {
  274. throw '编办数据有误';
  275. }
  276. let result = {};
  277. let sectionString = section + '_valuation';
  278. for(let valuation of compilationData[sectionString]) {
  279. if (valuation.id.toString() === id) {
  280. result = valuation;
  281. break;
  282. }
  283. }
  284. return [result, compilationData[sectionString]];
  285. /* 数据库获取编办
  286. let condition = {_id: versionId};
  287. let childCondition = {};
  288. childCondition[sectionString] = {$elemMatch: {_id: id}};
  289. let result = await this.db.findOne(condition, childCondition);
  290. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  291. */
  292. }
  293. /**
  294. * 删除计价规则
  295. *
  296. * @param {String} compilationId
  297. * @param {String} valuationId
  298. * @param {String} section
  299. * @return {Promise}
  300. */
  301. async deleteValuation(compilationId, valuationId, section) {
  302. let condition = {_id: compilationId};
  303. let sectionString = section + '_valuation';
  304. let deleteData = {};
  305. deleteData[sectionString] = {id: valuationId};
  306. // 利用pull删除嵌套数据
  307. let result = await this.db.deleteSet(condition, deleteData);
  308. return result !== null && result.ok === 1;
  309. }
  310. /**
  311. * 发布编办
  312. *
  313. * @param {String} id
  314. * @param {Number} status
  315. * @return {Promise}
  316. */
  317. async release(id, status) {
  318. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  319. if (status) {
  320. let compilationData = await this.findDataByCondition({_id: id});
  321. // 最少需要有一个计价规则存在
  322. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  323. throw '至少需要一个计价规则';
  324. }
  325. // 判断是否全部禁止
  326. let hasEnable = false;
  327. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  328. for (let valuation of valuationList) {
  329. if (valuation.enable) {
  330. hasEnable = true;
  331. break;
  332. }
  333. }
  334. if (!hasEnable) {
  335. throw '至少需要一个可用的计价规则';
  336. }
  337. }
  338. let updateData = {
  339. is_release: status === 1,
  340. release_time: new Date().getTime()
  341. };
  342. return this.updateById(id, updateData);
  343. }
  344. /**
  345. * 根据专业工程获取对应专业工程标准库id
  346. *
  347. * @param {String} valuationId
  348. * @param {String} section
  349. * @param {Number} engineering
  350. * @return {Promise}
  351. */
  352. async getEngineeringLib(valuationId, section, engineering) {
  353. let sectionString = section + "_valuation";
  354. let condition = {};
  355. condition[sectionString + ".id"] = valuationId;
  356. let compilationData = await this.findDataByCondition(condition);
  357. if (compilationData === null) {
  358. throw '不存在对应编办';
  359. }
  360. let valuationData = null;
  361. for(let valuation of compilationData[sectionString]) {
  362. if(valuation.id === valuationId) {
  363. valuationData = valuation;
  364. }
  365. }
  366. if (valuationData === null) {
  367. throw '不存在对应计价规则';
  368. }
  369. // 判断是否已有对应数据
  370. let engineeringList = valuationData.engineering_list;
  371. let engineeringLib = null;
  372. for(let tmpEngineering of engineeringList) {
  373. if (tmpEngineering.engineering === engineering) {
  374. engineeringLib = tmpEngineering;
  375. break;
  376. }
  377. }
  378. return engineeringLib;
  379. }
  380. /**
  381. * 新增专业工程标准库
  382. *
  383. * @param {String} valuationId
  384. * @param {String} section
  385. * @param {Object} data
  386. * @return {Promise}
  387. */
  388. async addEngineeringLib(valuationId, section, data) {
  389. let sectionString = section + "_valuation";
  390. let condition = {};
  391. condition[sectionString + ".id"] = valuationId;
  392. let insertData = {};
  393. insertData[sectionString + ".$.engineering_list"] = data;
  394. let result = await this.db.addToSet(condition, insertData);
  395. return result.ok === 1;
  396. }
  397. /*
  398. * 设置CLD办事处信息
  399. *
  400. * @param {String} compilationId
  401. * @param {int} category
  402. * @return {Promise}
  403. * */
  404. async updateCategory(compilationId, category) {
  405. return await this.updateById(compilationId, {categoryID: category});
  406. }
  407. // 拷贝计价规则
  408. async copyValuation(compilationID, valuationType, orgValuationID, newName) {
  409. const objectId = mongoose.Types.ObjectId(compilationID);
  410. const compilationData = await compilationModel.findOne({_id: objectId }).lean();
  411. const orgValuation = compilationData[valuationType].find(item => item.id === orgValuationID);
  412. if (!orgValuation) {
  413. throw '不存在对应计价规则';
  414. }
  415. const newValuation = {
  416. ...orgValuation,
  417. id: uuidV1(),
  418. enable: false,
  419. name: newName
  420. };
  421. await compilationModel.update({ _id: objectId }, { $push: { [valuationType]: newValuation } });
  422. await this.copyEngineeringList(orgValuationID, newValuation.id);
  423. }
  424. // 拷贝工程专业
  425. async copyEngineeringList(orgValuationID, newValuationID) {
  426. const engineeringList = await engineeringModel.find({ valuationID: orgValuationID }, '-_id').lean();
  427. console.log(engineeringList);
  428. const newEngineeringList = engineeringList.map(item => ({ ...item, valuationID: newValuationID }));
  429. if (newEngineeringList.length) {
  430. await engineeringModel.insertMany(newEngineeringList);
  431. }
  432. }
  433. }
  434. export default CompilationModel;