<?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.pomng.PchReqMapper">

    <select id="selectPchReqGridList" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
        select  /* PchReq.selectPchReqGridList */
                pch_req_unq_no, brand_id, fn_brand_nm(brand_id) as brand_nm,  pch_req_nm
                ,pch_req_st_cd, fn_code_nm('PCH_REQ_ST_Cd',pch_req_st_cd) as pch_req_st_nm
                , pch_req_mgr_nm, date_format(dlv_req_dt,'%Y.%m.%d') as dlv_req_dt, pch_req_total_amt
                ,pch_req_item_qty, date_format(pch_req_rjct_dt,'%Y.%m.%d') as pch_req_rjct_dt, pch_req_rjct_nm
                ,whs_id, fn_whs_nm(brand_id, whs_id) as whs_nm
                ,location, fn_whs_location_nm(brand_id, whs_id, location) as location_nm
                ,date_format(pch_req_dt,'%Y.%m.%d') as pch_req_dt, store_id
                ,pch_req_mgr_id
        from   pch_req_base_info
        where  brand_id = #{sBrandId}
        <if test="sStoreId == null or sStoreId == ''">
            and   ((store_id is null or store_id = '') or (store_id is not null and store_id <![CDATA[ <> ]]> '' and pch_req_st_cd <![CDATA[ <> ]]> 'PR00'))
        </if>
        <if test="sStoreId != null and sStoreId != ''">
        and    store_id = #{sStoreId}
        </if>
        and    date_format(sys_reg_dttm,'%Y%m%d') between #{fromDt} and #{toDt}
        <if test="sWhsId != null and sWhsId != ''">
            and    whs_id = #{sWhsId}
        </if>
        <if test="sPchReqStCd != null and sPchReqStCd != ''">
            and    pch_req_st_cd = #{sPchReqStCd}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and    pch_req_unq_no in ( select pch_req_unq_no from pch_req_dtl_ptcl
            where date_format(sys_reg_dttm,'%Y%m%d') between #{fromDt} and #{toDt}
            and  item_id like concat('%',#{sItemNm},'%') or item_nm like concat('%',#{sItemNm},'%'))
        </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 pch_req_unq_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by pch_req_unq_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by pch_req_unq_no desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectPchReqGridCnt" resultType="int">
            select count(*) as cnt
            from   pch_req_base_info
            where  brand_id = #{sBrandId}
            <if test="sStoreId == null or sStoreId == ''">
                and   ((store_id is null or store_id = '') or (store_id is not null and store_id <![CDATA[ <> ]]> '' and pch_req_st_cd <![CDATA[ <> ]]> 'PR00'))
            </if>
            <if test="sStoreId != null and sStoreId != ''">
                and    store_id = #{sStoreId}
            </if>
            and    date_format(sys_reg_dttm,'%Y%m%d') between #{fromDt} and #{toDt}
            <if test="sWhsId != null and sWhsId != ''">
                and    whs_id = #{sWhsId}
            </if>
            <if test="sPchReqStCd != null and sPchReqStCd != ''">
                and    pch_req_st_cd = #{sPchReqStCd}
            </if>
            <if test="sItemNm != null and sItemNm != ''">
                and    pch_req_unq_no in ( select pch_req_unq_no from pch_req_dtl_ptcl
                where date_format(sys_reg_dttm,'%Y%m%d') between #{fromDt} and #{toDt}
                and  item_id like concat('%',#{sItemNm},'%') or item_nm like concat('%',#{sItemNm},'%') )
            </if>
    </select>

    <select id="selectPchReqInfo" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
        SELECT pch_req_unq_no, brand_id, fn_brand_nm(brand_id) as brand_nm, store_id, pch_req_nm, pch_req_dept
            , pch_req_mgr_nm, DATE_FORMAT(pch_req_dt,'%Y.%m.%d') as pch_req_dt, pch_req_st_cd, fn_code_nm('PCH_REQ_ST_CD', pch_req_st_cd ) AS pch_req_st_nm, pch_req_dvsn
            , note, pch_req_item_qty, pch_req_total_amt,   DATE_FORMAT(pch_req_rjct_dt,'%Y.%m.%d') as  pch_req_rjct_dt, pch_req_rjct_nm
            , pch_req_rjct_rsn, pch_req_mgr_id, pch_req_rjct_id
            ,whs_id, fn_whs_nm(brand_id, whs_id) as whs_nm
            ,location, fn_whs_location_nm(brand_id, whs_id, location) as location_nm
            , DATE_FORMAT(dlv_req_dt ,'%Y.%m.%d')  as dlv_req_dt
            , DATE_FORMAT(sys_reg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_reg_dttm, sys_reg_id, DATE_FORMAT(sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, sys_chg_id
            , nvl(pch_appr_amt,0) as pch_appr_amt, case when pch_req_total_amt = pch_appr_amt then 'Y' else 'N' end pch_appr_yn
        FROM pch_req_base_info
        WHERE brand_id = #{brandId}
        <if test="storeId != null and storeId != ''">
            and    store_id = #{storeId}
        </if>
        and   pch_req_unq_no = #{pchReqUnqNo}
    </select>

    <select id="selectPchReqDtlPtcl" resultType="com.oqpo.api.entity.pomng.PchReqDtlPtclEntity">
            SELECT ptcl.pch_req_unq_no, ptcl.pch_req_dtl_no, ptcl.item_id, ptcl.item_nm, ptcl.unit, fn_code_nm('DSTRBT_COST_UNIT',ptcl.unit) AS unit_nm
                  , ptcl.pch_req_qty, ptcl.pch_req_amt, ptcl.unit_amt, ptcl.brand_unit_unq_no, ptcl.store_unit_unq_no, ptcl.unit_gubun
                  , ptcl.pch_req_dtl_st_cd, fn_code_nm(upper('PCH_REQ_DTL_ST_CD'), ptcl.pch_req_dtl_st_cd) as pch_req_dtl_st_nm
            FROM pch_req_dtl_ptcl ptcl
           /* left outer join pch_odr_dtl_ptcl odr on odr.pch_req_unq_no = ptcl.pch_req_unq_no and odr.pch_req_dtl_no = ptcl.pch_req_dtl_no */
            WHERE ptcl.pch_req_unq_no = #{pchReqUnqNo}
    </select>


    <insert id="insertPchReqBaseInfo" >
        INSERT INTO /* PchReq.insertPchReqBaseInfo */ pch_req_base_info
            (pch_req_unq_no, brand_id, store_id, pch_req_nm, pch_req_dept
            , pch_req_mgr_nm, pch_req_dt, pch_req_tm, pch_req_st_cd, pch_req_dvsn
            , note, pch_req_item_qty, pch_req_total_amt, pch_req_rjct_dt, pch_req_rjct_nm
            , pch_req_rjct_rsn, pch_req_mgr_id, pch_req_rjct_id
            , whs_id, location,dlv_req_dt
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.pchReqUnqNo}, #{entity.brandId}, #{entity.storeId}, #{entity.pchReqNm}, #{entity.pchReqDept}
            , #{entity.pchReqMgrNm}, case when  #{entity.pchReqStCd} = 'PR20' then   date_format(now(),'%Y%m%d') else null end, null, #{entity.pchReqStCd}, #{entity.pchReqDvsn}
            , #{entity.note}, #{entity.pchReqItemQty}, #{entity.pchReqTotalAmt}, null, null
            , null, #{userId}, null
            , #{entity.whsId}, #{entity.location}, #{entity.dlvReqDt}
            , NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updateOrdPchReqBaseInfo">
        UPDATE /* PchReq.updateOrdPchReqBaseInfo */
            pch_req_base_info SET
                pch_req_dt = case when  #{entity.pchReqStCd} = 'PR20' then  date_format(now(),'%Y%m%d') else null end ,
                pch_req_nm = #{entity.pchReqNm},
                pch_req_st_cd = #{entity.pchReqStCd},
                note = #{entity.note},
                pch_req_item_qty = #{entity.pchReqItemQty},
                pch_req_total_amt = #{entity.pchReqTotalAmt},
                whs_id = #{entity.whsId},
                location = #{entity.location},
                dlv_req_dt = #{entity.dlvReqDt},
                sys_chg_dttm = NOW(),
                sys_chg_id = #{userId}
            WHERE brand_id = #{entity.brandId}
            and   pch_req_unq_no = #{entity.pchReqUnqNo}
    </update>

    <update id="updateRejPchReqBaseInfo">
        UPDATE /* PchReq.updateRejPchReqBaseInfo */
            pch_req_base_info SET
                pch_req_st_cd = 'PR40',
                pch_req_rjct_nm = #{pchReqRjctNm},
                pch_req_rjct_dt = date_format(now(),'%Y%m%d'),
                pch_req_rjct_rsn = #{pchReqRjctRsn},
                pch_req_rjct_id = #{userId},
                sys_chg_dttm = NOW(),
                sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{pchReqUnqNo}
    </update>


    <insert id="insertPchReqDtlPtcl" >
        INSERT INTO /* PchReq.insertPchReqDtlPtcl */   pch_req_dtl_ptcl
            (pch_req_unq_no, pch_req_dtl_no, item_nm, unit, pch_req_qty
            , pch_req_amt, unit_amt, unit_gubun, brand_unit_unq_no, store_unit_unq_no
            , item_id, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.pchReqUnqNo}, #{item.pchReqDtlNo}, #{item.itemNm}, #{item.unit}, #{item.pchReqQty}
            , #{item.pchReqAmt}, #{item.unitAmt}, #{item.unitGubun} , #{item.brandUnitUnqNo} , #{item.storeUnitUnqNo}
            , #{item.itemId} , NOW(), #{userId}, NOW(), #{userId})
        </foreach>

    </insert>

    <update id="updatePchReqDtlPtcl">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE /* PchReq.updatePchReqDtlPtcl */
                pch_req_dtl_ptcl SET
                    unit = #{item.unit},
                    pch_req_qty = #{item.pchReqQty},
                    pch_req_amt = #{item.pchReqAmt},
                    unit_amt = #{item.unitAmt},
                    sys_chg_dttm = NOW(),
                    sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{pchReqUnqNo}
            AND pch_req_dtl_no = #{item.pchReqDtlNo}
        </foreach>
    </update>

    <delete id="deletePchReqDtlPtcl">
        DELETE  /* PchReq.deletePchReqDtlPtcl */
            FROM pch_req_dtl_ptcl
        WHERE pch_req_unq_no = #{pchReqUnqNo}
        and    pch_req_dtl_no   in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.pchReqDtlNo}
        </foreach>

    </delete>

    <select id="selectMaxPchReqDtlNo" resultType="int">
            SELECT nvl(Max(convert(pch_req_dtl_no,int)),0)
            FROM pch_req_dtl_ptcl
            WHERE  pch_req_unq_no = #{pchReqUnqNo}
    </select>

    <select id="selectSumReqAmt" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
            select count(*) as pch_req_item_qty,  nvl(sum(pch_req_amt),0) as pch_req_total_amt
            from  pch_req_dtl_ptcl
            where pch_req_unq_no = #{pchReqUnqNo}
    </select>

    <select id="selectPchReqPoTargetGridList" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
        select base.pch_req_unq_no, ptcl.pch_req_dtl_no as 'pchReqDtlInfo.pch_req_dtl_no', base.brand_id, fn_brand_nm(base.brand_id) as brand_nm
            , base.store_id, store.store_nm
            , bitem.spply_nm as 'itemUnitInfo.spply_nm'
            , fn_whs_nm(base.brand_id, base.whs_id) as whs_nm, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
            , ptcl.item_id as 'pchReqDtlInfo.item_id', ptcl.item_nm as 'pchReqDtlInfo.item_nm', ptcl.unit_amt as 'pchReqDtlInfo.unit_amt'
            , bitem.unit_amt as 'itemUnitInfo.unit_amt' , ptcl.pch_req_qty as 'pchReqDtlInfo.pch_req_qty', date_format(base.dlv_req_dt, '%Y.%m.%d') as dlv_req_dt
            , ptcl.pch_req_amt as 'pchReqDtlInfo.pch_req_amt', bitem.unit_amt * ptcl.pch_req_qty as 'pchReqDtlInfo.pch_ord_amt'
            , bitem.lead_time as 'itemUnitInfo.lead_time', bitem.spply_id as 'itemUnitInfo.spply_id', base.whs_id, base.location
            , case when base.store_id is null or base.store_id = '' then 'DL02' else  case when item.store_deli_type = '10' then 'DL01' else 'DL02' end   end as dlv_dvsn
            , fn_code_nm('DLV_DVSN',case when base.store_id is null or base.store_id = '' then 'DL02' else  case when item.store_deli_type = '10' then 'DL01' else 'DL02' end   end) as dlv_dvsn_nm
        from pch_req_base_info base
        join pch_req_dtl_ptcl ptcl on base.pch_req_unq_no = ptcl.pch_req_unq_no
        join brand_base_info brand on base.brand_id = brand.brand_id
        join brand_item_unit_info bitem on ptcl.brand_unit_unq_no = bitem.brand_unit_unq_no
        join item_base_info item on base.brand_id = item.brand_id and ptcl.item_id = item.item_id
        left outer join store_base_info store on base.brand_id = store.brand_id and base.store_id = store.store_id
        where 1 = 1
        and base.brand_id = #{sBrandId}
        and base.pch_req_st_cd = 'PR20'
        and ptcl.pch_req_dtl_st_cd = 'PRD20'
        and brand.shmt_col_proc_type = '10'
        and base.dlv_req_dt between #{fromDt} and #{toDt}
        <if test="sSpplyId != null and sSpplyId != ''">
        and bitem.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
        and base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
        and (ptcl.item_id like concat('%', #{sItemNm}, '%') or ptcl.item_nm like concat('%', #{sItemNm}, '%'))
        </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 base.whs_id asc, base.location, bitem.spply_id, base.dlv_req_dt ,base.pch_req_unq_no, ptcl.pch_req_dtl_no
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.whs_id desc, base.location, bitem.spply_id, base.dlv_req_dt , base.pch_req_unq_no, ptcl.pch_req_dtl_no
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.whs_id asc, base.location, bitem.spply_id, base.dlv_req_dt ,base.pch_req_unq_no, ptcl.pch_req_dtl_no
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>

    </select>

    <select id="selectPchReqPoTargetGridCnt" resultType="int">
        select count(*)
        from pch_req_base_info base
        join pch_req_dtl_ptcl ptcl on base.pch_req_unq_no = ptcl.pch_req_unq_no
        join brand_base_info brand on base.brand_id = brand.brand_id
        join brand_item_unit_info bitem on ptcl.brand_unit_unq_no = bitem.brand_unit_unq_no
        join item_base_info item on base.brand_id = item.brand_id and ptcl.item_id = item.item_id
        left outer join store_base_info store on base.brand_id = store.brand_id and base.store_id = store.store_id
        where 1 = 1
        and base.brand_id = #{sBrandId}
        and base.pch_req_st_cd = 'PR20'
        and ptcl.pch_req_dtl_st_cd = 'PRD20'
        and brand.shmt_col_proc_type = '10'
        and base.dlv_req_dt between #{fromDt} and #{toDt}
        <if test="sSpplyId != null and sSpplyId != ''">
            and bitem.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and (ptcl.item_id like concat('%', #{sItemNm}, '%') or ptcl.item_nm like concat('%', #{sItemNm}, '%'))
        </if>
    </select>

    <select id="selectPchRejectPrTargetList" resultType="com.oqpo.api.entity.pomng.PchReqDtlPtclEntity">
        select  /* PchReq.selectPchRejectPrTargetList */
              base.pch_req_unq_no, ptcl.pch_req_dtl_no
            , ptcl.item_id, ptcl.item_nm
            , ptcl.pch_req_qty, ptcl.pch_req_amt
        from pch_req_base_info base
            , pch_req_dtl_ptcl ptcl
        where 1 = 1
        and base.pch_req_unq_no = ptcl.pch_req_unq_no
        and base.pch_req_st_cd = 'PR20'
        and base.pch_req_unq_no  = #{pchReqUnqNo}
    </select>

    <update id="updatePchReqStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE /* PchReq.updatePchReqDtlPtcl */
            pch_req_base_info SET
                pch_req_st_cd = #{item.pchReqStCd},
                sys_chg_dttm = NOW(),
                sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{item.pchReqUnqNo}
        </foreach>
    </update>

    <update id="updateAllPchReqDtlStCd">
            UPDATE /* PchReq.updateAllPchReqDtlStCd */
            pch_req_dtl_ptcl SET
            pch_req_dtl_st_cd = #{pchReqDtlStCd},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{pchReqUnqNo}
    </update>

    <select id="selectPchReqDtlStatusList" resultType="com.oqpo.api.entity.pomng.PchReqDtlPtclEntity">
        select pch_req_dtl_st_cd, count(*) as pch_req_status_cnt
        from pch_req_dtl_ptcl
        where pch_req_unq_no = #{pchReqUnqNo}
        group by pch_req_dtl_st_cd
    </select>

    <update id="updateSelPchReqDtlStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE /* PchReq.updateSelPchReqDtlStCd */
            pch_req_dtl_ptcl SET
            pch_req_dtl_st_cd = #{item.pchReqDtlStCd},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{item.pchReqUnqNo}
            and   pch_req_dtl_no = #{item.pchReqDtlNo}
        </foreach>
    </update>

    <select id="selectWishListMgntNo" resultType="String">
        SELECT FN_GET_DEALNO(37, '') as wish_list_mgnt_no
    </select>

    <select id="selectWishListItem" resultType="com.oqpo.api.entity.pomng.PchWishListItemEntity">
        SELECT mgnt.WISH_LIST_MGNT_NO, mgnt.WISH_LIST_NM
            , mgnt.STORE_ID, FN_STORE_NM(#{brandId}, #{storeId}) AS store_nm
            , ptcl.ITEM_ID, item.ITEM_NM, unit.UNIT_AMT, unit.UNIT, unit.STORE_UNIT_UNQ_NO, 'S' as UNIT_GUBUN
            , fn_code_nm('PODR_PSSBL_DVSN',item.podr_pssbl_dvsn) AS podr_pssbl_dvsn_nm
            , bitem.BRAND_UNIT_UNQ_NO
        FROM 
            store_wish_list_mgnt mgnt
            INNER JOIN store_wish_list_ptcl ptcl 
                ON mgnt.WISH_LIST_MGNT_NO = ptcl.WISH_LIST_MGNT_NO
            INNER JOIN store_item_unit_info unit 
                ON mgnt.STORE_ID = unit.STORE_ID AND ptcl.ITEM_ID = unit.ITEM_ID AND DATE_FORMAT(NOW(), '%Y%m%d') BETWEEN unit.CONT_STT_DT AND unit.CONT_END_DT
            INNER JOIN item_base_info item
                ON ptcl.ITEM_ID = item.ITEM_ID AND item.podr_pssbl_dvsn='PO01'
            INNER JOIN brand_item_unit_info bitem 
                ON bitem.ITEM_ID=ptcl.ITEM_ID AND bitem.BRAND_ID=unit.BRAND_ID
        WHERE 1 = 1
            AND mgnt.STORE_ID = #{storeId}
            AND mgnt.WISH_LIST_MGNT_NO = #{wishListMgntNo}
            AND unit.BRAND_ID = #{brandId}
    </select>

    <select id="selectWishListRequest" resultType="com.oqpo.api.entity.pomng.PchWishListEntity">
        SELECT mgnt.WISH_LIST_MGNT_NO, mgnt.WISH_LIST_NM
            , mgnt.STORE_ID, FN_STORE_NM(#{brandId}, #{storeId}) AS store_nm, mgnt.DEL_YN
        FROM store_wish_list_mgnt mgnt
        WHERE 1 = 1
        AND mgnt.STORE_ID = #{storeId}
        /* AND mgnt.DEL_YN = 'N' */
    </select>

    <update id="updateWishListRequest">
            UPDATE /* PchReq.updateAllPchReqDtlStCd */
            store_wish_list_mgnt SET
                wish_list_nm = #{wishListNm},
                del_yn=#{delYn},
                sys_chg_dttm = NOW(),
                sys_chg_id = #{userId}
            WHERE wish_list_mgnt_no = #{wishListMgntNo} and store_id=#{storeId}
    </update>

    <insert id="saveWishListRequest" >
        /* PchReq.store_wish_list_mgnt */
        INSERT INTO store_wish_list_mgnt
            (wish_list_mgnt_no, wish_list_nm, store_id, del_yn, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id )
        VALUES (
            #{wishListMgntNo}, #{wishListNm}, #{storeId}, 'N', NOW(), #{userId}, NOW(), #{userId}
        ) 
    </insert>

    <insert id="insertWishListItems" >
            INSERT INTO store_wish_list_ptcl
            (
                wish_list_mgnt_no, item_id, item_nm, del_yn, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id 
            ) VALUES
            <foreach collection="gridInsertData" item="item" separator=",">
             (
                #{wishListMgntNo}, #{item.itemId}, #{item.itemNm}, 'N', NOW(), #{userId}, NOW(), #{userId}
            )
            </foreach>
    </insert>


    <delete id="deleteWishListItems">
        DELETE FROM store_wish_list_ptcl  /* deleteWishListItems */
        WHERE wish_list_mgnt_no = #{wishListMgntNo}
        AND item_id in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.itemId}
        </foreach>
    </delete>

    <update id="updateWishListItems">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update store_wish_list_ptcl set del_yn=#{item.delYn}, sys_chg_dttm=NOW(), sys_chg_id=#{userId}
            WHERE wish_list_mgnt_no = #{wishListMgntNo} and item_id=#{item.itemId}
        </foreach>
    </update>


    <select id="selectPchReqPoSteamlineGridList" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
        select base.pch_req_unq_no, ptcl.pch_req_dtl_no as 'pchReqDtlInfo.pch_req_dtl_no', base.brand_id, fn_brand_nm(base.brand_id) as brand_nm
        , base.store_id, store.store_nm
        , bitem.spply_nm as 'itemUnitInfo.spply_nm'
        , fn_whs_nm(base.brand_id, base.whs_id) as whs_nm, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
        , ptcl.item_id as 'pchReqDtlInfo.item_id', ptcl.item_nm as 'pchReqDtlInfo.item_nm', ptcl.unit_amt as 'pchReqDtlInfo.unit_amt'
        , bitem.unit_amt as 'itemUnitInfo.unit_amt' , ptcl.pch_req_qty as 'pchReqDtlInfo.pch_req_qty', date_format(base.dlv_req_dt, '%Y.%m.%d') as dlv_req_dt
        , ptcl.pch_req_amt as 'pchReqDtlInfo.pch_req_amt', bitem.unit_amt * ptcl.pch_req_qty as 'pchReqDtlInfo.pch_ord_amt'
        , bitem.lead_time as 'itemUnitInfo.lead_time', bitem.spply_id as 'itemUnitInfo.spply_id', base.whs_id, base.location
        , case when base.store_id is null or base.store_id = '' then 'DL02' else  case when item.store_deli_type = '10' then 'DL01' else 'DL02' end   end as dlv_dvsn
        , fn_code_nm('DLV_DVSN',case when base.store_id is null or base.store_id = '' then 'DL02' else  case when item.store_deli_type = '10' then 'DL01' else 'DL02' end   end) as dlv_dvsn_nm
        from pch_req_base_info base
        join pch_req_dtl_ptcl ptcl on base.pch_req_unq_no = ptcl.pch_req_unq_no
        join brand_item_unit_info bitem on ptcl.brand_unit_unq_no = bitem.brand_unit_unq_no
        join brand_base_info brand on base.brand_id = brand.brand_id
        join item_base_info item on base.brand_id = item.brand_id and ptcl.item_id = item.item_id
        left outer join store_base_info store on base.brand_id = store.brand_id and base.store_id = store.store_id
        where 1 = 1
        and base.brand_id = #{sBrandId}
        and base.pch_req_st_cd = 'PR20'
        and ptcl.pch_req_dtl_st_cd = 'PRD20'
        and brand.shmt_col_proc_type = '20'
        and base.dlv_req_dt between #{fromDt} and #{toDt}
        <if test="sWhsId != null and sWhsId != ''">
            and base.whs_id = #{sWhsId}
        </if>
        <if test="sPchReqUnqNo != null and sPchReqUnqNo != ''">
            and base.pch_req_unq_no = #{sPchReqUnqNo}
        </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 pch_req_unq_no asc , ptcl.pch_req_dtl_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by pch_req_unq_no desc , ptcl.pch_req_dtl_no asc
                    </if>
                </if>
            </when>
            <otherwise>
                order by pch_req_unq_no asc , ptcl.pch_req_dtl_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>

    </select>

    <select id="selectPchReqPoSteamlineCnt" resultType="int">
        select count(*)
        from pch_req_base_info base
        join pch_req_dtl_ptcl ptcl on base.pch_req_unq_no = ptcl.pch_req_unq_no
        join brand_item_unit_info bitem on ptcl.brand_unit_unq_no = bitem.brand_unit_unq_no
        join brand_base_info brand on base.brand_id = brand.brand_id
        join item_base_info item on base.brand_id = item.brand_id and ptcl.item_id = item.item_id
        left outer join store_base_info store on base.brand_id = store.brand_id and base.store_id = store.store_id
        where 1 = 1
        and base.brand_id = #{sBrandId}
        and base.pch_req_st_cd = 'PR20'
        and ptcl.pch_req_dtl_st_cd = 'PRD20'
        and brand.shmt_col_proc_type = '20'
        and base.dlv_req_dt between #{fromDt} and #{toDt}
        <if test="sWhsId != null and sWhsId != ''">
            and base.whs_id = #{sWhsId}
        </if>
        <if test="sPchReqUnqNo != null and sPchReqUnqNo != ''">
            and base.pch_req_unq_no = #{sPchReqUnqNo}
        </if>
    </select>

    <select id="selectPchReqStatProcYn" resultType="String">
        select case when pch_req_st_cd = 'PR00' then 'Y' else 'N' end
        from   pch_req_base_info
        where  brand_id = #{brandId}
        and    pch_req_unq_no = #{pchReqUnqNo}
    </select>

    <update id="updateStreamListPchReqDtlStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE /* PchReq.updateStreamListPchReqDtlStCd */
            pch_req_dtl_ptcl SET
            pch_req_dtl_st_cd = 'PRD30',
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{item.pchReqUnqNo}
        </foreach>
    </update>

    <update id="updateStreamListPchReqStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE /* PchReq.updateStreamListPchReqStCd */
            pch_req_base_info SET
            pch_req_st_cd ='PR30',
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE pch_req_unq_no = #{item.pchReqUnqNo}
        </foreach>
    </update>

    <select id="selectPchPoStreamlineReqList" resultType="com.oqpo.api.entity.pomng.PchReqBaseInfoEntity">
        select distinct pch_req_unq_no
        from pch_req_base_info
        where brand_id = #{brandId}
        and   PCH_REQ_ST_CD = 'PR20'
        AND pch_req_unq_no in
        <foreach collection="gridInsertData" item="item" separator="," open="(" close=")">
            #{item.pchReqUnqNo}
        </foreach>
    </select>

</mapper>
