fusenapi/server/home-user-auth/internal/logic/userlogosetlogic.go

189 lines
5.8 KiB
Go
Raw Normal View History

2023-09-07 10:52:01 +00:00
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)
}
2023-09-08 03:47:53 +00:00
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")
}
2023-09-07 10:52:01 +00:00
var userId int64
var guestId int64
2023-09-08 03:47:53 +00:00
NewFsUserMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
NewFsUserMaterialModelRow := NewFsUserMaterialModel.RowSelectBuilder(nil).Where("id = ?", req.LogoSelectedId)
2023-09-07 10:52:01 +00:00
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
guestId = userinfo.GuestId
2023-09-08 03:47:53 +00:00
NewFsUserMaterialModelRow.Where("guest_id = ?", guestId)
2023-09-07 10:52:01 +00:00
} else {
// 否则使用用户ID和用户键名格式
userId = userinfo.UserId
2023-09-08 03:47:53 +00:00
NewFsUserMaterialModelRow.Where("user_id = ?", userId)
2023-09-07 10:52:01 +00:00
}
2023-09-08 03:47:53 +00:00
userMaterialInfo, err := NewFsUserMaterialModel.FindOne(l.ctx, NewFsUserMaterialModelRow.Model(&gmodel.FsUserMaterial{}))
2023-09-07 10:52:01 +00:00
if err != nil {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "FsUserMaterial FindOne err%+v", err)
return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find")
2023-09-07 10:52:01 +00:00
}
var nowTime = time.Now().UTC()
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
// 更新merchant_category
2023-09-08 03:47:53 +00:00
if req.SetLogoCategory == 1 {
var metadataMapOldUserMaterial map[string]interface{}
if userMaterialInfo.Metadata != nil {
err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOldUserMaterial)
2023-09-07 10:52:01 +00:00
if err != nil {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err%+v", err)
2023-09-07 10:52:01 +00:00
return err
}
2023-09-08 03:47:53 +00:00
}
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)
2023-09-07 10:52:01 +00:00
return err
}
2023-09-08 03:47:53 +00:00
}
}
2023-09-08 10:22:44 +00:00
var module = "profile"
2023-09-08 03:47:53 +00:00
if req.SetLogoSelected == 1 {
var userInfo = &gmodel.FsUserInfo{}
2023-09-08 10:22:44 +00:00
BuilderDB := tx.Model(&gmodel.FsUserInfo{}).Where("module = ?", module)
2023-09-08 03:47:53 +00:00
if userId > 0 {
BuilderDB.Where("user_id=?", userId)
} else {
BuilderDB.Where("guest_id=?", guestId)
}
userInfoFirstRes := BuilderDB.First(userInfo)
err = userInfoFirstRes.Error
if err != nil {
2023-09-08 10:16:19 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "userInfo First err%+v", err)
2023-09-07 10:52:01 +00:00
return err
}
2023-09-08 03:47:53 +00:00
}
var metadataMapOldUserInfo map[string]interface{}
if userInfo.Metadata != nil {
err = json.Unmarshal(*userInfo.Metadata, &metadataMapOldUserInfo)
2023-09-07 10:52:01 +00:00
if err != nil {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "userInfo Metadata Unmarshal err%+v", err)
return err
2023-09-07 10:52:01 +00:00
}
}
2023-09-08 03:47:53 +00:00
var metadataChildUserInfo = make(map[string]interface{}, 1)
metadataChildUserInfo["logo_selected_id"] = req.LogoSelectedId
metadataMapUserInfo, err := metadata.SetMetadata(metadataChildUserInfo, metadataMapOldUserInfo)
2023-09-07 10:52:01 +00:00
if err != nil {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "userInfo Metadata SetMetadata err%+v", err)
2023-09-07 10:52:01 +00:00
return err
}
2023-09-08 03:47:53 +00:00
metadataBUserInfo, err := json.Marshal(metadataMapUserInfo)
2023-09-07 10:52:01 +00:00
if err != nil {
2023-09-08 03:47:53 +00:00
logc.Errorf(l.ctx, "userInfo Metadata marshal err%+v", err)
2023-09-07 10:52:01 +00:00
return err
}
2023-09-08 03:47:53 +00:00
userInfo.Metadata = &metadataBUserInfo
2023-09-08 10:26:23 +00:00
2023-09-08 10:19:24 +00:00
if userInfo.Id == 0 {
// 新增
2023-09-08 10:22:44 +00:00
userInfo.Module = &module
2023-09-08 10:26:23 +00:00
userInfo.Ctime = &nowTime
userInfo.Utime = &nowTime
userInfo.UserId = &userId
userInfo.GuestId = &guestId
2023-09-08 10:19:24 +00:00
resCreate := tx.Model(&userInfo).Create(&userInfo)
err = resCreate.Error
} else {
// 更新
2023-09-08 10:26:23 +00:00
userInfo.Utime = &nowTime
2023-09-08 10:19:24 +00:00
resUpdates := tx.Model(&userInfo).Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo)
err = resUpdates.Error
}
2023-09-07 10:52:01 +00:00
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)
2023-09-08 03:47:53 +00:00
return resp.SetStatus(basic.CodeDbSqlErr, "set logo fail")
2023-09-07 10:52:01 +00:00
}
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)
// }