This commit is contained in:
Hiven
2023-07-28 19:03:36 +08:00
parent ae717f932e
commit 1225a4efdc
23 changed files with 403 additions and 18 deletions

View File

@@ -6,11 +6,13 @@ import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
handlerUtils "fusenapi/utils/handler"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
@@ -50,11 +52,76 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
}
// 判断订单状态
if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) {
} else {
var notCancelStatusMap = make(map[constants.Order]struct{}, 3)
notCancelStatusMap[constants.STATUS_NEW_NOT_PAY] = struct{}{}
notCancelStatusMap[constants.STATUS_NEW_PART_PAY] = struct{}{}
notCancelStatusMap[constants.STATUS_NEW_PAY_COMPLETED] = struct{}{}
_, ok := notCancelStatusMap[constants.Order(*orderInfo.Status)]
if !ok {
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "the order status not cancle")
}
var cancelTime int64 = time.Now().Unix() - (*orderInfo.Ctime + int64(constants.CANCLE_ORDER_EXPIRE))
// 第一次支付成功后48小时后不能进行取消操作
if orderInfo.IsPayCompleted != nil && cancelTime > 0 {
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "The current order cannot be cancelled")
}
// 修改订单--取消状态和取消原因
*orderInfo.Status = int64(constants.STATUS_NEW_CANCEL)
*orderInfo.IsCancel = 1
orderInfo.RefundReasonId = &req.RefundReasonId
orderInfo.RefundReason = &req.RefundReason
var nowTime = time.Now().Unix()
var payList []handlerUtils.PayInfo
// 事务处理
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) {
// 修改订单信息
orderModelTS := gmodel.NewFsOrderModel(connGorm)
err = orderModelTS.Update(ctx, orderInfo)
if err != nil {
return err
}
// 新增退款记录
var isRefund int64 = 0
refundReasonModelTS := gmodel.NewFsRefundReasonModel(connGorm)
refundReasonModelTS.CreateOrUpdate(ctx, &gmodel.FsRefundReason{
IsRefund: &isRefund,
RefundReasonId: &req.RefundReasonId,
RefundReason: &req.RefundReason,
OrderId: &orderInfo.Id,
CreatedAt: &nowTime,
})
// 退款申请
// 退款申请--查询支付信息
fsPayModelTS := gmodel.NewFsPayModel(connGorm)
rbFsPay := fsPayModelTS.RowSelectBuilder(nil).Where("order_number = ?", orderInfo.Sn).Where("pay_status =?", constants.PAYSTATUS_SUCCESS).Where("is_refund =?", 0)
payInfoList, err := fsPayModelTS.FindAll(ctx, rbFsPay, nil, "")
if err != nil {
return err
}
for _, payInfo := range payInfoList {
var key string
if *payInfo.PaymentMethod == int64(constants.PAYMETHOD_STRIPE) {
key = l.svcCtx.Config.PayConfig.Stripe.Key
}
payList = append(payList, handlerUtils.PayInfo{
TradeNo: *payInfo.TradeNo,
PaymentMethod: *payInfo.PaymentMethod,
Key: key,
})
}
return nil
})
// 退款申请--调取第三方接口发起退款
handlerUtils.PayRefundHandler(&handlerUtils.PayRefundHandlerReq{
PayInfoList: payList,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeOrderCancelledNotOk, "the order cancle failed")
}
return resp.SetStatus(basic.CodeOK)
}