| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package roleperm
- import (
- "context"
- "fmt"
- "strings"
- "github.com/zeromicro/go-zero/core/stores/cache"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- var _ SysRolePermModel = (*customSysRolePermModel)(nil)
- type (
- SysRolePermModel interface {
- sysRolePermModel
- FindPermIdsByRoleId(ctx context.Context, roleId int64) ([]int64, error)
- FindPermIdsByRoleIds(ctx context.Context, roleIds []int64) ([]int64, error)
- DeleteByRoleIdTx(ctx context.Context, session sqlx.Session, roleId int64) error
- }
- customSysRolePermModel struct {
- *defaultSysRolePermModel
- }
- )
- func NewSysRolePermModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysRolePermModel {
- return &customSysRolePermModel{
- defaultSysRolePermModel: newSysRolePermModel(conn, c, cachePrefix, opts...),
- }
- }
- func (m *customSysRolePermModel) FindPermIdsByRoleId(ctx context.Context, roleId int64) ([]int64, error) {
- var ids []int64
- query := fmt.Sprintf("SELECT `permId` FROM %s WHERE `roleId` = ?", m.table)
- if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, roleId); err != nil {
- return nil, err
- }
- return ids, nil
- }
- func (m *customSysRolePermModel) FindPermIdsByRoleIds(ctx context.Context, roleIds []int64) ([]int64, error) {
- if len(roleIds) == 0 {
- return nil, nil
- }
- placeholders := make([]string, len(roleIds))
- args := make([]interface{}, len(roleIds))
- for i, id := range roleIds {
- placeholders[i] = "?"
- args[i] = id
- }
- var ids []int64
- query := fmt.Sprintf("SELECT DISTINCT `permId` FROM %s WHERE `roleId` IN (%s)", m.table, strings.Join(placeholders, ","))
- if err := m.QueryRowsNoCacheCtx(ctx, &ids, query, args...); err != nil {
- return nil, err
- }
- return ids, nil
- }
- func (m *customSysRolePermModel) DeleteByRoleIdTx(ctx context.Context, session sqlx.Session, roleId int64) error {
- query := fmt.Sprintf("DELETE FROM %s WHERE `roleId` = ?", m.table)
- _, err := session.ExecCtx(ctx, query, roleId)
- return err
- }
|