314 lines
7.2 KiB
Go
314 lines
7.2 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"management/internal/db/model/dto"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/erpserver/model/form"
|
|
"management/internal/erpserver/model/view"
|
|
"management/internal/pkg/convertor"
|
|
"management/internal/pkg/know"
|
|
"management/internal/pkg/redis"
|
|
)
|
|
|
|
type RoleBiz interface {
|
|
Create(ctx context.Context, req *form.Role) error
|
|
Update(ctx context.Context, req *form.Role) error
|
|
CreateOrUpdate(ctx context.Context, req *form.Role) 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)
|
|
XmSelectTree(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, req *form.Role) error {
|
|
parent := &db.SysRole{
|
|
ID: 0,
|
|
ParentID: 0,
|
|
ParentPath: ",0,",
|
|
}
|
|
if *req.ParentID > 0 {
|
|
var err error
|
|
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
|
if err != nil {
|
|
return errors.New("父级节点错误")
|
|
}
|
|
}
|
|
|
|
var order int32 = 6666
|
|
if *req.Sort > 0 {
|
|
order = *req.Sort
|
|
}
|
|
|
|
arg := &db.CreateSysRoleParams{
|
|
Name: req.Name,
|
|
DisplayName: req.DisplayName,
|
|
Vip: false,
|
|
ParentID: parent.ID,
|
|
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
|
Status: *req.Status,
|
|
Sort: order,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
_, err := b.store.CreateSysRole(ctx, arg)
|
|
if err != nil {
|
|
if db.IsUniqueViolation(err) {
|
|
return errors.New("角色名称已存在")
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *roleBiz) Update(ctx context.Context, req *form.Role) error {
|
|
parent := &db.SysRole{
|
|
ID: 0,
|
|
ParentID: 0,
|
|
ParentPath: ",0,",
|
|
}
|
|
if *req.ParentID > 0 {
|
|
var err error
|
|
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
|
if err != nil {
|
|
return errors.New("父级节点错误")
|
|
}
|
|
}
|
|
|
|
role, err := b.store.GetSysRole(ctx, *req.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var order int32 = 6666
|
|
if *req.Sort > 0 {
|
|
order = *req.Sort
|
|
}
|
|
|
|
arg := &db.UpdateSysRoleParams{
|
|
ID: role.ID,
|
|
DisplayName: req.DisplayName,
|
|
Status: *req.Status,
|
|
ParentID: parent.ID,
|
|
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
|
Sort: order,
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
_, err = b.store.UpdateSysRole(ctx, arg)
|
|
return err
|
|
}
|
|
|
|
func (b *roleBiz) CreateOrUpdate(ctx context.Context, req *form.Role) error {
|
|
parent := &db.SysRole{
|
|
ID: 0,
|
|
ParentID: 0,
|
|
ParentPath: ",0,",
|
|
}
|
|
if *req.ParentID > 0 {
|
|
var err error
|
|
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
|
if err != nil {
|
|
return errors.New("父级节点错误")
|
|
}
|
|
}
|
|
|
|
var order int32 = 6666
|
|
if *req.Sort > 0 {
|
|
order = *req.Sort
|
|
}
|
|
|
|
if *req.ID > 0 {
|
|
role, err := b.store.GetSysRole(ctx, *req.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
arg := &db.UpdateSysRoleParams{
|
|
ID: role.ID,
|
|
DisplayName: req.DisplayName,
|
|
Status: *req.Status,
|
|
ParentID: parent.ID,
|
|
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
|
Sort: order,
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
_, err = b.store.UpdateSysRole(ctx, arg)
|
|
return err
|
|
} else {
|
|
arg := &db.CreateSysRoleParams{
|
|
Name: req.Name,
|
|
DisplayName: req.DisplayName,
|
|
Vip: false,
|
|
ParentID: parent.ID,
|
|
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
|
Status: *req.Status,
|
|
Sort: order,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
_, err := b.store.CreateSysRole(ctx, arg)
|
|
if err != nil {
|
|
if db.IsUniqueViolation(err) {
|
|
return errors.New("角色名称已存在")
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (b *roleBiz) All(ctx context.Context) ([]*db.SysRole, error) {
|
|
key := know.GetManageKey(ctx, know.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 := know.GetManageKey(ctx, know.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) XmSelectTree(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
|
|
}
|