82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
admin "lone-services/rpc/admin/pb"
|
|
"lone-services/services/admin/internal/dao"
|
|
"lone-services/services/admin/internal/model"
|
|
"lone-services/services/admin/internal/svc"
|
|
"lone-services/services/admin/validator"
|
|
"strconv"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AuthorityNamesLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewAuthorityNamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorityNamesLogic {
|
|
return &AuthorityNamesLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AuthorityNamesLogic) AuthorityNames(in *admin.AuthorityNameRequest) (*admin.Response, error) {
|
|
var v validator.AuthorityNameItemsValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
var data []dao.AuthorityNames
|
|
modelObj := model.AuthorityModel{}.Init()
|
|
w := modelbase.Params{
|
|
Order: "level ASC, parent_id ASC, sort ASC",
|
|
Eq: map[string]string{
|
|
"service_id": strconv.FormatInt(in.ServiceId, utils.NumberTen),
|
|
"status": utils.StringStatusOk,
|
|
},
|
|
}
|
|
if in.Type == dao.QuerySortLIstWeb {
|
|
w.In = map[string][]string{"type in ?": {strconv.Itoa(dao.TypeDir), strconv.Itoa(dao.TypeWeb)}}
|
|
} else if in.Type == dao.QuerySortListNotButton {
|
|
w.In = map[string][]string{"type in ?": {strconv.Itoa(dao.TypeDir), strconv.Itoa(dao.TypeWeb)}}
|
|
}
|
|
|
|
err := modelObj.Items(w, &data)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
items := l.getNameList(data, utils.NumberZero)
|
|
return l.ok(items)
|
|
}
|
|
|
|
// 获取层级的下接数据 列表使用
|
|
func (l *AuthorityNamesLogic) getNameList(data []dao.AuthorityNames, pid int64) []dao.AuthorityNamesItems {
|
|
var dataArr []dao.AuthorityNamesItems
|
|
for _, v := range data {
|
|
if v.ParentId == pid {
|
|
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
|
|
children := l.getNameList(data, v.Id)
|
|
node := dao.AuthorityNamesItems{
|
|
Id: v.Id,
|
|
ParentId: v.ParentId,
|
|
Name: v.Name,
|
|
Level: v.Level,
|
|
Type: v.Type,
|
|
Children: children,
|
|
}
|
|
dataArr = append(dataArr, node)
|
|
}
|
|
}
|
|
return dataArr
|
|
}
|