<?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="ReportJudge">

	<resultMap id="RptJudgeMap" type="reportJudgeVO">
		<id property="juSeq" column="ju_seq"/>
		<result property="juInseq" column="ju_inseq"/>
		<result property="juGroupCode" column="ju_group_code"/>
		<result property="juChkdate" column="ju_chkdate"/>
		<result property="juJudge" column="ju_judge"/>
		<result property="juPlace1" column="ju_place1"/>
		<result property="juPlace2" column="ju_place2"/>
		<result property="juDuty" column="ju_duty"/>
		<result property="juPositionStr" column="juPositionStr"/>
		<result property="juName" column="ju_name"/>
		<result property="juUserid" column="ju_userid"/>
		<result property="juProxyId" column="ju_proxy_id"/>
		<result property="juDutyCode" column="ju_duty_code"/>
		<result property="juMessage" column="ju_message"/>
	</resultMap>

	<select id="ReportJudge.selectOne" parameterType="int" resultMap="RptJudgeMap">
		<![CDATA[
			SELECT
				T.*
			FROM
				H2_REPORT_JUDGE_HISTORY T
			WHERE
				ju_seq = #{juseq}
		]]>
	</select>
	
	<select id="ReportJudge.getJudgeName" parameterType="String" resultType="String">
		<![CDATA[
			SELECT
				codenm
			FROM
				TCODE
			WHERE
				CODE1 = 'DC05' AND
				CODE2 = #{judgeCode}
		]]>
	</select>
	
	<select id="ReportJudge.selectApprove" parameterType="String" resultType="memberVO">
		SELECT 
			 T2.CC_CODE_NAME AS place1Str
			, T3.CC_CODE_NAME AS place2Str
			, T4.CC_CODE_NAME AS dutyStr
  			, T1.USR_ID  AS userid
  			, T1.USR_NAME AS name
  			, T1.USR_PWD AS passwd
  			, T1.USR_PWD_CHG loginchk
  			, T1.USR_DUTY duties
  			, T1.USR_PLACE1 AS place1
  			, T1.USR_PLACE2 AS place2
  			, T1.USR_LV USEGRADE
  			, T5.ROLE_LEVEL AS ROLELEVEL
		FROM  
  			H2_USER_INFO T1
  			LEFT JOIN H2_COMMON_CODE T2 ON T1.USR_PLACE1 = T2.CC_CODE 
			LEFT JOIN H2_COMMON_CODE T3 ON T1.USR_PLACE2 = T3.CC_CODE AND T1.USR_PLACE1 = T3.CC_GROUP
			LEFT JOIN H2_COMMON_CODE T4 ON T1.USR_DUTY = T4.CC_CODE AND T4.CC_GROUP = 'DUTY'
  			LEFT JOIN T_SSO_AUTH_CODE T5 ON T5.CODE_ID = T1.USR_LV 
  	WHERE  
		T1.USR_LV = #{usrLv}
	</select>
	
	<insert id="ReportJudge.insert" parameterType="reportJudgeVO" useGeneratedKeys="true" keyColumn="ju_seq" keyProperty="juSeq">
		<![CDATA[
			INSERT INTO H2_REPORT_JUDGE_HISTORY 
				(JU_INSEQ,  JU_CHKDATE, JU_JUDGE
				, JU_PLACE1, JU_PLACE2
				, JU_DUTY, JU_DUTY_CODE
				, JU_NAME, JU_USERID
				, JU_PROXY_ID
				, JU_GROUP_CODE, JU_MESSAGE
				)
			VALUES
				(#{juInseq},  now(), #{juJudge}
				, #{juPlace1}, #{juPlace2}
				, #{juDuty}, #{juDutyCode}
				, #{juName}, #{juUserid}
				, #{juProxyId}
				, #{juGroupCode}, #{juMessage})
		]]>
	</insert>
	
	<update id="ReportJudge.update" parameterType="reportJudgeVO">
		<![CDATA[
			UPDATE 
				H2_REPORT_JUDGE_HISTORY
			SET 
				JU_JUDGE = #{juJudge}
			WHERE
				JU_SEQ = #{juSeq}
		]]>
	</update>
	
	<select id="ReportJudge.selectPlace" parameterType="reportSearchVO" resultType="HashMap">
		SELECT
			CC_CODE_NAME AS PLACE_NAME ,
			CC_CODE AS PLACE
		FROM
			H2_COMMON_CODE
		WHERE
			CC_GROUP = 'PLA1'
		<if test="places != null and places.length > 0">
			AND CC_CODE IN
			<foreach collection="places" item="place" open="(" close=")" separator=",">
				#{place}
			</foreach>
		</if>
		ORDER BY CC_ORDER ASC
	</select>	
	
	
	
	<!-- 분야별통계 -->
	<select id="ReportJudge.statsCategory" parameterType="reportSearchVO" resultType="HashMap">
		SELECT 
			 IN_PLACE
			 <if test="categoryType.equals(1)">
			, IN_CATEGORY1
			</if>
			<if test="categoryType.equals(2)">
			, IN_CATEGORY2
			</if>
			<if test="categoryType.equals(3)">
			, IN_JUDGE
			</if>
			, COUNT(*) AS CNT
		FROM
			H2_REPORT_INFO
		WHERE
		<![CDATA[IN_COMPLETE = '1' AND IN_STATE_CODE <> '99']]>
			
			<choose>
			<when test="myGroupCode.equals(3)"></when>
			<when test="myGroupCode.equals(2)"><![CDATA[ AND IN_AGENCY_CODE = #{userPlace} ]]></when>
			<when test="myGroupCode.equals(1)"><![CDATA[ AND IN_PLACE_CODE = #{userPlace} ]]></when>
		</choose>
		
		<if test="dateStart != null and dateStart != ''"><![CDATA[
			AND IN_INFODATE >= #{dateStart}
		]]></if>
		<if test="dateEnd != null and dateEnd != ''"><![CDATA[
			AND IN_INFODATE <= #{dateEnd}
		]]></if>
		<if test="subject != null and subject != ''"><![CDATA[
			AND IN_SUBJECT LIKE '%'||#{subject}||'%'
		]]></if>
		<if test="judges != null and judges.length > 0">
			AND IN_JUDGE IN
			<foreach collection="judges" item="judge" open="(" close=")" separator=",">
				#{judge}
			</foreach>
		</if>
		<if test="category1 != null and category1.length > 0">
			AND IN_CATEGORY1 IN
			<foreach collection="category1" item="category" open="(" close=")" separator=",">
				#{category}
			</foreach>
		</if>
		<if test="category2 != null and category2.length > 0">
			AND IN_CATEGORY2 IN
			<foreach collection="category2" item="category" open="(" close=")" separator=",">
				#{category}
			</foreach>
		</if>
		<if test="categoryType.equals(1)">
		GROUP BY IN_PLACE, IN_CATEGORY1
		</if>
		<if test="categoryType.equals(2)">
		GROUP BY IN_PLACE, in_judge
		</if>
		<if test="categoryType.equals(3)">
		GROUP BY IN_PLACE, IN_JUDGE
		</if>
	</select>
	
	
	<!-- 평가별통계 -->
	<select id="ReportJudge.statsJudge" parameterType="reportSearchVO" resultType="hashMap">
	<![CDATA[
		WITH RPT AS (
			SELECT 	* 
			FROM
				RPT_INFO
			WHERE
				IN_COMPLETE = '1' 
				AND IN_STATE_CODE <> '99'
	]]>
			<if test="policeType != null and policeType.length > 0">
				AND IN_TYPE IN
				<foreach collection="policeType" item="type" open="(" close=")" separator=",">
					#{type}
				</foreach>
			</if>
			<if test="judges != null and judges.length > 0">
				AND IN_JUDGE IN
				<foreach collection="judges" item="judge" open="(" close=")" separator=",">
					#{judge}
				</foreach>
			</if>
			<if test="category1 != null and category1.length > 0">
				AND IN_CATEGORY1 IN
				<foreach collection="category1" item="category" open="(" close=")" separator=",">
					#{category}
				</foreach>
			</if>
			<if test="category2 != null and category2.length > 0">
				AND IN_CATEGORY2 IN
				<foreach collection="category2" item="category" open="(" close=")" separator=",">
					#{category}
				</foreach>
			</if>
			<if test="category3 != null and category3.length > 0">
				AND IN_CATEGORY3 IN
				<foreach collection="category3" item="category" open="(" close=")" separator=",">
					#{category}
				</foreach>
			</if>
			<if test="category4 != null and category4.length > 0">
				AND IN_CATEGORY4 IN
				<foreach collection="category4" item="category" open="(" close=")" separator=",">
					#{category}
				</foreach>
			</if>
	<![CDATA[
		)
		SELECT 
			A.PLACENAME,
			TO_CHAR(REPORT1) REPORT1, TO_CHAR(REPORT2) REPORT2,
			TO_CHAR(JU1) JU1, TO_CHAR(JU2) JU2, TO_CHAR(JU3) JU3, TO_CHAR(JU4) JU4, TO_CHAR(JU5) JU5, TO_CHAR(JU6) JU6, TO_CHAR(JU7) JU7, 
			TO_CHAR((JU1 + JU1)) AS AG, TO_CHAR((JU3 + JU4)) AS BG, TO_CHAR((JU5 + JU6)) AS CG, TO_CHAR(JU7) AS DG
		FROM 
			((
				SELECT
					PLACENAME, PLACECODE, IN_JUDGE, IN_SEQ
				FROM
					RPT R 
				RIGHT OUTER JOIN 
				(
					SELECT CODE2 AS PLACECODE, CODENM AS PLACENAME 
					FROM TCODE 
					WHERE CODE1 = 'C001'
	]]>
					<if test="places != null and places.length > 0">
						AND CODE2 IN
						<foreach collection="places" item="place" open="(" close=")" separator=",">
							#{place}
						</foreach>
					</if>
	<![CDATA[
				) C 
				ON C.PLACECODE = R.IN_PLACE_CODE
			) PIVOT (COUNT(IN_SEQ) FOR IN_JUDGE IN (1 JU1, 2 JU2, 3 JU3, 4 JU4, 5 JU5, 6 JU6, 7 JU7))) A,
			( 
				SELECT 
					PLACENAME,
					SUM(CASE WHEN IN_REPORT1 = 1 THEN 1 ELSE 0 END) AS REPORT1,
					SUM(CASE WHEN IN_REPORT2 = 1 THEN 1 ELSE 0 END) AS REPORT2
				FROM 
					RPT R 
				RIGHT OUTER JOIN 
				(
					SELECT CODE2 AS PLACECODE, CODENM AS PLACENAME 
					FROM TCODE 
					WHERE CODE1 = 'C001'
	]]>
				<if test="places != null and places.length > 0">
						AND CODE2 IN
					<foreach collection="places" item="place" open="(" close=")" separator=",">
						#{place}
					</foreach>
				</if>
	<![CDATA[
					) C 
					ON C.PLACECODE = R.IN_PLACE_CODE
				GROUP BY 
					PLACENAME, PLACECODE ) B
		WHERE A.PLACENAME = B.PLACENAME
		ORDER BY PLACECODE
	]]>		
	</select>
</mapper>