| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 | 'use strict';/** * 决策大屏用户查看权限-数据模型 * * @author ellisran * @date 2021/09/23 * @version */const accountGroup = require('../const/account_group').group;module.exports = app => {    class datacollectAudit extends app.BaseService {        constructor(ctx) {            super(ctx);            this.tableName = 'datacollect_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 getCompanyInfo(pid, companyId) {            const sql = 'SELECT * FROM ?? WHERE pid = ? AND company_id = ? AND uid is NULL';            const sqlParam = [this.tableName, pid, companyId];            return await this.db.queryOne(sql, sqlParam);        }        async saveAudit(pid, uid) {            const data = {                pid,                uid,                create_time: new Date(),            };            return await this.db.insert(this.tableName, data);        }        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 saveCompany(pid, companyId) {            const transaction = await this.db.beginTransaction();            try {                // const companyInfo = await this.ctx.service.constructionUnit.getDataById(companyId);                // if (!companyInfo) throw '单位不存在';                // await transaction.delete(this.tableName, { pid, company_id: companyId });                // const nullCompanyList = await this.getAllDataByCondition({ where: { pid, company_id: null } });                // if (nullCompanyList.length > 0) {                //     const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { company: companyInfo.name } });                //     await transaction.delete(this.tableName, { pid, uid: this._.map(userList, 'id') });                // }                const data = {                    pid,                    company_id: companyId,                    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 getList(pid) {            const list = await this.db.select(this.tableName, { where: { pid } });            for (const l of list) {                if (l.uid) {                    const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);                    l.name = accountInfo.name;                } else if (l.groupid) {                    l.name = accountGroup[l.groupid];                } else if (l.company_id) {                    const companyInfo = await this.ctx.service.constructionUnit.getDataById(l.company_id);                    l.name = companyInfo ? companyInfo.name : null;                }            }            return list;        }    }    return datacollectAudit;};
 |