116 lines
4.1 KiB
Go
116 lines
4.1 KiB
Go
package repository
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"gorm.io/gorm"
|
||
"pay-bridge/internal/model"
|
||
)
|
||
|
||
// MerchantRepository 商户数据访问
|
||
type MerchantRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewMerchantRepository(db *gorm.DB) *MerchantRepository {
|
||
return &MerchantRepository{db: db}
|
||
}
|
||
|
||
func (r *MerchantRepository) Create(ctx context.Context, m *model.Merchant) error {
|
||
return r.db.WithContext(ctx).Create(m).Error
|
||
}
|
||
|
||
func (r *MerchantRepository) GetByMerchantID(ctx context.Context, merchantID string) (*model.Merchant, error) {
|
||
var m model.Merchant
|
||
err := r.db.WithContext(ctx).Where("merchant_id = ?", merchantID).First(&m).Error
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, nil
|
||
}
|
||
return &m, err
|
||
}
|
||
|
||
func (r *MerchantRepository) UpdateStatus(ctx context.Context, merchantID string, status model.MerchantStatus, updates map[string]any) error {
|
||
if updates == nil {
|
||
updates = make(map[string]any)
|
||
}
|
||
updates["status"] = status
|
||
return r.db.WithContext(ctx).Model(&model.Merchant{}).Where("merchant_id = ?", merchantID).Updates(updates).Error
|
||
}
|
||
|
||
func (r *MerchantRepository) List(ctx context.Context, status model.MerchantStatus, limit, offset int) ([]*model.Merchant, error) {
|
||
var merchants []*model.Merchant
|
||
q := r.db.WithContext(ctx)
|
||
if status != "" {
|
||
q = q.Where("status = ?", status)
|
||
}
|
||
err := q.Order("created_at DESC").Limit(limit).Offset(offset).Find(&merchants).Error
|
||
return merchants, err
|
||
}
|
||
|
||
// ListAnomalous 查询状态异常的商户(Frozen/Rejected)
|
||
func (r *MerchantRepository) ListAnomalous(ctx context.Context) ([]*model.Merchant, error) {
|
||
var merchants []*model.Merchant
|
||
err := r.db.WithContext(ctx).
|
||
Where("status IN ?", []model.MerchantStatus{
|
||
model.MerchantStatusFrozen,
|
||
model.MerchantStatusRejected,
|
||
}).Find(&merchants).Error
|
||
return merchants, err
|
||
}
|
||
|
||
// GetByMerchantIDAndAppID 带 appID 隔离查询(业务侧用)
|
||
func (r *MerchantRepository) GetByMerchantIDAndAppID(ctx context.Context, merchantID, appID string) (*model.Merchant, error) {
|
||
var m model.Merchant
|
||
err := r.db.WithContext(ctx).Where("merchant_id = ? AND app_id = ?", merchantID, appID).First(&m).Error
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, nil
|
||
}
|
||
return &m, err
|
||
}
|
||
|
||
// ListByAppID 按 appID 分页查询(业务侧用)
|
||
func (r *MerchantRepository) ListByAppID(ctx context.Context, appID string, status model.MerchantStatus, limit, offset int) ([]*model.Merchant, error) {
|
||
var merchants []*model.Merchant
|
||
q := r.db.WithContext(ctx).Where("app_id = ?", appID)
|
||
if status != "" {
|
||
q = q.Where("status = ?", status)
|
||
}
|
||
err := q.Limit(limit).Offset(offset).Order("id DESC").Find(&merchants).Error
|
||
return merchants, err
|
||
}
|
||
|
||
// CreateApplication 创建进件申请
|
||
func (r *MerchantRepository) CreateApplication(ctx context.Context, app *model.MerchantApplication) error {
|
||
return r.db.WithContext(ctx).Create(app).Error
|
||
}
|
||
|
||
// GetLatestApplication 获取商户最新进件申请
|
||
func (r *MerchantRepository) GetLatestApplication(ctx context.Context, merchantID string) (*model.MerchantApplication, error) {
|
||
var app model.MerchantApplication
|
||
err := r.db.WithContext(ctx).Where("merchant_id = ?", merchantID).
|
||
Order("created_at DESC").First(&app).Error
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, nil
|
||
}
|
||
return &app, err
|
||
}
|
||
|
||
// GetApprovedApplicationByChannel 查询指定商户在指定渠道已审核通过的进件记录
|
||
func (r *MerchantRepository) GetApprovedApplicationByChannel(ctx context.Context, merchantID, channelCode string) (*model.MerchantApplication, error) {
|
||
var app model.MerchantApplication
|
||
err := r.db.WithContext(ctx).
|
||
Where("merchant_id = ? AND channel_code = ? AND audit_status = ?", merchantID, channelCode, model.AuditStatusApproved).
|
||
First(&app).Error
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil, nil
|
||
}
|
||
return &app, err
|
||
}
|
||
|
||
// UpdateApplication 更新进件申请状态
|
||
func (r *MerchantRepository) UpdateApplication(ctx context.Context, applicationID string, updates map[string]any) error {
|
||
return r.db.WithContext(ctx).Model(&model.MerchantApplication{}).
|
||
Where("application_id = ?", applicationID).Updates(updates).Error
|
||
}
|