You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.2 KiB
Go
141 lines
3.2 KiB
Go
package order
|
|
|
|
import (
|
|
"cls/internal/domain/coupon"
|
|
"cls/internal/domain/payment"
|
|
"cls/internal/domain/purchase"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// OrderAggregate 订单聚合根
|
|
type OrderAggregate struct {
|
|
Order *Order
|
|
Payment *payment.Payment
|
|
Purchase *purchase.Purchase
|
|
Coupon *coupon.Coupon
|
|
}
|
|
|
|
// NewOrderAggregate 创建订单聚合根
|
|
func NewOrderAggregate(order *Order) *OrderAggregate {
|
|
return &OrderAggregate{
|
|
Order: order,
|
|
}
|
|
}
|
|
|
|
// CreatePayment 创建支付订单
|
|
func (a *OrderAggregate) CreatePayment(paymentType payment.PaymentType) error {
|
|
if a.Order.Status != OrderStatusPending {
|
|
return payment.ErrInvalidTransactionID
|
|
}
|
|
|
|
a.Payment = payment.NewPayment(
|
|
a.Order.OrderNo,
|
|
a.Order.UserID,
|
|
a.Order.TargetID,
|
|
paymentType,
|
|
a.Order.Amount,
|
|
a.Order.Description,
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// HandlePaymentSuccess 处理支付成功
|
|
func (a *OrderAggregate) HandlePaymentSuccess(transactionID string, notifyData string) error {
|
|
if a.Payment == nil {
|
|
return payment.ErrPaymentNotFound
|
|
}
|
|
|
|
if a.Payment.IsSuccess() {
|
|
return payment.ErrPaymentSuccess
|
|
}
|
|
|
|
// 更新支付状态
|
|
a.Payment.Status = payment.PaymentStatusSuccess
|
|
a.Payment.TransactionID = transactionID
|
|
a.Payment.NotifyData = notifyData
|
|
|
|
// 更新订单状态
|
|
a.Order.Status = OrderStatusPaid
|
|
|
|
// 创建购买记录
|
|
a.Purchase = &purchase.Purchase{
|
|
UserId: a.Order.UserID,
|
|
ContentId: a.Order.TargetID,
|
|
ContentType: purchase.ContentType(a.Order.Type),
|
|
Price: float64(a.Order.Amount) / 100, // 转换为元
|
|
OrderNo: a.Order.OrderNo,
|
|
Duration: a.Order.Duration,
|
|
ContentSource: purchase.ContentSourceBuy,
|
|
ExpiredAt: time.Now().AddDate(0, a.Order.Duration, 0),
|
|
Status: 1, // 有效状态
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// HandlePaymentFailed 处理支付失败
|
|
func (a *OrderAggregate) HandlePaymentFailed(notifyData string) error {
|
|
if a.Payment == nil {
|
|
return payment.ErrPaymentNotFound
|
|
}
|
|
|
|
if a.Payment.IsFailed() {
|
|
return payment.ErrPaymentFailed
|
|
}
|
|
|
|
// 更新支付状态
|
|
a.Payment.Status = payment.PaymentStatusFailed
|
|
a.Payment.NotifyData = notifyData
|
|
|
|
// 更新订单状态
|
|
a.Order.Status = OrderStatusCanceled
|
|
if a.Coupon != nil {
|
|
if a.Coupon.ID > 0 {
|
|
a.Coupon.Status = coupon.CouponStatusNormal
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// HandleRefund 处理退款
|
|
func (a *OrderAggregate) HandleRefund() error {
|
|
if a.Payment == nil {
|
|
return payment.ErrPaymentNotFound
|
|
}
|
|
|
|
if !a.Payment.IsSuccess() {
|
|
return errors.New("支付订单未支付成功")
|
|
}
|
|
|
|
// 更新支付状态
|
|
a.Payment.Status = payment.PaymentStatusRefunded
|
|
|
|
// 更新订单状态
|
|
a.Order.Status = OrderStatusRefunded
|
|
|
|
// 更新购买记录状态
|
|
if a.Purchase != nil {
|
|
a.Purchase.Status = 0 // 失效状态
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsValid 检查聚合根是否有效
|
|
func (a *OrderAggregate) IsValid() bool {
|
|
return a.Order != nil && a.Order.IsValid()
|
|
}
|
|
|
|
// IsPaid 检查是否已支付
|
|
func (a *OrderAggregate) IsPaid() bool {
|
|
return a.Order != nil && a.Order.IsPaid() && a.Payment != nil && a.Payment.IsSuccess()
|
|
}
|
|
|
|
// IsRefunded 检查是否已退款
|
|
func (a *OrderAggregate) IsRefunded() bool {
|
|
return a.Order != nil && a.Order.IsRefunded() && a.Payment != nil && a.Payment.IsRefunded()
|
|
}
|