123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- '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 = 'sub_project_permission';
- this.BudgetPermissionConst = {
- view: { title: '查看', value: 1 },
- edit: { title: '编辑', value: 2 },
- };
- this.FilePermissionConst = {
- view: { title: '查看', value: 1 },
- upload: { title: '上传文件', value: 2 },
- edit: { title: '文件类别编辑', value: 3 },
- };
- this.ManagePermissionConst = {
- 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 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() {
- 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 saveBudgetPermission(subProjectId, member) {
- const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
- const um = [], im = [];
- for (const om of orgMember) {
- const nm = member.find(x => { return om.uid === x.uid; });
- if (!nm) {
- um.push({ id: om.id, budget_permission: '' });
- } else {
- um.push({ id: om.id, budget_permission: nm.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.permission.join(',')
- })
- }
- const conn = await this.db.beginTransaction();
- try {
- 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 saveFilePermission(subProjectId, uid, permission) {
- const member = await this.getDataByCondition({ where: { spid: subProjectId, uid } });
- if (!permission) {
- if (!member) return;
- await this.defaultUpdate({ id: member.id, file_permission: '', manager_permission: '' });
- } else {
- if (!member) {
- await this.db.insert(this.tableName, { spid: subProjectId, uid, pid: this.ctx.sessionProject.id, ...permission });
- } else {
- await this.db.update(this.tableName, { id: member.id, ...permission });
- }
- }
- }
- }
- return BudgetPermission;
- };
|