123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- '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 },
- filing: { title: '文件类别编辑', value: 3 },
- },
- manage: {
- rela: { title: '关联标段', value: 1 },
- }
- };
- }
- 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 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;
- };
|