181 lines
5.8 KiB
Go
181 lines
5.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"gorm.io/gorm"
|
|
"strings"
|
|
"time"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/shopping-cart-confirmation/internal/svc"
|
|
"fusenapi/server/shopping-cart-confirmation/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CartOrderDetailLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCartOrderDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartOrderDetailLogic {
|
|
return &CartOrderDetailLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CartOrderDetailLogic) CartOrderDetail(req *types.CartOrderDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
if userinfo.GetIdType() != auth.IDTYPE_User {
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
|
|
}
|
|
req.Sn = strings.Trim(req.Sn, " ")
|
|
if req.Sn == "" {
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required")
|
|
}
|
|
//获取订单数据
|
|
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
|
|
orderInfo, err := orderModel.FindOneBySn(l.ctx, userinfo.UserId, req.Sn)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the order is not exists")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
|
|
}
|
|
//获取订单详细数据
|
|
orderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
|
|
orderDetailList, err := orderDetailModel.GetOrderDetailsByOrderId(l.ctx, orderInfo.Id)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order details")
|
|
}
|
|
if len(orderDetailList) == 0 {
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "order details is empty")
|
|
}
|
|
orderDetailTemplateIds := make([]int64, 0, len(orderDetailList))
|
|
productIds := make([]int64, 0, len(orderDetailList))
|
|
for _, v := range orderDetailList {
|
|
orderDetailTemplateIds = append(orderDetailTemplateIds, *v.OrderDetailTemplateId)
|
|
productIds = append(productIds, *v.ProductId)
|
|
}
|
|
//获取订单详情对应模板信息
|
|
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
|
|
orderDetailTemplateList, err := orderDetailTemplateModel.GetListByIds(l.ctx, orderDetailTemplateIds)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order details templates")
|
|
}
|
|
sizeIds := make([]int64, 0, len(orderDetailTemplateList))
|
|
mapDetailTemplate := make(map[int64]int)
|
|
for k, v := range orderDetailTemplateList {
|
|
sizeIds = append(sizeIds, *v.SizeId)
|
|
mapDetailTemplate[v.Id] = k
|
|
}
|
|
//获取尺寸信息
|
|
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
|
|
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
|
|
}
|
|
mapProductSize := make(map[int64]int)
|
|
for k, v := range productSizeList {
|
|
mapProductSize[v.Id] = k
|
|
}
|
|
//获取产品信息
|
|
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
|
|
productList, err := productModel.GetProductListByIds(l.ctx, productIds, "")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order products")
|
|
}
|
|
mapProduct := make(map[int64]int)
|
|
for k, v := range productList {
|
|
mapProduct[v.Id] = k
|
|
}
|
|
//获取用户地址信息
|
|
addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
|
addressList, err := addressModel.GetUserAllAddress(l.ctx, userinfo.UserId)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address")
|
|
}
|
|
//处理订单数据
|
|
addressItems := make([]types.CartAddr, 0, len(addressList))
|
|
for _, v := range addressList {
|
|
addressItems = append(addressItems, types.CartAddr{
|
|
Id: v.Id,
|
|
Name: *v.Name,
|
|
FirstName: *v.FirstName,
|
|
LastName: *v.LastName,
|
|
Mobile: *v.Mobile,
|
|
Street: *v.Street,
|
|
Suite: *v.Suite,
|
|
City: *v.City,
|
|
State: *v.State,
|
|
ZipCode: *v.ZipCode,
|
|
IsDefault: *v.IsDefault,
|
|
})
|
|
}
|
|
items := make([]types.CartDetailItem, 0, len(orderDetailList))
|
|
totalAmount := int64(0) //订单总金额
|
|
for _, v := range orderDetailList {
|
|
thisTotal := (*v.BuyNum) * (*v.Amount)
|
|
size := ""
|
|
if detailTemplateIndex, ok := mapDetailTemplate[*v.OrderDetailTemplateId]; ok {
|
|
detailTemplate := orderDetailTemplateList[detailTemplateIndex]
|
|
if sizeIndex, ok := mapProductSize[*detailTemplate.SizeId]; ok {
|
|
size = *productSizeList[sizeIndex].Capacity
|
|
}
|
|
}
|
|
name := ""
|
|
if productIndex, ok := mapProduct[*v.ProductId]; ok {
|
|
name = *productList[productIndex].Title
|
|
}
|
|
items = append(items, types.CartDetailItem{
|
|
Cover: *v.Cover,
|
|
Pcs: *v.BuyNum,
|
|
Amount: fmt.Sprintf("$ %.2f", float64(thisTotal)/100),
|
|
Option: *v.OptionalTitle,
|
|
Size: size,
|
|
Name: name,
|
|
})
|
|
totalAmount += thisTotal
|
|
}
|
|
//首付50%
|
|
total := totalAmount / 2
|
|
//尾款
|
|
remaining := totalAmount - total
|
|
payStep := int64(0) //未支付
|
|
if *orderInfo.PayedAmount == *orderInfo.TotalAmount/2 {
|
|
payStep = 1 //已支付首款
|
|
}
|
|
if *orderInfo.PayedAmount == *orderInfo.TotalAmount {
|
|
payStep = 2 //已支付尾款
|
|
}
|
|
payTime := ""
|
|
if *orderInfo.Ptime != 0 {
|
|
payTime = time.Unix(*orderInfo.Ptime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CartOrderDetailRsp{
|
|
DeliveryMethod: 1,
|
|
AddressId: 0,
|
|
PayTime: payTime,
|
|
PayMethod: 1,
|
|
PayStep: payStep,
|
|
Subtotal: fmt.Sprintf("$%.2f", float64(totalAmount)/100),
|
|
Total: fmt.Sprintf("$%.2f", float64(total)/100),
|
|
Remaining: fmt.Sprintf("$%.2f", float64(remaining)/100),
|
|
AddrList: addressItems,
|
|
Items: items,
|
|
})
|
|
}
|