compilation_model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. data.push(parseInt(projId));
  154. }
  155. }
  156. return await this.updateById(compilationId, {example: data});
  157. }
  158. /**
  159. * 新增计价规则
  160. *
  161. * @param {String} id
  162. * @param {String} section
  163. * @param {Object} data
  164. * @return {Promise}
  165. */
  166. async addValuation(id, section, data) {
  167. let condition = {_id: id};
  168. let compilationData = await this.findDataByCondition(condition);
  169. if (compilationData === null || compilationData.name === undefined) {
  170. throw '没有找到对应的数据';
  171. }
  172. if (data.name === undefined || data.name === '') {
  173. throw '计价规则名称为空';
  174. }
  175. if (this.sectionList.indexOf(section) < 0) {
  176. console.log('2');
  177. throw '数据有误';
  178. }
  179. let insertData = {};
  180. data.id = uuidV1();
  181. insertData[section + '_valuation'] = data;
  182. let result = await this.db.addToSet(condition, insertData);
  183. return result.ok === undefined ? false : data.id;
  184. }
  185. /**
  186. * 保存计价数据
  187. *
  188. * @param {String} valuationId
  189. * @param {Object} data
  190. * @return {Promise}
  191. */
  192. async saveValuation(valuationId, data) {
  193. data = this._filterValuationData(data);
  194. let sectionString = data.section + "_valuation";
  195. let condition = {};
  196. condition[sectionString + ".id"] = valuationId;
  197. let updateData = {};
  198. updateData[sectionString + ".$.name"] = data.valuationName;
  199. let result = await this.db.update(condition, updateData);
  200. return result !== null && result.ok === 1;
  201. };
  202. /**
  203. * 更改启用/禁用
  204. *
  205. * @param {String} valuationId
  206. * @param {String} section
  207. * @param {String} enable
  208. * @return {Promise}
  209. */
  210. async switchEnable(valuationId, section, enable) {
  211. let sectionString = section + "_valuation";
  212. let condition = {};
  213. condition[sectionString + ".id"] = valuationId;
  214. let updateData = {};
  215. updateData[sectionString + ".$.enable"] = enable === "true";
  216. let result = await this.db.update(condition, updateData);
  217. return result !== null && result.ok === 1;
  218. }
  219. /**
  220. * 设置计价规则适用类型
  221. *
  222. * @param {String} valuationId
  223. * @param {String} section
  224. * @param {String} setFileTypes
  225. * @return {Promise}
  226. */
  227. async setFileTypes(valuationId, section, fileTypes) {
  228. let sectionString = section + "_valuation";
  229. let condition = {};
  230. condition[sectionString + ".id"] = valuationId;
  231. let updateData = {};
  232. updateData[sectionString + ".$.fileTypes"] = fileTypes;
  233. let result = await this.db.update(condition, updateData);
  234. return result !== null && result.ok === 1;
  235. }
  236. /**
  237. * 过滤计价数据
  238. *
  239. * @param {Object} data
  240. * @return {Object}
  241. */
  242. _filterValuationData(data) {
  243. if (Object.keys(data).length <= 0) {
  244. console.log('3');
  245. throw '数据有误';
  246. }
  247. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  248. throw '类型错误';
  249. }
  250. // 判断名称
  251. if (data.valuationName === undefined || data.valuationName === '') {
  252. throw '名称不能为空';
  253. }
  254. return data;
  255. }
  256. /**
  257. * 获取计价规则数据
  258. *
  259. * @param {String} compilationId
  260. * @param {String} id
  261. * @param {String} section
  262. * @return {Promise|Array}
  263. */
  264. async getValuation(compilationId, id, section) {
  265. if (this.sectionList.indexOf(section) < 0) {
  266. throw '数据有误';
  267. }
  268. let compilationData = await this.findDataByCondition({_id: compilationId});
  269. if (Object.keys(compilationData).length <= 0) {
  270. throw '编办数据有误';
  271. }
  272. let result = {};
  273. let sectionString = section + '_valuation';
  274. for(let valuation of compilationData[sectionString]) {
  275. if (valuation.id.toString() === id) {
  276. result = valuation;
  277. break;
  278. }
  279. }
  280. return [result, compilationData[sectionString]];
  281. /* 数据库获取编办
  282. let condition = {_id: versionId};
  283. let childCondition = {};
  284. childCondition[sectionString] = {$elemMatch: {_id: id}};
  285. let result = await this.db.findOne(condition, childCondition);
  286. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  287. */
  288. }
  289. /**
  290. * 删除计价规则
  291. *
  292. * @param {String} compilationId
  293. * @param {String} valuationId
  294. * @param {String} section
  295. * @return {Promise}
  296. */
  297. async deleteValuation(compilationId, valuationId, section) {
  298. let condition = {_id: compilationId};
  299. let sectionString = section + '_valuation';
  300. let deleteData = {};
  301. deleteData[sectionString] = {id: valuationId};
  302. // 利用pull删除嵌套数据
  303. let result = await this.db.deleteSet(condition, deleteData);
  304. return result !== null && result.ok === 1;
  305. }
  306. /**
  307. * 发布编办
  308. *
  309. * @param {String} id
  310. * @param {Number} status
  311. * @return {Promise}
  312. */
  313. async release(id, status) {
  314. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  315. if (status) {
  316. let compilationData = await this.findDataByCondition({_id: id});
  317. // 最少需要有一个计价规则存在
  318. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  319. throw '至少需要一个计价规则';
  320. }
  321. // 判断是否全部禁止
  322. let hasEnable = false;
  323. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  324. for (let valuation of valuationList) {
  325. if (valuation.enable) {
  326. hasEnable = true;
  327. break;
  328. }
  329. }
  330. if (!hasEnable) {
  331. throw '至少需要一个可用的计价规则';
  332. }
  333. }
  334. let updateData = {
  335. is_release: status === 1,
  336. release_time: new Date().getTime()
  337. };
  338. return this.updateById(id, updateData);
  339. }
  340. /**
  341. * 根据专业工程获取对应专业工程标准库id
  342. *
  343. * @param {String} valuationId
  344. * @param {String} section
  345. * @param {Number} engineering
  346. * @return {Promise}
  347. */
  348. async getEngineeringLib(valuationId, section, engineering) {
  349. let sectionString = section + "_valuation";
  350. let condition = {};
  351. condition[sectionString + ".id"] = valuationId;
  352. let compilationData = await this.findDataByCondition(condition);
  353. if (compilationData === null) {
  354. throw '不存在对应编办';
  355. }
  356. let valuationData = null;
  357. for(let valuation of compilationData[sectionString]) {
  358. if(valuation.id === valuationId) {
  359. valuationData = valuation;
  360. }
  361. }
  362. if (valuationData === null) {
  363. throw '不存在对应计价规则';
  364. }
  365. // 判断是否已有对应数据
  366. let engineeringList = valuationData.engineering_list;
  367. let engineeringLib = null;
  368. for(let tmpEngineering of engineeringList) {
  369. if (tmpEngineering.engineering === engineering) {
  370. engineeringLib = tmpEngineering;
  371. break;
  372. }
  373. }
  374. return engineeringLib;
  375. }
  376. /**
  377. * 新增专业工程标准库
  378. *
  379. * @param {String} valuationId
  380. * @param {String} section
  381. * @param {Object} data
  382. * @return {Promise}
  383. */
  384. async addEngineeringLib(valuationId, section, data) {
  385. let sectionString = section + "_valuation";
  386. let condition = {};
  387. condition[sectionString + ".id"] = valuationId;
  388. let insertData = {};
  389. insertData[sectionString + ".$.engineering_list"] = data;
  390. let result = await this.db.addToSet(condition, insertData);
  391. return result.ok === 1;
  392. }
  393. /*
  394. * 设置CLD办事处信息
  395. *
  396. * @param {String} compilationId
  397. * @param {int} category
  398. * @return {Promise}
  399. * */
  400. async updateCategory(compilationId, category) {
  401. return await this.updateById(compilationId, {categoryID: category});
  402. }
  403. // 拷贝计价规则
  404. async copyValuation(compilationID, valuationType, orgValuationID, newName) {
  405. const objectId = mongoose.Types.ObjectId(compilationID);
  406. const compilationData = await compilationModel.findOne({_id: objectId }).lean();
  407. const orgValuation = compilationData[valuationType].find(item => item.id === orgValuationID);
  408. if (!orgValuation) {
  409. throw '不存在对应计价规则';
  410. }
  411. const newValuation = {
  412. ...orgValuation,
  413. id: uuidV1(),
  414. enable: false,
  415. name: newName
  416. };
  417. await compilationModel.update({ _id: objectId }, { $push: { [valuationType]: newValuation } });
  418. await this.copyEngineeringList(orgValuationID, newValuation.id);
  419. }
  420. // 拷贝工程专业
  421. async copyEngineeringList(orgValuationID, newValuationID) {
  422. const engineeringList = await engineeringModel.find({ valuationID: orgValuationID }, '-_id').lean();
  423. console.log(engineeringList);
  424. const newEngineeringList = engineeringList.map(item => ({ ...item, valuationID: newValuationID }));
  425. if (newEngineeringList.length) {
  426. await engineeringModel.insertMany(newEngineeringList);
  427. }
  428. }
  429. }
  430. export default CompilationModel;