| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | 'use strict';/** * 决策大屏用户查看权限-数据模型 * * @author ellisran * @date 2021/09/23 * @version */const accountGroup = require('../const/account_group').group;const paymentConst = require('../const/payment');module.exports = app => {    class paymentPermissionAudit extends app.BaseService {        constructor(ctx) {            super(ctx);            this.tableName = 'payment_permission_audit';        }        async getGroupInfo(pid, groupid) {            const sql = 'SELECT * FROM ?? WHERE pid = ? AND groupid = ? AND uid is NULL';            const sqlParam = [this.tableName, pid, groupid];            return await this.db.queryOne(sql, sqlParam);        }        async saveAudits(pid, accountList) {            const pushData = [];            for (const a of accountList) {                const data = {                    pid,                    groupid: a.account_group,                    uid: a.id,                    create_time: new Date(),                };                pushData.push(data);            }            return pushData.length > 0 ? await this.db.insert(this.tableName, pushData) : false;        }        async saveGroup(pid, groupid) {            const transaction = await this.db.beginTransaction();            try {                // 删除所在组的用户                await transaction.delete(this.tableName, { pid, groupid });                const data = {                    pid,                    groupid,                    create_time: new Date(),                };                await transaction.insert(this.tableName, data);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async delAudit(id) {            return await this.db.delete(this.tableName, { id });        }        async getOnePermission(is_admin, uid) {            if (is_admin) {                return paymentConst.audit_admin_permission;            }            const info = await this.getDataByCondition({ uid });            if (!info) return false;            info.permission_json = info.permission_json ? JSON.parse(info.permission_json) : paymentConst.audit_permission;            return info.permission_json;        }        async getList(pid) {            const list = await this.db.select(this.tableName, { where: { pid }, orders: [['id', 'desc']] });            for (const l of list) {                if (l.uid) {                    const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);                    l.name = accountInfo.name;                    l.company = accountInfo.company;                    l.permission_json = l.permission_json ? JSON.parse(l.permission_json) : paymentConst.audit_permission;                } else {                    l.name = accountGroup[l.groupid];                }            }            return list;        }        async updateOnePermission(updateData) {            if (!updateData.id || !updateData.permission_json) {                return false;            }            updateData.permission_json = JSON.stringify(updateData.permission_json);            return await this.db.update(this.tableName, updateData);        }        async updateAllPermission(pid, type, value) {            if (this._.indexOf(paymentConst.permission_type_array, type) === -1 || this._.indexOf(paymentConst.permission_value_array, value) === -1) {                return false;            }            const permissionAudits = await this.getList(pid);            if (permissionAudits.length > 0) {                const updateList = [];                for (const pa of permissionAudits) {                    if (pa.permission_json[type] !== value) {                        pa.permission_json[type] = value;                        updateList.push({                            id: pa.id,                            permission_json: JSON.stringify(pa.permission_json),                        });                    }                }                if (updateList.length > 0) await this.db.updateRows(this.tableName, updateList);            }            return true;        }    }    return paymentPermissionAudit;};
 |