245 lines
8.2 KiB
Go
245 lines
8.2 KiB
Go
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)
|
||
}
|
||
var nowTime = time.Now().UTC()
|
||
if req.LogoSelectedId == 0 {
|
||
NewFsUserMaterialModel1 := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
|
||
NewFsUserMaterialModelRow1 := NewFsUserMaterialModel1.RowSelectBuilder(nil).Where("id = ?", 0)
|
||
|
||
defaultMaterialInfo, err := NewFsUserMaterialModel1.FindOne(l.ctx, NewFsUserMaterialModelRow1.Model(&gmodel.FsUserMaterial{}))
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "defaultMaterialInfo FindOne err:%+v", err)
|
||
return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find")
|
||
}
|
||
var defaultMaterial = gmodel.FsUserMaterial{
|
||
Module: defaultMaterialInfo.Module,
|
||
UserId: &userinfo.UserId,
|
||
GuestId: &userinfo.GuestId,
|
||
ResourceId: defaultMaterialInfo.ResourceId,
|
||
ResourceUrl: defaultMaterialInfo.ResourceUrl,
|
||
Ctime: &nowTime,
|
||
Metadata: defaultMaterialInfo.Metadata,
|
||
}
|
||
MaterialCreateRes := l.svcCtx.MysqlConn.Create(&defaultMaterial)
|
||
err = MaterialCreateRes.Error
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "defaultMaterialInfo Create err:%+v", err)
|
||
return resp.SetStatus(basic.CodeLogoSetCategory, "logo not find")
|
||
}
|
||
req.LogoSelectedId = defaultMaterial.Id
|
||
}
|
||
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")
|
||
}
|
||
|
||
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
||
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 logoCategoryId int64
|
||
// 更新merchant_category
|
||
if req.SetLogoCategory == 1 {
|
||
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
|
||
}
|
||
}
|
||
logoCategoryId = req.CategoryId
|
||
}
|
||
var module = "profile"
|
||
if req.SetLogoSelected == 1 {
|
||
|
||
var userInfo = &gmodel.FsUserInfo{}
|
||
BuilderDB := tx.Model(&gmodel.FsUserInfo{}).Where("module = ?", module)
|
||
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)
|
||
|
||
userMaterialTemplateTagData, userMaterialTemplateTagEx := metadataMapOldUserMaterial["template_tag"]
|
||
if !userMaterialTemplateTagEx {
|
||
logc.Errorf(l.ctx, "userMaterialInfo Metadata template_tag err:%+v", err)
|
||
return err
|
||
}
|
||
userMaterialTemplateIdTagIdData, userMaterialTemplateTagIdEx := metadataMapOldUserMaterial["template_tag_id"]
|
||
if !userMaterialTemplateTagIdEx {
|
||
logc.Errorf(l.ctx, "userMaterialInfo Metadata template_tag_id err:%+v", err)
|
||
return err
|
||
}
|
||
userMaterialTemplateIdTagIds := userMaterialTemplateIdTagIdData.([]interface{})
|
||
|
||
userMaterialTemplateIdTagId := userMaterialTemplateIdTagIds[0].(string)
|
||
|
||
userMaterialTemplateTag := userMaterialTemplateTagData.(map[string]interface{})
|
||
userMaterialTemplateTagIndex := userMaterialTemplateTag[userMaterialTemplateIdTagId]
|
||
|
||
templateTagSelected := make(map[string]interface{}, 3)
|
||
templateTagSelected["color"] = userMaterialTemplateTagIndex
|
||
templateTagSelected["template_tag"] = userMaterialTemplateIdTagId
|
||
templateTagSelected["selected_index"] = 0
|
||
|
||
var logoSelectedMap = map[string]interface{}{
|
||
"logo_selected_id": req.LogoSelectedId,
|
||
"logo_url": userMaterialInfo.ResourceUrl,
|
||
"template_tag_selected": templateTagSelected,
|
||
}
|
||
if logoCategoryId != 0 {
|
||
logoSelectedMap["merchant_category"] = logoCategoryId
|
||
}
|
||
|
||
metadataChildUserInfo["logo_selected"] = logoSelectedMap
|
||
|
||
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
|
||
|
||
if userInfo.Id == 0 {
|
||
// 新增
|
||
userInfo.Module = &module
|
||
userInfo.Ctime = &nowTime
|
||
userInfo.Utime = &nowTime
|
||
userInfo.UserId = &userId
|
||
userInfo.GuestId = &guestId
|
||
resCreate := tx.Model(&userInfo).Create(&userInfo)
|
||
err = resCreate.Error
|
||
} else {
|
||
// 更新
|
||
userInfo.Utime = &nowTime
|
||
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)
|
||
// }
|