sysPermModel.go 5.6 KB

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