<?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="juReport" column="ju_report"/>
		<result property="juPlace1" column="ju_place1"/>
		<result property="juPlace2" column="ju_place2"/>
		<result property="juPosition" column="ju_position"/>
		<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="juDutiesCode" column="ju_duties_code"/>
		<result property="juMessage" column="ju_message"/>
		<result property="sign" column="sign"/>
	</resultMap>

	<!-- 평가 -->
	<select id="ReportJudge.selectJudgeCode" resultType="codeVO">
		<![CDATA[
			SELECT
				code1, code2, codenm, codenm_yak
			FROM
				TCODE
			WHERE
				CODE1 = 'DC05'
				AND CODE_USE_YN = 'Y'
			ORDER BY
				CODE2 ASC
		]]>
	</select>
	
	<select id="ReportJudge.selectOne" parameterType="int" resultMap="RptJudgeMap">
		<![CDATA[
			SELECT
				(SELECT usersign FROM TAGENT WHERE userid = ju_userid) AS sign,
				T.*
			FROM
				RPT_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="loginUserVO" resultType="memberVO">
		<![CDATA[
			SELECT
				A.*,
				(SELECT codenm FROM TCODE WHERE CODE1 = 'C001' AND CODE2 = A.PLACE1) AS place1Str,
				(SELECT codenm FROM TCODE WHERE CODE1 = PLACE1 AND CODE2 = A.PLACE2) AS place2Str,
				(SELECT codenm FROM TCODE WHERE CODE1 = 'C002' AND CODE2 = A.POSITION) AS positionStr,
				(SELECT codenm FROM TCODE WHERE CODE1 = 'C004' AND CODE2 = A.DUTIES) AS dutiesStr
			FROM Rpt_Agent_Approve R, TAGENT A
			WHERE R.Ap_Place1 = #{place1}
		]]>
			<choose>
				<when test="duties.equals('03')">
					AND R.Ap_Section_Id = A.USERID
				</when>
				<when test="duties.equals('04')">
					AND R.Ap_Head_Id = A.USERID
				</when>
				<otherwise>
					AND 1 = 0
				</otherwise>
			</choose>
	</select>
	
	<insert id="ReportJudge.insert" parameterType="reportJudgeVO">
		<![CDATA[
			INSERT INTO RPT_JUDGE_HISTORY 
				(JU_INSEQ, JU_GROUP_CODE, JU_CHKDATE, JU_JUDGE, JU_REPORT, JU_PLACE1, JU_PLACE2,
				JU_POSITION, JU_NAME, JU_USERID, JU_PROXY_ID, JU_DUTIES_CODE, JU_MESSAGE)
			VALUES
				(#{juInseq}, #{juGroupCode}, NOW(), #{juJudge}, #{juReport}, #{juPlace1}, #{juPlace2},
				#{juPosition}, #{juName}, #{juUserid}, #{juProxyId}, #{juDutiesCode}, #{juMessage})
		]]>
		<selectKey resultType="int" keyProperty="juInseq" order="AFTER">
	        SELECT RPT_JUDGE_HISTORY_JU_SEQ_SEQ.CURRVAL FROM DUAL
	    </selectKey>
	</insert>
	
	<update id="ReportJudge.update" parameterType="reportJudgeVO">
		<![CDATA[
			UPDATE 
				RPT_JUDGE_HISTORY
			SET 
				JU_JUDGE = #{juJudge}
			WHERE
				JU_SEQ = #{juSeq}
		]]>
	</update>
	
	
	<!-- 견문 통계 -->
	
	<!-- 분야별통계 -->
	<select id="ReportJudge.statsCategory" parameterType="reportSearchVO" resultType="hashMap">
		<![CDATA[
		/*ReportJudge.statsCategory*/
		SELECT 
		]]>
			<if test="categoryType.equals(1)">
				<foreach collection="category1" item="cate" separator="">
					MAX(CASE CATEGORYCODE WHEN #{cate} THEN TOTAL::int end) AS "${cate}",
				</foreach>
			</if>
			<if test="categoryType.equals(2)">
				<foreach collection="category2" item="cate" >
					MAX(CASE CATEGORYCODE WHEN #{cate} THEN TOTAL::int end) AS "${cate}",
				</foreach>
			</if>
			<if test="categoryType.equals(3)">
				<foreach collection="category3" item="cate" >
					MAX(CASE CATEGORYCODE WHEN #{cate} THEN TOTAL::int end) AS "${cate}",
				</foreach>
			</if>
			<if test="categoryType.equals(4)">
				<foreach collection="category4" item="cate" >
					MAX(CASE CATEGORYCODE WHEN #{cate} THEN TOTAL::int end) AS "${cate}",
			</foreach>
			</if>
		<![CDATA[
			PLACENAME, PLACECODE,
			SUM(TOTAL) AS TOTAL
		FROM
			(
				SELECT 
					COUNT(R.IN_PLACE_CODE) AS TOTAL, 
					T.CATEGORYNAME, T.CATEGORYCODE, 
					T.PLACENAME, T.PLACECODE
				FROM 
					(
						SELECT 
							* 
						FROM 
							RPT_INFO 
						WHERE 
							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}::date
					]]></if>
					<if test="dateEnd != null and dateEnd != ''"><![CDATA[
						AND IN_INFODATE <= #{dateEnd}::date
					]]></if>
					<if test="subject != null and subject != ''"><![CDATA[
						AND IN_SUBJECT LIKE '%'||#{subject}||'%'
					]]></if>
					
					<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="fullReport != null and fullReport.length > 0">
						AND 
						<foreach collection="fullReport" item="report" open="(" close=")" separator=" OR ">
							IN_REPORT${report} = 1
						</foreach>
					</if>	
		<![CDATA[
					) R 
					RIGHT OUTER JOIN 
					(
						SELECT
							RC.CODE AS CATEGORYCODE, RC.NAME AS CATEGORYNAME, 
							C.CODE2 AS PLACECODE, C.CODENM AS PLACENAME
						FROM 
							RPT_CATEGORY RC, 
							TCODE C
						WHERE 
							C.CODE1 = 'C001' AND 
							RC.TYPE = #{categoryType}
		]]>
						<if test="places != null and places.length > 0">
							AND C.CODE2 IN
							<foreach collection="places" item="place" open="(" close=")" separator=",">
								#{place}
							</foreach>
						</if>
						<if test="categoryType.equals(1) and searchCate1 and category1 != null and category1.length > 0">
							AND RC.CODE IN
							<foreach collection="category1" item="category" open="(" close=")" separator=",">
								#{category}
							</foreach>
						</if>
						<if test="categoryType.equals(2) and searchCate2 and category2 != null and category2.length > 0">
							AND RC.CODE IN
							<foreach collection="category2" item="category" open="(" close=")" separator=",">
								#{category}
							</foreach>
						</if>
						<if test="categoryType.equals(3) and searchCate3 and category3 != null and category3.length > 0">
							AND RC.CODE IN
							<foreach collection="category3" item="category" open="(" close=")" separator=",">
								#{category}
							</foreach>
						</if>
						<if test="categoryType.equals(4) and searchCate4 and category4 != null and category4.length > 0">
							AND RC.CODE IN
							<foreach collection="category4" item="category" open="(" close=")" separator=",">
								#{category}
							</foreach>
						</if>
		<![CDATA[
					) T 
					ON R.IN_PLACE_CODE = T.PLACECODE
		]]>
			<choose>
				
				<when test="categoryType.equals(2)"><![CDATA[
					AND R.IN_CATEGORY2 = T.CATEGORYCODE
				]]></when>
				<when test="categoryType.equals(1)"><![CDATA[
					AND R.IN_CATEGORY1 = T.CATEGORYCODE
				]]></when>
				<when test="categoryType.equals(3)"><![CDATA[
					AND R.IN_CATEGORY3 = T.CATEGORYCODE
				]]></when>
				<when test="categoryType.equals(4)"><![CDATA[
					AND R.IN_CATEGORY4 = T.CATEGORYCODE
				]]></when>
				<otherwise><![CDATA[
					AND 1=0
				]]></otherwise>
			</choose>	
		<![CDATA[
				GROUP BY 
					T.CATEGORYNAME, T.PLACENAME, T.PLACECODE, T.CATEGORYCODE
			) STATS
		GROUP BY PLACECODE, PLACENAME
		ORDER BY PLACECODE
		]]>
	</select>
	<!-- 평가별통계 -->
	<select id="ReportJudge.statsJudge" parameterType="reportSearchVO" resultType="hashMap">
	<![CDATA[
	/*ReportJudge.statsJudge*/
		WITH RPT AS (
			SELECT 	* 
			FROM
				RPT_INFO
			WHERE
				IN_COMPLETE = '1' 
				AND IN_STATE_CODE <> '99'
				AND IN_INFODATE >= #{dateStart}::date
				AND IN_INFODATE <= #{dateEnd}::date
	]]>
			<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 + JU2)) 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>