让分销站能按自身能力下调收号比例扩大利润,同时把主站回收比例作为硬边界,避免分站把比例加得比主站更高。 Co-authored-by: Cursor <cursoragent@cursor.com>
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package listing
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
)
|
|
|
|
func (r *Repository) ReceiveRatioDeltaBySiteCode(ctx context.Context, siteCode string) float64 {
|
|
siteCode = strings.TrimSpace(siteCode)
|
|
if r == nil || r.db == nil || siteCode == "" {
|
|
return 0
|
|
}
|
|
var delta float64
|
|
_ = r.db.WithContext(ctx).Model(&model.DistributionSite{}).
|
|
Select("COALESCE(receive_ratio_delta, 0)").
|
|
Where("site_code = ? AND status = 'active'", siteCode).
|
|
Limit(1).
|
|
Scan(&delta)
|
|
return NormalizeSiteReceiveRatioDelta(delta)
|
|
}
|
|
|
|
func (r *Repository) ResolveReceiveRatioDelta(ctx context.Context, listingID uint64, siteCode string) (float64, bool) {
|
|
if code := strings.TrimSpace(siteCode); code != "" {
|
|
return r.ReceiveRatioDeltaBySiteCode(ctx, code), true
|
|
}
|
|
if r == nil || r.db == nil || listingID == 0 {
|
|
return 0, false
|
|
}
|
|
var siteID *uint64
|
|
if err := r.db.WithContext(ctx).Model(&model.RentalListing{}).
|
|
Select("distribution_site_id").
|
|
Where("id = ?", listingID).
|
|
Limit(1).
|
|
Scan(&siteID).Error; err != nil || siteID == nil || *siteID == 0 {
|
|
return 0, false
|
|
}
|
|
var delta float64
|
|
_ = r.db.WithContext(ctx).Model(&model.DistributionSite{}).
|
|
Select("COALESCE(receive_ratio_delta, 0)").
|
|
Where("id = ? AND status = 'active'", *siteID).
|
|
Limit(1).
|
|
Scan(&delta)
|
|
return NormalizeSiteReceiveRatioDelta(delta), true
|
|
}
|