<?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.stockmng.StockMngMapper">

    <select id="selectStockMngGridList" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        /* StockMngMapper.selectStockMngGridList */
        SELECT A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            C.store_id, FN_STORE_NM(A.brand_id, C.store_id) AS store_nm,
            A.whs_id, C.whs_nm, A.location, D.location_nm, A.item_id, E.item_nm, E.unit, A.stck_qty,
            A.PRP_STCK_QTY as prp_stck_qty, 
            A.SFT_STCK_RT as sft_stck_rt, 
            CASE WHEN A.PRP_STCK_QTY > 0 THEN A.STCK_QTY / A.PRP_STCK_QTY * 100 ELSE 0 END AS cur_stck_rt
            /* , A.cur_stck_rt */
        FROM stck_mgnt_base_info A
        INNER JOIN whs_mgnt_base_info C ON A.brand_id = C.brand_id AND A.whs_id = C.whs_id
        INNER JOIN whs_mgnt_base_loc D ON A.brand_id = D.brand_id AND A.whs_id = D.whs_id AND A.location = D.location
        INNER JOIN item_base_info E ON A.item_id = E.item_id
        <if test="sItemClass1 != null and sItemClass1 != ''">
        INNER JOIN item_class_ptcl B ON A.item_id = B.item_id
            AND B.item_class1 = #{sItemClass1}
            <if test="sItemClass2 != null and sItemClass2 != ''">
            AND B.item_class2 = #{sItemClass2}
                <if test="sItemClass3 != null and sItemClass3 != ''">
                AND B.item_class3 = #{sItemClass3}
                    <if test="sItemClass4 != null and sItemClass4 != ''">
                    AND B.item_class4 = #{sItemClass4}
                    </if>
                </if>
            </if>
        </if>
        WHERE A.del_yn = 'N'
        AND A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND C.store_id = #{sStoreId}
            AND C.whs_dvsn = 'W02'
        </if>
        <if test="sStoreId == null or sStoreId == ''">
            AND C.whs_dvsn = 'W01'
        </if>
        <if test="sWhsId != null and sWhsId != ''">
        AND A.whs_id = #{sWhsId}
        </if>
        <if test="sLocation != null and sLocation != ''">
        AND A.location = #{sLocation}
        </if>
        <if test="sItemStCd != null and sItemStCd != ''">
        AND E.item_st_cd = #{sItemStCd}
        </if>
        <if test="sItemType != null and sItemType != ''">
        AND E.item_type = #{sItemType}
        </if>
        <if test='"P".equals(sItemGubun)'>
        AND E.pch_item_yn = 'Y'
        </if>
        <if test='"S".equals(sItemGubun)'>
        AND E.sale_item_yn = 'Y'
        </if>
        <if test="sItemId != null and sItemId != ''">
        AND A.item_id = #{sItemId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
        AND E.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 A.item_id ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.item_id DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.item_id ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStockMngGridCnt" resultType="int">
        /* StockMngMapper.selectStockMngGridCnt */
        SELECT COUNT(*)
        FROM stck_mgnt_base_info A
        INNER JOIN whs_mgnt_base_info C ON A.brand_id = C.brand_id AND A.whs_id = C.whs_id
        INNER JOIN whs_mgnt_base_loc D ON A.brand_id = D.brand_id AND A.whs_id = D.whs_id AND A.location = D.location
        INNER JOIN item_base_info E ON A.item_id = E.item_id
        <if test="sItemClass1 != null and sItemClass1 != ''">
            INNER JOIN item_class_ptcl B ON A.item_id = B.item_id
            AND B.item_class1 = #{sItemClass1}
            <if test="sItemClass2 != null and sItemClass2 != ''">
                AND B.item_class2 = #{sItemClass2}
                <if test="sItemClass3 != null and sItemClass3 != ''">
                    AND B.item_class3 = #{sItemClass3}
                    <if test="sItemClass4 != null and sItemClass4 != ''">
                        AND B.item_class4 = #{sItemClass4}
                    </if>
                </if>
            </if>
        </if>
        WHERE A.del_yn = 'N'
        AND A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND C.store_id = #{sStoreId}
            AND C.whs_dvsn = 'W02'
        </if>
        <if test="sStoreId == null or sStoreId == ''">
            AND C.whs_dvsn = 'W01'
        </if>
        <if test="sWhsId != null and sWhsId != ''">
            AND A.whs_id = #{sWhsId}
        </if>
        <if test="sLocation != null and sLocation != ''">
            AND A.location = #{sLocation}
        </if>
        <if test="sItemStCd != null and sItemStCd != ''">
            AND E.item_st_cd = #{sItemStCd}
        </if>
        <if test="sItemType != null and sItemType != ''">
            AND E.item_type = #{sItemType}
        </if>
        <if test='"P".equals(sItemGubun)'>
            AND E.pch_item_yn = 'Y'
        </if>
        <if test='"S".equals(sItemGubun)'>
            AND E.sale_item_yn = 'Y'
        </if>
        <if test="sItemId != null and sItemId != ''">
            AND A.item_id = #{sItemId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            AND E.item_nm LIKE CONCAT('%',#{sItemNm},'%')
        </if>
    </select>

    <select id="selectStckMgntBaseInfo" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        /* StockMngMapper.selectStckMgntBaseInfo */
        SELECT A.brand_id, A.whs_id, C.whs_nm, A.location, D.location_nm, A.item_id, A.unit, A.stck_qty, A.prp_stck_qty, A.sft_stck_rt, A.cur_stck_rt,
            DATE_FORMAT(A.sys_reg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_reg_dttm, A.sys_reg_id, DATE_FORMAT(A.sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, A.sys_chg_id,
            B.item_nm
        FROM stck_mgnt_base_info A
        LEFT JOIN item_base_info B ON A.item_id = B.item_id
        INNER JOIN whs_mgnt_base_info C ON A.brand_id = C.brand_id AND A.whs_id = C.whs_id
        INNER JOIN whs_mgnt_base_loc D ON A.brand_id = D.brand_id AND A.whs_id = D.whs_id AND A.location = D.location
        WHERE A.brand_id = #{brandId}
        AND A.whs_id = #{whsId}
        AND A.location = #{location}
        AND A.item_id = #{itemId}
    </select>

    <select id="selectStckMgntBaseInfo4CddProc" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        /* StockMngMapper.selectStckMgntBaseInfo4CddProc */
        SELECT A.brand_id, A.whs_id, C.whs_nm, A.location, D.location_nm, A.item_id, IFNULL(A.unit, B.UNIT) AS unit, A.stck_qty, A.prp_stck_qty, A.sft_stck_rt, A.cur_stck_rt,
            DATE_FORMAT(A.sys_reg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_reg_dttm, A.sys_reg_id, DATE_FORMAT(A.sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, A.sys_chg_id,
            B.item_nm
        FROM item_base_info B
        LEFT JOIN stck_mgnt_base_info A ON A.item_id = B.item_id AND A.brand_id = #{brandId} AND A.whs_id = #{whsId} AND A.location = #{location}
        LEFT JOIN whs_mgnt_base_info C ON A.brand_id = C.brand_id AND A.whs_id = C.whs_id
        LEFT JOIN whs_mgnt_base_loc D ON A.brand_id = D.brand_id AND A.whs_id = D.whs_id AND A.location = D.location
        WHERE B.item_id = #{itemId}
    </select>

    <insert id="insertStckMgntBaseInfo" >
        /* StockMngMapper.insertStckMgntBaseInfo */
        INSERT INTO stck_mgnt_base_info
            (brand_id, whs_id, location, item_id, unit, stck_qty, wait_qty, prp_stck_qty, sft_stck_rt,
             sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.brandId}, #{entity.whsId}, #{entity.location}, #{entity.itemId}, #{entity.unit}, #{entity.stckQty},
             #{entity.waitQty}, #{entity.prpStckQty}, #{entity.sftStckRt},
             NOW(), #{userId}, NOW(), #{userId})
        ON DUPLICATE KEY UPDATE
            unit = #{entity.unit},
            stck_qty = #{entity.stckQty},
            wait_qty = #{entity.waitQty},
            prp_stck_qty = #{entity.prpStckQty},
            sft_stck_rt = #{entity.sftStckRt},
            del_yn = 'N',
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
    </insert>

    <update id="updateStckMgntBaseInfo4StckProc" >
        /* StockMngMapper.updateStckMgntBaseInfo4StckProc */
        UPDATE stck_mgnt_base_info SET
            stck_qty = #{entity.stckQty},       /* 재고수량 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
        AND location = #{entity.location}
        AND item_id = #{entity.itemId}
    </update>

    <update id="updateStckMgntBaseInfo4BaseChg" >
        /* StockMngMapper.updateStckMgntBaseInfo4BaseChg */
        UPDATE stck_mgnt_base_info SET
            prp_stck_qty = #{entity.prpStckQty},    /* 적정재고수량 */
            sft_stck_rt = #{entity.sftStckRt},      /* 안전재고비율 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
        AND location = #{entity.location}
        AND item_id = #{entity.itemId}
    </update>

    <update id="updateStckMgntBaseInfo4Delete" >
        /* StockMngMapper.updateStckMgntBaseInfo4Delete */
        UPDATE stck_mgnt_base_info SET
            del_yn = 'Y',
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
        AND location = #{entity.location}
        AND item_id = #{entity.itemId}
    </update>

    <insert id="insertStckMgntDtlHist" >
        /* 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
            (#{entity.stckMgntDtlNo}, #{entity.brandId}, #{entity.whsId}, #{entity.location}, #{entity.itemId},
             #{entity.itemNm}, #{entity.inoutCd}, #{entity.inoutQty}, #{entity.whsMgntUnqNo}, #{entity.rtnMgntUnqNo}, #{entity.chgDvsn}, #{entity.relationNo},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <select id="selectStockHistGridList" resultType="com.oqpo.api.entity.stockmng.StckMgntDtlHistEntity">
        /* StockMngMapper.selectStockHistGridList */
        SELECT A.stck_mgnt_dtl_no,
            A.inout_cd, FN_CODE_NM('INOUT_CD', A.inout_cd) AS inout_nm, A.inout_qty,
            A.relation_no AS relation_no,    /* 관련번호 */
            A.chg_dvsn, FN_CODE_NM('CHG_DVSN', A.chg_dvsn) AS chg_dvsn_nm, DATE_FORMAT(A.sys_reg_dttm,'%Y.%m.%d') AS sys_reg_dttm
        FROM stck_mgnt_dtl_hist A
        WHERE A.brand_id = #{sBrandId}
        AND A.whs_id = #{sWhsId}
        AND A.location = #{sLocation}
        AND A.item_id = #{sItemId}
        <if test="sInoutCd != null and sInoutCd != ''">
            AND A.inout_cd = #{sInoutCd}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.sys_reg_dttm BETWEEN STR_TO_DATE(CONCAT(#{fromDt},'000000'),'%Y%m%d%H%i%S') AND STR_TO_DATE(CONCAT(#{toDt},'235959'),'%Y%m%d%H%i%S')
        </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 A.stck_mgnt_dtl_no ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.stck_mgnt_dtl_no DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.stck_mgnt_dtl_no ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStockHistGridCnt" resultType="int">
        /* StockMngMapper.selectStockHistGridCnt */
        SELECT COUNT(*)
        FROM stck_mgnt_dtl_hist A
        WHERE A.brand_id = #{sBrandId}
        AND A.whs_id = #{sWhsId}
        AND A.location = #{sLocation}
        AND A.item_id = #{sItemId}
        <if test="sInoutCd != null and sInoutCd != ''">
            AND A.inout_cd = #{sInoutCd}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.sys_reg_dttm BETWEEN STR_TO_DATE(CONCAT(#{fromDt},'000000'),'%Y%m%d%H%i%S') AND STR_TO_DATE(CONCAT(#{toDt},'235959'),'%Y%m%d%H%i%S')
        </if>
    </select>

    <select id="selectStockCddGridList" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        /* StockMngMapper.selectStockCddGridList */
        SELECT A.item_id, A.item_nm, IFNULL(B.unit, A.unit) AS unit, B.stck_qty
        FROM item_base_info A
        LEFT JOIN stck_mgnt_base_info B ON A.item_id = B.item_id AND B.brand_id = #{brandId} AND B.whs_id = #{whsId} AND B.location = #{location}
        WHERE A.brand_id = #{brandId}
        AND A.item_id IN
        <foreach collection="gridItemData" item="item" separator="," open="(" close=")">
            #{item.itemId}
        </foreach>
    </select>

    <update id="updateStckPoUseQty">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE   /* StockMngMapper.updateStckPoUseQty */
                 stck_mgnt_base_info SET
                 wait_qty = wait_qty + #{item.waitQty},
                 sys_chg_dttm = NOW(),
                 sys_chg_id = #{userId}
            WHERE brand_id = #{brandId}
            AND whs_id = #{whsId}
            AND location = #{location}
            AND item_id = #{item.itemId}
        </foreach>
    </update>

    <update id="updateMinusStckPoUseQty">
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE   /* StockMngMapper.updateStckPoUseQty */
            stck_mgnt_base_info SET
            wait_qty = wait_qty - #{item.waitQty},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE brand_id = #{brandId}
            AND whs_id = #{whsId}
            AND location = #{location}
            AND item_id = #{item.itemId}
        </foreach>
    </update>

    <update id="updateMinStockQty" >
        /* StockMngMapper.updateMinStockQty */
        <foreach collection="gridUpdateData" item="item" separator=";">
        UPDATE stck_mgnt_base_info SET
            stck_qty = stck_qty - #{item.stckQty},       /* 재고수량 */
            wait_qty = wait_qty - #{item.waitQty},       /* 대기수량 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{item.brandId}
        AND whs_id = #{item.whsId}
        AND location = #{item.location}
        AND item_id = #{item.itemId}
        </foreach>
    </update>

    <insert id="insertMultiStckMgntDtlHist" >
        /* StockMngMapper.insertMultiStckMgntDtlHist */
        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="updateWarehouseStockQty" >
        /* StockMngMapper.updateWarehouseStockQty */
        <foreach collection="gridUpdateData" item="item" separator=";">
            UPDATE stck_mgnt_base_info SET
            stck_qty = stck_qty + #{item.stckQty},       /* 재고수량 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
            WHERE brand_id = #{item.brandId}
            AND whs_id = #{item.whsId}
            AND location = #{item.location}
            AND item_id = #{item.itemId}
        </foreach>
    </update>

    <select id="selectWhsLocOutPassQty" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        /* StockMngMapper.selectWhsLocOutPassQty */
        SELECT A.item_id, A.item_nm, IFNULL(B.unit, A.unit) AS unit, nvl( B.stck_qty - B.wait_qty,0 ) as whs_pass_qty
        FROM item_base_info A
        LEFT JOIN stck_mgnt_base_info B ON A.item_id = B.item_id AND B.brand_id = #{brandId} AND B.whs_id = #{whsId} AND B.location = #{location}
        WHERE A.brand_id = #{brandId}
        AND A.item_id IN
        <foreach collection="itemList" item="item" separator="," open="(" close=")">
            #{item.itemId}
        </foreach>
    </select>


    <select id="selectPopItemStockMngGridList" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.item_id, item.item_nm, base.unit, base.stck_qty, base.wait_qty
        from  stck_mgnt_base_info base
            , item_base_info item
        where base.brand_id = item.brand_id
        and   base.item_id = item.item_id
        and   base.brand_id = #{sBrandId}
        and   base.whs_id = #{sWhsId}
        and   base.location = #{sLocation}
        <if test="sItemNm != null and sItemNm != ''">
        and   (item.item_id like concat('%', #{sItemNm},'%') or item.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 item.item_nm ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY item.item_nm DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY item.item_nm ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>

    </select>

    <select id="selectPopItemStockMngGridCnt" resultType="int">
        select count(*) as cnt
        from  stck_mgnt_base_info base
        , item_base_info item
        where base.brand_id = item.brand_id
        and   base.item_id = item.item_id
        and   base.brand_id = #{sBrandId}
        and   base.whs_id = #{sWhsId}
        and   base.location = #{sLocation}
        <if test="sItemNm != null and sItemNm != ''">
            and   (item.item_id like concat('%', #{sItemNm},'%') or item.item_nm like concat('%', #{sItemNm},'%'))
        </if>
    </select>

    <select id="selectPopItemStockInfo" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        select base.brand_id, fn_brand_nm(base.brand_id) as brand_nm, base.item_id, item.item_nm, base.unit, base.stck_qty, base.wait_qty
        from  item_base_info item
         left outer join stck_mgnt_base_info base on base.brand_id = item.brand_id and base.item_id = item.item_id
                        and   base.whs_id = #{sWhsId} and   base.location = #{sLocation}
        where item.brand_id = #{sBrandId}
        and   item.barcode = #{sBarcode}
    </select>


    <insert id="insertInOutStckMgntBaseInfo" >
        /* StockMngMapper.insertInOutStckMgntBaseInfo */
        INSERT INTO stck_mgnt_base_info
            (brand_id, whs_id, location, item_id, unit, stck_qty, wait_qty, prp_stck_qty, sft_stck_rt, cur_stck_rt,
             sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach item="item" collection="gridUpdateData" separator=",">
            (#{item.brandId}, #{item.whsId}, #{item.location}, #{item.itemId}, #{item.unit}, #{item.stckQty},
             0, null, null, null,
             NOW(), #{userId}, NOW(), #{userId})
        </foreach>
        ON DUPLICATE KEY UPDATE
            stck_qty = stck_qty + VALUES(stck_qty),
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
    </insert>

    <select id="selectWhsLocationItemList" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        select stck.item_id, item.item_nm, '' as excelQty
        from stck_mgnt_base_info  stck
        join item_base_info item on stck.brand_id = item.brand_id and stck.item_id = item.item_id
        where stck.brand_id = #{brandId}
        and   stck.whs_id = #{whsId}
        and   stck.location = #{location}
    </select>

    <select id="selectBoWhsInfo" resultType="com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity">
        select store.brand_id, store.store_id, whs.whs_id, loc.location
        from store_base_info  store
        join whs_mgnt_base_info whs on whs.brand_id = store.brand_id and whs.store_id = store.store_id
        join whs_mgnt_base_loc loc on whs.brand_id = store.brand_id and whs.whs_id = loc.whs_id
        where oq_brand_id = #{oqBrandId} and oq_store_id = #{oqStoreId}
    </select>

    <insert id="insertStckSaleMgntDtlHist" >
        /* StockMngMapper.insertStckSaleMgntDtlHist */
        INSERT INTO stck_sale_mgnt_dtl_hist
        (brand_id, whs_id, location, item_id, base_ym, item_nm, sale_qty, oq_brand_id, oq_menu_cd,
        sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        <foreach item="item" collection="gridUpdateData" separator=",">
            (#{item.brandId}, #{item.whsId}, #{item.location}, #{item.itemId}, STR_TO_DATE( #{item.baseYm}, '%Y%m%d%H%m%i' ) , #{item.itemNm},
            #{item.saleQty}, #{item.oqBrandId}, #{item.oqMenuCd},
            NOW(), #{userId}, NOW(), #{userId})
        </foreach>
        ON DUPLICATE KEY UPDATE
        sale_qty = sale_qty + VALUES(sale_qty),
        sys_chg_dttm = NOW(),
        sys_chg_id = #{userId}
    </insert>



</mapper>
