{
  "schemaVersion": 1,
  "id": "5a0e1f2b-3c4d-4e5f-8a6b-7c8d9e0f0105",
  "name": "Демо: заявки из Excel в очередь",
  "description": "Читает requests.xlsx из своей папки и добавляет каждую строку элементом очереди «Заявки» (ссылка — колонка «Номер»). Уже добавленные заявки пропускает",
  "variables": [
    { "name": "queue", "type": "string", "default": "Заявки", "isArgument": true },
    { "name": "rows", "type": "list", "default": [], "isArgument": false },
    { "name": "itemId", "type": "string", "default": null, "isArgument": false },
    { "name": "added", "type": "number", "default": 0, "isArgument": false },
    { "name": "error", "type": "object", "default": null, "isArgument": false }
  ],
  "steps": [
    {
      "id": "s_read0001",
      "type": "excel.readRange",
      "displayName": "Прочитать заявки",
      "props": { "path": "requests.xlsx", "sheet": "", "range": "", "hasHeaders": true },
      "out": { "rows": "rows" }
    },
    {
      "id": "s_log00001",
      "type": "control.log",
      "props": { "level": "info", "message": "Заявок в файле: {{ rows.length }}" }
    },
    {
      "id": "s_loop0001",
      "type": "control.forEach",
      "displayName": "Для каждой заявки",
      "props": { "items": "rows", "itemVariable": "row", "indexVariable": "i" },
      "children": {
        "body": [
          {
            "id": "s_try00001",
            "type": "control.tryCatch",
            "displayName": "Добавить, если её ещё нет",
            "props": { "errorVariable": "error" },
            "children": {
              "try": [
                {
                  "id": "s_add00001",
                  "type": "orch.queueAddItem",
                  "displayName": "Добавить заявку в очередь",
                  "props": { "queue": "{{ queue }}", "payload": "row", "reference": "{{ row['Номер'] }}", "priority": "normal" },
                  "out": { "itemId": "itemId" }
                },
                {
                  "id": "s_count001",
                  "type": "control.assign",
                  "props": { "variable": "added", "value": "added + 1" }
                },
                {
                  "id": "s_logadd01",
                  "type": "control.log",
                  "props": { "level": "info", "message": "Заявка {{ row['Номер'] }} добавлена в очередь «{{ queue }}»" }
                }
              ],
              "catch": [
                {
                  "id": "s_dup00001",
                  "type": "control.if",
                  "props": { "condition": "error.code === 'QueueItemDuplicate'" },
                  "children": {
                    "then": [
                      { "id": "s_logdup01", "type": "control.log", "props": { "level": "warn", "message": "Заявка {{ row['Номер'] }} уже есть в очереди — пропущена" } }
                    ],
                    "else": [
                      { "id": "s_rethrow1", "type": "control.throw", "props": { "message": "Не удалось добавить заявку {{ row['Номер'] }}: {{ error.message }}" } }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    },
    {
      "id": "s_output01",
      "type": "orch.setOutput",
      "displayName": "Результат: сколько добавлено",
      "props": { "name": "добавлено", "value": "added" }
    },
    {
      "id": "s_done0001",
      "type": "control.log",
      "props": { "level": "info", "message": "Готово: добавлено {{ added }} из {{ rows.length }}" }
    }
  ]
}
