All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m32s
- 新增 RechargeOrder 和 Payment 模型及对应 Store - 新增 ClientRechargeOrderHandler 提供充值订单列表/详情接口 - 修改 client_wallet.go 使用新表读写充值数据 - 修改 callback/payment.go 将 CRCH 订单路由到 rechargeOrderService - 修改 client_order/service.go 的强充流程使用新表 - 修改 auto_purchase.go 从 tb_recharge_order 读取 linked_package_ids - 修改 order/service.go 的 WalletPay 使用 tb_payment 记录 - 修改 wechat_config_store.go 从 tb_recharge_order 统计待支付充值数 - 移除 AssetRechargeStore 和 AssetRechargeRecord 的注册引用 - 修复文档生成器缺失 ClientRechargeOrder handler - 状态枚举改为 0-based: Pending=0, Paid=1, Closed=2, Refunded=3
153 lines
4.2 KiB
Go
153 lines
4.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type PaymentStore struct {
|
|
db *gorm.DB
|
|
redis *redis.Client
|
|
}
|
|
|
|
func NewPaymentStore(db *gorm.DB, redis *redis.Client) *PaymentStore {
|
|
return &PaymentStore{
|
|
db: db,
|
|
redis: redis,
|
|
}
|
|
}
|
|
|
|
func (s *PaymentStore) Create(ctx context.Context, payment *model.Payment) error {
|
|
return s.db.WithContext(ctx).Create(payment).Error
|
|
}
|
|
|
|
func (s *PaymentStore) CreateWithTx(ctx context.Context, tx *gorm.DB, payment *model.Payment) error {
|
|
return tx.WithContext(ctx).Create(payment).Error
|
|
}
|
|
|
|
func (s *PaymentStore) GetByPaymentNo(ctx context.Context, paymentNo string) (*model.Payment, error) {
|
|
var payment model.Payment
|
|
err := s.db.WithContext(ctx).
|
|
Where("payment_no = ?", paymentNo).
|
|
First(&payment).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &payment, nil
|
|
}
|
|
|
|
func (s *PaymentStore) GetByID(ctx context.Context, id uint) (*model.Payment, error) {
|
|
var payment model.Payment
|
|
err := s.db.WithContext(ctx).First(&payment, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &payment, nil
|
|
}
|
|
|
|
func (s *PaymentStore) UpdateStatus(ctx context.Context, id uint, status int) error {
|
|
return s.db.WithContext(ctx).
|
|
Model(&model.Payment{}).
|
|
Where("id = ?", id).
|
|
Update("status", status).Error
|
|
}
|
|
|
|
func (s *PaymentStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, status int) error {
|
|
return tx.WithContext(ctx).
|
|
Model(&model.Payment{}).
|
|
Where("id = ?", id).
|
|
Update("status", status).Error
|
|
}
|
|
|
|
func (s *PaymentStore) UpdateStatusWithOptimisticLock(ctx context.Context, id uint, oldStatus *int, newStatus int, paidAt interface{}) error {
|
|
return s.UpdateStatusWithOptimisticLockDB(ctx, s.db, id, oldStatus, newStatus, paidAt)
|
|
}
|
|
|
|
func (s *PaymentStore) UpdateStatusWithOptimisticLockDB(ctx context.Context, db *gorm.DB, id uint, oldStatus *int, newStatus int, paidAt interface{}) error {
|
|
updates := map[string]interface{}{
|
|
"status": newStatus,
|
|
}
|
|
if paidAt != nil {
|
|
updates["paid_at"] = paidAt
|
|
}
|
|
|
|
query := db.WithContext(ctx).Model(&model.Payment{}).Where("id = ?", id)
|
|
|
|
if oldStatus != nil {
|
|
query = query.Where("status = ?", *oldStatus)
|
|
}
|
|
|
|
result := query.Updates(updates)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PaymentStore) UpdatePaymentInfo(ctx context.Context, id uint, thirdPartyTradeNo string, paymentConfigID *uint) error {
|
|
return s.UpdatePaymentInfoWithDB(ctx, s.db, id, thirdPartyTradeNo, paymentConfigID)
|
|
}
|
|
|
|
func (s *PaymentStore) UpdatePaymentInfoWithDB(ctx context.Context, db *gorm.DB, id uint, thirdPartyTradeNo string, paymentConfigID *uint) error {
|
|
updates := map[string]interface{}{}
|
|
if thirdPartyTradeNo != "" {
|
|
updates["third_party_trade_no"] = thirdPartyTradeNo
|
|
}
|
|
if paymentConfigID != nil {
|
|
updates["payment_config_id"] = *paymentConfigID
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
result := db.WithContext(ctx).Model(&model.Payment{}).Where("id = ?", id).Updates(updates)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PaymentStore) ListByOrderID(ctx context.Context, orderID uint, orderType string) ([]*model.Payment, error) {
|
|
var payments []*model.Payment
|
|
err := s.db.WithContext(ctx).
|
|
Where("order_id = ? AND order_type = ?", orderID, orderType).
|
|
Order("created_at DESC").
|
|
Find(&payments).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return payments, nil
|
|
}
|
|
|
|
func (s *PaymentStore) ListByOrderIDAndStatus(ctx context.Context, orderID uint, orderType string, status int) (*model.Payment, error) {
|
|
var payment model.Payment
|
|
err := s.db.WithContext(ctx).
|
|
Where("order_id = ? AND order_type = ? AND status = ?", orderID, orderType, status).
|
|
First(&payment).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &payment, nil
|
|
}
|
|
|
|
func (s *PaymentStore) GetByOrderIDAndStatus(ctx context.Context, orderID uint, status int) (*model.Payment, error) {
|
|
var payment model.Payment
|
|
err := s.db.WithContext(ctx).
|
|
Where("order_id = ? AND status = ?", orderID, status).
|
|
First(&payment).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &payment, nil
|
|
}
|