-- =============================================
-- 质量追溯测试数据（Board4看板）
-- 表：REPORT_QUALITY_INSPECTION
-- 说明：使用真实澳柯玛产品型号，合格率90%以上
-- Oracle 11 兼容版本
-- =============================================

-- =============================================
-- 步骤1：创建序列（如果不存在）
-- =============================================
BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE SEQ_REPORT_QUALITY_INSPECTION'; EXCEPTION WHEN OTHERS THEN NULL; END;
/

CREATE SEQUENCE SEQ_REPORT_QUALITY_INSPECTION
    START WITH 1
    INCREMENT BY 1
    NOCACHE
    NOCYCLE;

-- =============================================
-- 步骤2：清理现有测试数据（可选）
-- =============================================
-- DELETE FROM REPORT_QUALITY_INSPECTION WHERE MATERIAL_NAME IN ('BD-150NF', 'BC/BD-150HY(N)', 'BC/BD-150HY', 'BD-150AF', 'AF-150KF', 'BD-150HA', 'BC/BD-150HE', 'BD(C)-410T', 'BC/BD-252PDHNY', 'BC/BD-252PDHN');
-- COMMIT;

-- =============================================
-- 步骤3：插入质量追溯测试数据
-- 产品合格率设计（全部90%以上）：
--   1. BD-150NF - 100% (10/10)
--   2. BC/BD-150HY(N) - 100% (10/10)
--   3. BC/BD-150HY - 100% (10/10)
--   4. BD-150AF - 100% (10/10)
--   5. AF-150KF - 100% (10/10)
--   6. BD-150HA - 100% (10/10)
--   7. BC/BD-150HE - 100% (10/10)
--   8. BD(C)-410T - 100% (10/10)
--   9. BC/BD-252PDHNY - 100% (10/10)
--   10. BC/BD-252PDHN - 90% (9/10)
-- =============================================

-- 产品1: BD-150NF - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BD-150NF', 'P001', 'T001', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品2: BC/BD-150HY(N) - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BC/BD-150HY(N)', 'P002', 'T002', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品3: BC/BD-150HY - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BC/BD-150HY', 'P003', 'T003', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品4: BD-150AF - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BD-150AF', 'P004', 'T004', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品5: AF-150KF - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'AF-150KF', 'P005', 'T005', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品6: BD-150HA - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BD-150HA', 'P006', 'T006', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品7: BC/BD-150HE - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BC/BD-150HE', 'P007', 'T007', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品8: BD(C)-410T - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BD(C)-410T', 'P008', 'T008', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品9: BC/BD-252PDHNY - 100%合格 (10条全部合格)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BC/BD-252PDHNY', 'P009', 'T009', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 10;

-- 产品10: BC/BD-252PDHN - 90%合格 (9条合格 + 1条返修)
INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
SELECT SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC' || LPAD(LEVEL, 4, '0'), 'BC/BD-252PDHN', 'P010', 'T010', '3', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1 FROM DUAL CONNECT BY LEVEL <= 9;

INSERT INTO REPORT_QUALITY_INSPECTION (OBJ_ID, BAR_CODE, MATERIAL_NAME, PROCESS_CODE, TEST_ITEM_CODE, TREATMENT_MEASURE, INSPECTOR_TIME, IS_FLAG)
VALUES (SEQ_REPORT_QUALITY_INSPECTION.NEXTVAL, 'BC0010', 'BC/BD-252PDHN', 'P010', 'T010', '1', SYSDATE - DBMS_RANDOM.VALUE(0, 30), 1);

COMMIT;

-- =============================================
-- 步骤4：验证数据
-- =============================================
-- 查看各产品的质检统计
SELECT MATERIAL_NAME AS 产品名称,
       COUNT(*) AS 总数量,
       SUM(CASE WHEN TREATMENT_MEASURE = '3' THEN 1 ELSE 0 END) AS 合格数,
       SUM(CASE WHEN TREATMENT_MEASURE = '1' THEN 1 ELSE 0 END) AS 返修数,
       ROUND(SUM(CASE WHEN TREATMENT_MEASURE = '3' THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 0) AS 合格率
FROM REPORT_QUALITY_INSPECTION
WHERE MATERIAL_NAME IN ('BD-150NF', 'BC/BD-150HY(N)', 'BC/BD-150HY', 'BD-150AF', 'AF-150KF',
                        'BD-150HA', 'BC/BD-150HE', 'BD(C)-410T', 'BC/BD-252PDHNY', 'BC/BD-252PDHN')
GROUP BY MATERIAL_NAME
ORDER BY 合格率 DESC;

-- 查看插入的总记录数
SELECT COUNT(*) AS 总记录数 FROM REPORT_QUALITY_INSPECTION;

-- =============================================
-- 清理测试数据（如需删除，执行以下语句）
-- =============================================
-- DELETE FROM REPORT_QUALITY_INSPECTION
-- WHERE MATERIAL_NAME IN ('BD-150NF', 'BC/BD-150HY(N)', 'BC/BD-150HY', 'BD-150AF', 'AF-150KF',
--                        'BD-150HA', 'BC/BD-150HE', 'BD(C)-410T', 'BC/BD-252PDHNY', 'BC/BD-252PDHN');
-- COMMIT;
