compilation_model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 (projId && projId.trim() !== "") data.push(projId.trim());
  231. }
  232. }
  233. return await this.updateById(compilationId, { example: data });
  234. }
  235. /**
  236. * 新增计价规则
  237. *
  238. * @param {String} id
  239. * @param {String} section
  240. * @param {Object} data
  241. * @return {Promise}
  242. */
  243. async addValuation(id, section, data) {
  244. let condition = { _id: id };
  245. let compilationData = await this.findDataByCondition(condition);
  246. if (compilationData === null || compilationData.name === undefined) {
  247. throw "没有找到对应的数据";
  248. }
  249. if (data.name === undefined || data.name === "") {
  250. throw "计价规则名称为空";
  251. }
  252. if (this.sectionList.indexOf(section) < 0) {
  253. console.log("2");
  254. throw "数据有误";
  255. }
  256. let insertData = {};
  257. data.id = uuidV1();
  258. insertData[section + "_valuation"] = data;
  259. let result = await this.db.addToSet(condition, insertData);
  260. return result.ok === undefined ? false : data.id;
  261. }
  262. /**
  263. * 保存计价数据
  264. *
  265. * @param {String} valuationId
  266. * @param {Object} data
  267. * @return {Promise}
  268. */
  269. async saveValuation(valuationId, data) {
  270. data = this._filterValuationData(data);
  271. let sectionString = data.section + "_valuation";
  272. let condition = {};
  273. condition[sectionString + ".id"] = valuationId;
  274. let updateData = {};
  275. updateData[sectionString + ".$.name"] = data.valuationName;
  276. let result = await this.db.update(condition, updateData);
  277. return result !== null && result.ok === 1;
  278. }
  279. /**
  280. * 更改启用/禁用
  281. *
  282. * @param {String} valuationId
  283. * @param {String} section
  284. * @param {String} enable
  285. * @return {Promise}
  286. */
  287. async switchEnable(valuationId, section, enable) {
  288. let sectionString = section + "_valuation";
  289. let condition = {};
  290. condition[sectionString + ".id"] = valuationId;
  291. let updateData = {};
  292. updateData[sectionString + ".$.enable"] = enable === "true";
  293. let result = await this.db.update(condition, updateData);
  294. return result !== null && result.ok === 1;
  295. }
  296. /**
  297. * 设置计价规则适用类型
  298. *
  299. * @param {String} valuationId
  300. * @param {String} section
  301. * @param {String} setFileTypes
  302. * @return {Promise}
  303. */
  304. async setFileTypes(valuationId, section, fileTypes) {
  305. let sectionString = section + "_valuation";
  306. let condition = {};
  307. condition[sectionString + ".id"] = valuationId;
  308. let updateData = {};
  309. updateData[sectionString + ".$.fileTypes"] = fileTypes;
  310. let result = await this.db.update(condition, updateData);
  311. return result !== null && result.ok === 1;
  312. }
  313. /**
  314. * 过滤计价数据
  315. *
  316. * @param {Object} data
  317. * @return {Object}
  318. */
  319. _filterValuationData(data) {
  320. if (Object.keys(data).length <= 0) {
  321. console.log("3");
  322. throw "数据有误";
  323. }
  324. if (
  325. data.section === undefined ||
  326. data.section === "" ||
  327. this.sectionList.indexOf(data.section) < 0
  328. ) {
  329. throw "类型错误";
  330. }
  331. // 判断名称
  332. if (data.valuationName === undefined || data.valuationName === "") {
  333. throw "名称不能为空";
  334. }
  335. return data;
  336. }
  337. /**
  338. * 获取计价规则数据
  339. *
  340. * @param {String} compilationId
  341. * @param {String} id
  342. * @param {String} section
  343. * @return {Promise|Array}
  344. */
  345. async getValuation(compilationId, id, section) {
  346. if (this.sectionList.indexOf(section) < 0) {
  347. throw "数据有误";
  348. }
  349. let compilationData = await this.findDataByCondition({
  350. _id: compilationId,
  351. });
  352. if (Object.keys(compilationData).length <= 0) {
  353. throw "编办数据有误";
  354. }
  355. let result = {};
  356. let sectionString = section + "_valuation";
  357. for (let valuation of compilationData[sectionString]) {
  358. if (valuation.id.toString() === id) {
  359. result = valuation;
  360. break;
  361. }
  362. }
  363. return [result, compilationData[sectionString]];
  364. /* 数据库获取编办
  365. let condition = {_id: versionId};
  366. let childCondition = {};
  367. childCondition[sectionString] = {$elemMatch: {_id: id}};
  368. let result = await this.db.findOne(condition, childCondition);
  369. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  370. */
  371. }
  372. /**
  373. * 删除计价规则
  374. *
  375. * @param {String} compilationId
  376. * @param {String} valuationId
  377. * @param {String} section
  378. * @return {Promise}
  379. */
  380. async deleteValuation(compilationId, valuationId, section) {
  381. let condition = { _id: compilationId };
  382. let sectionString = section + "_valuation";
  383. let deleteData = {};
  384. deleteData[sectionString] = { id: valuationId };
  385. // 利用pull删除嵌套数据
  386. let result = await this.db.deleteSet(condition, deleteData);
  387. return result !== null && result.ok === 1;
  388. }
  389. /**
  390. * 发布编办
  391. *
  392. * @param {String} id
  393. * @param {Number} status
  394. * @return {Promise}
  395. */
  396. async release(id, status) {
  397. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  398. if (status) {
  399. let compilationData = await this.findDataByCondition({ _id: id });
  400. // 最少需要有一个计价规则存在
  401. if (
  402. compilationData.bill_valuation.length <= 0 &&
  403. compilationData.ration_valuation.length <= 0
  404. ) {
  405. throw "至少需要一个计价规则";
  406. }
  407. // 判断是否全部禁止
  408. let hasEnable = false;
  409. let valuationList = compilationData.bill_valuation.concat(
  410. compilationData.ration_valuation
  411. );
  412. for (let valuation of valuationList) {
  413. if (valuation.enable) {
  414. hasEnable = true;
  415. break;
  416. }
  417. }
  418. if (!hasEnable) {
  419. throw "至少需要一个可用的计价规则";
  420. }
  421. }
  422. let updateData = {
  423. is_release: status === 1,
  424. release_time: new Date().getTime(),
  425. };
  426. return this.updateById(id, updateData);
  427. }
  428. /**
  429. * 根据专业工程获取对应专业工程标准库id
  430. *
  431. * @param {String} valuationId
  432. * @param {String} section
  433. * @param {Number} engineering
  434. * @return {Promise}
  435. */
  436. async getEngineeringLib(valuationId, section, engineering) {
  437. let sectionString = section + "_valuation";
  438. let condition = {};
  439. condition[sectionString + ".id"] = valuationId;
  440. let compilationData = await this.findDataByCondition(condition);
  441. if (compilationData === null) {
  442. throw "不存在对应编办";
  443. }
  444. let valuationData = null;
  445. for (let valuation of compilationData[sectionString]) {
  446. if (valuation.id === valuationId) {
  447. valuationData = valuation;
  448. }
  449. }
  450. if (valuationData === null) {
  451. throw "不存在对应计价规则";
  452. }
  453. // 判断是否已有对应数据
  454. let engineeringList = valuationData.engineering_list;
  455. let engineeringLib = null;
  456. for (let tmpEngineering of engineeringList) {
  457. if (tmpEngineering.engineering === engineering) {
  458. engineeringLib = tmpEngineering;
  459. break;
  460. }
  461. }
  462. return engineeringLib;
  463. }
  464. /**
  465. * 新增专业工程标准库
  466. *
  467. * @param {String} valuationId
  468. * @param {String} section
  469. * @param {Object} data
  470. * @return {Promise}
  471. */
  472. async addEngineeringLib(valuationId, section, data) {
  473. let sectionString = section + "_valuation";
  474. let condition = {};
  475. condition[sectionString + ".id"] = valuationId;
  476. let insertData = {};
  477. insertData[sectionString + ".$.engineering_list"] = data;
  478. let result = await this.db.addToSet(condition, insertData);
  479. return result.ok === 1;
  480. }
  481. /*
  482. * 设置CLD办事处信息
  483. *
  484. * @param {String} compilationId
  485. * @param {int} category
  486. * @return {Promise}
  487. * */
  488. async updateCategory(compilationId, category) {
  489. return await this.updateById(compilationId, { categoryID: category });
  490. }
  491. /*
  492. * 设置工程默认所在地
  493. *
  494. * @param {String} compilationId
  495. * @param {int} location
  496. * @return {Promise}
  497. * */
  498. async updateLocation(compilationId, location) {
  499. return await this.updateById(compilationId, { defaultLocation: location });
  500. }
  501. /*
  502. * 设置是否提供免费版
  503. *
  504. * @param {String} compilationId
  505. * @param {int} location
  506. * @return {Promise}
  507. * */
  508. async updateFreeUse(compilationId, freeUse) {
  509. return await this.updateById(compilationId, { freeUse: freeUse });
  510. }
  511. // 拷贝计价规则
  512. async copyValuation(compilationID, valuationType, orgValuationID, newName) {
  513. const objectId = mongoose.Types.ObjectId(compilationID);
  514. const compilationData = await compilationModel
  515. .findOne({ _id: objectId })
  516. .lean();
  517. const orgValuation = compilationData[valuationType].find(
  518. (item) => item.id === orgValuationID
  519. );
  520. if (!orgValuation) {
  521. throw "不存在对应计价规则";
  522. }
  523. const newValuation = {
  524. ...orgValuation,
  525. id: uuidV1(),
  526. enable: false,
  527. name: newName,
  528. };
  529. await compilationModel.update(
  530. { _id: objectId },
  531. { $push: { [valuationType]: newValuation } }
  532. );
  533. await this.copyEngineeringList(orgValuationID, newValuation.id);
  534. }
  535. // 拷贝工程专业
  536. async copyEngineeringList(orgValuationID, newValuationID) {
  537. const engineeringList = await engineeringModel
  538. .find({ valuationID: orgValuationID }, "-_id")
  539. .lean();
  540. console.log(engineeringList);
  541. const newEngineeringList = engineeringList.map((item) => ({
  542. ...item,
  543. valuationID: newValuationID,
  544. }));
  545. if (newEngineeringList.length) {
  546. await engineeringModel.insertMany(newEngineeringList);
  547. }
  548. }
  549. }
  550. export default CompilationModel;