新增了 退还用户优惠卷、新增了处理用户处方图片
This commit is contained in:
+61
-18
@@ -70,6 +70,11 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
return false, errors.New("订单未支付,无需取消")
|
||||
}
|
||||
|
||||
// 检测问诊类型-服务包类型不允许取消
|
||||
if orderInquiry.InquiryMode == 8 || orderInquiry.InquiryMode == 9 {
|
||||
return false, errors.New("服务包类型订单不允许退款")
|
||||
}
|
||||
|
||||
// 检测退款金额
|
||||
if req.RefundAmount > orderInquiry.PaymentAmountTotal {
|
||||
return false, errors.New("退款金额不可超过实付金额")
|
||||
@@ -100,14 +105,11 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
}
|
||||
}()
|
||||
|
||||
// 问诊订单修改数据
|
||||
orderInquiryData := make(map[string]interface{})
|
||||
|
||||
// 退款编号
|
||||
inquiryRefundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
refundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
|
||||
// 退款状态转换
|
||||
var inquiryRefundStatus int
|
||||
var refundStatus int
|
||||
var successTime time.Time
|
||||
var refundId string
|
||||
|
||||
@@ -116,7 +118,7 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
refundRequest := weChat.RefundRequest{
|
||||
TransactionId: orderInquiry.EscrowTradeNo,
|
||||
OutTradeNo: orderInquiry.InquiryNo,
|
||||
OutRefundNo: inquiryRefundNo,
|
||||
OutRefundNo: refundNo,
|
||||
Reason: "客服取消",
|
||||
RefundAmount: int64(req.RefundAmount * 100),
|
||||
PaymentAmountTotal: int64(orderInquiry.PaymentAmountTotal * 100),
|
||||
@@ -136,18 +138,18 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
|
||||
if *refund.Status == "SUCCESS" {
|
||||
// 退款成功
|
||||
inquiryRefundStatus = 3
|
||||
refundStatus = 3
|
||||
|
||||
if refund.SuccessTime != nil {
|
||||
successTime = *refund.SuccessTime
|
||||
}
|
||||
} else if *refund.Status == "CLOSED" {
|
||||
// 退款关闭
|
||||
inquiryRefundStatus = 5
|
||||
refundStatus = 5
|
||||
|
||||
} else if *refund.Status == "PROCESSING" {
|
||||
// 退款处理中
|
||||
inquiryRefundStatus = 2
|
||||
refundStatus = 2
|
||||
|
||||
} else if *refund.Status == "ABNORMAL" {
|
||||
// 退款异常
|
||||
@@ -168,26 +170,40 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
} else {
|
||||
// 支付金额为0,模拟退款
|
||||
refundId = "模拟退款:" + strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
inquiryRefundStatus = 3
|
||||
refundStatus = 3
|
||||
successTime = time.Now()
|
||||
|
||||
// 模拟退款时手动退还优惠卷
|
||||
if orderInquiry.CouponAmountTotal > 0 {
|
||||
res := ReturnInquiryCoupon(tx, orderInquiry.OrderInquiryId)
|
||||
if !res {
|
||||
orderService := OrderService{}
|
||||
res, err := orderService.ReturnOrderCoupon(orderInquiry.InquiryNo, tx)
|
||||
if err != nil || !res {
|
||||
// 退还优惠卷失败
|
||||
tx.Rollback()
|
||||
return false, errors.New("退还优惠卷失败")
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
orderInquiryData["inquiry_refund_status"] = inquiryRefundStatus
|
||||
// 修改订单为取消
|
||||
orderData := make(map[string]interface{})
|
||||
orderData["cancel_status"] = 1
|
||||
orderData["cancel_time"] = time.Now().Format("2006-01-02 15:04:05")
|
||||
orderData["cancel_remarks"] = req.CancelRemarks
|
||||
|
||||
orderDao := dao.OrderDao{}
|
||||
err = orderDao.EditOrderById(tx, orderInquiry.OrderId, orderData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("取消订单失败")
|
||||
}
|
||||
|
||||
// 修改问诊订单为取消
|
||||
orderInquiryData := make(map[string]interface{})
|
||||
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:支付超时)
|
||||
orderInquiryData["cancel_remarks"] = req.CancelRemarks // 取消订单备注(自动添加)
|
||||
|
||||
// 修改问诊订单退款状态
|
||||
err = orderInquiryDao.EditOrderInquiryById(tx, orderInquiryId, orderInquiryData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -195,18 +211,45 @@ func (r *OrderInquiryService) CancelOrderInquiry(req requests.CancelOrderInquiry
|
||||
}
|
||||
|
||||
// 新增退款表
|
||||
orderRefund := &model.OrderRefund{
|
||||
OrderId: orderInquiry.OrderId,
|
||||
PatientId: orderInquiry.PatientId,
|
||||
OrderNo: orderInquiry.InquiryNo,
|
||||
RefundNo: refundNo,
|
||||
RefundId: refundId,
|
||||
RefundStatus: refundStatus,
|
||||
RefundTotal: req.RefundAmount,
|
||||
RefundReason: req.CancelRemarks,
|
||||
}
|
||||
|
||||
if refundStatus == 3 && !successTime.IsZero() {
|
||||
orderRefund.SuccessTime = model.LocalTime(successTime)
|
||||
}
|
||||
|
||||
orderRefundDao := dao.OrderRefundDao{}
|
||||
orderRefund, err = orderRefundDao.AddOrderRefund(tx, orderRefund)
|
||||
if err != nil || orderRefund == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
// 新增问诊退款表
|
||||
orderInquiryRefund := &model.OrderInquiryRefund{
|
||||
PatientId: orderInquiry.PatientId,
|
||||
OrderInquiryId: orderInquiryId,
|
||||
InquiryNo: orderInquiry.InquiryNo,
|
||||
InquiryRefundNo: inquiryRefundNo,
|
||||
InquiryRefundNo: refundNo,
|
||||
RefundId: refundId,
|
||||
InquiryRefundStatus: inquiryRefundStatus,
|
||||
InquiryRefundStatus: refundStatus,
|
||||
RefundTotal: req.RefundAmount,
|
||||
RefundReason: req.CancelRemarks,
|
||||
SuccessTime: model.LocalTime(successTime),
|
||||
}
|
||||
|
||||
if refundStatus == 3 && !successTime.IsZero() {
|
||||
orderRefund.SuccessTime = model.LocalTime(successTime)
|
||||
}
|
||||
|
||||
orderInquiryRefundDao := dao.OrderInquiryRefundDao{}
|
||||
orderInquiryRefund, err = orderInquiryRefundDao.AddOrderInquiryRefund(tx, orderInquiryRefund)
|
||||
if err != nil || orderInquiryRefund == nil {
|
||||
|
||||
@@ -3,8 +3,10 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/extend/aliyun"
|
||||
"hospital-admin-api/extend/prescription"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
@@ -310,3 +312,145 @@ func (r *OrderService) ReportPreProduct(orderProductId, adminUserId int64) (bool
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 发起订单退款-未完成
|
||||
func (r *OrderService) orderRefund(orderNo string, refundReason string) (bool, error) {
|
||||
orderDao := dao.OrderDao{}
|
||||
|
||||
// 获取订单数据
|
||||
maps := make(map[string]interface{})
|
||||
maps["order_no"] = orderNo
|
||||
order, err := orderDao.GetOrder(maps)
|
||||
if err != nil || order == nil {
|
||||
return false, errors.New("订单数据错误")
|
||||
}
|
||||
|
||||
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||||
if order.RefundStatus == 2 {
|
||||
return false, errors.New("订单退款中")
|
||||
}
|
||||
|
||||
if order.RefundStatus == 3 {
|
||||
return false, errors.New("订单已退款成功")
|
||||
}
|
||||
|
||||
if order.RefundStatus == 5 {
|
||||
return false, errors.New("订单已退款关闭")
|
||||
}
|
||||
|
||||
// 检测支付状态 (1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
if order.PayStatus != 2 {
|
||||
return false, errors.New("订单支付状态错误")
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// PdfToImg 处理pdf转图片
|
||||
func (r *OrderService) PdfToImg() (bool, error) {
|
||||
//orderPrescription := dao.OrderPrescriptionDao{}
|
||||
orderPrescriptionFileDao := dao.OrderPrescriptionFileDao{}
|
||||
|
||||
// 获取订单数据
|
||||
maps := make(map[string]interface{})
|
||||
orderPrescriptionFiles, err := orderPrescriptionFileDao.GetOrderPrescriptionFileList(maps)
|
||||
if err != nil {
|
||||
return false, errors.New("订单数据错误")
|
||||
}
|
||||
|
||||
for _, orderPrescriptionFile := range orderPrescriptionFiles {
|
||||
if orderPrescriptionFile.PrescriptionPdfOssPath == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if orderPrescriptionFile.IsConvertedPdf != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
OrderPrescriptionId := fmt.Sprintf("%d", orderPrescriptionFile.OrderPrescriptionId)
|
||||
|
||||
PrescriptionImgOssPath := strings.TrimLeft(orderPrescriptionFile.PrescriptionImgOssPath, "/")
|
||||
|
||||
// 下载处方pdf到本地
|
||||
local, err := aliyun.GetObjectToLocal(PrescriptionImgOssPath,
|
||||
"/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/bak/"+OrderPrescriptionId+".jpg")
|
||||
if err != nil || !local {
|
||||
return false, err
|
||||
}
|
||||
|
||||
//pdfPath := "/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/" + OrderPrescriptionId + ".pdf"
|
||||
//outputDir := "/Users/wucongxing/Desktop/work/go/hospital-admin-api/data/img"
|
||||
//
|
||||
//if err := utils.ConvertPDFToImages(pdfPath, outputDir, OrderPrescriptionId+".jpg"); err != nil {
|
||||
// return false, err
|
||||
//}
|
||||
//
|
||||
//// 修改为已转换
|
||||
//// 开始事务
|
||||
//tx := global.Db.Begin()
|
||||
//defer func() {
|
||||
// if r := recover(); r != nil {
|
||||
// tx.Rollback()
|
||||
// }
|
||||
//}()
|
||||
//
|
||||
//data := make(map[string]interface{})
|
||||
//data["is_converted_pdf"] = 1
|
||||
//err = orderPrescriptionFileDao.EditOrderPrescriptionFileById(tx, orderPrescriptionFile.PrescriptionFileId, data)
|
||||
//if err != nil {
|
||||
// tx.Rollback()
|
||||
// return false, err
|
||||
//}
|
||||
//
|
||||
//tx.Commit()
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ReturnOrderCoupon 退还用户优惠卷
|
||||
func (r *OrderService) ReturnOrderCoupon(orderNo string, tx *gorm.DB) (bool, error) {
|
||||
orderCouponDao := dao.OrderCouponDao{}
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
// 获取该订单全部优惠卷数据
|
||||
maps := make(map[string]interface{})
|
||||
maps["order_no"] = orderNo
|
||||
orderCoupons, err := orderCouponDao.GetOrderCouponList(maps)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 无优惠卷数据
|
||||
if len(orderCoupons) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
for _, coupon := range orderCoupons {
|
||||
// 获取用户优惠卷数据
|
||||
userCoupon, err := userCouponDao.GetUserCouponById(coupon.UserCouponId)
|
||||
if err != nil || userCoupon == nil {
|
||||
// 无该优惠卷数据,无需处理
|
||||
continue
|
||||
}
|
||||
|
||||
// 恢复优惠卷
|
||||
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, errors.New("恢复优惠卷失败")
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user