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. class CompilationModel extends BaseModel {
  12. /**
  13. * 允许的块
  14. *
  15. * @var {Array}
  16. */
  17. sectionList = [
  18. "suggestion",
  19. "feasibility",
  20. "rough",
  21. "bill",
  22. "ration",
  23. "estimation",
  24. ];
  25. /**
  26. * 构造函数
  27. *
  28. * @return {void}
  29. */
  30. constructor() {
  31. let parent = super();
  32. parent.model = mongoose.model("compilation");
  33. parent.init();
  34. }
  35. /**
  36. * 获取编办列表
  37. *
  38. * @return {Promise}
  39. */
  40. async getCompilationList(fields = null) {
  41. // 筛选字段
  42. let field =
  43. fields == null
  44. ? {
  45. _id: 1,
  46. name: 1,
  47. is_release: 1,
  48. release_time: 1,
  49. defaultLocation: 1,
  50. categoryID: 1,
  51. description: 1,
  52. overWriteUrl: 1,
  53. example: 1,
  54. edition: 1,
  55. freeUse: 1,
  56. customMade: 1,
  57. compilationArea: 1,
  58. "ration_valuation.id": 1,
  59. "ration_valuation.name": 1,
  60. "ration_valuation.enable": 1,
  61. "suggestion_valuation.id": 1,
  62. "suggestion_valuation.name": 1,
  63. "suggestion_valuation.enable": 1,
  64. "feasibility_valuation.id": 1,
  65. "feasibility_valuation.name": 1,
  66. "feasibility_valuation.enable": 1,
  67. "rough_valuation.id": 1,
  68. "rough_valuation.name": 1,
  69. "rough_valuation.enable": 1,
  70. "bill_valuation.id": 1,
  71. "bill_valuation.name": 1,
  72. "bill_valuation.enable": 1,
  73. "estimation_valuation.id": 1,
  74. "estimation_valuation.name": 1,
  75. "estimation_valuation.enable": 1,
  76. "bill_valuation.fileTypes": 1,
  77. type: 1,
  78. }
  79. : fields;
  80. // 一定要查询序号,然后排序输出
  81. field.serialNumber = 1;
  82. let compilationData = await this.findDataByCondition(
  83. { name: { $ne: "" } },
  84. field,
  85. false
  86. );
  87. if (compilationData) {
  88. // 按照序号排序
  89. compilationData.sort((aa, bb) => {
  90. return (aa.serialNumber || 99999) - (bb.serialNumber || 99999);
  91. });
  92. return compilationData;
  93. }
  94. return [];
  95. }
  96. // 根据用户权限过滤编办
  97. async getPermissionCompilationList(req, fields = null) {
  98. const compilationPermission =
  99. req.session.managerData.compilationPermission || [];
  100. console.log(`req.session.managerData`);
  101. console.log(req.session.managerData.compilationPermission);
  102. const compilationList = await this.getCompilationList(fields);
  103. console.log(compilationList);
  104. return compilationList.filter(
  105. (item) =>
  106. compilationPermission.includes(item._id) ||
  107. compilationPermission.includes(item._id.toString())
  108. );
  109. }
  110. /**
  111. * 获取编办列表
  112. *
  113. * @return {Promise}
  114. */
  115. async getList() {
  116. // 筛选字段
  117. let field = {
  118. _id: 1,
  119. name: 1,
  120. is_release: 1,
  121. description: 1,
  122. categoryID: 1,
  123. };
  124. let compilationData = await this.findDataByCondition(
  125. { name: { $ne: "" }, is_release: true },
  126. field,
  127. false
  128. );
  129. return compilationData === null ? [] : compilationData;
  130. }
  131. /**
  132. * 根据id获取可用的编办数据
  133. *
  134. * @param {String} id
  135. * @return {Promise}
  136. */
  137. async getCompilationById(id) {
  138. let condition = { _id: id, is_release: true };
  139. let compilationData = await this.findDataByCondition(condition);
  140. if (!compilationData || compilationData.bill_valuation === undefined) {
  141. return compilationData;
  142. }
  143. if (compilationData.bill_valuation.length > 0) {
  144. let enableValuation = [];
  145. for (let index in compilationData.bill_valuation) {
  146. if (compilationData.bill_valuation[index].enable) {
  147. enableValuation.push(compilationData.bill_valuation[index]);
  148. }
  149. }
  150. compilationData.bill_valuation = enableValuation;
  151. }
  152. if (compilationData.ration_valuation.length > 0) {
  153. let enableValuation = [];
  154. for (let index in compilationData.ration_valuation) {
  155. if (compilationData.ration_valuation[index].enable) {
  156. enableValuation.push(compilationData.bill_valuation[index]);
  157. }
  158. }
  159. compilationData.ration_valuation = enableValuation;
  160. }
  161. return compilationData;
  162. }
  163. /**
  164. * 设置场景
  165. *
  166. * @param {string} scene
  167. * @return {void}
  168. */
  169. setScene(scene = "") {
  170. switch (scene) {
  171. // 新增
  172. case "add":
  173. this.model.schema.path("name").required(true);
  174. this.model.schema.path("creator").required(true);
  175. this.model.schema.path("create_time").required(true);
  176. break;
  177. }
  178. }
  179. /**
  180. * 新增编办
  181. *
  182. * @param {Object} data
  183. * @return {Promise}
  184. */
  185. async add(data) {
  186. let result = false;
  187. if (Object.keys(data).length <= 0) {
  188. return result;
  189. }
  190. this.setScene("add");
  191. data.create_time = new Date().getTime();
  192. result = this.db.create(data);
  193. return result;
  194. }
  195. /*
  196. * 设置版本描述
  197. *
  198. * @param {String} compilationId
  199. * @param {String} description
  200. * @return {Promise}
  201. * */
  202. async setDescription(compilationId, description) {
  203. return await this.updateById(compilationId, { description: description });
  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. for (let projId of example) {
  229. if (/^[0-9]+$/.test(projId)) {
  230. data.push(parseInt(projId));
  231. } else if (projId) {
  232. data.push(projId);
  233. }
  234. }
  235. return await this.updateById(compilationId, { example: data });
  236. }
  237. /**
  238. * 新增计价规则
  239. *
  240. * @param {String} id
  241. * @param {String} section
  242. * @param {Object} data
  243. * @return {Promise}
  244. */
  245. async addValuation(id, section, data) {
  246. let condition = { _id: id };
  247. let compilationData = await this.findDataByCondition(condition);
  248. if (compilationData === null || compilationData.name === undefined) {
  249. throw "没有找到对应的数据";
  250. }
  251. if (data.name === undefined || data.name === "") {
  252. throw "计价规则名称为空";
  253. }
  254. if (this.sectionList.indexOf(section) < 0) {
  255. console.log("2");
  256. throw "数据有误";
  257. }
  258. let insertData = {};
  259. data.id = uuidV1();
  260. insertData[section + "_valuation"] = data;
  261. console.log(insertData);
  262. let result = await this.db.addToSet(condition, insertData);
  263. return result.ok === undefined ? false : data.id;
  264. }
  265. /**
  266. * 保存计价数据
  267. *
  268. * @param {String} valuationId
  269. * @param {Object} data
  270. * @return {Promise}
  271. */
  272. async saveValuation(valuationId, data) {
  273. data = this._filterValuationData(data);
  274. let sectionString = data.section + "_valuation";
  275. let condition = {};
  276. condition[sectionString + ".id"] = valuationId;
  277. let updateData = {};
  278. updateData[sectionString + ".$.name"] = data.valuationName;
  279. let result = await this.db.update(condition, updateData);
  280. return result !== null && result.ok === 1;
  281. }
  282. /**
  283. * 更改启用/禁用
  284. *
  285. * @param {String} valuationId
  286. * @param {String} section
  287. * @param {String} enable
  288. * @return {Promise}
  289. */
  290. async switchEnable(valuationId, section, enable) {
  291. let sectionString = section + "_valuation";
  292. let condition = {};
  293. condition[sectionString + ".id"] = valuationId;
  294. let updateData = {};
  295. updateData[sectionString + ".$.enable"] = enable === "true";
  296. let result = await this.db.update(condition, updateData);
  297. return result !== null && result.ok === 1;
  298. }
  299. /**
  300. * 设置计价规则适用类型
  301. *
  302. * @param {String} valuationId
  303. * @param {String} section
  304. * @param {String} setFileTypes
  305. * @return {Promise}
  306. */
  307. async setFileTypes(valuationId, section, fileTypes) {
  308. let sectionString = section + "_valuation";
  309. let condition = {};
  310. condition[sectionString + ".id"] = valuationId;
  311. let updateData = {};
  312. updateData[sectionString + ".$.fileTypes"] = fileTypes;
  313. let result = await this.db.update(condition, updateData);
  314. return result !== null && result.ok === 1;
  315. }
  316. /**
  317. * 过滤计价数据
  318. *
  319. * @param {Object} data
  320. * @return {Object}
  321. */
  322. _filterValuationData(data) {
  323. if (Object.keys(data).length <= 0) {
  324. console.log("3");
  325. throw "数据有误";
  326. }
  327. if (
  328. data.section === undefined ||
  329. data.section === "" ||
  330. this.sectionList.indexOf(data.section) < 0
  331. ) {
  332. throw "类型错误";
  333. }
  334. // 判断名称
  335. if (data.valuationName === undefined || data.valuationName === "") {
  336. throw "名称不能为空";
  337. }
  338. return data;
  339. }
  340. /**
  341. * 获取计价规则数据
  342. *
  343. * @param {String} compilationId
  344. * @param {String} id
  345. * @param {String} section
  346. * @return {Promise|Array}
  347. */
  348. async getValuation(compilationId, id, section) {
  349. if (this.sectionList.indexOf(section) < 0) {
  350. throw "数据有误";
  351. }
  352. let compilationData = await this.findDataByCondition({
  353. _id: compilationId,
  354. });
  355. if (Object.keys(compilationData).length <= 0) {
  356. throw "编办数据有误";
  357. }
  358. let result = {};
  359. let sectionString = section + "_valuation";
  360. for (let valuation of compilationData[sectionString]) {
  361. if (valuation.id.toString() === id) {
  362. result = valuation;
  363. break;
  364. }
  365. }
  366. return [result, compilationData[sectionString]];
  367. /* 数据库获取编办
  368. let condition = {_id: versionId};
  369. let childCondition = {};
  370. childCondition[sectionString] = {$elemMatch: {_id: id}};
  371. let result = await this.db.findOne(condition, childCondition);
  372. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  373. */
  374. }
  375. /**
  376. * 删除计价规则
  377. *
  378. * @param {String} compilationId
  379. * @param {String} valuationId
  380. * @param {String} section
  381. * @return {Promise}
  382. */
  383. async deleteValuation(compilationId, valuationId, section) {
  384. let condition = { _id: compilationId };
  385. let sectionString = section + "_valuation";
  386. let deleteData = {};
  387. deleteData[sectionString] = { id: valuationId };
  388. // 利用pull删除嵌套数据
  389. let result = await this.db.deleteSet(condition, deleteData);
  390. return result !== null && result.ok === 1;
  391. }
  392. /**
  393. * 发布编办
  394. *
  395. * @param {String} id
  396. * @param {Number} status
  397. * @return {Promise}
  398. */
  399. async release(id, status) {
  400. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  401. if (status) {
  402. let compilationData = await this.findDataByCondition({ _id: id });
  403. // 最少需要有一个计价规则存在
  404. if (
  405. compilationData.suggestion_valuation.length <= 0 &&
  406. compilationData.feasibility_valuation.length <= 0 &&
  407. compilationData.rough_valuation.length <= 0 &&
  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. * 设置版本号
  519. *
  520. * @param {String} compilationId
  521. * @param {String} edition
  522. * @return {Promise}
  523. * */
  524. async setEdition(compilationId, edition) {
  525. return await this.updateById(compilationId, { edition: edition });
  526. }
  527. /*
  528. * 设置序号
  529. *
  530. * @param {String} compilationId
  531. * @param {Number} serialNumber
  532. * @return {Promise}
  533. * */
  534. async setSerialNumber(compilationId, serialNumber) {
  535. return await this.updateById(compilationId, { serialNumber: serialNumber });
  536. }
  537. /*
  538. * 设置编办地区
  539. *
  540. * @param {String} compilationId
  541. * @param {String} compilationArea
  542. * @return {Promise}
  543. * */
  544. async setCompilationArea(compilationId, compilationArea) {
  545. return await this.updateById(compilationId, {
  546. compilationArea: compilationArea,
  547. });
  548. }
  549. }
  550. export default CompilationModel;