123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 'use strict';
- /**
- * 质量管理 - 状态规则(规则组)
- *
- * @author Mai
- * @date 2024/7/19
- * @version
- */
- module.exports = app => {
- class QualityRule extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'quality_rule';
- }
- async getGroupList(pid) {
- const sql = `SELECT group_id, group_name FROM ${this.tableName} WHERE pid = ? GROUP BY group_id`;
- return await this.db.query(sql, [pid]);
- }
- analysisiRule(data) {
- data.condition = data.condition ? JSON.parse(data.condition) : [];
- data.push_status = data.push_status ? JSON.parse(data.push_status) : [];
- }
- analysisRuleGroups(datas) {
- const result = [];
- datas.forEach(x => {
- this.analysisiRule(x);
- let group = result.find(r => { return x.group_id === r.group_id; });
- if (!group) {
- group = { group_id: x.group_id, group_name: x.group_name, rules: [] };
- result.push(group);
- }
- group.rules.push(x);
- });
- return result;
- }
- async getRuleGroups(pid) {
- const result = await this.getAllDataByCondition({
- columns: ['id', 'group_id', 'group_name', 'name', 'condition', 'push_status', 'memo'],
- where: { pid },
- });
- return this.analysisRuleGroups(result);
- }
- async getGroupRule(group_id) {
- const result = await this.getAllDataByCondition({
- columns: ['id', 'group_id', 'group_name', 'name', 'condition', 'push_status', 'memo'],
- where: { group_id },
- });
- if (result.length === 0) return;
- result.forEach(x => {
- this.analysisiRule(x);
- });
- return { group_id, group_name: result[0].group_name, rules: result };
- }
- async getRule(id) {
- const result = await this.getDataById(id);
- this.analysisiRule(result);
- return result;
- }
- async getRules(ids) {
- const result = await this.getAllDataByCondition({ where: { id: ids } });
- for (const r of result) {
- this.analysisiRule(r);
- }
- return result;
- }
- async _delGroup(id) {
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.tableName, { group_id: id });
- // 下面这句update在qa上执行时,无法得到SET `group_id` = '',可能是对空值进行了过滤?
- // await conn.update(this.ctx.service.quality.tableName, { group_id: '' }, { where: { group_id: id } });
- await conn.query(`UPDATE ${this.ctx.service.quality.tableName} SET group_id = '' WHERE group_id = ?`, [id]);
- await conn.commit();
- } catch (err) {
- await conn.rollback();
- this.ctx.log(err);
- throw err;
- }
- return id;
- }
- async _saveGroup(id, group_name) {
- if (id) {
- await this.db.update(this.tableName, { group_name }, { where: { group_id: id } });
- return { group_id: id, group_name };
- }
- const newRule = {
- group_id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
- user_id: this.ctx.session.sessionUser.accountId,
- group_name: group_name || '新增规则组', name: '新增规则', condition: '[]', push_status: '[]',
- };
- const result = await this.db.insert(this.tableName, newRule);
- return await this.getGroupRule(newRule.group_id);
- }
- async saveGroup(data) {
- const result = {};
- try {
- if (data.add) result.add = await this._saveGroup();
- if (data.del) result.del = await this._delGroup(data.del);
- if (data.update) result.update = await this._saveGroup(data.update.group_id, data.update.group_name);
- } catch (err) {
- this.ctx.log(err);
- throw err;
- }
- return result;
- }
- async _saveRule(data) {
- if (data.id) {
- if (data.name) data.name = data.name;
- if (data.condition) data.condition = JSON.stringify(data.condition);
- if (data.push_status) data.push_status = JSON.stringify(data.push_status);
- await this.db.update(this.tableName, data);
- } else {
- data.pid = this.ctx.session.sessionProject.id;
- data.user_id = this.ctx.session.sessionUser.accountId;
- data.name = data.name || '新增规则';
- data.condition = data.condition ? JSON.stringify(data.condition) : '[]';
- data.push_status = data.push_status ? JSON.stringify(data.push_status) : '[]';
- const result = await this.db.insert(this.tableName, data);
- data.id = result.insertId;
- }
- return await this.getRule(data.id);
- }
- async _delRule(data) {
- await this.deleteById(data.id);
- return data;
- }
- async _copyRule(group_id, copy) {
- const copyId = copy.split(',').map(x => { return parseInt(x); });
- const rules = await this.getAllDataByCondition({ where: { id: copyId } });
- const group = await this.getDataByCondition({ group_id });
- if (!group) throw '您选择的规则组不存在,请刷新页面再试';
- const insertData = rules.map(rule => {
- return {
- pid: rule.pid, user_id: this.ctx.session.sessionUser.accountId,
- group_id: group.group_id, group_name: group.group_name,
- name: rule.name, condition: rule.condition, push_status: rule.push_status,
- };
- });
- const result = await this.db.insert(this.tableName, insertData);
- const ids = [];
- for (let i = 0; i < insertData.length; i++) {
- ids.push(result.insertId + i);
- }
- return await this.getRules(ids);
- }
- async saveRule(data) {
- const result = {};
- if (data.add) result.add = await this._saveRule(data.add);
- if (data.del) result.del = await this._delRule(data.del);
- if (data.update) result.update = await this._saveRule(data.update);
- if (data.copy) result.add = await this._copyRule(data.group_id, data.copy);
- return result;
- }
- }
- return QualityRule;
- };
|