"""合成账单的逐请求对账，不访问网络、不使用厂商价格。"""
from decimal import Decimal, ROUND_HALF_EVEN
from collections import Counter

gateway = {"a": "0.003000", "b": "0.003333", "c": "3.000000"}
upstream = {"a": "0.003000", "b": "0.003330", "c": "0.003000", "d": "0.002000"}
counts = Counter()
for key in sorted(set(gateway) | set(upstream)):
    if key not in gateway:
        kind = "missing_gateway"
    elif key not in upstream:
        kind = "missing_upstream"
    else:
        left, right = Decimal(gateway[key]), Decimal(upstream[key])
        if left == right:
            kind = "match"
        elif left.quantize(Decimal("0.00001"), rounding=ROUND_HALF_EVEN) == right:
            kind = "rounding"
        elif left == right * 1000:
            kind = "unit_error"
        else:
            kind = "unclassified"
    counts[kind] += 1
    print(key, kind)
assert counts == Counter(match=1, rounding=1, unit_error=1, missing_gateway=1)
print("四类合成差异已核对；单位错误分类仍需真实规则佐证。")
