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

	<resultMap id="corpResult" type="corpVO">
		<result property="place1" column="PLACE1" />
		<result property="place1str" column="CODENM" />
		<result property="orgId" column="ORG_ID" />
		<result property="orgNo" column="ORG_NO" />
		<result property="grpNo" column="GRP_NO" />
		<result property="name" column="NAME" />
		<result property="location" column="LOCATION" />
		<result property="ceo" column="CEO" />
		<result property="jobType" column="JOBTYPE" />
		<result property="tel" column="TEL" />
		<result property="fax" column="FAX" />
		<result property="comments" column="COMMENTS" />
		<result property="partner" column="PARTNER" />
		<result property="shipCnt" column="SHIPCNT" />
		<result property="memberCnt" column="MEMBERCNT" />
		<result property="basicYear" column="BASICYEAR" />
		<result property="fund" column="FUND" />
		<result property="writerPlace1" column="WRITER_PLACE1" />
		<result property="writer" column="WRITER" />
		<result property="credate" column="CREDATE" />
		<result property="logdate" column="LOGDATE" />
	</resultMap>
	
	<select id="select" parameterType="corpVO" resultMap="corpResult">
	<![CDATA[	
		SELECT
			CP.*, C.CODENM
		FROM
			TCORP CP, TCODE C
		WHERE
			C.CODE1 = 'C001'
			AND CP.PLACE1 = C.CODE2
			AND CP.PLACE1 = #{place1}
        	AND CP.ORG_ID = #{orgId}
        	AND CP.ORG_NO = #{orgNo}
	]]>
	</select>

	<select id="selectList" parameterType="corpSearchVO" resultMap="corpResult">
	<![CDATA[
        SELECT
        	CP.*, C.CODENM
        FROM
        	TCORP CP, TCODE C
        WHERE
        	C.CODE1 = 'C001'
        	AND CP.PLACE1 = C.CODE2
        	AND CP.ORG_ID = #{orgId}
        	AND CP.GRP_NO = #{grpNo}
  	]]>
  		<if test="place1 != null and place1 != ''">
			AND CP.PLACE1 = #{place1}
		</if>
		<if test="name != null and name != ''">
			AND CP.NAME LIKE '%' || #{name} || '%'
		</if>
		<if test="jobType != null and jobType != ''">
			AND CP.JOBTYPE = #{jobType}
		</if>
	<![CDATA[
		ORDER BY
			CP.PLACE1 ASC
	]]>
	</select>
	
	<select id="selectStats" parameterType="hashmap" resultType="hashmap" >
	<![CDATA[
		SELECT 
			COUNT(ORG_ID) AS TOT_CNT,
			'00' AS ORG_ID,
			'계' AS ORG_NM,
			'00' AS GRP_NO,
	]]>			
		<foreach collection="placeList" item="item" index="index" separator="," open="" close="">
	  		SUM(CASE PLACE1 WHEN #{item} THEN 1 ELSE 0 END) AS ${item}_CNT
		</foreach>
	<![CDATA[			  	 
		FROM TCORP 
		UNION 
		SELECT 
			COUNT(TP.ORG_ID) AS TOT_CNT,
			SUBSTR(TD.CODE2,1,2) AS ORG_ID,
			TD.CODENM AS ORG_NM,
			SUBSTR(TD.CODE2,3,2) AS GRP_NO,
	]]>
		<foreach collection="placeList" item="item" index="index" separator="," open="" close="">
	  		SUM(CASE PLACE1 WHEN #{item} THEN 1 ELSE 0 END) AS ${item}_CNT
		</foreach>
	<![CDATA[
		FROM TCORP TP LEFT JOIN TCODE TD ON TP.ORG_ID = SUBSTR(TD.CODE2,1,2)
		WHERE TD.CODE1 = 'CORP' 
		GROUP BY TP.ORG_ID, TD.CODENM, GRP_NO, TD.CODE2 
		ORDER BY ORG_ID ASC
	]]>
	</select>	

	<insert id="insert" parameterType="corpVO">
	<![CDATA[
		INSERT INTO TCORP
     	(
			PLACE1, ORG_ID, ORG_NO, GRP_NO, NAME,
			LOCATION, CEO, JOBTYPE, TEL, FAX,
			COMMENTS, PARTNER, SHIPCNT, MEMBERCNT, BASICYEAR,
			FUND, WRITER_PLACE1, WRITER, CREDATE, LOGDATE
     	)
     	VALUES
     	(
			#{place1}, #{orgId}, #{orgNo}, #{grpNo}, #{name}, 
			#{location}, #{ceo}, #{jobType}, #{tel}, #{fax}, 
			#{comments}, #{partner}, #{shipCnt}, #{memberCnt}, #{basicYear}, 
			#{fund}, #{writerPlace1}, #{writer}, TO_CHAR(NOW(), 'YYYYMMDDHH24MISS'), TO_CHAR(NOW(), 'YYYYMMDDHH24MISS')
     	)
	]]>
	</insert>

	<update id="update" parameterType="corpVO">
	<![CDATA[
		UPDATE 
			TCORP
		SET 
			GRP_NO = #{grpNo},
			NAME = #{name},
			LOCATION = #{location},
			CEO = #{ceo},
			JOBTYPE = #{jobType},
			TEL = #{tel},
			FAX = #{fax},
			COMMENTS = #{comments},
			PARTNER = #{partner},
			BASICYEAR = #{basicYear},
			SHIPCNT = #{shipCnt},
			MEMBERCNT = #{memberCnt},
			FUND = #{fund},
			WRITER = #{writer},
			WRITER_PLACE1 = #{writerPlace1},
			LOGDATE = TO_CHAR(NOW(), 'YYYYMMDDHH24MISS')
		WHERE 
			PLACE1 = #{place1}
			AND ORG_ID = #{orgId}
			AND ORG_NO = #{orgNo}
	]]>
	</update>

	<delete id="delete" parameterType="corpVO">
	<![CDATA[
		DELETE 
		FROM
			TCORP
		WHERE 
			PLACE1 = #{place1}
        	AND ORG_ID = #{orgId}
        	AND ORG_NO = #{orgNo}
	]]>
	</delete>

	<select id="selectMaxOrgNo" parameterType="corpVO" resultType="int">
	<![CDATA[	
		SELECT
			MAX(ORG_NO)
		FROM
			TCORP
		WHERE
        	ORG_ID = #{orgId}
	]]>
	</select>
</mapper>
