compilation_model.js 11 KB

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