compilation_model.js 9.3 KB

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