'use strict'; /** * * * @author Mai * @date * @version */ module.exports = app => { class subProjPermission extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'sub_project_permission'; this.PermissionConst = { budget: { view: { title: '查看', value: 1 }, edit: { title: '编辑', value: 2 }, }, file: { view: { title: '查看', value: 1 }, upload: { title: '上传文件', value: 2 }, edit: { title: '文件类别编辑', value: 3 }, }, manage: { rela: { title: '关联标段', value: 1 }, } }; } async showSubTab(uid, type) { const sql = `SELECT count(*) FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`; const result = await this.db.queryOne(sql, [uid]); return result.count; } async showBudget(uid) { return await this.showSubTab(uid, 'budget'); } async showFile(uid) { return await this.showSubTab(uid, 'file'); } parsePermission(data) { const _ = this.ctx.helper._; const datas = data instanceof Array ? data : [data]; datas.forEach(x => { x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : []; x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : []; x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : []; }); } async getPermission(subProjectId) { const result = await this.db.query(`SELECT spp.*, p.name, p.role FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]); this.parsePermission(result); return result; } async getBudgetPermission(subProjectId) { const result = await this.db.query(`SELECT spp.*, p.name, p.role FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]); this.parsePermission(result); return result; } async getUserPermission(pid, uid) { const result = await this.getAllDataByCondition({ where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id } }); this.parsePermission(result); return result; } async getBudgetUserPermission(bid) { const _ = this.ctx.helper._; const result = await this.getDataByCondition({uid: this.ctx.session.sessionUser.accountId, bid}); if (result) this.parsePermission(result); return result; } async savePermission(subProjectId, member) { const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } }); const dm = [], um = [], im = []; for (const om of orgMember) { const nm = member.find(x => { return om.uid === x.uid; }); if (!nm) { dm.push(om.id); } else { um.push({ id: om.id, budget_permission: nm.budget_permission.join(','), file_permission: nm.file_permission.join(','), manage_permission: nm.manage_permission.join(',') }); member.splice(member.indexOf(nm), 1); } } for (const m of member) { im.push({ spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid, budget_permission: m.budget_permission.join(','), file_permission: m.file_permission.join(','), manage_permission: m.manage_permission.join(',') }) } const conn = await this.db.beginTransaction(); try { if (dm.length > 0) await conn.delete(this.tableName, { id: dm }); if (um.length > 0) await conn.updateRows(this.tableName, um); if (im.length > 0) await conn.insert(this.tableName, im); await conn.commit(); } catch (err) { await conn.rollback(); throw err; } } } return subProjPermission; };