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

    <select id="selectMainNticeGridList" resultType="com.oqpo.api.entity.oper.NticeEntity">
        select /* Ntice.selectMainNticeGridList*/
        A.affl_shop_id, A.brand_id, fn_affl_nm(A.affl_shop_id) as affl_shop_nm, fn_brand_nm(A.brand_id) as brand_nm,
        A.ntice_no, A.ntice_titl, A.target_cd, fn_code_nm('TARGET_CD', A.target_cd) as target_nm
        , case when  date_format(now(),'%Y%m%d') <![CDATA[ >= ]]> A.noti_st_day and date_format(now(),'%Y%m%d') <![CDATA[ <= ]]> A.noti_ed_day then '공지중'
        when  date_format(now(),'%Y%m%d') <![CDATA[ < ]]> A.noti_st_day then '대기'
        when  date_format(now(),'%Y%m%d') <![CDATA[ > ]]> A.noti_ed_day then '공지종료' end as ntice_stat_nm
        ,date_format(A.noti_st_day ,'%Y.%m.%d') as noti_st_day, date_format(A.add_dt ,'%Y.%m.%d') as add_dt
        ,date_format(A.noti_ed_day ,'%Y.%m.%d') as noti_ed_day
        ,A.add_id
        ,A.ntice_desc
        from np_ntice A
        where 1=1
        <if test="authTpCd == '30'">
            /* 브랜드그룹 대상출력건 */
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} ) )
        </if>
        <if test="authTpCd == '40' ">
            /* 브랜드 대상출력건 */
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} ))
        </if>
        <if test="authTpCd == '50' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'T') ))
        </if>
        <if test="authTpCd == '60' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'S') ))
        </if>
            AND A.del_yn = 'N'
            AND DATE_FORMAT(NOW(), '%Y%m%d') BETWEEN A.NOTI_ST_DAY AND A.NOTI_ED_DAY
        order by A.ntice_no desc
        limit 0, 3

    </select>

    <select id="selectNticeGridList" resultType="com.oqpo.api.entity.oper.NticeEntity">
        select /* Ntice.selectNticeGridList*/
                A.affl_shop_id, A.brand_id, fn_affl_nm(A.affl_shop_id) as affl_shop_nm, fn_brand_nm(A.brand_id) as brand_nm,
                A.ntice_no, A.ntice_titl, A.target_cd, fn_code_nm('TARGET_CD', A.target_cd) as target_nm
                , case when  date_format(now(),'%Y%m%d') <![CDATA[ >= ]]> A.noti_st_day and date_format(now(),'%Y%m%d') <![CDATA[ <= ]]> A.noti_ed_day then '공지중'
                when  date_format(now(),'%Y%m%d') <![CDATA[ < ]]> A.noti_st_day then '대기'
                when  date_format(now(),'%Y%m%d') <![CDATA[ > ]]> A.noti_ed_day then '공지종료' end as ntice_stat_nm
                ,date_format(A.noti_st_day ,'%Y.%m.%d') as noti_st_day, date_format(A.add_dt ,'%Y.%m.%d') as add_dt
                ,date_format(A.noti_ed_day ,'%Y.%m.%d') as noti_ed_day
                ,A.add_id
        from np_ntice A
        where 1=1
        <if test="authTpCd == '10' or authTpCd == '20'">
            /* 전체 공지와 브랜드그룹 해당 건 */
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId == null or sBrandId == ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or (A.affl_shop_id = #{sAfflShopId} ) )
            </if>
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId != null and sBrandId != ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or (A.affl_shop_id = #{sAfflShopId} and ( A.brand_id is null or A.brand_id = '' ))
                or  (A.affl_shop_id = #{sAfflShopId} and  A.brand_id = #{sBrandId} ) )
            </if>
        </if>
        <if test="authTpCd == '30'">
            /* 브랜드그룹 대상출력건 */
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId == null or sBrandId == ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or  (A.affl_shop_id = #{afflShopId} ) )
            </if>
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId != null and sBrandId != ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or  (A.affl_shop_id = #{afflShopId} and ( A.brand_id is null or A.brand_id = '' ))
                or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{sBrandId} ))
            </if>
        </if>
        <if test="authTpCd == '40' ">
            /* 브랜드 대상출력건 */
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} ))
        </if>
        <if test="authTpCd == '50' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'T') ))
        </if>
        <if test="authTpCd == '60' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'S') ))
        </if>
        <if test="fromDt != null and fromDt != '' ">
            and  #{fromDt} between A.noti_st_day and  A.noti_ed_day
        </if>
        <if test="sNticeTitl != null and sNticeTitl != '' ">
            and A.ntice_titl like concat('%', #{sNticeTitl}, '%')
        </if>
        and A.del_yn = 'N'
        <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.ntice_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by A.ntice_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by A.ntice_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectNticeGridCnt" resultType="int">
        select count(*) as cnt  /* Ntice.selectNticeGridCnt*/
        from np_ntice A
        where 1=1
        <if test="authTpCd == '10' or authTpCd == '20'">
            /* 전체 공지와 브랜드그룹 해당 건 */
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId == null or sBrandId == ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or (A.affl_shop_id = #{sAfflShopId} ) )
            </if>
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId != null and sBrandId != ''">
                    and ((A.affl_shop_id is null or A.affl_shop_id = '')
                    or (A.affl_shop_id = #{sAfflShopId} and ( A.brand_id is null or A.brand_id = '' ))
                    or  (A.affl_shop_id = #{sAfflShopId} and  A.brand_id = #{sBrandId} ) )
            </if>
        </if>
        <if test="authTpCd == '30'">
            /* 브랜드그룹 대상출력건 */
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId == null or sBrandId == ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or  (A.affl_shop_id = #{afflShopId} ) )
            </if>
            <if test="sAfflShopId != null and sAfflShopId != '' and sBrandId != null and sBrandId != ''">
                and ((A.affl_shop_id is null or A.affl_shop_id = '')
                or  (A.affl_shop_id = #{afflShopId} and ( A.brand_id is null or A.brand_id = '' ))
                or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{sBrandId} ))
            </if>
        </if>
        <if test="authTpCd == '40' ">
            /* 브랜드 대상출력건 */
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} ))
        </if>
        <if test="authTpCd == '50' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'T') ))
        </if>
        <if test="authTpCd == '60' ">
            and ((A.affl_shop_id is null or A.affl_shop_id = '')
            or  (A.affl_shop_id = #{afflShopId} and  ( A.brand_id is null or A.brand_id = '' ))
            or  (A.affl_shop_id = #{afflShopId} and  A.brand_id = #{brandId} and A.target_cd in ('A', 'S') ))
        </if>
        <if test="fromDt != null and fromDt != '' ">
            and  #{fromDt} between A.noti_st_day and  A.noti_ed_day
        </if>
        <if test="sNticeTitl != null and sNticeTitl != '' ">
            and A.ntice_titl like concat('%', #{sNticeTitl}, '%')
        </if>
        and A.del_yn = 'N'
    </select>

    <select id="selectNticeInfo" resultType="com.oqpo.api.entity.oper.NticeEntity">
        select  /* ntice.selectnticeinfo */
                 ntice_no, target_cd, ntice_titl, ntice_desc, del_yn , fn_code_nm('TARGET_CD', target_cd) as target_nm
                , date_format(noti_st_day ,'%Y.%m.%d') as noti_st_day, date_format(noti_ed_day ,'%Y.%m.%d') as noti_ed_day
                , view_cnt, file_no, affl_shop_id, brand_id, fn_affl_nm(affl_shop_id) as affl_shop_nm, fn_brand_nm(brand_id) as brand_nm
                , add_id
        from np_ntice
        where ntice_no = #{nticeNo}
    </select>


    <select id="selectNticeTargetGridList" resultType="com.oqpo.api.entity.oper.NticeTargetEntity">
        select  /* ntice.selectNticeTargetGridList*/
                ntarget.ntice_no, ntarget.brand_id , binfo.brand_nm
        from   np_ntice_target ntarget
        join brand_base_info binfo on binfo.brand_id = ntarget.brand_id
        where ntarget.ntice_no = #{nticeNo}
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by binfo.brand_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by binfo.brand_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by binfo.brand_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectNticeTargetGridCnt" resultType="Integer">
        select /* ntice.selectNticeTargetGridCnt*/ count(*)
        from   np_ntice_target
        where  ntice_no = #{nticeNo}
    </select>

    <insert id="insertNticeInfo" >
            insert into np_ntice  /* ntice.insertNticeInfo*/
            (ntice_no, target_cd, ntice_titl, ntice_desc, del_yn, noti_st_day, noti_ed_day
             , file_no, add_dt, add_id, chg_dt, chg_id, affl_shop_id, brand_id)
            values
            (#{entity.nticeNo}, #{entity.targetCd}, #{entity.nticeTitl} , #{entity.nticeDesc} , nvl(#{entity.delYn},'N') , #{entity.notiStDay} , #{entity.notiEdDay}
            , #{entity.fileNo} , now(), #{userId}, now(), #{userId}, #{entity.afflShopId}, #{entity.brandId})
    </insert>

    <update id="updateNticeInfo">
        update np_ntice   /* ntice.updateNticeInfo*/
        set    chg_dt       = now()
              ,chg_id       = #{userId}
              ,target_cd    = #{entity.targetCd}
              ,ntice_titl   = #{entity.nticeTitl}
              ,ntice_desc   = #{entity.nticeDesc}
              ,del_yn       = nvl(#{entity.delYn},'N')
              ,noti_st_day  = #{entity.notiStDay}
              ,noti_ed_day  = #{entity.notiEdDay}
              ,brand_id     = #{entity.brandId}
              ,file_no      = #{entity.fileNo}
        where ntice_no = #{entity.nticeNo}
    </update>

    <insert id="insertNticeTarget" >
        insert into np_ntice_target /* ntice.insertNticeTarget*/
        (ntice_no, brand_id, add_dt, add_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            ( #{nticeNo},#{item.brandId}, now(), #{userId})
        </foreach>
    </insert>

    <delete id="deleteNticeTarget">
        delete from np_ntice_target /* ntice.deleteNticeTarget*/
        where  ntice_no  =  #{nticeNo}
        and    brand_id   in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.brandId}
        </foreach>
    </delete>

    <update id="updateNticeViewCnt">
        update np_ntice   /* ntice.updateNticeViewCnt*/
        set    view_cnt = view_cnt + 1
        where ntice_no = #{nticeNo}
    </update>


    <select id="selectMNticeList" resultType="com.oqpo.api.entity.oper.NticeEntity">
        select ROW_NUMBER() OVER(ORDER BY ntice_no desc) AS rnum, ntice_no, ntice_titl, date_format(noti_st_day ,'%Y.%m.%d')  as noti_st_day
        from    np_ntice
        where   date_format(now(),'%Y%m%d') between noti_st_day and noti_ed_day
        and     (target_cd = 'A'
         or     (target_cd = 'T' and ntice_no in  (select ntice_no from np_ntice_target where user_id = #{userId})))
    </select>

</mapper>
