compilation_model.js 15 KB

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