<?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="formulaMapper">
	
	<sql id="tableFormula">[dbo].[MBO_FORMULA]</sql>
	
	<select id="selectFormula" parameterType="java.util.HashMap" resultType="CamelHashMap">
		SELECT
       			A.FM_ID
       			,A.FM_TYPE
       			,A.FM_NAME
       			,A.FM_DESC
       			,A.DELETE_YN
       			,A.FIRST_REG_DATETIME
       			,A.FIRST_REGR_ID
       			,A.LAST_MOD_DATETIME
       			,A.LAST_MODR_ID
		FROM   <include refid="tableFormula" /> A		
		WHERE  A.DELETE_YN = '0'
		<if test="@com.samsung.SdlComparator@isNotEmpty(fmId)">
		AND    A.FM_ID = #{fmId}
		</if>
		<if test="@com.samsung.SdlComparator@isNotEmpty(fmName)">
		AND    A.FM_NAME like '%' || #{fmName} || '%'
		</if>
		<if test="@com.samsung.SdlComparator@isNotEmpty(fmType)">
		AND    A.FM_TYPE = #{fmType}
		</if>
		<if test="@com.samsung.SdlComparator@isNotEmpty(startDate)">
		AND    CONVERT(VARCHAR(8), A.LAST_MOD_DATETIME, 112) <![CDATA[ >= ]]> REPLACE(#{startDate}, '-', '')
		</if>
		<if test="@com.samsung.SdlComparator@isNotEmpty(endDate)">
		AND    CONVERT(VARCHAR(8), A.LAST_MOD_DATETIME, 112) <![CDATA[ <= ]]> REPLACE(#{endDate}, '-', '')
		</if>
	</select>
	
	<insert id="insertFormula" parameterType="HashMap">
		INSERT INTO <include refid="tableFormula" />
			([FM_ID]
           ,[FM_TYPE]
           ,[FM_NAME]
           ,[FM_DESC]
           ,[DELETE_YN]
           ,[FIRST_REG_DATETIME]
           ,[FIRST_REGR_ID]
           ,[LAST_MOD_DATETIME]
           ,[LAST_MODR_ID])
		VALUES
			(#{fmId}
           ,#{fmType}
           ,#{fmName}
           ,#{fmDesc}
           ,'0'
           ,GETDATE()
           ,#{userId}
           ,GETDATE()
           ,#{userId})
	</insert>
	
	<update id="updateFormula">
		UPDATE <include refid="tableFormula" />
		SET    	
			FM_TYPE = #{fmType}
			,FM_NAME = #{fmName}
			,FM_DESC = #{fmDesc}
			,LAST_MOD_DATETIME = GETDATE()
			,LAST_MODR_ID = #{userId}
		WHERE  FM_ID = #{fmId}
	</update>
	
	<delete id="deleteFormula" parameterType="java.util.HashMap">
		UPDATE <include refid="tableFormula" />
		SET    	DELETE_YN = '1'
				,LAST_MOD_DATETIME = GETDATE()
				,LAST_MODR_ID = #{userId}
		WHERE  FM_ID IN
			<foreach item="item" index="index" collection="items" open="(" separator="," close=")">
			#{item.fmId}
		    </foreach>
		        
	</delete>
</mapper>
