compilation_model.js 14 KB

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