<?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.inoutmng.ColOdrMapper">
    <select id="selectColRtnIngCnt" resultType="int">
        select count(*)  /* ColOdr.selectColRtnIngCnt */
        from  col_odr_base_info base
        ,col_odr_dtl_ptcl  ptcl
        where base.col_cmd_unq_no = ptcl.col_cmd_unq_no
        and   base.brand_id = #{brandId}
        and   base.col_cmd_st_cd in ('COL1','COL2')
        and   ptcl.rtn_podr_unq_no = #{rtnPodrUnqNo}
    </select>

    <select id="selectColTargetRtnGridList" 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, ptcl.rtn_podr_dtl_no as 'rtnOdrDtlInfo.rtn_podr_dtl_no'
            , ptcl.rtn_req_unq_no as 'rtnOdrDtlInfo.rtn_req_unq_no', ptcl.rtn_req_dtl_no as 'rtnOdrDtlInfo.rtn_req_dtl_no'
            , ptcl.item_id as 'rtnOdrDtlInfo.item_id', ptcl.item_nm as 'rtnOdrDtlInfo.item_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
            , ptcl.unit_amt as 'rtnOdrDtlInfo.unit_amt'
            , ptcl.rtn_req_qty as 'rtnOdrDtlInfo.rtn_req_qty',  ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.rtn_odr_qty'
            , date_format(base.col_req_dt,'%Y.%m.%d') as  col_req_dt, ptcl.rtn_amt as 'rtnOdrDtlInfo.rtn_amt'
            , base.whs_id,  fn_whs_nm(base.brand_id, base.whs_id) as whs_nm
            , base.location, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
            , base.store_id
        from rtn_odr_base_info base
             , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C001'
        <if test="sWhsId != null and sWhsId != ''">
        and    base.whs_id = #{sWhsId}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
        and    base.rtn_whs_id = #{sRtnWhsId}
        </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.rtn_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.rtn_podr_unq_no desc , ptcl.rtn_podr_dtl_no asc
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.rtn_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColTargetRtnGridCnt" resultType="int">
        select count(*) as cnt
        from rtn_odr_base_info base
            , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C001'
        <if test="sWhsId != null and sWhsId != ''">
            and    base.whs_id = #{sWhsId}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </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="selectColInstRtnGridList" 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, ptcl.rtn_podr_dtl_no as 'rtnOdrDtlInfo.rtn_podr_dtl_no'
            , ptcl.rtn_req_unq_no as 'rtnOdrDtlInfo.rtn_req_unq_no', ptcl.rtn_req_dtl_no as 'rtnOdrDtlInfo.rtn_req_dtl_no'
            , ptcl.item_id as 'rtnOdrDtlInfo.item_id', ptcl.item_nm as 'rtnOdrDtlInfo.item_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
            , ptcl.unit_amt as 'rtnOdrDtlInfo.unit_amt'
            , ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.col_qty',  ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.rtn_odr_qty'
            , date_format(base.col_req_dt,'%Y.%m.%d') as  col_req_dt, ptcl.rtn_amt as 'rtnOdrDtlInfo.rtn_amt'
            , base.whs_id,  fn_whs_nm(base.brand_id, base.whs_id) as whs_nm
            , base.location, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
            , base.store_id
        from rtn_odr_base_info base
            , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{brandId}
        and concat(base.rtn_podr_unq_no, ptcl.rtn_podr_dtl_no) in
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
            concat(#{item.rtnPodrUnqNo},#{item.rtnPodrDtlNo})
        </foreach>
    </select>


    <select id="selectPickJobDataList" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
        select  base.brand_id,base.rtn_podr_unq_no, ptcl.rtn_podr_dtl_no as 'rtnOdrDtlInfo.rtn_podr_dtl_no'
            ,ptcl.item_id as 'rtnOdrDtlInfo.item_id' , ptcl.item_nm as 'rtnOdrDtlInfo.item_nm', base.whs_id
            ,base.location, ptcl.unit_amt as 'rtnOdrDtlInfo.unit_amt', ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.rtn_odr_qty'
            ,ptcl.rtn_odr_amt as 'rtnOdrDtlInfo.rtn_odr_amt', date_format(base.col_req_dt,'%Y.%m.%d') as col_req_dt
            , base.rtn_whs_id, base.rtn_location, ptcl.rtn_req_unq_no as 'rtnOdrDtlInfo.rtn_req_unq_no' , ptcl.rtn_req_dtl_no as 'rtnOdrDtlInfo.rtn_req_dtl_no'
            , base.store_id , base.rtn_mgr_nm , base.rtn_mgr_tel_no, base.rtn_mgr_id
            , nvl(fn_get_telno((select mgr_tel_no from whs_mgnt_base_info where brand_id = base.brand_id and whs_id = base.rtn_whs_id)),'  ') as pick_tel_no
            , date_format(base.rtn_odr_reg_dt,'%Y%m%d') as rtn_odr_req_dt
            , base.spply_id, fn_spply_nm(base.spply_id) as spply_nm
            , ptcl.rtn_rsn_dvsn as  'rtnOdrDtlInfo.rtn_rsn_dvsn', ptcl.rtn_rsn as 'rtnOdrDtlInfo.rtn_rsn'
            , base.col_dvsn, ptcl.unit as 'rtnOdrDtlInfo.unit'
        from   rtn_odr_base_info base
        join rtn_odr_dtl_ptcl ptcl on base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no and ptcl.rodr_dtl_st_cd = 'ROD1'
        join item_base_info itm on ptcl.item_id = itm.item_id
        and    base.brand_id = #{brandId}
        and    concat(base.rtn_podr_unq_no,ptcl.rtn_podr_dtl_no) in
        <foreach collection="gridInsertData" item="item" separator="," open="(" close=")">
            concat(#{item.rtnPodrUnqNo},#{item.rtnPodrDtlNo})
        </foreach>
        order by  base.brand_id, base.whs_id, base.location, base.rtn_whs_id,base.rtn_location
    </select>

    <insert id="insertColOdrBase">
        insert into col_odr_base_info  /* ColOdr.insertColOdrBase*/
            (col_cmd_unq_no, col_reg_dt, brand_id, whs_id, location
            , store_id, item_qty, col_total_amt, col_req_dt, col_dvsn
            , col_cmd_st_cd, col_req_mgr_nm, col_req_mgr_id, col_dt, col_cnfm_nm
            , col_cnfm_id, col_mgr_nm, col_mgr_tel_no, col_mgr_emal, col_mgr_mbl_no
            , rtn_whs_id, rtn_location, spply_id, spply_nm, col_pick_unq_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
            (#{entity.colCmdUnqNo}, date_format(now(),'%Y%m%d'), #{entity.brandId}, #{entity.whsId}, #{entity.location}
            , #{entity.storeId}, #{entity.itemQty}, #{entity.colTotalAmt}, #{entity.colReqDt}, #{entity.colDvsn}
            , #{entity.colCmdStCd}, #{entity.colReqMgrNm}, #{entity.colReqMgrId}, #{entity.colDt}, #{entity.colCnfmNm}
            , #{entity.colCnfmId}, #{entity.colMgrNm}, #{entity.colMgrTelNo}, #{entity.colMgrEmal}, #{entity.colMgrMblNo}
            , #{entity.rtnWhsId}, #{entity.rtnLocation}, #{entity.spplyId}, #{entity.spplyNm}, #{entity.colPickUnqNo}
            , now(), #{userId}, now(), #{userId})

    </insert>

    <insert id="insertColOdrDtl">
        insert into col_odr_dtl_ptcl /* ColOdr.insertColOdrDtl*/
        (col_cmd_unq_no, col_cmd_dtl_no, item_id, item_nm, unit_amt
        , col_qty, col_amt, rtn_qty, rtn_amt, rtn_rsn_dvsn
        , rtn_rsn, rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_dtl_no, rtn_req_unq_no, col_cmd_dtl_st_cd
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
        (#{item.colCmdUnqNo}, #{item.colCmdDtlNo}, #{item.itemId}, #{item.itemNm}, #{item.unitAmt}
        , #{item.colQty}, #{item.colAmt}, #{item.rtnQty}, #{item.rtnAmt}, #{item.rtnRsnDvsn}
        , #{item.rtnRsn}, #{item.rtnPodrUnqNo}, #{item.rtnPodrDtlNo}, #{item.rtnReqDtlNo}, #{item.rtnReqUnqNo}, 'CDLD1'
        , now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <insert id="insertPickMgntBase">
        insert into pick_info_mgnt  /* ColOdr.insertPickMgntBase */
        (col_pick_unq_no, spply_id, spply_nm, store_id, col_reg_dt
        , col_tot_amt, pick_mgr_nm, pick_mgr_tel_no, pick_mgr_emal, pick_mgr_mbl_no
        , pndr_tel_no, pick_st_cd, rodr_reg_dt, col_req_dt, col_sch_dt
        , col_cmplt_dt, whs_id, location, whs_dvsn, brand_id, col_desc, col_dvsn
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        (#{entity.colPickUnqNo}, #{entity.spplyId}, #{entity.spplyNm}, #{entity.storeId}, date_format(now(),'%Y%m%d')
        , #{entity.colTotAmt}, #{entity.pickMgrNm}, #{entity.pickMgrTelNo}, #{entity.pickMgrEmal}, #{entity.pickMgrMblNo}
        , #{entity.pndrTelNo}, #{entity.pickStCd}, date_format(now(),'%Y%m%d'), #{entity.colReqDt}, #{entity.colSchDt}
        , #{entity.colCmpltDt}, #{entity.whsId}, #{entity.location}, #{entity.whsDvsn}, #{entity.brandId}, #{entity.colDesc}, #{entity.colDvsn}
        , now(), #{userId}, now(), #{userId})
    </insert>

    <insert id="insertPickDtlPtcl">
        insert into pick_dtl_ptcl  /* ColOdr.insertPickDtlPtcl */
        (col_pick_unq_no, col_pick_dtl_no, rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_unq_no
        , rtn_req_dtl_no, col_cmd_unq_no, col_cmd_dtl_no, pick_dtl_st_cd, item_id
        , item_nm, unit, unit_amt, price_unit, rodr_qty
        , col_req_qty, col_req_amt, delay_yn, delay_rsn
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
        (#{item.colPickUnqNo}, #{item.colPickDtlNo}, #{item.rtnPodrUnqNo}, #{item.rtnPodrDtlNo}, #{item.rtnReqUnqNo}
        , #{item.rtnReqDtlNo}, #{item.colCmdUnqNo}, #{item.colCmdDtlNo}, 'PICKD0', #{item.itemId}
        , #{item.itemNm}, #{item.unit}, #{item.unitAmt}, #{item.priceUnit}, #{item.rodrQty}
        , #{item.colReqQty}, #{item.colReqAmt}, #{item.delayYn}, #{item.delayRsn}
        , now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <select id="selectColGridList" resultType="com.oqpo.api.entity.inoutmng.ColOdrBaseEntity">
        select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.col_cmd_unq_no, ptcl.col_cmd_dtl_no as 'colDtlInfo.col_cmd_dtl_no'
            , ptcl.rtn_podr_unq_no as 'colDtlInfo.rtn_podr_unq_no', ptcl.rtn_podr_dtl_no as 'colDtlInfo.rtn_podr_dtl_no'
            , ptcl.col_cmd_dtl_st_cd as   'colDtlInfo.col_cmd_dtl_st_cd', fn_code_nm('COL_CMD_DTL_ST_CD', ptcl.col_cmd_dtl_st_cd) as 'colDtlInfo.col_cmd_dtl_st_nm'
            , ptcl.item_id as 'colDtlInfo.item_id' , ptcl.item_nm as  'colDtlInfo.item_nm', base.rtn_whs_id, base.rtn_location
            , concat(fn_whs_nm( base.brand_id, base.rtn_whs_id) , ' - ', fn_whs_location_nm(base.brand_id, base.rtn_whs_id,base.rtn_location) ) as rtn_whs_nm
            , base.col_dvsn, fn_code_nm('COL_DVSN',base.col_dvsn) as col_dvsn_nm
            , (case when base.whs_id is null or base.whs_id = '' then base.spply_id else base.whs_id end ) as whs_id
            , (case when base.whs_id is null or base.whs_id = '' then base.spply_nm else (concat(fn_whs_nm( base.brand_id, base.whs_id) , ' - ', fn_whs_location_nm(base.brand_id, base.whs_id,base.location) )) end ) as whs_nm
            , ptcl.rtn_qty as 'colDtlInfo.rtn_qty', ptcl.col_qty as 'colDtlInfo.col_qty', case when ptcl.col_cmd_dtl_st_cd = 'CDLD2' then ptcl.col_amt else 0 end as 'colDtlInfo.col_amt'
            , date_format(base.col_dt , '%Y.%m.%d') as col_dt , base.col_cnfm_nm, base.col_pick_unq_no
        from   col_odr_base_info base,
        col_odr_dtl_ptcl ptcl
        where 1=1
        and   base.col_cmd_unq_no = ptcl.col_cmd_unq_no
        and   base.brand_id = #{sBrandId}
        and   base.col_req_dt between #{fromDt} and #{toDt}
        <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="sRtnWhsId != null and sRtnWhsId != ''">
        and   base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sColCmdDtlStCd != null and sColCmdDtlStCd != ''">
        and   ptcl.col_cmd_dtl_st_cd = #{sColCmdDtlStCd}
        </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.col_cmd_unq_no asc , ptcl.col_cmd_dtl_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.col_cmd_unq_no desc , ptcl.col_cmd_dtl_no asc
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.col_cmd_unq_no desc , ptcl.col_cmd_dtl_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColGridCnt" resultType="int">
        select count(*) as cnt
        from   col_odr_base_info base,
        col_odr_dtl_ptcl ptcl
        where 1=1
        and   base.col_cmd_unq_no = ptcl.col_cmd_unq_no
        and   base.brand_id = #{sBrandId}
        and   base.col_req_dt between #{fromDt} and #{toDt}
        <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="sRtnWhsId != null and sRtnWhsId != ''">
            and   base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sColCmdDtlStCd != null and sColCmdDtlStCd != ''">
            and   ptcl.col_cmd_dtl_st_cd = #{sColCmdDtlStCd}
        </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="selectPickInfo" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select col_pick_unq_no, spply_id, spply_nm, store_id
                , date_format(col_reg_dt,'%Y.%m.%d') as col_reg_dt
                , col_tot_amt, pick_mgr_nm, fn_get_telno(pick_mgr_tel_no) as pick_mgr_tel_no
                , fn_get_telno(pndr_tel_no) as pndr_tel_no, pick_st_cd, fn_code_nm('PICK_ST_CD', pick_st_cd ) as pick_st_nm
                , date_format(rodr_reg_dt,'%Y.%m.%d') as rodr_reg_dt, date_format(col_req_dt,'%Y.%m.%d') as col_req_dt
                , date_format(col_sch_dt,'%Y.%m.%d') as col_sch_dt, date_format(col_cmplt_dt,'%Y.%m.%d') as col_cmplt_dt
                , 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
                , whs_dvsn, fn_code_nm('WHS_DVSN', whs_dvsn) as whs_dvsn_nm, brand_id, fn_brand_nm(brand_id) as brand_nm
                , col_desc, col_dvsn
        from pick_info_mgnt
        where col_pick_unq_no = #{colPickUnqNo}
        and   brand_id = #{brandId}
    </select>

    <select id="selectPickDtlPtcl" resultType="com.oqpo.api.entity.inoutmng.PickDtlPtclEntity">
        select ptcl.col_pick_unq_no, ptcl.col_pick_dtl_no, ptcl.rtn_podr_unq_no, ptcl.rtn_podr_dtl_no, ptcl.rtn_req_unq_no
            , ptcl.rtn_req_dtl_no, ptcl.col_cmd_unq_no, ptcl.col_cmd_dtl_no
            , ptcl.pick_dtl_st_cd, fn_code_nm('PICK_DTL_ST_CD',ptcl.pick_dtl_st_cd) as pick_dtl_st_nm, ptcl.item_id
            , ptcl.item_nm, ptcl.unit, ptcl.unit_amt, ptcl.price_unit, ptcl.rodr_qty
            , ptcl.col_req_qty, ptcl.col_req_amt, ptcl.delay_yn, ptcl.delay_rsn
            , rptcl.rtn_rsn_dvsn, fn_code_nm('RTN_RSN_DVSN', rptcl.rtn_rsn_dvsn ) as rtn_rsn_dvsn_nm
            , rptcl.rtn_rsn
        from pick_info_mgnt base, pick_dtl_ptcl ptcl
            , rtn_odr_dtl_ptcl rptcl
        where base.col_pick_unq_no = ptcl.col_pick_unq_no
        and   ptcl.rtn_podr_unq_no = rptcl.rtn_podr_unq_no
        and   ptcl.rtn_podr_dtl_no = rptcl.rtn_podr_dtl_no
        and   base.brand_id = #{brandId}
        and   base.col_pick_unq_no = #{colPickUnqNo}
    </select>

    <select id="selectColSpplyTargetRtnGridList" 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, ptcl.rtn_podr_dtl_no as 'rtnOdrDtlInfo.rtn_podr_dtl_no'
                , ptcl.rtn_req_unq_no as 'rtnOdrDtlInfo.rtn_req_unq_no', ptcl.rtn_req_dtl_no as 'rtnOdrDtlInfo.rtn_req_dtl_no'
                , ptcl.item_id as 'rtnOdrDtlInfo.item_id', ptcl.item_nm as 'rtnOdrDtlInfo.item_nm'
                , base.rtn_whs_id, fn_whs_nm(base.brand_id, base.rtn_whs_id) as rtn_whs_nm
                 , base.rtn_location, fn_whs_location_nm(base.brand_id, base.rtn_whs_id, base.rtn_location) as rtn_location_nm
                , ptcl.unit_amt as 'rtnOdrDtlInfo.unit_amt'
                , ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.rtn_odr_qty'
                , date_format(base.col_req_dt,'%Y.%m.%d') as  col_req_dt, ptcl.rtn_amt as 'rtnOdrDtlInfo.rtn_amt'
                , base.whs_id,  fn_whs_nm(base.brand_id, base.whs_id) as whs_nm
                , base.location, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
                , base.store_id, ptcl.rtn_odr_amt  as 'rtnOdrDtlInfo.rtn_odr_amt'
        from rtn_odr_base_info base
        , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.spply_id = #{sSpplyId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C002'
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </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.rtn_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.rtn_podr_unq_no desc , ptcl.rtn_podr_dtl_no asc
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.rtn_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColSpplyTargetRtnGridCnt" resultType="int">
        select count(*) as cnt
        from rtn_odr_base_info base
            , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.spply_id = #{sSpplyId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C002'
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </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="selectSpplyColOdrInfo" resultType="com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity">
        select  base.rtn_podr_unq_no, base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.rtn_podr_unq_no, date_format(col_req_dt, '%Y.%m.%d') as col_req_dt
               , (select sum(rtn_amt) from  rtn_odr_dtl_ptcl where rtn_podr_unq_no = #{rtnPodrUnqNo} and rodr_dtl_st_cd = 'ROD1' ) as rtn_dodr_total_amt
               , base.rtn_whs_id, fn_whs_nm(base.brand_id, base.rtn_whs_id) as rtn_whs_nm
               , base.rtn_location, fn_whs_location_nm(brand_id, base.rtn_whs_id, base.rtn_location) as rtn_location_nm
               , fn_get_telno((select tel_no from store_base_info where store_id = base.store_id )) as pick_tel_no
        from    rtn_odr_base_info base
        where  1=1
        and    base.brand_id = #{brandId}
        and    base.spply_id = #{spplyId}
        and    base.col_dvsn = 'C002'
        and    base.rtn_podr_unq_no = #{rtnPodrUnqNo}
    </select>

    <select id="selectSpplyColRtnList" resultType="com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity">
         select ptcl.rtn_podr_unq_no, ptcl.rtn_podr_dtl_no, ptcl.item_id, ptcl.item_nm, ptcl.unit, ptcl.rtn_odr_qty, ptcl.rtn_odr_qty as col_qty
                 , ptcl.unit_amt
                 , ptcl.rtn_rsn_dvsn, fn_code_nm('RTN_RSN_DVSN',ptcl.rtn_rsn_dvsn ) as rtn_rsn_dvsn_nm,  ptcl.rtn_rsn
         from   rtn_odr_dtl_ptcl ptcl
         where  ptcl.rtn_podr_unq_no = #{rtnPodrUnqNo}
         and    ptcl.rodr_dtl_st_cd = 'ROD1'
    </select>

    <!--  어디에 쓰는 쿼리인지 찿을 필요 있음
    <select id="selectColSpplyTargetRtnGridList" 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, ptcl.rtn_podr_dtl_no as 'rtnOdrDtlInfo.rtn_podr_dtl_no'
            , ptcl.rtn_req_unq_no as 'rtnOdrDtlInfo.rtn_req_unq_no', ptcl.rtn_req_dtl_no as 'rtnOdrDtlInfo.rtn_req_dtl_no'
            , ptcl.item_id as 'rtnOdrDtlInfo.item_id', ptcl.item_nm as 'rtnOdrDtlInfo.item_nm'
            , base.rtn_whs_id, fn_whs_nm(base.brand_id, base.rtn_whs_id) as rtn_whs_nm, ptcl.unit_amt as 'rtnOdrDtlInfo.unit_amt'
            , ptcl.rtn_qty as 'rtnOdrDtlInfo.rtn_qty',  ptcl.rtn_odr_qty as 'rtnOdrDtlInfo.rtn_odr_qty'
            , date_format(base.col_req_dt,'%Y.%m.%d') as  col_req_dt, ptcl.rtn_amt as 'rtnOdrDtlInfo.rtn_amt'
            , base.whs_id,  fn_whs_nm(base.brand_id, base.whs_id) as whs_nm
            , base.location, fn_whs_location_nm(base.brand_id, base.whs_id, base.location) as location_nm
            , base.store_id, ptcl.rtn_odr_amt  as 'rtnOdrDtlInfo.rtn_odr_amt'
        from rtn_odr_base_info base
            , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.spply_id = #{sSpplyId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C002'
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and   (ptcl.item_id like concat('%', '', '%') or ptcl.item_nm like concat('%', '', '%'))
        </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_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by base.rtn_podr_unq_no desc , ptcl.rtn_podr_dtl_no asc
                    </if>
                </if>
            </when>
            <otherwise>
                order by base.rtn_podr_unq_no asc , ptcl.rtn_podr_dtl_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColTargetRtnGridCnt" resultType="int">
        select count(*) as cnt
        from rtn_odr_base_info base
        , rtn_odr_dtl_ptcl ptcl
        where 1=1
        and    base.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and    base.brand_id = #{sBrandId}
        and    base.spply_id = #{sSpplyId}
        and    base.col_req_dt between #{fromDt} and #{toDt}
        and    base.rtn_odr_st_cd = 'RO20'
        and    ptcl.rodr_dtl_st_cd = 'ROD1'
        and    base.col_dvsn = 'C001'
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and    base.rtn_whs_id = #{sRtnWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and   (ptcl.item_id like concat('%', '', '%') or ptcl.item_nm like concat('%', '', '%'))
        </if>

    </select>

    -->


    <select id="selectColSpplyPickGridList" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select pmgnt.brand_id, fn_brand_nm(pmgnt.brand_id) as brand_nm
            , pmgnt.col_pick_unq_no , pmgnt.pick_st_cd, fn_code_nm('PICK_ST_CD',pmgnt.pick_st_cd) as pick_st_nm
            , date_format(pmgnt.col_reg_dt, '%Y.%m.%d') as col_reg_dt , date_format(pmgnt.col_req_dt, '%Y.%m.%d') as col_req_dt
            , date_format(pmgnt.col_sch_dt, '%Y.%m.%d') as col_sch_dt , date_format(pmgnt.col_cmplt_dt, '%Y.%m.%d') as col_cmplt_dt
            , pptcl.item_id as 'pickDtlInfo.item_id' , pptcl.item_nm as 'pickDtlInfo.item_nm', pptcl.col_req_qty as 'pickDtlInfo.col_req_qty'
            , pptcl.col_req_amt as 'pickDtlInfo.col_req_amt' , pptcl.unit_amt as 'pickDtlInfo.unit_amt', ptcl.rtn_rsn_dvsn as 'pickDtlInfo.rtn_rsn_dvsn'
            , fn_code_nm('RTN_RSN_DVSN', ptcl.rtn_rsn_dvsn) as 'pickDtlInfo.rtn_rsn_dvsn_nm', pmgnt.whs_id, pmgnt.location
            , fn_whs_nm(pmgnt.brand_id, pmgnt.whs_id) as whs_nm, fn_whs_location_nm(pmgnt.brand_id, pmgnt.whs_id, pmgnt.location) as location_nm
            , pmgnt.pick_mgr_nm, fn_get_telno(pmgnt.pick_mgr_tel_no) as pick_mgr_tel_no
        from rtn_odr_dtl_ptcl ptcl
            , pick_info_mgnt pmgnt
            , pick_dtl_ptcl  pptcl
        where 1 = 1
        and ptcl.rtn_podr_unq_no = pptcl.rtn_podr_unq_no
        and ptcl.rtn_podr_dtl_no = pptcl.rtn_podr_dtl_no
        and pptcl.col_pick_unq_no = pmgnt.col_pick_unq_no
        and pmgnt.brand_id = #{sBrandId}  -- 반품주체 공급사
        and pmgnt.spply_id = #{sSpplyId}  -- 반품주체 공급사
        and pmgnt.col_dvsn = 'C002'  -- c001 : 본사 수거, c002 : 공급사수거
        and pmgnt.col_req_dt between #{fromDt} and #{toDt}
        <if test="sWhsId != null and sWhsId != ''">
        and pmgnt.whs_id = #{sWhsId}  -- 반품창고
        </if>
        <if test="sItemNm != null and sItemNm != ''">
        and (pmgnt.col_pick_unq_no like concat('%', #{sItemNm}, '%') or pptcl.item_id like concat('%', #{sItemNm}, '%') or pptcl.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 pmgnt.col_pick_unq_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by pmgnt.col_pick_unq_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by pmgnt.col_pick_unq_no desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColSpplyPickGridCnt" resultType="int">
        select count(*)
        from rtn_odr_dtl_ptcl ptcl
        , pick_info_mgnt pmgnt
        , pick_dtl_ptcl  pptcl
        where 1 = 1
        and ptcl.rtn_podr_unq_no = pptcl.rtn_podr_unq_no
        and ptcl.rtn_podr_dtl_no = pptcl.rtn_podr_dtl_no
        and pptcl.col_pick_unq_no = pmgnt.col_pick_unq_no
        and pmgnt.brand_id = #{sBrandId}  -- 반품주체 공급사
        and pmgnt.spply_id = #{sSpplyId}  -- 반품주체 공급사
        and pmgnt.col_dvsn = 'C002'  -- c001 : 본사 수거, c002 : 공급사수거
        and pmgnt.col_req_dt between #{fromDt} and #{toDt}
        <if test="sWhsId != null and sWhsId != ''">
            and pmgnt.whs_id = #{sWhsId}  -- 반품창고
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and (pmgnt.col_pick_unq_no like concat('%', #{sItemNm}, '%') or pptcl.item_id like concat('%', #{sItemNm}, '%') or pptcl.item_nm like concat('%', #{sItemNm}, '%'))
        </if>

    </select>

    <select id="selectColRtnInstGridList" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select mgnt.brand_id, fn_brand_nm(mgnt.brand_id) as brand_nm
               ,mgnt.store_id
               ,mgnt.col_pick_unq_no, dtl.col_pick_dtl_no as 'pickDtlInfo.col_pick_dtl_no'
               , dtl.rtn_podr_unq_no as 'pickDtlInfo.rtn_podr_unq_no' , dtl.rtn_podr_dtl_no as 'pickDtlInfo.rtn_podr_dtl_no'
               ,dtl.item_id as 'pickDtlInfo.item_id', dtl.item_nm as 'pickDtlInfo.item_nm'
               , dtl.rodr_qty as 'pickDtlInfo.rodr_qty' , dtl.col_req_qty as 'pickDtlInfo.col_req_qty'
               , dtl.col_req_amt as 'pickDtlInfo.col_req_amt'
               /* , null as 'pickDtlInfo.col_qty', null as 'pickDtlInfo.col_amt' */
               , date_format(mgnt.col_sch_dt,'%Y.%m.%d') as col_sch_dt, mgnt.pick_mgr_nm, fn_get_telno(mgnt.pick_mgr_tel_no) as pick_mgr_tel_no
               , mgnt.whs_id as rtn_whs_id , mgnt.location as rtn_location
               , fn_whs_nm(mgnt.brand_id, mgnt.whs_id) as rtn_whs_nm, fn_whs_location_nm(mgnt.brand_id, mgnt.whs_id, mgnt.location) as rtn_location_nm
               , base.whs_id, base.location
               , 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
               , mgnt.spply_id, fn_spply_nm(mgnt.spply_id) as spply_nm
                , dtl.unit_amt as 'pickDtlInfo.unit_amt', date_format(mgnt.col_req_dt,'%Y.%m.%d') as col_req_dt
        from   pick_info_mgnt mgnt
              ,pick_dtl_ptcl dtl
              ,rtn_odr_dtl_ptcl ptcl
              ,rtn_odr_base_info base
        where mgnt.col_pick_unq_no = dtl.col_pick_unq_no
        and   dtl.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and   dtl.rtn_podr_dtl_no = ptcl.rtn_podr_dtl_no
        and   ptcl.rtn_podr_unq_no = base.rtn_podr_unq_no
        and   mgnt.brand_id = #{sBrandId}
        and   mgnt.col_sch_dt between #{fromDt} and #{toDt}
        and   mgnt.pick_st_cd = 'PICK0'
        and   dtl.pick_dtl_st_cd = 'PICKD0'
        <if test="sStoreId != null and sStoreId != ''">
            and   mgnt.store_id = #{sStoreId}
            and   mgnt.whs_dvsn = 'W02'
        </if>
        <if test="sStoreId == null or sStoreId == ''">
            and   mgnt.whs_dvsn = 'W01'
        </if>
        <if test="sColPickUnqNo != null and sColPickUnqNo != ''">
            and   mgnt.col_pick_unq_no = #{sColPickUnqNo}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
        and   mgnt.whs_id = #{sRtnWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
        and   mgnt.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
        and   base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
        and   (mgnt.col_pick_unq_no like concat('%',#{sItemNm},'%') or dtl.item_id like concat('%',#{sItemNm},'%') or dtl.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 mgnt.col_pick_unq_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by mgnt.col_pick_unq_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by mgnt.col_pick_unq_no desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColRtnInstGridCnt" resultType="int">
        select count(*) as cnt
        from   pick_info_mgnt mgnt
        ,pick_dtl_ptcl dtl
        ,rtn_odr_dtl_ptcl ptcl
        ,rtn_odr_base_info base
        where mgnt.col_pick_unq_no = dtl.col_pick_unq_no
        and   dtl.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and   dtl.rtn_podr_dtl_no = ptcl.rtn_podr_dtl_no
        and   ptcl.rtn_podr_unq_no = base.rtn_podr_unq_no
        and   mgnt.brand_id = #{sBrandId}
        and   mgnt.col_sch_dt between #{fromDt} and #{toDt}
        and   mgnt.pick_st_cd = 'PICK0'
        and   dtl.pick_dtl_st_cd = 'PICKD0'
        <if test="sStoreId != null and sStoreId != ''">
            and   mgnt.store_id = #{sStoreId}
            and   mgnt.whs_dvsn = 'W02'
        </if>
        <if test="sStoreId == null or sStoreId == ''">
            and   mgnt.whs_dvsn = 'W01'
        </if>
        <if test="sColPickUnqNo != null and sColPickUnqNo != ''">
            and   mgnt.col_pick_unq_no = #{sColPickUnqNo}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and   mgnt.whs_id = #{sRtnWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
            and   mgnt.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and   base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and   (mgnt.col_pick_unq_no like concat('%',#{sItemNm},'%') or dtl.item_id like concat('%',#{sItemNm},'%') or dtl.item_nm  like concat('%',#{sItemNm},'%') )
        </if>

    </select>

    <insert id="insertRtnProcInfoPtcl">
        insert into rtn_proc_info_ptcl
            (rtn_mgnt_unq_no, brand_id, store_id, item_id, item_nm
            , whs_id, location, unit, unit_amt, rtn_req_qty
            , rtn_odr_qty, col_qty, col_req_qty, col_req_dt, col_sch_dt, col_dt
            , col_cnfm_nm, col_mgr_nm, col_mgr_tel_no, col_mgr_emal, col_mgr_mbl_no
            , col_amt, col_agr_yn, col_cnfm_doc_file_no, sttl_yn, spply_id
            , spply_nm, col_pick_unq_no, col_pick_dtl_no, col_cmd_unq_no, col_cmd_dtl_no
            , rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_unq_no, rtn_req_dtl_no, sttl_mgnt_unq_no, sttl_mgnt_dtl_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.rtnMgntUnqNo}, #{item.brandId}, #{item.storeId}, #{item.itemId}, #{item.itemNm}
            , #{item.whsId}, #{item.location}, #{item.unit}, #{item.unitAmt}, #{item.rtnReqQty}
            , #{item.rtnOdrQty}, #{item.colQty}, #{item.colReqQty}, #{item.colReqDt}, #{item.colSchDt}, date_format(now(),'%Y%m%d')
            , #{item.colCnfmNm}, #{item.colMgrNm}, #{item.colMgrTelNo}, #{item.colMgrEmal}, #{item.colMgrMblNo}
            , #{item.colAmt}, #{item.colAgrYn}, #{item.colCnfmDocFileNo}, #{item.sttlYn}, #{item.spplyId}
            , #{item.spplyNm}, #{item.colPickUnqNo}, #{item.colPickDtlNo}, #{item.colCmdUnqNo}, #{item.colCmdDtlNo}
            , #{item.rtnPodrUnqNo}, #{item.rtnPodrDtlNo}, #{item.rtnReqUnqNo}, #{item.rtnReqDtlNo}, #{item.sttlMgntUnqNo}, #{item.sttlMgntDtlNo}
            , now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>


    <select id="selectColRtnInstResultGridList" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select mgnt.brand_id, fn_brand_nm(mgnt.brand_id) as brand_nm
            ,mgnt.col_pick_unq_no, dtl.col_pick_dtl_no as 'pickDtlInfo.col_pick_dtl_no'
            , dtl.rtn_podr_unq_no as 'pickDtlInfo.rtn_podr_unq_no' , dtl.rtn_podr_dtl_no as 'pickDtlInfo.rtn_podr_dtl_no'
            ,dtl.item_id as 'pickDtlInfo.item_id', dtl.item_nm as 'pickDtlInfo.item_nm'
            , dtl.rodr_qty as 'pickDtlInfo.rodr_qty' , dtl.col_req_qty as 'pickDtlInfo.col_req_qty'
            , dtl.col_qty as 'pickDtlInfo.col_qty', dtl.col_amt as 'pickDtlInfo.col_amt'
            , date_format(mgnt.col_sch_dt,'%Y.%m.%d') as col_sch_dt, date_format(mgnt.col_cmplt_dt,'%Y.%m.%d') as col_cmplt_dt
            , mgnt.pick_mgr_nm, fn_get_telno(mgnt.pick_mgr_tel_no) as pick_mgr_tel_no
            , mgnt.whs_id as rtn_whs_id , mgnt.location as rtn_location
            , fn_whs_nm(mgnt.brand_id, mgnt.whs_id) as rtn_whs_nm, fn_whs_location_nm(mgnt.brand_id, mgnt.whs_id, mgnt.location) as rtn_location_nm
            , base.whs_id, base.location
            , 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
            , mgnt.spply_id, fn_spply_nm(mgnt.spply_id) as spply_nm
            , dtl.pick_dtl_st_cd as 'pickDtlInfo.pick_dtl_st_cd'
            , fn_code_nm('PICK_DTL_ST_CD', dtl.pick_dtl_st_cd) as  'pickDtlInfo.pick_dtl_st_nm'
        from   pick_info_mgnt mgnt
            ,pick_dtl_ptcl dtl
            ,rtn_odr_dtl_ptcl ptcl
            ,rtn_odr_base_info base
        where mgnt.col_pick_unq_no = dtl.col_pick_unq_no
        and   dtl.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and   dtl.rtn_podr_dtl_no = ptcl.rtn_podr_dtl_no
        and   ptcl.rtn_podr_unq_no = base.rtn_podr_unq_no
        and   mgnt.brand_id = #{sBrandId}
        and   mgnt.col_cmplt_dt between #{fromDt} and #{toDt}
        <if test="sStoreId != null and sStoreId != ''">
            and   mgnt.store_id = #{sStoreId}
        </if>
        <if test="sColPickUnqNo != null and sColPickUnqNo != ''">
            and   mgnt.col_pick_unq_no = #{sColPickUnqNo}
        </if>
        <if test="sRtnPodrUnqNo != null and sRtnPodrUnqNo != ''">
            and   base.rtn_podr_unq_no = #{sRtnPodrUnqNo}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and   mgnt.whs_id = #{sRtnWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
            and   mgnt.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and   base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and   (mgnt.col_pick_unq_no like concat('%',#{sItemNm},'%') or dtl.item_id like concat('%',#{sItemNm},'%') or dtl.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 mgnt.col_pick_unq_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by mgnt.col_pick_unq_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by mgnt.col_pick_unq_no desc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectColRtnInstResultGridCnt" resultType="int">
        select count(*) as cnt
        from   pick_info_mgnt mgnt
        ,pick_dtl_ptcl dtl
        ,rtn_odr_dtl_ptcl ptcl
        ,rtn_odr_base_info base
        where mgnt.col_pick_unq_no = dtl.col_pick_unq_no
        and   dtl.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and   dtl.rtn_podr_dtl_no = ptcl.rtn_podr_dtl_no
        and   ptcl.rtn_podr_unq_no = base.rtn_podr_unq_no
        and   mgnt.brand_id = #{sBrandId}
        and   mgnt.col_cmplt_dt between #{fromDt} and #{toDt}
        <if test="sStoreId != null and sStoreId != ''">
            and   mgnt.store_id = #{sStoreId}
        </if>
        <if test="sColPickUnqNo != null and sColPickUnqNo != ''">
            and   mgnt.col_pick_unq_no = #{sColPickUnqNo}
        </if>
        <if test="sRtnPodrUnqNo != null and sRtnPodrUnqNo != ''">
            and   base.rtn_podr_unq_no = #{sRtnPodrUnqNo}
        </if>
        <if test="sRtnWhsId != null and sRtnWhsId != ''">
            and   mgnt.whs_id = #{sRtnWhsId}
        </if>
        <if test="sSpplyId != null and sSpplyId != ''">
            and   mgnt.spply_id = #{sSpplyId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and   base.whs_id = #{sWhsId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            and   (mgnt.col_pick_unq_no like concat('%',#{sItemNm},'%') or dtl.item_id like concat('%',#{sItemNm},'%') or dtl.item_nm  like concat('%',#{sItemNm},'%') )
        </if>

    </select>

    <select id="selectColRtnInstProcList" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select mgnt.brand_id, fn_brand_nm(mgnt.brand_id) as brand_nm
            , mgnt.store_id,dtl.item_id as 'pickDtlInfo.item_id', dtl.item_nm as 'pickDtlInfo.item_nm'
            , base.whs_id, base.location
            , 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
            , mgnt.whs_id as rtn_whs_id, mgnt.location as rtn_location
            , fn_whs_nm(base.brand_id, mgnt.whs_id) as rtn_whs_nm, fn_whs_location_nm(base.brand_id, mgnt.whs_id, mgnt.location) as rtn_location_nm
            , dtl.unit as 'pickDtlInfo.unit' , dtl.unit_amt as 'pickDtlInfo.unit_amt'
            , nvl(ptcl.rtn_req_qty,0) as  'pickDtlInfo.rtn_req_qty'   , nvl(dtl.rodr_qty,0) as 'pickDtlInfo.rodr_qty'
            , nvl(dtl.col_req_qty,0) as 'pickDtlInfo.col_req_qty'
            , date_format(mgnt.col_req_dt,'%Y%m%d') as col_req_dt
            , date_format(mgnt.col_sch_dt,'%Y%m%d') as col_sch_dt
            , date_format(mgnt.col_cmplt_dt,'%Y%m%d') as col_cmplt_dt
            , mgnt.pick_mgr_nm, fn_get_telno(mgnt.pick_mgr_tel_no) as pick_mgr_tel_no
            , dtl.col_req_amt as 'pickDtlInfo.col_req_amt'
            , mgnt.spply_id, fn_spply_nm(mgnt.spply_id) as spply_nm
            , mgnt.col_pick_unq_no, dtl.col_pick_dtl_no as 'pickDtlInfo.col_pick_dtl_no'
            , dtl.col_cmd_unq_no as 'pickDtlInfo.col_cmd_unq_no', dtl.col_cmd_dtl_no as 'pickDtlInfo.col_cmd_dtl_no'
            , dtl.rtn_podr_unq_no as 'pickDtlInfo.rtn_podr_unq_no', dtl.rtn_podr_dtl_no as 'pickDtlInfo.rtn_podr_dtl_no'
            , ptcl.rtn_req_unq_no as  'pickDtlInfo.rtn_req_unq_no', ptcl.rtn_req_dtl_no as  'pickDtlInfo.rtn_req_dtl_no'
            , mgnt.whs_dvsn, mgnt.col_dvsn
        from   pick_info_mgnt mgnt
                ,pick_dtl_ptcl dtl
                ,rtn_odr_dtl_ptcl ptcl
                ,rtn_odr_base_info base
        where mgnt.col_pick_unq_no = dtl.col_pick_unq_no
        and   dtl.rtn_podr_unq_no = ptcl.rtn_podr_unq_no
        and   dtl.rtn_podr_dtl_no = ptcl.rtn_podr_dtl_no
        and   ptcl.rtn_podr_unq_no = base.rtn_podr_unq_no
        and   mgnt.brand_id = #{brandId}
        and   mgnt.whs_id = #{whsId}
        and    concat(mgnt.col_pick_unq_no, dtl.col_pick_dtl_no) in
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
            #{item.colPickUnqNo}
        </foreach>
    </select>


    <update id="updateChgStckQtyStckMgnt">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update stck_mgnt_base_info
            set    sys_chg_id = #{userId}
                 , sys_chg_dttm = now()
                 , stck_qty = stck_qty + #{item.stckQty}
            where brand_id = #{item.brandId}
            and   whs_id = #{item.whsId}
            and   location = #{item.location}
            and   item_id = #{item.itemId}
        </foreach>
    </update>

    <insert id="insertChgStckStckMgntDtlHist" >
        /* StockMngMapper.insertStckMgntDtlHist */
        INSERT INTO stck_mgnt_dtl_hist
            (stck_mgnt_dtl_no, brand_id, whs_id, location, item_id, item_nm, inout_cd, inout_qty, whs_mgnt_unq_no,
             rtn_mgnt_unq_no, chg_dvsn, relation_no, sys_reg_dttm,
             sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.stckMgntDtlNo}, #{item.brandId}, #{item.whsId}, #{item.location}, #{item.itemId},
             #{item.itemNm}, #{item.inoutCd}, #{item.inoutQty}, #{item.whsMgntUnqNo}, #{item.rtnMgntUnqNo}, #{item.chgDvsn}, #{item.relationNo},
             NOW(), #{userId}, NOW(), #{userId})
        </foreach>
    </insert>
    <update id="updateChgStckCloDtlStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update col_odr_dtl_ptcl
            set    sys_chg_id = #{userId}
                , sys_chg_dttm = now()
                , col_cmd_dtl_st_cd = #{item.colCmdDtlStCd}
            where col_cmd_unq_no = #{item.colCmdUnqNo}
            and   col_cmd_dtl_no = #{item.colCmdDtlNo}
        </foreach>
    </update>

    <update id="updateChgStckPickDtlStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update pick_dtl_ptcl
            set    sys_chg_id = #{userId}
                , sys_chg_dttm = now()
                , pick_dtl_st_cd = #{item.pickDtlStCd}
            where col_pick_unq_no = #{item.colPickUnqNo}
            and   col_pick_dtl_no = #{item.colPickDtlNo}
        </foreach>
    </update>

    <select id="selectColCmdStDtlList" resultType="com.oqpo.api.entity.inoutmng.ColOdrBaseEntity">
        select stData.col_cmd_unq_no , case when stData.cdld1 <![CDATA[>]]> 0 then 'COL1' when stData.cdld1 = 0 and stData.cdld2 <![CDATA[>]]> 0 then 'COL2' else 'COL3' end as col_cmd_st_cd
        from  (
                select col_cmd_unq_no
                    , sum(case when col_cmd_dtl_st_cd = 'CDLD1' then 1 else 0 end)  as cdld1
                    , sum(case when col_cmd_dtl_st_cd = 'CDLD2' then 1 else 0 end)  as cdld2
                    , sum(case when col_cmd_dtl_st_cd = 'CDLD3' then 1 else 0 end)  as cdld3
                from  col_odr_dtl_ptcl
                where col_cmd_unq_no in
                <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
                    #{item.colCmdUnqNo}
                </foreach>
                group by col_cmd_unq_no
        ) stData
    </select>

    <update id="updateChgStckColOdrStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update col_odr_base_info
            set    sys_chg_id = #{userId}
                , sys_chg_dttm = now()
                , col_cmd_st_cd = #{item.colCmdStCd}
            where col_cmd_unq_no = #{item.colCmdUnqNo}
        </foreach>
    </update>

    <select id="selectColPickStList" resultType="com.oqpo.api.entity.inoutmng.PickInfoMgntEntity">
        select stData.col_pick_unq_no , case when stData.pickd0 <![CDATA[>]]> 0 then 'PICK0' when stData.pickd0 = 0 and stData.pickd1 <![CDATA[>]]> 0 then 'PICK1' else 'PICK3' end as pick_st_cd
        from  (
            select col_pick_unq_no
                , sum(case when pick_dtl_st_cd = 'PICKD0' then 1 else 0 end)  as pickd0
                , sum(case when pick_dtl_st_cd = 'PICKD1' then 1 else 0 end)  as pickd1
                , sum(case when pick_dtl_st_cd = 'PICKD3' then 1 else 0 end)  as pickd3
            from  pick_dtl_ptcl
            where col_pick_unq_no in
                <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
                    #{item.colPickUnqNo}
                </foreach>
            group by col_pick_unq_no
        ) stData
    </select>

    <update id="updateChgStckPickStCd">
        <foreach collection="gridUpdateData" item="item" separator=";">
            update pick_info_mgnt
            set    sys_chg_id = #{userId}
                , sys_chg_dttm = now()
                , pick_st_cd = #{item.pickStCd}
                , col_cmplt_dt = case when #{item.pickStCd} in ('PICK1','PICK3') then date_format(now(), '%Y%m%d') else col_cmplt_dt end
            where col_pick_unq_no = #{item.colPickUnqNo}
        </foreach>
    </update>


    <insert id="insertStreamlineColOdrBase">
        insert into col_odr_base_info  /* ColOdr.insertColOdrBase*/
            (col_cmd_unq_no, col_reg_dt, brand_id, whs_id, location
            , store_id, item_qty, col_total_amt, col_req_dt, col_dvsn
            , col_cmd_st_cd, col_req_mgr_nm, col_req_mgr_id, col_dt, col_cnfm_nm
            , col_cnfm_id, col_mgr_nm, col_mgr_tel_no, col_mgr_emal, col_mgr_mbl_no
            , rtn_whs_id, rtn_location, spply_id, spply_nm, col_pick_unq_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.colCmdUnqNo}, date_format(now(),'%Y%m%d'), #{item.brandId}, #{item.whsId}, #{item.location}
            , #{item.storeId}, #{item.itemQty}, #{item.colTotalAmt}, #{item.colReqDt}, #{item.colDvsn}
            , #{item.colCmdStCd}, #{item.colReqMgrNm}, #{item.colReqMgrId}, #{item.colDt}, #{item.colCnfmNm}
            , #{item.colCnfmId}, #{item.colMgrNm}, #{item.colMgrTelNo}, #{item.colMgrEmal}, #{item.colMgrMblNo}
            , #{item.rtnWhsId}, #{item.rtnLocation}, #{item.spplyId}, #{item.spplyNm}, #{item.colPickUnqNo}
            , now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>

    <insert id="insertStreamlinePickMgntBase">
        insert into pick_info_mgnt  /* ColOdr.insertPickMgntBase */
        (col_pick_unq_no, spply_id, spply_nm, store_id, col_reg_dt
        , col_tot_amt, pick_mgr_nm, pick_mgr_tel_no, pick_mgr_emal, pick_mgr_mbl_no
        , pndr_tel_no, pick_st_cd, rodr_reg_dt, col_req_dt, col_sch_dt
        , col_cmplt_dt, whs_id, location, whs_dvsn, brand_id, col_desc, col_dvsn
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
        (#{item.colPickUnqNo}, #{item.spplyId}, #{item.spplyNm}, #{item.storeId}, date_format(now(),'%Y%m%d')
        , #{item.colTotAmt}, #{item.pickMgrNm}, #{item.pickMgrTelNo}, #{item.pickMgrEmal}, #{item.pickMgrMblNo}
        , #{item.pndrTelNo}, #{item.pickStCd}, date_format(now(),'%Y%m%d'), #{item.colReqDt}, #{item.colSchDt}
        , #{item.colCmpltDt}, #{item.whsId}, #{item.location}, #{item.whsDvsn}, #{item.brandId}, #{item.colDesc}, #{item.colDvsn}
        , now(), #{userId}, now(), #{userId})
        </foreach>
    </insert>
    <!--


    in
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
            concat(#{item.rtnPodrUnqNo},#{item.rtnPodrDtlNo})
        </foreach>
        <insert id="insertRtnProcInfoPtcl">
            insert into rtn_proc_info_ptcl
            (rtn_mgnt_unq_no, brand_id, store_id, item_id, item_nm
            , whs_id, location, unit, unit_amt, rtn_req_qty
            , rtn_odr_qty, col_req_qty, col_qty, col_req_dt, col_sch_dt
            , col_dt, col_cnfm_nm, col_mgr_nm, col_mgr_tel_no, col_mgr_emal
            , col_mgr_mbl_no, col_amt, col_agr_yn, col_cnfm_doc_file_no, sttl_yn
            , spply_id, spply_nm, col_pick_unq_no, col_pick_dtl_no, col_cmd_unq_no
            , col_cmd_dtl_no, rtn_podr_unq_no, rtn_podr_dtl_no, rtn_req_unq_no, rtn_req_dtl_no
            , sttl_mgnt_unq_no, sttl_mgnt_dtl_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
            values
            (#{entity.rtnMgntUnqNo}, #{entity.brandId}, #{entity.storeId}, #{entity.itemId}, #{entity.itemNm}
            , #{entity.whsId}, #{entity.location}, #{entity.unit}, #{entity.unitAmt}, #{entity.rtnReqQty}
            , #{entity.rtnOdrQty}, #{entity.colReqQty}, #{entity.colQty}, #{entity.colReqDt}, #{entity.colSchDt}
            , date_fromat(now(),'%Y%m%d'), #{entity.colCnfmNm}, #{entity.colMgrNm}, #{entity.colMgrTelNo}, #{entity.colMgrEmal}
            , #{entity.colMgrMblNo}, #{entity.colAmt}, #{entity.colAgrYn}, #{entity.colCnfmDocFileNo}, #{entity.sttlYn}
            , #{entity.spplyId}, #{entity.spplyNm}, #{entity.colPickUnqNo}, #{entity.colPickDtlNo}, #{entity.colCmdUnqNo}
            , #{entity.colCmdDtlNo}, #{entity.rtnPodrUnqNo}, #{entity.rtnPodrDtlNo}, #{entity.rtnReqUnqNo}, #{entity.rtnReqDtlNo}
            , #{entity.sttlMgntUnqNo}, #{entity.sttlMgntDtlNo}
            , now(), #{userId}, now(), #{userId})
        </insert>
    -->
    </mapper>
