新增金额为0时退还优惠卷

This commit is contained in:
2023-08-03 15:50:25 +08:00
parent 96308ff608
commit ef1222b803
6 changed files with 321 additions and 26 deletions
+84 -24
View File
@@ -2,6 +2,7 @@ package service
import (
"errors"
"gorm.io/gorm"
"hospital-admin-api/api/dao"
"hospital-admin-api/api/model"
"hospital-admin-api/config"
@@ -86,11 +87,16 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
// 问诊订单修改数据
orderInquiryData := make(map[string]interface{})
// 退款编号
inquiryRefundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
// 退款状态转换
var inquiryRefundStatus int
var successTime time.Time
var refundId string
// 发起退款
if orderInquiry.PaymentAmountTotal > 0 {
// 退款编号
inquiryRefundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
refundRequest := weChat.RefundRequest{
TransactionId: orderInquiry.EscrowTradeNo,
OutTradeNo: orderInquiry.InquiryNo,
@@ -111,10 +117,6 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
return false, errors.New("退款状态错误")
}
// 退款状态转换
var inquiryRefundStatus int
var successTime time.Time
if *refund.Status == "SUCCESS" {
// 退款成功
inquiryRefundStatus = 3
@@ -126,9 +128,11 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
} else if *refund.Status == "CLOSED" {
// 退款关闭
inquiryRefundStatus = 5
} else if *refund.Status == "PROCESSING" {
// 退款处理中
inquiryRefundStatus = 2
} else if *refund.Status == "ABNORMAL" {
// 退款异常
tx.Rollback()
@@ -143,27 +147,25 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
return false, errors.New("缺少退款订单编号")
}
// 新增退款表
orderInquiryRefundDao := dao.OrderInquiryRefundDao{}
// 退款编号
refundId = *refund.RefundId
} else {
// 支付金额为0,模拟退款
refundId = "模拟退款:" + strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
inquiryRefundStatus = 3
successTime = time.Now()
orderInquiryRefund := &model.OrderInquiryRefund{
PatientId: orderInquiry.PatientId,
OrderInquiryId: orderInquiryId,
InquiryNo: orderInquiry.InquiryNo,
InquiryRefundNo: inquiryRefundNo,
RefundId: *refund.RefundId,
InquiryRefundStatus: inquiryRefundStatus,
RefundTotal: orderInquiry.PaymentAmountTotal,
RefundReason: "客服取消",
SuccessTime: successTime,
}
orderInquiryRefund, err = orderInquiryRefundDao.AddOrderInquiryRefund(tx, orderInquiryRefund)
if err != nil || orderInquiryRefund == nil {
tx.Rollback()
return false, errors.New(err.Error())
// 模拟退款时手动退还优惠卷
if orderInquiry.CouponAmountTotal > 0 {
res := ReturnInquiryCoupon(tx, orderInquiry.OrderInquiryId)
if !res {
tx.Rollback()
return false, errors.New("退还优惠卷失败")
}
}
}
orderInquiryData["inquiry_refund_status"] = inquiryRefundStatus
orderInquiryData["inquiry_status"] = 7 // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
orderInquiryData["cancel_time"] = time.Now().Format("2006-01-02 15:04:05") // 订单取消时间
orderInquiryData["cancel_reason"] = 4 // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
@@ -176,7 +178,65 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
return false, errors.New("取消订单失败")
}
// 新增退款表
orderInquiryRefund := &model.OrderInquiryRefund{
PatientId: orderInquiry.PatientId,
OrderInquiryId: orderInquiryId,
InquiryNo: orderInquiry.InquiryNo,
InquiryRefundNo: inquiryRefundNo,
RefundId: refundId,
InquiryRefundStatus: inquiryRefundStatus,
RefundTotal: orderInquiry.PaymentAmountTotal,
RefundReason: "客服取消",
SuccessTime: successTime,
}
orderInquiryRefundDao := dao.OrderInquiryRefundDao{}
orderInquiryRefund, err = orderInquiryRefundDao.AddOrderInquiryRefund(tx, orderInquiryRefund)
if err != nil || orderInquiryRefund == nil {
tx.Rollback()
return false, errors.New(err.Error())
}
tx.Commit()
return true, nil
}
// ReturnInquiryCoupon 退还问诊订单优惠卷
func ReturnInquiryCoupon(tx *gorm.DB, orderInquiryId int64) bool {
orderInquiryCouponDao := dao.OrderInquiryCouponDao{}
userCouponDao := dao.UserCouponDao{}
// 获取用户优惠卷信息
orderInquiry, err := orderInquiryCouponDao.GetOrderInquiryCouponByOrderInquiryId(orderInquiryId)
if err != nil || orderInquiry == nil {
// 订单未使用优惠卷,无需退还
return true
}
// 获取用户优惠卷数据
userCoupon, err := userCouponDao.GetUserCouponById(orderInquiry.UserCouponId)
if err != nil || userCoupon == nil {
// 无该优惠卷数据,无需处理
return true
}
// 恢复优惠卷
userCouponData := make(map[string]interface{})
// 检测优惠卷过期时间。判断是否需要退还
if userCoupon.ValidEndTime.Before(time.Now()) {
userCouponData["user_coupon_status"] = 3
} else {
userCouponData["user_coupon_status"] = 0
userCouponData["coupon_use_date"] = nil
}
err = userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, userCouponData)
if err != nil {
// 恢复优惠卷失败
return false
}
return true
}