chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
package packageprice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type Source struct {
|
||||
PackageCode string
|
||||
CostPrice float64
|
||||
TerminalCost float64
|
||||
SysDefaultRatio float64
|
||||
SysDefaultProfit float64
|
||||
SysDefaultTerminalRatio float64
|
||||
SysDefaultTerminalProfit float64
|
||||
}
|
||||
|
||||
type BaseCosts struct {
|
||||
AgentCost float64
|
||||
TerminalCost float64
|
||||
}
|
||||
|
||||
type Rule struct {
|
||||
PriceSource string
|
||||
AgentDefaultPriceId int64
|
||||
TargetPriceId int64
|
||||
AgentCostRatio float64
|
||||
AgentCostProfit float64
|
||||
TerminalCostRatio float64
|
||||
TerminalCostProfit float64
|
||||
Remark string
|
||||
UpdatedAt *gtime.Time
|
||||
}
|
||||
|
||||
type SalePrice struct {
|
||||
Base BaseCosts
|
||||
Rule Rule
|
||||
AgentShipmentPrice float64
|
||||
TerminalSalePrice float64
|
||||
}
|
||||
|
||||
type priceStore interface {
|
||||
ParentDeptID(ctx context.Context, tenantID string, deptID int64) (int64, error)
|
||||
PriceOverride(ctx context.Context, tenantID string, deptID int64, scopeType int, targetAgentID int64, packageCode string) (*entity.TSimFlowPackagePrice, error)
|
||||
}
|
||||
|
||||
type dbStore struct {
|
||||
tx gdb.TX
|
||||
}
|
||||
|
||||
func SourceFromPackageInfo(pkg *entity.TSimFlowPackageInfo) Source {
|
||||
if pkg == nil {
|
||||
return Source{}
|
||||
}
|
||||
return Source{
|
||||
PackageCode: strings.TrimSpace(pkg.PackageCode),
|
||||
CostPrice: pkg.CostPrice,
|
||||
TerminalCost: pkg.TerminalCost,
|
||||
SysDefaultRatio: pkg.SysDefaultRatio,
|
||||
SysDefaultProfit: pkg.SysDefaultProfit,
|
||||
SysDefaultTerminalRatio: pkg.SysDefaultTerminalRatio,
|
||||
SysDefaultTerminalProfit: pkg.SysDefaultTerminalProfit,
|
||||
}
|
||||
}
|
||||
|
||||
func SourceFromPriceListItem(pkg *model.TSimFlowPackagePriceAgentDefaultPackagePricePackageItem) Source {
|
||||
if pkg == nil {
|
||||
return Source{}
|
||||
}
|
||||
return Source{
|
||||
PackageCode: strings.TrimSpace(pkg.PackageCode),
|
||||
CostPrice: pkg.CostPrice,
|
||||
TerminalCost: pkg.TerminalCost,
|
||||
SysDefaultRatio: pkg.SysDefaultRatio,
|
||||
SysDefaultProfit: pkg.SysDefaultProfit,
|
||||
SysDefaultTerminalRatio: pkg.SysDefaultTerminalRatio,
|
||||
SysDefaultTerminalProfit: pkg.SysDefaultTerminalProfit,
|
||||
}
|
||||
}
|
||||
|
||||
func ResolveEffectiveBaseCosts(ctx context.Context, tx gdb.TX, tenantID string, deptID int64, pkg Source) (BaseCosts, error) {
|
||||
return resolveEffectiveBaseCosts(ctx, dbStore{tx: tx}, tenantID, deptID, pkg)
|
||||
}
|
||||
|
||||
func ResolveEffectiveSalePrice(ctx context.Context, tx gdb.TX, tenantID string, ownerDeptID int64, targetDeptID int64, pkg Source) (SalePrice, error) {
|
||||
return resolveEffectiveSalePrice(ctx, dbStore{tx: tx}, tenantID, ownerDeptID, targetDeptID, pkg)
|
||||
}
|
||||
|
||||
func NormalizeRatio(ratio float64) float64 {
|
||||
if ratio > 10 {
|
||||
return ratio / 100
|
||||
}
|
||||
return ratio
|
||||
}
|
||||
|
||||
func RoundAmount(value float64) float64 {
|
||||
return math.Round(value*100) / 100
|
||||
}
|
||||
|
||||
func resolveEffectiveBaseCosts(ctx context.Context, store priceStore, tenantID string, deptID int64, pkg Source) (BaseCosts, error) {
|
||||
visited := make(map[int64]struct{})
|
||||
return resolveEffectiveBaseCostsDepth(ctx, store, tenantID, deptID, pkg, visited)
|
||||
}
|
||||
|
||||
func resolveEffectiveSalePrice(ctx context.Context, store priceStore, tenantID string, ownerDeptID int64, targetDeptID int64, pkg Source) (SalePrice, error) {
|
||||
base, err := resolveEffectiveBaseCosts(ctx, store, tenantID, ownerDeptID, pkg)
|
||||
if err != nil {
|
||||
return SalePrice{}, err
|
||||
}
|
||||
rule, err := resolveEffectivePriceRule(ctx, store, tenantID, ownerDeptID, targetDeptID, pkg)
|
||||
if err != nil {
|
||||
return SalePrice{}, err
|
||||
}
|
||||
return SalePrice{
|
||||
Base: base,
|
||||
Rule: rule,
|
||||
AgentShipmentPrice: RoundAmount(base.AgentCost*rule.AgentCostRatio + rule.AgentCostProfit),
|
||||
TerminalSalePrice: RoundAmount(base.TerminalCost*rule.TerminalCostRatio + rule.TerminalCostProfit),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resolveEffectiveBaseCostsDepth(ctx context.Context, store priceStore, tenantID string, deptID int64, pkg Source, visited map[int64]struct{}) (BaseCosts, error) {
|
||||
if strings.TrimSpace(pkg.PackageCode) == "" {
|
||||
return BaseCosts{}, gerror.New("套餐编码不能为空")
|
||||
}
|
||||
if deptID <= 0 {
|
||||
return systemBaseCosts(pkg), nil
|
||||
}
|
||||
if _, ok := visited[deptID]; ok {
|
||||
return BaseCosts{}, gerror.New("组织定价层级存在循环,无法计算生效成本")
|
||||
}
|
||||
visited[deptID] = struct{}{}
|
||||
|
||||
parentID, err := store.ParentDeptID(ctx, tenantID, deptID)
|
||||
if err != nil {
|
||||
return BaseCosts{}, err
|
||||
}
|
||||
if parentID <= 0 {
|
||||
return systemBaseCosts(pkg), nil
|
||||
}
|
||||
|
||||
parentBase, err := resolveEffectiveBaseCostsDepth(ctx, store, tenantID, parentID, pkg, visited)
|
||||
if err != nil {
|
||||
return BaseCosts{}, err
|
||||
}
|
||||
parentRule, err := resolveEffectivePriceRule(ctx, store, tenantID, parentID, deptID, pkg)
|
||||
if err != nil {
|
||||
return BaseCosts{}, err
|
||||
}
|
||||
return BaseCosts{
|
||||
AgentCost: RoundAmount(parentBase.AgentCost*parentRule.AgentCostRatio + parentRule.AgentCostProfit),
|
||||
TerminalCost: RoundAmount(parentBase.TerminalCost*parentRule.TerminalCostRatio + parentRule.TerminalCostProfit),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func systemBaseCosts(pkg Source) BaseCosts {
|
||||
return BaseCosts{
|
||||
AgentCost: RoundAmount(pkg.CostPrice),
|
||||
TerminalCost: RoundAmount(pkg.TerminalCost),
|
||||
}
|
||||
}
|
||||
|
||||
func resolveEffectivePriceRule(ctx context.Context, store priceStore, tenantID string, ownerDeptID int64, targetDeptID int64, pkg Source) (Rule, error) {
|
||||
rule := systemPriceRule(pkg)
|
||||
if ownerDeptID <= 0 {
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
if targetDeptID > 0 {
|
||||
targetPrice, err := store.PriceOverride(ctx, tenantID, ownerDeptID, 1, targetDeptID, pkg.PackageCode)
|
||||
if err != nil {
|
||||
return rule, err
|
||||
}
|
||||
if targetPrice != nil {
|
||||
return ruleFromOverride("target", targetPrice), nil
|
||||
}
|
||||
}
|
||||
|
||||
defaultPrice, err := store.PriceOverride(ctx, tenantID, ownerDeptID, 0, 0, pkg.PackageCode)
|
||||
if err != nil {
|
||||
return rule, err
|
||||
}
|
||||
if defaultPrice != nil {
|
||||
return ruleFromOverride("agentDefault", defaultPrice), nil
|
||||
}
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
func systemPriceRule(pkg Source) Rule {
|
||||
return Rule{
|
||||
PriceSource: "system",
|
||||
AgentCostRatio: NormalizeRatio(pkg.SysDefaultRatio),
|
||||
AgentCostProfit: pkg.SysDefaultProfit,
|
||||
TerminalCostRatio: NormalizeRatio(pkg.SysDefaultTerminalRatio),
|
||||
TerminalCostProfit: pkg.SysDefaultTerminalProfit,
|
||||
}
|
||||
}
|
||||
|
||||
func ruleFromOverride(priceSource string, price *entity.TSimFlowPackagePrice) Rule {
|
||||
if price == nil {
|
||||
return Rule{PriceSource: priceSource}
|
||||
}
|
||||
rule := Rule{
|
||||
PriceSource: priceSource,
|
||||
AgentCostRatio: NormalizeRatio(price.AgentCostRatio),
|
||||
AgentCostProfit: price.AgentCostProfit,
|
||||
TerminalCostRatio: NormalizeRatio(price.UserCostRatio),
|
||||
TerminalCostProfit: price.UserCostProfit,
|
||||
Remark: price.Remark,
|
||||
UpdatedAt: price.UpdatedAt,
|
||||
}
|
||||
if priceSource == "target" {
|
||||
rule.TargetPriceId = price.Id
|
||||
} else {
|
||||
rule.AgentDefaultPriceId = price.Id
|
||||
}
|
||||
return rule
|
||||
}
|
||||
|
||||
func (s dbStore) ParentDeptID(ctx context.Context, tenantID string, deptID int64) (int64, error) {
|
||||
if deptID <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
dept := &entity.SysDept{}
|
||||
columns := dao.SysDept.Columns()
|
||||
err := s.model(ctx, dao.SysDept.Table()).
|
||||
Fields(columns.DeptId, columns.ParentId).
|
||||
Where(columns.TenantId, tenantID).
|
||||
Where(columns.DeptId, deptID).
|
||||
WhereNull(columns.DeletedAt).
|
||||
Scan(dept)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, gerror.New("组织不存在")
|
||||
}
|
||||
return 0, gerror.Wrap(err, "查询组织上级失败")
|
||||
}
|
||||
if dept.DeptId == 0 {
|
||||
return 0, gerror.New("组织不存在")
|
||||
}
|
||||
return dept.ParentId, nil
|
||||
}
|
||||
|
||||
func (s dbStore) PriceOverride(ctx context.Context, tenantID string, deptID int64, scopeType int, targetAgentID int64, packageCode string) (*entity.TSimFlowPackagePrice, error) {
|
||||
packageCode = strings.TrimSpace(packageCode)
|
||||
if tenantID == "" || deptID <= 0 || packageCode == "" {
|
||||
return nil, nil
|
||||
}
|
||||
price := &entity.TSimFlowPackagePrice{}
|
||||
columns := dao.TSimFlowPackagePrice.Columns()
|
||||
err := s.model(ctx, dao.TSimFlowPackagePrice.Table()).
|
||||
Where(columns.TenantId, tenantID).
|
||||
Where(columns.DeptId, deptID).
|
||||
Where(columns.AgentScopeType, scopeType).
|
||||
Where(columns.TargetAgentId, targetAgentID).
|
||||
Where(columns.PackageCode, packageCode).
|
||||
Where(columns.Enabled, 1).
|
||||
Scan(price)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, gerror.Wrap(err, "查询套餐定价失败")
|
||||
}
|
||||
if price.Id == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
|
||||
func (s dbStore) model(ctx context.Context, table string) *gdb.Model {
|
||||
if s.tx != nil {
|
||||
return s.tx.Ctx(ctx).Model(table)
|
||||
}
|
||||
return g.DB().Ctx(ctx).Model(table)
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package packageprice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
)
|
||||
|
||||
type fakePriceStore struct {
|
||||
parents map[int64]int64
|
||||
prices map[priceKey]*entity.TSimFlowPackagePrice
|
||||
}
|
||||
|
||||
type priceKey struct {
|
||||
deptID int64
|
||||
scopeType int
|
||||
targetAgentID int64
|
||||
packageCode string
|
||||
}
|
||||
|
||||
func (s fakePriceStore) ParentDeptID(ctx context.Context, tenantID string, deptID int64) (int64, error) {
|
||||
return s.parents[deptID], nil
|
||||
}
|
||||
|
||||
func (s fakePriceStore) PriceOverride(ctx context.Context, tenantID string, deptID int64, scopeType int, targetAgentID int64, packageCode string) (*entity.TSimFlowPackagePrice, error) {
|
||||
return s.prices[priceKey{
|
||||
deptID: deptID,
|
||||
scopeType: scopeType,
|
||||
targetAgentID: targetAgentID,
|
||||
packageCode: packageCode,
|
||||
}], nil
|
||||
}
|
||||
|
||||
func TestResolveEffectiveSalePriceUsesRecursiveParentCost(t *testing.T) {
|
||||
store := fakePriceStore{
|
||||
parents: map[int64]int64{
|
||||
117: 100,
|
||||
131: 117,
|
||||
},
|
||||
prices: map[priceKey]*entity.TSimFlowPackagePrice{
|
||||
{deptID: 100, scopeType: 1, targetAgentID: 117, packageCode: "yd-100m-3y"}: {
|
||||
Id: 86,
|
||||
AgentCostRatio: 1,
|
||||
AgentCostProfit: 4,
|
||||
UserCostRatio: 1,
|
||||
UserCostProfit: 4,
|
||||
},
|
||||
{deptID: 117, scopeType: 0, targetAgentID: 0, packageCode: "yd-100m-3y"}: {
|
||||
Id: 91,
|
||||
AgentCostRatio: 1,
|
||||
AgentCostProfit: 1,
|
||||
UserCostRatio: 1,
|
||||
UserCostProfit: 1,
|
||||
},
|
||||
{deptID: 117, scopeType: 1, targetAgentID: 131, packageCode: "yd-100m-3y"}: {
|
||||
Id: 92,
|
||||
AgentCostRatio: 1,
|
||||
AgentCostProfit: 5,
|
||||
UserCostRatio: 1,
|
||||
UserCostProfit: 5,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
got, err := resolveEffectiveSalePrice(
|
||||
context.Background(),
|
||||
store,
|
||||
"000000",
|
||||
117,
|
||||
131,
|
||||
Source{
|
||||
PackageCode: "yd-100m-3y",
|
||||
CostPrice: 10,
|
||||
TerminalCost: 10,
|
||||
SysDefaultRatio: 1,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("resolveEffectiveSalePrice() error = %v", err)
|
||||
}
|
||||
if got.Base.AgentCost != 14 {
|
||||
t.Fatalf("base agent cost = %.2f, want 14.00", got.Base.AgentCost)
|
||||
}
|
||||
if got.AgentShipmentPrice != 19 {
|
||||
t.Fatalf("agent shipment price = %.2f, want 19.00", got.AgentShipmentPrice)
|
||||
}
|
||||
if got.Rule.PriceSource != "target" || got.Rule.TargetPriceId != 92 {
|
||||
t.Fatalf("rule = %+v, want target price 92", got.Rule)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user