quality_rule.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. /**
  3. * 质量管理 - 状态规则(规则组)
  4. *
  5. * @author Mai
  6. * @date 2024/7/19
  7. * @version
  8. */
  9. module.exports = app => {
  10. class QualityRule extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'quality_rule';
  20. }
  21. async getGroupList(pid) {
  22. const sql = `SELECT group_id, group_name FROM ${this.tableName} WHERE pid = ? GROUP BY group_id`;
  23. return await this.db.query(sql, [pid]);
  24. }
  25. analysisiRule(data) {
  26. data.condition = data.condition ? JSON.parse(data.condition) : [];
  27. data.push_status = data.push_status ? JSON.parse(data.push_status) : [];
  28. }
  29. analysisRuleGroups(datas) {
  30. const result = [];
  31. datas.forEach(x => {
  32. this.analysisiRule(x);
  33. let group = result.find(r => { return x.group_id === r.group_id; });
  34. if (!group) {
  35. group = { group_id: x.group_id, group_name: x.group_name, rules: [] };
  36. result.push(group);
  37. }
  38. group.rules.push(x);
  39. });
  40. return result;
  41. }
  42. async getRuleGroups(pid) {
  43. const result = await this.getAllDataByCondition({
  44. columns: ['id', 'group_id', 'group_name', 'name', 'condition', 'push_status', 'memo'],
  45. where: { pid },
  46. });
  47. return this.analysisRuleGroups(result);
  48. }
  49. async getGroupRule(group_id) {
  50. const result = await this.getAllDataByCondition({
  51. columns: ['id', 'group_id', 'group_name', 'name', 'condition', 'push_status', 'memo'],
  52. where: { group_id },
  53. });
  54. if (result.length === 0) return;
  55. result.forEach(x => {
  56. this.analysisiRule(x);
  57. });
  58. return { group_id, group_name: result[0].group_name, rules: result };
  59. }
  60. async getRule(id) {
  61. const result = await this.getDataById(id);
  62. this.analysisiRule(result);
  63. return result;
  64. }
  65. async getRules(ids) {
  66. const result = await this.getAllDataByCondition({ where: { id: ids } });
  67. for (const r of result) {
  68. this.analysisiRule(r);
  69. }
  70. return result;
  71. }
  72. async _delGroup(id) {
  73. const conn = await this.db.beginTransaction();
  74. try {
  75. await conn.delete(this.tableName, { group_id: id });
  76. // 下面这句update在qa上执行时,无法得到SET `group_id` = '',可能是对空值进行了过滤?
  77. // await conn.update(this.ctx.service.quality.tableName, { group_id: '' }, { where: { group_id: id } });
  78. await conn.query(`UPDATE ${this.ctx.service.quality.tableName} SET group_id = '' WHERE group_id = ?`, [id]);
  79. await conn.commit();
  80. } catch (err) {
  81. await conn.rollback();
  82. this.ctx.log(err);
  83. throw err;
  84. }
  85. return id;
  86. }
  87. async _saveGroup(id, group_name) {
  88. if (id) {
  89. await this.db.update(this.tableName, { group_name }, { where: { group_id: id } });
  90. return { group_id: id, group_name };
  91. }
  92. const newRule = {
  93. group_id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
  94. user_id: this.ctx.session.sessionUser.accountId,
  95. group_name: group_name || '新增规则组', name: '新增规则', condition: '[]', push_status: '[]',
  96. };
  97. const result = await this.db.insert(this.tableName, newRule);
  98. return await this.getGroupRule(newRule.group_id);
  99. }
  100. async saveGroup(data) {
  101. const result = {};
  102. try {
  103. if (data.add) result.add = await this._saveGroup();
  104. if (data.del) result.del = await this._delGroup(data.del);
  105. if (data.update) result.update = await this._saveGroup(data.update.group_id, data.update.group_name);
  106. } catch (err) {
  107. this.ctx.log(err);
  108. throw err;
  109. }
  110. return result;
  111. }
  112. async _saveRule(data) {
  113. if (data.id) {
  114. if (data.name) data.name = data.name;
  115. if (data.condition) data.condition = JSON.stringify(data.condition);
  116. if (data.push_status) data.push_status = JSON.stringify(data.push_status);
  117. await this.db.update(this.tableName, data);
  118. } else {
  119. data.pid = this.ctx.session.sessionProject.id;
  120. data.user_id = this.ctx.session.sessionUser.accountId;
  121. data.name = data.name || '新增规则';
  122. data.condition = data.condition ? JSON.stringify(data.condition) : '[]';
  123. data.push_status = data.push_status ? JSON.stringify(data.push_status) : '[]';
  124. const result = await this.db.insert(this.tableName, data);
  125. data.id = result.insertId;
  126. }
  127. return await this.getRule(data.id);
  128. }
  129. async _delRule(data) {
  130. await this.deleteById(data.id);
  131. return data;
  132. }
  133. async _copyRule(group_id, copy) {
  134. const copyId = copy.split(',').map(x => { return parseInt(x); });
  135. const rules = await this.getAllDataByCondition({ where: { id: copyId } });
  136. const group = await this.getDataByCondition({ group_id });
  137. if (!group) throw '您选择的规则组不存在,请刷新页面再试';
  138. const insertData = rules.map(rule => {
  139. return {
  140. pid: rule.pid, user_id: this.ctx.session.sessionUser.accountId,
  141. group_id: group.group_id, group_name: group.group_name,
  142. name: rule.name, condition: rule.condition, push_status: rule.push_status,
  143. };
  144. });
  145. const result = await this.db.insert(this.tableName, insertData);
  146. const ids = [];
  147. for (let i = 0; i < insertData.length; i++) {
  148. ids.push(result.insertId + i);
  149. }
  150. return await this.getRules(ids);
  151. }
  152. async saveRule(data) {
  153. const result = {};
  154. if (data.add) result.add = await this._saveRule(data.add);
  155. if (data.del) result.del = await this._delRule(data.del);
  156. if (data.update) result.update = await this._saveRule(data.update);
  157. if (data.copy) result.add = await this._copyRule(data.group_id, data.copy);
  158. return result;
  159. }
  160. }
  161. return QualityRule;
  162. };