version_model.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. break;
  44. }
  45. }
  46. /**
  47. * 新增版本
  48. *
  49. * @param {Object} data
  50. * @return {Promise}
  51. */
  52. async add(data) {
  53. let result = false;
  54. if (Object.keys(data).length <= 0) {
  55. return result;
  56. }
  57. this.setScene('add');
  58. data.create_time = new Date().getTime();
  59. result = this.db.create(data);
  60. return result;
  61. }
  62. /**
  63. * 新增标准清单/定额库
  64. *
  65. * @param {Object} postData
  66. * @return {Promise}
  67. */
  68. async addLib(postData) {
  69. if (postData.id === undefined || postData.model === undefined) {
  70. throw '参数错误';
  71. }
  72. let model = postData.model;
  73. let id = postData.id;
  74. let standardBillId = postData.standard_bill_id;
  75. let standardBill = postData.standard_bill;
  76. let rationLibId = postData.ration_lib_id;
  77. let rationLib = postData.ration_lib;
  78. switch (model) {
  79. case 'bill':
  80. if (standardBillId === undefined || standardBill === undefined) {
  81. throw '参数错误';
  82. }
  83. break;
  84. case 'ration':
  85. if (rationLibId === undefined || rationLib === undefined) {
  86. throw '参数错误';
  87. }
  88. break;
  89. }
  90. let versionData = await this.findDataByCondition({_id: id});
  91. if (versionData === null || versionData.standard_bill === undefined) {
  92. throw '没有找到对应的数据';
  93. }
  94. let updateData = {};
  95. let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
  96. let tmpData = {
  97. id: model === 'bill' ? standardBillId : rationLibId,
  98. name: model === 'bill' ? standardBill : rationLib
  99. };
  100. versionData[field].push(tmpData);
  101. updateData[field] = versionData[field];
  102. let result = await this.db.update({_id: id}, updateData);
  103. return result.ok === undefined ? false : result.ok;
  104. }
  105. /**
  106. * 删除对应的标准清单库/定额库
  107. *
  108. * @param {Number} id
  109. * @param {Number} libId
  110. * @param {String} model
  111. * @return {Promise}
  112. */
  113. async deleteLib(id, libId, model) {
  114. let versionData = await this.findDataByCondition({_id: id});
  115. if (versionData === null || versionData.standard_bill === undefined) {
  116. throw '没有找到对应的数据';
  117. }
  118. let field = model === 'bill' ? 'standard_bill' : 'ration_lib';
  119. if (versionData[field].length <= 0) {
  120. throw '没有对应的库数据';
  121. }
  122. let own = false;
  123. for (let index in versionData[field]) {
  124. if (versionData[field][index].id === libId) {
  125. versionData[field].splice(index, 1);
  126. own = true;
  127. break;
  128. }
  129. }
  130. if (!own) {
  131. throw '没有对应的库数据';
  132. }
  133. let updateData = {};
  134. updateData[field] = versionData[field];
  135. let result = await this.db.update({_id: id}, updateData);
  136. return result.ok === undefined ? false : result.ok;
  137. }
  138. }
  139. export default VersionModel;