sysPermModel.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package perm
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "perms-system-server/internal/consts"
  7. "github.com/zeromicro/go-zero/core/stores/cache"
  8. "github.com/zeromicro/go-zero/core/stores/sqlx"
  9. )
  10. var _ SysPermModel = (*customSysPermModel)(nil)
  11. type (
  12. SysPermModel interface {
  13. sysPermModel
  14. FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error)
  15. FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error)
  16. FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error)
  17. FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error)
  18. DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error)
  19. }
  20. customSysPermModel struct {
  21. *defaultSysPermModel
  22. }
  23. )
  24. func NewSysPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysPermModel {
  25. return &customSysPermModel{
  26. defaultSysPermModel: newSysPermModel(conn, c, cachePrefix, opts...),
  27. }
  28. }
  29. func (m *customSysPermModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error) {
  30. var total int64
  31. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  32. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  33. return nil, 0, err
  34. }
  35. var list []*SysPerm
  36. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY id DESC LIMIT ?,?", sysPermRows, m.table)
  37. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  38. return nil, 0, err
  39. }
  40. return list, total, nil
  41. }
  42. func (m *customSysPermModel) FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error) {
  43. var codes []string
  44. query := fmt.Sprintf("SELECT `code` FROM %s WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusEnabled)
  45. if err := m.QueryRowsNoCacheCtx(ctx, &codes, query, productCode); err != nil {
  46. return nil, err
  47. }
  48. return codes, nil
  49. }
  50. func (m *customSysPermModel) FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error) {
  51. if len(ids) == 0 {
  52. return nil, nil
  53. }
  54. placeholders := make([]string, len(ids))
  55. args := make([]interface{}, len(ids))
  56. for i, id := range ids {
  57. placeholders[i] = "?"
  58. args[i] = id
  59. }
  60. var list []*SysPerm
  61. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysPermRows, m.table, strings.Join(placeholders, ","))
  62. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  63. return nil, err
  64. }
  65. return list, nil
  66. }
  67. func (m *customSysPermModel) FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error) {
  68. var list []*SysPerm
  69. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
  70. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
  71. return nil, err
  72. }
  73. result := make(map[string]*SysPerm, len(list))
  74. for _, p := range list {
  75. result[p.Code] = p
  76. }
  77. return result, nil
  78. }
  79. func (m *customSysPermModel) DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error) {
  80. var query string
  81. var args []interface{}
  82. if len(codes) == 0 {
  83. query = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusDisabled, consts.StatusEnabled)
  84. args = []interface{}{now, productCode}
  85. } else {
  86. placeholders := make([]string, len(codes))
  87. args = make([]interface{}, 0, len(codes)+3)
  88. args = append(args, now, productCode)
  89. for i, code := range codes {
  90. placeholders[i] = "?"
  91. args = append(args, code)
  92. }
  93. query = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d AND `code` NOT IN (%s)", m.table, consts.StatusDisabled, consts.StatusEnabled, strings.Join(placeholders, ","))
  94. }
  95. result, err := session.ExecCtx(ctx, query, args...)
  96. if err != nil {
  97. return 0, err
  98. }
  99. affected, _ := result.RowsAffected()
  100. return affected, nil
  101. }