2023-07-20 06:43:59 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fusenapi/constants"
|
|
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
2023-07-24 02:10:26 +00:00
|
|
|
|
|
2023-07-20 06:43:59 +00:00
|
|
|
|
"fusenapi/utils/format"
|
2023-07-21 10:17:01 +00:00
|
|
|
|
"fusenapi/utils/order"
|
2023-07-20 06:43:59 +00:00
|
|
|
|
"math"
|
2023-07-20 09:49:09 +00:00
|
|
|
|
"time"
|
2023-07-20 06:43:59 +00:00
|
|
|
|
|
2023-07-20 07:43:48 +00:00
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"fusenapi/server/home-user-auth/internal/svc"
|
|
|
|
|
"fusenapi/server/home-user-auth/internal/types"
|
2023-07-20 06:43:59 +00:00
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
2023-07-20 07:55:01 +00:00
|
|
|
|
type UserOrderListLogic struct {
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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{
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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) {
|
2023-07-20 06:43:59 +00:00
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
|
// userinfo 传入值时, 一定不为null
|
2023-07-20 07:43:48 +00:00
|
|
|
|
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
2023-07-20 06:43:59 +00:00
|
|
|
|
|
|
|
|
|
// 查询总数
|
|
|
|
|
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 {
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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 02:10:26 +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
|
|
|
|
|
2023-07-20 06:43:59 +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
|
|
|
|
|
|
|
|
|
|
statusAndLogisticsRes := order.GetOrderStatusAndLogistics(order.GetOrderStatusAndLogisticsReq{
|
|
|
|
|
OrderStatus: constants.Order(*item.Status),
|
|
|
|
|
DeliveryMethod: constants.DeliveryMethod(*item.DeliveryMethod),
|
|
|
|
|
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 02:10:26 +00:00
|
|
|
|
OrderCtime: *item.Ctime,
|
|
|
|
|
//WebSetTimeInfo: orderTimeConfig,
|
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)
|
|
|
|
|
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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
|
2023-07-20 06:43:59 +00:00
|
|
|
|
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{
|
2023-07-20 06:43:59 +00:00
|
|
|
|
Items: respList,
|
|
|
|
|
Meta: types.Meta{
|
|
|
|
|
TotalCount: total,
|
|
|
|
|
PageCount: int64(math.Ceil(float64(total) / float64(pageSize))),
|
|
|
|
|
CurrentPage: int(page),
|
|
|
|
|
PerPage: int(pageSize),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|