// price.go S2-D4:生效价引擎——R1/R6 全链唯一「条→元」换算点。 // // 口径(D4-0 用户批准方案默认): // Q1 下家:金额=Σ(日成功条数×当日生效价);PriceAt=价格时间线 EffectiveAt≤当日 // 取最近;无价日按 0 元入账(账单明细如实体现,不阻断生成); // Q2 阶梯:月成功总量落档后**整月单价按档位价覆写**(不做分档混合计价); // Q3 上家 all 档=submit_count(全部提交);success_unknown=成功+未知; // 空 billing_mode 按 success(保守)。 package settle import ( "context" "time" "gorm.io/gorm" "go-sms/internal/model/entity" ) // PriceAtAccount 账号业务线当日生效价(时间线回溯);found=false=无价格记录。 func (s *Service) PriceAtAccount(ctx context.Context, accountID int64, business string, day time.Time) (price float64, found bool) { lo := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location()) var row entity.AccountPrice err := s.db.WithContext(ctx). Where("account_id = ? AND business = ? AND effective_at <= ?", accountID, business, lo). Order("effective_at DESC").First(&row).Error if err != nil { if err == gorm.ErrRecordNotFound { return 0, false } return 0, false // 查询异常按无价处理(0 元入账,对账期暴露),不阻断出账 } return row.Price, true } // PriceAtChannel 通道当日生效成本价(时间线回溯);found=false=无价记录。 func (s *Service) PriceAtChannel(ctx context.Context, channelID int64, day time.Time) (price float64, found bool) { lo := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location()) var row entity.ChannelPrice err := s.db.WithContext(ctx). Where("channel_id = ? AND effective_at <= ?", channelID, lo). Order("effective_at DESC").First(&row).Error if err != nil { return 0, false } return row.Price, true } // LadderPrice 阶梯落档:owner 月成功(或计费)总量命中的最高档单价; // found=false=该 owner 未配阶梯(回退时间线价,Q2)。 func (s *Service) LadderPrice(ctx context.Context, ownerType string, ownerID, monthCount int64) (price float64, found bool) { var row entity.SettleLadder err := s.db.WithContext(ctx). Where("owner_type = ? AND owner_id = ? AND tier_from <= ?", ownerType, ownerID, monthCount). Order("tier_from DESC").First(&row).Error if err != nil { return 0, false } return row.Price, true }