compilation_model.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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() {
  34. // 筛选字段
  35. let field = {_id: 1, name: 1, is_release: 1, description: 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};
  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. * @param {String} id
  86. * @param {String} section
  87. * @param {Object} data
  88. * @return {Promise}
  89. */
  90. async addValuation(id, section, data) {
  91. let condition = {_id: id};
  92. let compilationData = await this.findDataByCondition(condition);
  93. if (compilationData === null || compilationData.name === undefined) {
  94. throw '没有找到对应的数据';
  95. }
  96. if (data.name === undefined || data.name === '') {
  97. throw '计价规则名称为空';
  98. }
  99. if (this.sectionList.indexOf(section) < 0) {
  100. console.log('2');
  101. throw '数据有误';
  102. }
  103. let insertData = {};
  104. data.id = uuidV1();
  105. insertData[section + '_valuation'] = data;
  106. let result = await this.db.addToSet(condition, insertData);
  107. return result.ok === undefined ? false : data.id;
  108. }
  109. /**
  110. * 保存计价数据
  111. *
  112. * @param {String} valuationId
  113. * @param {Object} data
  114. * @return {Promise}
  115. */
  116. async saveValuation(valuationId, data) {
  117. data = this._filterValuationData(data);
  118. let sectionString = data.section + "_valuation";
  119. let condition = {};
  120. condition[sectionString + ".id"] = valuationId;
  121. let updateData = {};
  122. updateData[sectionString + ".$.name"] = data.name;
  123. let result = await this.db.update(condition, updateData);
  124. return result !== null && result.ok === 1;
  125. };
  126. /**
  127. * 更改启用/禁用
  128. *
  129. * @param {String} valuationId
  130. * @param {String} section
  131. * @param {String} enable
  132. * @return {Promise}
  133. */
  134. async switchEnable(valuationId, section, enable) {
  135. let sectionString = section + "_valuation";
  136. let condition = {};
  137. condition[sectionString + ".id"] = valuationId;
  138. let updateData = {};
  139. updateData[sectionString + ".$.enable"] = enable === "true";
  140. let result = await this.db.update(condition, updateData);
  141. return result !== null && result.ok === 1;
  142. }
  143. /**
  144. * 过滤计价数据
  145. *
  146. * @param {Object} data
  147. * @return {Object}
  148. */
  149. _filterValuationData(data) {
  150. if (Object.keys(data).length <= 0) {
  151. console.log('3');
  152. throw '数据有误';
  153. }
  154. if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
  155. throw '类型错误';
  156. }
  157. // 判断名称
  158. if (data.name === undefined || data.name === '') {
  159. throw '名称不能为空';
  160. }
  161. return data;
  162. }
  163. /**
  164. * 获取计价规则数据
  165. *
  166. * @param {String} compilationId
  167. * @param {String} id
  168. * @param {String} section
  169. * @return {Promise|Array}
  170. */
  171. async getValuation(compilationId, id, section) {
  172. if (this.sectionList.indexOf(section) < 0) {
  173. throw '数据有误';
  174. }
  175. let compilationData = await this.findDataByCondition({_id: compilationId});
  176. if (Object.keys(compilationData).length <= 0) {
  177. throw '编办数据有误';
  178. }
  179. let result = {};
  180. let sectionString = section + '_valuation';
  181. for(let valuation of compilationData[sectionString]) {
  182. if (valuation.id.toString() === id) {
  183. result = valuation;
  184. break;
  185. }
  186. }
  187. return [result, compilationData[sectionString]];
  188. /* 数据库获取编办
  189. let condition = {_id: versionId};
  190. let childCondition = {};
  191. childCondition[sectionString] = {$elemMatch: {_id: id}};
  192. let result = await this.db.findOne(condition, childCondition);
  193. return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
  194. */
  195. }
  196. /**
  197. * 删除计价规则
  198. *
  199. * @param {String} compilationId
  200. * @param {String} valuationId
  201. * @param {String} section
  202. * @return {Promise}
  203. */
  204. async deleteValuation(compilationId, valuationId, section) {
  205. let condition = {_id: compilationId};
  206. let sectionString = section + '_valuation';
  207. let deleteData = {};
  208. deleteData[sectionString] = {id: valuationId};
  209. // 利用pull删除嵌套数据
  210. let result = await this.db.deleteSet(condition, deleteData);
  211. return result !== null && result.ok === 1;
  212. }
  213. /**
  214. * 发布编办
  215. *
  216. * @param {String} id
  217. * @param {Number} status
  218. * @return {Promise}
  219. */
  220. async release(id, status) {
  221. // 如果是发布编办则需要判断配置的内容是否满足发布条件
  222. if (status) {
  223. let compilationData = await this.findDataByCondition({_id: id});
  224. // 最少需要有一个计价规则存在
  225. if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
  226. throw '至少需要一个计价规则';
  227. }
  228. // 判断是否全部禁止
  229. let hasEnable = false;
  230. let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
  231. for (let valuation of valuationList) {
  232. if (valuation.enable) {
  233. hasEnable = true;
  234. break;
  235. }
  236. }
  237. if (!hasEnable) {
  238. throw '至少需要一个可用的计价规则';
  239. }
  240. }
  241. let updateData = {
  242. is_release: status === 1,
  243. release_time: new Date().getTime()
  244. };
  245. return this.updateById(id, updateData);
  246. }
  247. /**
  248. * 根据专业工程获取对应专业工程标准库id
  249. *
  250. * @param {String} valuationId
  251. * @param {String} section
  252. * @param {Number} engineering
  253. * @return {Promise}
  254. */
  255. async getEngineeringLib(valuationId, section, engineering) {
  256. let sectionString = section + "_valuation";
  257. let condition = {};
  258. condition[sectionString + ".id"] = valuationId;
  259. let compilationData = await this.findDataByCondition(condition);
  260. if (compilationData === null) {
  261. throw '不存在对应编办';
  262. }
  263. let valuationData = null;
  264. for(let valuation of compilationData[sectionString]) {
  265. if(valuation.id === valuationId) {
  266. valuationData = valuation;
  267. }
  268. }
  269. if (valuationData === null) {
  270. throw '不存在对应计价规则';
  271. }
  272. // 判断是否已有对应数据
  273. let engineeringList = valuationData.engineering_list;
  274. let engineeringLib = null;
  275. for(let tmpEngineering of engineeringList) {
  276. if (tmpEngineering.engineering === engineering) {
  277. engineeringLib = tmpEngineering;
  278. break;
  279. }
  280. }
  281. return engineeringLib;
  282. }
  283. /**
  284. * 新增专业工程标准库
  285. *
  286. * @param {String} valuationId
  287. * @param {String} section
  288. * @param {Object} data
  289. * @return {Promise}
  290. */
  291. async addEngineeringLib(valuationId, section, data) {
  292. let sectionString = section + "_valuation";
  293. let condition = {};
  294. condition[sectionString + ".id"] = valuationId;
  295. let insertData = {};
  296. insertData[sectionString + ".$.engineering_list"] = data;
  297. let result = await this.db.addToSet(condition, insertData);
  298. return result.ok === 1;
  299. }
  300. }
  301. export default CompilationModel;