compilation_model.js 16 KB

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