compilation_model.js 15 KB

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