# -*- coding: utf-8 -*-
"""300 号 / 100 台机器 / 每天 9000 条：2027 全年收益（自动化机群模型）"""
import math, random, statistics as st, sys, json

sys.stdout.reconfigure(encoding="utf-8")
random.seed(23)
N_TRIAL = 2000
ACCOUNTS = 300
POSTS = 30
DAYS = 30

SEASON = [1.00, 0.90, 1.00, 1.00, 1.00, 1.25, 1.00, 1.00, 1.05, 1.10, 1.80, 1.15]
BOOST, HAIRCUT = 1.25, 0.80

CLASSES = {
    "C": [(0.60, 0.04, 2.0, 0.38), (0.28, 0.18, 2.0, 0.70), (0.09, 0.80, 1.9, 1.00), (0.03, 5.00, 1.7, 1.00)],
    "D": [(0.42, 0.12, 2.0, 0.55), (0.36, 0.45, 2.0, 0.85), (0.17, 1.80, 1.9, 1.00), (0.05, 9.00, 1.7, 1.00)],
}


def pick(cls):
    r = random.random(); cum = 0
    for p, med, sig, act in cls:
        cum += p
        if r <= cum:
            return med, sig, act
    return cls[-1][1], cls[-1][2], cls[-1][3]


def account_year(cls):
    med, sig, act = pick(cls)
    prev1 = prev2 = 0.0
    alive = 1.0
    total = 0.0
    for m in range(12):
        g = 1.0 + 0.15 * min(m, 5)
        n = POSTS * DAYS * act
        new = sum(random.lognormvariate(math.log(max(med * g, 1e-6)), sig) for _ in range(int(n)))
        rev = (0.60 * new + 0.25 * prev1 + 0.15 * prev2) * SEASON[m] * BOOST * HAIRCUT * alive
        total += rev
        prev2, prev1 = prev1, new
        if random.random() < 0.06:
            alive *= 0.3
    return total


def pct(xs, p):
    xs = sorted(xs); return xs[min(int(len(xs) * p), len(xs) - 1)]


def run(mix_d=0.5, ban_p=0.35, ban_sev=0.2):
    c_samp = [account_year(CLASSES["C"]) for _ in range(4000)]
    d_samp = [account_year(CLASSES["D"]) for _ in range(4000)]
    totals = []
    for _ in range(N_TRIAL):
        shared = random.lognormvariate(0, 0.2)
        if random.random() < ban_p:
            shared *= ban_sev
        s = sum((random.choice(d_samp) if random.random() < mix_d else random.choice(c_samp)) for _ in range(ACCOUNTS))
        totals.append(s * shared)
    return st.mean(c_samp), st.median(c_samp), st.mean(d_samp), st.median(d_samp), totals


if __name__ == "__main__":
    cm, cmed, dm, dmed, tot_c = run(mix_d=0.0, ban_p=0.0, ban_sev=1.0)
    _, _, _, _, tot_d = run(mix_d=1.0, ban_p=0.0, ban_sev=1.0)
    _, _, _, _, tot_mix = run(mix_d=0.3, ban_p=0.0, ban_sev=1.0)
    print(f"单号年收益：C 均值 {cm:,.0f} / 中位 {cmed:,.0f}；D 均值 {dm:,.0f} / 中位 {dmed:,.0f}")

    print("\n=== 300 号｜2027 毛佣金（无平台级封禁情景）===")
    for label, xs in [("全 C", tot_c), ("全 D", tot_d), ("70%C+30%D", tot_mix)]:
        print(f"{label}: 均值 {st.mean(xs):,.0f} | 中位 {st.median(xs):,.0f} | P25 {pct(xs,.25):,.0f} | P90 {pct(xs,.9):,.0f}")

    _, _, _, _, tot_mix_ban = run(mix_d=0.3, ban_p=0.35, ban_sev=0.2)
    print("\n=== 叠加平台治理（35% 概率年度清零、损失 80%）===")
    print(f"70%C+30%D: 均值 {st.mean(tot_mix_ban):,.0f} | 中位 {st.median(tot_mix_ban):,.0f} | P25 {pct(tot_mix_ban,.25):,.0f} | P90 {pct(tot_mix_ban,.9):,.0f}")

    # ---- 成本：100 台机器 ----
    pcs = 100
    capex = pcs * 7000
    capex_amort = capex / 3
    power = pcs * 0.25 * 24 * 365 * 0.6   # 0.25kW/台, 0.6 元/度
    net = pcs * 200 * 12                   # 每条宽带/代理 200/月
    staff = 4 * 8000 * 12                  # 4 人运维
    accounts_cost = ACCOUNTS * 100
    software = 60000
    opex = power + net + staff + accounts_cost + software
    total_cost = opex + capex_amort
    print("\n=== 成本（100 台机器）===")
    print(f"硬件 {capex/1e4:.0f} 万（按 3 年摊 {capex_amort/1e4:.0f} 万/年）")
    print(f"电费 {power/1e4:.1f} 万（0.25kW×24h×0.6 元）| 网络/代理 {net/1e4:.1f} 万 | 运维 4 人 {staff/1e4:.1f} 万")
    print(f"账号 300×100 {accounts_cost/1e4:.1f} 万 | 自动化软件 {software/1e4:.1f} 万")
    print(f"年成本合计 ≈ {total_cost/1e4:.0f} 万（首年含硬件 {capex/1e4:.0f} 万）")

    videos = ACCOUNTS * POSTS * 360
    print(f"\n年产 {videos:,.0f} 条；单条佣金（混合）{st.mean(tot_mix)/videos:.2f} 元；单条成本 {total_cost/videos:.2f} 元")
    for label, xs in [("全C", tot_c), ("全D", tot_d), ("70C30D", tot_mix), ("70C30D+治理", tot_mix_ban)]:
        print(f"净利 {label}: 均值 {(st.mean(xs)-total_cost)/1e4:,.0f} 万 | 中位 {(st.median(xs)-total_cost)/1e4:,.0f} 万")
    json.dump({"mix_mean": st.mean(tot_mix), "mix_median": st.median(tot_mix),
               "mix_ban_mean": st.mean(tot_mix_ban), "cost": total_cost, "videos": videos},
              open(r"D:\Deepseek\douyin_fengdaquan\_farm_result.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
