<?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.rtnmng.RtnOdrMapper">

    <select id="selectRecRtnInitInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
        select 	/* RtnOrd.selectRecRtnPassInitInfo  */ distinct	base.brand_id, fn_brand_nm(base.brand_id) as brand_nm
                ,  base.rtn_whs_id, base.rtn_location
                , fn_whs_nm(base.brand_id, base.rtn_whs_id) as rtn_whs_nm
                , fn_whs_location_nm(base.brand_id, base.rtn_whs_id,base.rtn_location) as rtn_location_nm
                , case when base.store_id is null or base.store_id = '' then 'C002' else 'C001' end as col_dvsn
                , fn_code_nm('COL_DVSN',case when base.store_id is null or base.store_id = '' then 'C002' else 'C001' end) as col_dvsn_nm
                , base.rtn_whs_id, base.store_id
        from rtn_req_base_info base
        , rtn_req_dtl_ptcl ptcl
        where 1 = 1
        and base.rtn_req_unq_no = ptcl.rtn_req_unq_no
        and base.brand_id = #{brandId}
        and base.rtn_req_st_cd = 'RR20'
        AND concat(base.rtn_req_unq_no, ptcl.rtn_req_dtl_no) IN
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
            concat(#{item.rtnReqUnqNo},#{item.rtnReqDtlNo})
        </foreach>
    </select>

    <select id="selectRecRtnDtlInitInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity">
        select ptcl.rtn_req_unq_no , ptcl.rtn_req_dtl_no
            , ptcl.item_id , ptcl.item_nm , ptcl.unit_amt , ptcl.unit
            , ptcl.rtn_qty as rtn_req_qty, ptcl.rtn_qty as rtn_qty, ptcl.rtn_amt
            , ptcl.spply_id, fn_spply_nm(ptcl.spply_id) as spply_nm
            , ptcl.brand_unit_unq_no, ptcl.store_unit_unq_no
            , ptcl.rtn_rsn_dvsn, ptcl.rtn_rsn, ptcl.unit_gubun
            , fn_code_nm('RTN_RSN_DVSN',ptcl.rtn_rsn_dvsn) as rtn_rsn_dvsn_nm
            ,date_format(base.col_req_dt , '%Y.%m.%d') as col_req_dt
        from rtn_req_base_info base
            , rtn_req_dtl_ptcl ptcl
        where 1 = 1
        and base.rtn_req_unq_no = ptcl.rtn_req_unq_no
        and base.brand_id = 'BRD221000001'
        and base.rtn_req_st_cd = 'RR20'
        and concat(base.rtn_req_unq_no, ptcl.rtn_req_dtl_no) in
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
           concat(#{item.rtnReqUnqNo},#{item.rtnReqDtlNo})
        </foreach>
    </select>

    <select id="selectRtnInitInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
             select brand_id, brand_nm, 'C002' as col_dvsn
                    , fn_code_nm('COL_DVSN','C002')  as col_dvsn_nm
             from   brand_base_info
             where  brand_id = #{brandId}
     </select>

    <insert id="insertRtnOdrBaseInfo">
            insert into rtn_odr_base_info
            (rtn_podr_unq_no, brand_id, rtn_odr_reg_dt, rtn_req_dvsn, rtn_dodr_total_amt
            , rtn_dodr_item_qty, rtn_mgr_nm, rtn_mgr_id, rtn_mgr_tel_no , rtn_odr_st_cd
            , spply_id, whs_id, location, col_dvsn
            , col_req_dt, store_id, rtn_whs_id, rtn_location
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
            values
            (#{entity.rtnPodrUnqNo}, #{entity.brandId}, date_format(now(),'%Y%m%d'), #{entity.rtnReqDvsn}, #{entity.rtnDodrTotalAmt}
            , #{entity.rtnDodrItemQty}, #{entity.rtnMgrNm}, #{entity.rtnMgrId}, #{entity.rtnMgrTelNo},'RO20'
            , #{entity.spplyId}, #{entity.whsId}, #{entity.location}, #{entity.colDvsn}
            , #{entity.colReqDt}, #{entity.storeId}, #{entity.rtnWhsId}, #{entity.rtnLocation}
            , NOW(), #{userId}, NOW(), #{userId})
        </insert>

        <insert id="insertRtnOdrDtlPtcl">
            insert into rtn_odr_dtl_ptcl
            (rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_unq_no, rtn_req_dtl_no, item_id
            , item_nm, unit, unit_amt, col_req_dt, rtn_dvsn, rtn_req_qty
            , rtn_qty, rtn_amt, rtn_rsn_dvsn, rtn_rsn, rtn_odr_qty
            , rtn_odr_amt, rodr_dtl_st_cd
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
            values
            <foreach collection="gridInsertData" item="item" separator=",">
               (#{item.rtnPodrUnqNo}, #{item.rtnPodrDtlNo}, #{item.rtnReqUnqNo}, #{item.rtnReqDtlNo}, #{item.itemId}
            , #{item.itemNm}, #{item.unit}, #{item.unitAmt}, #{item.colReqDt}, #{item.rtnDvsn}, #{item.rtnReqQty}
            , #{item.rtnQty}, #{item.rtnAmt}, #{item.rtnRsnDvsn}, #{item.rtnRsn}, #{item.rtnOdrQty}
            , #{item.rtnOdrAmt}, #{item.rodrDtlStCd}, NOW(), #{userId}, NOW(), #{userId})
            </foreach>
        </insert>


        <select id="selectPoRtnDtlSaveInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity">
            select ptcl.item_id, ptcl.item_nm,  ptcl.unit, ptcl.unit_amt, bunit.spply_id
            from  rtn_req_dtl_ptcl ptcl
              join rtn_req_base_info base on ptcl.rtn_req_unq_no = base.rtn_req_unq_no
              join brand_item_unit_info bunit on ptcl.brand_unit_unq_no = bunit.brand_unit_unq_no
            where 1=1
            and   base.rtn_req_unq_no = #{rtnReqUnqNo}
            and   ptcl.rtn_req_dtl_no = #{rtnReqDtlNo}
        </select>


        <select id="selectRtnItemDtlSaveInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity">
            select item_id, item_nm, unit, unit_amt, spply_id
            from brand_item_unit_info
            where 1=1
            and   brand_unit_unq_no = #{brandUnitUnqNo}
            and   brand_id = #{brandId}
            and   item_id = #{itemId}
            and   date_format(now(), '%Y%m%d') between cont_stt_dt and cont_end_dt
        </select>


    <select id="selectRtnOdrGridList" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
        select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.rtn_podr_unq_no, base.rtn_odr_st_cd, fn_code_nm('RTN_ODR_ST_CD',base.rtn_odr_st_cd) as rtn_odr_st_nm
            , date_format(base.rtn_odr_reg_dt,'%Y.%m.%d') as rtn_odr_reg_dt, base.rtn_whs_id, fn_whs_nm(base.brand_id,base.rtn_whs_id) as rtn_whs_nm, base.spply_id
            , fn_spply_nm(base.spply_id) as spply_nm, base.rtn_dodr_total_amt, base.rtn_dodr_item_qty, date_format(base.col_req_dt,'%Y.%m.%d') as col_req_dt
            , base.col_dvsn,  fn_code_nm('COL_DVSN', base.col_dvsn) as col_dvsn_nm, base.whs_id, fn_whs_nm(base.brand_id,base.whs_id) as whs_nm
        from   rtn_odr_base_info base
        where  1=1
        and    base.brand_id = #{sBrandId}
        and    base.rtn_odr_reg_dt between  #{fromDt} and  #{toDt}
        <if test="sRtnOdrStCd != null and sRtnOdrStCd != ''">
        and    base.rtn_odr_st_cd = #{sRtnOdrStCd}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
        and    base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sColDvsn != null and sColDvsn != ''">
        and    base.col_dvsn = #{sColDvsn}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
        and    base.whs_id = #{sWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
        and    base.spply_id = #{sSpplyId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
        and    base.rtn_podr_unq_no in (select ba.rtn_podr_unq_no
                                            from   rtn_odr_base_info ba
                                            , rtn_odr_dtl_ptcl ptcl
                                            where ba.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
                                            and    base.brand_id = #{sBrandId}
                                            and   ba.rtn_odr_reg_dt between  #{fromDt} and  #{toDt}
                                            <if test="sRtnOdrStCd != null and sRtnOdrStCd != ''">
                                            and    ba.rtn_odr_st_cd = #{sRtnOdrStCd}
                                            </if>
                                            <if test="sRtnWhsId != null and sRtnWhsId != ''">
                                            and    ba.RTN_WHS_ID = #{sRtnWhsId}
                                            </if>
                                            <if test="sColDvsn != null and sColDvsn != ''">
                                            and    ba.col_dvsn = #{sColDvsn}
                                            </if>
                                            <if test="sWhsId != null and sWhsId != ''">
                                            and    ba.whs_id = #{sWhsId}
                                            </if>
                                            <if test="sSpplyId != null and sSpplyId != ''">
                                            and    ba.spply_id = #{sSpplyId}
                                            </if>
                                            and   ( ba.rtn_podr_unq_no like concat('%',#{sItemNm},'%') or
                                                    ptcl.item_id like concat('%',#{sItemNm},'%') or ptcl.item_nm like conct('%',#{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.rtn_odr_st_cd asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.rtn_odr_st_cd desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.rtn_odr_st_cd desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectRtnOdrGridCnt" resultType="int">
        select count(*)
        from   rtn_odr_base_info base
        where  1=1
        and    base.brand_id = #{sBrandId}
        and    base.rtn_odr_reg_dt between  #{fromDt} and  #{toDt}
        <if test="sRtnOdrStCd != null and sRtnOdrStCd != ''">
            and    base.rtn_odr_st_cd = #{sRtnOdrStCd}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sColDvsn != null and sColDvsn != ''">
            and    base.col_dvsn = #{sColDvsn}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and    base.whs_id = #{sWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
            and    base.spply_id = #{sSpplyId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and    base.rtn_podr_unq_no in (select ba.rtn_podr_unq_no
                                            from   rtn_odr_base_info ba
                                            , rtn_odr_dtl_ptcl ptcl
                                            where ba.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
                                            and    base.brand_id = #{sBrandId}
                                            and   ba.rtn_odr_reg_dt between  #{fromDt} and  #{toDt}
                                            <if test="sRtnOdrStCd != null and sRtnOdrStCd != ''">
                                                and    ba.rtn_odr_st_cd = #{sRtnOdrStCd}
                                            </if>
                                            <if test="sRtnWhsId != null and sRtnWhsId != ''">
                                                and    ba.RTN_WHS_ID = #{sRtnWhsId}
                                            </if>
                                            <if test="sColDvsn != null and sColDvsn != ''">
                                                and    ba.col_dvsn = #{sColDvsn}
                                            </if>
                                            <if test="sWhsId != null and sWhsId != ''">
                                                and    ba.whs_id = #{sWhsId}
                                            </if>
                                            <if test="sSpplyId != null and sSpplyId != ''">
                                                and    ba.spply_id = #{sSpplyId}
                                            </if>
                                            and   ( ba.rtn_podr_unq_no like concat('%',#{sItemNm},'%') or
                                            ptcl.item_id like concat('%',#{sItemNm},'%') or ptcl.item_nm like conct('%',#{sItemNm},'%') )
        </if>

    </select>

    <select id="selectRtnOdrInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
              select rtn_podr_unq_no, brand_id, fn_brand_nm(brand_id) as brand_nm, date_format(rtn_odr_reg_dt,'%Y.%m.%d') as rtn_odr_reg_dt
                  , rtn_req_dvsn, fn_code_nm('RTN_REQ_DVSN', rtn_req_dvsn) as rtn_req_dvsn_nm, rtn_dodr_total_amt
                  , rtn_dodr_item_qty, rtn_mgr_nm, rtn_mgr_id, fn_get_telno(rtn_mgr_tel_no) as rtn_mgr_tel_no
                    , rtn_odr_st_cd , fn_code_nm('RTN_ODR_ST_CD',rtn_odr_st_cd) AS rtn_odr_st_nm
                  , spply_id, fn_spply_nm(spply_id) as spply_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
                    , col_dvsn, fn_code_nm('COL_DVSN', col_dvsn) as col_dvsn_nm,  date_format(col_req_dt ,'%Y.%m.%d') as col_req_dt
                  , store_id, fn_store_nm( brand_id, store_id) as store_nm,  rtn_whs_id, rtn_location
                  ,fn_whs_nm(brand_id, rtn_whs_id) as rtn_whs_nm, fn_whs_location_nm(brand_id, rtn_whs_id, rtn_location) as rtn_location_nm
            from rtn_odr_base_info
            where 1=1
            and  rtn_podr_unq_no = #{rtnPodrUnqNo}
           </select>

    <select id="selectROdrDtlList" resultType="com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity">
               select rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_unq_no, rtn_req_dtl_no
                     , item_id, item_nm, unit, unit_amt, date_format(col_req_dt, '%Y.%m.%d') as col_req_dt
                     , rtn_req_qty, rtn_dvsn, fn_code_nm('RTN_DVSN',rtn_dvsn) as rtn_dvsn_nm,   rtn_qty, rtn_amt
                      , rtn_rsn_dvsn, fn_code_nm('RTN_RSN_DVSN',rtn_rsn_dvsn) as rtn_rsn_dvsn_nm
                     , rtn_rsn, rtn_odr_qty, rtn_odr_amt, rodr_dtl_st_cd, fn_code_nm('RODR_DTL_ST_CD',rodr_dtl_st_cd) as rodr_dtl_st_nm
                from rtn_odr_dtl_ptcl
                where rtn_podr_unq_no = #{rtnPodrUnqNo}
    </select>

    <update id="updateChgRodrStCd">
            UPDATE /* RtnOrd.updateChgRodrStCd */
            rtn_odr_base_info SET
            rtn_odr_st_cd = #{rtnOdrStCd},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE rtn_podr_unq_no = #{rtnPodrUnqNo}
    </update>

    <update id="updateChangeRodrDtlStCd">
            UPDATE /* RtnOrd.updateChangeRodrDtlStCd */
            rtn_odr_dtl_ptcl SET
            rodr_dtl_st_cd = #{rodrDtlStCd},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE rtn_podr_unq_no = #{rtnPodrUnqNo}
    </update>



    <!--



           <select id="selectPchOdrGridList" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
               select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.pch_podr_unq_no
                   , base.pch_odr_st_cd, fn_code_nm(upper('pch_odr_st_cd'), base.pch_odr_st_cd) as pch_odr_st_nm
                   , date_format(base.podr_dt, '%Y.%m.%d') as podr_dt
                   , 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
                   , base.spply_id, fn_spply_nm( base.spply_id) as spply_nm
                   , base.podr_total_amt, base.podr_item_qty, base.podr_mgr_nm
                   , fn_whs_nm(base.brand_id, base.shmt_whs_id) as shmt_whs_nm
                   , fn_whs_location_nm(base.brand_id, base.shmt_whs_id, base.shmt_location) as shmt_location_nm
               from pch_odr_base_info base
               where 1 = 1
               and base.brand_id = #{sBrandId}
               and base.podr_dt between #{fromDt} and  #{toDt}
               <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
               and base.pch_odr_st_cd = #{sPchOdrStCd}
               </if>
               <if test="sSpplyId != null and sSpplyId != ''">
               and base.spply_id = #{sSpplyId}
               </if>
               <if test="sWhsId != null and sWhsId != ''">
               and base.whs_id = #{sWhsId}
               </if>
               <if test="sItemNm != null and sItemNm != ''">
               and ((base.pch_podr_unq_no like concat('%', #{sItemNm}, '%')) or
                     (base.pch_podr_unq_no in (select base.pch_podr_unq_no
                                               from pch_odr_base_info base
                                               , pch_odr_dtl_ptcl ptcl
                                               where 1 = 1
                                               and base.brand_id = #{sBrandId}
                                               and base.pch_podr_unq_no = ptcl.pch_podr_unq_no
                                               and base.podr_dt between #{fromDt} and  #{toDt}
                                               <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                                               and base.pch_odr_st_cd = #{sPchOdrStCd}
                                               </if>
                                               <if test="sSpplyId != null and sSpplyId != ''">
                                               and base.spply_id =  #{sSpplyId}
                                               </if>
                                               <if test="sSpplyId != null and sSpplyId != ''">
                                               and base.whs_id = #{sWhsId}
                                               </if>
                                               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 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="selectPchOdrGridCnt" resultType="int">
               select count(*) as cnt
               from pch_odr_base_info base
               where 1 = 1
               and base.brand_id = #{sBrandId}
               and base.podr_dt between #{fromDt} and  #{toDt}
               <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                   and base.pch_odr_st_cd = #{sPchOdrStCd}
               </if>
               <if test="sSpplyId != null and sSpplyId != ''">
                   and base.spply_id = #{sSpplyId}
               </if>
               <if test="sWhsId != null and sWhsId != ''">
                   and base.whs_id = #{sWhsId}
               </if>
               <if test="sItemNm != null and sItemNm != ''">
                   and ((base.pch_podr_unq_no like concat('%', #{sItemNm}, '%')) or
                   (base.pch_podr_unq_no in (select base.pch_podr_unq_no
                   from pch_odr_base_info base
                   , pch_odr_dtl_ptcl ptcl
                   where 1 = 1
                   and base.pch_podr_unq_no = ptcl.pch_podr_unq_no
                   and base.podr_dt between #{fromDt} and  #{toDt}
                   <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                       and base.pch_odr_st_cd = #{sPchOdrStCd}
                   </if>
                   <if test="sSpplyId != null and sSpplyId != ''">
                       and base.spply_id =  #{sSpplyId}
                   </if>
                   <if test="sSpplyId != null and sSpplyId != ''">
                       and base.whs_id = #{sWhsId}
                   </if>
                   and (ptcl.item_id like concat('%', #{sItemNm}, '%') or ptcl.item_nm like concat('%', #{sItemNm}, '%')))))
               </if>

           </select>

           <select id="selectPchOdrInfo" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
               select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.pch_podr_unq_no
                   , base.pch_odr_st_cd, fn_code_nm(upper('pch_odr_st_cd'), base.pch_odr_st_cd) as pch_odr_st_nm
                   , base.spply_id, fn_spply_nm(base.spply_id) as spply_nm, date_format(base.podr_dt, '%Y.%m.%d') as podr_dt
                   , 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
                   , fn_whs_nm(base.brand_id, base.shmt_whs_id) as shmt_whs_nm
                   , fn_whs_location_nm(base.brand_id, base.shmt_whs_id, base.shmt_location) as shmt_location_nm
                   , base.podr_total_amt, base.podr_item_qty, base.podr_mgr_nm, base.podr_mgr_tel_no
                   , fn_code_nm(upper('dlv_dvsn'),base.dlv_dvsn) as dlv_dvsn_nm,  date_format(base.dlv_req_dt, '%y.%m.%d') as dlv_req_dt
                   , base.store_id
               from pch_odr_base_info base
               where 1 = 1
               and base.pch_podr_unq_no = #{pchPodrUnqNo}
           </select>

           <select id="selectOdrDtlList" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
               select ptcl.pch_podr_dtl_no, ptcl.item_id, ptcl.item_nm
                   , ptcl.unit_amt, ptcl.unit, ptcl.podr_qty, ptcl.podr_amt
                   , nvl((stck.stck_qty - stck.wait_qty),0) as whs_pass_qty
                   , ptcl.podr_dtl_st_cd
                   , fn_code_nm(upper('podr_dtl_st_cd'),ptcl.podr_dtl_st_cd ) as podr_dtl_st_nm
               from  pch_odr_base_info base
               join  pch_odr_dtl_ptcl ptcl on base.pch_podr_unq_no = ptcl.pch_podr_unq_no
               left outer join stck_mgnt_base_info stck on stck.brand_id = base.brand_id and stck.whs_id = base.shmt_whs_id and stck.location = base.shmt_location
               where 1 = 1
               and ptcl.pch_podr_unq_no = #{pchPodrUnqNo}
           </select>

           <select id="selectOdrDtlStatusList" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
               select podr_dtl_st_cd, count(*) as podr_status_cnt
               from pch_odr_dtl_ptcl
               where pch_podr_unq_no = #{pchPodrUnqNo}
               group by podr_dtl_st_cd
           </select>

           <update id="updateCancelPodrDtl">
               <foreach collection="gridUpdateData" item="item" separator=";">
                   UPDATE /* PchOrd.updateCancelPodrDtl */
                   pch_odr_dtl_ptcl SET
                   podr_dtl_st_cd = 'POD2',
                   sys_chg_dttm = NOW(),
                   sys_chg_id = #{userId}
                   WHERE pch_podr_unq_no = #{item.pchPodrUnqNo}
                   AND pch_podr_dtl_no = #{item.pchPodrDtlNo}
               </foreach>
           </update>


           <update id="updateChgPodrStCd">
                   UPDATE /* PchOrd.updateChgPodrStCd */
                   pch_odr_base_info SET
                   pch_odr_st_cd = #{pchOdrStCd},
                   sys_chg_dttm = NOW(),
                   sys_chg_id = #{userId}
                   WHERE pch_podr_unq_no = #{pchPodrUnqNo}
           </update>

           <update id="updateChangeStPodrDtl">
               <foreach collection="gridUpdateData" item="item" separator=";">
                   UPDATE /* PchOrd.updateChangeStPodrDtl */
                   pch_odr_dtl_ptcl SET
                   podr_dtl_st_cd = #{item.podrDtlStCd},
                   sys_chg_dttm = NOW(),
                   sys_chg_id = #{userId}
                   WHERE pch_podr_unq_no = #{item.pchPodrUnqNo}
                   AND pch_podr_dtl_no = #{item.pchPodrDtlNo}
               </foreach>
           </update>

           <update id="updateChgMultiPodrStCd">
               <foreach collection="gridUpdateData" item="item" separator=";">
                   UPDATE /* PchOrd.updateChgPodrStCd */
                   pch_odr_base_info SET
                   pch_odr_st_cd = #{item.pchOdrStCd},
                   sys_chg_dttm = NOW(),
                   sys_chg_id = #{userId}
                   WHERE pch_podr_unq_no = #{item.pchPodrUnqNo}
               </foreach>
           </update>

           <select id="selectSpplyPoGridList" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
               select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.pch_podr_unq_no
               , base.pch_odr_st_cd, fn_code_nm(upper('pch_odr_st_cd'), base.pch_odr_st_cd) as pch_odr_st_cd_nm
               , date_format(base.podr_dt, '%Y.%m.%d %H:%i:%s') as podr_dt
               , date_format(base.dlv_req_dt, '%Y.%m.%d %H:%i:%s') as dlv_req_dt
               , 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
               , base.podr_total_amt, base.podr_item_qty, base.podr_mgr_nm
               from pch_odr_base_info base
               where 1 = 1
               and base.brand_id = #{sBrandId}
               and base.spply_id = #{sSpplyId}
               and base.podr_dt between #{fromDt} and #{toDt}
               <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                   and base.pch_odr_st_cd = #{sPchOdrStCd}
               </if>
               <if test="sWhsId != null and sWhsId != ''">
                   and base.whs_id = #{sWhsId}
               </if>
               <if test="sItemNm != null and sItemNm != ''">
                   and ((base.pch_podr_unq_no like concat('%', #{sItemNm}, '%')) or
                   (base.pch_podr_unq_no in (select base.pch_podr_unq_no
                   from pch_odr_base_info base
                   , pch_odr_dtl_ptcl ptcl
                   where 1 = 1
                   and base.pch_podr_unq_no = ptcl.pch_podr_unq_no
                   and base.brand_id = #{sBrandId}
                   and base.spply_id = #{sSpplyId}
                   and base.podr_dt between #{fromDt} and #{toDt}
                   <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                       and base.pch_odr_st_cd = #{sPchOdrStCd}
                   </if>
                   <if test="sWhsId != null and sWhsId != ''">
                       and base.whs_id = #{sWhsId}
                   </if>
                   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 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="selectSpplyPoGridCnt" resultType="int">
               select count(*)
               from pch_odr_base_info base
               where 1 = 1
               and base.brand_id = #{sBrandId}
               and base.spply_id = #{sSpplyId}
               and base.podr_dt between #{fromDt} and #{toDt}
               <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                   and base.pch_odr_st_cd = #{sPchOdrStCd}
               </if>
               <if test="sWhsId != null and sWhsId != ''">
                   and base.whs_id = #{sWhsId}
               </if>
               <if test="sItemNm != null and sItemNm != ''">
                   and ((base.pch_podr_unq_no like concat('%', #{sItemNm}, '%')) or
                   (base.pch_podr_unq_no in (select base.pch_podr_unq_no
                   from pch_odr_base_info base
                   , pch_odr_dtl_ptcl ptcl
                   where 1 = 1
                   and base.pch_podr_unq_no = ptcl.pch_podr_unq_no
                   and base.brand_id = #{sBrandId}
                   and base.spply_id = #{sSpplyId}
                   and base.podr_dt between #{fromDt} and #{toDt}
                   <if test="sPchOdrStCd != null and sPchOdrStCd != ''">
                       and base.pch_odr_st_cd = #{sPchOdrStCd}
                   </if>
                   <if test="sWhsId != null and sWhsId != ''">
                       and base.whs_id = #{sWhsId}
                   </if>
                   and (ptcl.item_id like concat('%', #{sItemNm}, '%') or ptcl.item_nm like concat('%', #{sItemNm}, '%')))))
               </if>
           </select>

           <select id="selectSpplyPchOdrInfo" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
               select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.pch_podr_unq_no
                   , base.pch_odr_st_cd, fn_code_nm(upper('pch_odr_st_cd'), base.pch_odr_st_cd) as pch_odr_st_nm
                   , base.spply_id, fn_spply_nm(base.spply_id) as spply_nm, date_format(base.podr_dt, '%Y.%m.%d') as podr_dt
                   , 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
                   , fn_whs_nm(base.brand_id, base.shmt_whs_id) as shmt_whs_nm
                   , fn_whs_location_nm(base.brand_id, base.shmt_whs_id, base.shmt_location) as shmt_location_nm
                   , base.podr_total_amt, base.podr_item_qty, base.podr_mgr_nm, base.podr_mgr_tel_no
                   , fn_code_nm(upper('dlv_dvsn'),base.dlv_dvsn) as dlv_dvsn_nm,  date_format(base.dlv_req_dt, '%y.%m.%d') as dlv_req_dt
                   , base.store_id
               from pch_odr_base_info base
               where 1 = 1
               and base.brand_id = #{brandId}
               and base.pch_podr_unq_no = #{pchPodrUnqNo}
               and base.spply_id = #{spplyId}
           </select>

           <select id="selectSpplyOdrDtlList" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
               select ptcl.pch_podr_dtl_no, ptcl.item_id, ptcl.item_nm
                   , ptcl.unit_amt, ptcl.unit, ptcl.podr_qty, ptcl.podr_amt
                   , ptcl.podr_dtl_st_cd
                   , fn_code_nm(upper('podr_dtl_st_cd'),ptcl.podr_dtl_st_cd ) as podr_dtl_st_nm
               from  pch_odr_base_info base
               join  pch_odr_dtl_ptcl ptcl on base.pch_podr_unq_no = ptcl.pch_podr_unq_no
               where 1 = 1
               and base.brand_id = #{brandId}
               and ptcl.pch_podr_unq_no = #{pchPodrUnqNo}
               and base.spply_id = #{spplyId}
           </select>

       -->


</mapper>
