Files
iotlcm/internal/service/renewchange/price_effective.go
T
MiFaZhan 2bec257289 chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。

- Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件)
- 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、
  库名 xiujie_admin → iotlcm、哨兵名占位
- 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异,
  不破坏「校验 MSISDN 格式」等测试断言)
- 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像
  digest 记录
- Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23),
  移除写死的 git 凭据
- README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0
  LICENSE,LF 行尾 + .gitattributes

已知:3 个测试包在源仓同一提交上同样失败(client/upstream、
service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
2026-10-22 16:37:56 +08:00

147 lines
5.1 KiB
Go

package renewchange
import (
"context"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
rc "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/repository/renewchange"
)
type renewEffectivePriceStore interface {
LoadDept(ctx context.Context, tenantID string, deptID int64) (*rc.SysDeptRow, error)
LoadPriceOverride(ctx context.Context, tenantID string, deptID, targetAgentID int64, scopeType int, packageCode string) (*rc.FlowPackagePrice, error)
}
type renewEffectivePriceRule struct {
PriceSource string
AgentDefaultPriceId int64
TargetPriceId int64
AgentCostRatio float64
AgentCostProfit float64
}
type renewEffectiveSalePrice struct {
BaseAgentCost float64
Rule renewEffectivePriceRule
AgentShipmentPrice float64
}
type EffectivePriceStore = renewEffectivePriceStore
type EffectivePriceRule = renewEffectivePriceRule
type EffectiveSalePrice = renewEffectiveSalePrice
type renewTxEffectivePriceStore struct {
tx gdb.TX
}
func (s renewTxEffectivePriceStore) LoadDept(ctx context.Context, tenantID string, deptID int64) (*rc.SysDeptRow, error) {
return loadRenewDeptTx(ctx, s.tx, tenantID, deptID)
}
func (s renewTxEffectivePriceStore) LoadPriceOverride(ctx context.Context, tenantID string, deptID, targetAgentID int64, scopeType int, packageCode string) (*rc.FlowPackagePrice, error) {
return loadRenewPriceOverrideTx(ctx, s.tx, tenantID, deptID, targetAgentID, scopeType, packageCode)
}
func ResolveEffectiveSalePrice(ctx context.Context, store EffectivePriceStore, tenantID string, ownerDeptID int64, targetDeptID int64, pkg *rc.FlowPackageInfo) (EffectiveSalePrice, error) {
return resolveRenewEffectiveSalePrice(ctx, store, tenantID, ownerDeptID, targetDeptID, pkg)
}
func resolveRenewEffectiveSalePrice(ctx context.Context, store renewEffectivePriceStore, tenantID string, ownerDeptID int64, targetDeptID int64, pkg *rc.FlowPackageInfo) (renewEffectiveSalePrice, error) {
baseCost, err := resolveRenewEffectiveBaseCost(ctx, store, tenantID, ownerDeptID, pkg, make(map[int64]struct{}))
if err != nil {
return renewEffectiveSalePrice{}, err
}
rule, err := resolveRenewEffectivePriceRule(ctx, store, tenantID, ownerDeptID, targetDeptID, pkg)
if err != nil {
return renewEffectiveSalePrice{}, err
}
return renewEffectiveSalePrice{
BaseAgentCost: baseCost,
Rule: rule,
AgentShipmentPrice: rc.RoundAmount(baseCost*rule.AgentCostRatio + rule.AgentCostProfit),
}, nil
}
func resolveRenewEffectiveBaseCost(ctx context.Context, store renewEffectivePriceStore, tenantID string, deptID int64, pkg *rc.FlowPackageInfo, visited map[int64]struct{}) (float64, error) {
if pkg == nil || strings.TrimSpace(pkg.PackageCode) == "" {
return 0, gerror.New("套餐编码不能为空")
}
if deptID <= 0 {
return rc.RoundAmount(pkg.CostPrice), nil
}
if _, ok := visited[deptID]; ok {
return 0, gerror.New("组织定价层级存在循环,无法计算生效成本")
}
visited[deptID] = struct{}{}
dept, err := store.LoadDept(ctx, tenantID, deptID)
if err != nil {
return 0, err
}
if dept == nil || dept.ParentId <= 0 {
return rc.RoundAmount(pkg.CostPrice), nil
}
parentBaseCost, err := resolveRenewEffectiveBaseCost(ctx, store, tenantID, dept.ParentId, pkg, visited)
if err != nil {
return 0, err
}
parentRule, err := resolveRenewEffectivePriceRule(ctx, store, tenantID, dept.ParentId, deptID, pkg)
if err != nil {
return 0, err
}
return rc.RoundAmount(parentBaseCost*parentRule.AgentCostRatio + parentRule.AgentCostProfit), nil
}
func resolveRenewEffectivePriceRule(ctx context.Context, store renewEffectivePriceStore, tenantID string, ownerDeptID int64, targetDeptID int64, pkg *rc.FlowPackageInfo) (renewEffectivePriceRule, error) {
rule := renewSystemPriceRule(pkg)
if ownerDeptID <= 0 {
return rule, nil
}
if targetDeptID > 0 {
targetPrice, err := store.LoadPriceOverride(ctx, tenantID, ownerDeptID, targetDeptID, PriceScopeTargetAgent, pkg.PackageCode)
if err != nil {
return rule, err
}
if targetPrice != nil {
return renewRuleFromOverride(PriceSourceTarget, targetPrice), nil
}
}
defaultPrice, err := store.LoadPriceOverride(ctx, tenantID, ownerDeptID, 0, PriceScopeAgentDefault, pkg.PackageCode)
if err != nil {
return rule, err
}
if defaultPrice != nil {
return renewRuleFromOverride(PriceSourceAgentDefault, defaultPrice), nil
}
return rule, nil
}
func renewSystemPriceRule(pkg *rc.FlowPackageInfo) renewEffectivePriceRule {
if pkg == nil {
return renewEffectivePriceRule{PriceSource: PriceSourceSystem}
}
return renewEffectivePriceRule{
PriceSource: PriceSourceSystem,
AgentCostRatio: normalizePriceRatio(pkg.SysDefaultRatio),
AgentCostProfit: pkg.SysDefaultProfit,
}
}
func renewRuleFromOverride(source string, price *rc.FlowPackagePrice) renewEffectivePriceRule {
rule := renewEffectivePriceRule{
PriceSource: source,
AgentCostRatio: normalizePriceRatio(price.AgentCostRatio),
AgentCostProfit: price.AgentCostProfit,
}
if source == PriceSourceTarget {
rule.TargetPriceId = price.Id
} else {
rule.AgentDefaultPriceId = price.Id
}
return rule
}