fusenapi/server/home-user-auth/internal/logic/userorderlistlogic.go

208 lines
6.4 KiB
Go
Raw Normal View History

package logic
import (
"errors"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-07-24 04:19:34 +00:00
"fusenapi/utils/configs"
2023-07-24 02:10:26 +00:00
"fusenapi/utils/format"
2023-07-21 10:17:01 +00:00
"fusenapi/utils/order"
"math"
2023-07-20 09:49:09 +00:00
"time"
2023-07-20 07:43:48 +00:00
"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"
)
2023-07-20 07:55:01 +00:00
type UserOrderListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
2023-07-20 07:55:01 +00:00
func NewUserOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderListLogic {
return &UserOrderListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
2023-07-20 07:55:01 +00:00
func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-07-20 07:43:48 +00:00
2023-07-24 04:19:34 +00:00
// 测试
userinfo.UserId = 39
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
2023-07-20 09:49:09 +00:00
rowBuilder = rowBuilder.Where("user_id =?", userinfo.UserId)
if req.Status != -1 {
rowBuilder = rowBuilder.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 {
2023-07-20 09:49:09 +00:00
rowBuilder = rowBuilder.Preload("FsOrderAffiliateInfo").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 {
2023-07-21 10:17:01 +00:00
// 获取订单时间配置
2023-07-24 04:19:34 +00:00
orderTimeConfig, err := configs.GetOrderTimeConfig(l.ctx, l.svcCtx.MysqlConn)
if err != nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get config time info")
}
2023-07-21 10:17:01 +00:00
// 数据处理
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
2023-07-21 10:17:01 +00:00
var surplusAt int64
//如果是部分支付状态那么取消订单倒计时2天
if *item.Status == int64(constants.STATUS_NEW_PART_PAY) {
surplusAt = (*item.Ctime + int64(constants.CANCLE_ORDER_EXPIRE)) - time.Now().Unix()
if surplusAt < 0 {
surplusAt = 0
}
}
fsOrderAffiliateInfo := item.FsOrderAffiliateInfo
2023-07-24 04:19:34 +00:00
var getOrderStatusAndLogisticsReq = order.GetOrderStatusAndLogisticsReq{
OrderStatus: 1,
DeliveryMethod: 1,
2023-07-21 10:17:01 +00:00
IsPayCompleted: *item.IsAllProductCompleted,
SureTime: *fsOrderAffiliateInfo.SureTime,
ProductTime: *fsOrderAffiliateInfo.SureTime,
ProductEndtime: *fsOrderAffiliateInfo.SureTime,
DeliverTime: *fsOrderAffiliateInfo.SureTime,
UpsDeliverTime: *fsOrderAffiliateInfo.SureTime,
UpsTime: *fsOrderAffiliateInfo.SureTime,
ArrivalTime: *fsOrderAffiliateInfo.SureTime,
RecevieTime: *fsOrderAffiliateInfo.SureTime,
2023-07-24 04:19:34 +00:00
OrderCtime: *item.Ctime,
WebSetTimeInfo: orderTimeConfig,
}
statusAndLogisticsRes := order.GetOrderStatusAndLogistics(getOrderStatusAndLogisticsReq)
2023-07-21 10:17:01 +00:00
// 流程控制
statusTime := make([]*types.StatusTime, 5)
for _, itemTimes := range statusAndLogisticsRes.Times {
statusTime = append(statusTime, &types.StatusTime{
Key: itemTimes.Key,
Time: itemTimes.Time,
})
}
pbData.StatusTimes = statusTime
pbData.LogisticsStatus = int64(statusAndLogisticsRes.LogisticsStatus)
pbData.Status = int64(statusAndLogisticsRes.OrderStatus)
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.Title = *fsOrderDetailItem.FsProductInfo.Title
productList = append(productList, &product)
}
pbData.ProductList = productList
}
pbData.PcsBox = pcsBox
pbData.Pcs = pcs
2023-07-20 09:49:09 +00:00
pbData.SurplusAt = surplusAt
pbData.Deposit = *item.TotalAmount / 2
pbData.Remaining = pbData.Deposit
respList = append(respList, pbData)
}
}
2023-07-20 07:55:01 +00:00
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),
},
})
}