#!/usr/bin/env bash
set -euo pipefail
: "${TARGET_TOKEN:?TARGET_TOKEN is required}"
base=https://gstyj.kxunpt.cn
header=(-H "token: $TARGET_TOKEN" -H "Cookie: _token=$TARGET_TOKEN" -H 'Referer: https://gstyj.kxunpt.cn/dist/')
declare -A cate=(
  [1]=33D6C92D-5F1F-28D2-30BC-79FB859932FE
  [2]=1B99F99E-02CF-E392-8B9D-584008BA6136
  [3]=50079201-C7D9-04EA-E054-C83C5077AE96
  [10]=F0B2EE63-EB5C-FCAF-0EE1-26F320B4C866
  [11]=7E69A4B5-83A4-3546-AD94-F038D786C21B
  [12]=45C51D37-A8C8-8B54-EA49-06E4DD56A504
  [13]=18D6421E-3204-93CE-82D9-28D05CFFD2A5
  [14]=51325397-9B1E-65B2-CF00-8B9A1F28E7C7
)
declare -A expected=([1]=29 [2]=11 [3]=598 [10]=2 [11]=8 [12]=8 [13]=3 [14]=9)
for type in 1 2 3 10 11 12 13 14; do
  json=$(curl -fsS -X POST "$base/p/equipment_record/phyLists" "${header[@]}" -d "page=1&page_size=1&cate_uuid=${cate[$type]}&name=&start_date=&end_date=&department_uuid_str=&staff_uuid_str=")
  code=$(jq -r '.code' <<<"$json"); total=$(jq -r '.data.total' <<<"$json"); id=$(jq -r '.data.rows[0].id' <<<"$json"); row_type=$(jq -r '.data.rows[0].type' <<<"$json")
  [[ "$code" == 0 && "$total" == "${expected[$type]}" && "$id" != null ]]
  detail=$(curl -fsS -X POST "$base/p/equipment_record/phyRead" "${header[@]}" -d "id=$id&type=$row_type")
  jq -e '.code==0 and .data!=null and .data!={}' >/dev/null <<<"$detail"
  jq -nc --arg type "$type" --arg total "$total" --arg id "$id" '{type:$type,total:$total,sample_id:$id,ok:true}'
done
