123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 'use strict';
- /**
- * 联合限制计量
- *
- * @author Mai
- * @date 2024/6/27
- * @version
- */
- module.exports = app => {
- class MultiLimit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'multi_limit';
- }
- async getLimitList(pid) {
- const sql = `SELECT limit_id, name FROM ${this.tableName} WHERE pid = ? GROUP BY limit_id`;
- return await this.db.query(sql, [pid]);
- }
- analysisLimits(datas) {
- const result = [];
- datas.forEach(x => {
- x.condition = x.condition ? JSON.parse(x.condition) : [];
- let limit = result.find(r => { return x.limit_id === r.limit_id; });
- if (!limit) {
- limit = { limit_id: x.limit_id, name: x.name, options: [] };
- result.push(limit);
- }
- limit.options.push(x);
- });
- return result;
- }
- async getLimits(pid) {
- const result = await this.getAllDataByCondition({
- columns: ['id', 'limit_id', 'name', 'limit', 'lower', 'upper', 'condition', 'hint'],
- where: { pid },
- });
- return this.analysisLimits(result);
- }
- async getLimitOptions(limit_id) {
- const result = await this.getAllDataByCondition({
- columns: ['limit_id', 'name', 'limit', 'lower', 'upper', 'condition', 'hint'],
- where: { limit_id },
- });
- result.forEach(x => { x.condition = x.condition ? JSON.parse(x.condition) : []; });
- return result;
- }
- async getLimitOption(id) {
- const result = await this.getDataById(id);
- result.condition = result.condition ? JSON.parse(result.condition) : [];
- return result;
- }
- async _delLimit(id) {
- await this.db.delete(this.tableName, { limit_id: id });
- return id;
- }
- async _saveLimit(id, name) {
- if (id) {
- await this.db.update(this.tableName, { name }, { where: { limit_id: id } });
- return { limit_id: id, name };
- }
- const newLimitOption = {
- limit_id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
- user_id: this.ctx.session.sessionUser.accountId,
- name: name || '新增计量配置', condition: '[]', limit: 1,
- };
- const result = await this.db.insert(this.tableName, newLimitOption);
- const lo = await this.getLimitOption(result.insertId);
- return { limit_id: lo.limit_id, name: lo.name, options: [lo] };
- }
- async saveLimit(data) {
- const result = {};
- if (data.add) result.add = await this._saveLimit();
- if (data.del) result.del = await this._delLimit(data.del);
- if (data.update) result.update = await this._saveLimit(data.update.limit_id, data.update.name);
- return result;
- }
- async _saveLimitOption(data) {
- if (data.id) {
- data.condition = JSON.stringify(data.condition);
- await this.db.update(this.tableName, data);
- } else {
- data.pid = this.ctx.session.sessionProject.id;
- data.user_id = this.ctx.session.sessionUser.accountId;
- data.condition = JSON.stringify(data.condition);
- const result = await this.db.insert(this.tableName, data);
- data.id = result.insertId;
- }
- return await this.getLimitOption(data.id);
- }
- async _delLimitOption(data) {
- await this.deleteById(data.id);
- return data;
- }
- async saveLimitOption(data) {
- const result = {};
- if (data.add) result.add = await this._saveLimitOption(data.add);
- if (data.del) result.del = await this._delLimitOption(data.del);
- if (data.update) result.update = await this._saveLimitOption(data.update);
- return result;
- }
- }
- return MultiLimit;
- };
|