compilation_model.js 16 KB

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