sysRoleModel.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package role
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "perms-system-server/internal/consts"
  9. "github.com/zeromicro/go-zero/core/stores/cache"
  10. "github.com/zeromicro/go-zero/core/stores/sqlx"
  11. )
  12. var ErrUpdateConflict = errors.New("update conflict: data has been modified by another operation")
  13. var _ SysRoleModel = (*customSysRoleModel)(nil)
  14. type (
  15. SysRoleModel interface {
  16. sysRoleModel
  17. FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysRole, int64, error)
  18. FindByIds(ctx context.Context, ids []int64) ([]*SysRole, error)
  19. FindMinPermsLevelByUserIdAndProductCode(ctx context.Context, userId int64, productCode string) (int64, error)
  20. UpdateWithOptLock(ctx context.Context, data *SysRole, expectedUpdateTime int64) error
  21. }
  22. customSysRoleModel struct {
  23. *defaultSysRoleModel
  24. }
  25. )
  26. func NewSysRoleModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysRoleModel {
  27. return &customSysRoleModel{
  28. defaultSysRoleModel: newSysRoleModel(conn, c, cachePrefix, opts...),
  29. }
  30. }
  31. func (m *customSysRoleModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysRole, int64, error) {
  32. var total int64
  33. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  34. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  35. return nil, 0, err
  36. }
  37. var list []*SysRole
  38. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY `permsLevel` ASC, id DESC LIMIT ?,?", sysRoleRows, m.table)
  39. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  40. return nil, 0, err
  41. }
  42. return list, total, nil
  43. }
  44. func (m *customSysRoleModel) FindByIds(ctx context.Context, ids []int64) ([]*SysRole, error) {
  45. if len(ids) == 0 {
  46. return nil, nil
  47. }
  48. args := make([]interface{}, len(ids))
  49. marks := make([]string, len(ids))
  50. for i, id := range ids {
  51. args[i] = id
  52. marks[i] = "?"
  53. }
  54. var list []*SysRole
  55. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysRoleRows, m.table, strings.Join(marks, ","))
  56. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  57. return nil, err
  58. }
  59. return list, nil
  60. }
  61. func (m *customSysRoleModel) UpdateWithOptLock(ctx context.Context, data *SysRole, expectedUpdateTime int64) error {
  62. sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, data.Id)
  63. sysRoleProductCodeNameKey := fmt.Sprintf("%s%v:%v", cacheSysRoleProductCodeNamePrefix, data.ProductCode, data.Name)
  64. res, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  65. query := fmt.Sprintf("UPDATE %s SET `name`=?, `remark`=?, `status`=?, `permsLevel`=?, `updateTime`=? WHERE `id`=? AND `updateTime`=?", m.table)
  66. return conn.ExecCtx(ctx, query, data.Name, data.Remark, data.Status, data.PermsLevel, data.UpdateTime, data.Id, expectedUpdateTime)
  67. }, sysRoleIdKey, sysRoleProductCodeNameKey)
  68. if err != nil {
  69. return err
  70. }
  71. affected, _ := res.RowsAffected()
  72. if affected == 0 {
  73. return ErrUpdateConflict
  74. }
  75. return nil
  76. }
  77. func (m *customSysRoleModel) FindMinPermsLevelByUserIdAndProductCode(ctx context.Context, userId int64, productCode string) (int64, error) {
  78. var level int64
  79. query := fmt.Sprintf(
  80. "SELECT IFNULL(MIN(r.`permsLevel`), -1) FROM %s r INNER JOIN `sys_user_role` ur ON r.`id` = ur.`roleId` WHERE ur.`userId` = ? AND r.`productCode` = ? AND r.`status` = ?",
  81. m.table,
  82. )
  83. if err := m.QueryRowNoCacheCtx(ctx, &level, query, userId, productCode, consts.StatusEnabled); err != nil {
  84. return 0, err
  85. }
  86. if level < 0 {
  87. return 0, ErrNotFound
  88. }
  89. return level, nil
  90. }