<?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="sampleApprovalKnox">
	
	<resultMap id="sampleApprovalDocument"  extends="common.markedObjectResult" type="com.samsung.sample.approval.knox.entity.KnoxApprovalSampleDocument" >
		<id property="docId" column="DOC_ID"/>
		<result property="contents" column="CONTENTS" />
		<result property="title" column="TITLE"/>
		<result property="docStatus" column="DOC_STATUS"/>
		<result property="creator" column="CREATOR"/>
	</resultMap>
	
	<sql id="tableSampleApprovalDocument">TN_CF_SAMPLE_APPROVAL_KNOX_DOCUMENT</sql>
	
	<sql id="columnSampleApprovalDocument">
	DOC_ID, CONTENTS, TITLE,
	FIRST_REG_DATETIME, FIRST_REGR_ID, LAST_MOD_DATETIME, LAST_MODR_ID,
	DOC_STATUS, CREATOR
	</sql>
	
	<sql id="conditionSampleApprovalDocument">
		<trim prefix="WHERE" prefixOverrides="AND|OR">
			<if test="@com.samsung.SdlComparator@isNotEmpty(startDate)">
				AND CONVERT(VARCHAR(8), FIRST_REG_DATETIME, 112) <![CDATA[ >= ]]> #{startDate}
			</if>
			<if test="@com.samsung.SdlComparator@isNotEmpty(endDate)">
				AND CONVERT(VARCHAR(8), FIRST_REG_DATETIME, 112) <![CDATA[ <= ]]> #{endDate}
			</if>
			<if test="!systemAdmin">
				AND FIRST_REGR_ID = #{firstRegrId}
			</if>
		</trim>
	</sql>
	
	<select id="selectSampleApprovalDocumentPagingList" parameterType="java.util.HashMap" resultMap="sampleApprovalDocument">
		SELECT T.*
		FROM   (SELECT ROW_NUMBER() OVER(ORDER BY D.FIRST_REG_DATETIME DESC) RNUM,
		               <include refid="columnSampleApprovalDocument" />
		        FROM   <include refid="tableSampleApprovalDocument" /> D
		        <include refid="conditionSampleApprovalDocument" />) T
		WHERE  T.RNUM BETWEEN (#{pageIndex} - 1) * #{pageSize} + 1 AND #{pageIndex} * #{pageSize}
	</select>
	
	<select id="countSampleApprovalDocument" parameterType="java.util.HashMap" resultType="int">
		SELECT COUNT(D.DOC_ID) TOTAL
		FROM   <include refid="tableSampleApprovalDocument" /> D
		<include refid="conditionSampleApprovalDocument" />
	</select>
	
	<select id="selectSampleApprovalDocument" parameterType="java.util.HashMap" resultMap="sampleApprovalDocument">
		SELECT <include refid="columnSampleApprovalDocument" />
		FROM   <include refid="tableSampleApprovalDocument" />
		WHERE  DOC_ID = #{docId}
	</select>
	
	<insert id="insertSampleApprovalDocument" parameterType="java.util.HashMap">
		INSERT INTO <include refid="tableSampleApprovalDocument" />
		(<include refid="columnSampleApprovalDocument" />)
		VALUES
		(#{docId}, #{contents}, #{title},
		 #{firstRegDatetime}, #{firstRegrId}, #{lastModDatetime}, #{lastModrId},
		 #{docStatus}, #{creator})
	</insert>
	
	<update id="modifySampleApprovalDocument" parameterType="java.util.HashMap">
		UPDATE <include refid="tableSampleApprovalDocument" />
		SET    CONTENTS = #{contents}
		       ,TITLE = #{title}
		WHERE  DOC_ID = #{docId}
	</update>
	
	<delete id="deleteSampleApprovalDocument" parameterType="java.util.HashMap">
		DELETE FROM <include refid="tableSampleApprovalDocument" />
		WHERE  DOC_ID = #{docId}
	</delete>
	
	<update id="updateSampleApprovalDocument" parameterType="java.util.HashMap">
		UPDATE <include refid="tableSampleApprovalDocument" />
		SET    DOC_STATUS = #{approvalDocStatus}
		WHERE  DOC_ID = #{docId}
	</update>
	
</mapper>