<?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">
    select (ROW_NUMBER() OVER()) AS RNUM,
            aa.*
    from (select pl1.codenm place1str,
                pl2.codenm PLACE2STR,
                pl3.codenm PLACE3STR,
                pst.codenm POSITIONSTR,
                jt.codenm JOBTYPESTR,
                a.*
          from tagent a
              inner join tcode pl1 on pl1.code1 = 'C001' and a.place1 = pl1.code2
              inner join tcode pl2 on a.place1 = pl2.code1 and a.place2 = pl2.code2
              inner join tcode pl3 on pl3.code1 = 'C009' and a.place3 = pl3.code2
              inner join tcode pst on pst.code1 = 'C002' and a.position  = pst.code2
              inner join tcode jt on jt.code1 = 'C003' and a.jobtype = jt.code2
          where PLACE1 =#{place1}
          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
          limit #{recordCountPerAuthPage} offset #{firstIndex} ) aa
	</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>
