compilation_model.js 16 KB

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