version_model.js 4.2 KB

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