compilation_model.js 15 KB

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