<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.oqpo.api.mapper.pomng.PchOrdMapper">
    <select id="selectRecPchInitInfo" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
        SELECT distinct base.BRAND_ID, FN_BRAND_NM(base.brand_id) AS brand_nm
            , date_format(now() ,'%Y.%m.%d') as podr_dt
            , base.whs_id, base.location
            , concat(FN_WHS_NM(base.brand_id, base.whs_id) ,'-',FN_WHS_LOCATION_NM(base.brand_id, base.whs_id, base.location)) as dlv_place
            , case when base.store_id is null or base.store_id = '' then 'DL02' else 'DL01' end as dlv_dvsn
            , fn_code_nm('DLV_DVSN',case when base.store_id is null or base.store_id = '' then 'DL02' else 'DL01' end)  as dlv_dvsn_nm
            , base.store_id
        FROM pch_req_base_info base
        , pch_req_dtl_ptcl ptcl
        WHERE 1 = 1
        and base.brand_id = #{brandId}
        AND base.PCH_REQ_UNQ_NO = ptcl.PCH_REQ_UNQ_NO
        AND base.PCH_REQ_ST_CD = 'PR20'
        AND concat(base.PCH_REQ_UNQ_NO, ptcl.PCH_REQ_DTL_NO) IN
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
            concat(#{item.pchReqUnqNo},#{item.pchReqDtlNo})
        </foreach>
    </select>

    <select id="selectRecPchDtlInitInfo" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
        select base.pch_req_unq_no , ptcl.pch_req_dtl_no
            , ptcl.item_id , ptcl.item_nm , ptcl.unit_amt , ptcl.unit
            , ptcl.pch_req_qty, ptcl.pch_req_qty as podr_qty, ptcl.pch_req_amt
            , ptcl.pch_req_amt as podr_amt , date_format(base.dlv_req_dt,'%Y.%m.%d') as dlv_req_dt
            , 0 as whs_pass_qty, bunit.spply_id, fn_spply_nm(bunit.spply_id) as spply_nm
            , ptcl.brand_unit_unq_no, ptcl.store_unit_unq_no
        from pch_req_base_info base
            , pch_req_dtl_ptcl ptcl
            , brand_item_unit_info bunit
        where 1 = 1
        and base.brand_id = #{brandId}
        and base.pch_req_unq_no = ptcl.pch_req_unq_no
        and ptcl.brand_unit_unq_no = bunit.brand_unit_unq_no
        and base.pch_req_st_cd = 'PR20'
        and concat(base.pch_req_unq_no, ptcl.pch_req_dtl_no) in
        <foreach collection="gridUpdateData" item="item" separator="," open="(" close=")">
           concat(#{item.pchReqUnqNo},#{item.pchReqDtlNo})
        </foreach>
    </select>

    <select id="selectPoInitInfo" resultType="com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity">
         select brand_id, brand_nm, date_format(now() ,'%Y.%m.%d') as podr_dt
                  , 'DL02' as dlv_dvsn
                , fn_code_nm('DLV_DVSN','DL02')  as dlv_dvsn_nm
         from   brand_base_info
         where  brand_id = #{brandId}
    </select>

    <insert id="insertPchOdrBaseInfo">
        INSERT INTO pch_odr_base_info
        (pch_podr_unq_no, brand_id, podr_total_amt
        , dlv_dvsn, dlv_req_dt, podr_mgr_nm, podr_dvsn
        , pch_odr_st_cd, note, spply_id, podr_mgr_id
        , podr_dt, podr_mgr_tel_no, podr_item_qty, whs_dvsn, whs_id
        , location, shmt_whs_id, shmt_location, store_id
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        (#{entity.pchPodrUnqNo}, #{entity.brandId}, #{entity.podrTotalAmt}
       , #{entity.dlvDvsn}, #{entity.dlvReqDt}, #{entity.podrMgrNm}, #{entity.podrDvsn}
        , 'PO20', #{entity.note}, #{entity.spplyId}, #{userId}
        , date_format(now(),'%Y%m%d') , #{entity.podrMgrTelNo}, #{entity.podrItemQty}, #{entity.whsDvsn}, #{entity.whsId}
        , #{entity.location}, #{entity.shmtWhsId}, #{entity.shmtLocation}, #{entity.storeId}
        , NOW(), #{userId}, NOW(), #{userId})

    </insert>

    <insert id="insertPchOdrDtlPtcl">
        INSERT INTO pch_odr_dtl_ptcl
            (pch_podr_unq_no, pch_podr_dtl_no, pch_req_unq_no, pch_req_dtl_no, item_id
            , item_nm, podr_qty, podr_amt, curr, unit, podr_dtl_st_cd
            , unit_amt, dlv_sch_dt, store_unit_unq_no, brand_unit_unq_no
            , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach collection="gridInsertData" item="item" separator=",">
            (#{item.pchPodrUnqNo}, #{item.pchPodrDtlNo}, #{item.pchReqUnqNo}, #{item.pchReqDtlNo}, #{item.itemId}
            , #{item.itemNm}, #{item.podrQty}, #{item.podrAmt}, #{item.curr}, #{item.unit}, #{item.podrDtlStCd}
            , #{item.unitAmt}, #{item.dlvSchDt}, #{item.storeUnitUnqNo}, #{item.brandUnitUnqNo}
            , NOW(), #{userId}, NOW(), #{userId})
        </foreach>
    </insert>


    <select id="selectPoPchDtlSaveInfo" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
        select ptcl.item_id, ptcl.item_nm,  ptcl.unit, ptcl.unit_amt, bunit.spply_id
        from  pch_req_dtl_ptcl ptcl
          join pch_req_base_info base on ptcl.pch_req_unq_no = base.pch_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.pch_req_unq_no = #{pchReqUnqNo}
        and   ptcl.pch_req_dtl_no = #{pchReqDtlNo}
    </select>


    <select id="selectPoItemDtlSaveInfo" resultType="com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity">
        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="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, fn_get_telno(base.podr_mgr_tel_no) as 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, fn_get_telno(base.podr_mgr_tel_no) as 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>
