179 lines
5.8 KiB
Go
179 lines
5.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"errors"
|
|
"fusenapi/constants"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/format"
|
|
"math"
|
|
"time"
|
|
|
|
"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)
|
|
|
|
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 {
|
|
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 {
|
|
// 数据处理
|
|
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
|
|
}
|
|
|
|
var surplusAt int64
|
|
surplusAt = (*item.Ctime + constants.CANCLE_ORDER_EXPIRE) - time.Now().Unix()
|
|
if surplusAt < 0 {
|
|
surplusAt = 0
|
|
}
|
|
//fsOrderAffiliateInfo := item.FsOrderAffiliateInfo
|
|
|
|
// 流程控制
|
|
// statusTime := make([]*types.StatusTime,8)
|
|
// statusTime[0] = &types.StatusTime{
|
|
// Key: 1,Time:*fsOrderAffiliateInfo.SureTime,
|
|
// }
|
|
// statusTime[1] = &types.StatusTime{
|
|
// Key: 1,Time:*ifsOrderAffiliateInfo.ProductTime,
|
|
// }
|
|
|
|
// `sure_time` int(10) unsigned DEFAULT '0' COMMENT '确认时间',
|
|
// `product_time` int(10) unsigned DEFAULT '0' COMMENT '生产时间',
|
|
// `product_endtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '生成完成时间',
|
|
// `deliver_time` int(10) unsigned DEFAULT '0' COMMENT '发货时间',
|
|
// `ups_deliver_time` int(10) unsigned DEFAULT '0' COMMENT 'ups发货时间',
|
|
// `ups_time` int(10) unsigned DEFAULT '0' COMMENT 'UPS提货时间',
|
|
// `arrival_time` int(10) unsigned DEFAULT '0' COMMENT '到达云仓的时间',
|
|
// `recevie_time` int(10) unsigned DEFAULT '0' COMMENT '云仓收货时间',
|
|
|
|
pbData.PcsBox = pcsBox
|
|
pbData.Pcs = pcs
|
|
pbData.SurplusAt = surplusAt
|
|
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),
|
|
},
|
|
})
|
|
}
|