You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
19 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package order
import (
"fmt"
"recook/internal/api/mobile/pay/public"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/aftersales"
"recook/internal/model/goods"
"recook/internal/model/order"
"recook/internal/v2/logic/manage/jyy"
goods2 "recook/internal/v2/model/recook/goods"
"recook/tools"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
)
type queryOrderListParam struct {
UserID uint `json:"userId"`
Page uint `json:"page"`
OrderType string `json:"orderType" validate:"omitempty,oneof=0 1"`
IsSale bool `json:"is_sale"`
}
type queryOrderListResponse struct {
order.Information
CanConfirm bool `json:"canConfirm"`
TotalGoodsCount uint `json:"totalGoodsCount"`
GoodsList []order.GoodsDetail `json:"goodsList"`
OrderType string `json:"orderType"` // 订单类型,线上订单(配送) or 门店订单(自提)
CanPay bool `json:"can_pay"`
}
var TypeMap = map[uint]string{
0: "线上订单",
1: "门店订单",
}
/*全部订单*/
func QueryAllOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfoList []order.Information
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, nil).Find(&orderInfoList, "user_id = ? and deleted_at is null", p.UserID)
list := make([]queryOrderListResponse, 0, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
var totalGoodsCount uint = 0
for i2, v2 := range goodsList {
totalGoodsCount += v2.Quantity
//这个将这个产品列表的状态显示出来
goodsList[i2].RStatus = getStatus(v1, v2)
}
canConfirm := true
if v1.Status == 1 {
isAllAss := true
// for _, v := range goodsList { // 所有商品都已发货
// if v.ExpressStatus == 0 {
// canConfirm = false
// break
// }
// if v.AssType == 0 { // 如果商品都申请了售后,不能确认
// isAllAss = false
// break
// }
// }
for _, v := range goodsList { // 商品流程走完
if !v.IsOrderGoodsEnd() {
isAllAss = false
break
}
}
if isAllAss == false {
canConfirm = false
}
} else {
canConfirm = false
}
// 检测是全部发货还是部分发货
if v1.ExpressStatus == 1 {
isAllExpress := 2
for _, v := range goodsList {
if v.AssType == 0 && v.ExpressStatus == 0 {
isAllExpress = 1
break
}
}
v1.ExpressStatus = uint(isAllExpress)
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: canConfirm,
TotalGoodsCount: totalGoodsCount,
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
func getStatus(orderInfo order.Information, goodsDetail order.GoodsDetail) string {
//获取售后
var afterSales aftersales.Goods
dbc.DB.Last(&afterSales, "order_goods_id=?", goodsDetail.ID)
txt := ""
//0未付款 1支付成功 2订单取消 3订单过期 4交易成功 5订单关闭
if orderInfo.Status == 0 {
txt = ""
} else if orderInfo.Status >= 1 { //1支付成功
if orderInfo.ShippingMethod == 0 { //在线支付
if goodsDetail.AssType == 0 { //不是售后
if goodsDetail.ExpressStatus == 0 { //未发货
txt = "待发货"
} else {
txt = "已发货"
if goodsDetail.IsClosed == 1 {
txt = "退货退款关闭"
}
}
} else if goodsDetail.AssType == 1 { //仅退款
if afterSales.ReturnStatus == 2 { //未发货且售后完结
}
if goodsDetail.ExpressStatus == 0 {
txt = "待发货"
} else {
txt = "已发货"
}
if afterSales.ReturnStatus == 1 {
txt = "退款审核中"
}
if afterSales.ReturnStatus == 3 && goodsDetail.RefundStatus == 1 {
txt = "退款中"
}
if afterSales.ReturnStatus == 5 && goodsDetail.RefundStatus == 2 {
txt = "退款成功"
}
if goodsDetail.IsClosed == 1 {
txt = "退款关闭"
}
} else if goodsDetail.AssType == 2 { //售后完结
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 2 {
txt = "商家拒绝退款退货"
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 1 {
txt = "退货审核中"
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 3 {
txt = "待买家退回"
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 4 && goodsDetail.IsClosed == 0 {
txt = "待卖家收货"
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 5 && goodsDetail.RefundStatus == 1 {
txt = "退款中"
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 5 && goodsDetail.RefundStatus == 2 {
txt = "退款成功"
}
//if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 4 && goodsDetail.IsClosed == 1 {
// txt = "退货退款关闭"
//}
if goodsDetail.IsClosed == 1 {
txt = "退货退款关闭"
}
} else if goodsDetail.AssType == 3 {
if goodsDetail.RefundStatus == 1 {
txt = "退款中"
}
if goodsDetail.RefundStatus == 2 {
txt = "退款成功"
}
if afterSales.ReturnStatus == 1 {
txt = "补偿订单审核中"
}
if afterSales.ReturnStatus == 2 {
txt = "补偿订单被拒绝"
}
}
} else { //门店订单
if goodsDetail.ExpressStatus == 0 && goodsDetail.AssType == 0 {
txt = "待自提"
}
if goodsDetail.ExpressStatus == 0 && afterSales.ReturnStatus == 1 && goodsDetail.AssType == 1 {
txt = "退款审核中"
}
if goodsDetail.ExpressStatus == 0 && afterSales.ReturnStatus == 3 && goodsDetail.AssType == 1 && goodsDetail.RefundStatus == 1 {
txt = "退款中"
}
if goodsDetail.ExpressStatus == 0 && afterSales.ReturnStatus == 3 && goodsDetail.AssType == 1 && goodsDetail.RefundStatus == 2 {
txt = "退款成功"
}
if goodsDetail.ExpressStatus == 0 && goodsDetail.AssType == 1 && afterSales.ReturnStatus == 2 {
txt = "待自提"
}
if goodsDetail.ExpressStatus == 1 && orderInfo.Status == 1 {
txt = "自提待确认"
}
}
if goodsDetail.ExpressStatus == 1 && afterSales.ReturnStatus == 4 && goodsDetail.IsClosed == 1 {
txt = "退货退款关闭"
}
//过期订单不显示状态
if orderInfo.Status == 3 {
txt = ""
}
}
return txt
}
/*未付款订单*/
func QueryUnpaidOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfoList []order.Information
canpay := true
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, &canpay).Find(&orderInfoList, "user_id = ? AND status = 0 AND expire_time > ?", p.UserID, time.Now())
list := make([]queryOrderListResponse, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
for i2, v2 := range goodsList {
goodsList[i2].RStatus = getStatus(v1, v2)
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: false,
TotalGoodsCount: uint(len(goodsList)),
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
// 待处理订单
func QueryUnDealOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfoList []order.Information
canpay := false
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, &canpay).Find(&orderInfoList, "user_id = ? AND status = 0 AND expire_time > ?", p.UserID, time.Now())
list := make([]queryOrderListResponse, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
for i2, v2 := range goodsList {
goodsList[i2].RStatus = getStatus(v1, v2)
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: false,
TotalGoodsCount: uint(len(goodsList)),
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
/*待发货 付款成功*/
func QueryUndeliveredOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
//p.UserID = 97
//p.OrderType = "0"
//p.Page = 0
var orderInfoList []order.Information
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, nil).Find(&orderInfoList, "user_id = ? AND status = 1 AND express_status = 0", p.UserID)
list := make([]queryOrderListResponse, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
for i2, v2 := range goodsList {
goodsList[i2].RStatus = getStatus(v1, v2)
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: false,
TotalGoodsCount: uint(len(goodsList)),
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
/*待收货*/
func QueryWaitingForReceiptOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfoList []order.Information
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, nil).Find(&orderInfoList, "user_id = ? AND status=1 AND express_status = 1", p.UserID)
list := make([]queryOrderListResponse, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
for i2, v2 := range goodsList {
goodsList[i2].RStatus = getStatus(v1, v2)
}
canConfirm := true
for _, v := range goodsList {
if !v.IsOrderGoodsEnd() {
canConfirm = false
break
}
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: canConfirm,
TotalGoodsCount: uint(len(goodsList)),
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
/*待评价*/
func QueryUnEvaluateOrders(c *gin.Context) {
var p queryOrderListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfoList []order.Information
SearchOrderList(p.OrderType, p.Page, p.UserID, p.IsSale, nil).Find(&orderInfoList, "user_id = ? AND status=4 AND evaluated_at IS NULL", p.UserID)
list := make([]queryOrderListResponse, 0)
for i1, v1 := range orderInfoList {
var goodsList []order.GoodsDetail
dbc.DB.Find(&goodsList, "order_id = ?", v1.ID)
for i2, v2 := range goodsList {
goodsList[i2].RStatus = getStatus(v1, v2)
}
list = append(list, queryOrderListResponse{
Information: orderInfoList[i1],
CanConfirm: false,
TotalGoodsCount: uint(len(goodsList)),
GoodsList: goodsList,
OrderType: TypeMap[orderInfoList[i1].ShippingMethod],
CanPay: v1.CanPay,
})
}
back.Suc(c, "", list)
}
type queryOrderDetailParam struct {
UserID uint `json:"userId"`
OrderID uint `json:"orderId"`
}
type queryOrderDetailResponse struct {
order.Information
CanConfirm bool `json:"canConfirm"`
TotalGoodsCount uint `json:"totalGoodsCount"`
Coupon *order.CouponDetail `json:"coupon"`
Addr *order.Addr `json:"addr"`
Brands []BrandManyGoods `json:"brands"`
Invoice *order.Invoice `json:"invoice"`
BuyerMsg string `json:"buyerMsg"`
PayTime formatime.Second `json:"payTime"`
StatusList []goodsStatus `json:"status_list"`
MakeUpText string `json:"make_up_text"`
MakeUpAmount decimal.Decimal `json:"make_up_amount"`
CanPay bool `json:"can_pay"`
}
type goodsStatus struct {
GoodsId uint `json:"goods_id"`
Status uint `json:"status"`
}
// 一对多
type BrandManyGoods struct {
BrandID uint `json:"brandId"`
BrandName string `json:"brandName"`
BrandLogoUrl string `json:"brandLogoUrl"`
BrandExpressTotalAmount decimal.Decimal `json:"brandExpressTotalAmount"`
BrandGoodsTotalAmount decimal.Decimal `json:"brandGoodsTotalAmount"`
BrandGoodsTotalCount uint `json:"brandGoodsTotalCount"`
Goods []order.GoodsDetail `json:"goods"`
Coupon *order.CouponDetail `json:"coupon"` // 品牌优惠券
}
func QueryOrderDetail(c *gin.Context) {
var p queryOrderDetailParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
//p.OrderID = 91045098
//p.UserID = 4755
condition := !public.Judge(p.OrderID)
var orderInfo []order.Information
if condition {
if err := dbc.DB.Find(&orderInfo, "virtual_id = ?", p.OrderID).Error; err != nil {
back.Fail(c, err.Error())
return
}
} else {
if err := dbc.DB.Find(&orderInfo, "id = ?", p.OrderID).Error; err != nil {
back.Fail(c, err.Error())
return
}
}
od := make(map[uint]order.Information)
for _, v := range orderInfo {
od[v.ID] = v
}
// 买家留言
var buyerMsg = orderInfo[0].BuyerMessage
//var invoice order.Invoice
//dbc.DB.First(&invoice, "order_id = ?", orderInfo[0].ID)
var addr order.Addr
dbc.DB.First(&addr, "order_id = ?", orderInfo[0].ID)
//var universeCoupon order.CouponDetail
//dbc.DB.First(&universeCoupon, "order_id = ? AND scope = 0", orderInfo.ID)
// 获取到所有商品, 内存中进行分组操作
var goodsList []order.GoodsDetail
if condition {
subQ := dbc.DB.Table("recook_order_info").Select("id").Where("virtual_id = ?", p.OrderID).SubQuery()
dbc.DB.Find(&goodsList, "order_id in ?", subQ)
} else {
dbc.DB.Find(&goodsList, "order_id = ?", p.OrderID)
}
var totalGoodsCount uint = 0
brandMap := map[uint]uint{}
for i2, v := range goodsList {
brandMap[v.BrandID] = 1
totalGoodsCount += v.Quantity
//这边改线下状态
fmt.Println(v)
goodsList[i2].RStatus = getStatus(od[v.OrderID], v)
}
brandList := make([]BrandManyGoods, 0, 0)
for k := range brandMap {
var brandInfo goods.Brand
dbc.DB.Select("id, name, logo_url").First(&brandInfo, "id = ?", k)
//var brandCoupon order.CouponDetail
//dbc.DB.First(&brandCoupon, "order_id = ? AND brand_id = ?", orderInfo.ID, k)
internalGoodsList := make([]order.GoodsDetail, 0, 0)
brandExpressTotalFee := decimal.NewFromFloat(0.0)
brandGoodsTotalAmount := decimal.NewFromFloat(0.0)
brandGoodsTotalCount := 0
for i, v := range goodsList {
if v.BrandID == k {
internalGoodsList = append(internalGoodsList, goodsList[i])
brandExpressTotalFee = brandExpressTotalFee.Add(v.ExpressFee)
brandGoodsTotalAmount = brandGoodsTotalAmount.Add(v.GoodsAmount)
brandGoodsTotalCount += int(v.Quantity)
}
}
//brandCouponPtr := &order.CouponDetail{}
//if brandCoupon.ID == 0 {
// brandCouponPtr = nil
//} else {
// brandCouponPtr = &brandCoupon
//}
brandList = append(brandList, BrandManyGoods{
BrandID: brandInfo.ID,
BrandName: brandInfo.Name,
BrandLogoUrl: brandInfo.LogoURL,
BrandExpressTotalAmount: brandExpressTotalFee,
BrandGoodsTotalAmount: brandGoodsTotalAmount,
BrandGoodsTotalCount: uint(brandGoodsTotalCount),
Goods: internalGoodsList,
Coupon: nil,
})
}
//universeCouponPtr := &order.CouponDetail{}
//if universeCoupon.ID == 0 {
// universeCouponPtr = nil
//} else {
// universeCouponPtr = &universeCoupon
//}
//invoicePtr := &order.Invoice{}
//if invoice.ID == 0 {
// invoicePtr = nil
//} else {
// invoicePtr = &invoice
//}
canConfirm := true
if orderInfo[0].Status == 1 {
isAllAss := true
for _, v := range goodsList { // 所有商品都已发货
if v.ExpressStatus == 0 {
canConfirm = false
break
}
if v.AssType == 0 { // 如果商品都申请了售后,不能确认
isAllAss = false
break
}
}
if isAllAss {
canConfirm = false
}
} else {
canConfirm = false
}
// 检测是全部发货还是部分发货
if orderInfo[0].ExpressStatus == 1 {
isAllExpress := 2
for _, v := range goodsList {
if v.AssType == 0 && v.ExpressStatus == 0 {
isAllExpress = 1
break
}
}
orderInfo[0].ExpressStatus = uint(isAllExpress)
}
/*
新增内容返回goods上架状态
*/
var statusList []goodsStatus
for _, v := range goodsList {
var goodsS goods2.RecookGoodsInfoModel
dbc.DB.Table(goodsS.TableName()).First(&goodsS, "id=?", v.GoodsID)
statusList = append(statusList, goodsStatus{
GoodsId: v.GoodsID,
Status: goodsS.PublishStatus,
})
}
temp := order.Information{
ID: p.OrderID,
CoinTotalAmount: decimal.Decimal{},
ExpressTotalFee: decimal.Decimal{},
GoodsTotalAmount: decimal.Decimal{},
GoodsTotalCommission: decimal.Decimal{},
ActualTotalAmount: decimal.Decimal{},
Status: orderInfo[0].Status,
ExpressStatus: orderInfo[0].ExpressStatus,
EvaluatedAt: orderInfo[0].EvaluatedAt,
CreatedAt: orderInfo[0].CreatedAt,
ExpireTime: orderInfo[0].ExpireTime,
TradeNo: orderInfo[0].TradeNo,
PayTime: orderInfo[0].PayTime,
PayMethod: orderInfo[0].PayMethod,
CompletedAt: orderInfo[0].CompletedAt,
CanPay: orderInfo[0].CanPay,
}
for _, v := range orderInfo {
temp.CoinTotalAmount = temp.CoinTotalAmount.Add(v.CoinTotalAmount)
temp.ExpressTotalFee = temp.ExpressTotalFee.Add(v.ExpressTotalFee)
temp.GoodsTotalAmount = temp.GoodsTotalAmount.Add(v.GoodsTotalAmount)
temp.GoodsTotalCommission = temp.GoodsTotalCommission.Add(v.GoodsTotalCommission)
temp.ActualTotalAmount = temp.ActualTotalAmount.Add(v.ActualTotalAmount)
}
var o jyy.OrderConfirmReq
dbc.DB.Table((&jyy.OrderConfirmReq{}).TableName()).First(&o, "order_goods_id = ?", goodsList[0].ID)
back.Suc(c, "", queryOrderDetailResponse{
Information: temp,
CanConfirm: canConfirm,
TotalGoodsCount: totalGoodsCount,
Coupon: nil,
Addr: &addr,
Brands: brandList,
Invoice: nil,
BuyerMsg: buyerMsg,
PayTime: orderInfo[0].PayTime,
StatusList: statusList,
MakeUpText: o.Reason,
MakeUpAmount: o.Amount,
CanPay: temp.CanPay,
})
}
// 搜索订单 条件
func SearchOrderList(OrderType string, Page, UserID uint, isSale bool, canPay *bool) *gorm.DB {
queryDB := dbc.DB
if len(OrderType) > 0 {
oType, _ := strconv.Atoi(OrderType)
queryDB = queryDB.Where("shipping_method = ?", oType)
}
if isSale {
queryDB = queryDB.Where("order_type = 2")
} else {
queryDB = queryDB.Where("order_type = 1")
}
if canPay != nil && isSale {
queryDB = queryDB.Where("can_pay = ?", *canPay)
}
queryDB = queryDB.Where("is_split = 0").Order("id desc").Limit(10).Offset(10 * Page)
return queryDB
}