"""不可行分配的容量说明;容量上界不能替代产品范围和双证引用的联合求解。""" from decimal import Decimal def allocation_diagnostics(demands, purchases, transports): rows = [] for kind, permits in [('purchase', purchases), ('transport', transports)]: for unit in sorted({s.unit for s in demands}): sources = [s for s in demands if s.unit == unit] products = {s.product_id for s in sources} eligible = [p for p in permits if p.product_ids & products] demand = sum((s.quantity for s in sources), Decimal(0)) foreign = any(p.unit != unit for p in eligible) available = sum((p.available for p in eligible if p.unit == unit), Decimal(0)) gap = max(Decimal(0), demand - available) reason = ('存在异单位证件,请逐来源人工填写换算量;不能按证面余额推算可覆盖量' if foreign else '余额不足,请补充该证种额度' if gap else '总余额仅为上界;请核对每个合同产品范围、运输引用关系及人工分配约束') rows.append({'certType': kind, 'sourceIds': [s.source_id for s in sources], 'unit': unit, 'demand': format(demand, 'f'), 'availableUpperBound': None if foreign else format(available, 'f'), 'gapLowerBound': None if foreign else format(gap, 'f'), 'reason': reason}) return rows