167 lines
4.4 KiB
Go
167 lines
4.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
product "lone-services/rpc/product/pb"
|
|
"lone-services/services/order/internal/dao"
|
|
"lone-services/services/order/internal/model"
|
|
"lone-services/services/order/validator"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"lone-services/rpc/order/pb"
|
|
"lone-services/services/order/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type OrderCreateLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewOrderCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderCreateLogic {
|
|
return &OrderCreateLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *OrderCreateLogic) OrderCreate(in *order.AdminCreateOrderRequest) (*order.Response, error) {
|
|
var v validator.AdminOrderCreateValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"id": strconv.FormatInt(in.AddressId, utils.NumberTen),
|
|
"type": strconv.Itoa(dao.OrderTypeStore),
|
|
"target_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
},
|
|
}
|
|
var address dao.Address
|
|
err := model.AddressModel{}.Init().GetOne(w, &address)
|
|
if err != nil {
|
|
return l.fail(utils.Fail)
|
|
}
|
|
if address.Id < utils.NumberOne {
|
|
return l.out(utils.Fail, "地址不存在")
|
|
}
|
|
|
|
data := dao.OrderCreate{
|
|
Note: in.Note,
|
|
Mobile: address.Mobile,
|
|
VerifyStatus: model.VerifyCheckIn,
|
|
Address: address.Address,
|
|
Addressee: address.Name,
|
|
DistrictIds: address.DistrictIds,
|
|
}
|
|
|
|
ww := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"type": strconv.Itoa(dao.OrderTypeStore),
|
|
"store_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
},
|
|
}
|
|
data.StoreId = int(adminInfo.ID)
|
|
data.StoreName = adminInfo.Name
|
|
data.SaleId = int(adminInfo.SaleId)
|
|
data.GroupId = int(adminInfo.GroupId)
|
|
data.SaleMobile = adminInfo.SaleMobile
|
|
data.SaleProvince = int(adminInfo.SaleProvince)
|
|
data.CreateType = dao.OrderTypeStore
|
|
data.Type = dao.OrderTypeStore
|
|
data.OrderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeStore), adminInfo.ID)
|
|
|
|
var cart dao.Cart
|
|
|
|
ids, pErr := svc.GetCartInfo(in.Info)
|
|
if pErr != utils.Ok {
|
|
return l.fail(utils.Fail)
|
|
}
|
|
products, pErr := svc.GetCartData(l.svcCtx.ProductSvcName, ids)
|
|
if pErr != utils.Ok {
|
|
return l.fail(utils.Fail)
|
|
}
|
|
keys := make([]string, utils.NumberZero, len(products.Items))
|
|
errData := make(map[int64]int64)
|
|
productItems := []dao.OrderProductCreate{}
|
|
|
|
NumItems := make([]*product.NumberItem, utils.NumberZero)
|
|
for _, item := range products.Items {
|
|
if buyNum, ok := ids[item.Id]; ok {
|
|
if buyNum > int64(item.Number) {
|
|
errData[item.Id] = int64(item.Number)
|
|
return l.out(utils.Fail,
|
|
item.Name+"购买数量超出库存,目前可以够买:"+strconv.Itoa(int(item.Number)))
|
|
} else {
|
|
numItem := &product.NumberItem{Id: item.Id, Number: uint32(buyNum)}
|
|
NumItems = append(NumItems, numItem)
|
|
keys = append(keys, strconv.Itoa(int(item.Id)))
|
|
productItems = append(productItems, dao.OrderProductCreate{
|
|
OrderSn: data.OrderSn,
|
|
ProductId: int(item.Id),
|
|
ProductName: item.Name,
|
|
Price: utils.NumberZero,
|
|
Number: int(buyNum),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
if len(keys) < utils.NumberOne {
|
|
return l.out(utils.Fail, "购买信息不存在")
|
|
}
|
|
data.ProductIds = strings.Join(keys, utils.DecollatorComma)
|
|
data.TotalPrice = utils.NumberZero
|
|
mobileObj := model.OrderModel{}.Init()
|
|
|
|
mobileObj.Begin()
|
|
err = mobileObj.Create(&data)
|
|
if err != nil {
|
|
mobileObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
for _, item := range productItems {
|
|
err = model.OrderProductModel{}.Init().Create(&item)
|
|
if err != nil {
|
|
mobileObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
}
|
|
|
|
_, err = model.CartModel{}.Init().Del(ww, &cart)
|
|
if err != nil {
|
|
mobileObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName)
|
|
if err != nil {
|
|
mobileObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
productClient := product.NewProductClient(cli.Conn())
|
|
_, err = productClient.NumberItems(l.ctx, &product.NumberItemsReq{Type: utils.NumberOne, Items: NumItems})
|
|
if err != nil {
|
|
mobileObj.Rollback()
|
|
logx.Errorf("get products err: %v", err)
|
|
return l.out(utils.Fail, "产品库存不足")
|
|
}
|
|
|
|
mobileObj.Commit()
|
|
|
|
return l.ok(utils.NumberOne)
|
|
}
|