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

198 lines
6.0 KiB
Go
Raw Normal View History

package logic
import (
2023-09-07 08:25:29 +00:00
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-09-07 08:25:29 +00:00
"fusenapi/utils/metadata"
"fusenapi/utils/validate"
"time"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
2023-09-07 08:25:29 +00:00
"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/logx"
2023-09-07 08:25:29 +00:00
"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{})
2023-09-07 08:25:29 +00:00
BuilderDB1 := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
if req.Module == "merchant_category" {
2023-11-24 08:47:09 +00:00
BuilderDB1 = BuilderDB1.Where("module = ?", "profile")
2023-09-07 08:25:29 +00:00
} else {
2023-11-24 08:47:09 +00:00
BuilderDB1 = BuilderDB1.Where("module = ?", req.Module)
2023-09-07 08:25:29 +00:00
}
//.Where("user_id=?", userId).Where("guest_id=?", guestId)
if userId > 0 {
2023-11-24 08:47:09 +00:00
BuilderDB1 = BuilderDB1.Where("user_id=?", userId)
2023-09-07 08:25:29 +00:00
} else {
2023-11-24 08:47:09 +00:00
BuilderDB1 = BuilderDB1.Where("guest_id=?", guestId)
2023-09-07 08:25:29 +00:00
}
userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil)
if err != nil {
2023-09-07 08:25:29 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
}
}
2023-09-07 08:25:29 +00:00
2023-08-23 08:53:43 +00:00
var nowTime = time.Now().UTC()
if userInfo.Id != 0 {
2023-09-07 08:54:49 +00:00
var reqMetadata map[string]interface{}
2023-09-07 08:25:29 +00:00
json.Unmarshal([]byte(req.Metadata), &reqMetadata)
2023-09-07 08:54:49 +00:00
var metadataMapOld map[string]interface{}
json.Unmarshal(*userInfo.Metadata, &metadataMapOld)
2023-09-07 08:25:29 +00:00
var metadataMap map[string]interface{}
2023-09-07 08:54:49 +00:00
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
2023-09-07 08:25:29 +00:00
// 更新merchant_category
if req.Module == "merchant_category" {
2023-09-07 08:54:49 +00:00
logoSelectedId, isEx := metadataMapOld["logo_selected_id"]
if isEx {
var materialId int64 = int64(logoSelectedId.(float64))
2023-09-07 08:25:29 +00:00
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
}
}
2023-09-07 08:54:49 +00:00
}
} else {
var metadataChild = make(map[string]interface{}, 1)
metadataChild["logo_selected_id"] = reqMetadata["logo_selected_id"]
metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld)
2023-09-07 08:25:29 +00:00
if err != nil {
2023-09-07 08:54:49 +00:00
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
}
2023-09-07 08:25:29 +00:00
}
}
2023-09-07 08:54:49 +00:00
return nil
})
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "service fail")
2023-09-07 08:25:29 +00:00
}
2023-09-07 08:54:49 +00:00
2023-09-07 08:25:29 +00:00
return resp.SetStatus(basic.CodeOK)
} else {
userInfo.GuestId = &guestId
userInfo.UserId = &userId
userInfo.Module = &req.Module
2023-08-24 03:47:22 +00:00
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)
}