compilation_model.js 15 KB

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