'use strict'; /** * * * @author Mai * @date 2025/7/17 * @version */ module.exports = app => { class tenderPermission extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'tender_permission'; this.PermissionConst = { quality: { view: { title: '查看', value: 1, isDefault: 1 }, upload: { title: '上传文件', value: 2 }, add: { title: '新增功能', value: 3 }, add_inspection: { title: '添加质量巡检', value: 4 }, }, }; this.PermissionBlock = [ { key: 'quality', name: '质量管理', field: 'quality' }, ]; 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]}); } } } this.AdminPermission = {}; for (const p in this.PermissionConst) { this.AdminPermission[p] = this.ctx.helper.mapAllSubField(this.PermissionConst[p], 'value'); } } partPermissionConst(part) { if (!part) return this.PermissionConst; const parts = part instanceof Array ? part : [part]; const result = {}; for (const p of parts) { result[p] = this.PermissionConst[p]; } return result; } partPermissionBlock(part) { if (!part) return this.PermissionBlock; const parts = part instanceof Array ? part : [part]; const result = this.PermissionBlock.filter(x => { return parts.indexOf(x.key) >= 0; }); return result; } parsePermission(data) { const _ = this.ctx.helper._; const datas = data instanceof Array ? data : [data]; datas.forEach(x => { for (const p in this.PermissionConst) { x[p] = x[p] ? _.map(x[p].split(','), _.toInteger) : []; } }); } // 权限检查 conversePermission(permission) { const result = {}; for (const block of this.PermissionBlock) { const per = {}; for(const p of block.permission) { per[p.key] = permission[block.key].indexOf(p.value) >= 0; } result[block.key] = per; } return result; } getAdminPermission() { return this.conversePermission(this.AdminPermission); } async getUserPermission(tid, uid) { const result = await this.getDataByCondition({ tid, uid }); this.parsePermission(result); return this.conversePermission(result); } // 用户权限编辑 async getPartsPermission(tid, parts) { if (!parts || parts.length === 0) return []; const partSql = parts.map(x => { return `${x} <> ''`}).join(' OR '); const sql = `SELECT qp.*, pa.name, pa.role FROM ${this.tableName} qp LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON qp.uid = pa.id WHERE qp.tid = ? AND (${partSql}) ORDER BY qp.create_time DESC`; const result = await this.db.query(sql, [tid]); this.parsePermission(result); return result; } async savePermission(tid, member, permissionBlock) { const orgMember = await this.getAllDataByCondition({ where: { tid } }); const updateMember = [], insertMember = []; for (const om of orgMember) { const nmi = member.findIndex(x => { return om.uid == x.uid; }); if (nmi < 0) { const um = { id: om.id }; for (const p of permissionBlock) { um[p] = ''; } updateMember.push(um); } else { const nm = member[nmi]; const um = { id: om.id }; for (const p in this.PermissionConst) { if (nm[p]) um[p] = nm[p].join(','); } updateMember.push(um); member.splice(nmi, 1); } } for (const m of member) { const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid, uid: m.uid }; for (const p in this.PermissionConst) { if (m[p]) im[p] = m[p].join(','); } insertMember.push(im); } const conn = await this.db.beginTransaction(); try { if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember); if (insertMember.length > 0) await conn.insert(this.tableName, insertMember); await conn.commit(); } catch (err) { await conn.rollback(); throw err; } } async saveOnePermission(tid, uids, member, permissionBlock, transaction = null) { const orgMember = await this.getAllDataByCondition({ where: { tid, uid: uids } }); const updateMember = [], insertMember = []; for (const om of orgMember) { const nmi = member.findIndex(x => { return om.uid == x.uid; }); if (nmi < 0) { const um = { id: om.id }; for (const p of permissionBlock) { um[p] = ''; } updateMember.push(um); } else { const nm = member[nmi]; const um = { id: om.id }; for (const p in this.PermissionConst) { if (nm[p]) um[p] = nm[p].join(','); } updateMember.push(um); member.splice(nmi, 1); } } for (const m of member) { const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid, uid: m.uid }; for (const p in this.PermissionConst) { if (m[p]) im[p] = m[p].join(','); } insertMember.push(im); } if (!transaction) { const conn = await this.db.beginTransaction(); try { if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember); if (insertMember.length > 0) await conn.insert(this.tableName, insertMember); await conn.commit(); } catch (err) { await conn.rollback(); throw err; } } else { if (updateMember.length > 0) await transaction.updateRows(this.tableName, updateMember); if (insertMember.length > 0) await transaction.insert(this.tableName, insertMember); } } async setOtherTender(tidList, userList, permissionBlock) { // 根据标段找出创建人去除,已存在的先删除再插入 const transaction = await this.db.beginTransaction(); try { const tenderList = await this.ctx.service.tender.getAllDataByCondition({ columns: ['id', 'user_id'], where: { id: tidList.split(',') }, }); const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } }); const insertData = []; const updateData = []; for (const user of userList) { for (const t of tenderList) { const updateInfo = this._.find(oldTouristList, { tid: t.id, uid: user.uid }); // if (delId) deleteIdData.push(delId.id); if (updateInfo) { const um = { id: updateInfo.id }; for (const p of permissionBlock) { um[p] = user.member.join(','); } updateData.push(um); } else if (user.uid !== t.user_id) { const um = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid: t.id, uid: user.uid }; for (const p of permissionBlock) { um[p] = user.member.join(','); } insertData.push(um); } } } if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData); if (insertData.length > 0) await transaction.insert(this.tableName, insertData); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } } return tenderPermission; };