<?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="go.kr.project.common.mapper.CommonCodeMapper">
    <!-- 특정 코드 그룹에 속한 코드 상세 목록 조회 -->
    <select id="selectCodeDetailListByGroupId" parameterType="String" resultType="CodeDetailVO">
        select CD_GROUP_ID
             , CD_ID
             , CD_NM
             , CD_DC
             , SORT_ORDR
             , USE_YN
             , ATTRIBUTE1
             , ATTRIBUTE2
             , ATTRIBUTE3
             , ATTRIBUTE4
             , ATTRIBUTE5
             , REG_DTTM
             , RGTR
             , MDFCN_DTTM
             , MDFR
        from tb_cd_detail
        where CD_GROUP_ID = #{cdGroupId}
          and USE_YN = 'Y'
        order by SORT_ORDR ASC
    </select>


    <!-- 특정 코드 그룹에 속한 코드 상세 목록 조회 -->
    <select id="selectCodeDetailList" parameterType="CmmnCodeSearchVO" resultType="CodeDetailVO">
        select CD_GROUP_ID
             , CD_ID
             , CD_NM
             , CD_DC
             , SORT_ORDR
             , USE_YN
             , ATTRIBUTE1
             , ATTRIBUTE2
             , ATTRIBUTE3
             , ATTRIBUTE4
             , ATTRIBUTE5
             , REG_DTTM
             , RGTR
             , MDFCN_DTTM
             , MDFR
        from tb_cd_detail
        where 1 = 1
        <if test="searchCdGroupId != null and searchCdGroupId != ''">
          and CD_GROUP_ID = #{searchCdGroupId}
        </if>
        <if test="searchCdId != null and searchCdId != ''">
            and CD_ID = #{searchCdId}
        </if>
        <if test="searchCdNm != null and searchCdNm != ''">
            and CD_NM like CONCAT('%', #{searchCdNm}, '%')
        </if>
        <if test="searchUseYn != null and searchUseYn != ''">
            and USE_YN = #{searchUseYn}
        </if>
        <if test="searchAttribute1 != null and searchAttribute1 != ''">
            and ATTRIBUTE1 = #{searchAttribute1}
        </if>
        <if test="searchAttribute2 != null and searchAttribute2 != ''">
            and ATTRIBUTE2 = #{searchAttribute2}
        </if>
        <if test="searchAttribute3 != null and searchAttribute3 != ''">
            and ATTRIBUTE3 = #{searchAttribute3}
        </if>
        <if test="searchAttribute4 != null and searchAttribute4 != ''">
            and ATTRIBUTE4 = #{searchAttribute4}
        </if>
        <if test="searchAttribute5 != null and searchAttribute5 != ''">
            and ATTRIBUTE5 = #{searchAttribute5}
        </if>
        <if test="searchRegDttmStart != null and searchRegDttmStart != ''">
            and REG_DTTM &gt;= #{searchRegDttmStart}
        </if>
        <if test="searchRegDttmEnd != null and searchRegDttmEnd != ''">
            and REG_DTTM &lt;= #{searchRegDttmEnd}
        </if>
        <if test="searchRgtr != null and searchRgtr != ''">
            and RGTR like CONCAT('%', #{searchRgtr}, '%')
        </if>
        <if test="searchMdfcnDttmStart != null and searchMdfcnDttmStart != ''">
            and MDFCN_DTTM &gt;= #{searchMdfcnDttmStart}
        </if>
        <if test="searchMdfcnDttmEnd != null and searchMdfcnDttmEnd != ''">
            and MDFCN_DTTM &lt;= #{searchMdfcnDttmEnd}
        </if>
        <if test="searchMdfr != null and searchMdfr != ''">
            and MDFR like CONCAT('%', #{searchMdfr}, '%')
        </if>
        order by
        <choose>
            <when test="sortColumn != null and sortColumn != ''">
                <choose>
                    <when test="sortColumn == 'SORT_ORDR'">SORT_ORDR</when>
                    <when test="sortColumn == 'CD_GROUP_ID'">CD_GROUP_ID</when>
                    <when test="sortColumn == 'CD_ID'">CD_ID</when>
                    <when test="sortColumn == 'CD_NM'">CD_NM</when>
                    <when test="sortColumn == 'CD_DC'">CD_DC</when>
                    <when test="sortColumn == 'USE_YN'">USE_YN</when>
                    <when test="sortColumn == 'ATTRIBUTE1'">ATTRIBUTE1</when>
                    <when test="sortColumn == 'ATTRIBUTE2'">ATTRIBUTE2</when>
                    <when test="sortColumn == 'ATTRIBUTE3'">ATTRIBUTE3</when>
                    <when test="sortColumn == 'ATTRIBUTE4'">ATTRIBUTE4</when>
                    <when test="sortColumn == 'ATTRIBUTE5'">ATTRIBUTE5</when>
                    <when test="sortColumn == 'REG_DTTM'">REG_DTTM</when>
                    <when test="sortColumn == 'RGTR'">RGTR</when>
                    <when test="sortColumn == 'MDFCN_DTTM'">MDFCN_DTTM</when>
                    <when test="sortColumn == 'MDFR'">MDFR</when>
                    <otherwise>SORT_ORDR</otherwise>
                </choose>
                <choose>
                    <when test="sortAscending != null and !sortAscending">DESC</when>
                    <otherwise>ASC</otherwise>
                </choose>
            </when>
            <otherwise>
                SORT_ORDR ASC
            </otherwise>
        </choose>
    </select>

</mapper>
