<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "文件上传（探伤报告、质检报告）">

<query id = "FileUpdae.getReportNo" desc = "生成探伤报告的报告编号">
<![CDATA[
   SELECT 'DIC' || ? || TO_CHAR(SYSDATE, 'YYYYMMDD') ||
       (SELECT LPAD(NVL(MAX(SUBSTR(T.REPORT_NO, 16)), 0) + 1, 8, 0) REPORT_NO
          FROM QCM_INSPECTION_REPORT T
         WHERE T.PLINE_CODE = ?
           AND TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD') =
               TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
  FROM DUAL
]]>
</query>

<query id = "FileUpdae.getReportNo2" desc = "生成表检报告的报告编号">
<![CDATA[
   SELECT 'DIC' || ? || TO_CHAR(SYSDATE, 'YYYYMMDD') ||
       (SELECT LPAD(NVL(MAX(SUBSTR(T.REPORT_NO, 16)), 0) + 1, 8, 0) REPORT_NO
          FROM QCM_SURFACE_REPORT T
         WHERE T.PLINE_CODE = ?
           AND TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD') =
               TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
  FROM DUAL
]]>
</query>

<query id = "FileUpdae.selectReportNo" desc = "查询探伤报告的报告编号">
<![CDATA[
    SELECT T.REPORT_NO
      FROM QCM_INSPECTION_REPORT T
     WHERE T.JUDGE_STOVE_NO = ?
       AND T.PRO_BC = ?
       AND T.PRO_BZ = ?
       AND T.PLINE_CODE = ?
]]>
</query>

<query id = "FileUpdae.selectReportNo2" desc = "查询表检报告的报告编号">
<![CDATA[
    SELECT T.REPORT_NO
      FROM QCM_SURFACE_REPORT T
     WHERE T.JUDGE_STOVE_NO = ?
       AND T.PRO_BC = ?
       AND T.PRO_BZ = ?
       AND T.PLINE_CODE = ?
       AND T.FOLLOW = ?
]]>
</query>

<query id = "FileUpdae.getReportNo1" desc = "生成表检报告的报告编号">
<![CDATA[
SELECT 'SUR' || ? ||
       (SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
               LPAD(NVL(MAX(SUBSTR(T.REPORT_NO, 16)), 0) + 1, 8, 0) REPORT_NO
          FROM QCM_SURFACE_REPORT T
         WHERE T.PLINE_CODE = ?
           AND TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD') =
               TO_CHAR(SYSDATE, 'YYYY-MM-DD')) REPORT_NO
  FROM DUAL
]]>
</query>

<query id = "FileUpdate.updateInspectionReport" desc = "上传探伤报告">
<![CDATA[
DECLARE
  V_REPORT_NO          QCM_INSPECTION_REPORT.REPORT_NO%TYPE := NULL;
  V_COUNT              MIL_PLAN.SAW_NUM%TYPE := NULL;
  V_JUDGE_STOVE_NO     QCM_INSPECTION_REPORT.JUDGE_STOVE_NO%TYPE := ?;
  V_PLINE_CODE         QCM_INSPECTION_REPORT.PLINE_CODE%TYPE := ?;
  V_PRO_BC             QCM_INSPECTION_REPORT.PRO_BC%TYPE := ?;
  V_PRO_BZ             QCM_INSPECTION_REPORT.PRO_BZ%TYPE := ?;
  V_CREATE_NAME        QCM_INSPECTION_REPORT.CREATE_NAME%TYPE := ?;
  V_DETECT_RESULT_PATH QCM_INSPECTION_REPORT.DETECT_RESULT_PATH%TYPE := ?;
  V_PRO_PLAN_ID        QCM_INSPECTION_REPORT.PRO_PLAN_ID%TYPE := ?;
  V_GX_PLAN_NO         QCM_INSPECTION_REPORT.GX_PLAN_NO%TYPE := ?;

BEGIN

  SELECT COUNT(1)
    INTO V_COUNT
    FROM QCM_INSPECTION_REPORT T
   WHERE T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
     AND T.PRO_BC = V_PRO_BC
     AND T.PRO_BZ = V_PRO_BZ
     AND NVL(T.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
     AND NVL(T.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
     AND T.PLINE_CODE = V_PLINE_CODE;

  IF V_COUNT > 0 THEN
  
    UPDATE QCM_INSPECTION_REPORT T
       SET T.UPDATE_NAME = V_CREATE_NAME, T.UPDATE_TIME = SYSDATE
     WHERE T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
       AND T.PRO_BC = V_PRO_BC
       AND T.PRO_BZ = V_PRO_BZ
       AND NVL(T.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
       AND NVL(T.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
       AND T.PLINE_CODE = V_PLINE_CODE;
  
  ELSE
    --生成主键 REPORT_NO
    SELECT 'DIC' || V_PLINE_CODE || TO_CHAR(SYSDATE, 'YYYYMMDD') ||
           (SELECT LPAD(NVL(MAX(SUBSTR(T.REPORT_NO, 16)), 0) + 1, 8, 0) REPORT_NO
              FROM QCM_INSPECTION_REPORT T
             WHERE T.PLINE_CODE = V_PLINE_CODE
               AND TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD') =
                   TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
      INTO V_REPORT_NO
      FROM DUAL;
  
    --插入新报告记录
    FOR MYROW IN (SELECT A.*,
                         (SELECT B.PLINE_NAME
                            FROM COM_BASE_PLINE B
                           WHERE B.PLINE_CODE = A.PLINE_CODE) PLINE_NAME,
                         SUBSTR(A.CONTRACT_NO,
                                0,
                                INSTR(A.CONTRACT_NO, '/', 1, 1) - 1) ORDER_NO,
                         SUBSTR(A.CONTRACT_NO,
                                INSTR(A.CONTRACT_NO, '/', 1, 1) + 1) ORDER_SEQ
                    FROM MIL_PLAN A
                   WHERE A.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO || '-01'
                     AND NVL(A.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
                     AND NVL(A.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
                     AND A.PLINE_CODE = V_PLINE_CODE) LOOP
    
      INSERT INTO QCM_INSPECTION_REPORT
        (REPORT_NO,
         LOT_NO,
         JUDGE_STOVE_NO,
         --BATCH_NO,
         PROCESS_CODE,
         PLINE_CODE,
         PLINE_NAME,
         CRAF_NO,
         ORDER_NO,
         ORDER_SEQ,
         ORD_LN_DLY_PK,
         PRO_ORDER_NO,
         --PRO_ORDER_ID,
         --GX_PLAN_NO,
         --DETECT_TYPE_CODE,
         --DETECT_TYPE_NAME,
         DETECT_RESULT_PATH,
         PRO_BC,
         PRO_BZ,
         VALIDFLAG,
         CREATE_NAME,
         CREATE_TIME,
         UPDATE_NAME,
         UPDATE_TIME,
         PRO_PLAN_ID,
         GX_PLAN_NO)
      VALUES
        (V_REPORT_NO,
         MYROW.LOT_NO,
         V_JUDGE_STOVE_NO,
         --?,
         'D',
         V_PLINE_CODE,
         MYROW.PLINE_NAME,
         MYROW.IMP_PROCESS,
         MYROW.ORDER_NO,
         MYROW.ORDER_SEQ,
         MYROW.ORD_LN_DLY_PK,
         MYROW.CONTRACT_NO,
         --?,
         --?,
         --?,
         --?,
         V_DETECT_RESULT_PATH,
         V_PRO_BC,
         V_PRO_BZ,
         '10',
         V_CREATE_NAME,
         SYSDATE,
         V_CREATE_NAME,
         SYSDATE,
         V_PRO_PLAN_ID,
         V_GX_PLAN_NO);
    END LOOP;
  END IF;
END;
]]>
</query>

<query id = "FileUpdate.updateSurfaceReport" desc = "上传表检报告">
<![CDATA[
DECLARE
  V_REPORT_NO        QCM_SURFACE_REPORT.REPORT_NO%TYPE := NULL;
  V_COUNT            MIL_PLAN.SAW_NUM%TYPE := NULL;
  V_JUDGE_STOVE_NO   QCM_SURFACE_REPORT.JUDGE_STOVE_NO%TYPE := ?;
  V_PLINE_CODE       QCM_SURFACE_REPORT.PLINE_CODE%TYPE := ?;
  V_PRO_BC           QCM_SURFACE_REPORT.PRO_BC%TYPE := ?;
  V_PRO_BZ           QCM_SURFACE_REPORT.PRO_BZ%TYPE := ?;
  V_CREATE_NAME      QCM_SURFACE_REPORT.CREATE_NAME%TYPE := ?;
  V_FACE_RESULT_PATH QCM_SURFACE_REPORT.FACE_RESULT_PATH%TYPE := ?;
  V_FOLLOW           QCM_SURFACE_REPORT.FOLLOW%TYPE := ?;
  V_PRO_PLAN_ID      QCM_SURFACE_REPORT.PRO_PLAN_ID%TYPE := ?;
  V_GX_PLAN_NO       QCM_SURFACE_REPORT.GX_PLAN_NO%TYPE := ?;

BEGIN

  SELECT COUNT(1)
    INTO V_COUNT
    FROM QCM_SURFACE_REPORT T
   WHERE T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
     AND NVL(T.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
     AND NVL(T.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
     AND T.PRO_BC = V_PRO_BC
     AND T.PRO_BZ = V_PRO_BZ
     AND T.PLINE_CODE = V_PLINE_CODE
     AND T.FOLLOW = V_FOLLOW;

  IF V_COUNT > 0 THEN
  
    UPDATE QCM_SURFACE_REPORT T
       SET T.UPDATE_NAME = V_CREATE_NAME, T.UPDATE_TIME = SYSDATE
     WHERE T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
       AND NVL(T.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
       AND NVL(T.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
       AND T.PRO_BC = V_PRO_BC
       AND T.PRO_BZ = V_PRO_BZ
       AND T.PLINE_CODE = V_PLINE_CODE
       AND T.FOLLOW = V_FOLLOW;
  
  ELSE
    --生成主键 REPORT_NO
    SELECT 'DIC' || V_PLINE_CODE || TO_CHAR(SYSDATE, 'YYYYMMDD') ||
           (SELECT LPAD(NVL(MAX(SUBSTR(T.REPORT_NO, 16)), 0) + 1, 8, 0) REPORT_NO
              FROM QCM_SURFACE_REPORT T
             WHERE T.PLINE_CODE = V_PLINE_CODE
               AND TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD') =
                   TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
      INTO V_REPORT_NO
      FROM DUAL;
  
    --插入新报告记录
    FOR MYROW IN (SELECT A.*,
                         (SELECT B.PLINE_NAME
                            FROM COM_BASE_PLINE B
                           WHERE B.PLINE_CODE = A.PLINE_CODE) PLINE_NAME,
                         SUBSTR(A.CONTRACT_NO,
                                0,
                                INSTR(A.CONTRACT_NO, '/', 1, 1) - 1) ORDER_NO,
                         SUBSTR(A.CONTRACT_NO,
                                INSTR(A.CONTRACT_NO, '/', 1, 1) + 1) ORDER_SEQ
                    FROM MIL_PLAN A
                   WHERE A.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO || '-01'
                     AND NVL(A.PRO_PLAN_ID, '/') = NVL(V_PRO_PLAN_ID, '/')
                     AND NVL(A.GX_PLAN_NO, '/') = NVL(V_GX_PLAN_NO, '/')
                     AND A.PLINE_CODE = V_PLINE_CODE) LOOP
    
      INSERT INTO QCM_SURFACE_REPORT
        (REPORT_NO,
         LOT_NO,
         JUDGE_STOVE_NO,
         --BATCH_NO,
         PROCESS_CODE,
         PLINE_CODE,
         PLINE_NAME,
         CRAF_NO,
         ORDER_NO,
         ORDER_SEQ,
         ORD_LN_DLY_PK,
         PRO_ORDER_NO,
         --PRO_ORDER_ID,
         --GX_PLAN_NO,
         --DETECT_TYPE_CODE,
         --DETECT_TYPE_NAME,
         FACE_RESULT_PATH,
         PRO_BC,
         PRO_BZ,
         VALIDFLAG,
         CREATE_NAME,
         CREATE_TIME,
         UPDATE_NAME,
         UPDATE_TIME,
         FOLLOW,
         PRO_PLAN_ID,
         GX_PLAN_NO)
      VALUES
        (V_REPORT_NO,
         MYROW.LOT_NO,
         V_JUDGE_STOVE_NO,
         --?,
         'D',
         V_PLINE_CODE,
         MYROW.PLINE_NAME,
         MYROW.IMP_PROCESS,
         MYROW.ORDER_NO,
         MYROW.ORDER_SEQ,
         MYROW.ORD_LN_DLY_PK,
         MYROW.CONTRACT_NO,
         --?,
         --?,
         --?,
         --?,
         V_FACE_RESULT_PATH,
         V_PRO_BC,
         V_PRO_BZ,
         '10',
         V_CREATE_NAME,
         SYSDATE,
         V_CREATE_NAME,
         SYSDATE,
         V_FOLLOW,
         V_PRO_PLAN_ID,
         V_GX_PLAN_NO);
    END LOOP;
  END IF;
END;
]]>
</query>

</queryMap>