package model import ( "context" "fmt" "strings" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ SysRoleMenuModel = (*customSysRoleMenuModel)(nil) type ( SysRoleMenuModel interface { sysRoleMenuModel FindByRoleId(ctx context.Context, roleId string) ([]*SysRoleMenu, error) DeleteByRoleId(ctx context.Context, roleId string) error BatchInsert(ctx context.Context, roleId string, menuIds []string) error } customSysRoleMenuModel struct { *defaultSysRoleMenuModel } ) func NewSysRoleMenuModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) SysRoleMenuModel { return &customSysRoleMenuModel{ defaultSysRoleMenuModel: newSysRoleMenuModel(conn, c, opts...), } } func (m *customSysRoleMenuModel) FindByRoleId(ctx context.Context, roleId string) ([]*SysRoleMenu, error) { var list []*SysRoleMenu query := fmt.Sprintf("SELECT %s FROM %s WHERE `role_id` = ?", sysRoleMenuRows, m.table) err := m.QueryRowsNoCacheCtx(ctx, &list, query, roleId) if err != nil { return nil, err } return list, nil } func (m *customSysRoleMenuModel) DeleteByRoleId(ctx context.Context, roleId string) error { query := fmt.Sprintf("DELETE FROM %s WHERE `role_id` = ?", m.table) _, err := m.ExecNoCacheCtx(ctx, query, roleId) return err } func (m *customSysRoleMenuModel) BatchInsert(ctx context.Context, roleId string, menuIds []string) error { if len(menuIds) == 0 { return nil } values := make([]string, 0, len(menuIds)) args := make([]interface{}, 0, len(menuIds)*2) for _, menuId := range menuIds { values = append(values, "(?, ?)") args = append(args, roleId, menuId) } query := fmt.Sprintf("INSERT INTO %s (`role_id`, `menu_id`) VALUES %s", m.table, strings.Join(values, ",")) _, err := m.ExecNoCacheCtx(ctx, query, args...) return err }