compilation_model.js 16 KB

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