fusenapi/server/home-user-auth/internal/logic/userinfosetlogic.go
2023-09-07 16:54:49 +08:00

198 lines
5.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/metadata"
"fusenapi/utils/validate"
"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 UserInfoSetLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserInfoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoSetLogic {
return &UserInfoSetLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserInfoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserInfoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo.IsOnlooker() {
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
var userId int64
var guestId int64
// 检查用户是否是游客
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
guestId = userinfo.GuestId
} else {
// 否则使用用户ID和用户键名格式
userId = userinfo.UserId
}
_, err := validate.Validate(&req.Module, &req.Metadata)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeRequestParamsErr, "UserInfoSet error Validate failed")
}
// 根据模块类型检查数据
var userInfo = &gmodel.FsUserInfo{}
fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn)
BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
BuilderDB1 := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
if req.Module == "merchant_category" {
BuilderDB1.Where("module = ?", "profile")
} else {
BuilderDB1.Where("module = ?", req.Module)
}
//.Where("user_id=?", userId).Where("guest_id=?", guestId)
if userId > 0 {
BuilderDB1.Where("user_id=?", userId)
} else {
BuilderDB1.Where("guest_id=?", guestId)
}
userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
}
}
var nowTime = time.Now().UTC()
if userInfo.Id != 0 {
var reqMetadata map[string]interface{}
json.Unmarshal([]byte(req.Metadata), &reqMetadata)
var metadataMapOld map[string]interface{}
json.Unmarshal(*userInfo.Metadata, &metadataMapOld)
var metadataMap map[string]interface{}
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
// 更新merchant_category
if req.Module == "merchant_category" {
logoSelectedId, isEx := metadataMapOld["logo_selected_id"]
if isEx {
var materialId int64 = int64(logoSelectedId.(float64))
var userMaterialInfo gmodel.FsUserMaterial
userMaterialGorm := tx.Where("id = ?", materialId)
resUserMaterialInfo := userMaterialGorm.First(&userMaterialInfo)
err = resUserMaterialInfo.Error
if err != nil {
logc.Errorf(l.ctx, "FsUserMaterial First err%+v", err)
return err
}
var metadataMapOld map[string]interface{}
if userMaterialInfo.Id > 0 {
err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOld)
if err != nil {
logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err%+v", err)
return err
}
}
var metadataChild = make(map[string]interface{}, 1)
metadataChild["merchant_category"] = reqMetadata["category_id"]
metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld)
if err != nil {
logc.Errorf(l.ctx, "metadata SetMetadata err%+v", err)
return err
}
metadataB, err := json.Marshal(metadataMap)
if err != nil {
logc.Errorf(l.ctx, "metadata marshal err%+v", err)
return err
}
userMaterialInfo.Metadata = &metadataB
resUpdates := tx.Select("metadata").Where("id = ?", materialId).Updates(&userMaterialInfo)
err = resUpdates.Error
if err != nil {
if err != gorm.ErrRecordNotFound {
logc.Errorf(l.ctx, "userMaterialInfo Updates err%+v", err)
return err
}
}
}
} else {
var metadataChild = make(map[string]interface{}, 1)
metadataChild["logo_selected_id"] = reqMetadata["logo_selected_id"]
metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld)
if err != nil {
logc.Errorf(l.ctx, "metadata SetMetadata err%+v", err)
return err
}
metadataB, err := json.Marshal(metadataMap)
if err != nil {
logc.Errorf(l.ctx, "metadata marshal err%+v", err)
return err
}
userInfo.Metadata = &metadataB
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, "service fail")
}
return resp.SetStatus(basic.CodeOK)
} else {
userInfo.GuestId = &guestId
userInfo.UserId = &userId
userInfo.Module = &req.Module
v := []byte(req.Metadata)
userInfo.Metadata = &v
userInfo.Ctime = &nowTime
}
_, err = fsUserInfoModel.CreateOrUpdate(BuilderDB, userInfo)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
}
return resp.SetStatus(basic.CodeOK)
}