package logic import ( "errors" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" "strings" "time" "context" "fusenapi/server/ldap-admin/internal/svc" "fusenapi/server/ldap-admin/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type SaveMenuLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewSaveMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMenuLogic { return &SaveMenuLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *SaveMenuLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *SaveMenuLogic) SaveMenu(req *types.SaveMenuReq, userinfo *auth.UserInfo) (resp *basic.Response) { req.Path = strings.Trim(req.Path, " ") req.Name = strings.Trim(req.Name, " ") req.Title = strings.Trim(req.Title, " ") if req.Path == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数path不能为空") } if req.Name == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数name不能为空") } if req.Title == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数title不能为空") } now := time.Now().UTC() data := &gmodel.LdapMenus{ Name: &req.Name, Title: &req.Title, Icon: &req.Icon, Path: &req.Path, Sort: &req.Sort, Status: &req.Status, ParentId: &req.ParentId, Utime: &now, } //查询重复 info, err := l.svcCtx.AllModels.LdapMenus.FindByPath(l.ctx, req.Path) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取检查重复路径失败") } //没有 } else { //有 //是新增/或者是更新但是id不等 if req.Id == 0 || (info.Id != req.Id) { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "该路径已经存在") } } if req.Id > 0 { //更新 //更新 if err = l.svcCtx.AllModels.LdapMenus.Update(l.ctx, req.Id, data); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "更新失败") } return resp.SetStatusWithMessage(basic.CodeOK, "更新成功") } //添加 data.Ctime = &now if err = l.svcCtx.AllModels.LdapMenus.Create(l.ctx, data); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "添加失败") } return resp.SetStatusWithMessage(basic.CodeOK, "添加成功") } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *SaveMenuLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }