<?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.JobMapper">

    <select id="selectJobGridList" resultType="com.oqpo.api.entity.system.JobEntity">
        select /* selectJobGridList */
            job_no
            , user_id
            , fn_user_nm(user_id) as user_nm
            , menu_no
            , fn_get_menu_nm(menu_no) as menu_nm
            , act_tp_cd
            , fn_code_nm('ACT_TP_CD', act_tp_cd) as act_tp_nm
            , act_nm
            , ip
            , add_dt
            , add_id
        from np_job
        where 1=1
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            and   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="menuNo != null and menuNo != ''">
            and   menu_no = #{menuNo}
        </if>
        <if test="actTpCd != null and actTpCd != ''">
            and   act_tp_cd = #{actTpCd}
        </if>
        <if test="userId != null and userId != ''">
            and   user_id =#{userId}
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'job_no'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by job_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by job_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by job_no desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectJobInfoGridListCnt" resultType="int">
        select count(*)
        from np_job
        where 1=1
    </select>

    <select id="selectJobGridListCnt" resultType="int">
        select /* selectJobGridListCnt */
        count(*)
        from np_job
        where 1=1
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            and   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="menuNo != null and menuNo != ''">
            and   menu_no = #{menuNo}
        </if>
        <if test="actTpCd != null and actTpCd != ''">
            and   act_tp_cd = #{actTpCd}
        </if>
        <if test="userId != null and userId != ''">
            and   user_id = #{userId}
        </if>
    </select>

    <insert id="insertJobInfo" >
        insert into np_job
        (job_no, user_id, menu_no, act_tp_cd, act_nm, ip, add_dt, add_id)
        values
        (#{jobNo}, #{userId}, #{menuNo}, #{actTpCd}, #{actNm} , #{ip}, now(), #{userId})
    </insert>

    <select id="selectConnectUserAuth" resultType="com.oqpo.api.entity.system.JobEntity">
        select distinct act.menu_no, act.ACT_TP_CD, act.ACT_NM
        from    np_user_auth uauth
                ,np_auth_dtl  auth
                ,np_act_info  act
        where uauth.auth_no = auth.auth_no
        and   auth.menu_no = act.menu_no
        <!--and   ((auth.WR_YN = 'Y' and act.WR_YN = 'Y') or (auth.WR_YN = 'N')) -->
        <!-- and   uauth.user_id = #{userId} -->
        and   act.conn_url = #{connUrl}
    </select>

</mapper>
