compilation_model.js 13 KB

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