# 全局业务流程图

```mermaid
flowchart TD
    %% ================= 角色与外部系统 =================
    subgraph Clients [客户与渠道]
        User((C端个人用户))
        Enterprise["B端企业客户<br/>(批量采购/自行管卡)"]
        Agent[代理商/分销商]
    end

    subgraph UpstreamLayer [上游基础设施]
        Upstream[上游运营商/厂家API]
        Tower[基站/用量数据源]
    end

    %% ================= 平台核心逻辑 =================
    subgraph Platform [Junhong CMP 核心平台]
        direction TB

        %% --- 1. 交易与开卡链路 ---
        subgraph TradeFlow [交易与开通]
            OrderStart{订单类型?}
            
            %% 流量卡分支
            TrafficCard["流量卡业务<br/>(平台定价+代理加价)"]
            PayPlatform[支付给平台]
            SplitBill[资金分流: <br/>1. 扣除平台成本<br/>2. 代理赚差价]
            
            %% 号卡分支
            NumberCard["号卡业务<br/>(厂家固定定价)"]
            PayFactory[用户直付运营商]
            WaitSettle[等待厂家结算佣金]
            
            %% 开通动作
            ActivateCmd[调用上游激活API]
        end

        %% --- 2. 监控与风控链路 (卡管核心) ---
        subgraph MonitorFlow [监控与生命周期]
            DataSync[用量/状态同步]
            
            MonitorEngine{风控规则引擎}
            
            %% 监控分支
            CheckPersonal{个人套餐<br/>是否超量?}
            CheckEnterprise{企业卡池/额度<br/>是否超标?}
            
            %% 动作
            ActionStop[发送停机指令]
            ActionResume[发送复机指令]
            Alert[发送预警通知]
        end

        %% --- 3. 佣金结算链路 ---
        subgraph FinanceFlow [佣金结算]
            CommCalc[佣金计算]
            
            %% 流量卡佣金
            TrafficProfit[流量卡收益:<br/>差价 + 基础返点]
            
            %% 号卡佣金
            NumberProfit[号卡收益:<br/>冻结 -> 三无校验 -> 解冻]
            
            Wallet[(用户/代理余额)]
        end
    end

    %% ================= 连线关系 =================

    %% 1. 推广与购买
    Agent -->|推广链接/开户| Clients
    User -->|购买单卡| OrderStart
    Enterprise -->|批量采购| OrderStart

    OrderStart -->|流量卡| TrafficCard
    TrafficCard --> PayPlatform
    PayPlatform --> SplitBill
    SplitBill --> ActivateCmd
    
    OrderStart -->|号卡| NumberCard
    NumberCard --> PayFactory
    PayFactory -.->|资金不经平台| Upstream
    PayFactory -->|通知开通| ActivateCmd

    ActivateCmd -->|API请求| Upstream

    %% 2. 资金结算流
    SplitBill -->|入账| Wallet
    Upstream -.->|周期结算单| WaitSettle
    WaitSettle --> CommCalc
    SplitBill --> CommCalc
    
    CommCalc --> TrafficProfit
    CommCalc --> NumberProfit
    TrafficProfit --> Wallet
    NumberProfit --> Wallet

    %% 3. 监控与控制流 (闭环)
    Upstream -->|1. 推送话单/用量| DataSync
    DataSync --> MonitorEngine
    
    MonitorEngine --> CheckPersonal
    MonitorEngine --> CheckEnterprise
    
    %% 个人判断
    CheckPersonal -->|超量| ActionStop
    CheckPersonal -->|正常| DataSync
    
    %% 企业判断
    Enterprise -->|设置额度/查看报表| CheckEnterprise
    CheckEnterprise -->|卡池耗尽/单卡超额| ActionStop
    CheckEnterprise -->|余额充足| ActionResume
    
    %% 执行控制
    ActionStop -->|API: 停机| Upstream
    ActionStop -->|短信/邮件| Alert --> Clients
    ActionResume -->|API: 复机| Upstream

    %% 样式定义
    classDef actor fill:#e1f5ff,stroke:#0288d1,stroke-width:2px
    classDef process fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
    classDef logic fill:#fff9c4,stroke:#fbc02d,stroke-width:1px
    classDef warning fill:#ffcdd2,stroke:#c62828,stroke-width:2px

    class User,Enterprise,Agent,Upstream actor
    class PayPlatform,PayFactory,ActivateCmd,DataSync,ActionStop,ActionResume process
    class OrderStart,MonitorEngine,CheckPersonal,CheckEnterprise,CommCalc logic
    class ActionStop warning
```
