<?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.oper.InterfaceMapper">

    <select id="selectInterfaceGridList" resultType="com.oqpo.api.entity.oper.InterfaceEntity">
        SELECT A.if_no, A.if_id, A.if_nm, A.use_yn, fn_code_nm('USE_YN', A.use_yn) AS use_yn_nm,
               A.link_info,
               A.com_meth_cd, fn_code_nm('COM_METH_CD', A.com_meth_cd) AS com_meth_nm,
               A.sr_tp_cd, fn_code_nm('SR_TP_CD', A.sr_tp_cd) AS sr_tp_nm,
               A.if_url, A.api_info, A.request_info, A.response_info,
               DATE_FORMAT(A.add_dt,'%Y.%m.%d %H:%i:%S') AS add_dt, A.add_id,
               DATE_FORMAT(A.chg_dt,'%Y.%m.%d %H:%i:%S') AS chg_dt, A.chg_id
        FROM np_if_info A
        WHERE A.del_yn = 'N'
        <if test=" fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.add_dt BETWEEN DATE_FORMAT(CONCAT(FN_DATE_TO(#{fromDt}), '000000'), '%Y%m%d%H%i%S') AND DATE_FORMAT(CONCAT(FN_DATE_TO(#{toDt}), '235959'), '%Y%m%d%H%i%S')
        </if>
        <if test="sLinkInfo != null and sLinkInfo != '' ">
            AND A.link_info = #{sLinkInfo}
        </if>
        <if test="sIfId != null and sIfId != '' ">
            AND A.if_id = #{sIfId}
        </if>
        <if test="sUseYn != null and sUseYn != '' ">
            AND A.use_yn = #{sUseYn}
        </if>
        <if test="sIfNm != null and sIfNm != '' ">
            AND A.if_nm = #{sIfNm}
        </if>
        <if test="sComMethCd != null and sComMethCd != '' ">
            AND A.com_meth_cd = #{sComMethCd}
        </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 A.if_no ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.if_no DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.if_no ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectInterfaceGridCnt" resultType="int">
        SELECT COUNT(*) AS cnt
        FROM np_if_info A
        WHERE A.del_yn = 'N'
        <if test=" fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.add_dt BETWEEN DATE_FORMAT(CONCAT(FN_DATE_TO(#{fromDt}), '000000'), '%Y%m%d%H%i%S') AND DATE_FORMAT(CONCAT(FN_DATE_TO(#{toDt}), '235959'), '%Y%m%d%H%i%S')
        </if>
        <if test="sLinkInfo != null and sLinkInfo != '' ">
            AND A.link_info = #{sLinkInfo}
        </if>
        <if test="sIfId != null and sIfId != '' ">
            AND A.if_id = #{sIfId}
        </if>
        <if test="sUseYn != null and sUseYn != '' ">
            AND A.use_yn = #{sUseYn}
        </if>
        <if test="sIfNm != null and sIfNm != '' ">
            AND A.if_nm = #{sIfNm}
        </if>
        <if test="sComMethCd != null and sComMethCd != '' ">
            AND A.com_meth_cd = #{sComMethCd}
        </if>
    </select>

    <select id="selectInterfaceInfo" resultType="com.oqpo.api.entity.oper.InterfaceEntity">
        SELECT A.if_no, A.if_id, A.if_nm, A.use_yn, fn_code_nm('USE_YN', A.use_yn) AS use_yn_nm, A.link_info,
               A.com_meth_cd, fn_code_nm('COM_METH_CD', A.com_meth_cd) AS com_meth_nm,
               A.sr_tp_cd, fn_code_nm('SR_TP_CD', A.sr_tp_cd) AS sr_tp_nm,
               A.if_url, A.api_info, A.request_info, A.response_info,
               DATE_FORMAT(A.add_dt,'%Y.%m.%d %H:%i:%S') AS add_dt, A.add_id, fn_user_nm(A.add_id) AS add_nm,
               DATE_FORMAT(A.chg_dt,'%Y.%m.%d %H:%i:%S') AS chg_dt, A.chg_id, fn_user_nm(A.chg_id) as chg_nm
        FROM np_if_info A
        WHERE A.if_no = #{ifNo}
    </select>

    <insert id="insertInterfaceInfo" >
        INSERT INTO np_if_info
            (if_no, if_id, if_nm, use_yn, link_info, com_meth_cd, sr_tp_cd, if_url, api_info, request_info, response_info,
             add_dt, add_id, chg_dt, chg_id)
        VALUES
            (#{entity.ifNo}, #{entity.ifId}, #{entity.ifNm}, #{entity.useYn}, #{entity.linkInfo}, #{entity.comMethCd}, #{entity.srTpCd},
             #{entity.ifUrl}, #{entity.apiInfo}, #{entity.requestInfo}, #{entity.responseInfo},
             NOW(), #{entity.addId}, NOW(), #{entity.chgId})
    </insert>

    <update id="updateInterfaceInfo">
        UPDATE np_if_info SET
            if_id = #{entity.ifId},
            if_nm = #{entity.ifNm},
            use_yn = #{entity.useYn},
            link_info = #{entity.linkInfo},
            com_meth_cd = #{entity.comMethCd},
            sr_tp_cd = #{entity.srTpCd},
            if_url = #{entity.ifUrl},
            api_info = #{entity.apiInfo},
            request_info = #{entity.requestInfo},
            response_info = #{entity.responseInfo},
            chg_dt = NOW(),
            chg_id = #{entity.chgId}
        WHERE if_no = #{entity.ifNo}
    </update>

    <update id="deleteInterfaceInfo">
        UPDATE np_if_info SET
            del_yn = 'Y',
            chg_dt = NOW(),
            chg_id = #{entity.chgId}
        WHERE if_no = #{entity.ifNo}
    </update>

</mapper>
