version_model.js 4.1 KB

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