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

    <select id="selectStockCddGridList" resultType="com.oqpo.api.entity.stockmng.StckCddMgntInfoEntity">
        /* StockCddMapper.selectStockCddGridList */
        SELECT A.cdd_id,
            DATE_FORMAT(A.cdd_dt, '%Y.%m.%d') AS cdd_dt, TIME_FORMAT(A.cdd_tm, '%H:%i:%s') AS cdd_tm,
            A.cdd_dvsn, FN_CODE_NM('CDD_DVSN', A.cdd_dvsn) AS cdd_dvsn_nm,
            IFNULL(A.cdd_mgr_nm, FN_USER_NM(A.sys_reg_id)) AS cdd_mgr_nm,   /* 실사담당자 */
            A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.whs_id, C.whs_nm, A.location, D.location_nm
        FROM stck_cdd_mgnt_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
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
        AND C.store_id = #{sStoreId}
        </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="sCddDvsn != null and sCddDvsn != ''">
            AND A.cdd_dvsn = #{sCddDvsn}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.cdd_dt BETWEEN #{fromDt} AND #{toDt}
        </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.cdd_id ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.cdd_id DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.cdd_id ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStockCddGridCnt" resultType="int">
        /* StockCddMapper.selectStockCddGridCnt */
        SELECT COUNT(*)
        FROM stck_cdd_mgnt_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
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND C.store_id = #{sStoreId}
        </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="sCddDvsn != null and sCddDvsn != ''">
            AND A.cdd_dvsn = #{sCddDvsn}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.cdd_dt BETWEEN #{fromDt} AND #{toDt}
        </if>
    </select>

    <insert id="insertStckCddMgntInfo" >
        /* StockCddMapper.insertStckCddMgntInfo */
        INSERT INTO stck_cdd_mgnt_info
            (cdd_id, cdd_dt, cdd_tm, cdd_dvsn, cdd_mgr_nm, brand_id, store_id, whs_id, location, sys_reg_dttm,
             sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.cddId}, #{entity.cddDt}, #{entity.cddTm}, #{entity.cddDvsn}, #{entity.cddMgrNm},
             #{entity.brandId}, #{entity.storeId}, #{entity.whsId}, #{entity.location},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <insert id="insertStckCddMgntDtl" >
        /* StockCddMapper.insertStckCddMgntDtl */
        INSERT INTO stck_cdd_mgnt_dtl
            (cdd_id, cdd_seq, item_id, unit, com_exst_qty, cdd_qty, chg_qty, sys_reg_dttm,
             sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.cddId}, #{entity.cddSeq}, #{entity.itemId}, #{entity.unit}, #{entity.comExstQty}, #{entity.cddQty}, #{entity.chgQty},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <select id="selectStockCddMgntInfo" resultType="com.oqpo.api.entity.stockmng.StckCddMgntInfoEntity">
        /* StockCddMapper.selectStockCddMgntInfo */
        SELECT A.cdd_id, DATE_FORMAT(A.cdd_dt, '%Y.%m.%d') AS cdd_dt, TIME_FORMAT(A.cdd_tm, '%H:%i:%s') AS cdd_tm,
            A.cdd_dvsn, IFNULL(A.cdd_mgr_nm, FN_USER_NM(A.sys_reg_id)) AS cdd_mgr_nm,   /* 실사담당자 */
            A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.whs_id, B.whs_nm, A.location, C.location_nm,
            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
        FROM stck_cdd_mgnt_info A
        INNER JOIN whs_mgnt_base_info B ON A.brand_id = B.brand_id AND A.whs_id = B.whs_id
        INNER JOIN whs_mgnt_base_loc C ON A.brand_id = C.brand_id AND A.whs_id = C.whs_id AND A.location = C.location
        WHERE A.cdd_id = #{cddId}
    </select>

    <select id="selectStockCddDtlGridList" resultType="com.oqpo.api.entity.stockmng.StckCddMgntDtlEntity">
        /* StockCddMapper.selectStockCddDtlGridList */
        SELECT A.cdd_id, A.cdd_seq, A.item_id, E.item_nm, A.unit, A.com_exst_qty, A.cdd_qty, A.chg_qty,
            B.brand_id, FN_BRAND_NM(B.brand_id) AS brand_nm,
            B.store_id, FN_STORE_NM(B.brand_id, B.store_id) AS store_nm,
            B.whs_id, C.whs_nm, B.location, D.location_nm
        FROM stck_cdd_mgnt_dtl A
        INNER JOIN stck_cdd_mgnt_info B ON A.cdd_id = B.cdd_id
        INNER JOIN whs_mgnt_base_info C ON B.brand_id = C.brand_id AND B.whs_id = C.whs_id
        INNER JOIN whs_mgnt_base_loc D ON B.brand_id = D.brand_id AND B.whs_id = D.whs_id AND B.location = D.location
        INNER JOIN item_base_info E ON A.item_id = E.item_id
        WHERE A.cdd_id = #{cddId}
        <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.cdd_seq ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.cdd_seq DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.cdd_seq ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStockCddDtlGridCnt" resultType="int">
        /* StockCddMapper.selectStockCddDtlGridCnt */
        SELECT COUNT(*)
        FROM stck_cdd_mgnt_dtl A
        INNER JOIN stck_cdd_mgnt_info B ON A.cdd_id = B.cdd_id
        INNER JOIN whs_mgnt_base_info C ON B.brand_id = C.brand_id AND B.whs_id = C.whs_id
        INNER JOIN whs_mgnt_base_loc D ON B.brand_id = D.brand_id AND B.whs_id = D.whs_id AND B.location = D.location
        INNER JOIN item_base_info E ON A.item_id = E.item_id
        WHERE A.cdd_id = #{cddId}
    </select>



</mapper>
