'use strict'; /** * * * @author Mai * @date * @version */ module.exports = app => { class BudgetPermission extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'budget_permission'; this.PermissionConst = { view: { title: '查看', value: 1 }, edit: { title: '编辑', value: 2 }, }; } async showBudget(uid) { const count = await this.count({ pid: this.ctx.session.sessionProject.id, uid }); return count > 0; } async getBudgetPermission(bid) { const _ = this.ctx.helper._; const result = await this.db.query(`SELECT bp.*, p.name, p.role FROM ${this.tableName} bp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p On bp.uid = p.id WHERE bid = ?`, [bid]); result.forEach(x => { x.permission = x.permission ? _.map(x.permission.split(','), _.toInteger) : [] }); return result; } async getUserPermission() { const _ = this.ctx.helper._; const result = await this.getAllDataByCondition({ where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id } }); result.forEach(x => { x.permission = x.permission ? _.map(x.permission.split(','), _.toInteger) : [] }); return result; } async getBudgetUserPermission(bid) { const _ = this.ctx.helper._; const result = await this.getDataByCondition({uid: this.ctx.session.sessionUser.accountId, bid}); if (result) result.permission = result.permission ? _.map(result.permission.split(','), _.toInteger) : []; return result; } async saveBudgetPermission(bid, member) { const orgMember = await this.getAllDataByCondition({ where: { bid } }); const dm = [], um = [], im = [], cur = new Date(); 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, permission: nm.permission.join(','), modify_time: cur, }); member.splice(member.indexOf(nm), 1); } } for (const m of member) { im.push({ pid: this.ctx.session.sessionProject.id, bid, uid: m.uid, permission: m.permission.join(','), in_time: cur, modify_time: cur, }) } 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 BudgetPermission; };