address add bystoreid api
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/services/order/internal/dao"
|
||||
"lone-services/services/order/internal/model"
|
||||
"lone-services/services/order/validator"
|
||||
"strconv"
|
||||
|
||||
"lone-services/rpc/order/pb"
|
||||
"lone-services/services/order/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddressByStoreLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewAddressByStoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddressByStoreLogic {
|
||||
return &AddressByStoreLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddressByStoreLogic) AddressByStore(in *order.AddressByStoreItemsRequest) (*order.Response, error) {
|
||||
var v validator.AddressByStoreItemsValidator
|
||||
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)
|
||||
}
|
||||
var info []dao.Address
|
||||
w := modelbase.Params{
|
||||
Order: "id desc",
|
||||
Eq: map[string]string{
|
||||
"target_id": strconv.FormatInt(in.StoreId, utils.NumberTen),
|
||||
"type": strconv.Itoa(dao.AddressTypeStore),
|
||||
"status": utils.StringStatusOk,
|
||||
},
|
||||
}
|
||||
|
||||
err := model.AddressModel{}.Init().Items(w, &info)
|
||||
if err != nil {
|
||||
l.Logger.Error(err)
|
||||
return l.fail(utils.ErrorNotFund)
|
||||
}
|
||||
for key, v := range info {
|
||||
mobile, aErr := utils.DecryptPhone(v.Mobile)
|
||||
if aErr == nil {
|
||||
info[key].AesMobile = v.Mobile
|
||||
info[key].Mobile = utils.DecryptPhoneReplace(mobile)
|
||||
}
|
||||
}
|
||||
return l.ok(info)
|
||||
}
|
||||
@@ -38,6 +38,10 @@ func (l *BaseLogic) out(status utils.Status, msg string) (*order.Response, error
|
||||
return &order.Response{Code: status.Code, Msg: msg}, nil
|
||||
}
|
||||
|
||||
func (l *BaseLogic) outData(status utils.Status, msg, data string) (*order.Response, error) {
|
||||
return &order.Response{Code: status.Code, Msg: msg, Data: data}, nil
|
||||
}
|
||||
|
||||
func (l *BaseLogic) ok(data any) (*order.Response, error) {
|
||||
buf, _ := jsoniter.Marshal(data)
|
||||
return &order.Response{Code: utils.Ok.Code, Msg: utils.Ok.Msg, Data: string(buf)}, nil
|
||||
|
||||
@@ -66,7 +66,7 @@ func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Respons
|
||||
DistrictIds: address.DistrictIds,
|
||||
}
|
||||
|
||||
w = modelbase.Params{
|
||||
ww := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"type": strconv.Itoa(int(in.Type)),
|
||||
},
|
||||
@@ -78,14 +78,14 @@ func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Respons
|
||||
data.SaleName = adminInfo.Name
|
||||
data.CreateType = dao.OrderTypeSale
|
||||
data.Type = dao.OrderTypeSale
|
||||
w.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
ww.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeSale), adminInfo.ID)
|
||||
case dao.CartTypeUser:
|
||||
data.UserId = int(adminInfo.ID)
|
||||
data.UserName = adminInfo.Name
|
||||
data.CreateType = dao.OrderTypePersonal
|
||||
data.Type = dao.OrderTypePersonal
|
||||
w.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
ww.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypePersonal), adminInfo.ID)
|
||||
case dao.CartTypeStore:
|
||||
data.StoreId = int(adminInfo.ID)
|
||||
@@ -96,13 +96,15 @@ func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Respons
|
||||
data.SaleProvince = int(adminInfo.SaleProvince)
|
||||
data.CreateType = dao.OrderTypeStore
|
||||
data.Type = dao.OrderTypeStore
|
||||
w.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
ww.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeStore), adminInfo.ID)
|
||||
}
|
||||
data.OrderSn = orderSn
|
||||
|
||||
var cart dao.Cart
|
||||
err = model.CartModel{}.Init().GetOne(w, &address)
|
||||
|
||||
err = model.CartModel{}.Init().GetOne(ww, &cart)
|
||||
l.Logger.Error(cart)
|
||||
if err != nil {
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
@@ -114,20 +116,40 @@ func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Respons
|
||||
if pErr != utils.Ok {
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
products, pErr := svc.GetCartData(ids)
|
||||
products, pErr := svc.GetCartData(l.svcCtx.ProductSvcName, ids)
|
||||
if pErr != utils.Ok {
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
keys := make([]string, utils.NumberZero, len(products))
|
||||
keys := make([]string, utils.NumberZero, len(products.Items))
|
||||
var totalPrice float64
|
||||
for _, item := range products {
|
||||
keys = append(keys, strconv.Itoa(int(item.Id)))
|
||||
totalPrice += item.Price * float64(item.Number)
|
||||
errData := make(map[int64]int64)
|
||||
productItems := []dao.OrderProductCreate{}
|
||||
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 {
|
||||
keys = append(keys, strconv.Itoa(int(item.Id)))
|
||||
totalPrice += item.Price * float64(buyNum)
|
||||
productItems = append(productItems, dao.OrderProductCreate{
|
||||
OrderSn: data.OrderSn,
|
||||
ProductId: int(item.Id),
|
||||
ProductName: item.Name,
|
||||
Price: item.Price,
|
||||
Number: int(buyNum),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(keys) < utils.NumberOne {
|
||||
return l.out(utils.Fail, "购买信息不存在")
|
||||
}
|
||||
data.ProductIds = strings.Join(keys, utils.DecollatorComma)
|
||||
data.TotalPrice = totalPrice
|
||||
|
||||
mobileObj := model.OrderModel{}.Init()
|
||||
|
||||
mobileObj.Begin()
|
||||
err = mobileObj.Create(&data)
|
||||
if err != nil {
|
||||
@@ -135,38 +157,35 @@ func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Respons
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
|
||||
for _, item := range products {
|
||||
orderProduct := dao.OrderProductCreate{
|
||||
Ordersn: data.OrderSn,
|
||||
ProductId: int(item.Id),
|
||||
ProductName: item.Name,
|
||||
Price: item.Price,
|
||||
Number: item.Number,
|
||||
}
|
||||
err = model.OrderProductModel{}.Init().Create(&orderProduct)
|
||||
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(w, &cart)
|
||||
if err != nil {
|
||||
mobileObj.Rollback()
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
//TODO 扣除库存
|
||||
//$decrease = Product::decrease(
|
||||
//['id' => $item['id'], ['number', '>=', $item['number']]],
|
||||
//'number',
|
||||
//$item['number']
|
||||
//);
|
||||
//if ($decrease < 1) {
|
||||
//Order::rollBack();
|
||||
//$retData['code'] = Code::code('low_stocks');
|
||||
//$retData['data'] = [$item['id'] => Product::value(['id' => $item['id']], 'number')];
|
||||
//return $retData;
|
||||
}
|
||||
|
||||
_, err = model.CartModel{}.Init().Del(ww, &cart)
|
||||
if err != nil {
|
||||
mobileObj.Rollback()
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
|
||||
//TODO 扣库存接口
|
||||
//cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName)
|
||||
//if err != nil {
|
||||
// mobileObj.Rollback()
|
||||
// return l.fail(utils.Fail)
|
||||
//}
|
||||
|
||||
//productClient := product.NewProductClient(cli.Conn())
|
||||
//_, err = productClient.Number(l.ctx, &product.NumberReq{Id: item.Id, Type: utils.NumberOne, Number: buyNum})
|
||||
//if err != nil {
|
||||
// mobileObj.Rollback()
|
||||
// logx.Errorf("get products err: %v", err)
|
||||
// return l.fail(utils.Fail, "产品库存不足")
|
||||
//}
|
||||
|
||||
mobileObj.Commit()
|
||||
|
||||
return l.ok(utils.NumberOne)
|
||||
|
||||
@@ -2,9 +2,15 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/rpc/order/pb"
|
||||
product "lone-services/rpc/product/pb"
|
||||
"lone-services/services/order/internal/dao"
|
||||
"lone-services/services/order/internal/model"
|
||||
"lone-services/services/order/internal/svc"
|
||||
"lone-services/services/order/validator"
|
||||
"strconv"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -13,6 +19,7 @@ type WebCreateOneLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewWebCreateOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebCreateOneLogic {
|
||||
@@ -24,7 +31,129 @@ func NewWebCreateOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebC
|
||||
}
|
||||
|
||||
func (l *WebCreateOneLogic) WebCreateOne(in *order.CreateOrderOneRequest) (*order.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
var v validator.OrderOneCreateValidator
|
||||
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)
|
||||
}
|
||||
|
||||
return &order.Response{}, nil
|
||||
w := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"id": strconv.FormatInt(in.AddressId, utils.NumberTen),
|
||||
"type": strconv.Itoa(int(in.Type)),
|
||||
"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,
|
||||
ExpiredTime: utils.GetAfterMinutes(utils.NumberThirty),
|
||||
Mobile: address.Mobile,
|
||||
Address: address.Address,
|
||||
Addressee: address.Name,
|
||||
DistrictIds: address.DistrictIds,
|
||||
}
|
||||
|
||||
ww := modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"type": strconv.Itoa(int(in.Type)),
|
||||
},
|
||||
}
|
||||
var orderSn string
|
||||
switch in.Type {
|
||||
case dao.CartTypeSale:
|
||||
data.SaleId = int(adminInfo.ID)
|
||||
data.SaleName = adminInfo.Name
|
||||
data.CreateType = dao.OrderTypeSale
|
||||
data.Type = dao.OrderTypeSale
|
||||
ww.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeSale), adminInfo.ID)
|
||||
case dao.CartTypeUser:
|
||||
data.UserId = int(adminInfo.ID)
|
||||
data.UserName = adminInfo.Name
|
||||
data.CreateType = dao.OrderTypePersonal
|
||||
data.Type = dao.OrderTypePersonal
|
||||
ww.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypePersonal), adminInfo.ID)
|
||||
case dao.CartTypeStore:
|
||||
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
|
||||
ww.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
||||
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeStore), adminInfo.ID)
|
||||
}
|
||||
data.OrderSn = orderSn
|
||||
|
||||
productInfo, pErr := svc.GetOneData(l.svcCtx.ProductSvcName, in.ProductId)
|
||||
if pErr != utils.Ok {
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
if productInfo.Id != in.ProductId {
|
||||
return l.out(utils.Fail, "购买信息不存在")
|
||||
}
|
||||
|
||||
if uint32(in.Num) > productInfo.Number {
|
||||
return l.out(utils.Fail,
|
||||
productInfo.Name+"购买数量超出库存,目前可以够买:"+strconv.Itoa(int(in.Num)))
|
||||
}
|
||||
|
||||
orderProduct := dao.OrderProductCreate{
|
||||
OrderSn: data.OrderSn,
|
||||
ProductId: int(in.ProductId),
|
||||
ProductName: productInfo.Name,
|
||||
Price: productInfo.Price,
|
||||
Number: int(in.Num),
|
||||
}
|
||||
|
||||
data.ProductIds = strconv.FormatInt(in.ProductId, utils.NumberTen)
|
||||
data.TotalPrice = float64(in.Num) * productInfo.Price
|
||||
mobileObj := model.OrderModel{}.Init()
|
||||
|
||||
mobileObj.Begin()
|
||||
err = mobileObj.Create(&data)
|
||||
if err != nil {
|
||||
mobileObj.Rollback()
|
||||
return l.fail(utils.Fail)
|
||||
}
|
||||
|
||||
err = model.OrderProductModel{}.Init().Create(&orderProduct)
|
||||
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.Number(l.ctx, &product.NumberReq{Id: in.ProductId, Type: utils.NumberOne, Number: uint32(in.Num)})
|
||||
if err != nil {
|
||||
mobileObj.Rollback()
|
||||
logx.Errorf("get products err: %v", err)
|
||||
return l.out(utils.Fail,
|
||||
productInfo.Name+"购买数量超出库存,目前可以够买:"+strconv.Itoa(int(in.Num)))
|
||||
}
|
||||
|
||||
mobileObj.Commit()
|
||||
|
||||
return l.ok(utils.NumberOne)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user