585133532c
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/wangjia/pay/internal/model"
|
|
"github.com/wangjia/pay/internal/money"
|
|
)
|
|
|
|
// DBProductResolver 按 biz_code 解析套餐,金额取给定结算币种的权威价。
|
|
// 优先查 ProductPrice(分币种 int64 价);该币种无行且币种=CNY 时回退 Product.Price(v1 元 string)。
|
|
type DBProductResolver struct{ db *gorm.DB }
|
|
|
|
func NewDBProductResolver(db *gorm.DB) *DBProductResolver { return &DBProductResolver{db: db} }
|
|
|
|
func (r *DBProductResolver) Resolve(sku, currency string) (int64, string, string, error) {
|
|
var p model.Product
|
|
err := r.db.Where("biz_code = ? AND active = ?", sku, true).First(&p).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return 0, "", "", ErrProductNotFound
|
|
}
|
|
if err != nil {
|
|
return 0, "", "", err
|
|
}
|
|
// 1) 分币种权威价
|
|
var pp model.ProductPrice
|
|
err = r.db.Where("product_id = ? AND currency = ?", p.ID, currency).First(&pp).Error
|
|
if err == nil {
|
|
return pp.AmountMinor, p.Name, p.BizCode, nil
|
|
}
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return 0, "", "", err
|
|
}
|
|
// 2) 回退:仅 CNY 用 v1 Product.Price(元 string)
|
|
if currency == "CNY" && p.Price != "" {
|
|
minor, perr := money.Parse(p.Price, "CNY")
|
|
if perr != nil {
|
|
return 0, "", "", perr
|
|
}
|
|
return minor, p.Name, p.BizCode, nil
|
|
}
|
|
// 该套餐不支持此结算币种
|
|
return 0, "", "", ErrProductNotFound
|
|
}
|