From ad242af2c48b3fa9694084ca4f45e9555c1b9ae5 Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Mon, 3 Jun 2024 17:11:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8D=AF=E5=93=81=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/model/orderProduct.go | 1 + api/service/orderProduct.go | 119 ++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/api/model/orderProduct.go b/api/model/orderProduct.go index f4d27c0..836f1e8 100644 --- a/api/model/orderProduct.go +++ b/api/model/orderProduct.go @@ -23,6 +23,7 @@ type OrderProduct struct { IsDelete int `gorm:"column:is_delete;type:tinyint(1);default:0;comment:删除状态(0:否 1:是)" json:"is_delete"` CancelReason int `gorm:"column:cancel_reason;type:tinyint(1);comment:订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时 4:客服取消)" json:"cancel_reason"` AmountTotal float64 `gorm:"column:amount_total;type:decimal(10,2);default:0.00;comment:订单金额" json:"amount_total"` + CouponAmountTotal float64 `gorm:"column:coupon_amount_total;type:decimal(10,2);default:0.00;comment:优惠卷总金额" json:"coupon_amount_total"` PaymentAmountTotal float64 `gorm:"column:payment_amount_total;type:decimal(10,2);default:0.00;comment:实际付款金额" json:"payment_amount_total"` LogisticsFee float64 `gorm:"column:logistics_fee;type:decimal(10,2);default:0.00;comment:运费金额" json:"logistics_fee"` LogisticsNo string `gorm:"column:logistics_no;type:varchar(100);comment:物流编号" json:"logistics_no"` diff --git a/api/service/orderProduct.go b/api/service/orderProduct.go index f45ae63..19e6326 100644 --- a/api/service/orderProduct.go +++ b/api/service/orderProduct.go @@ -161,67 +161,78 @@ func (r *OrderProductService) CancelOrderProduct(req requests.CancelOrderProduct var successTime time.Time var refundId string - // // 通知处方平台订单取消 - // _, err = prescription.NoticePreOrderCancel(orderProduct.OrderProductNo) - // if err != nil { - // tx.Rollback() - // return false, err - // } - - // 微信退款 - refundRequest := weChat.RefundRequest{ - TransactionId: orderProduct.EscrowTradeNo, - OutTradeNo: orderProduct.OrderProductNo, - OutRefundNo: refundNo, - Reason: "客服取消", - RefundAmount: int64(orderProduct.PaymentAmountTotal * 100), - PaymentAmountTotal: int64(orderProduct.PaymentAmountTotal * 100), - NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.PatientProductRefundNotifyUrl, - } - - refund, err := refundRequest.Refund(2) - if err != nil { - tx.Rollback() - return false, errors.New(err.Error()) - } - - if refund.Status == nil { - tx.Rollback() - return false, errors.New("退款状态错误") - } - - if *refund.Status == "SUCCESS" { - // 退款成功 - refundStatus = 3 - - if refund.SuccessTime != nil { - successTime = *refund.SuccessTime + if orderProduct.PaymentAmountTotal > 0 { + // 微信退款 + refundRequest := weChat.RefundRequest{ + TransactionId: orderProduct.EscrowTradeNo, + OutTradeNo: orderProduct.OrderProductNo, + OutRefundNo: refundNo, + Reason: "客服取消", + RefundAmount: int64(orderProduct.PaymentAmountTotal * 100), + PaymentAmountTotal: int64(orderProduct.PaymentAmountTotal * 100), + NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.PatientProductRefundNotifyUrl, } - } else if *refund.Status == "CLOSED" { - // 退款关闭 - refundStatus = 5 - } else if *refund.Status == "PROCESSING" { - // 退款处理中 - refundStatus = 2 + refund, err := refundRequest.Refund(2) + if err != nil { + tx.Rollback() + return false, errors.New(err.Error()) + } - } else if *refund.Status == "ABNORMAL" { - // 退款异常 - tx.Rollback() - return false, errors.New("退款状态错误") + if refund.Status == nil { + tx.Rollback() + return false, errors.New("退款状态错误") + } + + if *refund.Status == "SUCCESS" { + // 退款成功 + refundStatus = 3 + + if refund.SuccessTime != nil { + successTime = *refund.SuccessTime + } + } else if *refund.Status == "CLOSED" { + // 退款关闭 + refundStatus = 5 + + } else if *refund.Status == "PROCESSING" { + // 退款处理中 + refundStatus = 2 + + } else if *refund.Status == "ABNORMAL" { + // 退款异常 + tx.Rollback() + return false, errors.New("退款状态错误") + } else { + tx.Rollback() + return false, errors.New("退款状态错误") + } + + if refund.RefundId == nil { + tx.Rollback() + return false, errors.New("缺少退款订单编号") + } + + // 退款编号 + refundId = *refund.RefundId } else { - tx.Rollback() - return false, errors.New("退款状态错误") - } + // 支付金额为0,模拟退款 + refundId = "模拟退款:" + strconv.FormatInt(global.Snowflake.Generate().Int64(), 10) + refundStatus = 3 + successTime = time.Now() - if refund.RefundId == nil { - tx.Rollback() - return false, errors.New("缺少退款订单编号") + // 模拟退款时手动退还优惠卷 + if orderProduct.CouponAmountTotal > 0 { + orderService := OrderService{} + res, err := orderService.ReturnOrderCoupon(orderProduct.OrderProductNo, tx) + if err != nil || !res { + // 退还优惠卷失败 + tx.Rollback() + return false, err + } + } } - // 退款编号 - refundId = *refund.RefundId - // 修改问诊订单退款状态 orderProductData["refund_status"] = refundStatus orderProductData["order_product_status"] = 5 // 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)