feat: user info
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
package analysisstorelogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
analysis "lone-services/rpc/analysis/pb"
|
||||
"lone-services/services/analysis/internal/dao"
|
||||
"lone-services/services/analysis/internal/logic/common"
|
||||
"lone-services/services/analysis/internal/model"
|
||||
"lone-services/services/analysis/internal/svc"
|
||||
"lone-services/services/analysis/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type StoreUserUpdateLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewStoreUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreUserUpdateLogic {
|
||||
return &StoreUserUpdateLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *StoreUserUpdateLogic) StoreUserUpdate(in *analysis.StoreUserUpdateReq) (*analysis.Response, error) {
|
||||
var req validator.StoreUserUpdateValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return common.OutResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
if req.Birthday != nil && *req.Birthday != utils.StringEmpty {
|
||||
if _, err := time.ParseInLocation("2006-01-02", dateOnly(*req.Birthday), time.Local); err != nil {
|
||||
return common.OutResponse(utils.ErrorParams, "生日格式不正确"), nil
|
||||
}
|
||||
}
|
||||
|
||||
storeId, _, fail := common.RequireStoreId(l.ctx)
|
||||
if fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
|
||||
if err := replaceUserTags(storeId, req.UserId, in.Tag); err != nil {
|
||||
l.Errorf("store user update tags: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
data := map[string]interface{}{}
|
||||
if req.Name != nil {
|
||||
data["name"] = *req.Name
|
||||
}
|
||||
if req.Gender != nil {
|
||||
if *req.Gender == utils.NumberZero {
|
||||
data["gender"] = nil
|
||||
} else {
|
||||
data["gender"] = *req.Gender
|
||||
}
|
||||
}
|
||||
if req.Birthday != nil {
|
||||
if *req.Birthday == utils.StringEmpty {
|
||||
data["birthday"] = nil
|
||||
} else {
|
||||
data["birthday"] = dateOnly(*req.Birthday)
|
||||
}
|
||||
}
|
||||
|
||||
where := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"user_id": strconv.FormatInt(req.UserId, utils.NumberTen),
|
||||
"store_id": strconv.FormatInt(storeId, utils.NumberTen),
|
||||
},
|
||||
}
|
||||
remarkModel := model.UserRemarkModel{}.Init()
|
||||
count, err := remarkModel.CountNumber(where)
|
||||
if err != nil {
|
||||
l.Errorf("store user update count: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
if count > utils.NumberZero {
|
||||
if len(data) == utils.NumberZero {
|
||||
return common.OkResponse(nil), nil
|
||||
}
|
||||
if _, err = remarkModel.Edit(where, data); err != nil {
|
||||
l.Errorf("store user update edit: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
return common.OkResponse(nil), nil
|
||||
}
|
||||
|
||||
row := dao.UserRemark{
|
||||
StoreId: storeId,
|
||||
UserId: req.UserId,
|
||||
CreatedAt: utils.Now(),
|
||||
}
|
||||
if req.Name != nil {
|
||||
row.Name = *req.Name
|
||||
}
|
||||
if req.Gender != nil && *req.Gender != utils.NumberZero {
|
||||
row.Gender = int8(*req.Gender)
|
||||
}
|
||||
if req.Birthday != nil && *req.Birthday != utils.StringEmpty {
|
||||
row.Birthday = dateOnly(*req.Birthday)
|
||||
}
|
||||
if err = remarkModel.Create(&row); err != nil {
|
||||
l.Errorf("store user update create: %v", err)
|
||||
return common.FailResponse(utils.Fail), nil
|
||||
}
|
||||
return common.OkResponse(nil), nil
|
||||
}
|
||||
|
||||
func replaceUserTags(storeId, userId int64, tags []string) error {
|
||||
where := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"user_id": strconv.FormatInt(userId, utils.NumberTen),
|
||||
"store_id": strconv.FormatInt(storeId, utils.NumberTen),
|
||||
},
|
||||
}
|
||||
tagModel := model.UserImageTagModel{}.Init()
|
||||
if _, err := tagModel.Edit(where, map[string]interface{}{
|
||||
"status": dao.StatusDisabled,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(tags))
|
||||
for _, tag := range tags {
|
||||
tag = strings.TrimSpace(tag)
|
||||
if tag == utils.StringEmpty {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[tag]; ok {
|
||||
continue
|
||||
}
|
||||
seen[tag] = struct{}{}
|
||||
if err := tagModel.Create(&dao.UserImageTag{
|
||||
StoreId: storeId,
|
||||
UserId: userId,
|
||||
Tag: tag,
|
||||
Status: dao.StatusEnabled,
|
||||
CreatedAt: utils.Now(),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user