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

    <select id="selectShmtTargetOdrGridList" 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
               , ptcl.pch_podr_dtl_no as 'pchOdrDtlInfo.pch_podr_dtl_no'
                ,ptcl.item_id as 'pchOdrDtlInfo.item_id' , ptcl.item_nm as 'pchOdrDtlInfo.item_nm', 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
                ,ptcl.unit_amt as 'pchOdrDtlInfo.unit_amt', ptcl.podr_qty as 'pchOdrDtlInfo.podr_qty'
                , ptcl.podr_qty as 'pchOdrDtlInfo.shmt_qty', nvl(stck.stck_qty,0) as 'pchOdrDtlInfo.stck_qty'
                ,itm.expry_yn as 'pchOdrDtlInfo.expry_yn', itm.expry_period as 'pchOdrDtlInfo.expry_period'
                , date_format(adddate(now(), interval itm.expry_period day),'%Y.%m.%d') as 'pchOdrDtlInfo.expry_date'
                ,ptcl.podr_amt as 'pchOdrDtlInfo.podr_amt', date_format(base.dlv_req_dt,'%Y.%m.%d') as dlv_req_dt, base.shmt_whs_id, base.shmt_location
                ,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
        join pch_odr_dtl_ptcl ptcl on base.pch_podr_unq_no = ptcl.pch_podr_unq_no and ptcl.podr_dtl_st_cd = 'POD0'
        <if test="sItemNm != null and sItemNm != ''">
                and (ptcl.item_id like concat('%',#{sItemNm},'%') or ptcl.item_nm like concat('%',#{sItemNm},'%'))
        </if>
        join item_base_info itm on ptcl.item_id = itm.item_id
        left outer join stck_mgnt_base_info stck on base.brand_id = stck.brand_id and base.shmt_whs_id = stck.whs_id and base.shmt_location = stck.location
        and ptcl.item_id = stck.item_id
        where  1=1
        and    base.brand_id = #{sBrandId}
        and    base.dlv_req_dt between #{fromDt} and #{toDt}
        and    base.dlv_dvsn = 'DL01'  -- 본사배송구분
        and    base.whs_dvsn = 'W02'  -- 납품매장창고구분
        <if test="sShmtWhsId != null and sShmtWhsId != ''">
        and    base.shmt_whs_id = #{sShmtWhsId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
        and    base.whs_id = #{sWhsId}
        </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="selectShmtTargetOdrGridCnt" resultType="int">
        select count(*) as cnt
        from   pch_odr_base_info base
        join pch_odr_dtl_ptcl ptcl on base.pch_podr_unq_no = ptcl.pch_podr_unq_no and ptcl.podr_dtl_st_cd = 'POD0'
        <if test="sItemNm != null and sItemNm != ''">
            and (ptcl.item_id like concat('%',#{sItemNm},'%') or ptcl.item_nm like concat('%',#{sItemNm},'%'))
        </if>
        join item_base_info itm on ptcl.item_id = itm.item_id
        left outer join stck_mgnt_base_info stck on base.brand_id = stck.brand_id and base.shmt_whs_id = stck.whs_id and base.shmt_location = stck.location
        and ptcl.item_id = stck.item_id
        where  1=1
        and    base.brand_id = #{sBrandId}
        and    base.dlv_req_dt between #{fromDt} and #{toDt}
        and    base.dlv_dvsn = 'DL01'  -- 본사배송구분
        and    base.whs_dvsn = 'W02'  -- 납품매장창고구분
        <if test="sShmtWhsId != null and sShmtWhsId != ''">
            and    base.shmt_whs_id = #{sShmtWhsId}
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            and    base.whs_id = #{sWhsId}
        </if>

    </select>

    <insert id="insertShmtOdrBase">
        insert into shmt_odr_base_info
            (shmt_req_unq_no, shmt_req_st_cd, shmt_reg_dt, shmt_whs_id, shmt_location
            , whs_dvsn, brand_id, store_id, podr_dvsn, item_qty
            , shmt_total_amt,  podr_mgr_nm, dlv_req_dt, whs_id, location
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
            (#{entity.shmtReqUnqNo}, #{entity.shmtReqStCd}, date_format(now(),'%Y%m%d'), #{entity.shmtWhsId}, #{entity.shmtLocation}
            , #{entity.whsDvsn}, #{entity.brandId}, #{entity.storeId}, #{entity.podrDvsn}, #{entity.itemQty}
            , #{entity.shmtTotalAmt}, #{entity.podrMgrNm}, #{entity.dlvReqDt}, #{entity.whsId}
            , #{entity.location}, NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <insert id="insertShmtOdrDtl">
        insert into shmt_odr_dtl_ptcl
            (shmt_req_unq_no, shmt_req_dtl_no, item_id, item_nm, unit_amt
            , unit, podr_qty, podr_amt, shmt_qty, shmt_amt, expry_date
            , pch_podr_unq_no, pch_podr_dtl_no, pch_req_unq_no, pch_req_dtl_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.shmtReqUnqNo}, #{item.shmtReqDtlNo}, #{item.itemId}, #{item.itemNm}, #{item.unitAmt}
            , #{item.unit}, #{item.podrQty}, #{item.podrAmt}, #{item.shmtQty}, #{item.shmtAmt}, #{item.expryDate}
            , #{item.pchPodrUnqNo}, #{item.pchPodrDtlNo}, #{item.pchReqUnqNo}, #{item.pchReqDtlNo}
            , NOW(), #{userId}, NOW(), #{userId})
        </foreach>
    </insert>

    <select id="selectShmtJobDataList" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
        select base.brand_id,base.pch_podr_unq_no, ptcl.pch_podr_dtl_no as 'pchOdrDtlInfo.pch_podr_dtl_no'
                ,ptcl.item_id as 'pchOdrDtlInfo.item_id' , ptcl.item_nm as 'pchOdrDtlInfo.item_nm', base.whs_id
                ,base.location, ptcl.unit_amt as 'pchOdrDtlInfo.unit_amt', ptcl.podr_qty as 'pchOdrDtlInfo.podr_qty'
                ,nvl(stck.stck_qty,0) as 'pchOdrDtlInfo.stck_qty'
                ,itm.expry_yn as 'pchOdrDtlInfo.expry_yn', nvl(itm.expry_period,0) as 'pchOdrDtlInfo.expry_period'
                ,ptcl.podr_amt as 'pchOdrDtlInfo.podr_amt', date_format(base.dlv_req_dt,'%Y.%m.%d') as dlv_req_dt
                , base.shmt_whs_id, base.shmt_location, ptcl.pch_req_unq_no as 'pchOdrDtlInfo.pch_req_unq_no' , ptcl.pch_req_dtl_no as 'pchOdrDtlInfo.pch_req_dtl_no'
                , base.store_id , base.podr_mgr_nm , base.podr_mgr_tel_no
                , fn_get_telno((select tel_no from store_base_info where store_id = base.store_id )) as deli_tel_no
                , date_format(base.podr_dt,'%Y%m%d') as podr_dt
        from   pch_odr_base_info base
        join pch_odr_dtl_ptcl ptcl on base.pch_podr_unq_no = ptcl.pch_podr_unq_no and ptcl.podr_dtl_st_cd = 'POD0'
        join item_base_info itm on ptcl.item_id = itm.item_id
        left outer join stck_mgnt_base_info stck on base.brand_id = stck.brand_id and base.shmt_whs_id = stck.whs_id and base.shmt_location = stck.location
        and ptcl.item_id = stck.item_id
        where  1=1
        and    base.brand_id = #{brandId}
        and    concat(base.pch_podr_unq_no,ptcl.pch_podr_dtl_no) in
        <foreach collection="gridInsertData" item="item" separator="," open="(" close=")">
            concat(#{item.pchPodrUnqNo},#{item.pchPodrDtlNo})
        </foreach>
        order by  base.brand_id, base.shmt_whs_id, base.shmt_location, base.whs_id,base.location
    </select>

    <insert id="insertDeliInfoMngt">
        INSERT INTO deli_info_mgnt
        (dlv_sttmt_unq_no, spply_id, spply_nm, dlv_reg_dt, dlv_amt
        , dlv_mgr_nm, dlv_mgr_tel_no, dlv_mgr_emal, dlv_mgr_mbl_no, vndr_tel_no
        , dlv_st_cd, podr_reg_dt, dlv_req_dt, dlv_sch_dt, dlv_cmplt_dt
        , whs_id, location, whs_dvsn, brand_id
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        (#{entity.dlvSttmtUnqNo}, #{entity.spplyId}, #{entity.spplyNm}, date_format(now(),'%Y%m%d'), #{entity.dlvAmt}
        , #{entity.dlvMgrNm}, #{entity.dlvMgrTelNo}, #{entity.dlvMgrEmal}, #{entity.dlvMgrMblNo}, #{entity.vndrTelNo}
        , #{entity.dlvStCd}, #{entity.podrRegDt}, #{entity.dlvReqDt}, #{entity.dlvSchDt}, #{entity.dlvCmpltDt}
        , #{entity.whsId}, #{entity.location}, #{entity.whsDvsn}, #{entity.brandId}
        , NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <insert id="insertDeliDtlPtcl" >
        INSERT INTO deli_dtl_ptcl
        (dlv_sttmt_unq_no, dlv_sttmt_dtl_no, pch_podr_unq_no, pch_podr_dtl_no, pch_req_unq_no
        , pch_req_dtl_no, item_id, item_nm, unit, unit_amt
        , price_unit, podr_qty, dlv_qty, podr_amt, dlv_amt
        , expry_date, delay_yn, delay_rsn
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach collection="gridInsertData" item="item" separator=",">
        (#{item.dlvSttmtUnqNo}, #{item.dlvSttmtDtlNo}, #{item.pchPodrUnqNo}, #{item.pchPodrDtlNo}, #{item.pchReqUnqNo}
        , #{item.pchReqDtlNo}, #{item.itemId}, #{item.itemNm}, #{item.unit}, #{item.unitAmt}
        , #{item.priceUnit}, #{item.podrQty}, #{item.dlvQty}, #{item.podrAmt}, #{item.dlvAmt}
        , #{item.expryDate}, #{item.delayYn}, #{item.delayRsn}
        , NOW(), #{userId}, NOW(), #{userId})
        </foreach>
    </insert>

    <select id="selectShmtSearchGridList" resultType="com.oqpo.api.entity.inoutmng.ShmtOdrBaseEntity">
        /*  shmtOdr.selectShmtSearchGridList */
        select base.shmt_req_unq_no , ptcl.shmt_req_dtl_no as 'shmtOdrDtlInfo.shmt_req_dtl_no' ,  base.brand_id, fn_brand_nm(base.brand_id) as brand_nm
               ,ptcl.pch_podr_unq_no as 'shmtOdrDtlInfo.pch_podr_unq_no' , ptcl.pch_podr_dtl_no as 'shmtOdrDtlInfo.pch_podr_dtl_no'
               ,base.shmt_req_st_cd, fn_code_nm('SHMT_REQ_ST_CD',base.shmt_req_st_cd) as shmt_req_st_nm  , ptcl.item_id as 'shmtOdrDtlInfo.item_id'
               ,ptcl.item_nm as 'shmtOdrDtlInfo.item_nm'  , 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  , ptcl.shmt_qty as 'shmtOdrDtlInfo.shmt_qty'
               ,ptcl.shmt_amt as 'shmtOdrDtlInfo.shmt_amt' , nvl(proc.whs_qty,0) as 'whsProcInfo.whs_qty' , nvl(proc.whs_amt,0) as 'whsProcInfo.whs_amt'
                 ,date_format(proc.whs_dt,'%Y.%m.%d') as 'whsProcInfo.whs_dt', proc.whs_cnfm_nm as 'whsProcInfo.whs_cnfm_nm'
        from  shmt_odr_base_info base
         join shmt_odr_dtl_ptcl ptcl on base.shmt_req_unq_no = ptcl.shmt_req_unq_no
         join deli_dtl_ptcl deli on deli.pch_podr_unq_no = ptcl.pch_podr_unq_no and deli.pch_podr_dtl_no = ptcl.pch_podr_dtl_no
         left outer join whs_proc_info_ptcl proc on proc.dlv_sttmt_unq_no = deli.dlv_sttmt_unq_no and proc.dlv_sttmt_dtl_no = deli.dlv_sttmt_dtl_no
        where 1=1
        and   base.brand_id = #{sBrandId}
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != ''">
        and   base.shmt_reg_dt between #{fromDt} and #{toDt}
        </if>
        <if test="sShmtWhsId != null and sShmtWhsId != ''">
        and   base.shmt_whs_id = #{sShmtWhsId}
        </if>
        <if test="sShmtReqStCd != null and sShmtReqStCd != ''">
        and   base.shmt_req_st_cd = #{sShmtReqStCd}
        </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 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="selectShmtSearchGridCnt" resultType="int">
        /*  shmtOdr.selectShmtSearchGridCnt */
        select count(*)
        from  shmt_odr_base_info base
        join shmt_odr_dtl_ptcl ptcl on base.shmt_req_unq_no = ptcl.shmt_req_unq_no
        join deli_dtl_ptcl deli on deli.pch_podr_unq_no = ptcl.pch_podr_unq_no and deli.pch_podr_dtl_no = ptcl.pch_podr_dtl_no
        left outer join whs_proc_info_ptcl proc on proc.dlv_sttmt_unq_no = deli.dlv_sttmt_unq_no and proc.dlv_sttmt_dtl_no = deli.dlv_sttmt_dtl_no
        where 1=1
        and   base.brand_id = #{sBrandId}
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != ''">
            and   base.shmt_reg_dt between #{fromDt} and #{toDt}
        </if>
        <if test="sShmtWhsId != null and sShmtWhsId != ''">
            and   base.shmt_whs_id = #{sShmtWhsId}
        </if>
        <if test="sShmtReqStCd != null and sShmtReqStCd != ''">
            and   base.shmt_req_st_cd = #{sShmtReqStCd}
        </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>

</mapper>
