173 lines
5.8 KiB
Go
173 lines
5.8 KiB
Go
|
|
package component_report
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"go.uber.org/zap"
|
||
|
|
|
||
|
|
finance_entities "tyapi-server/internal/domains/finance/entities"
|
||
|
|
financeRepositories "tyapi-server/internal/domains/finance/repositories"
|
||
|
|
"tyapi-server/internal/domains/product/repositories"
|
||
|
|
"tyapi-server/internal/shared/payment"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ComponentReportHandler 组件报告处理器
|
||
|
|
type ComponentReportHandlerFixed struct {
|
||
|
|
exampleJSONGenerator *ExampleJSONGenerator
|
||
|
|
zipGenerator *ZipGenerator
|
||
|
|
productRepo repositories.ProductRepository
|
||
|
|
componentReportRepo repositories.ComponentReportRepository
|
||
|
|
purchaseOrderRepo financeRepositories.PurchaseOrderRepository
|
||
|
|
rechargeRecordRepo interface {
|
||
|
|
Create(ctx context.Context, record finance_entities.RechargeRecord) (finance_entities.RechargeRecord, error)
|
||
|
|
}
|
||
|
|
alipayOrderRepo interface {
|
||
|
|
Create(ctx context.Context, order finance_entities.AlipayOrder) (finance_entities.AlipayOrder, error)
|
||
|
|
GetByOutTradeNo(ctx context.Context, outTradeNo string) (*finance_entities.AlipayOrder, error)
|
||
|
|
Update(ctx context.Context, order finance_entities.AlipayOrder) error
|
||
|
|
}
|
||
|
|
wechatOrderRepo interface {
|
||
|
|
Create(ctx context.Context, order finance_entities.WechatOrder) (finance_entities.WechatOrder, error)
|
||
|
|
GetByOutTradeNo(ctx context.Context, outTradeNo string) (*finance_entities.WechatOrder, error)
|
||
|
|
Update(ctx context.Context, order finance_entities.WechatOrder) error
|
||
|
|
}
|
||
|
|
aliPayService *payment.AliPayService
|
||
|
|
wechatPayService *payment.WechatPayService
|
||
|
|
logger *zap.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewComponentReportHandlerFixed 创建组件报告处理器(修复版)
|
||
|
|
func NewComponentReportHandlerFixed(
|
||
|
|
productRepo repositories.ProductRepository,
|
||
|
|
docRepo repositories.ProductDocumentationRepository,
|
||
|
|
apiConfigRepo repositories.ProductApiConfigRepository,
|
||
|
|
componentReportRepo repositories.ComponentReportRepository,
|
||
|
|
purchaseOrderRepo financeRepositories.PurchaseOrderRepository,
|
||
|
|
rechargeRecordRepo interface {
|
||
|
|
Create(ctx context.Context, record finance_entities.RechargeRecord) (finance_entities.RechargeRecord, error)
|
||
|
|
},
|
||
|
|
alipayOrderRepo interface {
|
||
|
|
Create(ctx context.Context, order finance_entities.AlipayOrder) (finance_entities.AlipayOrder, error)
|
||
|
|
GetByOutTradeNo(ctx context.Context, outTradeNo string) (*finance_entities.AlipayOrder, error)
|
||
|
|
Update(ctx context.Context, order finance_entities.AlipayOrder) error
|
||
|
|
},
|
||
|
|
wechatOrderRepo interface {
|
||
|
|
Create(ctx context.Context, order finance_entities.WechatOrder) (finance_entities.WechatOrder, error)
|
||
|
|
GetByOutTradeNo(ctx context.Context, outTradeNo string) (*finance_entities.WechatOrder, error)
|
||
|
|
Update(ctx context.Context, order finance_entities.WechatOrder) error
|
||
|
|
},
|
||
|
|
aliPayService *payment.AliPayService,
|
||
|
|
wechatPayService *payment.WechatPayService,
|
||
|
|
logger *zap.Logger,
|
||
|
|
) *ComponentReportHandlerFixed {
|
||
|
|
exampleJSONGenerator := NewExampleJSONGenerator(productRepo, docRepo, apiConfigRepo, logger)
|
||
|
|
zipGenerator := NewZipGenerator(logger)
|
||
|
|
|
||
|
|
return &ComponentReportHandlerFixed{
|
||
|
|
exampleJSONGenerator: exampleJSONGenerator,
|
||
|
|
zipGenerator: zipGenerator,
|
||
|
|
productRepo: productRepo,
|
||
|
|
componentReportRepo: componentReportRepo,
|
||
|
|
purchaseOrderRepo: purchaseOrderRepo,
|
||
|
|
rechargeRecordRepo: rechargeRecordRepo,
|
||
|
|
alipayOrderRepo: alipayOrderRepo,
|
||
|
|
wechatOrderRepo: wechatOrderRepo,
|
||
|
|
aliPayService: aliPayService,
|
||
|
|
wechatPayService: wechatPayService,
|
||
|
|
logger: logger,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckPaymentStatusFixed 检查支付状态(修复版)
|
||
|
|
func (h *ComponentReportHandlerFixed) 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
|
||
|
|
|
||
|
|
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
|
||
|
|
} else {
|
||
|
|
paymentStatus = "pending"
|
||
|
|
canDownload = false
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查是否过期
|
||
|
|
if download.IsExpired() {
|
||
|
|
canDownload = false
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, CheckPaymentStatusResponse{
|
||
|
|
OrderID: download.ID,
|
||
|
|
PaymentStatus: paymentStatus,
|
||
|
|
CanDownload: canDownload,
|
||
|
|
})
|
||
|
|
}
|