roles.service.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const connection = require('../sql');
  2. class RoleService {
  3. // 查询总条数
  4. async listCount(projectId, name) {
  5. const statement = "SELECT COUNT(`id`) total FROM roles WHERE project_id = ? and (name LIKE COALESCE(CONCAT('%',?,'%'), name) OR ? IS NULL) ;";
  6. const [result] = await connection.execute(statement, [projectId, name || null, name || null]);
  7. return result[0];
  8. }
  9. async list(projectId, name, pageNum = 1, pageSize = 10) {
  10. const offset = (+pageNum - 1) * pageSize + '';
  11. const limit = pageSize;
  12. const statement = `SELECT id,project_id projectId,name,half_checked halfChecked,checked,remark,updated_at updatedAt,created_at createdAt,user_id userId,user_name userName FROM roles WHERE project_id = ? and (name LIKE COALESCE(CONCAT('%',?,'%'), name) OR ? IS NULL) ORDER BY updated_at DESC LIMIT ${offset},${limit};`;
  13. const [result] = await connection.execute(statement, [projectId, name || null, name || null]);
  14. return result;
  15. }
  16. async listAll(projectId) {
  17. const statement =
  18. 'SELECT id,project_id projectId,name,half_checked halfChecked,checked,remark,updated_at updatedAt,created_at createdAt,user_id userId,user_name userName FROM roles WHERE project_id = ?;';
  19. const [result] = await connection.execute(statement, [projectId]);
  20. return result;
  21. }
  22. async create(projectId, name, remark, userId, userName) {
  23. const statement = 'INSERT INTO roles (project_id, name, remark, user_id, user_name) VALUES (?, ?, ?, ?, ?);';
  24. const [result] = await connection.execute(statement, [projectId, name, remark, userId, userName]);
  25. return result;
  26. }
  27. async delete(id, projectId) {
  28. let result = [];
  29. const statement = 'DELETE FROM roles WHERE id = ? && project_id = ?';
  30. [result] = await connection.execute(statement, [id, projectId]);
  31. return result;
  32. }
  33. async deleteByProjectId(projectId) {
  34. let result = [];
  35. const statement = 'DELETE FROM roles WHERE project_id = ?';
  36. [result] = await connection.execute(statement, [projectId]);
  37. return result;
  38. }
  39. async update(id, projectId, name = '', remark = '') {
  40. let result = [];
  41. const statement = 'UPDATE roles SET name = ?, remark = ? WHERE id = ? && project_id = ?';
  42. [result] = await connection.execute(statement, [name, remark, id, projectId]);
  43. return result;
  44. }
  45. async updateLimits(id, projectId, checked = '', halfChecked = '') {
  46. let result = [];
  47. const statement = 'UPDATE roles SET checked = ?, half_checked = ? WHERE id = ? && project_id = ?';
  48. [result] = await connection.execute(statement, [checked, halfChecked, id, projectId]);
  49. return result;
  50. }
  51. // 根据角色ID查询权限ID
  52. async getRoleInfo(id) {
  53. const statement =
  54. 'SELECT id,project_id projectId,name,half_checked halfChecked,checked,remark,updated_at updatedAt,created_at createdAt,user_id userId,user_name userName FROM roles WHERE id = ?';
  55. const [result] = await connection.execute(statement, [id]);
  56. return result[0];
  57. }
  58. // 用户注销所有角色
  59. async deleteAllRole(userId) {
  60. const statement = 'DELETE FROM roles WHERE user_id = ?';
  61. const [result] = await connection.execute(statement, [userId]);
  62. return result;
  63. }
  64. }
  65. module.exports = new RoleService();