All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m6s
65 lines
2.6 KiB
Go
65 lines
2.6 KiB
Go
// Package agentrecharge 提供代理充值本地状态只读投影。
|
|
package agentrecharge
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
|
)
|
|
|
|
// PaymentStatusQuery 查询代理充值本地支付与到账事实。
|
|
type PaymentStatusQuery struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewPaymentStatusQuery 创建代理充值支付状态 Query。
|
|
func NewPaymentStatusQuery(db *gorm.DB) *PaymentStatusQuery {
|
|
return &PaymentStatusQuery{db: db}
|
|
}
|
|
|
|
// Get 读取当前数据范围内的充值单和支付单,不调用第三方渠道。
|
|
func (q *PaymentStatusQuery) Get(ctx context.Context, rechargeID uint) (*dto.AgentRechargePaymentStatusResponse, error) {
|
|
if q == nil || q.db == nil || rechargeID == 0 {
|
|
return nil, errors.New(errors.CodeInvalidParam, "代理充值支付状态查询参数无效")
|
|
}
|
|
var recharge model.AgentRechargeRecord
|
|
rechargeQuery := middleware.ApplyShopFilter(ctx, q.db.WithContext(ctx).Model(&model.AgentRechargeRecord{}))
|
|
if err := rechargeQuery.Where("id = ?", rechargeID).First(&recharge).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeForbidden, "无权限操作该资源或资源不存在")
|
|
}
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询代理充值状态失败")
|
|
}
|
|
var payment model.Payment
|
|
if err := q.db.WithContext(ctx).
|
|
Where("order_id = ? AND order_type = ?", recharge.ID, model.PaymentOrderTypeAgentRecharge).
|
|
First(&payment).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeForbidden, "无权限操作该资源或资源不存在")
|
|
}
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询代理充值支付状态失败")
|
|
}
|
|
source, sourceName := constants.GetAgentRechargeSource(recharge.PaymentMethod)
|
|
result := &dto.AgentRechargePaymentStatusResponse{
|
|
RechargeID: recharge.ID, RechargeNo: recharge.RechargeNo,
|
|
RechargeSource: source, RechargeSourceName: sourceName,
|
|
Status: recharge.Status, StatusName: constants.GetRechargeStatusName(recharge.Status),
|
|
PaymentStatus: payment.Status, PaymentStatusName: constants.GetPaymentRecordStatusName(payment.Status),
|
|
}
|
|
if payment.PaidAt != nil {
|
|
paidAt := payment.PaidAt.Format("2006-01-02 15:04:05")
|
|
result.PaidAt = &paidAt
|
|
}
|
|
if recharge.CompletedAt != nil {
|
|
completedAt := recharge.CompletedAt.Format("2006-01-02 15:04:05")
|
|
result.CompletedAt = &completedAt
|
|
}
|
|
return result, nil
|
|
}
|