<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Equip">

	<resultMap id="resultMap" type="hashmap">
	    <result property="EU_DETAIL" column="EU_DETAIL" jdbcType="CLOB" javaType="java.lang.String" />
	</resultMap>

	<resultMap id="useStateResult" type="useStateVO">
		<result property="police" column="EU_POLICE" />
		<result property="policeStr" column="EU_POLICE_NM" />
		<result property="year" column="EU_YEAR" />
		<result property="quater" column="EU_QUATER" />
		<result property="code" column="EU_CODE" />
		<result property="codeStr" column="EU_CODE_NM" />
		<result property="cnt" column="EU_CNT" />
		<result property="detail" column="EU_DETAIL" />
		<result property="state" column="EU_STATE" />
		<result property="check" column="EU_CHECK" />
		<result property="approvalSeq" column="ESJ_SEQ" />
		<result property="approvalDate" column="ESJ_CHECKDATE" />
		<result property="regdate" column="EU_REGDATE" />
		<result property="writer" column="EU_WRITER" />
	</resultMap>
	<resultMap id="equipResult" type="equipVO">
		<result property="serNo" column="SERNO" />
		<result property="num" column="NUM" />
		<result property="police" column="POLICE" />
		<result property="policeStr" column="POLICE_STR" />
		<result property="equipType" column="EQUIPGUBUN" />
		<result property="equipTypeName" column="EQUIPGUBUN_STR" />
		<result property="equipName" column="EQUIPNAME" />
		<result property="buyYear" column="BYEAR" />
		<result property="buyMonth" column="BMON" />
		<result property="buyDay" column="BDAY" />
		<result property="buyDate" column="BDATE" />
		<result property="useTerm" column="USETERM" />
		<result property="standard" column="STANDARD" />
		<result property="purpose" column="PURPOSE" />
		<result property="buyPlace" column="BPLACE" />
		<result property="price" column="PRICE" />
		<result property="madeIn" column="MADEIN" />
		<result property="serialNo" column="SERIALNO" />
		<result property="useSection" column="USESECTION" />
		<result property="operate" column="OPERATE" />
		
		<result property="bigo" column="BIGO" />
		<result property="fileName1" column="FILENAME1" />
		<result property="fileName2" column="FILENAME2" />
		<result property="fileName3" column="FILENAME3" />
		<result property="createDate" column="CREADATE" />
		<result property="logDate" column="LOGDATE" />		
	</resultMap>

	<select id="Equip.getEquipmentList" parameterType="hashmap" resultType="hashmap">
		SELECT
			'계' AS EQUIP_NM, COUNT(EQ.POLICE) AS TOTAL, CAST('00' AS INT) AS ORDER_VALUE, '00' AS EQUIP,
		<foreach collection="place1" item="item" separator=",">
	    	SUM(CASE POLICE WHEN #{item.code2} THEN 1 ELSE 0 END) AS ${item.code2},
	    	SUM(CASE POLICE WHEN 'ps18' THEN 1 ELSE 0 END) AS ps18,
			SUM(CASE POLICE WHEN 'ps90' THEN 1 ELSE 0 END) AS ps90
	 	</foreach>	 
		FROM
		  TEQUIPMENT EQ
		  RIGHT JOIN
		  (
		    SELECT 
		      *
		    FROM
		      TCODE
		    WHERE 
		      CODE1 = 'EQUI' AND 
		      CODE_USE_YN='Y' AND 
		      CODE2 NOT IN ('PS95','PS96','PS97','PS99', 'STAT')
		  ) TD
		  ON 
		  	EQ.EQUIPGUBUN = TD.CODE2
		UNION
			SELECT
		   		TD.CODENM AS EQUIP_NM, COUNT(EQ.POLICE) AS TOTAL, CAST(TD.CODE2 AS INT) AS ORDER_VALUE, TD.CODE2 AS EQUIP,
			<foreach collection="place1" item="item" separator=",">
	    	SUM(CASE POLICE WHEN #{item.code2} THEN 1 ELSE 0 END) AS ${item.code2},
	    	SUM(CASE POLICE WHEN 'ps18' THEN 1 ELSE 0 END) AS ps18,
			SUM(CASE POLICE WHEN 'ps90' THEN 1 ELSE 0 END) AS ps90
		 	</foreach>	 
			FROM
		  		TEQUIPMENT EQ 
			  RIGHT JOIN
			  (
			    SELECT 
			      *
			    FROM
			      TCODE
			    WHERE 
			      CODE1 = 'EQUI' AND 
			      CODE_USE_YN='Y' AND 
			      CODE2 NOT IN ('PS95','PS96','PS97','PS99', 'STAT')
			  ) TD
			  ON 
			  	EQ.EQUIPGUBUN = TD.CODE2
			GROUP BY 
				EQ.EQUIPGUBUN, TD.CODENM, TD.CODE2
			ORDER BY 
				ORDER_VALUE
	</select>
	
	
	<select id="Equip.getEquipUseStateList" parameterType="hashmap" resultType="hashmap">
	<![CDATA[
		SELECT
			1 AS CNT, '계' AS GUBUN, '' AS CODE,
			COALESCE(SUM(${type}), 0) AS TOTAL,
	]]>
		<foreach collection="place1" item="item" separator=",">
			COALESCE(SUM(CASE EU_POLICE WHEN #{item.code2} THEN ${type} ELSE 0 END), 0) AS ${item.code2}
	 	</foreach>	 
	<![CDATA[ 
		FROM H3_EQUIP_USE_STATE
    WHERE EU_YEAR = (#{criteria.year}||'')
      AND EU_QUATER = (#{criteria.quater}||'')
		UNION
			SELECT
		   		2 AS CNT, B.CODENM AS GUBUN, B.CODE2 AS CODE,
    			COALESCE(SUM(${type}), 0) AS TOTAL,
		]]>
			<foreach collection="place1" item="item" separator=",">
				COALESCE(SUM(CASE EU_POLICE WHEN #{item.code2} THEN ${type} ELSE 0 END), 0) AS ${item.code2}
		 	</foreach>	
	 	<![CDATA[
			FROM
		    (
		      SELECT *
		      FROM H3_EQUIP_USE_STATE
		      WHERE EU_YEAR = (#{criteria.year}||'')
		        AND EU_QUATER = (#{criteria.quater}||'')
		    ) A
		    RIGHT JOIN
		      (
		        SELECT CODE2, CODENM
		        FROM TCODE
		        WHERE CODE1 = 'EQUI'
		          AND CODE_USE_YN = 'Y'
		        ORDER BY CODE2
		      ) B
		    ON A.EU_CODE = B.CODE2 
		GROUP BY CODE2, CODENM
		ORDER BY CNT, CODE
	]]>
	</select>
	
	<select id="Equip.getEquipYearList" parameterType="hashmap" resultType="hashmap">
	<![CDATA[
		SELECT 
			UYEAR 
		FROM 
			TEQUIP_USE 
		GROUP BY 
			UYEAR 
		ORDER BY 
			UYEAR ASC
	]]>
	</select>
	
	<select id="Equip.getEquipmentCodeList" parameterType="hashmap" resultType="hashmap">
	<![CDATA[
		SELECT 
          CODE2 AS CODE, CODENM AS GUBUN
        FROM 
          TCODE 
        WHERE 
          CODE1 = 'EQUI' AND 
          CODE_USE_YN = 'Y'
        ORDER BY
          CODE2
	]]>
	</select>
	
	<select id="Equip.getEquipUseStateInfo" parameterType="equipSearchVO" resultMap="resultMap">
	<![CDATA[
		SELECT
		  B.CODENM, B.CODE2, A.*
		FROM
		(
		  SELECT *
		  FROM H3_EQUIP_USE_STATE
		  WHERE EU_YEAR =  (#{year}||'')
		    AND EU_QUATER =  (#{quater}||'')
		    AND EU_POLICE =  #{police}
		) A
		RIGHT JOIN
		  (
		    SELECT CODE2, CODENM
		    FROM TCODE
		    WHERE CODE1 = 'EQUI'
		      AND CODE_USE_YN = 'Y'
		    ORDER BY CODE2
		) B
		ON A.EU_CODE = B.CODE2
		ORDER BY B.CODE2
	]]>
	</select>
	
	<select id="Equip.getEquipUseStateInfoByCode" parameterType="hashmap" resultType="hashmap">
		SELECT *
    FROM H3_EQUIP_USE_STATE
    WHERE EU_YEAR =  (#{year}||'')
      AND EU_QUATER =  (#{quater}||'')
      AND EU_POLICE =  #{police}
		  AND EU_CODE = #{code}
	</select>
	
	<insert id="Equip.insertEquipUseState" parameterType="hashmap">
		INSERT INTO 
			H3_EQUIP_USE_STATE (
				EU_POLICE, EU_YEAR, EU_QUATER,
				EU_CODE, EU_CNT, EU_DETAIL,
				EU_STATE, EU_CHECK, EU_REGDATE, EU_WRITER
		) VALUES (
				#{police}, #{year}, #{quater},
				#{code}, #{cnt}, #{detail},
				#{state}, #{check}, NOW(), #{writer}
		)
	</insert>
	
	<update id="Equip.updateEquipUseState" parameterType="hashmap">
		UPDATE 
			H3_EQUIP_USE_STATE 
		SET 
			EU_CNT = #{cnt}, EU_DETAIL = #{detail},
			EU_STATE = #{state}, EU_CHECK = #{check}
		WHERE
			EU_POLICE = #{police} AND EU_YEAR = #{year} AND
			EU_QUATER = #{quater} AND EU_CODE = #{code}
	</update>
	
	<!-- 사용실적 결재 -->
	<select id="selectStateWaitingList" parameterType="stateWaitingSearchVO" resultMap="useStateResult">
	<![CDATA[
		SELECT
			A.*, COALESCE(ESJ_SEQ, 0) AS ESJ_SEQ, ESJ_CHECKDATE,
		    (SELECT CODENM FROM TCODE WHERE CODE1 = 'C001' AND CODE2 = A.EU_POLICE) AS POLICE_NAME
		FROM    
			(SELECT EU_POLICE, EU_YEAR, EU_QUATER FROM H3_EQUIP_USE_STATE GROUP BY EU_POLICE, EU_YEAR, EU_QUATER) A 
		    LEFT OUTER JOIN  H3_EQUIP_APPROVAL_HISTORY B
		    	ON B.ESJ_POLICE = A.EU_POLICE AND B.ESJ_YEAR = A.EU_YEAR AND A.EU_QUATER = B.ESJ_QUATER
		WHERE
			1=1
	]]>
		<if test="police != null and police != ''">
			AND A.EU_POLICE = #{police}
		</if>
		<if test="year != null and year != ''">
			AND A.EU_YEAR = #{year}
		</if>
	<![CDATA[	
		ORDER BY 
			EU_QUATER
	]]>
	</select>
	
	
	<select id="getEquipList" parameterType="arrestSearchVO" resultMap="equipResult">
	<![CDATA[
	SELECT A.SERNO,
        A.NUM || '호' NUM,
        A.POLICE,
        B.CODENM POLICE_STR,
        A.EQUIPGUBUN,
        C.CODENM EQUIPGUBUN_STR,
        A.EQUIPNAME,
        A.BYEAR,
        A.BMON,
        A.BDAY,
        A.BYEAR || '-' || A.BMON || '-' || A.BDAY BDATE, 
        A.USETERM,
        A.STANDARD,
        A.PURPOSE,
        A.BPLACE,
        A.PRICE,
        A.MADEIN,
        A.SERIALNO,
        A.USESECTION,
        A.OPERATE,
        A.BIGO,
        A.FILENAME1,
        A.FILENAME2,
        A.FILENAME3,
        A.CREADATE,
        A.WRITER,
        A.LOGDATE
  FROM TEQUIPMENT A, TCODE B/* 배치 */, TCODE C 
 WHERE A.EQUIPGUBUN =  #{type} /**P*/
   AND (A.POLICE = B.CODE2 AND B.CODE1 = 'C001')
   AND (A.EQUIPGUBUN = C.CODE2 AND C.CODE1 = 'EQUI')
 ORDER BY A.POLICE, A.BYEAR, A.BMON, A.BDAY
			
		]]>			
	</select>
	
	<select id="getEquipment" parameterType="equipVO"
		resultMap="equipResult">
		SELECT A.SERNO,
        A.NUM,
        A.POLICE,
        B.CODENM POLICE_STR,
        A.EQUIPGUBUN,
        C.CODENM EQUIPGUBUN_STR,
        A.EQUIPNAME,
        A.BYEAR,
        A.BMON,
        A.BDAY,
        A.BYEAR || '-' || A.BMON || '-' || A.BDAY BDATE, 
        A.USETERM,
        A.STANDARD,
        A.PURPOSE,
        A.BPLACE,
        A.PRICE,
        A.MADEIN,
        A.SERIALNO,
        A.USESECTION,
        A.OPERATE,
        A.BIGO,
        A.FILENAME1,
        A.FILENAME2,
        A.FILENAME3,
        A.CREADATE,
        A.WRITER,
        A.LOGDATE
  FROM TEQUIPMENT A, TCODE B/* 배치 */, TCODE C 
 WHERE A.SERNO =  #{serNo} /**P*/
   AND (A.POLICE = B.CODE2 AND B.CODE1 = 'C001')
   AND (A.EQUIPGUBUN = C.CODE2 AND C.CODE1 = 'EQUI')
 ORDER BY A.POLICE, A.BYEAR, A.BMON, A.BDAY
	</select>
	
	<insert id="Equip.insertEquipment" parameterType="equipVO">
	<![CDATA[	
INSERT INTO TEQUIPMENT 
   (  SERNO,
      NUM,
      POLICE,
      EQUIPGUBUN,
      EQUIPNAME,
      BYEAR,BMON,
      BDAY,USETERM,                          
      STANDARD,
      PURPOSE,
      BPLACE,
      PRICE,
      MADEIN,
      SERIALNO,
      OPERATE,
      USESECTION,
      BIGO,
      FILENAME1,
      FILENAME2,
      FILENAME3,
      CREADATE,
      WRITER,
      LOGDATE) 
 	values (
       TEQUIPMENT_SERNO_SEQ.NEXTVAL,
       (CASE WHEN (SELECT MAX(NUM) FROM TEQUIPMENT WHERE POLICE = #{police}) IS NULL THEN 1 
            ELSE (SELECT MAX(NUM) + 1 FROM TEQUIPMENT WHERE POLICE = #{police}) END),
       #{police},
       #{equipType},
       #{equipName},
       #{buyYear},
       #{buyMonth},
       #{buyDay},
       #{useTerm},
       #{standard},
       #{purpose},
       #{buyPlace},
       #{price},
       #{madeIn},
       #{serialNo},
       #{operate},
       #{useSection},
       #{bigo},
       #{fileName1},
       #{fileName2},
       #{fileName3},
       TO_CHAR(NOW(), 'YYYYMMDDHH24MISS'),
       #{writer},
       NULL
     )
	]]>
		<selectKey resultType="String" keyProperty="serNo" order="AFTER">
	        SELECT TEQUIPMENT_SERNO_SEQ.CURRVAL FROM DUAL
	    </selectKey>
	</insert>
	
	<update id="updateEquipment" parameterType="arrestVO">
	<![CDATA[
		UPDATE
			TEQUIPMENT
		SET
			NUM = #{num},
			EQUIPGUBUN = #{equipType},
			EQUIPNAME = #{equipName},
			POLICE = #{police},
			BYEAR = #{buyYear},
			BMON = #{buyMonth},
			BDAY = #{buyDay},
			USETERM = #{useTerm},
			STANDARD = #{standard},
			PURPOSE = #{purpose},
			BPLACE = #{buyPlace},
			PRICE = #{price},
			MADEIN = #{madeIn},
			SERIALNO = #{serialNo},
			OPERATE = #{operate},
			USESECTION = #{useSection},
			BIGO = #{bigo},
			FILENAME1 = #{fileName1},
			FILENAME2 = #{fileName2},
			FILENAME3 = #{fileName3},
			LOGDATE = TO_CHAR(NOW(), 'YYYYMMDDHH24MISS')
		 WHERE 
		    SERNO = #{serNo}
			

			
			
			
	]]>
	</update>
	
	<delete id="deleteEquipment" parameterType="equipVO">
	<![CDATA[
		DELETE FROM TEQUIPMENT		   
		 WHERE SERNO = #{serNo}
	]]>
	</delete>
</mapper>
