multi_limit.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. /**
  3. * 联合限制计量
  4. *
  5. * @author Mai
  6. * @date 2024/6/27
  7. * @version
  8. */
  9. module.exports = app => {
  10. class MultiLimit extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'multi_limit';
  20. }
  21. async getLimitList(pid) {
  22. const sql = `SELECT limit_id, name FROM ${this.tableName} WHERE pid = ? GROUP BY limit_id`;
  23. return await this.db.query(sql, [pid]);
  24. }
  25. analysisLimits(datas) {
  26. const result = [];
  27. datas.forEach(x => {
  28. x.condition = x.condition ? JSON.parse(x.condition) : [];
  29. let limit = result.find(r => { return x.limit_id === r.limit_id; });
  30. if (!limit) {
  31. limit = { limit_id: x.limit_id, name: x.name, options: [] };
  32. result.push(limit);
  33. }
  34. limit.options.push(x);
  35. });
  36. return result;
  37. }
  38. async getLimits(pid) {
  39. const result = await this.getAllDataByCondition({
  40. columns: ['id', 'limit_id', 'name', 'limit', 'lower', 'upper', 'condition', 'hint'],
  41. where: { pid },
  42. });
  43. return this.analysisLimits(result);
  44. }
  45. async getLimitOptions(limit_id) {
  46. const result = await this.getAllDataByCondition({
  47. columns: ['limit_id', 'name', 'limit', 'lower', 'upper', 'condition', 'hint'],
  48. where: { limit_id },
  49. });
  50. result.forEach(x => { x.condition = x.condition ? JSON.parse(x.condition) : []; });
  51. return result;
  52. }
  53. async getLimitOption(id) {
  54. const result = await this.getDataById(id);
  55. result.condition = result.condition ? JSON.parse(result.condition) : [];
  56. return result;
  57. }
  58. async _delLimit(id) {
  59. await this.db.delete(this.tableName, { limit_id: id });
  60. return id;
  61. }
  62. async _saveLimit(id, name) {
  63. if (id) {
  64. await this.db.update(this.tableName, { name }, { where: { limit_id: id } });
  65. return { limit_id: id, name };
  66. }
  67. const newLimitOption = {
  68. limit_id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
  69. user_id: this.ctx.session.sessionUser.accountId,
  70. name: name || '新增计量配置', condition: '[]', limit: 1,
  71. };
  72. const result = await this.db.insert(this.tableName, newLimitOption);
  73. const lo = await this.getLimitOption(result.insertId);
  74. return { limit_id: lo.limit_id, name: lo.name, options: [lo] };
  75. }
  76. async saveLimit(data) {
  77. const result = {};
  78. if (data.add) result.add = await this._saveLimit();
  79. if (data.del) result.del = await this._delLimit(data.del);
  80. if (data.update) result.update = await this._saveLimit(data.update.limit_id, data.update.name);
  81. return result;
  82. }
  83. async _saveLimitOption(data) {
  84. if (data.id) {
  85. data.condition = JSON.stringify(data.condition);
  86. await this.db.update(this.tableName, data);
  87. } else {
  88. data.pid = this.ctx.session.sessionProject.id;
  89. data.user_id = this.ctx.session.sessionUser.accountId;
  90. data.condition = JSON.stringify(data.condition);
  91. const result = await this.db.insert(this.tableName, data);
  92. data.id = result.insertId;
  93. }
  94. return await this.getLimitOption(data.id);
  95. }
  96. async _delLimitOption(data) {
  97. await this.deleteById(data.id);
  98. return data;
  99. }
  100. async saveLimitOption(data) {
  101. const result = {};
  102. if (data.add) result.add = await this._saveLimitOption(data.add);
  103. if (data.del) result.del = await this._delLimitOption(data.del);
  104. if (data.update) result.update = await this._saveLimitOption(data.update);
  105. return result;
  106. }
  107. }
  108. return MultiLimit;
  109. };