compilation_model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. "ration_valuation.id": 1,
  57. "ration_valuation.name": 1,
  58. "ration_valuation.enable": 1,
  59. "suggestion_valuation.id": 1,
  60. "suggestion_valuation.name": 1,
  61. "suggestion_valuation.enable": 1,
  62. "feasibility_valuation.id": 1,
  63. "feasibility_valuation.name": 1,
  64. "feasibility_valuation.enable": 1,
  65. "rough_valuation.id": 1,
  66. "rough_valuation.name": 1,
  67. "rough_valuation.enable": 1,
  68. "bill_valuation.id": 1,
  69. "bill_valuation.name": 1,
  70. "bill_valuation.enable": 1,
  71. "estimation_valuation.id": 1,
  72. "estimation_valuation.name": 1,
  73. "estimation_valuation.enable": 1,
  74. "bill_valuation.fileTypes": 1,
  75. type: 1,
  76. }
  77. : fields;
  78. // 一定要查询序号,然后排序输出
  79. field.serialNumber = 1;
  80. let compilationData = await this.findDataByCondition(
  81. { name: { $ne: "" } },
  82. field,
  83. false
  84. );
  85. if (compilationData) {
  86. // 按照序号排序
  87. compilationData.sort((aa, bb) => {
  88. return (aa.serialNumber || 99999) - (bb.serialNumber || 99999);
  89. });
  90. return compilationData;
  91. }
  92. return [];
  93. }
  94. // 根据用户权限过滤编办
  95. async getPermissionCompilationList(req, fields = null) {
  96. const compilationPermission =
  97. req.session.managerData.compilationPermission || [];
  98. console.log(`req.session.managerData`);
  99. console.log(req.session.managerData.compilationPermission);
  100. const compilationList = await this.getCompilationList(fields);
  101. console.log(compilationList);
  102. return compilationList.filter(
  103. (item) =>
  104. compilationPermission.includes(item._id) ||
  105. compilationPermission.includes(item._id.toString())
  106. );
  107. }
  108. /**
  109. * 获取编办列表
  110. *
  111. * @return {Promise}
  112. */
  113. async getList() {
  114. // 筛选字段
  115. let field = {
  116. _id: 1,
  117. name: 1,
  118. is_release: 1,
  119. description: 1,
  120. categoryID: 1,
  121. };
  122. let compilationData = await this.findDataByCondition(
  123. { name: { $ne: "" }, is_release: true },
  124. field,
  125. false
  126. );
  127. return compilationData === null ? [] : compilationData;
  128. }
  129. /**
  130. * 根据id获取可用的编办数据
  131. *
  132. * @param {String} id
  133. * @return {Promise}
  134. */
  135. async getCompilationById(id) {
  136. let condition = { _id: id, is_release: true };
  137. let compilationData = await this.findDataByCondition(condition);
  138. if (!compilationData || compilationData.bill_valuation === undefined) {
  139. return compilationData;
  140. }
  141. if (compilationData.bill_valuation.length > 0) {
  142. let enableValuation = [];
  143. for (let index in compilationData.bill_valuation) {
  144. if (compilationData.bill_valuation[index].enable) {
  145. enableValuation.push(compilationData.bill_valuation[index]);
  146. }
  147. }
  148. compilationData.bill_valuation = enableValuation;
  149. }
  150. if (compilationData.ration_valuation.length > 0) {
  151. let enableValuation = [];
  152. for (let index in compilationData.ration_valuation) {
  153. if (compilationData.ration_valuation[index].enable) {
  154. enableValuation.push(compilationData.bill_valuation[index]);
  155. }
  156. }
  157. compilationData.ration_valuation = enableValuation;
  158. }
  159. return compilationData;
  160. }
  161. /**
  162. * 设置场景
  163. *
  164. * @param {string} scene
  165. * @return {void}
  166. */
  167. setScene(scene = "") {
  168. switch (scene) {
  169. // 新增
  170. case "add":
  171. this.model.schema.path("name").required(true);
  172. this.model.schema.path("creator").required(true);
  173. this.model.schema.path("create_time").required(true);
  174. break;
  175. }
  176. }
  177. /**
  178. * 新增编办
  179. *
  180. * @param {Object} data
  181. * @return {Promise}
  182. */
  183. async add(data) {
  184. let result = false;
  185. if (Object.keys(data).length <= 0) {
  186. return result;
  187. }
  188. this.setScene("add");
  189. data.create_time = new Date().getTime();
  190. result = this.db.create(data);
  191. return result;
  192. }
  193. /*
  194. * 设置版本描述
  195. *
  196. * @param {String} compilationId
  197. * @param {String} description
  198. * @return {Promise}
  199. * */
  200. async setDescription(compilationId, description) {
  201. return await this.updateById(compilationId, { description: description });
  202. }
  203. /*
  204. 设置代码覆盖路径
  205. */
  206. async setOverWriteUrl(
  207. compilationId,
  208. overWriteUrl,
  209. priceProp,
  210. consumeAmtProp
  211. ) {
  212. return await this.updateById(compilationId, {
  213. overWriteUrl: overWriteUrl,
  214. priceProperties: priceProp,
  215. consumeAmtProperties: consumeAmtProp,
  216. });
  217. }
  218. /*
  219. * 设置例题
  220. * @param {String} compilationId
  221. * @param {Array} example
  222. * @return {Promise}
  223. * */
  224. async setExample(compilationId, example) {
  225. let data = [];
  226. for (let projId of example) {
  227. if (/^[0-9]+$/.test(projId)) {
  228. data.push(parseInt(projId));
  229. } else if (projId) {
  230. data.push(projId);
  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. console.log(insertData);
  260. let result = await this.db.addToSet(condition, insertData);
  261. return result.ok === undefined ? false : data.id;
  262. }
  263. /**
  264. * 保存计价数据
  265. *
  266. * @param {String} valuationId
  267. * @param {Object} data
  268. * @return {Promise}
  269. */
  270. async saveValuation(valuationId, data) {
  271. data = this._filterValuationData(data);
  272. let sectionString = data.section + "_valuation";
  273. let condition = {};
  274. condition[sectionString + ".id"] = valuationId;
  275. let updateData = {};
  276. updateData[sectionString + ".$.name"] = data.valuationName;
  277. let result = await this.db.update(condition, updateData);
  278. return result !== null && result.ok === 1;
  279. }
  280. /**
  281. * 更改启用/禁用
  282. *
  283. * @param {String} valuationId
  284. * @param {String} section
  285. * @param {String} enable
  286. * @return {Promise}
  287. */
  288. async switchEnable(valuationId, section, enable) {
  289. let sectionString = section + "_valuation";
  290. let condition = {};
  291. condition[sectionString + ".id"] = valuationId;
  292. let updateData = {};
  293. updateData[sectionString + ".$.enable"] = enable === "true";
  294. let result = await this.db.update(condition, updateData);
  295. return result !== null && result.ok === 1;
  296. }
  297. /**
  298. * 设置计价规则适用类型
  299. *
  300. * @param {String} valuationId
  301. * @param {String} section
  302. * @param {String} setFileTypes
  303. * @return {Promise}
  304. */
  305. async setFileTypes(valuationId, section, fileTypes) {
  306. let sectionString = section + "_valuation";
  307. let condition = {};
  308. condition[sectionString + ".id"] = valuationId;
  309. let updateData = {};
  310. updateData[sectionString + ".$.fileTypes"] = fileTypes;
  311. let result = await this.db.update(condition, updateData);
  312. return result !== null && result.ok === 1;
  313. }
  314. /**
  315. * 过滤计价数据
  316. *
  317. * @param {Object} data
  318. * @return {Object}
  319. */
  320. _filterValuationData(data) {
  321. if (Object.keys(data).length <= 0) {
  322. console.log("3");
  323. throw "数据有误";
  324. }
  325. if (
  326. data.section === undefined ||
  327. data.section === "" ||
  328. this.sectionList.indexOf(data.section) < 0
  329. ) {
  330. throw "类型错误";
  331. }
  332. // 判断名称
  333. if (data.valuationName === undefined || data.valuationName === "") {
  334. throw "名称不能为空";
  335. }
  336. return data;
  337. }
  338. /**
  339. * 获取计价规则数据
  340. *
  341. * @param {String} compilationId
  342. * @param {String} id
  343. * @param {String} section
  344. * @return {Promise|Array}
  345. */
  346. async getValuation(compilationId, id, section) {
  347. if (this.sectionList.indexOf(section) < 0) {
  348. throw "数据有误";
  349. }
  350. let compilationData = await this.findDataByCondition({
  351. _id: compilationId,
  352. });
  353. if (Object.keys(compilationData).length <= 0) {
  354. throw "编办数据有误";
  355. }
  356. let result = {};
  357. let sectionString = section + "_valuation";
  358. for (let valuation of compilationData[sectionString]) {
  359. if (valuation.id.toString() === id) {
  360. result = valuation;
  361. break;
  362. }
  363. }
  364. return [result, compilationData[sectionString]];
  365. /* 数据库获取编办
  366. let condition = {_id: versionId};
  367. let childCondition = {};
  368. childCondition[sectionString] = {$elemMatch: {_id: id}};
  369. let result = await this.db.findOne(condition, childCondition);
  370. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  371. */
  372. }
  373. /**
  374. * 删除计价规则
  375. *
  376. * @param {String} compilationId
  377. * @param {String} valuationId
  378. * @param {String} section
  379. * @return {Promise}
  380. */
  381. async deleteValuation(compilationId, valuationId, section) {
  382. let condition = { _id: compilationId };
  383. let sectionString = section + "_valuation";
  384. let deleteData = {};
  385. deleteData[sectionString] = { id: valuationId };
  386. // 利用pull删除嵌套数据
  387. let result = await this.db.deleteSet(condition, deleteData);
  388. return result !== null && result.ok === 1;
  389. }
  390. /**
  391. * 发布编办
  392. *
  393. * @param {String} id
  394. * @param {Number} status
  395. * @return {Promise}
  396. */
  397. async release(id, status) {
  398. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  399. if (status) {
  400. let compilationData = await this.findDataByCondition({ _id: id });
  401. // 最少需要有一个计价规则存在
  402. if (
  403. compilationData.suggestion_valuation.length <= 0 &&
  404. compilationData.feasibility_valuation.length <= 0 &&
  405. compilationData.rough_valuation.length <= 0 &&
  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. * 设置版本号
  517. *
  518. * @param {String} compilationId
  519. * @param {String} edition
  520. * @return {Promise}
  521. * */
  522. async setEdition(compilationId, edition) {
  523. return await this.updateById(compilationId, { edition: edition });
  524. }
  525. /*
  526. * 设置序号
  527. *
  528. * @param {String} compilationId
  529. * @param {Number} serialNumber
  530. * @return {Promise}
  531. * */
  532. async setSerialNumber(compilationId, serialNumber) {
  533. return await this.updateById(compilationId, { serialNumber: serialNumber });
  534. }
  535. }
  536. export default CompilationModel;