Files
pay-bridge/backend/internal/repository/profit_sharing.go
2026-03-13 15:51:59 +08:00

91 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"pay-bridge/internal/model"
)
// ProfitSharingRepository 分润数据访问
type ProfitSharingRepository struct {
db *gorm.DB
}
func NewProfitSharingRepository(db *gorm.DB) *ProfitSharingRepository {
return &ProfitSharingRepository{db: db}
}
// GetConfigByAppID 按 app_id 获取分润配置
func (r *ProfitSharingRepository) GetConfigByAppID(ctx context.Context, appID string) (*model.ProfitSharingConfig, error) {
var cfg model.ProfitSharingConfig
err := r.db.WithContext(ctx).Where("app_id = ? AND status = 1", appID).First(&cfg).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &cfg, err
}
// SaveConfig 创建或更新分润配置
func (r *ProfitSharingRepository) SaveConfig(ctx context.Context, cfg *model.ProfitSharingConfig) error {
return r.db.WithContext(ctx).Save(cfg).Error
}
// CreateOrder 创建分润记录
func (r *ProfitSharingRepository) CreateOrder(ctx context.Context, order *model.ProfitSharingOrder) error {
return r.db.WithContext(ctx).Create(order).Error
}
// GetOrderByTradeNo 按 trade_no 查询分润记录
func (r *ProfitSharingRepository) GetOrderByTradeNo(ctx context.Context, tradeNo string) (*model.ProfitSharingOrder, error) {
var order model.ProfitSharingOrder
err := r.db.WithContext(ctx).Where("trade_no = ?", tradeNo).First(&order).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &order, err
}
// GetOrderBySharingNo 按 sharing_no 查询
func (r *ProfitSharingRepository) GetOrderBySharingNo(ctx context.Context, sharingNo string) (*model.ProfitSharingOrder, error) {
var order model.ProfitSharingOrder
err := r.db.WithContext(ctx).Where("sharing_no = ?", sharingNo).First(&order).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &order, err
}
// UpdateOrderStatus 更新分润状态
func (r *ProfitSharingRepository) UpdateOrderStatus(ctx context.Context, sharingNo string, fromStatus, toStatus model.ProfitSharingStatus, updates map[string]any) (bool, error) {
if updates == nil {
updates = make(map[string]any)
}
updates["status"] = toStatus
result := r.db.WithContext(ctx).Model(&model.ProfitSharingOrder{}).
Where("sharing_no = ? AND status = ?", sharingNo, fromStatus).
Updates(updates)
if result.Error != nil {
return false, result.Error
}
return result.RowsAffected > 0, nil
}
// CreateLog 记录分润流水
func (r *ProfitSharingRepository) CreateLog(ctx context.Context, log *model.ProfitSharingLog) error {
return r.db.WithContext(ctx).Create(log).Error
}
// ListPendingOrders 查询需要补偿的分润单PROCESSING 超时)
func (r *ProfitSharingRepository) ListPendingOrders(ctx context.Context, limit int) ([]*model.ProfitSharingOrder, error) {
var orders []*model.ProfitSharingOrder
err := r.db.WithContext(ctx).
Where("status IN ?", []model.ProfitSharingStatus{
model.ProfitSharingStatusPending,
model.ProfitSharingStatusProcessing,
}).
Limit(limit).Find(&orders).Error
return orders, err
}