Jelajahi Sumber

fix: 补充

Tony Kang 2 minggu lalu
induk
melakukan
288c3a5a7b
2 mengubah file dengan 168 tambahan dan 0 penghapusan
  1. 38 0
      app/controller/report_permission_controller.js
  2. 130 0
      app/service/rpt_permission.js

+ 38 - 0
app/controller/report_permission_controller.js

@@ -0,0 +1,38 @@
+'use strict';
+
+/**
+ * Created by Tony on 2025/12/30.
+ */
+
+module.exports = app => {
+    class ReportPermissionController extends app.BaseController {
+        async getTenderRptPermissions(ctx) {
+            const params = JSON.parse(ctx.request.body.params);
+            const permissions = await ctx.service.rptPermission.getTenderPermissions(params.tid, params.rpt_id);
+            ctx.body = { permissions };
+            ctx.status = 201;
+        }
+
+        async addRptPermission(ctx) {
+            const params = JSON.parse(ctx.request.body.params);
+            const rst = await ctx.service.rptPermission.insertRptPermission(params);
+            ctx.body = { msg: rst };
+            ctx.status = 201;
+        }
+
+        async deleteRptPermission(ctx) {
+            const params = JSON.parse(ctx.request.body.params);
+            const rst = await ctx.service.rptPermission.deletePermission(params);
+            ctx.body = { msg: rst };
+            ctx.status = 201;
+        }
+
+        async batchApplyPermission(ctx) {
+            const params = JSON.parse(ctx.request.body.params);
+            const rst = await ctx.service.rptPermission.batchApplyPermission(params.tid, params.userIds, params.rptIds);
+            ctx.body = { msg: rst };
+            ctx.status = 201;
+        }
+    }
+    return ReportPermissionController;
+};

+ 130 - 0
app/service/rpt_permission.js

@@ -0,0 +1,130 @@
+'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 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;
+};