<?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="com.oqpo.api.mapper.system.CodeMapper">
    <select id="selectSearchCodeList" resultType="com.oqpo.api.entity.CodeEntity">
        select /* selectSearchCodeList */
            code_cd, code_nm, fn_code_nm('CODE_TP_CD', code_tp_cd) as code_tp_nm, use_yn
        from   np_cd_info
        where  1=1
        <if test="sCodeTpCd != null and sCodeTpCd != ''">
        and    code_tp_cd = #{sCodeTpCd}
        </if>
        <if test="sUseYn != null and sUseYn != ''">
        and    use_yn = #{sUseYn}
        </if>
        <if test="sCodeNm != null and sCodeNm != ''">
        and    (code_cd like concat('%',#{sCodeNm},'%') or code_nm like concat('%',#{sCodeNm},'%'))
        </if>
        order by code_cd asc
    </select>

    <select id="selectCodeList" resultType="com.oqpo.api.entity.CodeEntity">
        select /* selectCodeList */
            code_cd
            , code_nm
        from np_cd_info
        where use_yn = 'Y'
        order by code_cd asc
    </select>

    <select id="selectCodeInfo" resultType="com.oqpo.api.entity.CodeEntity">
        select /* selectCodeInfo */
            code_cd
            , code_nm
            , code_tp_cd
            , use_yn
        from np_cd_info
        where code_cd = #{codeCd}
    </select>

    <select id="selectCodeDtlList" parameterType="string" resultType="com.oqpo.api.entity.CodeEntity">
        select /* selectCodeDtlList */
            code_dtl_cd
            , code_dtl_nm
        from np_cd_dtl
        where code_cd = #{codeCd}
        and use_yn = 'Y'
        order by view_num asc
    </select>

    <select id="selectCodeDtlGridList" resultType="com.oqpo.api.entity.CodeEntity">
        select /* selectCodeDtlGridList */
            code_cd
            , code_dtl_cd
            , code_dtl_nm
            , use_yn
            , view_num
            , code_desc
        from np_cd_dtl
        where code_cd = #{codeCd}
        <if test="useYn != null and useYn != ''">
        and use_yn = #{useYn}
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by view_num asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by view_num desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by view_num asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectCodeDtlGridListCnt" resultType="int">
        select count(1) as cnt /* selectCodeDtlGridListCnt */
        from np_cd_dtl
        where code_cd = #{codeCd}
        and use_yn = 'Y'
    </select>

    <select id="selectCodeDupYn" resultType="String">
        select case when count(*) <![CDATA[ > ]]> 0 then 'Y' else 'N' end as cnt /* selectCodeDupYn */
        from np_cd_info
        where code_cd = #{codeCd}
    </select>

    <insert id="insertCodeInfo" >
        insert into np_cd_info  /* insertCodeInfo */
        (code_cd, code_tp_cd, code_nm, use_yn, add_dt, add_id, chg_dt, chg_id)
        values
        (#{entity.codeCd}, #{entity.codeTpCd}, #{entity.codeNm}, #{entity.useYn}, now(), #{userId}, now(), #{userId})
    </insert>

    <insert id="insertCodeDtl" >
        insert into np_cd_dtl /** insertCodeDtl */
        (code_cd, code_dtl_cd, code_dtl_nm, use_yn, view_num, code_desc, add_dt, add_id, chg_dt, chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
        ( #{item.codeCd},#{item.codeDtlCd},#{item.codeDtlNm},#{item.useYn},#{item.viewNum},#{item.codeDesc}, now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <update id="updateCodeInfo">
        UPDATE np_cd_info SET
            code_tp_cd = #{entity.codeTpCd},
            code_nm = #{entity.codeNm},
            use_yn = #{entity.useYn},
            chg_dt = NOW(),
            chg_id = #{userId}
        WHERE code_cd = #{entity.codeCd}
    </update>

    <update id="updateCodeDtl">
        <foreach collection="gridUpdateData" item="item" separator=";">
        UPDATE np_cd_dtl SET
            code_dtl_nm = #{item.codeDtlNm},
            use_yn = #{item.useYn},
            view_num = #{item.viewNum},
            code_desc = #{item.codeDesc},
            chg_dt = NOW(),
            chg_id = #{userId}
        WHERE code_cd = #{item.codeCd}
        AND code_dtl_cd = #{item.codeDtlCd}
        </foreach>
    </update>

    <delete id="deleteCodeDtl" >
        delete from np_cd_dtl /** deleteCodeDtl */
        where code_cd = #{codeCd}
        and code_dtl_cd in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.codeDtlCd}
        </foreach>
    </delete>
</mapper>
