compilation_model.js 12 KB

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