63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
|
package logic
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fusenapi/model/gmodel"
|
||
|
"fusenapi/utils/auth"
|
||
|
"fusenapi/utils/basic"
|
||
|
|
||
|
"context"
|
||
|
|
||
|
"fusenapi/server/ldap-admin/internal/svc"
|
||
|
"fusenapi/server/ldap-admin/internal/types"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type AddDepartmentLogic struct {
|
||
|
logx.Logger
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
}
|
||
|
|
||
|
func NewAddDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddDepartmentLogic {
|
||
|
return &AddDepartmentLogic{
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 处理进入前逻辑w,r
|
||
|
// func (l *AddDepartmentLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||
|
// }
|
||
|
|
||
|
func (l *AddDepartmentLogic) AddDepartment(req *types.AddDepartmentReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||
|
// userinfo 传入值时, 一定不为null
|
||
|
if req.ParentId > 0 {
|
||
|
_, err := l.svcCtx.AllModels.LdapDepartment.GetOneById(l.ctx, req.ParentId)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
|
basic.CodeServiceErr.Message = "查询不到上级"
|
||
|
} else {
|
||
|
basic.CodeServiceErr.Message = "系统出错"
|
||
|
}
|
||
|
return resp.SetStatus(basic.CodeServiceErr)
|
||
|
}
|
||
|
}
|
||
|
err := l.svcCtx.AllModels.LdapDepartment.InsertOne(l.ctx, gmodel.LdapDepartment{
|
||
|
Name: &req.Name,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return resp.SetStatus(basic.CodeServiceErr)
|
||
|
}
|
||
|
return resp.SetStatus(basic.CodeOK)
|
||
|
}
|
||
|
|
||
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||
|
// func (l *AddDepartmentLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||
|
// }
|