feat: product store
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"product/internal/dao"
|
||||
"product/internal/model"
|
||||
"product/internal/svc"
|
||||
"product/product"
|
||||
"product/validator"
|
||||
|
||||
"pkg.local/log"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/utils"
|
||||
validateService "pkg.local/validate"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CreateLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLogic {
|
||||
return &CreateLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateLogic) Create(in *product.CreateReq) (*product.Response, error) {
|
||||
var req validator.ProductCreateValidator
|
||||
if msg := validateService.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return msgResponse(utils.Ok.Code, msg), nil
|
||||
}
|
||||
|
||||
if req.Type == dao.ProductTypeNormal {
|
||||
if req.AgentPrice == 0 || req.StorePrice == 0 ||
|
||||
req.SalePrice == 0 || req.SharePrice == 0 ||
|
||||
req.SaleReward == "" {
|
||||
return msgResponse(int32(utils.ErrorMissingParams.GetCode()), "缺少相关价格"), nil
|
||||
}
|
||||
}
|
||||
|
||||
publishTime := time.Now()
|
||||
if len(req.PublishTime) > 1 {
|
||||
t, err := parsePublishTime(req.PublishTime)
|
||||
if err != nil {
|
||||
return msgResponse(int32(utils.ErrorMissingParams.GetCode()), "时间错误"), nil
|
||||
}
|
||||
publishTime = t
|
||||
}
|
||||
|
||||
var check dao.ProductCheckExist
|
||||
err := model.ProductModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{"name": req.Name},
|
||||
}, &check)
|
||||
if err != nil {
|
||||
log.Errorf("check product name: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if check.Id > 0 {
|
||||
return failResponse(utils.ErrorDataIsExist), nil
|
||||
}
|
||||
|
||||
// 管理员信息后续从 ctx / metadata 取
|
||||
adminId := 0
|
||||
adminName := ""
|
||||
|
||||
data := dao.ProductCreate{
|
||||
Name: req.Name,
|
||||
Subhead: req.Subhead,
|
||||
Content: req.Content,
|
||||
ModelCode: req.ModelCode,
|
||||
Price: float32(req.Price),
|
||||
StorePrice: float32(req.StorePrice),
|
||||
SalePrice: float32(req.SalePrice),
|
||||
SharePrice: float32(req.SharePrice),
|
||||
AgentPrice: float32(req.AgentPrice),
|
||||
SaleReward: req.SaleReward,
|
||||
Waybill: req.Waybill,
|
||||
SalesModel: uint8(req.SalesModel),
|
||||
Weight: float32(req.Weight),
|
||||
Images: req.Images,
|
||||
PeriodValidity: int16(req.PeriodValidity),
|
||||
Type: uint8(req.Type),
|
||||
NormsNumber: uint8(req.NormsNumber),
|
||||
Number: uint(req.Number),
|
||||
Sort: 1,
|
||||
Label: req.Label,
|
||||
Cubage: req.Cubage,
|
||||
BoxNumber: req.BoxNumber,
|
||||
IsIndex: uint8(req.IsIndex),
|
||||
IndexImage: req.IndexImage,
|
||||
IsBuy: uint8(req.IsBuy),
|
||||
PublishTime: publishTime,
|
||||
TryNumber: uint8(req.TryNumber),
|
||||
AdminName: adminName,
|
||||
AdminId: adminId,
|
||||
}
|
||||
if data.IsIndex == 0 {
|
||||
data.IsIndex = 2
|
||||
}
|
||||
if data.IsBuy == 0 {
|
||||
data.IsBuy = 2
|
||||
}
|
||||
if data.TryNumber == 0 {
|
||||
data.TryNumber = 1
|
||||
}
|
||||
|
||||
err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := (model.ProductModel{}.Init().WithTX(tx).Create(&data)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content := dao.ProductVerify{
|
||||
Id: data.Id,
|
||||
ModelCode: data.ModelCode,
|
||||
Price: data.Price,
|
||||
StorePrice: data.StorePrice,
|
||||
SalePrice: data.SalePrice,
|
||||
SharePrice: data.SharePrice,
|
||||
AgentPrice: data.AgentPrice,
|
||||
SaleReward: data.SaleReward,
|
||||
Type: data.Type,
|
||||
BoxNumber: data.BoxNumber,
|
||||
NormsNumber: data.NormsNumber,
|
||||
AdminName: data.AdminName,
|
||||
AdminId: data.AdminId,
|
||||
}
|
||||
contentBytes, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
verify := dao.VerifyCreate{
|
||||
VerifyId: strconv.Itoa(data.Id),
|
||||
Content: string(contentBytes),
|
||||
Type: dao.VerifyTypeProduct,
|
||||
}
|
||||
return model.VerifyModel{}.Init().WithTX(tx).Create(&verify)
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("create product: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(utils.StructToJson(data.Id)), nil
|
||||
}
|
||||
|
||||
func parsePublishTime(s string) (time.Time, error) {
|
||||
for _, layout := range []string{time.DateTime, time.DateOnly} {
|
||||
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, errors.New("invalid time format")
|
||||
}
|
||||
Reference in New Issue
Block a user