119 lines
2.9 KiB
Go
119 lines
2.9 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"
|
|
|
|
"lone-services/rpc/order/pb"
|
|
"lone-services/services/order/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type WebCartLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewWebCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebCartLogic {
|
|
return &WebCartLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *WebCartLogic) WebCart(in *order.CartRequest) (*order.Response, error) {
|
|
var v validator.CartValidator
|
|
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.CartInfo
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"type": strconv.Itoa(int(in.Type)),
|
|
},
|
|
}
|
|
switch in.Type {
|
|
case dao.CartTypeSale:
|
|
w.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
|
case dao.CartTypeUser:
|
|
w.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
|
case dao.CartTypeStore:
|
|
w.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
|
|
}
|
|
|
|
err := model.CartModel{}.Init().GetOne(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.ErrorNotFund)
|
|
}
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
var ret []dao.CartRet
|
|
if len(info.Content) < utils.NumberOne {
|
|
return l.ok(ret)
|
|
}
|
|
|
|
var data [][]int
|
|
ok := utils.JsonStringToStruct(info.Content, &data)
|
|
if !ok {
|
|
return l.ok(ret)
|
|
}
|
|
|
|
var ids []int64
|
|
idsName := make(map[int64]int)
|
|
for _, v := range data {
|
|
ids = append(ids, int64(v[0]))
|
|
idsName[int64(v[0])] = v[1]
|
|
}
|
|
|
|
cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName)
|
|
if err != nil {
|
|
logx.Errorf("get rpc client err: %v", err)
|
|
return l.fail(utils.ErrorInternalServer)
|
|
}
|
|
productClient := product.NewProductClient(cli.Conn())
|
|
productItems, err := productClient.ItemsByIds(context.Background(), &product.ItemsByIdsReq{Ids: ids})
|
|
if err != nil {
|
|
logx.Errorf("get products err: %v", err)
|
|
return l.fail(utils.ErrorInternalServer)
|
|
}
|
|
|
|
logx.Errorf("get products: %v", productItems.Items)
|
|
logx.Errorf("get ids: %v", idsName)
|
|
|
|
for _, productItem := range productItems.Items {
|
|
num := 0
|
|
if v, ok := idsName[productItem.Id]; ok {
|
|
num = v
|
|
}
|
|
ret = append(ret, dao.CartRet{
|
|
ProductId: productItem.Id,
|
|
ProductName: productItem.Name,
|
|
Price: utils.RoundFloat(productItem.Price),
|
|
SalePrice: utils.RoundFloat(productItem.SalePrice),
|
|
StorePrice: utils.RoundFloat(productItem.StorePrice),
|
|
Stock: productItem.Number,
|
|
Images: productItem.Images,
|
|
Number: num,
|
|
})
|
|
}
|
|
|
|
return l.ok(ret)
|
|
}
|