compilation_model.js 9.8 KB

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