refactor(distribution): 重写分销迁移并落地利润百分比分成与货架比例

将 000064-000078 合并为 064/065/066,默认规则改为收号 35%、卖出 40%、服务费 25%。
货架调价改为增加分站 1 元兑换哈夫币比例。sql-guard 仅保护 main 已发布迁移。
This commit is contained in:
yml2213
2026-09-08 19:02:58 +08:00
parent 48a2961245
commit d762b33f54
48 changed files with 745 additions and 1073 deletions
@@ -237,11 +237,11 @@ func (r *Repository) FindPublic(ctx context.Context, id uint64, siteCode string)
return nil, err
}
applyPublicListingURLs(dto)
markups, err := r.shelfMarkupByListingID(ctx, siteCode, []uint64{dto.ID})
if err != nil {
items := []ListingDTO{*dto}
if err := r.applyShelfMarkup(ctx, siteCode, items); err != nil {
return nil, err
}
dto.PriceCent += markups[dto.ID]
*dto = items[0]
return dto, nil
}
@@ -253,36 +253,40 @@ func (r *Repository) applyShelfMarkup(ctx context.Context, siteCode string, item
for _, item := range items {
ids = append(ids, item.ID)
}
markups, err := r.shelfMarkupByListingID(ctx, siteCode, ids)
deltas, err := r.shelfRatioDeltaByListingID(ctx, siteCode, ids)
if err != nil {
return err
}
for i := range items {
items[i].PriceCent += markups[items[i].ID]
delta := deltas[items[i].ID]
if delta == 0 {
continue
}
items[i].PriceCent, _, _ = ApplyShelfRatio(items[i].HafCoinAmount, items[i].PriceCent, delta)
}
return nil
}
func (r *Repository) shelfMarkupByListingID(ctx context.Context, siteCode string, listingIDs []uint64) (map[uint64]int64, error) {
result := make(map[uint64]int64, len(listingIDs))
func (r *Repository) shelfRatioDeltaByListingID(ctx context.Context, siteCode string, listingIDs []uint64) (map[uint64]float64, error) {
result := make(map[uint64]float64, len(listingIDs))
if siteCode == "" || len(listingIDs) == 0 {
return result, nil
}
type markupRow struct {
ListingID uint64 `gorm:"column:listing_id"`
MarkupAmountCent int64 `gorm:"column:markup_amount_cent"`
type ratioRow struct {
ListingID uint64 `gorm:"column:listing_id"`
ShelfRatioDelta float64 `gorm:"column:shelf_ratio_delta"`
}
var rows []markupRow
var rows []ratioRow
if err := r.db.WithContext(ctx).Table("distribution_site_accounts AS dsa").
Select("dsa.listing_id, COALESCE(dsa.markup_amount_cent, 0) AS markup_amount_cent").
Select("dsa.listing_id, COALESCE(dsa.shelf_ratio_delta, 0) AS shelf_ratio_delta").
Joins("INNER JOIN distribution_sites AS ds ON ds.id = dsa.site_id AND ds.site_code = ? AND ds.status = 'active'", siteCode).
Where("dsa.display_status = 'enabled' AND dsa.listing_id IN ?", listingIDs).
Scan(&rows).Error; err != nil {
return nil, err
}
for _, row := range rows {
if row.MarkupAmountCent > 0 {
result[row.ListingID] = row.MarkupAmountCent
if row.ShelfRatioDelta > 0 {
result[row.ListingID] = row.ShelfRatioDelta
}
}
return result, nil