147 lines
3.4 KiB
Go
147 lines
3.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"admin/internal/dao"
|
|
"admin/internal/model"
|
|
"admin/validator"
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"admin/admin"
|
|
"admin/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"pkg.local/modelbase"
|
|
"pkg.local/utils"
|
|
)
|
|
|
|
type RoleEditLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewRoleEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleEditLogic {
|
|
return &RoleEditLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *RoleEditLogic) RoleEdit(in *admin.RoleEditRequest) (*admin.Response, error) {
|
|
var v validator.RoleEditValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
|
|
check, err := l.check(in.Name, in.ServiceId)
|
|
if err != utils.Ok {
|
|
return l.fail(err)
|
|
}
|
|
|
|
edit := dao.Role{
|
|
Name: in.Name,
|
|
ServiceId: in.ServiceId,
|
|
Rules: l.getIds(in.Rules, in.ServiceId),
|
|
AdminId: adminInfo.ID,
|
|
AdminName: adminInfo.Name,
|
|
}
|
|
|
|
modelObj := model.RoleModel{}.Init()
|
|
id := in.Id
|
|
var editErr error
|
|
if id > utils.NumberZero {
|
|
if check.Id > utils.NumberZero && check.Id != id {
|
|
return l.fail(utils.ErrorExist)
|
|
}
|
|
|
|
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(in.Id, utils.NumberTen)}}
|
|
info := dao.Role{}
|
|
InfoErr := modelObj.GetOne(w, &info)
|
|
if InfoErr != nil {
|
|
l.Logger.Error(InfoErr)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
if info.Id < utils.NumberOne {
|
|
return l.fail(utils.ErrorNotFund)
|
|
}
|
|
|
|
edit.Id = info.Id
|
|
edit.Status = info.Status
|
|
|
|
w = modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(info.Id, utils.NumberTen)}}
|
|
_, editErr = modelObj.Edit(w, edit)
|
|
} else {
|
|
if check.Id > utils.NumberZero {
|
|
return l.fail(utils.ErrorExist)
|
|
}
|
|
editErr = modelObj.Create(&edit)
|
|
id = edit.Id
|
|
}
|
|
|
|
if editErr != nil {
|
|
l.Logger.Error(editErr)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
return l.ok(id)
|
|
}
|
|
|
|
func (l *RoleEditLogic) check(name string, id int64) (dao.Role, utils.Status) {
|
|
w := modelbase.Params{Eq: map[string]string{"name": name, "service_id": strconv.FormatInt(id, utils.NumberTen)}}
|
|
info := dao.Role{}
|
|
err := model.RoleModel{}.Init().GetOne(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return info, utils.Fail
|
|
}
|
|
return info, utils.Ok
|
|
}
|
|
|
|
func (l *RoleEditLogic) getIds(ids []int64, serviceId int64) string {
|
|
var roles []string
|
|
if len(ids) > utils.NumberZero {
|
|
for _, role := range ids {
|
|
roles = append(roles, strconv.Itoa(int(role)))
|
|
}
|
|
}
|
|
|
|
var items []dao.Authority
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{"status": utils.StringStatusOk, "service_id": strconv.FormatInt(serviceId, utils.NumberTen)},
|
|
In: map[string][]string{"id in ?": roles},
|
|
}
|
|
w.Order = "level ASC, parent_id ASC, sort ASC"
|
|
err := model.AuthorityModel{}.Init().Items(w, &items)
|
|
if err != nil {
|
|
return utils.StringEmpty
|
|
}
|
|
|
|
var idsArr []string
|
|
for _, item := range items {
|
|
idsArr = append(idsArr, strconv.Itoa(int(item.Id)))
|
|
tmp := strings.Split(item.ParentIds, utils.DecollatorComma)
|
|
idsArr = append(idsArr, tmp...)
|
|
}
|
|
|
|
var retIdsArr []string
|
|
keys := make(map[string]bool)
|
|
for _, id := range idsArr {
|
|
if id != utils.StringZero {
|
|
if _, value := keys[id]; !value {
|
|
keys[id] = true
|
|
retIdsArr = append(retIdsArr, id)
|
|
}
|
|
}
|
|
}
|
|
|
|
return strings.Join(retIdsArr, utils.DecollatorComma)
|
|
}
|