package logic import ( "errors" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" //"fusenapi/utils/configs" "fusenapi/utils/format" "fusenapi/utils/order" "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 { // 获取订单时间配置 // orderTimeConfig, err := configs.GetOrderTimeConfig(l.ctx, l.svcCtx.MysqlConn) // if err != nil { // return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get config time info") // } // 数据处理 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 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, OrderCtime: *item.Ctime, //WebSetTimeInfo: orderTimeConfig, }) // 流程控制 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.Size = *fsOrderDetailItem.FsProductInfo.s product.Title = *fsOrderDetailItem.FsProductInfo.Title productList = append(productList, &product) } pbData.ProductList = productList } pbData.PcsBox = pcsBox pbData.Pcs = pcs pbData.SurplusAt = surplusAt 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), }, }) }