{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "服务器交易数据 v2.0",
  "description": "CNY_10K 表示万元；比例以百分数填写，如 30 表示 30%。同节点先交付保函，再付上游，再收下游并支付返点。 各居间方按每台返点乘绑定客户的销售数量计算，并随对应客户每次回款比例支付。",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "currency",
    "amountUnit",
    "procurement",
    "guaranteeAllocation",
    "customers",
    "intermediaries"
  ],
  "properties": {
    "schemaVersion": {
      "const": "2.0"
    },
    "currency": {
      "const": "CNY"
    },
    "amountUnit": {
      "const": "CNY_10K"
    },
    "procurement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "unitPrice",
        "quantity",
        "advanceRate",
        "guaranteeRate",
        "guaranteeNode",
        "advanceNode",
        "balanceNode"
      ],
      "properties": {
        "unitPrice": {
          "type": "number",
          "minimum": 1e-06,
          "maximum": 1000000000.0
        },
        "quantity": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1000000
        },
        "advanceRate": {
          "type": "number",
          "minimum": 0,
          "maximum": 100
        },
        "guaranteeRate": {
          "type": "number",
          "minimum": 0,
          "maximum": 1000
        },
        "guaranteeNode": {
          "type": "integer",
          "minimum": 1,
          "maximum": 999
        },
        "advanceNode": {
          "type": "integer",
          "minimum": 1,
          "maximum": 999
        },
        "balanceNode": {
          "type": "integer",
          "minimum": 1,
          "maximum": 999
        },
        "guaranteeBasePrice": {
          "type": "number",
          "minimum": 1e-06,
          "maximum": 1000000000.0,
          "description": "保函开立基数 B1，万元/台。省略时跟随采购单价。"
        },
        "guaranteeBaseMode": {
          "enum": [
            "follow",
            "manual"
          ],
          "description": "follow 跟随采购单价；manual 使用 guaranteeBasePrice。"
        }
      }
    },
    "guaranteeAllocation": {
      "enum": [
        "quantity",
        "manual"
      ]
    },
    "customers": {
      "type": "array",
      "maxItems": 200,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "unitPrice",
          "quantity",
          "advanceRate",
          "advanceNode",
          "balanceNode"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 40,
            "pattern": "\\S"
          },
          "unitPrice": {
            "type": "number",
            "minimum": 1e-06,
            "maximum": 1000000000.0
          },
          "quantity": {
            "type": "integer",
            "minimum": 1,
            "maximum": 1000000
          },
          "advanceRate": {
            "type": "number",
            "minimum": 0,
            "maximum": 100
          },
          "advanceNode": {
            "type": "integer",
            "minimum": 1,
            "maximum": 999
          },
          "balanceNode": {
            "type": "integer",
            "minimum": 1,
            "maximum": 999
          },
          "guaranteeAmount": {
            "type": "number",
            "minimum": 0,
            "maximum": 1000000000.0
          },
          "advanceMode": {
            "enum": [
              "auto",
              "manual"
            ],
            "default": "manual",
            "description": "auto 按保函比例减 m 重算；manual 使用 advanceRate。旧数据省略时保留手动预付款。"
          },
          "id": {
            "type": "integer",
            "minimum": 1,
            "maximum": 9007199254740991,
            "description": "稳定且唯一的客户 ID，居间方通过 customerIds 绑定；旧数据省略时按数组位置从 1 分配。"
          }
        }
      }
    },
    "intermediaries": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "rebatePerUnit"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 40,
            "pattern": "\\S"
          },
          "rebatePerUnit": {
            "type": "number",
            "minimum": 0,
            "maximum": 1000000000.0
          },
          "customerIds": {
            "type": "array",
            "uniqueItems": true,
            "maxItems": 200,
            "items": {
              "type": "integer",
              "minimum": 1,
              "maximum": 9007199254740991
            },
            "description": "绑定客户 ID。省略时绑定全部客户（含后续新增），空数组表示不绑定；非空数组只绑定列出的客户，所有 ID 必须存在。"
          }
        }
      }
    },
    "advanceMarginPoints": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "default": 3,
      "description": "预付款差额 m，单位：百分点；自动预付款按实际保函比例减 m 后四舍五入至 0.1 个百分点，并限制在 0–100。"
    },
    "calculation": {
      "type": "object",
      "description": "完整计算快照，包括客户、居间方、现金流事件、收益和零垫资状态。导入时忽略并按输入重新计算。"
    },
    "tradeName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 60,
      "pattern": "\\S",
      "description": "贸易条件名称，暂存、JSON 和客户图片使用同一名称。"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "guaranteeAllocation": {
            "const": "manual"
          }
        }
      },
      "then": {
        "properties": {
          "customers": {
            "items": {
              "required": [
                "guaranteeAmount"
              ]
            }
          }
        }
      }
    }
  ]
}
