'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._definePermission(); } _definePermission() { this.PermissionConst = { budget: { view: { title: '查看', value: 1 }, edit: { title: '编辑', value: 2 }, }, file: { view: { title: '查看', value: 1 }, upload: { title: '上传/引用', value: 2 }, delete: { title: '删除文件', value: 4 }, filing: { title: '文件类别编辑', value: 3 }, }, manage: { rela: { title: '关联标段', value: 1 }, }, info: { view: { title: '查看', value: 1}, edit: { title: '编辑', value: 2 }, }, datacollect: { view: { title: '查看', value: 1}, }, contract: { edit: { title: '编辑节点', value: 1 }, add: { title: '添加合同', value: 2 }, node: { title: '授权查看本节点合同', value: 3 }, unit: { title: '授权查看本单位合同', value: 4 }, }, fund_trans: { view: { title: '查看', value: 1 }, add: { title: '新建划拨', value: 2 }, att: { title: '上传附件', value: 3 }, }, fund_pay: { view: { title: '查看', value: 1 }, att: { title: '上传附件', value: 3 }, }, }; this.PermissionBlock = [ { key: 'datacollect', name: '决策大屏', field: 'datacollect_permission' }, { key: 'info', name: '项目概况', field: 'info_permission' }, { key: 'contract', name: '合同管理', field: 'contract_permission', hint: ['1、编辑节点:编辑合同管理内页树结构', '2、添加合同:允许添加合同', '3、授权查看本节点合同:授权节点下查看所有人上传的合同', '4、授权查看本单位合同:授权节点下查看本单位人员添加的所有合同'] }, { key: 'file', name: '资料归集', field: 'file_permission' }, { key: 'budget', name: '动态投资', field: 'budget_permission' }, { key: 'financial', name: '资金监管', children: [ { key: 'fund_trans', name: '资金划拨', field: 'fund_trans_permission' }, { key: 'fund_pay', name: '资金支付', field: 'fund_pay_permission' }, ] }, ]; for (const p of this.PermissionBlock) { if (p.children) { for (const c of p.children) { c.permission = []; const pConst = this.PermissionConst[c.key]; if (!pConst) continue; for (const prop in pConst) { c.permission.push({ key: prop, ...pConst[prop]}); } } } else { p.permission = []; const pConst = this.PermissionConst[p.key]; if (!pConst) continue; for (const prop in pConst) { p.permission.push({ key: prop, ...pConst[prop]}); } } } } get adminPermission () { return { budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'), file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'), manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'), filing_type: 'all', } } async showSubTab(uid, type) { const sql = `SELECT count(*) as 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) : []; x.filing_type = x.filing_type ? _.map(x.filing_type.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 subProj = await this.service.subProject.getDataByCondition({ budget_id: bid }); const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId }); if (result) this.parsePermission(result); return result; } async getSubProjectUserPermission(spid, uid) { const result = await this.getDataByCondition({ spid, uid }); 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({ id: this.uuid.v4(), 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; } } async _addUser(subProject, data) { const ids = data instanceof Array ? data : [data]; const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } }); if (exists.length > 0) throw '请勿重复选择账号'; const insertData = ids.map(x => { return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x }; }); await this.db.insert(this.tableName, insertData); return insertData; } async _delUser(subProject, data) { const ids = data instanceof Array ? data : [data]; const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } }); await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) }); return data; } async _updateUserPermission(data) { const datas = data instanceof Array ? data : [data]; const updateData = []; datas.forEach(x => { const ud = { id: x.id }; for (const p of this.PermissionBlock) { if (p.children) { for (const c of p.children) { if (data[c.field] !== undefined) ud[c.field] = x[c.field] || ''; } } else { if (data[p.field] !== undefined) ud[p.field] = x[p.field] || ''; } } updateData.push(ud); }); await this.db.updateRows(this.tableName, updateData); return updateData; } async updatePermission(subProject, data) { const result = {}; if (data.add) result.add = await this._addUser(subProject, data.add); if (data.del) result.del = await this._delUser(subProject, data.del); if (data.update) result.update = await this._updateUserPermission(data.update); return result; } async getFilingType(subProjectId) { const permissionConst = {}, prefix = 'f'; for (const p in this.PermissionConst.file) { const fp = this.PermissionConst.file[p]; permissionConst[prefix + fp.value] = fp.title; } const result = await this.db.query(`SELECT spp.id, p.name, p.role, p.company, p.mobile, spp.file_permission, spp.filing_type, spp.create_time FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]); result.forEach(x => { const filePermission = x.file_permission.split(','); x.file_permission = filePermission.map(x => { return permissionConst[prefix + x] || ''; }).join(','); }); return result; } // 资料归集,授权固定分类 async saveFilingType(data) { const updateData = []; data.forEach(x => { updateData.push({ id: x.id, filing_type: x.filing_type }); }); if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData); } } return subProjPermission; };