This commit is contained in:
2026-01-16 18:37:47 +08:00
parent 96dfa3d758
commit 96d530a67d
12 changed files with 959 additions and 156 deletions

View File

@@ -712,6 +712,13 @@ func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID
zap.String("product_id", purchaseOrder.ProductID))
// 创建新的下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
newDownload := &productEntities.ComponentReportDownload{
UserID: purchaseOrder.UserID,
ProductID: purchaseOrder.ProductID,
@@ -719,6 +726,7 @@ func (s *ComponentReportOrderService) DownloadFile(ctx context.Context, orderID
ProductName: product.Name,
OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo,
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: purchaseOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(),
}
@@ -847,6 +855,13 @@ func (s *ComponentReportOrderService) createDownloadRecordForPaidOrder(ctx conte
}
// 创建新的下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &productEntities.ComponentReportDownload{
UserID: purchaseOrder.UserID,
ProductID: purchaseOrder.ProductID,
@@ -854,6 +869,7 @@ func (s *ComponentReportOrderService) createDownloadRecordForPaidOrder(ctx conte
ProductName: product.Name,
OrderID: &purchaseOrder.ID,
OrderNumber: &purchaseOrder.OrderNo,
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: purchaseOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), // 30天后过期
}

View File

@@ -1251,9 +1251,11 @@ func NewContainer() *Container {
// 组件报告订单处理器
func(
componentReportOrderService *product.ComponentReportOrderService,
purchaseOrderRepo domain_finance_repo.PurchaseOrderRepository,
config *config.Config,
logger *zap.Logger,
) *handlers.ComponentReportOrderHandler {
return handlers.NewComponentReportOrderHandler(componentReportOrderService, logger)
return handlers.NewComponentReportOrderHandler(componentReportOrderService, purchaseOrderRepo, config, logger)
},
// UI组件HTTP处理器
func(

View File

@@ -24,6 +24,7 @@ type ComponentReportDownload struct {
SubProductCodes string `gorm:"type:text" comment:"子产品编号列表JSON数组"`
// 价格相关字段
OriginalPrice decimal.Decimal `gorm:"type:decimal(10,2);default:0.00" comment:"原始价格组合包使用UIComponentPrice单品使用Price"`
DownloadPrice decimal.Decimal `gorm:"type:decimal(10,2);default:0.00" comment:"实际支付价格"`
// 下载相关信息

View File

@@ -6,25 +6,34 @@ import (
"strings"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"go.uber.org/zap"
"tyapi-server/internal/application/product"
"tyapi-server/internal/config"
financeRepositories "tyapi-server/internal/domains/finance/repositories"
)
// ComponentReportOrderHandler 组件报告订单处理器
type ComponentReportOrderHandler struct {
service *product.ComponentReportOrderService
logger *zap.Logger
service *product.ComponentReportOrderService
purchaseOrderRepo financeRepositories.PurchaseOrderRepository
config *config.Config
logger *zap.Logger
}
// NewComponentReportOrderHandler 创建组件报告订单处理器
func NewComponentReportOrderHandler(
service *product.ComponentReportOrderService,
purchaseOrderRepo financeRepositories.PurchaseOrderRepository,
config *config.Config,
logger *zap.Logger,
) *ComponentReportOrderHandler {
return &ComponentReportOrderHandler{
service: service,
logger: logger,
service: service,
purchaseOrderRepo: purchaseOrderRepo,
config: config,
logger: logger,
}
}
@@ -241,6 +250,40 @@ func (h *ComponentReportOrderHandler) CreatePaymentOrder(c *gin.Context) {
zap.String("pay_url", response.PayURL),
)
// 开发环境下,自动将订单状态设置为已支付
if h.config != nil && h.config.App.IsDevelopment() {
h.logger.Info("开发环境:自动设置订单为已支付状态",
zap.String("order_id", response.OrderID),
zap.String("order_no", response.OrderNo))
// 获取订单信息
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), response.OrderID)
if err != nil {
h.logger.Error("开发环境:获取订单信息失败", zap.Error(err), zap.String("order_id", response.OrderID))
} else {
// 解析金额
amount, err := decimal.NewFromString(response.Amount)
if err != nil {
h.logger.Error("开发环境:解析订单金额失败", zap.Error(err), zap.String("amount", response.Amount))
} else {
// 标记为已支付(使用开发环境的模拟交易号)
tradeNo := "DEV_" + response.OrderNo
purchaseOrder.MarkPaid(tradeNo, "", "", amount, amount)
// 更新订单状态
err = h.purchaseOrderRepo.Update(c.Request.Context(), purchaseOrder)
if err != nil {
h.logger.Error("开发环境:更新订单状态失败", zap.Error(err), zap.String("order_id", response.OrderID))
} else {
h.logger.Info("开发环境:订单状态已自动设置为已支付",
zap.String("order_id", response.OrderID),
zap.String("order_no", response.OrderNo),
zap.String("trade_no", tradeNo))
}
}
}
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"data": response,
@@ -300,7 +343,7 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
})
return
}
h.logger.Info("获取用户ID", zap.String("user_id", userID))
orderID := c.Param("orderId")
@@ -312,17 +355,17 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
})
return
}
h.logger.Info("获取订单ID", zap.String("order_id", orderID))
filePath, err := h.service.DownloadFile(c.Request.Context(), orderID)
if err != nil {
h.logger.Error("下载文件失败", zap.Error(err), zap.String("order_id", orderID), zap.String("user_id", userID))
// 根据错误类型返回不同的状态码和消息
errorMessage := err.Error()
statusCode := http.StatusInternalServerError
// 根据错误消息判断具体错误类型
if strings.Contains(errorMessage, "购买订单不存在") {
statusCode = http.StatusNotFound
@@ -331,7 +374,7 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
} else if strings.Contains(errorMessage, "生成报告文件失败") {
statusCode = http.StatusInternalServerError
}
c.JSON(statusCode, gin.H{
"code": statusCode,
"message": "下载文件失败",
@@ -339,9 +382,9 @@ func (h *ComponentReportOrderHandler) DownloadFile(c *gin.Context) {
})
return
}
h.logger.Info("成功获取文件路径",
zap.String("order_id", orderID),
h.logger.Info("成功获取文件路径",
zap.String("order_id", orderID),
zap.String("user_id", userID),
zap.String("file_path", filePath))

View File

@@ -1,102 +0,0 @@
package component_report
import (
"net/http"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
finance_entities "tyapi-server/internal/domains/finance/entities"
)
// CheckPaymentStatusFixed 修复版检查支付状态方法
func (h *ComponentReportHandler) CheckPaymentStatusFixed(c *gin.Context) {
userID := c.GetString("user_id")
if userID == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": 401,
"message": "用户未登录",
})
return
}
orderID := c.Param("orderId")
if orderID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "订单ID不能为空",
})
return
}
// 根据订单ID查询下载记录
download, err := h.componentReportRepo.GetDownloadByID(c.Request.Context(), orderID)
if err != nil {
h.logger.Error("查询下载记录失败", zap.Error(err), zap.String("order_id", orderID))
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"message": "订单不存在",
})
return
}
// 验证订单是否属于当前用户
if download.UserID != userID {
c.JSON(http.StatusForbidden, gin.H{
"code": 403,
"message": "无权访问此订单",
})
return
}
// 使用购买订单状态来判断支付状态
var paymentStatus string
var canDownload bool
// 优先使用OrderID查询购买订单状态
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("order_id", *download.OrderID))
paymentStatus = "unknown"
} else {
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
} else if download.OrderNumber != nil {
// 兼容旧的支付订单逻辑
paymentStatus = "success" // 简化处理,有支付订单号就认为已支付
canDownload = true
} else {
paymentStatus = "pending"
canDownload = false
}
// 检查是否过期
if download.IsExpired() {
canDownload = false
}
c.JSON(http.StatusOK, CheckPaymentStatusResponse{
OrderID: download.ID,
PaymentStatus: paymentStatus,
CanDownload: canDownload,
})
}

View File

@@ -300,7 +300,7 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
return
}
// 直接检查用户是否有已支付的购买记录,而不是查询下载记录
// 直接检查用户是否有已支付的购买记录
orders, _, err := h.purchaseOrderRepo.GetByUserID(c.Request.Context(), userID, 100, 0)
if err != nil {
h.logger.Error("查询用户购买记录失败", zap.Error(err), zap.String("user_id", userID), zap.String("product_id", req.ProductID))
@@ -384,10 +384,12 @@ func (h *ComponentReportHandler) GenerateAndDownloadZip(c *gin.Context) {
}
// 更新下载次数和最后下载时间
err = h.componentReportRepo.IncrementDownloadCount(c.Request.Context(), download.ID)
if err != nil {
h.logger.Warn("更新下载次数失败", zap.Error(err))
// 不影响下载流程,只记录警告
if download != nil {
err = h.componentReportRepo.IncrementDownloadCount(c.Request.Context(), download.ID)
if err != nil {
h.logger.Warn("更新下载次数失败", zap.Error(err))
// 不影响下载流程,只记录警告
}
}
// 清理过期的缓存(非阻塞方式)
@@ -979,6 +981,13 @@ func (h *ComponentReportHandler) CreatePaymentOrder(c *gin.Context) {
}
// 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: userID,
ProductID: productID,
@@ -989,7 +998,8 @@ func (h *ComponentReportHandler) CreatePaymentOrder(c *gin.Context) {
// 关联购买订单ID
OrderID: &createdPurchaseOrder.ID,
OrderNumber: &outTradeNo,
DownloadPrice: finalPrice, // 设置下载价格
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: finalPrice, // 设置下载价格
}
// 记录创建前的详细信息用于调试
@@ -1384,6 +1394,13 @@ func (h *ComponentReportHandler) createDownloadRecordIfEligible(ctx context.Cont
}
// 3. 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: userID,
ProductID: productID,
@@ -1391,6 +1408,7 @@ func (h *ComponentReportHandler) createDownloadRecordIfEligible(ctx context.Cont
ProductName: product.Name,
OrderID: &validOrder.ID, // 添加OrderID字段
OrderNumber: &validOrder.OrderNo, // 使用OrderNumber字段
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: validOrder.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(), // 从创建日起30天
}
@@ -1563,6 +1581,13 @@ func (h *ComponentReportHandler) createDownloadRecordForPaidOrder(ctx context.Co
}
// 创建下载记录
// 设置原始价格组合包使用UIComponentPrice单品使用Price
var originalPrice decimal.Decimal
if product.IsPackage {
originalPrice = product.UIComponentPrice
} else {
originalPrice = product.Price
}
download := &entities.ComponentReportDownload{
UserID: order.UserID,
ProductID: order.ProductID,
@@ -1570,7 +1595,8 @@ func (h *ComponentReportHandler) createDownloadRecordForPaidOrder(ctx context.Co
ProductName: order.ProductName,
OrderID: &order.ID,
OrderNumber: &order.OrderNo,
DownloadPrice: order.Amount, // 设置下载价格(从订单获取)
OriginalPrice: originalPrice, // 设置原始价格
DownloadPrice: order.Amount, // 设置下载价格(从订单获取)
ExpiresAt: calculateExpiryTime(),
}

View File

@@ -124,39 +124,30 @@ func (h *ComponentReportHandlerFixed) CheckPaymentStatusFixed(c *gin.Context) {
var paymentStatus string
var canDownload bool
if download.OrderID != nil {
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("OrderID", *download.OrderID))
paymentStatus = "unknown"
} else {
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
} else if download.OrderNumber != nil {
// 兼容旧的支付订单逻辑
paymentStatus = "success" // 简化处理,有支付订单号就认为已支付
canDownload = true
// 查询购买订单状态
purchaseOrder, err := h.purchaseOrderRepo.GetByID(c.Request.Context(), *download.OrderID)
if err != nil {
h.logger.Error("查询购买订单失败", zap.Error(err), zap.String("OrderID", *download.OrderID))
paymentStatus = "unknown"
} else {
paymentStatus = "pending"
canDownload = false
// 根据购买订单状态设置支付状态
switch purchaseOrder.Status {
case finance_entities.PurchaseOrderStatusPaid:
paymentStatus = "success"
canDownload = true
case finance_entities.PurchaseOrderStatusCreated:
paymentStatus = "pending"
canDownload = false
case finance_entities.PurchaseOrderStatusCancelled:
paymentStatus = "cancelled"
canDownload = false
case finance_entities.PurchaseOrderStatusFailed:
paymentStatus = "failed"
canDownload = false
default:
paymentStatus = "unknown"
canDownload = false
}
}
// 检查是否过期