compilation_model.js 13 KB

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