version_model.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * 版本管理业务逻辑模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model";
  9. import VersionSchema from "./schemas/version";
  10. class VersionModel extends BaseModel {
  11. /**
  12. * 构造函数
  13. *
  14. * @return {void}
  15. */
  16. constructor() {
  17. let parent = super();
  18. parent.model = VersionSchema;
  19. parent.init();
  20. }
  21. /**
  22. * 获取版本列表
  23. *
  24. * @return {Promise}
  25. */
  26. async getVersionList() {
  27. let versionData = await this.findDataByCondition({name: {$ne: ''}}, null, false);
  28. return versionData === null ? [] : versionData;
  29. }
  30. /**
  31. * 设置场景
  32. *
  33. * @param {string} scene
  34. * @return {void}
  35. */
  36. setScene(scene = '') {
  37. switch (scene) {
  38. // 新增
  39. case 'add':
  40. this.model.schema.path('name').required(true);
  41. this.model.schema.path('standard_bill').required(true);
  42. this.model.schema.path('ration_lib').required(true);
  43. this.model.schema.path('creator').required(true);
  44. this.model.schema.path('create_time').required(true);
  45. break;
  46. }
  47. }
  48. /**
  49. * 新增版本
  50. *
  51. * @param {Object} data
  52. * @return {Promise}
  53. */
  54. async add(data) {
  55. let result = false;
  56. if (Object.keys(data).length <= 0) {
  57. return result;
  58. }
  59. this.setScene('add');
  60. data.create_time = new Date().getTime();
  61. result = this.db.create(data);
  62. return result;
  63. }
  64. /**
  65. * 新增标准清单/定额库
  66. *
  67. * @param {Object} postData
  68. * @return {Promise}
  69. */
  70. async addLib(postData) {
  71. if (postData.id === undefined || postData.model === undefined) {
  72. throw '参数错误';
  73. }
  74. let model = postData.model;
  75. let id = postData.id;
  76. let standardBillId = postData.standard_bill_id;
  77. let standardBill = postData.standard_bill;
  78. let rationLibId = postData.ration_lib_id;
  79. let rationLib = postData.ration_lib;
  80. switch (model) {
  81. case 'bill':
  82. if (standardBillId === undefined || standardBill === undefined) {
  83. throw '参数错误';
  84. }
  85. break;
  86. case 'ration':
  87. if (rationLibId === undefined || rationLib === undefined) {
  88. throw '参数错误';
  89. }
  90. break;
  91. }
  92. let versionData = await this.findDataByCondition({_id: id});
  93. if (versionData === null || versionData.standard_bill === undefined) {
  94. throw '没有找到对应的数据';
  95. }
  96. let updateData = {};
  97. let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
  98. let tmpData = {
  99. id: model === 'bill' ? standardBillId : rationLibId,
  100. name: model === 'bill' ? standardBill : rationLib
  101. };
  102. // 判断是否有重复
  103. if (versionData[field].length > 0) {
  104. for (let tmp of versionData[field]) {
  105. if (tmp.id === tmpData.id) {
  106. throw '已存在对应数据';
  107. }
  108. }
  109. }
  110. versionData[field].push(tmpData);
  111. updateData[field] = versionData[field];
  112. let result = await this.db.update({_id: id}, updateData);
  113. return result.ok === undefined ? false : result.ok;
  114. }
  115. /**
  116. * 删除对应的标准清单库/定额库
  117. *
  118. * @param {Number} id
  119. * @param {Number} libId
  120. * @param {String} model
  121. * @return {Promise}
  122. */
  123. async deleteLib(id, libId, model) {
  124. let versionData = await this.findDataByCondition({_id: id});
  125. if (versionData === null || versionData.standard_bill === undefined) {
  126. throw '没有找到对应的数据';
  127. }
  128. let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
  129. if (versionData[field].length <= 0) {
  130. throw '没有对应的库数据';
  131. }
  132. let own = false;
  133. for (let index in versionData[field]) {
  134. if (versionData[field][index].id === libId) {
  135. versionData[field].splice(index, 1);
  136. own = true;
  137. break;
  138. }
  139. }
  140. if (!own) {
  141. throw '没有对应的库数据';
  142. }
  143. let updateData = {};
  144. updateData[field] = versionData[field];
  145. let result = await this.db.update({_id: id}, updateData);
  146. return result.ok === undefined ? false : result.ok;
  147. }
  148. }
  149. export default VersionModel;