181 lines
4.4 KiB
Go
181 lines
4.4 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
|
|
"management/internal/db/model/dto"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/erpserver/model/view"
|
|
"management/internal/global/keys"
|
|
"management/internal/pkg/redis"
|
|
)
|
|
|
|
type RoleBiz interface {
|
|
Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error)
|
|
Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error)
|
|
All(ctx context.Context) ([]*db.SysRole, error)
|
|
List(ctx context.Context, q dto.SearchDto) ([]*db.SysRole, int64, error)
|
|
Get(ctx context.Context, id int32) (*db.SysRole, error)
|
|
Refresh(ctx context.Context) ([]*db.SysRole, error)
|
|
RebuildParentPath(ctx context.Context) error
|
|
|
|
Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error)
|
|
XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error)
|
|
|
|
RoleExpansion
|
|
}
|
|
|
|
type RoleExpansion interface{}
|
|
|
|
type roleBiz struct {
|
|
store db.Store
|
|
redis redis.IRedis
|
|
}
|
|
|
|
var _ RoleBiz = (*roleBiz)(nil)
|
|
|
|
func NewRole(store db.Store, redis redis.IRedis) *roleBiz {
|
|
return &roleBiz{
|
|
store: store,
|
|
redis: redis,
|
|
}
|
|
}
|
|
|
|
func (b *roleBiz) Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error) {
|
|
return b.store.CreateSysRole(ctx, arg)
|
|
}
|
|
|
|
func (b *roleBiz) Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error) {
|
|
return b.store.UpdateSysRole(ctx, arg)
|
|
}
|
|
|
|
func (b *roleBiz) All(ctx context.Context) ([]*db.SysRole, error) {
|
|
key := keys.GetManageKey(ctx, keys.AllRoles)
|
|
bs, err := redis.GetBytes(ctx, key)
|
|
if err == nil {
|
|
var res []*db.SysRole
|
|
if err := json.Unmarshal(bs, &res); err == nil {
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
return b.Refresh(ctx)
|
|
}
|
|
|
|
func (b *roleBiz) List(ctx context.Context, q dto.SearchDto) ([]*db.SysRole, int64, error) {
|
|
countArg := &db.CountSysRoleConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int32(q.SearchStatus),
|
|
IsID: q.SearchID != 0,
|
|
ID: int32(q.SearchID),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
DisplayName: q.SearchName,
|
|
}
|
|
|
|
dataArg := &db.ListSysRoleConditionParams{
|
|
IsStatus: q.SearchStatus != 9999,
|
|
Status: int32(q.SearchStatus),
|
|
IsID: q.SearchID != 0,
|
|
ID: int32(q.SearchID),
|
|
IsParentID: q.SearchParentID != 0,
|
|
ParentID: int32(q.SearchParentID),
|
|
DisplayName: q.SearchName,
|
|
Skip: (int32(q.Page) - 1) * int32(q.Rows),
|
|
Size: int32(q.Rows),
|
|
}
|
|
count, err := b.store.CountSysRoleCondition(ctx, countArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
departs, err := b.store.ListSysRoleCondition(ctx, dataArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return departs, count, nil
|
|
}
|
|
|
|
func (b *roleBiz) Get(ctx context.Context, id int32) (*db.SysRole, error) {
|
|
return b.store.GetSysRole(ctx, id)
|
|
}
|
|
|
|
func (b *roleBiz) Refresh(ctx context.Context) ([]*db.SysRole, error) {
|
|
all, err := b.store.AllSysRole(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bs, err := json.Marshal(all)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
key := keys.GetManageKey(ctx, keys.AllRoles)
|
|
err = redis.Set(ctx, key, bs, time.Hour*6)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
func (b *roleBiz) RebuildParentPath(ctx context.Context) error {
|
|
return b.store.SysRoleRebuildPath(ctx)
|
|
}
|
|
|
|
func (b *roleBiz) Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error) {
|
|
all, err := b.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.toTree(id, all), nil
|
|
}
|
|
|
|
func (b *roleBiz) XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error) {
|
|
all, err := b.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b.toXmSelectTree(id, all), nil
|
|
}
|
|
|
|
func (b *roleBiz) toTree(parentId int32, data []*db.SysRole) []*view.LayuiTree {
|
|
var res []*view.LayuiTree
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := view.LayuiTree{}
|
|
item.ID = strconv.FormatInt(int64(v.ID), 10)
|
|
item.Title = v.DisplayName
|
|
item.Children = b.toTree(v.ID, data)
|
|
if v.ParentID == 0 {
|
|
item.Spread = true
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (b *roleBiz) toXmSelectTree(parentId int32, data []*db.SysRole) []*view.XmSelectTree {
|
|
var res []*view.XmSelectTree
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := view.XmSelectTree{
|
|
Name: v.DisplayName,
|
|
Value: strconv.FormatInt(int64(v.ID), 10),
|
|
Children: b.toXmSelectTree(v.ID, data),
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|