<?xml version="1.0" encoding="UTF-8"?>
 
<queryMap desc="包装要求">
   <query id="JGAndMSC.GetMSC" desc="获取冶金规范码">
		<![CDATA[ 
		      SELECT T.MSC,T.MSC_DESC
		      FROM COM_MSC T
		      WHERE T.VALIDFLAG!='D'   
		]]>
	</query>
	<query id="JGAndMSC.AndJG" desc="添加接箍">
		<![CDATA[ 
DECLARE
  P_MSC         COM_MSC_R_JG.MSC%TYPE := '#MSC#';
  P_MSC_DESC    COM_MSC_R_JG.MSC_DESC%TYPE := '#MSC_DESC#';
  P_CODE_JG     COM_MSC_R_JG.CODE_JG%TYPE := '#CODE_JG#';
  P_STOCK_STYLE COM_MSC_R_JG.STOCK_STYLE%TYPE := '#STOCK_STYLE#';
  P_NAME        COM_MSC_R_JG.CREATE_NAME%TYPE := '#CREATE_NAME#';
  P_ALPHA_CODE  COM_BASE_STD_ALPHA.ALPHA_CODE%TYPE;
  P_M_STD_NO    COM_MSC_R_JG.M_STD_NO%TYPE;
  P_W_STD_NO    COM_MSC_R_JG.W_STD_NO%TYPE;
  P_M_PRINT_NO  COM_MSC_R_JG.M_PRINT_NO%TYPE;
  P_W_PRINT_NO  COM_MSC_R_JG.W_PRINT_NO%TYPE;
  P_FILE_PATH   COM_MSC_R_JG.FILE_PATH%TYPE;
  P_JG_MEMO     COM_MSC_R_JG.JG_MEMO%TYPE;

BEGIN
  SELECT USE_CODE INTO P_ALPHA_CODE FROM COM_MSC WHERE MSC = P_MSC;
  SELECT A.M_STD_NO,
         A.W_STD_NO,
         A.M_PRINT_NO,
         A.W_PRINT_NO,
         A.FILE_PATH,
         A.MEMO
    INTO P_M_STD_NO,
         P_W_STD_NO,
         P_M_PRINT_NO,
         P_W_PRINT_NO,
         P_FILE_PATH,
         P_JG_MEMO
    FROM COM_MSC_JG A
   WHERE A.CODE_JG = '#CODE_JG#';
  INSERT INTO COM_MSC_R_JG
    (MSC,
     MSC_DESC,
     CODE_JG,
     STOCK_STYLE,
     CREATE_NAME,
     CREATE_TIME,
     CUST_ALPHA_NO,
     ORDER_SPECIAL_DESC,
     ALPHA_CODE,
     KEY_JG,
     DEMAND_DETECTION,
     DEMAND_BLAST,
     M_STD_NO,
     W_STD_NO,
     M_PRINT_NO,
     W_PRINT_NO,
     FILE_PATH,
     JG_MEMO)
  VALUES
    (P_MSC,
     P_MSC_DESC,
     P_CODE_JG,
     P_STOCK_STYLE,
     P_NAME,
     SYSDATE,
     '#CUST_ALPHA_NO#',
     '#ORDER_SPECIAL_DESC#',
     P_ALPHA_CODE,
     '#KEY_JG#',
     '40840102',
     '40840402',
     P_M_STD_NO,
     P_W_STD_NO,
     P_M_PRINT_NO,
     P_W_PRINT_NO,
     '#FILE_PATH#',
     P_JG_MEMO);
END;
		]]>
	</query>
	
	<query id="JGAndMSC.deleteJg" desc="删除接箍（以及接箍下面的交付标准和制程向下）">
		<![CDATA[ 
	 DECLARE
		BEGIN
		  FOR VROW IN (SELECT T.CODE_JG, T.MSC
		                 FROM COM_MSC_R_JG T
		                WHERE T.MSC = '#MSC#'
		                  AND T.STOCK_STYLE = '#STOCK_STYLE#'
		                  AND T.CODE_JG NOT IN ('#CODE_JGS#')) LOOP
		    DELETE FROM COM_MSC_R_JG T
		     WHERE T.MSC = VROW.MSC
		        AND T.STOCK_STYLE = '#STOCK_STYLE#'
		       AND T.CODE_JG = VROW.CODE_JG;
        DELETE FROM COM_MSC_STD_CHEM T WHERE T.CIC = 
        (SELECT A.CIC FROM COM_MSC_STD_CIC A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_CIC T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG;
        
        DELETE FROM COM_MSC_STD_PHY A WHERE A.PIC = 
        (SELECT A.PIC FROM COM_MSC_STD_PIC A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_PIC T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG;
        
        DELETE FROM COM_MSC_STD_DETECTION_C A WHERE A.DIC = 
        (SELECT A.DIC FROM COM_MSC_STD_DETECTION A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_DETECTION T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG; 
        
        DELETE FROM COM_MSC_STD_SPEC_C A WHERE A.SIC = 
        (SELECT A.SIC FROM COM_MSC_STD_SPEC A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_SPEC T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG; 
        
        DELETE FROM COM_MSC_STD_WATER_C A WHERE A.WIC = 
        (SELECT A.WIC FROM COM_MSC_STD_WATER A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_WATER T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG;
        
        DELETE FROM COM_MSC_STD_PROCESS_C A WHERE A.GIC = 
        (SELECT A.GIC FROM COM_MSC_STD_PROCESS A WHERE A.MSC = '#MSC#'
        AND A.CODE_JG = VROW.CODE_JG);
        DELETE FROM COM_MSC_STD_PROCESS T WHERE T.MSC = '#MSC#'
        AND T.CODE_JG = VROW.CODE_JG;
        
        --DELETE FROM COM_MSC_GRADE_R_SPEC T WHERE T.MSC = '#CODE_JG#';
        --DELETE FROM COM_MSC_GRADE T WHERE T.MSC = '#CODE_JG#'; 
        --DELETE FROM COM_MSC_STATION T WHERE T.MSC = '#CODE_JG#';
        --DELETE FROM COM_MSC_STATION T WHERE T.MSC = '#CODE_JG#';
        --DELETE FROM COM_MSC_BACKLOG T WHERE T.MSC = '#CODE_JG#';
      END LOOP;
    END;
		]]>
	</query>
	
	<query id="JGAndMSC.getBaseJgFile" desc="查询基础接箍文件">
		<![CDATA[ 
SELECT T.FILE_PATH VALUE FROM COM_MSC_JG T WHERE T.Code_Jg = {0}
		]]>
	</query>
	
	<query id="JGAndMSC.QUERY" desc="查询">
		<![CDATA[ 
SELECT A.CODE_JG
  FROM COM_MSC_R_JG A
 WHERE A.VALIDFLAG IN ('1', ?)
   AND A.MSC = ?
		]]>
	</query>
	
	<query id="JGAndMSC.queryMscRJg" desc="查询">
		<![CDATA[ 
SELECT A.CODE_JG
  FROM COM_MSC_R_JG A
 WHERE A.VALIDFLAG = '1'
   AND A.MSC = ?
   AND A.STOCK_STYLE = ?
		]]>
	</query>
	
	<query id="JGAndMSC.AndBL" desc="添加备料">
		<![CDATA[ 
DECLARE
  P_COUNT       NUMBER(5);
  P_MSC         COM_MSC_R_JG.MSC%TYPE := '#MSC#';
  P_MSC_DESC    COM_MSC_R_JG.MSC_DESC%TYPE := '#MSC_DESC#';
  P_CODE_JG     COM_MSC_R_JG.CODE_JG%TYPE := '#CODE_JG#';
  P_STOCK_STYLE COM_MSC_R_JG.STOCK_STYLE%TYPE := '#STOCK_STYLE#';
  P_NAME        COM_MSC_R_JG.CREATE_NAME%TYPE := '#CREATE_NAME#';

BEGIN
  SELECT COUNT(1)
    INTO P_COUNT
    FROM COM_MSC_R_JG
   WHERE MSC = P_MSC
     AND CODE_JG = P_CODE_JG;
  IF P_COUNT = 0 THEN
    INSERT INTO COM_MSC_R_JG
      (MSC, MSC_DESC, CODE_JG, STOCK_STYLE, CREATE_NAME, CREATE_TIME)
    VALUES
      (P_MSC, P_MSC_DESC, P_CODE_JG, P_STOCK_STYLE, P_NAME, SYSDATE);
  ELSE
    UPDATE COM_MSC_R_JG
       SET VALIDFLAG = '1'
     WHERE MSC = P_MSC
       AND CODE_JG = P_CODE_JG;
  END IF;
END;
		]]>
	</query>
	
	<query id="JGAndMSC.DeleteBL" desc="删除备料">
		<![CDATA[ 
		DELETE FROM COM_MSC_R_JG T WHERE T.MSC = '#MSC#' AND T.STOCK_STYLE = 'B'
		AND T.CODE_JG NOT IN (#CODE_JG_S#)
		]]>
	</query>
	
	<query id="JGAndMSC.DELETE" desc="删除">
		<![CDATA[
DECLARE
  P_KEY_JG COM_MSC_R_JG.KEY_JG%TYPE := ?;
BEGIN
  DELETE COM_MSC_R_JG WHERE KEY_JG = P_KEY_JG;
  DELETE COM_MSC_JG_R_BL WHERE KEY_JG = P_KEY_JG;
  DELETE COM_MSC_BACKLOG WHERE MSC = P_KEY_JG;
  DELETE COM_MSC_STD_CRAFT WHERE MSC = P_KEY_JG;
  DELETE COM_MSC_PROCESS WHERE MSC = P_KEY_JG;
  DELETE COM_MSC_STATION WHERE MSC = P_KEY_JG;
END;
		]]>
	</query>
	
	<query id="JGAndMSC.RESUME" desc="恢复">
		<![CDATA[ 
		 UPDATE COM_MSC_R_JG
		 SET VALIDFLAG='1',
		 DELETE_NAME='',
		 DELETE_TIME=''
		 WHERE MSC=? AND CODE_JG=?
		]]>
	</query>
	
	<query id="JGAndMSC.insertMscItem" desc="新增冶金规范项目">
		<![CDATA[
DECLARE
  P_ITEM_C    COM_MSC_ITEM.ITEM_C%TYPE;
  P_INDEX_SEQ COM_MSC_ITEM.INDEX_SEQ%TYPE := 0;
  P_COUNT     NUMBER(5);
BEGIN
  FOR VROW IN (SELECT BASECODE, BASENAME
                 FROM COM_BASE_INFO
                WHERE SORTCODE = '4030') LOOP
    P_ITEM_C    := VROW.BASECODE || '01';
    P_INDEX_SEQ := P_INDEX_SEQ + 1;
    
    SELECT COUNT(1)
      INTO P_COUNT
      FROM COM_MSC_ITEM A
     WHERE A.MSC = '#CODE_JG#'
       AND A.INDEX_SEQ = P_INDEX_SEQ;
       
    IF P_COUNT = 0 THEN
      INSERT INTO COM_MSC_ITEM T
        (T.MSC,
         T.INDEX_SEQ,
         T.ITEM,
         T.ITEM_C,
         T.CREATE_NAME,
         T.MEMO,
         T.CREATE_TIME,
         T.VALIDFLAG)
      VALUES
        ('#CODE_JG#',
         P_INDEX_SEQ,
         VROW.BASECODE,
         P_ITEM_C,
         '#CREATE_NAME#',
         '',
         SYSDATE,
         '1');
    ELSE
      UPDATE COM_MSC_ITEM A
         SET ITEM        = VROW.BASECODE,
             ITEM_C      = P_ITEM_C,
             UPDATE_NAME = '#CREATE_NAME#',
             UPDATE_TIME = SYSDATE,
             VALIDFLAG   = '1'
       WHERE A.MSC = '#CODE_JG#'
         AND A.INDEX_SEQ = P_INDEX_SEQ;
    END IF;
  END LOOP;
END;


		 ]]>
	</query> 
	
	<query id="JGAndMSC.DeleteMscItem" desc="删除冶金规范项目">
		<![CDATA[
		DECLARE
		BEGIN
		  FOR VROW IN (SELECT T.CODE_JG
		                 FROM COM_MSC_R_JG T
		                WHERE T.MSC = '#MSC#'
		                  AND T.STOCK_STYLE = '#STOCK_STYLE#') LOOP
		    DELETE FROM COM_MSC_ITEM WHERE MSC = VROW.CODE_JG;
		  END LOOP;
		END;
		 ]]>
	</query> 
	
	<query id="JGAndMSC.getGuid" desc="获取GUID">
		<![CDATA[
SELECT SYS_GUID() VALUE FROM DUAL
		 ]]>
	</query> 
	
	<query id="JGAndMSC.isRepeat" desc="是否重复">
		<![CDATA[
SELECT COUNT(1)
  FROM COM_MSC_R_JG T
 WHERE T.MSC = '#MSC#'
   AND NVL(T.ORDER_SPECIAL_DESC, '/') = NVL('#ORDER_SPECIAL_DESC#', '/')
   AND T.ALPHA_CODE =
       (SELECT USE_CODE FROM COM_MSC A WHERE A.MSC = '#MSC#')
   AND T.CODE_JG = '#CODE_JG#'
   AND T.DEMAND_DETECTION = '40840102'
   AND T.DEMAND_BONDERIZING IS NULL
   AND T.DEMAND_COPPER IS NULL
   AND T.DEMAND_BLAST = '40840402'
		 ]]>
	</query> 
</queryMap>