| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 'use strict';
- /**
- * Created by Tony on 2025/12/30.
- */
- const BaseService = require('../base/base_service');
- module.exports = app => {
- class RptPermission extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'rpt_permission';
- this.dataId = 'id';
- }
- async getPermissionById(id) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('id', {
- value: id,
- operate: '=',
- });
- this.sqlBuilder.columns = ['id', 'tid', 'user_id', 'rpt_id', 'create_time'];
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- async getAllTenderPermissions(tid) {
- const sql = 'SELECT tt.*,' +
- 'pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- 'FROM ?? As tt LEFT JOIN ?? As pa ON tt.`user_id` = pa.`id` ' +
- 'WHERE tt.`tid` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
- return await this.db.query(sql, sqlParam);
- }
- async getTenderPermissions(tid, rpt_id) {
- const sql = 'SELECT tt.*,' +
- 'pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- 'FROM ?? As tt LEFT JOIN ?? As pa ON tt.`user_id` = pa.`id` ' +
- 'WHERE tt.`tid` = ? and tt.`rpt_id`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, rpt_id];
- return await this.db.query(sql, sqlParam);
- }
- async getPermissions(tid, user_id, rpt_id) {
- const sql = 'SELECT tt.*,' +
- 'pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- 'FROM ?? As tt LEFT JOIN ?? As pa ON tt.`user_id` = pa.`id` ' +
- 'WHERE tt.`tid` = ? and tt.`user_id` = ? and tt.`rpt_id`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, user_id, rpt_id];
- return await this.db.query(sql, sqlParam);
- }
- async insertRptPermission(permission) {
- let rst = null;
- this.transaction = await this.db.beginTransaction();
- try {
- const data = {
- tid: permission.tid,
- user_id: permission.user_id,
- rpt_id: permission.rpt_id,
- create_time: new Date(),
- };
- rst = await this.transaction.insert(this.tableName, data);
- await this.transaction.commit();
- } catch (ex) {
- console.log(ex);
- // 回滚
- await this.transaction.rollback();
- }
- return rst;
- }
- async deletePermission(permission) {
- const transaction = await this.db.beginTransaction();
- try {
- const condition = { tid: permission.tid, user_id: permission.user_id, rpt_id: permission.rpt_id };
- const pm = await this.getPermissions(permission.tid, permission.user_id, permission.rpt_id);
- if (!pm) {
- throw '该用户权限不存在';
- }
- await transaction.delete(this.tableName, condition);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async deletePermissionById(id) {
- let rst = null;
- this.transaction = await this.db.beginTransaction();
- try {
- rst = await this.transaction.delete(this.tableName, { id });
- await this.transaction.commit();
- //
- } catch (ex) {
- console.log(ex);
- // 回滚
- await this.transaction.rollback();
- }
- return rst;
- }
- async batchApplyPermission(tid, userIds = [], rptIds = []) {
- if (rptIds.length > 0 && userIds.length > 0) {
- const transaction = await this.db.beginTransaction();
- try {
- const condition = { tid, user_id: userIds, rpt_id: rptIds };
- await transaction.delete(this.tableName, condition);
- for (const uId of userIds) {
- for (const rId of rptIds) {
- const data = { tid, user_id: uId, rpt_id: rId, create_time: new Date() };
- await transaction.insert(this.tableName, data);
- }
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- }
- }
- return RptPermission;
- };
|