-- AIDOPB UAT B2: minimal IQC / IPQC seed (tenant_id = 824585161322565)
-- Idempotent by business bill no prefix B-
-- Prerequisite: WP0-D4 qms_* tenant_id columns exist

SET @tid := 824585161322565;

-- S5 IQC application head
INSERT INTO qms_qcp_inspecapplyn (id, tenant_id, FBILLNO, FBILLTYPE, FAPPLYTIME, FCOMMENT)
SELECT 900000000000001, @tid, 'B-IQC-APP-001', 'UAT', NOW(), 'AIDOPB UAT seed'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM qms_qcp_inspecapplyn WHERE FBILLNO='B-IQC-APP-001' AND tenant_id=@tid);

-- S5 IQC application entry
INSERT INTO qms_qcp_insappnentry (id, tenant_id, glid, FMATERIALCFG, wlmc, FLOTNUMBER, FINSPECTSTATUS, FSUPPLIER, gysmc)
SELECT 900000000000002, @tid, 900000000000001, 'MAT-B-001', 'AIDOPB试验物料', 'LOT-B-001', '待检', 'SUP-B-01', 'AIDOPB试验供应商'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM qms_qcp_insappnentry WHERE id=900000000000002 AND tenant_id=@tid);

-- S5 IQC inspection bill
INSERT INTO qms_qcp_inspbill (
  id, tenant_id, FBILLNO, lydjbh, FMATERIALCFG, wlmc, gysmc, pch,
  FRINSQTY, jysl, dhsl, bhgsl, pd, clfs, FBILLSTATUS, FINSPECTORID, FCREATETIME
)
SELECT
  900000000000003, @tid, 'B-IQC-001', 'B-IQC-APP-001', 'MAT-B-001', 'AIDOPB试验物料', 'AIDOPB试验供应商', 'LOT-B-001',
  10, 10, 10, 0, 0, NULL, 'A', 'UAT', NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM qms_qcp_inspbill WHERE FBILLNO='B-IQC-001' AND tenant_id=@tid);

-- S6 IPQC head
INSERT INTO qms_gcjyd (id, tenant_id, djbh, cplx, scph, wlbm, wlmc, jyr, jgpd, status)
SELECT 900000000000011, @tid, 'B-IPQC-001', 'MODEL-B', 'PB-B-001', 'MAT-B-001', 'AIDOPB试验物料', 'UAT', 0, 'A'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM qms_gcjyd WHERE djbh='B-IPQC-001' AND tenant_id=@tid);

-- S6 IPQC detail
INSERT INTO qms_gcjydzb (id, tenant_id, glid, jyxm, jyyj, pd, jysl, bhgsl)
SELECT 900000000000012, @tid, 900000000000011, '外观', '目视', 0, 5, 0
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM qms_gcjydzb WHERE id=900000000000012 AND tenant_id=@tid);
