compilation_model.js 12 KB

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