| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package perm
- import (
- "context"
- "fmt"
- "strings"
- "perms-system-server/internal/consts"
- "github.com/zeromicro/go-zero/core/stores/cache"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- var _ SysPermModel = (*customSysPermModel)(nil)
- type (
- SysPermModel interface {
- sysPermModel
- FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error)
- FindAllByProductCode(ctx context.Context, productCode string) ([]*SysPerm, error)
- FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error)
- FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error)
- FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error)
- DisableNotInCodes(ctx context.Context, productCode string, codes []string, now int64) (int64, error)
- DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error)
- }
- customSysPermModel struct {
- *defaultSysPermModel
- }
- )
- func NewSysPermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysPermModel {
- return &customSysPermModel{
- defaultSysPermModel: newSysPermModel(conn, c, cachePrefix, opts...),
- }
- }
- func (m *customSysPermModel) FindListByProductCode(ctx context.Context, productCode string, page, pageSize int64) ([]*SysPerm, int64, error) {
- var total int64
- countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE `productCode` = ?", m.table)
- if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, productCode); err != nil {
- return nil, 0, err
- }
- var list []*SysPerm
- query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? ORDER BY id DESC LIMIT ?,?", sysPermRows, m.table)
- if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode, (page-1)*pageSize, pageSize); err != nil {
- return nil, 0, err
- }
- return list, total, nil
- }
- func (m *customSysPermModel) FindAllByProductCode(ctx context.Context, productCode string) ([]*SysPerm, error) {
- var list []*SysPerm
- query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ? AND `status` = %d", sysPermRows, m.table, consts.StatusEnabled)
- if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
- return nil, err
- }
- return list, nil
- }
- func (m *customSysPermModel) FindAllCodesByProductCode(ctx context.Context, productCode string) ([]string, error) {
- var codes []string
- query := fmt.Sprintf("SELECT `code` FROM %s WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusEnabled)
- if err := m.QueryRowsNoCacheCtx(ctx, &codes, query, productCode); err != nil {
- return nil, err
- }
- return codes, nil
- }
- func (m *customSysPermModel) FindByIds(ctx context.Context, ids []int64) ([]*SysPerm, error) {
- if len(ids) == 0 {
- return nil, nil
- }
- placeholders := make([]string, len(ids))
- args := make([]interface{}, len(ids))
- for i, id := range ids {
- placeholders[i] = "?"
- args[i] = id
- }
- var list []*SysPerm
- query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` IN (%s)", sysPermRows, m.table, strings.Join(placeholders, ","))
- if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
- return nil, err
- }
- return list, nil
- }
- func (m *customSysPermModel) FindMapByProductCode(ctx context.Context, productCode string) (map[string]*SysPerm, error) {
- var list []*SysPerm
- query := fmt.Sprintf("SELECT %s FROM %s WHERE `productCode` = ?", sysPermRows, m.table)
- if err := m.QueryRowsNoCacheCtx(ctx, &list, query, productCode); err != nil {
- return nil, err
- }
- result := make(map[string]*SysPerm, len(list))
- for _, p := range list {
- result[p.Code] = p
- }
- return result, nil
- }
- func (m *customSysPermModel) DisableNotInCodes(ctx context.Context, productCode string, codes []string, now int64) (int64, error) {
- var query string
- var args []interface{}
- if len(codes) == 0 {
- query = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusDisabled, consts.StatusEnabled)
- args = []interface{}{now, productCode}
- } else {
- placeholders := make([]string, len(codes))
- args = make([]interface{}, 0, len(codes)+3)
- args = append(args, now, productCode)
- for i, code := range codes {
- placeholders[i] = "?"
- args = append(args, code)
- }
- 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, ","))
- }
- result, err := m.ExecNoCacheCtx(ctx, query, args...)
- if err != nil {
- return 0, err
- }
- affected, _ := result.RowsAffected()
- return affected, nil
- }
- func (m *customSysPermModel) DisableNotInCodesWithTx(ctx context.Context, session sqlx.Session, productCode string, codes []string, now int64) (int64, error) {
- var query string
- var args []interface{}
- if len(codes) == 0 {
- query = fmt.Sprintf("UPDATE %s SET `status` = %d, `updateTime` = ? WHERE `productCode` = ? AND `status` = %d", m.table, consts.StatusDisabled, consts.StatusEnabled)
- args = []interface{}{now, productCode}
- } else {
- placeholders := make([]string, len(codes))
- args = make([]interface{}, 0, len(codes)+3)
- args = append(args, now, productCode)
- for i, code := range codes {
- placeholders[i] = "?"
- args = append(args, code)
- }
- 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, ","))
- }
- result, err := session.ExecCtx(ctx, query, args...)
- if err != nil {
- return 0, err
- }
- affected, _ := result.RowsAffected()
- return affected, nil
- }
|