321 lines
11 KiB
Go
321 lines
11 KiB
Go
package service
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"hospital-admin-api/api/dao"
|
||
"hospital-admin-api/api/model"
|
||
"hospital-admin-api/api/requests"
|
||
"hospital-admin-api/api/responses/orderProductResponse"
|
||
"hospital-admin-api/config"
|
||
"hospital-admin-api/extend/weChat"
|
||
"hospital-admin-api/global"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
type OrderProductService struct {
|
||
}
|
||
|
||
// GetOrderProduct 药品订单详情
|
||
func (r *OrderProductService) GetOrderProduct(orderProductId int64) (getOrderProductResponse *orderProductResponse.GetOrderProduct, err error) {
|
||
// 获取药品订单数据
|
||
orderProductDao := dao.OrderProductDao{}
|
||
orderProduct, err := orderProductDao.GetOrderProductPreloadById(orderProductId)
|
||
if err != nil || orderProduct == nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 获取退款数据
|
||
orderProductRefundService := OrderProductRefundService{}
|
||
orderProductRefund, err := orderProductRefundService.GetOrderProductRefundByOrderProductId(orderProductId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 获取商品数据
|
||
orderProductItemService := OrderProductItemService{}
|
||
orderProductItem, err := orderProductItemService.GetOrderProductItemByOrderProductId(orderProductId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 获取物流数据
|
||
orderProductLogisticsService := OrderProductLogisticsService{}
|
||
orderProductLogistics, err := orderProductLogisticsService.GetOrderProductLogisticsByOrderProductId(orderProductId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 获取处方数据
|
||
orderPrescriptionService := OrderPrescriptionService{}
|
||
orderPrescription, err := orderPrescriptionService.GetOrderPrescriptionById(orderProduct.OrderPrescriptionId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 获取问诊病例
|
||
orderInquiryCaseService := OrderInquiryCaseService{}
|
||
orderInquiryCase, err := orderInquiryCaseService.GetOrderInquiryCaseByOrderInquiryId(orderProduct.OrderInquiryId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 医生数据
|
||
userDoctorService := UserDoctorService{}
|
||
userDoctor, err := userDoctorService.GetOrderInquiryUserDoctor(orderProduct.DoctorId)
|
||
if err != nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 处理返回值
|
||
getOrderProductResponse = &orderProductResponse.GetOrderProduct{
|
||
OrderProductId: fmt.Sprintf("%v", orderProduct.OrderProductId),
|
||
OrderInquiryId: fmt.Sprintf("%v", orderProduct.OrderInquiryId),
|
||
OrderPrescriptionId: fmt.Sprintf("%v", orderProduct.OrderPrescriptionId),
|
||
DoctorId: fmt.Sprintf("%v", orderProduct.DoctorId),
|
||
PatientId: fmt.Sprintf("%v", orderProduct.PatientId),
|
||
FamilyId: fmt.Sprintf("%v", orderProduct.FamilyId),
|
||
OrderProductNo: orderProduct.OrderProductNo,
|
||
EscrowTradeNo: orderProduct.EscrowTradeNo,
|
||
OrderProductStatus: orderProduct.OrderProductStatus,
|
||
PayChannel: orderProduct.PayChannel,
|
||
PayStatus: orderProduct.PayStatus,
|
||
CancelReason: orderProduct.CancelReason,
|
||
AmountTotal: orderProduct.AmountTotal,
|
||
PaymentAmountTotal: orderProduct.PaymentAmountTotal,
|
||
LogisticsFee: orderProduct.LogisticsFee,
|
||
LogisticsNo: orderProduct.LogisticsNo,
|
||
LogisticsCompanyCode: orderProduct.LogisticsCompanyCode,
|
||
DeliveryTime: orderProduct.DeliveryTime,
|
||
PayTime: orderProduct.PayTime,
|
||
Remarks: orderProduct.Remarks,
|
||
RefundStatus: orderProduct.RefundStatus,
|
||
CancelTime: orderProduct.CancelTime,
|
||
CancelRemarks: orderProduct.CancelRemarks,
|
||
ReportPreStatus: orderProduct.ReportPreStatus,
|
||
ReportPreTime: orderProduct.ReportPreTime,
|
||
ReportPreFailReason: orderProduct.ReportPreFailReason,
|
||
ProvinceId: orderProduct.ProvinceId,
|
||
Province: orderProduct.Province,
|
||
CityId: orderProduct.CityId,
|
||
City: orderProduct.City,
|
||
CountyId: orderProduct.CountyId,
|
||
County: orderProduct.County,
|
||
AddressMask: orderProduct.AddressMask,
|
||
ConsigneeNameMask: orderProduct.ConsigneeNameMask,
|
||
ConsigneeTelMask: orderProduct.ConsigneeTelMask,
|
||
CreatedAt: orderProduct.CreatedAt,
|
||
UpdatedAt: orderProduct.UpdatedAt,
|
||
OrderProductRefund: orderProductRefund,
|
||
OrderProductItem: orderProductItem,
|
||
OrderProductLogistics: orderProductLogistics,
|
||
UserDoctor: userDoctor,
|
||
OrderPrescription: orderPrescription,
|
||
OrderInquiryCase: orderInquiryCase,
|
||
}
|
||
|
||
return getOrderProductResponse, nil
|
||
}
|
||
|
||
// CancelOrderProduct 取消药品订单
|
||
func (r *OrderProductService) CancelOrderProduct(req requests.CancelOrderProduct, orderProductId, adminUserId int64) (bool, error) {
|
||
// 获取药品订单详情
|
||
orderProductDao := dao.OrderProductDao{}
|
||
orderProduct, err := orderProductDao.GetOrderProductPreloadById(orderProductId)
|
||
if err != nil || orderProduct == nil {
|
||
return false, errors.New("订单数据错误")
|
||
}
|
||
|
||
// 检测订单状态
|
||
if orderProduct.OrderProductStatus == 1 {
|
||
return false, errors.New("订单处于待支付状态,无法取消")
|
||
}
|
||
|
||
if orderProduct.OrderProductStatus == 3 {
|
||
return false, errors.New("订单已发货,无法取消")
|
||
}
|
||
|
||
if orderProduct.OrderProductStatus == 4 {
|
||
return false, errors.New("订单已签收,无法取消")
|
||
}
|
||
|
||
if orderProduct.OrderProductStatus == 5 {
|
||
return false, errors.New("订单已取消,无法取消")
|
||
}
|
||
|
||
// 已上报暂不允许取消订单
|
||
if orderProduct.ReportPreStatus == 1 {
|
||
return false, errors.New("订单已上报,暂不允许取消")
|
||
}
|
||
|
||
// 检测订单退款状态
|
||
if orderProduct.RefundStatus == 1 {
|
||
return false, errors.New("订单申请退款中,无法取消")
|
||
}
|
||
|
||
if orderProduct.RefundStatus == 2 {
|
||
return false, errors.New("订单正在退款中,无法取消")
|
||
}
|
||
|
||
if orderProduct.RefundStatus == 3 {
|
||
return false, errors.New("订单已退款成功,无法取消")
|
||
}
|
||
|
||
if orderProduct.RefundStatus == 6 {
|
||
return false, errors.New("订单退款异常,请联系技术人员")
|
||
}
|
||
|
||
// 检测订单支付状态
|
||
if orderProduct.PayStatus != 2 {
|
||
return false, errors.New("订单未支付,无需取消")
|
||
}
|
||
|
||
// 开始事务
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
// 药品订单修改数据
|
||
orderProductData := make(map[string]interface{})
|
||
|
||
// 退款编号
|
||
refundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||
|
||
// 退款状态转换
|
||
var refundStatus int
|
||
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: "客服取消",
|
||
PaymentAmountTotal: int64(orderProduct.PaymentAmountTotal * 100),
|
||
NotifyUrl: config.C.Wechat.RefundNotifyDomain + config.C.Wechat.PatientProductRefundNotifyUrl,
|
||
}
|
||
|
||
refund, err := refundRequest.Refund()
|
||
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
|
||
}
|
||
} 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
|
||
|
||
// 修改问诊订单退款状态
|
||
orderProductData["refund_status"] = refundStatus
|
||
orderProductData["order_product_status"] = 5 // 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
orderProductData["cancel_time"] = time.Now().Format("2006-01-02 15:04:05") // 订单取消时间
|
||
orderProductData["cancel_reason"] = 4 // 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时 4:客服取消)
|
||
orderProductData["cancel_remarks"] = req.CancelRemarks // 取消订单备注(自动添加)
|
||
// orderProductData["report_pre_status"] = 3 // 上报处方平台状态(0:未上报 1:已上报 2:上报失败 3:上报取消)
|
||
err = orderProductDao.EditOrderProductById(tx, orderProductId, orderProductData)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
return false, errors.New("取消订单失败")
|
||
}
|
||
|
||
// 新增退款表
|
||
orderProductRefund := &model.OrderProductRefund{
|
||
PatientId: orderProduct.PatientId,
|
||
OrderProductId: orderProductId,
|
||
OrderProductNo: orderProduct.OrderProductNo,
|
||
ProductRefundNo: refundNo,
|
||
RefundId: refundId,
|
||
ProductRefundStatus: refundStatus,
|
||
RefundTotal: orderProduct.PaymentAmountTotal,
|
||
RefundReason: req.CancelRemarks,
|
||
SuccessTime: model.LocalTime(successTime),
|
||
}
|
||
|
||
orderProductRefundDao := dao.OrderProductRefundDao{}
|
||
orderProductRefund, err = orderProductRefundDao.AddOrderProductRefund(tx, orderProductRefund)
|
||
if err != nil || orderProductRefund == nil {
|
||
tx.Rollback()
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 记录日志
|
||
orderOperationLog := &model.OrderOperationLog{
|
||
OrderId: orderProduct.OrderProductId,
|
||
OrderNo: orderProduct.OrderProductNo,
|
||
OrderType: 2,
|
||
OperatorId: adminUserId,
|
||
OperationContent: "取消订单并退款",
|
||
}
|
||
|
||
orderOperationLogDao := dao.OrderOperationLogDao{}
|
||
orderOperationLog, err = orderOperationLogDao.AddOrderOperationLog(tx, orderOperationLog)
|
||
if err != nil || orderOperationLog == nil {
|
||
tx.Rollback()
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
tx.Commit()
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// GetOrderProductConsignee 获取药品订单收货人数据
|
||
func (r *OrderProductService) GetOrderProductConsignee(orderProductId int64) (*orderProductResponse.GetOrderProductConsignee, error) {
|
||
// 获取药品订单数据
|
||
orderProductDao := dao.OrderProductDao{}
|
||
orderProduct, err := orderProductDao.GetOrderProductById(orderProductId)
|
||
if err != nil || orderProduct == nil {
|
||
return nil, errors.New(err.Error())
|
||
}
|
||
|
||
// 处理返回值
|
||
u := orderProductResponse.GetOrderProductConsigneeResponse(orderProduct)
|
||
|
||
return u, nil
|
||
}
|