compilation_model.js 15 KB

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