<?xml version="1.0" encoding="UTF-8"?>
 
<queryMap desc="备料码基础管理">
    <query id="BLCodeManage.InitBLSTD" desc="获取标准代码、描述">
		<![CDATA[ 
		      SELECT T.STD_NAME,T.STD_CODE 
		      FROM COM_BASE_STD T
		      WHERE T.VALIDFLAG='1' 
		      AND (T.STDSTYLE='G' OR T.STDSTYLE='X')
		      ORDER BY T.STD_NAME
		]]>
	</query>
	<query id="BLCodeManage.getComBaseStdAlpha" desc="获取用途码基础数据">
		<![CDATA[ 
SELECT T.ALPHA_CODE, T.ALPHA_NAME, T.UNIT_DESC, T.DEPARTMENT_DESC, T.MEMO
  FROM COM_BASE_STD_ALPHA T
 WHERE T.STD_CODE = {0}
 ORDER BY T.ALPHA_CODE
		]]>
	</query>
	<query id="BLCodeManage.InitBLSTDTYPE" desc="获取标准类型代码、描述">
		<![CDATA[ 
		      SELECT T.STD_STYLE_DESC,T.STD_STYLE 
		      FROM COM_BASE_STD_STYLE T
		      WHERE T.VALIDFLAG='1' 
		      ORDER BY T.STD_STYLE 
		]]>
	</query>
	<query id="BLCodeManage.InitBLSTD_ALPHA" desc="获取ALPHA标准代码、描述">
		<![CDATA[ 
		      SELECT ALPHA_NAME STD_NAME, ALPHA_CODE STD_CODE 
		      FROM COM_BASE_STD_ALPHA T
		      WHERE T.VALIDFLAG='1' AND T.STD_CODE = '#STD_CODE#'
		]]>
	</query>
	 
	<query id="BLCodeManage.GetBLCode" desc="获取存在的备料码(可以对比max函数效率)">
		<![CDATA[ 
		    SELECT T.PSC_BL
            FROM 
            (SELECT PSC_BL FROM COM_MSC_BL  ORDER BY PSC_BL DESC ) T
             WHERE ROWNUM='1'
		]]> 
	</query>
	
	<query id="BLCodeManage.QUERY" desc="查询">
		<![CDATA[
SELECT 'False' CHK,
       A.PSC_BL,
       A.PRODUCCODE,
       A.PRODUCNAME,
       A.STD_CODE,
       A.STD_NAME,
       A.STEELCODE,
       A.STEELNAME,
       A.STD_STYLE,
       A.STD_STYLE_DESC,
       A.MODEL_CODE,
       A.MODEL_DESC,
       A.MIN_D,
       A.MIN_H,
       A.MIN_L,
       A.MAX_L,
       A.USE_CODE,
       A.USE_DESC,
       A.CREATE_NAME,
       TO_CHAR(A.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       A.UPDATE_NAME,
       TO_CHAR(A.UPDATE_TIME, 'YYYY-MM-DD HH24:MI:SS') UPDATE_TIME,
       A.DELETE_NAME,
       TO_CHAR(A.DELETE_TIME, 'YYYY-MM-DD HH24:MI:SS') DELETE_TIME,
       DECODE(A.VALIDFLAG, '1', '有效', '无效') VALIDFLAG,
       A.MEMO,
       DECODE(A.REVIEW_STATUS,
              '0',
              '待技术评审',
              '1',
              '技术评审中',
              '2',
              '评审通过',
              '3',
              '评审不通过',
              '未识别') REVIEW_STATUS,
       A.OUTS_BILLET_FL,
       A.OUTS_STL_PIPE_FL,
       B.BASENAME OUTS_BILLET_FL_DESC,
       C.BASENAME OUTS_STL_PIPE_FL_DESC,
       D.CRAFT_PATH,
       D.CRAFT_NO,
       E.ORD_COUNT,
       T3.EXE_STATUS PLN_STATUS,
       A.BL_LEVEL
  FROM COM_MSC_BL A
  LEFT JOIN COM_BASE_INFO B
    ON A.OUTS_BILLET_FL = B.BASECODE
  LEFT JOIN COM_BASE_INFO C
    ON A.OUTS_STL_PIPE_FL = C.BASECODE
  LEFT JOIN CRAFT_ORD_DESIGN D
    ON D.ORD_LN_PK = A.PSC_BL
   AND D.VALIDFLAG = '1'
  LEFT JOIN (SELECT COUNT(1) ORD_COUNT, CODE_JG_BL
               FROM (SELECT T1.CODE_JG_BL, T.ORDER_NO
                       FROM PLN_SALEORD_PROC  T,
                            PLN_PORDER_BL_REQ T1,
                            PLN_SALEORD       T2
                      WHERE T.ORD_LN_DLY_PK = T1.REQ_BL_ID
                        AND T.ORDER_SOURCE = '备料需求'
                        AND T2.ORD_LN_DLY_PK = T.ORD_LN_DLY_PK
                        AND (T.PLANSTATUS IN ('1', '2', '3') OR
                            T.PLANSTATUS IN ('4', '5') AND
                            TO_DATE(T2.ROLL_MONTH, 'YYYY-MM') =
                            TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM'), 'YYYY-MM'))
                      GROUP BY T1.CODE_JG_BL, T.ORDER_NO)
              GROUP BY CODE_JG_BL) E
    ON E.CODE_JG_BL = A.PSC_BL
   LEFT JOIN (SELECT MIN(T2.EXE_STATUS) EXE_STATUS, T2.CODE_JG_BL
               FROM PLN_GX_JG_PROP_S T2
               WHERE T2.CODE_JG IS NOT NULL
              group by t2.CODE_JG_BL) t3
    ON t3.CODE_JG_BL = A.PSC_BL
 WHERE UPPER(PRODUCNAME) || '&' LIKE '%' || UPPER('#PRODUCNAME#') || '%'
   AND UPPER(STD_NAME) || '&' LIKE '%' || UPPER('#STD_NAME#') || '%'
   AND UPPER(STEELNAME) || '&' LIKE '%' || UPPER('#STEELNAME#') || '%'
   AND UPPER(STD_STYLE_DESC) || '&' LIKE
       '%' || UPPER('#STD_STYLE_DESC#') || '%'
   AND UPPER(MODEL_DESC) || '&' LIKE '%' || UPPER('#MODEL_DESC#') || '%'
   AND A.VALIDFLAG IN ('1', '#VALIDFLAG#')
   AND DECODE('#STEELCODE#', '1', '1', STEELCODE) IN ('#STEELCODE2#')
   AND DECODE('#MIN_D#', NULL, '1', MIN_D) =
       DECODE('#MIN_D#', NULL, '1', '#MIN_D#')
   AND DECODE('#MIN_H#', NULL, '1', MIN_H) =
       DECODE('#MIN_H#', NULL, '1', '#MIN_H#')
   AND DECODE('#STD_CODE#', NULL, '1', STD_CODE) =
       DECODE('#STD_CODE#', NULL, '1', '#STD_CODE#')
   AND NVL2('#KEY_JG#', MIN_D, 1) >=
       NVL2('#KEY_JG#',
            (SELECT MIN_D_CP
               FROM COM_MSC_R_JG A, COM_MSC_JG B
              WHERE A.KEY_JG = '#KEY_JG#'
                AND B.CODE_JG = A.CODE_JG),
            -1)
   AND NVL2('#KEY_JG#', (MIN_D - 2 * MIN_H), -1) <=
       NVL2('#KEY_JG#',
            (SELECT MAX_D_CP
               FROM COM_MSC_R_JG A, COM_MSC_JG B
              WHERE A.KEY_JG = '#KEY_JG#'
                AND B.CODE_JG = A.CODE_JG),
            1)
		]]>
	</query>
	
	<query id="BLCodeManage.QUERY1" desc="界面BLCodeManage.cs查询">
		<![CDATA[
          SELECT 
         'False' CHK
        ,PSC_BL
        ,PRODUCCODE
        ,PRODUCNAME
        ,STD_CODE
        ,STD_NAME
        ,STEELCODE
        ,STEELNAME
        ,STD_STYLE
        ,STD_STYLE_DESC
        ,MODEL_CODE
        ,MODEL_DESC
        ,MIN_D
        ,MIN_H
        ,USE_CODE
        ,USE_DESC
        ,CREATE_NAME
        ,TO_CHAR(CREATE_TIME,'YYYY-MM-DD HH24:MI:SS') CREATE_TIME
        ,UPDATE_NAME
        ,TO_CHAR(UPDATE_TIME,'YYYY-MM-DD HH24:MI:SS') UPDATE_TIME
        ,DELETE_NAME
        ,TO_CHAR(DELETE_TIME,'YYYY-MM-DD HH24:MI:SS') DELETE_TIME
        ,DECODE(VALIDFLAG,'1','有效','无效') VALIDFLAG
        ,MEMO
        FROM COM_MSC_BL 
        WHERE VALIDFLAG ='1'
		]]>
	</query>
	
	<query id="BLCodeManage.queryJgRBl" desc="根据接箍代码查询与其关联的备料码.">
		<![CDATA[
        SELECT T.PSC_BL FROM COM_MSC_JG_R_BL T WHERE T.CODE_JG = ?
         AND T.CUST_ALPHA_NO = ? AND T.ALPHA_CODE = ?
         AND T.VALIDFLAG = 1
		]]>
	</query>
	
	<query id="BLCodeManage.QueryWithMsc" desc="冶金规范中的备料查询">
		<![CDATA[
SELECT 'False' CHK,
       T.MSC,
       T.GROUP_NUM,
       A.PSC_BL,
       A.PRODUCCODE,
       NVL(B.PRODUCNAME, A.PRODUCNAME) PRODUCNAME,
       A.STD_CODE,
       NVL(C.STD_NAME, A.STD_NAME) STD_NAME,
       A.STEELCODE,
       NVL(D.STEELNAME, A.STEELNAME) STEELNAME,
       A.STD_STYLE,
       NVL(F.STD_STYLE_DESC, A.STD_STYLE_DESC) STD_STYLE_DESC,
       A.MODEL_CODE,
       NVL(G.MODEL_DESC, A.MODEL_DESC) MODEL_DESC,
       A.MIN_D,
       A.MIN_H,
       A.USE_CODE,
       NVL(H.STD_NAME, A.USE_DESC) USE_DESC,
       A.CREATE_NAME,
       A.CREATE_TIME,
       A.UPDATE_NAME,
       A.UPDATE_TIME,
       A.DELETE_NAME,
       A.DELETE_TIME,
       A.VALIDFLAG,
       DECODE(A.VALIDFLAG, '1', '有效', '0', '无效', '未识别') VALIDFLAG_NAME,
       A.MEMO,
       '备料评审' REVIEW,
       DECODE(A.REVIEW_STATUS,
              '0',
              '待技术评审',
              '1',
              '技术评审中',
              '2',
              '技术评审通过',
              '3',
              '技术评审失败') REVIEW_STATUS,
       DECODE(I.VALIDFLAG,
              '0',
              '无效',
              '1',
              '生效',
              '2',
              '制定中',
              '3',
              '待审核',
              '4',
              '待审批',
              '未制定') CRAFT_STATUS,
       DECODE(L.DESIGN_STS, 'S', KK.UNIT_DESC, MM.UNIT_DESC) UNIT_DESC,
       T.MSC_SET,
       T.MSC_DESC_SET,
       A.BL_LEVEL
  FROM COM_MSC_R_JG T, COM_MSC_BL A
  LEFT JOIN SLM_ORD_DESIGN_BL J
    ON J.PSC_BL = A.PSC_BL
   AND J.VALIDFLAG = '1'
  LEFT JOIN (SELECT K.UNIT_DESC, K.DESGIN_KEY
               FROM SLM_ORD_DESIGN_STS_TKLG_LST K
              WHERE K.VALIDFLAG = '1'
                AND K.DESIGN_STS = 'S'
                AND K.SEQ_INDEX =
                    (SELECT MAX(KKK.SEQ_INDEX)
                       FROM SLM_ORD_DESIGN_STS_TKLG_LST KKK
                      WHERE KKK.DESGIN_KEY = K.DESGIN_KEY)) KK
    ON KK.DESGIN_KEY = J.DESGIN_KEY
  LEFT JOIN (SELECT MAX(M.UNIT_DESC) UNIT_DESC, M.DESGIN_KEY
               FROM SLM_ORD_DESIGN_STS_TKLG_LST M
              WHERE M.VALIDFLAG = '1'
                AND M.DESIGN_STS = 'F'
              GROUP BY M.DESGIN_KEY) MM
    ON MM.DESGIN_KEY = J.DESGIN_KEY
  LEFT JOIN SLM_ORD_DESIGN_STS_TKLG L
    ON L.DESGIN_KEY = J.DESGIN_KEY
   AND L.VALIDFLAG = '1'
  LEFT JOIN COM_BASE_PRODUCT B
    ON B.PRODUCCODE = A.PRODUCCODE
  LEFT JOIN COM_BASE_STD C
    ON C.STD_CODE = A.STD_CODE
  LEFT JOIN COM_BASE_STEEL D
    ON D.STEELCODE = A.STEELCODE
  LEFT JOIN COM_BASE_STD_STYLE F
    ON F.STD_STYLE = A.STD_STYLE
  LEFT JOIN COM_BASE_MODEL G
    ON G.MODEL_CODE = A.MODEL_CODE
  LEFT JOIN COM_BASE_STD H
    ON H.STD_CODE = A.USE_CODE
  LEFT JOIN CRAFT_ORD_DESIGN I
    ON I.ORD_LN_PK = A.PSC_BL
 WHERE A.PSC_BL = T.CODE_JG
   AND T.MSC = ?
   AND T.STOCK_STYLE = 'B'
   AND T.VALIDFLAG = 1
   AND NVL(I.CRAFT_SEQ, '1') =
       DECODE(I.CRAFT_SEQ,
              NULL,
              '1',
              (SELECT MAX(CRAFT_SEQ)
                 FROM CRAFT_ORD_DESIGN
                WHERE ORD_LN_PK = A.PSC_BL))
		]]>
	</query>
	
	<query id="BLCodeManage.ADD" desc="添加">
		<![CDATA[
DECLARE
  P_PRODUCCODE      COM_MSC_BL.PRODUCCODE%TYPE := '#PRODUCCODE#';
  P_PRODUCNAME      COM_MSC_BL.PRODUCNAME%TYPE := '#PRODUCNAME#';
  P_STD_CODE        COM_MSC_BL.STD_CODE%TYPE := '#STD_CODE#';
  P_STD_NAME        COM_MSC_BL.STD_NAME%TYPE := '#STD_NAME#';
  P_STEELCODE       COM_MSC_BL.STEELCODE%TYPE := '#STEELCODE#';
  P_STEELNAME       COM_MSC_BL.STEELNAME%TYPE := '#STEELNAME#';
  P_STD_STYLE       COM_MSC_BL.STD_STYLE%TYPE := '#STD_STYLE#';
  P_STD_STYLE_DESC  COM_MSC_BL.STD_STYLE_DESC%TYPE := '#STD_STYLE_DESC#';
  P_MODEL_CODE      COM_MSC_BL.MODEL_CODE%TYPE := '#MODEL_CODE#';
  P_MODEL_DESC      COM_MSC_BL.MODEL_DESC%TYPE := '#MODEL_DESC#';
  P_MIN_D           COM_MSC_BL.MIN_D%TYPE := '#MIN_D#';
  P_MIN_H           COM_MSC_BL.MIN_H%TYPE := '#MIN_H#';
  P_USE_CODE        COM_MSC_BL.USE_CODE%TYPE := '#USE_CODE#';
  P_USE_DESC        COM_MSC_BL.USE_DESC%TYPE := '#USE_DESC#';
  P_MEMO            COM_MSC_BL.MEMO%TYPE := '#MEMO#';
  P_MIN_L           COM_MSC_BL.MIN_L%TYPE := '#MIN_L#';
  P_MAX_L           COM_MSC_BL.MAX_L%TYPE := '#MAX_L#';
  P_DEPARTMENT_CODE COM_PSC.DEPARTMENT_CODE%TYPE := '#DEPARTMENT_CODE#';
  P_DEPARTMENT_DESC COM_PSC.DEPARTMENT_DESC%TYPE := '#DEPARTMENT_DESC#';
  P_UNIT_CODE       COM_PSC.UNIT_CODE%TYPE := '#UNIT_CODE#';
  P_UNIT_DESC       COM_PSC.UNIT_DESC%TYPE := '#UNIT_DESC#';
  P_NAME            COM_MSC_BL.CREATE_NAME%TYPE := '#CREATE_NAME#';
  P_PSC_BL          COM_MSC_BL.PSC_BL%TYPE := '#PSC_BL#';
  P_PSC             COM_PSC.PSC%TYPE := '#PSC#';
  P_PSC_DESC        COM_PSC.PSC_DESC%TYPE := '#PSC_DESC#';
  P_COUNT           NUMBER(3);
  P_PRODUC_HEAD     COM_PSC.PRODUC_HEAD%TYPE;
  P_PRODUC_TYPE     COM_PSC.PRODUC_TYPE%TYPE;
  P_PRODUC_STYLE    COM_PSC.PRODUC_STYLE%TYPE;
  P_PRODUC_DESC     COM_PSC.PRODUC_DESC%TYPE;
BEGIN
  INSERT INTO COM_MSC_BL
    (PRODUCCODE,
     PRODUCNAME,
     STD_CODE,
     STD_NAME,
     STEELCODE,
     STEELNAME,
     STD_STYLE,
     STD_STYLE_DESC,
     MODEL_CODE,
     MODEL_DESC,
     MIN_D,
     MIN_H,
     USE_CODE,
     USE_DESC,
     CREATE_TIME,
     MEMO,
     MIN_L,
     MAX_L,
     CREATE_NAME,
     PSC_BL,
     REVIEW_STATUS,
     OUTS_BILLET_FL,
     OUTS_STL_PIPE_FL,
     BL_LEVEL)
  VALUES
    (P_PRODUCCODE,
     P_PRODUCNAME,
     P_STD_CODE,
     P_STD_NAME,
     P_STEELCODE,
     P_STEELNAME,
     P_STD_STYLE,
     P_STD_STYLE_DESC,
     P_MODEL_CODE,
     P_MODEL_DESC,
     P_MIN_D,
     P_MIN_H,
     P_USE_CODE,
     P_USE_DESC,
     SYSDATE,
     P_MEMO,
     P_MIN_L,
     P_MAX_L,
     P_NAME,
     P_PSC_BL,
     '0',
     '#OUTS_BILLET_FL#',
     '#OUTS_STL_PIPE_FL#',
     '#BL_LEVEL#');

  SELECT COUNT(1)
    INTO P_COUNT
    FROM COM_PSC A
   WHERE DECODE(P_PRODUCCODE, NULL, '1', A.PRODUCCODE) = DECODE(P_PRODUCCODE, NULL, '1', P_PRODUCCODE)
     AND DECODE(P_PRODUCCODE, NULL, A.PRODUCCODE, NULL) IS NULL
     AND DECODE(P_STD_CODE, NULL, '1', A.STD_CODE) = DECODE(P_STD_CODE, NULL, '1', P_STD_CODE)
     AND DECODE(P_STD_CODE, NULL, A.STD_CODE, NULL) IS NULL
     AND DECODE(P_STEELCODE, NULL, '1', A.STEELCODE) = DECODE(P_STEELCODE, NULL, '1', P_STEELCODE)
     AND DECODE(P_STEELCODE, NULL, A.STEELCODE, NULL) IS NULL
     AND DECODE(P_STD_STYLE, NULL, '1', A.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
     AND DECODE(P_STD_STYLE, NULL, A.STD_STYLE, NULL) IS NULL;
     
  SELECT T.PRODUC_HEAD, T.PRODUC_TYPE, T.PRODUC_STYLE, T.PRODUC_DESC
    INTO P_PRODUC_HEAD, P_PRODUC_TYPE, P_PRODUC_STYLE, P_PRODUC_DESC
    FROM COM_BASE_PRODUCT T
   WHERE T.PRODUCCODE = P_PRODUCCODE;
   
  IF P_COUNT = 0 THEN
    INSERT INTO COM_PSC
      (PSC,
       PSC_DESC,
       PRODUCCODE,
       PRODUCNAME,
       STD_CODE,
       STD_NAME,
       STEELCODE,
       STEELNAME,
       STD_STYLE,
       STD_STYLE_DESC,
       NEW_PROC_CODE,
       NEW_PROC_DESC,
       DEPARTMENT_CODE,
       DEPARTMENT_DESC,
       UNIT_CODE,
       AUTOJUDGE,
       UNIT_DESC,
       CREATE_NAME,
       PRODUC_HEAD,
       PRODUC_TYPE,
       PRODUC_STYLE,
       PRODUC_DESC,
       CREATE_TIME,
       VALIDFLAG,
       MEMO)
    VALUES
      (P_PSC,
       P_PSC_DESC,
       P_PRODUCCODE,
       P_PRODUCNAME,
       P_STD_CODE,
       P_STD_NAME,
       P_STEELCODE,
       P_STEELNAME,
       P_STD_STYLE,
       P_STD_STYLE_DESC,
       '0',
       '',
       P_DEPARTMENT_CODE,
       P_DEPARTMENT_DESC,
       P_UNIT_CODE,
       '0',
       P_UNIT_DESC,
       P_NAME,
       P_PRODUC_HEAD,
       P_PRODUC_TYPE,
       P_PRODUC_STYLE,
       P_PRODUC_DESC,
       SYSDATE,
       DEFAULT,
       '');
  ELSE
    UPDATE COM_PSC B
       SET B.VALIDFLAG       = '1',
           B.PRODUC_HEAD     = P_PRODUC_HEAD,
           B.PRODUC_TYPE     = P_PRODUC_TYPE,
           B.PRODUC_STYLE    = P_PRODUC_STYLE,
           B.PRODUC_DESC     = P_PRODUC_DESC,
           B.DEPARTMENT_CODE = P_DEPARTMENT_CODE,
           B.DEPARTMENT_DESC = P_DEPARTMENT_DESC,
           B.UNIT_CODE = P_UNIT_CODE,
           B.UNIT_DESC = P_UNIT_DESC
     WHERE DECODE(P_PRODUCCODE, NULL, '1', B.PRODUCCODE) = DECODE(P_PRODUCCODE, NULL, '1', P_PRODUCCODE)
     AND DECODE(P_PRODUCCODE, NULL, B.PRODUCCODE, NULL) IS NULL
     AND DECODE(P_STD_CODE, NULL, '1', B.STD_CODE) = DECODE(P_STD_CODE, NULL, '1', P_STD_CODE)
     AND DECODE(P_STD_CODE, NULL, B.STD_CODE, NULL) IS NULL
     AND DECODE(P_STEELCODE, NULL, '1', B.STEELCODE) = DECODE(P_STEELCODE, NULL, '1', P_STEELCODE)
     AND DECODE(P_STEELCODE, NULL, B.STEELCODE, NULL) IS NULL
     AND DECODE(P_STD_STYLE, NULL, '1', B.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
     AND DECODE(P_STD_STYLE, NULL, B.STD_STYLE, NULL) IS NULL
     AND ROWNUM <= 1;
  END IF;
END;
		]]>
	</query>
	
	<query id="BLCodeManage.getPsc" desc="获取产品码">
		<![CDATA[
SELECT A.PSC, A.PSC_DESC
    FROM COM_PSC A
   WHERE DECODE('#PRODUCCODE#', NULL, '1', A.PRODUCCODE) = DECODE('#PRODUCCODE#', NULL, '1', '#PRODUCCODE#')
     AND DECODE('#PRODUCCODE#', NULL, A.PRODUCCODE, NULL) IS NULL
     AND DECODE('#STD_CODE#', NULL, '1', A.STD_CODE) = DECODE('#STD_CODE#', NULL, '1', '#STD_CODE#')
     AND DECODE('#STD_CODE#', NULL, A.STD_CODE, NULL) IS NULL
     AND DECODE('#STEELCODE#', NULL, '1', A.STEELCODE) = DECODE('#STEELCODE#', NULL, '1', '#STEELCODE#')
     AND DECODE('#STEELCODE#', NULL, A.STEELCODE, NULL) IS NULL
     AND DECODE('#STD_STYLE#', NULL, '1', A.STD_STYLE) = DECODE('#STD_STYLE#', NULL, '1', '#STD_STYLE#')
     AND DECODE('#STD_STYLE#', NULL, A.STD_STYLE, NULL) IS NULL
		]]>
	</query>
	
	<query id="BLCodeManage.getMaxPsc" desc="获取产品码">
		<![CDATA[
SELECT 'PSC' ||
           LPAD(TO_CHAR(TO_NUMBER(SUBSTR(NVL(MAX(PSC), 'PSC0000000'), 4, 7)) + 1),
                7,
                '0') MAX_VALUE
      FROM COM_PSC
		]]>
	</query>
	
	<query id="BLCodeManage.getComPsc" desc="获取产品码">
		<![CDATA[ 
SELECT * FROM COM_PSC A WHERE A.PSC = '#PSC#'
		]]>
	</query>
	
	<query id="BLCodeManage.getStdMscByPsc" desc="获取MSC">
		<![CDATA[ 
SELECT A.MSC, A.MSC_DESC
  FROM COM_MSC A, COM_PSC_R_MSC B
 WHERE B.PSC = '#PSC#'
   AND A.MSC = B.MSC
   AND A.MSC_SEQ IS NULL
		]]>
	</query>
	
	<query id="BLCodeManage.getMscByPscUseCode" desc="通过用途码和PSC查询MSC">
		<![CDATA[
SELECT T.MSC VALUE
  FROM COM_MSC T, COM_PSC_R_MSC A
 WHERE T.MSC = A.MSC
   AND A.PSC = '#PSC#'
   AND T.USE_CODE = '#USE_CODE#'
		]]>
	</query>
	
	<query id="BLCodeManage.UPDATE" desc="更新">
		<![CDATA[
DECLARE
  P_PRODUCCODE      COM_MSC_BL.PRODUCCODE%TYPE := '#PRODUCCODE#';
  P_PRODUCNAME      COM_MSC_BL.PRODUCNAME%TYPE := '#PRODUCNAME#';
  P_STD_CODE        COM_MSC_BL.STD_CODE%TYPE := '#STD_CODE#';
  P_STD_NAME        COM_MSC_BL.STD_NAME%TYPE := '#STD_NAME#';
  P_STEELCODE       COM_MSC_BL.STEELCODE%TYPE := '#STEELCODE#';
  P_STEELNAME       COM_MSC_BL.STEELNAME%TYPE := '#STEELNAME#';
  P_STD_STYLE       COM_MSC_BL.STD_STYLE%TYPE := '#STD_STYLE#';
  P_STD_STYLE_DESC  COM_MSC_BL.STD_STYLE_DESC%TYPE := '#STD_STYLE_DESC#';
  P_MODEL_CODE      COM_MSC_BL.MODEL_CODE%TYPE := '#MODEL_CODE#';
  P_MODEL_DESC      COM_MSC_BL.MODEL_DESC%TYPE := '#MODEL_DESC#';
  P_MIN_D           COM_MSC_BL.MIN_D%TYPE := '#MIN_D#';
  P_MIN_H           COM_MSC_BL.MIN_H%TYPE := '#MIN_H#';
  P_USE_CODE        COM_MSC_BL.USE_CODE%TYPE := '#USE_CODE#';
  P_USE_DESC        COM_MSC_BL.USE_DESC%TYPE := '#USE_DESC#';
  P_MEMO            COM_MSC_BL.MEMO%TYPE := '#MEMO#';
  P_MIN_L           COM_MSC_BL.MIN_L%TYPE := '#MIN_L#';
  P_MAX_L           COM_MSC_BL.MAX_L%TYPE := '#MAX_L#';
  P_NAME            COM_MSC_BL.CREATE_NAME%TYPE := '#CREATE_NAME#';
  P_PSC_BL          COM_MSC_BL.PSC_BL%TYPE := '#PSC_BL#';
  P_DEPARTMENT_CODE COM_PSC.DEPARTMENT_CODE%TYPE := '#DEPARTMENT_CODE#';
  P_DEPARTMENT_DESC COM_PSC.DEPARTMENT_DESC%TYPE := '#DEPARTMENT_DESC#';
  P_UNIT_CODE       COM_PSC.UNIT_CODE%TYPE := '#UNIT_CODE#';
  P_UNIT_DESC       COM_PSC.UNIT_DESC%TYPE := '#UNIT_DESC#';
  P_PSC             COM_PSC.PSC%TYPE := '#PSC#';
  P_PSC_DESC        COM_PSC.PSC_DESC%TYPE := '#PSC_DESC#';
  P_COUNT           NUMBER(3);
  P_PRODUC_HEAD     COM_PSC.PRODUC_HEAD%TYPE;
  P_PRODUC_TYPE     COM_PSC.PRODUC_TYPE%TYPE;
  P_PRODUC_STYLE    COM_PSC.PRODUC_STYLE%TYPE;
  P_PRODUC_DESC     COM_PSC.PRODUC_DESC%TYPE;
BEGIN
  UPDATE COM_MSC_BL
     SET PRODUCCODE     = P_PRODUCCODE,
         PRODUCNAME     = P_PRODUCNAME,
         STD_CODE       = P_STD_CODE,
         STD_NAME       = P_STD_NAME,
         STEELCODE      = P_STEELCODE,
         STEELNAME      = P_STEELNAME,
         STD_STYLE      = P_STD_STYLE,
         STD_STYLE_DESC = P_STD_STYLE_DESC,
         MODEL_CODE     = P_MODEL_CODE,
         MODEL_DESC     = P_MODEL_DESC,
         MIN_D          = P_MIN_D,
         MIN_H          = P_MIN_H,
         USE_CODE       = P_USE_CODE,
         USE_DESC       = P_USE_DESC,
         MEMO           = P_MEMO,
         MIN_L          = P_MIN_L,
         MAX_L          = P_MAX_L,
         UPDATE_NAME    = P_NAME,
         UPDATE_TIME    = SYSDATE,
         DESGIN_KEY     = '',
         REVIEW_STATUS  = DECODE(REVIEW_STATUS, '1', '1', '0'),
         OUTS_BILLET_FL = '#OUTS_BILLET_FL#',
         OUTS_STL_PIPE_FL = '#OUTS_STL_PIPE_FL#',
         BL_LEVEL = '#BL_LEVEL#'
   WHERE PSC_BL = P_PSC_BL
     AND VALIDFLAG = '1';

  SELECT COUNT(1)
    INTO P_COUNT
    FROM COM_PSC A
   WHERE DECODE(P_PRODUCCODE, NULL, '1', A.PRODUCCODE) = DECODE(P_PRODUCCODE, NULL, '1', P_PRODUCCODE)
     AND DECODE(P_PRODUCCODE, NULL, A.PRODUCCODE, NULL) IS NULL
     AND DECODE(P_STD_CODE, NULL, '1', A.STD_CODE) = DECODE(P_STD_CODE, NULL, '1', P_STD_CODE)
     AND DECODE(P_STD_CODE, NULL, A.STD_CODE, NULL) IS NULL
     AND DECODE(P_STEELCODE, NULL, '1', A.STEELCODE) = DECODE(P_STEELCODE, NULL, '1', P_STEELCODE)
     AND DECODE(P_STEELCODE, NULL, A.STEELCODE, NULL) IS NULL
     AND DECODE(P_STD_STYLE, NULL, '1', A.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
     AND DECODE(P_STD_STYLE, NULL, A.STD_STYLE, NULL) IS NULL;

  SELECT T.PRODUC_HEAD, T.PRODUC_TYPE, T.PRODUC_STYLE, T.PRODUC_DESC
    INTO P_PRODUC_HEAD, P_PRODUC_TYPE, P_PRODUC_STYLE, P_PRODUC_DESC
    FROM COM_BASE_PRODUCT T
   WHERE T.PRODUCCODE = P_PRODUCCODE;

  IF P_COUNT = 0 THEN
    INSERT INTO COM_PSC
      (PSC,
       PSC_DESC,
       PRODUCCODE,
       PRODUCNAME,
       STD_CODE,
       STD_NAME,
       STEELCODE,
       STEELNAME,
       STD_STYLE,
       STD_STYLE_DESC,
       NEW_PROC_CODE,
       NEW_PROC_DESC,
       DEPARTMENT_CODE,
       DEPARTMENT_DESC,
       UNIT_CODE,
       AUTOJUDGE,
       UNIT_DESC,
       PRODUC_HEAD,
       PRODUC_TYPE,
       PRODUC_STYLE,
       PRODUC_DESC,
       CREATE_NAME,
       CREATE_TIME,
       VALIDFLAG,
       MEMO)
    VALUES
      (P_PSC,
       P_PSC_DESC,
       P_PRODUCCODE,
       P_PRODUCNAME,
       P_STD_CODE,
       P_STD_NAME,
       P_STEELCODE,
       P_STEELNAME,
       P_STD_STYLE,
       P_STD_STYLE_DESC,
       '0',
       '',
       P_DEPARTMENT_CODE,
       P_DEPARTMENT_DESC,
       P_UNIT_CODE,
       '0',
       P_UNIT_DESC,
       P_PRODUC_HEAD,
       P_PRODUC_TYPE,
       P_PRODUC_STYLE,
       P_PRODUC_DESC,
       P_NAME,
       SYSDATE,
       DEFAULT,
       '');
  ELSE
    UPDATE COM_PSC B
       SET B.VALIDFLAG       = '1',
           B.PRODUC_HEAD     = P_PRODUC_HEAD,
           B.PRODUC_TYPE     = P_PRODUC_TYPE,
           B.PRODUC_STYLE    = P_PRODUC_STYLE,
           B.PRODUC_DESC     = P_PRODUC_DESC,
           B.DEPARTMENT_CODE = P_DEPARTMENT_CODE,
           B.DEPARTMENT_DESC = P_DEPARTMENT_DESC,
           B.UNIT_CODE = P_UNIT_CODE,
           B.UNIT_DESC = P_UNIT_DESC
     WHERE DECODE(P_PRODUCCODE, NULL, '1', B.PRODUCCODE) = DECODE(P_PRODUCCODE, NULL, '1', P_PRODUCCODE)
     AND DECODE(P_PRODUCCODE, NULL, B.PRODUCCODE, NULL) IS NULL
     AND DECODE(P_STD_CODE, NULL, '1', B.STD_CODE) = DECODE(P_STD_CODE, NULL, '1', P_STD_CODE)
     AND DECODE(P_STD_CODE, NULL, B.STD_CODE, NULL) IS NULL
     AND DECODE(P_STEELCODE, NULL, '1', B.STEELCODE) = DECODE(P_STEELCODE, NULL, '1', P_STEELCODE)
     AND DECODE(P_STEELCODE, NULL, B.STEELCODE, NULL) IS NULL
     AND DECODE(P_STD_STYLE, NULL, '1', B.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
     AND DECODE(P_STD_STYLE, NULL, B.STD_STYLE, NULL) IS NULL
     AND ROWNUM <= 1;
  END IF;
END;
		]]>
	</query>
	<query id="BLCodeManage.DELETE" desc="删除">
		<![CDATA[
DECLARE
  P_USER_NAME COM_MSC_BL.DELETE_NAME%TYPE := ?;
  P_PSC_BL    COM_MSC_BL.PSC_BL%TYPE := ?;
BEGIN
  UPDATE COM_MSC_BL
     SET DELETE_NAME = P_USER_NAME, DELETE_TIME = SYSDATE, VALIDFLAG = '0'
   WHERE PSC_BL = P_PSC_BL;
  UPDATE COM_MSC_R_JG T
     SET T.VALIDFLAG = '0'
   WHERE T.CODE_JG = P_PSC_BL
     AND T.STOCK_STYLE = 'B';
  UPDATE COM_MSC_JG_R_BL T SET T.VALIDFLAG = '0' WHERE T.PSC_BL = P_PSC_BL;
END;
		]]>
	</query>
	<query id="BLCodeManage.RESUME" desc="恢复">
		<![CDATA[ 
		   UPDATE COM_MSC_BL
           SET DELETE_NAME = '', DELETE_TIME = '', VALIDFLAG = '1'
           WHERE PSC_BL = ?
		]]>
	</query>
	
	<query id="BLCodeManage.isCompleteMsc" desc="是否是完整的msc">
		<![CDATA[ 
		   SELECT SUM(CNT) FROM(
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC A WHERE A.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?)
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_BACKLOG A WHERE A.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?)
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_PROCESS A WHERE A.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?)
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STATION A WHERE A.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?)
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STD_CIC A
			WHERE DECODE(?, NULL, '1', A.MSC) IN (SELECT DECODE(?, NULL, '1', MSC) FROM COM_PSC_R_MSC WHERE PSC = ? AND VALIDFLAG = '1')
			AND DECODE(?, NULL, '1', A.STD_CODE_NK) = DECODE(?, NULL, '1', ?)
			AND VALIDFLAG = '1'
			OR NOT EXISTS( SELECT 1 FROM COM_MSC_STATION T WHERE T.STATION_TYPE = '403201' 
				AND T.VALIDFLAG = '1'  AND T.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?))
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STD_PIC A
			WHERE DECODE(?, NULL, '1', A.MSC) IN (SELECT DECODE(?, NULL, '1', MSC) FROM COM_PSC_R_MSC WHERE PSC = ? AND VALIDFLAG = '1')
			AND DECODE(?, NULL, '1', A.STD_CODE_NK) = DECODE(?, NULL, '1', ?)
			AND VALIDFLAG = '1'
			OR NOT EXISTS( SELECT 1 FROM COM_MSC_STATION T WHERE T.STATION_TYPE = '403202'
				 AND T.VALIDFLAG = '1'  AND T.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?))
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STD_SPEC A
			WHERE DECODE(?, NULL, '1', A.MSC) IN (SELECT DECODE(?, NULL, '1', MSC) FROM COM_PSC_R_MSC WHERE PSC = ? AND VALIDFLAG = '1')
			AND DECODE(?, NULL, '1', A.STD_CODE) = DECODE(?, NULL, '1', ?)
			AND VALIDFLAG = '1'
			OR NOT EXISTS( SELECT 1 FROM COM_MSC_STATION T WHERE T.STATION_TYPE = '403203'
				 AND T.VALIDFLAG = '1'  AND T.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?))
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STD_DETECTION A
			WHERE DECODE(?, NULL, '1', A.MSC) IN (SELECT DECODE(?, NULL, '1', MSC) FROM COM_PSC_R_MSC WHERE PSC = ? AND VALIDFLAG = '1')
			AND DECODE(?, NULL, '1', A.STD_CODE_NK) = DECODE(?, NULL, '1', ?)
			AND VALIDFLAG = '1'
			OR NOT EXISTS( SELECT 1 FROM COM_MSC_STATION T WHERE T.STATION_TYPE = '403204'
				 AND T.VALIDFLAG = '1'  AND T.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?))
			UNION ALL
			SELECT DECODE(COUNT(1), 0, 0, 1) CNT FROM COM_MSC_STD_WATER A
			WHERE DECODE(?, NULL, '1', A.MSC) IN (SELECT DECODE(?, NULL, '1', MSC) FROM COM_PSC_R_MSC WHERE PSC = ? AND VALIDFLAG = '1')
			AND DECODE(?, NULL, '1', A.STD_CODE_NK) = DECODE(?, NULL, '1', ?)
			AND VALIDFLAG = '1'
			OR NOT EXISTS( SELECT 1 FROM COM_MSC_STATION T WHERE T.STATION_TYPE = '403205'
				 AND T.VALIDFLAG = '1'  AND T.MSC IN (SELECT MSC FROM COM_PSC_R_MSC WHERE PSC = ?))
			)
		]]>
	</query>
	
	<query id="BLCodeManage.Group" desc="分组">
		<![CDATA[ 
		UPDATE COM_MSC_R_JG T SET T.GROUP_NUM = ? WHERE T.MSC = ? AND T.CODE_JG = ?
		]]>
	</query>
	
	<query id="BLCodeManage.isExistBl" desc="是否存在备料码">
		<![CDATA[
		SELECT COUNT(1)
		  FROM COM_MSC_BL T
		 WHERE DECODE(?, NULL, '1', T.PRODUCCODE) = DECODE(?, NULL, '1', ?)
		   AND DECODE(?, NULL, '1', T.STD_CODE) = DECODE(?, NULL, '1', ?)
		   AND DECODE(?, NULL, '1', T.STEELCODE) = DECODE(?, NULL, '1', ?)
		   AND NVL(T.STD_STYLE, '1') = NVL(?, '1')
		   AND DECODE(?, NULL, '1', T.MODEL_CODE) = DECODE(?, NULL, '1', ?)
		   AND DECODE(?, NULL, '1', T.MIN_D) = DECODE(?, NULL, '1', ?)
		   AND DECODE(?, NULL, '1', T.MIN_H) = DECODE(?, NULL, '1', ?)
		   AND DECODE(?, NULL, '1', T.USE_CODE) = DECODE(?, NULL, '1', ?)
		]]>
	</query>
	
	<query id="BLCodeManage.insertBlToPsc" desc="把备料码插入到产品表中">
		<![CDATA[ 
		DECLARE
		P_PSC COM_PSC.PSC%TYPE;
		P_PSC_DESC COM_PSC.PSC_DESC%TYPE;
		P_PRODUCCODE COM_PSC.PRODUCCODE%TYPE := ':PRODUCCODE:';
		P_PRODUCNAME COM_PSC.PRODUCNAME%TYPE := ':PRODUCNAME:';
		P_STD_CODE COM_PSC.STD_CODE%TYPE := ':STD_CODE:';
		P_STD_NAME COM_PSC.STD_NAME%TYPE := ':STD_NAME:';
		P_STEELCODE COM_PSC.STEELCODE%TYPE := ':STEELCODE:';
		P_STEELNAME COM_PSC.STEELNAME%TYPE := ':STEELNAME:';
		P_STD_STYLE COM_PSC.STD_STYLE%TYPE := ':STD_STYLE:';
		P_STD_STYLE_DESC COM_PSC.STD_STYLE_DESC%TYPE := ':STD_STYLE_DESC:';
		P_DEPARTMENT_CODE COM_PSC.DEPARTMENT_CODE%TYPE := ':DEPARTMENT_CODE:';
		P_DEPARTMENT_DESC COM_PSC.DEPARTMENT_DESC%TYPE := ':DEPARTMENT_DESC:';
		P_UNIT_CODE COM_PSC.UNIT_CODE%TYPE := ':UNIT_CODE:';
		P_UNIT_DESC COM_PSC.UNIT_DESC%TYPE := ':UNIT_DESC:';
		P_CREATE_NAME COM_PSC.CREATE_NAME%TYPE := ':CREATE_NAME:';
		P_COUNT NUMBER(3);
		P_VALIDFLAG VARCHAR2(1);
		P_PRODUC_HEAD COM_BASE_PRODUCT.PRODUC_HEAD%TYPE;
		BEGIN
		SELECT 'PSC' || LPAD(TO_CHAR(TO_NUMBER(SUBSTR(NVL(MAX(PSC), 'PSC0000000'), 4, 7)) + 1),7,'0') INTO P_PSC FROM COM_PSC;
		P_PSC_DESC := P_PRODUCNAME || ',' || P_STEELNAME || ',' || P_STD_STYLE_DESC || ',' || P_STD_NAME;
		SELECT T.PRODUC_HEAD INTO P_PRODUC_HEAD FROM COM_BASE_PRODUCT T WHERE T.PRODUCCODE = P_PRODUCCODE;
		SELECT COUNT(1) INTO P_COUNT FROM COM_PSC T WHERE T.PRODUCCODE = P_PRODUCCODE
		 AND T.STD_CODE = P_STD_CODE AND T.STEELCODE = P_STEELCODE 
		 AND DECODE(P_STD_STYLE, NULL, '1', T.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
		 AND DECODE(P_STD_STYLE, NULL, T.STD_STYLE, NULL) IS NULL;
		IF P_COUNT != 0 THEN
		   UPDATE COM_PSC T SET VALIDFLAG = '1' WHERE T.PRODUCCODE = P_PRODUCCODE
		 AND T.STD_CODE = P_STD_CODE AND T.STEELCODE = P_STEELCODE 
		 AND DECODE(P_STD_STYLE, NULL, '1', T.STD_STYLE) = DECODE(P_STD_STYLE, NULL, '1', P_STD_STYLE)
		 AND DECODE(P_STD_STYLE, NULL, T.STD_STYLE, NULL) IS NULL;
		ELSIF P_COUNT = 0 THEN
		   INSERT INTO COM_PSC(
		        PSC,
		        PSC_DESC,
		        PRODUCCODE,
		        PRODUCNAME,
		        STD_CODE,
		        STD_NAME,
		        STEELCODE,
		        STEELNAME,
		        STD_STYLE,
		        STD_STYLE_DESC,
		        DEPARTMENT_CODE,
		        DEPARTMENT_DESC,
		        UNIT_CODE,
		        UNIT_DESC,
		        NEW_PROC_CODE,
		        AUTOJUDGE,
		        CREATE_NAME,
		        PRODUC_HEAD,
		        CREATE_TIME,
		        VALIDFLAG)
		 VALUES( 
		        P_PSC,
		        P_PSC_DESC,
		        P_PRODUCCODE,
		        P_PRODUCNAME,
		        P_STD_CODE,
		        P_STD_NAME,
		        P_STEELCODE,
		        P_STEELNAME,
		        P_STD_STYLE,
		        P_STD_STYLE_DESC,
		        P_DEPARTMENT_CODE,
		        P_DEPARTMENT_DESC,
		        P_UNIT_CODE,
		        P_UNIT_DESC,
		        '0',
		        '0',
		        P_CREATE_NAME,
		        P_PRODUC_HEAD,
		        SYSDATE,
		        '1');
		END IF;
		END;
		]]>
	</query>
	
	<query id="BLCodeManage.queryPscCode" desc="根据品名、标准、钢级、标准类别查询产品规范码">
		<![CDATA[ 
		SELECT T.PSC
		  FROM COM_PSC T
		 WHERE 
		   DECODE('#PRODUCCODE#', NULL, '1', T.PRODUCCODE) = DECODE('#PRODUCCODE#', NULL, '1', '#PRODUCCODE#')
		   AND DECODE('#STD_CODE#', NULL, '1', T.STD_CODE) = DECODE('#STD_CODE#', NULL, '1', '#STD_CODE#')
		   AND DECODE('#STEELCODE#', NULL, '1', T.STEELCODE) = DECODE('#STEELCODE#', NULL, '1', '#STEELCODE#')
		   AND DECODE('#STD_STYLE#', NULL, '1',  'NULL', '1', T.STD_STYLE) = DECODE('#STD_STYLE#', NULL, '1', 'NULL', '1', '#STD_STYLE#')
		   AND DECODE('#STD_STYLE#', 'NULL', T.STD_STYLE, NULL) IS NULL
		   AND T.VALIDFLAG = '1'
		]]>
	</query>
	
	<query id="BLCodeManage.queryIsRef" desc="查询是否被引用">
		<![CDATA[ 
SELECT SUM(CNT)
  FROM (SELECT COUNT(1) CNT
          FROM COM_MSC_R_JG T
         WHERE T.CODE_JG = '#BL_CODE#'
           AND T.VALIDFLAG = '1'
        UNION ALL
        SELECT COUNT(1) CNT
          FROM COM_MSC_JG_R_BL A
         WHERE A.PSC_BL = '#BL_CODE#'
           AND A.VALIDFLAG = '1')
		]]>
	</query>
	
	<query id="BLCodeManage.queryMscBlByPk" desc="查询备料码通过主键">
		<![CDATA[ 
 SELECT 
       DESGIN_KEY,
       REVIEW_STATUS,
       PSC_BL,
       PRODUCCODE,
       PRODUCNAME,
       STD_CODE,
       STD_NAME,
       STEELCODE,
       STEELNAME,
       STD_STYLE,
       STD_STYLE_DESC,
       MODEL_CODE,
       MODEL_DESC,
       MIN_D,
       MIN_H,
       USE_CODE,
       USE_DESC,
       CREATE_NAME,
       TO_CHAR(CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       UPDATE_NAME,
       TO_CHAR(UPDATE_TIME, 'YYYY-MM-DD HH24:MI:SS') UPDATE_TIME,
       DELETE_NAME,
       TO_CHAR(DELETE_TIME, 'YYYY-MM-DD HH24:MI:SS') DELETE_TIME,
       VALIDFLAG,
       MEMO,
       MIN_L,
       MAX_L,
       FILE_JPG
 FROM COM_MSC_BL T
 WHERE T.PSC_BL = '#PSC_BL#'
		]]>
	</query>
	
	<query id="BLCodeManage.updateReviewStateByPk" desc="通过主键更新评审状态">
		<![CDATA[ 
UPDATE COM_MSC_BL T
   SET T.REVIEW_STATUS = '#REVIEW_STATUS#',
   T.DESGIN_KEY = DECODE('#DESGIN_KEY#', NULL, T.DESGIN_KEY, '#DESGIN_KEY#')
 WHERE T.PSC_BL = '#PSC_BL#'
		]]>
	</query>
	
	<query id="BLCodeManage.getBlDesignSts" desc="获取备料码的设计状态">
		<![CDATA[ 
SELECT A.DESIGN_STS VALUE
  FROM SLM_ORD_DESIGN_BL T, SLM_ORD_DESIGN_STS_TKLG A
 WHERE T.PSC_BL = '#PSC_BL#'
   AND T.VALIDFLAG = '1'
   AND T.DESGIN_KEY = A.DESGIN_KEY
   AND A.VALIDFLAG = '1'
		]]>
	</query>
	
	<query id="BLCodeManage.getRefCnt" desc="获取备料引用数目">
		<![CDATA[
SELECT SUM(CNT) FROM (
SELECT COUNT(1) CNT
  FROM COM_MSC_R_JG T
 WHERE T.CODE_JG = '#CODE_JG#'
   AND T.VALIDFLAG = '1'
UNION ALL
SELECT COUNT(1) CNT
  FROM COM_MSC_JG_R_BL A
 WHERE A.PSC_BL = '#PSC_BL#'
   AND A.VALIDFLAG = '1'
   )
		]]>
	</query>
	
	<query id="BLCodeManage.getPscByBl" desc="通过备料获取对应的PSC">
		<![CDATA[
SELECT T.PSC
  FROM COM_PSC T, COM_MSC_BL A
 WHERE A.PRODUCCODE = T.PRODUCCODE
   AND A.STD_CODE = T.STD_CODE
   AND NVL(A.STD_STYLE, '/') = NVL(T.STD_STYLE, '/')
   AND A.STEELCODE = T.STEELCODE
   AND A.PSC_BL = {0}
   AND T.VALIDFLAG = '1'
		]]>
	</query>
	
	<query id="BLCodeManage.getUseOrder" desc="查询使用该备料的合同">
		<![CDATA[
SELECT DISTINCT *
  FROM (SELECT F.ORDER_NO || '/' || E.ORDER_SEQ ORDER_NO,
               NVL((SELECT MIN(Q.UNIT_DESC)
                     FROM CRAFT_ORD_DESIGN_STS_TKLG_LST Q
                    WHERE Q.DESIGN_STS = 'F'
                      AND Q.VALIDFLAG = '1'
                      AND Q.DESGIN_KEY = D.DESGIN_KEY),
                   H.UNIT_DESC_PROCESS) UNIT_DESC,
               I.CRAFT_NAME,
               D.CRAFT_NO,
               D.CRAFT_PATH
          FROM CRAFT_ORD_DESIGN         A,
               CRAFT_ORD_DESIGN_MSC_BL  C,
               CRAFT_ORD_DESIGN         D,
               SLM_ORDER_LINE           E,
               SLM_ORDER_HEAD           F,
               CRAFT_ORD_DESIGN_MSC     G,
               COM_MSC                  H,
               CRAFT_ORD_DESIGN_APPOINT I
         WHERE A.ORD_LN_PK = {0}
           AND EXISTS (SELECT 1
                  FROM PLN_SALEORD B
                 WHERE B.ORD_LN_PK = D.ORD_LN_PK
                   AND B.PORDERSTATUS IN ('0', '1', '2', '3'))
           AND A.DESGIN_KEY = C.DESGIN_KEY_BL
           AND D.DESGIN_KEY = C.DESGIN_KEY
           AND E.ORD_LN_PK = D.ORD_LN_PK
           AND F.ORD_PK = E.ORD_PK
           AND G.DESGIN_KEY = D.DESGIN_KEY
           AND H.MSC = G.MSC
           AND I.ORD_LN_PK = D.ORD_LN_PK
        UNION ALL
        SELECT F.ORDER_NO || '/' || E.ORDER_SEQ,
               NVL((SELECT MIN(Q.UNIT_DESC)
                     FROM CRAFT_ORD_DESIGN_STS_TKLG_LST Q
                    WHERE Q.DESIGN_STS = 'F'
                      AND Q.VALIDFLAG = '1'
                      AND Q.DESGIN_KEY = D.DESGIN_KEY),
                   H.UNIT_DESC_PROCESS) UNIT_DESC,
               I.CRAFT_NAME,
               D.CRAFT_NO,
               D.CRAFT_PATH
          FROM CRAFT_ORD_DESIGN           A,
               CRAFT_ORD_DESIGN_MSC_JG_BL C,
               CRAFT_ORD_DESIGN           D,
               SLM_ORDER_LINE             E,
               SLM_ORDER_HEAD             F,
               CRAFT_ORD_DESIGN_MSC       G,
               COM_MSC                    H,
               CRAFT_ORD_DESIGN_APPOINT   I
         WHERE A.ORD_LN_PK = {0}
           AND EXISTS (SELECT 1
                  FROM PLN_SALEORD B
                 WHERE B.ORD_LN_PK = D.ORD_LN_PK
                   AND B.PORDERSTATUS IN ('0', '1', '2', '3'))
           AND A.DESGIN_KEY = C.DESGIN_KEY_BL
           AND D.DESGIN_KEY = C.DESGIN_KEY
           AND E.ORD_LN_PK = D.ORD_LN_PK
           AND F.ORD_PK = E.ORD_PK
           AND G.DESGIN_KEY = D.DESGIN_KEY
           AND H.MSC = G.MSC
           AND I.ORD_LN_PK = D.ORD_LN_PK)
 order by CRAFT_NO
		]]>
	</query>
</queryMap>