'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, transaction = null) { // 判断是否已存在该用户,存在则不插入 const pauditList = await this.getAllDataByCondition({ where: { pid } }); const pushData = []; for (const a of this._.uniqBy(accountList, 'id')) { if (this._.findIndex(pauditList, { uid: a.id }) === -1) { const data = { pid, groupid: a.account_group, uid: a.id, create_time: new Date(), }; pushData.push(data); } } if (pushData.length > 0) { return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData); } return 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, spid, 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; };