sysPermModel.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package perm
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "perms-system-server/internal/consts"
  8. "github.com/zeromicro/go-zero/core/stores/cache"
  9. "github.com/zeromicro/go-zero/core/stores/sqlx"
  10. )
  11. var _ SysPermModel = (*customSysPermModel)(nil)
  12. type (
  13. SysPermModel interface {
  14. sysPermModel
  15. FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error)
  16. FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error)
  17. FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error)
  18. FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error)
  19. // FindMapByProductCodeWithTx 在事务内查询权限快照;配合 SysProductModel.LockByCodeTx 锁住
  20. // product 行,可把"读取现有权限 → 增/改/禁用"这段与其他 SyncPermissions 串行化,
  21. // 避免两次并发同步都认为 code X 不存在并并发 INSERT 导致 1062(见审计 M-6)。
  22. FindMapByProductCodeWithTx(ctx context.Context, session sqlx.Session, productCode string) (map[string]*SysPerm, error)
  23. DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error)
  24. }
  25. customSysPermModel struct {
  26. *defaultSysPermModel
  27. }
  28. )
  29. func NewSysPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysPermModel {
  30. return &customSysPermModel{
  31. defaultSysPermModel: newSysPermModel(conn, c, cachePrefix, opts...),
  32. }
  33. }
  34. func (m *customSysPermModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error) {
  35. var total int64
  36. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  37. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  38. return nil, 0, err
  39. }
  40. var list []*SysPerm
  41. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY id DESC LIMIT ?,?", sysPermRows, m.table)
  42. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  43. return nil, 0, err
  44. }
  45. return list, total, nil
  46. }
  47. func (m *customSysPermModel) FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error) {
  48. var codes []string
  49. query := fmt.Sprintf("SELECT `code` FROM %s WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusEnabled)
  50. if err := m.QueryRowsNoCacheCtx(ctx, &codes, query, productCode); err != nil {
  51. return nil, err
  52. }
  53. return codes, nil
  54. }
  55. func (m *customSysPermModel) FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error) {
  56. if len(ids) == 0 {
  57. return nil, nil
  58. }
  59. placeholders := make([]string, len(ids))
  60. args := make([]interface{}, len(ids))
  61. for i, id := range ids {
  62. placeholders[i] = "?"
  63. args[i] = id
  64. }
  65. var list []*SysPerm
  66. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysPermRows, m.table, strings.Join(placeholders, ","))
  67. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  68. return nil, err
  69. }
  70. return list, nil
  71. }
  72. func (m *customSysPermModel) FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error) {
  73. var list []*SysPerm
  74. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
  75. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
  76. return nil, err
  77. }
  78. result := make(map[string]*SysPerm, len(list))
  79. for _, p := range list {
  80. result[p.Code] = p
  81. }
  82. return result, nil
  83. }
  84. func (m *customSysPermModel) FindMapByProductCodeWithTx(ctx context.Context, session sqlx.Session, productCode string) (map[string]*SysPerm, error) {
  85. var list []*SysPerm
  86. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
  87. if err := session.QueryRowsCtx(ctx, &list, query, productCode); err != nil {
  88. return nil, err
  89. }
  90. result := make(map[string]*SysPerm, len(list))
  91. for _, p := range list {
  92. result[p.Code] = p
  93. }
  94. return result, nil
  95. }
  96. func (m *customSysPermModel) DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error) {
  97. // 先查出将被禁用的行,构建缓存 key
  98. var findQuery string
  99. var findArgs []interface{}
  100. if len(codes) == 0 {
  101. findQuery = fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = %d", sysPermRows, m.table, consts.StatusEnabled)
  102. findArgs = []interface{}{productCode}
  103. } else {
  104. placeholders := make([]string, len(codes))
  105. findArgs = make([]interface{}, 0, len(codes)+1)
  106. findArgs = append(findArgs, productCode)
  107. for i, code := range codes {
  108. placeholders[i] = "?"
  109. findArgs = append(findArgs, code)
  110. }
  111. findQuery = fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = %d AND `code` NOT IN (%s)",
  112. sysPermRows, m.table, consts.StatusEnabled, strings.Join(placeholders, ","))
  113. }
  114. var affected []*SysPerm
  115. if err := session.QueryRowsCtx(ctx, &affected, findQuery+" FOR UPDATE", findArgs...); err != nil {
  116. return 0, err
  117. }
  118. if len(affected) == 0 {
  119. return 0, nil
  120. }
  121. keys := make([]string, 0, len(affected)*2)
  122. for _, data := range affected {
  123. keys = append(keys,
  124. fmt.Sprintf("%s%v", cacheSysPermIdPrefix, data.Id),
  125. fmt.Sprintf("%s%v:%v", cacheSysPermProductCodeCodePrefix, data.ProductCode, data.Code),
  126. )
  127. }
  128. var updateQuery string
  129. var updateArgs []interface{}
  130. if len(codes) == 0 {
  131. updateQuery = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusDisabled, consts.StatusEnabled)
  132. updateArgs = []interface{}{now, productCode}
  133. } else {
  134. placeholders := make([]string, len(codes))
  135. updateArgs = make([]interface{}, 0, len(codes)+2)
  136. updateArgs = append(updateArgs, now, productCode)
  137. for i, code := range codes {
  138. placeholders[i] = "?"
  139. updateArgs = append(updateArgs, code)
  140. }
  141. updateQuery = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d AND `code` NOT IN (%s)",
  142. m.table, consts.StatusDisabled, consts.StatusEnabled, strings.Join(placeholders, ","))
  143. }
  144. res, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) {
  145. return session.ExecCtx(ctx, updateQuery, updateArgs...)
  146. }, keys...)
  147. if err != nil {
  148. return 0, err
  149. }
  150. rows, _ := res.RowsAffected()
  151. return rows, nil
  152. }