165 lines
3.8 KiB
Go
165 lines
3.8 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
|
|
"management/internal/db/model/dto"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/global/keys"
|
|
"management/internal/pkg/redis"
|
|
)
|
|
|
|
func CreateSysRole(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error) {
|
|
return db.Engine.CreateSysRole(ctx, arg)
|
|
}
|
|
|
|
func UpdateSysRole(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error) {
|
|
return db.Engine.UpdateSysRole(ctx, arg)
|
|
}
|
|
|
|
func GetSysRole(ctx context.Context, id int32) (*db.SysRole, error) {
|
|
return db.Engine.GetSysRole(ctx, id)
|
|
}
|
|
|
|
func ListSysRoleCondition(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 := db.Engine.CountSysRoleCondition(ctx, countArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
roles, err := db.Engine.ListSysRoleCondition(ctx, dataArg)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return roles, count, nil
|
|
}
|
|
|
|
func XmSelectSysRole(ctx context.Context, id int32) ([]*dto.XmSelectTreeDto, error) {
|
|
all, err := db.Engine.AllSysRole(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toXmSelectTree(id, all), nil
|
|
}
|
|
|
|
func toXmSelectTree(parentId int32, data []*db.SysRole) []*dto.XmSelectTreeDto {
|
|
var res []*dto.XmSelectTreeDto
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := dto.XmSelectTreeDto{
|
|
Name: v.Name,
|
|
Value: strconv.FormatInt(int64(v.ID), 10),
|
|
Children: toXmSelectTree(v.ID, data),
|
|
}
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func SetMenu(ctx context.Context, roleID int32, menus []*db.SysMenu) error {
|
|
return db.Engine.ExecTx(ctx, func(q *db.Queries) error {
|
|
err := db.Engine.DeleteRoleMneuByRoleID(ctx, roleID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, m := range menus {
|
|
err := db.Engine.CreateRoleMenu(ctx, &db.CreateRoleMenuParams{
|
|
RoleID: roleID,
|
|
MenuID: m.ID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func DTreeSysRole(ctx context.Context, id int32) ([]*dto.DTreeDto, error) {
|
|
all, err := db.Engine.AllSysRole(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toDtreeSysRole(id, all), nil
|
|
}
|
|
|
|
func RefreshSysRole(ctx context.Context) error {
|
|
all, err := db.Engine.AllSysRole(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b, err := json.Marshal(all)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
key := keys.GetManageKey(ctx, keys.AllRoles)
|
|
err = redis.Set(ctx, key, b, time.Hour*6)
|
|
return err
|
|
}
|
|
|
|
func RebuildSysRoleParentPath(ctx context.Context) error {
|
|
return db.Engine.SysRoleRebuildPath(ctx)
|
|
}
|
|
|
|
func toDtreeSysRole(parentId int32, data []*db.SysRole) []*dto.DTreeDto {
|
|
var res []*dto.DTreeDto
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
item := dto.DTreeDto{}
|
|
item.ID = strconv.FormatInt(int64(v.ID), 10)
|
|
item.Title = v.DisplayName
|
|
item.Last = !hasSysRoleChildren(v.ID, data)
|
|
item.ParentId = strconv.FormatInt(int64(v.ParentID), 10)
|
|
item.Children = toDtreeSysRole(v.ID, data)
|
|
res = append(res, &item)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func hasSysRoleChildren(parentId int32, data []*db.SysRole) bool {
|
|
if len(data) > 0 {
|
|
for _, v := range data {
|
|
if v.ParentID == parentId {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|