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

	<select id="MemberAuth.getHead" parameterType="String" resultType="memberVO">
	<![CDATA[
		SELECT
		  *
		FROM
		  TAGENT A,
		  (SELECT 
		      *
		    FROM
		      RPT_AGENT_APPROVE
		    WHERE
		      AP_PLACE1 = #{place1}) B
		WHERE
		  A.USERID = B.AP_HEAD_ID
	]]>
	</select>
	
	<select id="MemberAuth.getSection" parameterType="String" resultType="memberVO">
	<![CDATA[
		SELECT
		  *
		FROM
		  TAGENT A,
		  (SELECT 
		      *
		    FROM
		      RPT_AGENT_APPROVE
		    WHERE
		      AP_PLACE1 = #{place1}) B
		WHERE
		  	A.USERID = B.AP_SECTION_ID
	]]>
	</select>
	
	<select id="MemberAuth.getMemberListByPlace1" parameterType="memberSearchVO" resultType="loginUserVO">
	<![CDATA[
		SELECT 
		  *
		FROM (
		  SELECT 
		    ROWNUM RNUM, MEMBER_LIST.*
		  FROM (
		    SELECT 
		      B.CODENM AS PLACE3STR, A.*
		    FROM
		      (
		      SELECT
		        B.CODENM AS PLACE2STR, A.*
		      FROM
		        (SELECT 
		          B.CODENM AS POSITIONSTR, A.*
		        FROM
		          (SELECT
		          B.CODENM AS PLACE1STR, A.*
		        FROM
		          (SELECT 
		            B.CODENM AS DUTIESSTR, A.*  
		          FROM 
		            (SELECT 
		                AGENT.USERID, AGENT.NAME, AGENT.PLACE1, AGENT.PLACE2, AGENT.PLACE3, AGENT.DUTIES, AGENT.POSITION, AGENT.USEGRADE,
		                PT_AUTH.AU_CODE AS AUTHCODE
		              FROM
		                ( TAGENT AGENT 
		                  LEFT JOIN
		                    RPT_AGENT_AUTH PT_AUTH
		                  ON AGENT.USERID = PT_AUTH.AU_ID) 
		                LEFT JOIN 
		                    H3_AGENT_AUTH ST_AUTH
		                  ON
		                    AGENT.USERID = ST_AUTH.AU_USERID
		              WHERE
		                PLACE1 = #{place1}
		            ) A,
		            TCODE B
		          WHERE
		            B.CODE1 = 'C004' AND
		            A.DUTIES = B.CODE2) A,
		          TCODE B
		        WHERE
		          A.PLACE1 = B.CODE2 AND
		            B.CODE1 = 'C001' ) A,
		          TCODE B
		        WHERE    
		          A.POSITION = B.CODE2 AND
		          B.CODE1 = 'C002') A,
		        TCODE B
		      WHERE    
		        A.PLACE1 = B.CODE1(+) AND
		        B.CODE2(+) = A.PLACE2
		      ORDER BY 
		        PLACE1, 
		        PLACE2, PLACE3, POSITION DESC, DUTIES, NAME
		    ) A,
		    TCODE B
		    WHERE
		      A.PLACE3 = B.CODE2(+) AND
		      CODE1(+) = 'C009'
		     ORDER BY]]>
		     <if test="orderkey != null and orderkey != ''">
				${orderkey} <choose><when test="ordertype == 1">DESC</when><otherwise>ASC</otherwise></choose>,
			</if>
		        POSITION DESC,
		        PLACE2, PLACE3, DUTIES, NAME
<![CDATA[) MEMBER_LIST
		) 
		WHERE
		  	RNUM  > #{firstIndex} AND  
	  		RNUM <= #{firstIndex} + #{recordCountPerAuthPage}
		ORDER BY
			RNUM
	]]>
	</select>
	<select id="MemberAuth.getMemberListByPlace1Cnt" parameterType="String" resultType="int">
	<![CDATA[
		SELECT 
          COUNT(*)
        FROM
          ( TAGENT AGENT 
            LEFT JOIN
              RPT_AGENT_AUTH PT_AUTH
            ON AGENT.USERID = PT_AUTH.AU_ID) 
          LEFT JOIN 
              H3_AGENT_AUTH ST_AUTH
            ON
              AGENT.USERID = ST_AUTH.AU_USERID
        WHERE
          PLACE1 = #{place1}
	]]>
	</select>
	
	<update id="MemberAuth.updateMemberAuth" parameterType="memberVO">
	<![CDATA[
		UPDATE
          TAGENT
        SET
          USEGRADE = #{usegrade}
        WHERE
          USERID = #{userid}
	]]>
	</update>
	
	<select id="MemberAuth.getMemberRpt" parameterType="memberVO" resultType="hashmap">
	<![CDATA[
		SELECT
			*
		FROM
          RPT_AGENT_AUTH
        WHERE
          AU_ID = #{userid}
	]]>
	</select>
	
	<update id="MemberAuth.updateMemberRpt" parameterType="memberVO">
	<![CDATA[
		UPDATE
          RPT_AGENT_AUTH
        SET
          AU_CODE = #{usegrade}
        WHERE
          AU_ID = #{userid}
	]]>
	</update>
	
	<insert id="MemberAuth.insertMemberRpt" parameterType="memberVO">
	<![CDATA[
		INSERT INTO
          RPT_AGENT_AUTH ( AU_ID, AU_CODE )
        VALUES ( #{userid}, #{usegrade} )
	]]>
	</insert>
	
	<update id="MemberAuth.updateSection" parameterType="memberVO">
	<![CDATA[
		UPDATE
          RPT_AGENT_APPROVE
        SET
          AP_SECTION_ID = #{userid}
        WHERE
          AP_PLACE1 = #{place1}
	]]>
	</update>
	
	<update id="MemberAuth.updateHead" parameterType="memberVO">
	<![CDATA[
		UPDATE
          RPT_AGENT_APPROVE
        SET
          AP_HEAD_ID = #{userid}
        WHERE
          AP_PLACE1 = #{place1}
	]]>
	</update>
	
	<insert id="insertRptApprove" parameterType="memberVO">
	<![CDATA[
		INSERT INTO RPT_AGENT_APPROVE
			( AP_PLACE1 )
		VALUES
			( #{place1} )
	]]>
	</insert>
	
	<select id="existRptApprove" parameterType="memberVO" resultType="int">
	<![CDATA[
		SELECT COUNT(*) FROM RPT_AGENT_APPROVE WHERE AP_PLACE1 = #{place1}
	]]>
	</select>
</mapper>
