fix 1
This commit is contained in:
@@ -524,8 +524,9 @@ func (s *ComponentReportOrderService) CreatePaymentOrder(ctx context.Context, re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 返回支付响应,包含支付URL
|
// 返回支付响应,包含支付URL
|
||||||
|
// 使用购买订单ID而不是下载记录ID,以便前端可以正确检查支付状态
|
||||||
response := &CreatePaymentOrderResponse{
|
response := &CreatePaymentOrderResponse{
|
||||||
OrderID: download.ID,
|
OrderID: createdPurchaseOrder.ID, // 修改为购买订单ID
|
||||||
OrderNo: createdPurchaseOrder.OrderNo,
|
OrderNo: createdPurchaseOrder.OrderNo,
|
||||||
PaymentType: req.PaymentType,
|
PaymentType: req.PaymentType,
|
||||||
Amount: finalPrice.String(),
|
Amount: finalPrice.String(),
|
||||||
@@ -601,7 +602,7 @@ func (s *ComponentReportOrderService) createFreeOrder(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &CreatePaymentOrderResponse{
|
return &CreatePaymentOrderResponse{
|
||||||
OrderID: download.ID,
|
OrderID: createdPurchaseOrder.ID, // 修改为购买订单ID
|
||||||
OrderNo: createdPurchaseOrder.OrderNo,
|
OrderNo: createdPurchaseOrder.OrderNo,
|
||||||
PaymentType: "free",
|
PaymentType: "free",
|
||||||
Amount: "0.00",
|
Amount: "0.00",
|
||||||
@@ -650,106 +651,90 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
|
|||||||
zap.Time("check_time", time.Now()),
|
zap.Time("check_time", time.Now()),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 获取下载记录信息
|
// 直接查询购买订单
|
||||||
download, err := s.componentReportRepo.GetDownloadByID(ctx, orderID)
|
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, orderID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("获取下载记录信息失败",
|
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", orderID))
|
||||||
zap.String("order_id", orderID),
|
return &CheckPaymentStatusResponse{
|
||||||
zap.Error(err))
|
OrderID: orderID,
|
||||||
return nil, fmt.Errorf("获取下载记录信息失败: %w", err)
|
PaymentStatus: "unknown",
|
||||||
|
CanDownload: false,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Info("下载记录信息",
|
// 如果订单不存在,返回未知状态
|
||||||
zap.String("order_id", orderID),
|
if purchaseOrder == nil {
|
||||||
zap.String("user_id", download.UserID),
|
s.logger.Error("购买订单不存在", zap.String("order_id", orderID))
|
||||||
zap.String("product_id", download.ProductID),
|
return &CheckPaymentStatusResponse{
|
||||||
zap.String("product_code", download.ProductCode),
|
OrderID: orderID,
|
||||||
zap.String("product_name", download.ProductName),
|
PaymentStatus: "unknown",
|
||||||
zap.Any("order_id", download.OrderID),
|
CanDownload: false,
|
||||||
zap.Any("order_number", download.OrderNumber),
|
}, nil
|
||||||
)
|
}
|
||||||
|
|
||||||
// 使用OrderID查询购买订单状态来判断支付状态
|
// 如果购买订单状态是 Created(待支付),需要主动查询支付状态
|
||||||
|
if purchaseOrder.Status == entities.PurchaseOrderStatusCreated {
|
||||||
|
s.logger.Info("购买订单状态为待支付,主动查询支付状态",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("order_no", purchaseOrder.OrderNo),
|
||||||
|
zap.String("pay_channel", purchaseOrder.PayChannel),
|
||||||
|
zap.String("payment_type", purchaseOrder.PaymentType),
|
||||||
|
zap.String("amount", purchaseOrder.Amount.String()),
|
||||||
|
zap.Time("query_start_time", time.Now()))
|
||||||
|
|
||||||
|
// 根据支付渠道查询支付状态
|
||||||
|
if purchaseOrder.PayChannel == "alipay" && s.aliPayService != nil {
|
||||||
|
s.logger.Info("开始查询支付宝订单状态",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("order_no", purchaseOrder.OrderNo))
|
||||||
|
err = s.queryAlipayOrderStatusAndUpdate(ctx, purchaseOrder.ID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("查询支付宝订单状态失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.Error(err))
|
||||||
|
}
|
||||||
|
} else if purchaseOrder.PayChannel == "wechat" && s.wechatPayService != nil {
|
||||||
|
s.logger.Info("开始查询微信订单状态",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("order_no", purchaseOrder.OrderNo))
|
||||||
|
err = s.queryWechatOrderStatusAndUpdate(ctx, purchaseOrder.ID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("查询微信订单状态失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新获取更新后的购买订单信息
|
||||||
|
updatedOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrder.ID)
|
||||||
|
if err == nil && updatedOrder != nil {
|
||||||
|
s.logger.Info("获取更新后的购买订单信息",
|
||||||
|
zap.String("purchase_order_id", updatedOrder.ID),
|
||||||
|
zap.String("order_no", updatedOrder.OrderNo),
|
||||||
|
zap.String("status", string(updatedOrder.Status)))
|
||||||
|
purchaseOrder = updatedOrder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据购买订单状态设置支付状态
|
||||||
var paymentStatus string
|
var paymentStatus string
|
||||||
var canDownload bool
|
var canDownload bool
|
||||||
|
|
||||||
if download.OrderID != nil {
|
switch purchaseOrder.Status {
|
||||||
// 查询购买订单状态
|
case entities.PurchaseOrderStatusPaid:
|
||||||
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
|
paymentStatus = "success"
|
||||||
if err != nil {
|
canDownload = true
|
||||||
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
|
case entities.PurchaseOrderStatusCreated:
|
||||||
paymentStatus = "unknown"
|
|
||||||
} else {
|
|
||||||
// 如果购买订单状态是 Created(待支付),需要主动查询支付状态
|
|
||||||
if purchaseOrder.Status == entities.PurchaseOrderStatusCreated {
|
|
||||||
s.logger.Info("购买订单状态为待支付,主动查询支付状态",
|
|
||||||
zap.String("purchase_order_id", purchaseOrder.ID),
|
|
||||||
zap.String("order_no", purchaseOrder.OrderNo),
|
|
||||||
zap.String("pay_channel", purchaseOrder.PayChannel),
|
|
||||||
zap.String("payment_type", purchaseOrder.PaymentType),
|
|
||||||
zap.String("amount", purchaseOrder.Amount.String()),
|
|
||||||
zap.Time("query_start_time", time.Now()))
|
|
||||||
|
|
||||||
// 根据支付渠道查询支付状态
|
|
||||||
if purchaseOrder.PayChannel == "alipay" && s.aliPayService != nil {
|
|
||||||
s.logger.Info("开始查询支付宝订单状态",
|
|
||||||
zap.String("purchase_order_id", purchaseOrder.ID),
|
|
||||||
zap.String("order_no", purchaseOrder.OrderNo))
|
|
||||||
err = s.queryAlipayOrderStatusAndUpdate(ctx, purchaseOrder.ID)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("查询支付宝订单状态失败",
|
|
||||||
zap.String("purchase_order_id", purchaseOrder.ID),
|
|
||||||
zap.Error(err))
|
|
||||||
}
|
|
||||||
} else if purchaseOrder.PayChannel == "wechat" && s.wechatPayService != nil {
|
|
||||||
s.logger.Info("开始查询微信订单状态",
|
|
||||||
zap.String("purchase_order_id", purchaseOrder.ID),
|
|
||||||
zap.String("order_no", purchaseOrder.OrderNo))
|
|
||||||
err = s.queryWechatOrderStatusAndUpdate(ctx, purchaseOrder.ID)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("查询微信订单状态失败",
|
|
||||||
zap.String("purchase_order_id", purchaseOrder.ID),
|
|
||||||
zap.Error(err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重新获取更新后的购买订单信息
|
|
||||||
updatedOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
|
|
||||||
if err == nil && updatedOrder != nil {
|
|
||||||
s.logger.Info("获取更新后的购买订单信息",
|
|
||||||
zap.String("purchase_order_id", updatedOrder.ID),
|
|
||||||
zap.String("order_no", updatedOrder.OrderNo),
|
|
||||||
zap.String("status", string(updatedOrder.Status)))
|
|
||||||
purchaseOrder = updatedOrder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根据购买订单状态设置支付状态
|
|
||||||
switch purchaseOrder.Status {
|
|
||||||
case entities.PurchaseOrderStatusPaid:
|
|
||||||
paymentStatus = "success"
|
|
||||||
canDownload = true
|
|
||||||
case entities.PurchaseOrderStatusCreated:
|
|
||||||
paymentStatus = "pending"
|
|
||||||
canDownload = false
|
|
||||||
case entities.PurchaseOrderStatusCancelled:
|
|
||||||
paymentStatus = "cancelled"
|
|
||||||
canDownload = false
|
|
||||||
case entities.PurchaseOrderStatusFailed:
|
|
||||||
paymentStatus = "failed"
|
|
||||||
canDownload = false
|
|
||||||
default:
|
|
||||||
paymentStatus = "unknown"
|
|
||||||
canDownload = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
paymentStatus = "pending"
|
paymentStatus = "pending"
|
||||||
canDownload = false
|
canDownload = false
|
||||||
}
|
case entities.PurchaseOrderStatusCancelled:
|
||||||
|
paymentStatus = "cancelled"
|
||||||
// 检查是否过期
|
canDownload = false
|
||||||
if download.IsExpired() {
|
case entities.PurchaseOrderStatusFailed:
|
||||||
|
paymentStatus = "failed"
|
||||||
|
canDownload = false
|
||||||
|
default:
|
||||||
|
paymentStatus = "unknown"
|
||||||
canDownload = false
|
canDownload = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -762,7 +747,7 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
|
|||||||
)
|
)
|
||||||
|
|
||||||
return &CheckPaymentStatusResponse{
|
return &CheckPaymentStatusResponse{
|
||||||
OrderID: download.ID,
|
OrderID: orderID,
|
||||||
PaymentStatus: paymentStatus,
|
PaymentStatus: paymentStatus,
|
||||||
CanDownload: canDownload,
|
CanDownload: canDownload,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -770,41 +755,83 @@ func (s *ComponentReportOrderService) CheckPaymentStatus(ctx context.Context, or
|
|||||||
|
|
||||||
// DownloadFile 下载文件
|
// DownloadFile 下载文件
|
||||||
func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID string) (string, error) {
|
func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID string) (string, error) {
|
||||||
// 获取下载记录信息
|
s.logger.Info("开始下载文件", zap.String("order_id", orderID))
|
||||||
download, err := s.componentReportRepo.GetDownloadByID(ctx, orderID)
|
|
||||||
|
// 首先通过orderID查询购买订单
|
||||||
|
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, orderID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("获取下载记录信息失败: %w", err)
|
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", orderID))
|
||||||
|
return "", fmt.Errorf("查询购买订单失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用OrderID查询购买订单状态来判断支付状态
|
if purchaseOrder == nil {
|
||||||
var canDownload bool
|
s.logger.Error("购买订单不存在", zap.String("order_id", orderID))
|
||||||
|
return "", fmt.Errorf("购买订单不存在")
|
||||||
if download.OrderID != nil {
|
}
|
||||||
// 查询购买订单状态
|
|
||||||
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, *download.OrderID)
|
// 检查购买订单状态
|
||||||
if err != nil {
|
if purchaseOrder.Status != entities.PurchaseOrderStatusPaid {
|
||||||
s.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
|
s.logger.Error("订单未支付,无法下载文件",
|
||||||
canDownload = false
|
zap.String("order_id", orderID),
|
||||||
} else {
|
zap.String("status", string(purchaseOrder.Status)))
|
||||||
// 检查购买订单状态
|
return "", fmt.Errorf("订单未支付,无法下载文件")
|
||||||
canDownload = purchaseOrder.Status == entities.PurchaseOrderStatusPaid
|
}
|
||||||
|
|
||||||
|
// 获取产品信息
|
||||||
|
product, err := s.productRepo.GetByID(ctx, purchaseOrder.ProductID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("获取产品信息失败", zap.Error(err), zap.String("product_id", purchaseOrder.ProductID))
|
||||||
|
return "", fmt.Errorf("获取产品信息失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已有下载记录
|
||||||
|
download, err := s.componentReportRepo.GetDownloadByPaymentOrderID(ctx, orderID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Warn("查询下载记录失败,将创建新记录", zap.Error(err), zap.String("order_id", orderID))
|
||||||
|
download = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有下载记录,创建一个新的
|
||||||
|
if download == nil {
|
||||||
|
s.logger.Info("创建新的下载记录",
|
||||||
|
zap.String("order_id", orderID),
|
||||||
|
zap.String("user_id", purchaseOrder.UserID),
|
||||||
|
zap.String("product_id", purchaseOrder.ProductID))
|
||||||
|
|
||||||
|
// 创建新的下载记录
|
||||||
|
newDownload := &productEntities.ComponentReportDownload{
|
||||||
|
UserID: purchaseOrder.UserID,
|
||||||
|
ProductID: purchaseOrder.ProductID,
|
||||||
|
ProductCode: product.Code,
|
||||||
|
ProductName: product.Name,
|
||||||
|
OrderID: &purchaseOrder.ID,
|
||||||
|
OrderNumber: &purchaseOrder.OrderNo,
|
||||||
|
ExpiresAt: calculateExpiryTime(),
|
||||||
}
|
}
|
||||||
} else if download.OrderNumber != nil {
|
|
||||||
// 兼容旧的支付订单逻辑
|
// 保存下载记录
|
||||||
canDownload = true // 简化处理,有支付订单号就认为已支付
|
err = s.componentReportRepo.Create(ctx, newDownload)
|
||||||
} else {
|
if err != nil {
|
||||||
canDownload = false
|
s.logger.Error("创建下载记录失败", zap.Error(err))
|
||||||
|
return "", fmt.Errorf("创建下载记录失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("成功创建下载记录",
|
||||||
|
zap.String("order_id", orderID),
|
||||||
|
zap.String("download_id", newDownload.ID),
|
||||||
|
zap.String("product_id", newDownload.ProductID))
|
||||||
|
|
||||||
|
download = newDownload
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否过期
|
// 检查是否过期
|
||||||
if download.IsExpired() {
|
if download.IsExpired() {
|
||||||
canDownload = false
|
s.logger.Error("下载链接已过期",
|
||||||
|
zap.String("order_id", orderID),
|
||||||
|
zap.Time("expires_at", *download.ExpiresAt))
|
||||||
|
return "", fmt.Errorf("下载链接已过期,无法下载文件")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !canDownload {
|
|
||||||
return "", fmt.Errorf("订单未支付或已过期,无法下载文件")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查文件是否已存在
|
// 检查文件是否已存在
|
||||||
if download.FilePath != nil && *download.FilePath != "" {
|
if download.FilePath != nil && *download.FilePath != "" {
|
||||||
// 文件已存在,直接返回文件路径
|
// 文件已存在,直接返回文件路径
|
||||||
@@ -815,8 +842,10 @@ func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 文件不存在,生成文件
|
// 文件不存在,生成文件
|
||||||
|
s.logger.Info("开始生成报告文件", zap.String("order_id", orderID))
|
||||||
filePath, err := s.generateReportFile(ctx, download)
|
filePath, err := s.generateReportFile(ctx, download)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
s.logger.Error("生成报告文件失败", zap.Error(err), zap.String("order_id", orderID))
|
||||||
return "", fmt.Errorf("生成报告文件失败: %w", err)
|
return "", fmt.Errorf("生成报告文件失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -882,6 +911,60 @@ func (s *ComponentReportOrderService) GetUserOrders(ctx context.Context, userID
|
|||||||
return result, int64(len(result)), nil
|
return result, int64(len(result)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createDownloadRecordForPaidOrder 为已支付订单创建下载记录
|
||||||
|
func (s *ComponentReportOrderService) createDownloadRecordForPaidOrder(ctx context.Context, purchaseOrder *entities.PurchaseOrder) error {
|
||||||
|
s.logger.Info("开始为已支付订单创建下载记录",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("order_no", purchaseOrder.OrderNo),
|
||||||
|
zap.String("user_id", purchaseOrder.UserID),
|
||||||
|
zap.String("product_id", purchaseOrder.ProductID))
|
||||||
|
|
||||||
|
// 检查是否已有下载记录
|
||||||
|
existingDownload, err := s.componentReportRepo.GetDownloadByPaymentOrderID(ctx, purchaseOrder.ID)
|
||||||
|
if err == nil && existingDownload != nil {
|
||||||
|
s.logger.Info("下载记录已存在,跳过创建",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("download_id", existingDownload.ID))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取产品信息
|
||||||
|
product, err := s.productRepo.GetByID(ctx, purchaseOrder.ProductID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("获取产品信息失败",
|
||||||
|
zap.Error(err),
|
||||||
|
zap.String("product_id", purchaseOrder.ProductID))
|
||||||
|
return fmt.Errorf("获取产品信息失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建新的下载记录
|
||||||
|
download := &productEntities.ComponentReportDownload{
|
||||||
|
UserID: purchaseOrder.UserID,
|
||||||
|
ProductID: purchaseOrder.ProductID,
|
||||||
|
ProductCode: product.Code,
|
||||||
|
ProductName: product.Name,
|
||||||
|
OrderID: &purchaseOrder.ID,
|
||||||
|
OrderNumber: &purchaseOrder.OrderNo,
|
||||||
|
ExpiresAt: calculateExpiryTime(), // 30天后过期
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存下载记录
|
||||||
|
err = s.componentReportRepo.Create(ctx, download)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("创建下载记录失败", zap.Error(err))
|
||||||
|
return fmt.Errorf("创建下载记录失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Info("成功为已支付订单创建下载记录",
|
||||||
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
|
zap.String("order_no", purchaseOrder.OrderNo),
|
||||||
|
zap.String("download_id", download.ID),
|
||||||
|
zap.String("product_id", download.ProductID),
|
||||||
|
zap.String("user_id", download.UserID))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// calculateExpiryTime 计算下载有效期(从创建日起30天)
|
// calculateExpiryTime 计算下载有效期(从创建日起30天)
|
||||||
func calculateExpiryTime() *time.Time {
|
func calculateExpiryTime() *time.Time {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -944,18 +1027,33 @@ func (s *ComponentReportOrderService) queryAlipayOrderStatusAndUpdate(ctx contex
|
|||||||
zap.String("purchase_order_id", purchaseOrderID),
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 查询支付宝订单 - 使用RechargeID字段关联
|
// 首先获取购买订单信息
|
||||||
alipayOrder, err := s.alipayOrderRepo.GetByRechargeID(ctx, purchaseOrderID)
|
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrderID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("查找购买订单失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
|
zap.Error(err))
|
||||||
|
return fmt.Errorf("查找购买订单失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if purchaseOrder == nil {
|
||||||
|
s.logger.Error("购买订单不存在",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID))
|
||||||
|
return fmt.Errorf("购买订单不存在")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用购买订单ID查询支付宝订单(RechargeID字段存储的是购买订单ID)
|
||||||
|
alipayOrder, err := s.alipayOrderRepo.GetByRechargeID(ctx, purchaseOrder.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("查找支付宝订单失败",
|
s.logger.Error("查找支付宝订单失败",
|
||||||
zap.String("purchase_order_id", purchaseOrderID),
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
return fmt.Errorf("查找支付宝订单失败: %w", err)
|
return fmt.Errorf("查找支付宝订单失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if alipayOrder == nil {
|
if alipayOrder == nil {
|
||||||
s.logger.Error("支付宝订单不存在",
|
s.logger.Error("支付宝订单不存在",
|
||||||
zap.String("purchase_order_id", purchaseOrderID))
|
zap.String("purchase_order_id", purchaseOrder.ID))
|
||||||
return fmt.Errorf("支付宝订单不存在")
|
return fmt.Errorf("支付宝订单不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1044,6 +1142,17 @@ func (s *ComponentReportOrderService) queryAlipayOrderStatusAndUpdate(ctx contex
|
|||||||
zap.String("order_no", purchaseOrder.OrderNo),
|
zap.String("order_no", purchaseOrder.OrderNo),
|
||||||
zap.String("status", "paid"),
|
zap.String("status", "paid"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 支付成功后,自动创建下载记录
|
||||||
|
err = s.createDownloadRecordForPaidOrder(ctx, purchaseOrder)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("创建下载记录失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
|
zap.Error(err))
|
||||||
|
} else {
|
||||||
|
s.logger.Info("自动创建下载记录成功",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if alipayStatus == "TRADE_CLOSED" {
|
} else if alipayStatus == "TRADE_CLOSED" {
|
||||||
@@ -1086,18 +1195,33 @@ func (s *ComponentReportOrderService) queryWechatOrderStatusAndUpdate(ctx contex
|
|||||||
zap.String("purchase_order_id", purchaseOrderID),
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 查询微信订单 - 使用RechargeID字段关联
|
// 首先获取购买订单信息
|
||||||
wechatOrder, err := s.wechatOrderRepo.GetByRechargeID(ctx, purchaseOrderID)
|
purchaseOrder, err := s.purchaseOrderRepo.GetByID(ctx, purchaseOrderID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("查找购买订单失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
|
zap.Error(err))
|
||||||
|
return fmt.Errorf("查找购买订单失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if purchaseOrder == nil {
|
||||||
|
s.logger.Error("购买订单不存在",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID))
|
||||||
|
return fmt.Errorf("购买订单不存在")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用购买订单ID查询微信订单(RechargeID字段存储的是购买订单ID)
|
||||||
|
wechatOrder, err := s.wechatOrderRepo.GetByRechargeID(ctx, purchaseOrder.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("查找微信订单失败",
|
s.logger.Error("查找微信订单失败",
|
||||||
zap.String("purchase_order_id", purchaseOrderID),
|
zap.String("purchase_order_id", purchaseOrder.ID),
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
return fmt.Errorf("查找微信订单失败: %w", err)
|
return fmt.Errorf("查找微信订单失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wechatOrder == nil {
|
if wechatOrder == nil {
|
||||||
s.logger.Error("微信订单不存在",
|
s.logger.Error("微信订单不存在",
|
||||||
zap.String("purchase_order_id", purchaseOrderID))
|
zap.String("purchase_order_id", purchaseOrder.ID))
|
||||||
return fmt.Errorf("微信订单不存在")
|
return fmt.Errorf("微信订单不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1193,6 +1317,17 @@ func (s *ComponentReportOrderService) queryWechatOrderStatusAndUpdate(ctx contex
|
|||||||
zap.String("order_no", purchaseOrder.OrderNo),
|
zap.String("order_no", purchaseOrder.OrderNo),
|
||||||
zap.String("status", "paid"),
|
zap.String("status", "paid"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 支付成功后,自动创建下载记录
|
||||||
|
err = s.createDownloadRecordForPaidOrder(ctx, purchaseOrder)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("创建下载记录失败",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID),
|
||||||
|
zap.Error(err))
|
||||||
|
} else {
|
||||||
|
s.logger.Info("自动创建下载记录成功",
|
||||||
|
zap.String("purchase_order_id", purchaseOrderID))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if tradeState == "CLOSED" || tradeState == "REVOKED" {
|
} else if tradeState == "CLOSED" || tradeState == "REVOKED" {
|
||||||
|
|||||||
@@ -250,6 +250,10 @@ type QCXG7A2BReq struct {
|
|||||||
IDCard string `json:"id_card" validate:"required,validIDCard"`
|
IDCard string `json:"id_card" validate:"required,validIDCard"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QCXG4896Req struct {
|
||||||
|
PlateNo string `json:"plate_no" validate:"required"`
|
||||||
|
AuthDate string `json:"auth_date" validate:"required,validAuthDate" encrypt:"false"`
|
||||||
|
}
|
||||||
type COMENT01Req struct {
|
type COMENT01Req struct {
|
||||||
EntName string `json:"ent_name" validate:"required,min=1,validEnterpriseName"`
|
EntName string `json:"ent_name" validate:"required,min=1,validEnterpriseName"`
|
||||||
EntCode string `json:"ent_code" validate:"required,validUSCI"`
|
EntCode string `json:"ent_code" validate:"required,validUSCI"`
|
||||||
|
|||||||
@@ -217,6 +217,7 @@ func registerAllProcessors(combService *comb.CombService) {
|
|||||||
"QCXG9P1C": qcxg.ProcessQCXG9P1CRequest,
|
"QCXG9P1C": qcxg.ProcessQCXG9P1CRequest,
|
||||||
"QCXG8A3D": qcxg.ProcessQCXG8A3DRequest,
|
"QCXG8A3D": qcxg.ProcessQCXG8A3DRequest,
|
||||||
"QCXG6B4E": qcxg.ProcessQCXG6B4ERequest,
|
"QCXG6B4E": qcxg.ProcessQCXG6B4ERequest,
|
||||||
|
"QCXG4896": qcxg.ProcessQCXG4896Request,
|
||||||
|
|
||||||
// DWBG系列处理器 - 多维报告
|
// DWBG系列处理器 - 多维报告
|
||||||
"DWBG6A2C": dwbg.ProcessDWBG6A2CRequest,
|
"DWBG6A2C": dwbg.ProcessDWBG6A2CRequest,
|
||||||
|
|||||||
@@ -199,6 +199,7 @@ func (s *FormConfigServiceImpl) getDTOStruct(ctx context.Context, apiCode string
|
|||||||
"IVYZ5A9O": &dto.IVYZ5A9OReq{}, //全国⾃然⼈⻛险评估评分模型
|
"IVYZ5A9O": &dto.IVYZ5A9OReq{}, //全国⾃然⼈⻛险评估评分模型
|
||||||
"IVYZ6M8P": &dto.IVYZ6M8PReq{}, //职业资格证书
|
"IVYZ6M8P": &dto.IVYZ6M8PReq{}, //职业资格证书
|
||||||
"QYGL5CMP": &dto.QYGL5CMPReq{}, //企业五要素验证
|
"QYGL5CMP": &dto.QYGL5CMPReq{}, //企业五要素验证
|
||||||
|
"QCXG4896": &dto.QCXG4896Req{}, //网约车风险查询
|
||||||
}
|
}
|
||||||
|
|
||||||
// 优先返回已配置的DTO
|
// 优先返回已配置的DTO
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package qcxg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"tyapi-server/internal/domains/api/dto"
|
||||||
|
"tyapi-server/internal/domains/api/services/processors"
|
||||||
|
"tyapi-server/internal/infrastructure/external/muzi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProcessQCXG4896MRequest QCXG4896 API处理方法 - 网约车风险查询
|
||||||
|
func ProcessQCXG4896Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
|
||||||
|
var paramsDto dto.QCXG4896Req
|
||||||
|
if err := json.Unmarshal(params, ¶msDto); err != nil {
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
|
||||||
|
return nil, errors.Join(processors.ErrInvalidParam, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
reqData := map[string]interface{}{
|
||||||
|
"paramName": "licenseNo",
|
||||||
|
"paramValue": paramsDto.PlateNo,
|
||||||
|
"startTime": strings.Split(paramsDto.AuthDate, "-")[0],
|
||||||
|
"endTime": strings.Split(paramsDto.AuthDate, "-")[1],
|
||||||
|
}
|
||||||
|
|
||||||
|
respData, err := deps.MuziService.CallAPI(ctx, "PC0031", reqData)
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, muzi.ErrDatasource):
|
||||||
|
return nil, errors.Join(processors.ErrDatasource, err)
|
||||||
|
case errors.Is(err, muzi.ErrSystem):
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
|
default:
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return respData, nil
|
||||||
|
}
|
||||||
@@ -14,7 +14,6 @@ type ComponentReportDownload struct {
|
|||||||
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
|
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
|
||||||
ProductCode string `gorm:"type:varchar(50);not null;index" comment:"产品编号"`
|
ProductCode string `gorm:"type:varchar(50);not null;index" comment:"产品编号"`
|
||||||
ProductName string `gorm:"type:varchar(200);not null" comment:"产品名称"`
|
ProductName string `gorm:"type:varchar(200);not null" comment:"产品名称"`
|
||||||
|
|
||||||
// 直接关联购买订单
|
// 直接关联购买订单
|
||||||
OrderID *string `gorm:"type:varchar(36);index" comment:"关联的购买订单ID"`
|
OrderID *string `gorm:"type:varchar(36);index" comment:"关联的购买订单ID"`
|
||||||
OrderNumber *string `gorm:"type:varchar(64);index" comment:"关联的购买订单号"`
|
OrderNumber *string `gorm:"type:varchar(64);index" comment:"关联的购买订单号"`
|
||||||
|
|||||||
@@ -289,41 +289,70 @@ func (h *ComponentReportOrderHandler) CheckPaymentStatus(c *gin.Context) {
|
|||||||
// DownloadFile 下载文件
|
// DownloadFile 下载文件
|
||||||
// GET /api/v1/component-report/download/:orderId
|
// GET /api/v1/component-report/download/:orderId
|
||||||
func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
|
func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
|
||||||
|
h.logger.Info("开始处理文件下载请求")
|
||||||
|
|
||||||
userID := c.GetString("user_id")
|
userID := c.GetString("user_id")
|
||||||
if userID == "" {
|
if userID == "" {
|
||||||
|
h.logger.Error("用户未登录")
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
"code": 401,
|
"code": 401,
|
||||||
"message": "用户未登录",
|
"message": "用户未登录",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.logger.Info("获取用户ID", zap.String("user_id", userID))
|
||||||
|
|
||||||
orderID := c.Param("orderId")
|
orderID := c.Param("orderId")
|
||||||
if orderID == "" {
|
if orderID == "" {
|
||||||
|
h.logger.Error("订单ID不能为空")
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"code": 400,
|
"code": 400,
|
||||||
"message": "订单ID不能为空",
|
"message": "订单ID不能为空",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.logger.Info("获取订单ID", zap.String("order_id", orderID))
|
||||||
|
|
||||||
filePath, err := h.service.DownloadFile(c.Request.Context(), orderID)
|
filePath, err := h.service.DownloadFile(c.Request.Context(), orderID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logger.Error("下载文件失败", zap.Error(err), zap.String("order_id", orderID))
|
h.logger.Error("下载文件失败", zap.Error(err), zap.String("order_id", orderID), zap.String("user_id", userID))
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
|
||||||
"code": 500,
|
// 根据错误类型返回不同的状态码和消息
|
||||||
|
errorMessage := err.Error()
|
||||||
|
statusCode := http.StatusInternalServerError
|
||||||
|
|
||||||
|
// 根据错误消息判断具体错误类型
|
||||||
|
if strings.Contains(errorMessage, "购买订单不存在") {
|
||||||
|
statusCode = http.StatusNotFound
|
||||||
|
} else if strings.Contains(errorMessage, "订单未支付") || strings.Contains(errorMessage, "已过期") {
|
||||||
|
statusCode = http.StatusForbidden
|
||||||
|
} else if strings.Contains(errorMessage, "生成报告文件失败") {
|
||||||
|
statusCode = http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(statusCode, gin.H{
|
||||||
|
"code": statusCode,
|
||||||
"message": "下载文件失败",
|
"message": "下载文件失败",
|
||||||
"error": err.Error(),
|
"error": errorMessage,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.logger.Info("成功获取文件路径",
|
||||||
|
zap.String("order_id", orderID),
|
||||||
|
zap.String("user_id", userID),
|
||||||
|
zap.String("file_path", filePath))
|
||||||
|
|
||||||
// 设置响应头
|
// 设置响应头
|
||||||
c.Header("Content-Type", "application/zip")
|
c.Header("Content-Type", "application/zip")
|
||||||
c.Header("Content-Disposition", "attachment; filename=component_report.zip")
|
c.Header("Content-Disposition", "attachment; filename=component_report.zip")
|
||||||
|
|
||||||
// 发送文件
|
// 发送文件
|
||||||
|
h.logger.Info("开始发送文件", zap.String("file_path", filePath))
|
||||||
c.File(filePath)
|
c.File(filePath)
|
||||||
|
h.logger.Info("文件发送成功", zap.String("file_path", filePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserOrders 获取用户订单列表
|
// GetUserOrders 获取用户订单列表
|
||||||
|
|||||||
Reference in New Issue
Block a user