package logic import ( "encoding/json" "errors" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/metadata" "time" "context" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" "github.com/zeromicro/go-zero/core/logc" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type UserLogoSetLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUserLogoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoSetLogic { return &UserLogoSetLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *UserLogoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null // 更新用户信息 if userinfo.IsOnlooker() { // 如果是,返回未授权的错误码 return resp.SetStatus(basic.CodeUnAuth) } if req.LogoSelectedId == 0 { return resp.SetStatus(basic.CodeLogoSetCategory, "logo logo_selected_id not null") } if req.SetLogoCategory == 1 && req.CategoryId == 0 { return resp.SetStatus(basic.CodeLogoSetCategory, "logo category_id not null") } var userId int64 var guestId int64 NewFsUserMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) NewFsUserMaterialModelRow := NewFsUserMaterialModel.RowSelectBuilder(nil).Where("id = ?", req.LogoSelectedId) if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 guestId = userinfo.GuestId NewFsUserMaterialModelRow.Where("guest_id = ?", guestId) } else { // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId NewFsUserMaterialModelRow.Where("user_id = ?", userId) } userMaterialInfo, err := NewFsUserMaterialModel.FindOne(l.ctx, NewFsUserMaterialModelRow.Model(&gmodel.FsUserMaterial{})) if err != nil { logc.Errorf(l.ctx, "FsUserMaterial FindOne err:%+v", err) return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find") } var nowTime = time.Now().UTC() err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { // 更新merchant_category if req.SetLogoCategory == 1 { var metadataMapOldUserMaterial map[string]interface{} if userMaterialInfo.Metadata != nil { err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOldUserMaterial) if err != nil { logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err:%+v", err) return err } } var metadataChildUserMaterial = make(map[string]interface{}, 1) metadataChildUserMaterial["merchant_category"] = req.CategoryId metadataMapUserMaterial, err := metadata.SetMetadata(metadataChildUserMaterial, metadataMapOldUserMaterial) if err != nil { logc.Errorf(l.ctx, "userMaterialInfo Metadata SetMetadata err:%+v", err) return err } metadataBUserMaterial, err := json.Marshal(metadataMapUserMaterial) if err != nil { logc.Errorf(l.ctx, "userMaterialInfo Metadata err:%+v", err) return err } userMaterialInfo.Metadata = &metadataBUserMaterial resUpdates := tx.Select("metadata").Where("id = ?", req.LogoSelectedId).Updates(&userMaterialInfo) err = resUpdates.Error if err != nil { if err != gorm.ErrRecordNotFound { logc.Errorf(l.ctx, "userMaterialInfo Updates err:%+v", err) return err } } } if req.SetLogoSelected == 1 { var userInfo = &gmodel.FsUserInfo{} BuilderDB := tx.Model(&gmodel.FsUserInfo{}).Where("module = ?", "profile") if userId > 0 { BuilderDB.Where("user_id=?", userId) } else { BuilderDB.Where("guest_id=?", guestId) } userInfoFirstRes := BuilderDB.First(userInfo) err = userInfoFirstRes.Error if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { logc.Errorf(l.ctx, "userInfo First err:%+v", err) return err } } var metadataMapOldUserInfo map[string]interface{} if userInfo.Metadata != nil { err = json.Unmarshal(*userInfo.Metadata, &metadataMapOldUserInfo) if err != nil { logc.Errorf(l.ctx, "userInfo Metadata Unmarshal err:%+v", err) return err } } var metadataChildUserInfo = make(map[string]interface{}, 1) metadataChildUserInfo["logo_selected_id"] = req.LogoSelectedId metadataMapUserInfo, err := metadata.SetMetadata(metadataChildUserInfo, metadataMapOldUserInfo) if err != nil { logc.Errorf(l.ctx, "userInfo Metadata SetMetadata err:%+v", err) return err } metadataBUserInfo, err := json.Marshal(metadataMapUserInfo) if err != nil { logc.Errorf(l.ctx, "userInfo Metadata marshal err:%+v", err) return err } userInfo.Metadata = &metadataBUserInfo userInfo.Utime = &nowTime if userInfo.Id == 0 { // 新增 resCreate := tx.Model(&userInfo).Create(&userInfo) err = resCreate.Error } else { // 更新 resUpdates := tx.Model(&userInfo).Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo) err = resUpdates.Error } if err != nil { if err != gorm.ErrRecordNotFound { logc.Errorf(l.ctx, "FsUserInfo Updates err:%+v", err) return err } } } return nil }) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr, "set logo fail") } return resp.SetStatus(basic.CodeOK) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *UserLogoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }