compilation_model.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. class CompilationModel extends BaseModel {
  11. /**
  12. * 允许的块
  13. *
  14. * @var {Array}
  15. */
  16. sectionList = ['bill', 'ration'];
  17. /**
  18. * 构造函数
  19. *
  20. * @return {void}
  21. */
  22. constructor() {
  23. let parent = super();
  24. parent.model = mongoose.model('compilation');
  25. parent.init();
  26. }
  27. /**
  28. * 获取编办列表
  29. *
  30. * @return {Promise}
  31. */
  32. async getCompilationList() {
  33. // 筛选字段
  34. let field = {_id: 1, name: 1, is_release: 1, description: 1, "ration_valuation.id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
  35. "bill_valuation.id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1};
  36. let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);
  37. return compilationData === null ? [] : compilationData;
  38. }
  39. /**
  40. * 设置场景
  41. *
  42. * @param {string} scene
  43. * @return {void}
  44. */
  45. setScene(scene = '') {
  46. switch (scene) {
  47. // 新增
  48. case 'add':
  49. this.model.schema.path('name').required(true);
  50. this.model.schema.path('creator').required(true);
  51. this.model.schema.path('create_time').required(true);
  52. break;
  53. }
  54. }
  55. /**
  56. * 新增编办
  57. *
  58. * @param {Object} data
  59. * @return {Promise}
  60. */
  61. async add(data) {
  62. let result = false;
  63. if (Object.keys(data).length <= 0) {
  64. return result;
  65. }
  66. this.setScene('add');
  67. data.create_time = new Date().getTime();
  68. result = this.db.create(data);
  69. return result;
  70. }
  71. /*
  72. * 设置版本描述
  73. *
  74. * @param {String} compilationId
  75. * @param {String} description
  76. * @return {Promise}
  77. * */
  78. async setDescription(compilationId, description){
  79. return await this.updateById(compilationId, {description: description});
  80. }
  81. /**
  82. * 新增计价规则
  83. *
  84. * @param {String} id
  85. * @param {String} section
  86. * @param {Object} data
  87. * @return {Promise}
  88. */
  89. async addValuation(id, section, data) {
  90. let condition = {_id: id};
  91. let compilationData = await this.findDataByCondition(condition);
  92. if (compilationData === null || compilationData.name === undefined) {
  93. throw '没有找到对应的数据';
  94. }
  95. if (data.name === undefined || data.name === '') {
  96. throw '计价规则名称为空';
  97. }
  98. if (this.sectionList.indexOf(section) < 0) {
  99. console.log('2');
  100. throw '数据有误';
  101. }
  102. let insertData = {};
  103. data.id = "12580";
  104. insertData[section + '_valuation'] = data;
  105. let result = await this.db.addToSet(condition, insertData);
  106. console.log(result);
  107. return result.ok === undefined ? false : result.ok;
  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;