<?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.stinfo.StoreItemUnitMapper">

    <select id="selectStoreItemUnitGridList" resultType="com.oqpo.api.entity.stinfo.StoreItemUnitEntity">
        select  /* selectStoreItemUnitGridList */
                iunit.brand_id, fn_brand_nm(iunit.brand_id) as brand_nm, iunit.store_id,  fn_store_nm(iunit.brand_id, iunit.store_id) as store_nm,
                date_format(iunit.cont_stt_dt,'%Y.%m.%d') as cont_stt_dt, date_format(iunit.cont_end_dt,'%Y.%m.%d') as cont_end_dt,
                date_format(iunit.sys_reg_dttm,'%Y.%m.%d') as sys_reg_dttm, iunit.item_cnt
        from (
        select  brand_id, store_id, cont_stt_dt, cont_end_dt, min(sys_reg_dttm) as sys_reg_dttm, count(*) as item_cnt
        from    store_item_unit_info
        where   brand_id = #{sBrandId}
        <if test="sStoreNm != null and sStoreNm != ''">
        and    store_id in (select a.store_id from store_base_info a where a.brand_id = #{sBrandId} and a.store_nm like concat('%', #{sStoreNm}, '%'))
        </if>
        and     unit_target_cd = 'Y'
        group by brand_id, store_id, cont_stt_dt, cont_end_dt
        ) iunit
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by store_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by store_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by store_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStoreItemUnitGridCnt" resultType="int">
        select /*selectStoreItemUnitGridCnt*/
        count(*)
        from (
        select  distinct brand_id, store_id, cont_stt_dt, cont_end_dt
        from    store_item_unit_info
        where   brand_id = #{sBrandId}
        <if test="sStoreNm != null and sStoreNm != ''">
            and    store_id in (select a.store_id from store_base_info a where a.brand_id = #{sBrandId} and a.store_nm like concat('%', #{sStoreNm}, '%'))
        </if>
        and     unit_target_cd = 'Y'
        ) iunit
    </select>
<!--
    <select id="selectSstoreItemUnitGridList" resultType="com.oqpo.api.entity.stinfo.StoreItemUnitEntity">
        select  /* selectStoreItemUnitGridList */
        iunit.brand_id, fn_brand_nm(iunit.brand_id) as brand_nm, iunit.store_id,  fn_store_nm(iunit.brand_id, iunit.store_id) as store_nm,
        date_format(iunit.cont_stt_dt,'%Y.%m.%d') as cont_stt_dt, date_format(iunit.cont_end_dt,'%Y.%m.%d') as cont_end_dt,
        date_format(iunit.sys_reg_dttm,'%Y.%m.%d') as sys_reg_dttm, iunit.item_cnt
        from (
        select  brand_id, store_id, cont_stt_dt, cont_end_dt, min(sys_reg_dttm) as sys_reg_dttm, count(*) as item_cnt
        from    store_item_unit_info
        where   brand_id = #{sBrandId}
        and     store_id = #{sStoreId}
        and     unit_target_cd = 'Y'
        group by brand_id, store_id, cont_stt_dt, cont_end_dt
        ) iunit
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by store_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by store_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by store_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectSstoreItemUnitGridCnt" resultType="int">
        select /*selectStoreItemUnitGridCnt*/
        count(*)
        from (
        select  distinct brand_id, store_id, cont_stt_dt, cont_end_dt
        from    store_item_unit_info
        where   brand_id = #{sBrandId}
        and     store_id = #{sStoreId}
        and     unit_target_cd = 'Y'
        ) iunit
    </select>
-->
    <select id="selectStoreItemUnitInfo" resultType="com.oqpo.api.entity.stinfo.StoreItemUnitEntity">
        select /*selectStoreItemUnitInfo*/
                        case when unit.store_unit_unq_no is null then 'C' else 'R' end view_cd,
                        unit.store_unit_unq_no, item.brand_id, item.item_id, item.item_nm,
                        case when unit.cont_stt_dt is null or unit.cont_stt_dt = '' then date_format(#{contSttDt},'%Y.%m.%d') else date_format(unit.cont_stt_dt,'%Y.%m.%d') end as cont_stt_dt,
                        case when unit.cont_end_dt is null or unit.cont_end_dt = '' then date_format(#{contEndDt},'%Y.%m.%d') else date_format(unit.cont_end_dt,'%Y.%m.%d') end as cont_end_dt,
                        case when unit.unit is null or  unit.unit = '' or store.loan_dvsn = 'LD03' then item.unit else unit.unit end as unit   ,
                        case when unit.unit is null or  unit.unit = '' or store.loan_dvsn = 'LD03' then fn_code_nm('DSTRBT_COST_UNIT',item.unit) else fn_code_nm('DSTRBT_COST_UNIT',unit.unit) end as unit_nm   ,
                        case when nvl(unit.stdd_qty,0) = 0 or store.loan_dvsn = 'LD03' then item.store_sale_stdd_qty else unit.stdd_qty end as stdd_qty  ,
                        case when unit.curr is null or  unit.curr = '' or store.loan_dvsn = 'LD03' then item.curr else unit.curr end as curr,
                        case when unit.curr is null or  unit.curr = '' or store.loan_dvsn = 'LD03' then fn_code_nm('CURR',item.curr) else fn_code_nm('CURR',unit.curr) end as curr_nm,
                        case when unit.unit_amt is null or  unit.unit_amt = 0 or store.loan_dvsn = 'LD03' then item.store_sale_unit_amt else unit.unit_amt end as unit_amt,
                        case when unit.unit_target_cd is null or unit.unit_target_cd = '' or store.loan_dvsn = 'LD03' then 'Y' else unit.unit_target_cd end as unit_target_cd,
                        case when unit.unit_target_cd is null or unit.unit_target_cd = '' or store.loan_dvsn = 'LD03' then fn_code_nm('DSTRBT_COST_UNIT','Y') else  fn_code_nm('DSTRBT_COST_UNIT',unit.unit_target_cd)  end as unit_target_nm,
                        nvl(item.unit_amt,0) as spply_unit_amt
                        ,item.brand_unit_unq_no
                from   brand_item_unit_info item
                left outer join store_item_unit_info unit on  item.brand_unit_unq_no = unit.brand_unit_unq_no and  item.brand_id = unit.brand_id and item.item_id = unit.item_id and unit.store_id = #{storeId}
                                  and unit.cont_stt_dt = #{contSttDt} and unit.cont_end_dt = #{contEndDt}
                left outer join store_base_info store on unit.store_id = store.store_id and store.store_id = #{storeId}
                where item.brand_id = #{brandId}
                order by view_cd
    </select>

    <select id="selectStoreItemUnitContDateInfo" resultType="String">
        select /* selectStoreItemUnitContDateInfo */
                nvl(max(cont_end_dt),'') as cont_end_dt
        from   store_item_unit_info
        where  brand_id = #{brandId}
        and    store_id = #{storeId}
        and    cont_end_dt between #{contSttDt} and #{contEndDt}
    </select>

    <select id="selectStoreItemUnitExistYn" resultType="String">
        select /* selectStoreItemUnitContDateInfo */
                case when count(*) > 0 then 'Y' else 'N' END
        from   store_item_unit_info
        where  brand_id = #{brandId}
        and    store_id = #{storeId}
        and    cont_stt_dt = #{contSttDt}
        and    cont_end_dt = #{contEndDt}
    </select>

    <select id="selectStoreItemUnitContDtInfo" resultType="com.oqpo.api.entity.stinfo.StoreItemUnitEntity">
        select  distinct nvl(max(cont_stt_dt) ,'') as cont_stt_dt, nvl(max(cont_end_dt) ,'') as cont_end_dt
        from  store_item_unit_info
        where  brand_id = #{brandId}
        and    store_id = #{storeId}
    </select>


    <update id="updateStoreItemUnitContEnd" >
        update /* updateStoreItemUnitContEnd */
                store_item_unit_info
        set    cont_end_dt =  date_format(DATE_SUB(date_format(#{contSttDt},'%Y%m%d'), INTERVAL 1 DAY),'%Y%m%d')
        where  brand_id = #{brandId}
        and    store_id = #{storeId}
        and    cont_end_dt = #{contEndDt}

    </update>

    <insert id="insertStoreItemUnitInfo" >
        insert into  /** insertStoreItemUnitInfo */
        store_item_unit_info
        (store_unit_unq_no, cont_stt_dt, cont_end_dt, unit,
        curr, unit_amt, store_id, brand_id, short_cont_file_no,
        unit_target_cd, item_id, stdd_qty, brand_unit_unq_no,
        sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridUpdateData" item="item" separator=",">
            ( fn_get_dealno(34,''), #{item.contSttDt}, #{item.contEndDt}, #{item.unit},
            #{item.curr}, #{item.unitAmt}, #{storeId}, #{brandId}, #{item.shortContFileNo},
            #{item.unitTargetCd}, #{item.itemId}, #{item.stddQty}, #{item.brandUnitUnqNo},
            NOW(), #{userId}, NOW(), #{userId})
        </foreach>
    </insert>

    <update id="updateStoreItemUnitInfo">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE  store_item_unit_info /* storeitemUnit.updateStoreItemUnitInfo */
            SET sys_chg_dttm          = now()
            , sys_chg_id            = #{userId}
            , cont_end_dt       = #{item.contEndDt}
            , unit              = #{item.unit}
            , curr              =  #{item.curr}
            , unit_amt          = #{item.unitAmt}
            , unit_target_cd    = #{item.unitTargetCd}
            WHERE store_unit_unq_no = #{item.storeUnitUnqNo}
        </foreach>
    </update>

    <insert id="insertMergStoreItemUnitInfo" >
            insert into  /** Merge insertMergStoreItemUnitInfo */
                store_item_unit_info
                (store_unit_unq_no, cont_stt_dt, cont_end_dt, unit,
                 curr, unit_amt, store_id, brand_id, short_cont_file_no,
                 unit_target_cd, item_id,  brand_unit_unq_no,
                 sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
            values
            <foreach collection="gridUpdateData" item="item" separator=",">
                ( fn_get_dealno(34,''), #{item.contSttDt}, #{item.contEndDt}, #{item.unit},
                  #{item.curr}, #{item.unitAmt}, #{storeId}, #{brandId}, #{item.shortContFileNo},
                  #{item.unitTargetCd}, #{item.itemId}, #{item.brandUnitUnqNo},
                  NOW(), #{userId}, NOW(), #{userId})
            </foreach>
            on duplicate key
            update
              sys_chg_dttm          = now()
            , sys_chg_id            = #{userId}
            , cont_stt_dt       = VALUES(cont_stt_dt)
            , cont_end_dt       = VALUES(cont_end_dt)
            , unit              = VALUES(unit)
            , curr              = VALUES(curr)
            , unit_amt          = VALUES(unit_amt)
            , unit_target_cd    = VALUES(unit_target_cd)
            , stdd_qty          = VALUES(stdd_qty)
    </insert>

    <select id="selectStoreNewItemUnitInfo" resultType="com.oqpo.api.entity.stinfo.StoreItemUnitEntity">
        select /*selectStoreNewItemUnitInfo*/
                 'C' view_cd,
                 '' as store_unit_unq_no, item.brand_id, item.item_id, item.item_nm,
                 '' as cont_stt_dt, '' as cont_end_dt,
                 item.unit, fn_code_nm('DSTRBT_COST_UNIT',item.unit) as unit_nm,
                 item.stdd_qty, item.CURR, fn_code_nm('CURR',item.curr)  as curr_nm,
                 item.store_sale_unit_amt as usit_amt, 'Y' as unit_target_cd,
                 fn_code_nm('DSTRBT_COST_UNIT','Y') as unit_target_nm,
                 item.unit_amt as spply_unit_amt,
                 item.brand_unit_unq_no, item.store_sale_unit_amt as unit_amt

         from   brand_item_unit_info item
         where  item.brand_id = #{brandId}
    </select>

    <select id="selectStoreItemUnitEndDtInfo" resultType="String">
        select max(cont_end_dt)
        from  store_item_unit_info
        where brand_id = #{brandId}
        and   store_id = #{storeId}
        and   cont_stt_dt = #{contSttDt}
    </select>

    <update id="updateStoreItemUnitChgContEnd" >
        update /* updateStoreItemUnitChgContEnd */
                store_item_unit_info
        set    cont_end_dt =  #{contEndDt}
        where  brand_id = #{brandId}
        and    store_id = #{storeId}
        and    cont_stt_dt = #{contSttDt}

    </update>

</mapper>
