147 lines
5.1 KiB
Go
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
|
|
}
|