sysPermModel.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. FindAllByProductCode(ctx context.Context, productCode string) ([]*SysPerm, 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. DisableNotInCodes(ctx context.Context, productCode string, codes []string, now int64) (int64, error)
  20. }
  21. customSysPermModel struct {
  22. *defaultSysPermModel
  23. }
  24. )
  25. func NewSysPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysPermModel {
  26. return &customSysPermModel{
  27. defaultSysPermModel: newSysPermModel(conn, c, cachePrefix, opts...),
  28. }
  29. }
  30. func (m *customSysPermModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error) {
  31. var total int64
  32. countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
  33. if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
  34. return nil, 0, err
  35. }
  36. var list []*SysPerm
  37. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY id DESC LIMIT ?,?", sysPermRows, m.table)
  38. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
  39. return nil, 0, err
  40. }
  41. return list, total, nil
  42. }
  43. func (m *customSysPermModel) FindAllByProductCode(ctx context.Context, productCode string) ([]*SysPerm, error) {
  44. var list []*SysPerm
  45. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = %d", sysPermRows, m.table, consts.StatusEnabled)
  46. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
  47. return nil, err
  48. }
  49. return list, nil
  50. }
  51. func (m *customSysPermModel) FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error) {
  52. var codes []string
  53. query := fmt.Sprintf("SELECT `code` FROM %s WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusEnabled)
  54. if err := m.QueryRowsNoCacheCtx(ctx, &codes, query, productCode); err != nil {
  55. return nil, err
  56. }
  57. return codes, nil
  58. }
  59. func (m *customSysPermModel) FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error) {
  60. if len(ids) == 0 {
  61. return nil, nil
  62. }
  63. placeholders := make([]string, len(ids))
  64. args := make([]interface{}, len(ids))
  65. for i, id := range ids {
  66. placeholders[i] = "?"
  67. args[i] = id
  68. }
  69. var list []*SysPerm
  70. query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysPermRows, m.table, strings.Join(placeholders, ","))
  71. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
  72. return nil, err
  73. }
  74. return list, nil
  75. }
  76. func (m *customSysPermModel) FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error) {
  77. var list []*SysPerm
  78. query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
  79. if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
  80. return nil, err
  81. }
  82. result := make(map[string]*SysPerm, len(list))
  83. for _, p := range list {
  84. result[p.Code] = p
  85. }
  86. return result, nil
  87. }
  88. func (m *customSysPermModel) DisableNotInCodes(ctx context.Context, productCode string, codes []string, now int64) (int64, error) {
  89. var query string
  90. var args []interface{}
  91. if len(codes) == 0 {
  92. query = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusDisabled, consts.StatusEnabled)
  93. args = []interface{}{now, productCode}
  94. } else {
  95. placeholders := make([]string, len(codes))
  96. args = make([]interface{}, 0, len(codes)+3)
  97. args = append(args, now, productCode)
  98. for i, code := range codes {
  99. placeholders[i] = "?"
  100. args = append(args, code)
  101. }
  102. 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, ","))
  103. }
  104. result, err := m.ExecNoCacheCtx(ctx, query, args...)
  105. if err != nil {
  106. return 0, err
  107. }
  108. affected, _ := result.RowsAffected()
  109. return affected, nil
  110. }