fusenapi/server/home-user-auth/internal/logic/userorderlistlogic.go
2023-07-20 15:55:01 +08:00

149 lines
4.6 KiB
Go

package logic
import (
"errors"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"math"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserOrderListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderListLogic {
return &UserOrderListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
orderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
rowBuilder := orderModel.RowSelectBuilder(nil)
if userinfo == nil || userinfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
// 查询条件
var page = req.Page
var pageSize = req.PageSize
var listRes []*gmodel.FsOrderRel
rowBuilder = rowBuilder.Where("user_id =?", userinfo.UserId).Where("status =?", req.Status)
// 查询总数
total, err := orderModel.FindCount(l.ctx, rowBuilder, nil)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
}
// 查询数据
if total > 0 {
rowBuilder = rowBuilder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo").Preload("FsProductInfo")
})
listRes, err = orderModel.FindPageListByPage(l.ctx, rowBuilder, &page, &pageSize, nil, "")
}
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
}
listResLen := len(listRes)
var respList []types.Items
if listResLen > 0 {
// 数据处理
for _, item := range listRes {
var pbData types.Items
pbData.ID = item.Id
pbData.Sn = *item.Sn
pbData.UserID = *item.UserId
pbData.TotalAmount = *item.TotalAmount
pbData.Ctime = format.TimeIntToFormat(*item.Ctime)
pbData.Status = *item.Status
pbData.DeliveryMethod = *item.DeliveryMethod
pbData.TsTime = format.TimeToFormat(*item.TsTime)
pbData.IsPayCompleted = *item.IsPayCompleted
pbData.DeliverSn = *item.DeliverSn
var pcsBox int64
var pcs int64
var productList []*types.Product
if len(item.FsOrderDetails) > 0 {
for _, fsOrderDetailItem := range item.FsOrderDetails {
fsOrderDetailBuyNum := *fsOrderDetailItem.FsOrderDetail.BuyNum
fsOrderDetailEachBoxNum := *fsOrderDetailItem.FsOrderDetailTemplateInfo.EachBoxNum
pcs = pcs + fsOrderDetailBuyNum
pcsBoxNum := fsOrderDetailBuyNum / fsOrderDetailEachBoxNum
var csBoxNumF int64
if (fsOrderDetailBuyNum % fsOrderDetailEachBoxNum) > 0 {
csBoxNumF = 1
}
pcsBox = pcsBox + pcsBoxNum + csBoxNumF
var product types.Product
product.Cover = *fsOrderDetailItem.Cover
product.Fitting = *fsOrderDetailItem.OptionalTitle
product.OptionPrice = *fsOrderDetailItem.OptionPrice
product.OrderDetailTemplateId = *fsOrderDetailItem.OrderDetailTemplateId
product.OrderId = *fsOrderDetailItem.OrderId
product.Pcs = fsOrderDetailBuyNum
product.PcsBox = pcsBox
product.Price = *fsOrderDetailItem.FsOrderDetail.Amount
product.ProductId = *fsOrderDetailItem.OptionPrice
//product.Size = *fsOrderDetailItem.FsProductInfo.s
product.Title = *fsOrderDetailItem.FsProductInfo.Title
productList = append(productList, &product)
}
pbData.ProductList = productList
}
pbData.PcsBox = pcsBox
pbData.Pcs = pcs
pbData.SurplusAt = *item.Ctime + constants.CANCLE_ORDER_EXPIRE
pbData.LogisticsStatus = 1
pbData.Deposit = *item.TotalAmount / 2
pbData.Remaining = pbData.Deposit
respList = append(respList, pbData)
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UserOrderListRsp{
Items: respList,
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(pageSize))),
CurrentPage: int(page),
PerPage: int(pageSize),
},
})
}