<?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="moduleMapper">
	<select id="selectModule" parameterType="java.util.HashMap" resultType="CamelHashMap">
		SELECT
		       A.KPI_MODULE_ID,
		       A.KPI_MODULE_NAME,
		       A.KPI_MODULE_DESC,
		       A.KPI_MODULE_TYPE,
		       A.KPI_MODULE_PROGRAM,
		       A.CREATED_AT,
		       A.CREATED_BY,
		       A.UPDATED_AT,
		       A.UPDATED_BY
		FROM   DBO.EMBO_KPI_MODULE A

		WHERE  A.USE_YN = 'Y'
		<if test="@com.samsung.SdlComparator@isNotEmpty(kpiModuleId)">
		AND    A.KPI_MODULE_ID = #{kpiModuleId}
		</if>

		<if test="@com.samsung.SdlComparator@isNotEmpty(kpiModuleType)">
		AND    A.KPI_MODULE_TYPE = #{kpiModuleType}
		</if>
		<if test="@com.samsung.SdlComparator@isNotEmpty(searchKeyword)">
				<if test="searchCondition.equals('kpiModuleId')">
					AND LOWER(KPI_MODULE_ID) LIKE '%' + LOWER(#{searchKeyword}) + '%'
				</if>
				<if test="searchCondition.equals('kpiModuleName')">
					AND LOWER(KPI_MODULE_NAME) LIKE '%' + LOWER(#{searchKeyword}) + '%'
				</if>
				<if test="searchCondition.equals('kpiModuleDesc')">
					AND LOWER(KPI_MODULE_DESC) LIKE '%' + LOWER(#{searchKeyword}) + '%'
				</if>
			</if>
		ORDER BY A.KPI_MODULE_ID DESC
	</select>

	<insert id="insertModule" parameterType="HashMap" >
	INSERT
		INTO   dbo.EMBO_KPI_MODULE
		       (
		              kpi_module_id,
		              kpi_module_name,
		              kpi_module_desc,
		              kpi_module_type,
		              kpi_module_program,
		              created_at,
		              created_by,
		              updated_at,
		              updated_by
		       )
		       VALUES
		       (
		              #{kpiModuleId},
		              #{kpiModuleName},
		              #{kpiModuleDesc},
		              #{kpiModuleType},
		              #{kpiModuleProgram},
		              GETDATE(),
		              #{userId},
		              GETDATE(),
		              #{userId}
		       )

	</insert>

	<update id="updateModule">
		UPDATE dbo.EMBO_KPI_MODULE
		SET
			kpi_module_name = #{kpiModuleName}
			,kpi_module_desc = #{kpiModuleDesc}
			,kpi_module_type = #{kpiModuleType}
			,kpi_module_program = #{kpiModuleProgram}
			,updated_at = GETDATE()
			,updated_by = #{userId}
		WHERE  kpi_module_id = #{kpiModuleId}
	</update>

	<delete id="deleteModule" parameterType="java.util.HashMap">
		UPDATE dbo.EMBO_KPI_MODULE
		SET    	USE_YN = 'N'
				,updated_at = GETDATE()
				,updated_by = #{userId}
		WHERE  kpi_module_id IN
			<foreach item="item" index="index" collection="items" open="(" separator="," close=")">
			#{item.kpiModuleId}
		    </foreach>

	</delete>
</mapper>