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

    <select id="selectWhsMngGridList" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity">
        /* WhsMngMapper.selectWhsMngGridList */
        SELECT A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.whs_id, A.whs_nm, A.whs_dvsn, FN_CODE_NM('WHS_DVSN', A.whs_dvsn) AS whs_dvsn_nm,
            A.mgr_nm, FN_GET_TELNO(A.mgr_tel_no) AS mgr_tel_no, A.whs_st_cd, FN_CODE_NM('WHS_ST_CD', A.whs_st_cd) AS whs_st_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.base_yn
        FROM whs_mgnt_base_info A
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
        AND A.store_id = #{sStoreId}
        </if>
        <if test="sWhsDvsn != null and sWhsDvsn != ''">
            AND A.whs_dvsn = #{sWhsDvsn}
        </if>
        <if test="sWhsStCd != null and sWhsStCd != ''">
            AND A.whs_st_cd = #{sWhsStCd}
        </if>
        <if test="sWhsNm != null and sWhsNm != ''">
            AND A.whs_nm LIKE CONCAT('%',#{sWhsNm},'%')
        </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.whs_id ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.whs_id DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.whs_id ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectWhsMngGridCnt" resultType="int">
        /* WhsMngMapper.selectWhsMngGridCnt */
        SELECT COUNT(*)
        FROM whs_mgnt_base_info A
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sWhsDvsn != null and sWhsDvsn != ''">
            AND A.whs_dvsn = #{sWhsDvsn}
        </if>
        <if test="sWhsStCd != null and sWhsStCd != ''">
            AND A.whs_st_cd = #{sWhsStCd}
        </if>
        <if test="sWhsNm != null and sWhsNm != ''">
            AND A.whs_nm LIKE CONCAT('%',#{sWhsNm},'%')
        </if>
    </select>

    <select id="selectWhsMgntBaseInfo" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity">
        /* WhsMngMapper.selectWhsMgntBaseInfo */
        SELECT A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.whs_id, A.whs_nm, A.whs_dvsn, FN_CODE_NM('WHS_DVSN', A.whs_dvsn) AS whs_dvsn_nm,
            A.mgr_nm, FN_GET_TELNO(A.mgr_tel_no) AS mgr_tel_no, A.zip_no,
            A.addr1, A.addr2, A.whs_st_cd, FN_CODE_NM('WHS_ST_CD', A.whs_st_cd) AS whs_st_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.base_yn,
            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 whs_mgnt_base_info A
        WHERE A.brand_id = #{brandId}
        <if test="whsId != null and whsId != ''">
        AND A.whs_id = #{whsId}
        </if>
        <if test="storeId != null and storeId != ''">
        AND A.store_id = #{storeId}
        </if>
    </select>

    <select id="selectWhsLocGridList" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseLocEntity">
        /* WhsMngMapper.selectWhsLocGridList */
        SELECT A.brand_id, A.whs_id, A.location, A.location_nm,
            A.stck_dvsn, FN_CODE_NM('STCK_DVSN', A.stck_dvsn) AS stck_dvsn_nm,
            A.loc_st_cd, FN_CODE_NM('LOC_ST_CD', A.loc_st_cd) AS loc_st_nm,
            A.loc_base_yn
        FROM whs_mgnt_base_loc A
        WHERE A.brand_id = #{brandId}
        AND A.whs_id = #{whsId}
        ORDER BY A.location ASC
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectWhsLocGridCnt" resultType="int">
        /* WhsMngMapper.selectWhsLocGridCnt */
        SELECT COUNT(*)
        FROM whs_mgnt_base_loc A
        WHERE A.brand_id = #{brandId}
        AND A.whs_id = #{whsId}
    </select>

    <insert id="insertWhsMgntBaseInfo" >
        /* WhsMngMapper.insertWhsMgntBaseInfo */
        INSERT INTO whs_mgnt_base_info
            (brand_id, whs_id, whs_nm, whs_dvsn, mgr_nm, mgr_tel_no, zip_no, addr1, addr2, whs_st_cd, store_id,
             base_yn, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.brandId}, #{entity.whsId}, #{entity.whsNm}, #{entity.whsDvsn}, #{entity.mgrNm}, #{entity.mgrTelNo},
             #{entity.zipNo}, #{entity.addr1}, #{entity.addr2}, #{entity.whsStCd}, #{entity.storeId},
             #{entity.baseYn}, NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updateWhsMgntBaseInfo" >
        /* WhsMngMapper.updateWhsMgntBaseInfo */
        UPDATE whs_mgnt_base_info SET
            whs_nm = #{entity.whsNm},
            whs_dvsn = #{entity.whsDvsn},
            mgr_nm = #{entity.mgrNm},
            mgr_tel_no = #{entity.mgrTelNo},
            zip_no = #{entity.zipNo},
            addr1 = #{entity.addr1},
            addr2 = #{entity.addr2},
            whs_st_cd = #{entity.whsStCd},
            store_id = #{entity.storeId},
            base_yn = #{entity.baseYn},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
    </update>

    <delete id="deleteWhsMgntBaseInfo" >
        /* WhsMngMapper.deleteWhsMgntBaseInfo */
        DELETE FROM whs_mgnt_base_info
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
    </delete>

    <delete id="deleteWhsMgntBaseLoc4Info" >
        /* WhsMngMapper.deleteWhsMgntBaseLoc4Info */
        DELETE FROM whs_mgnt_base_loc
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
    </delete>

    <select id="selectNextLocation4Whs" resultType="java.lang.String">
        /* WhsMngMapper.selectNextLocation4Whs */
        SELECT IFNULL(MAX(A.LOCATION), 1000) + 1
        FROM whs_mgnt_base_loc A
        WHERE A.brand_id = #{brandId}
        AND A.whs_id = #{whsId}
    </select>

    <insert id="insertWhsMgntBaseLoc" >
        /* WhsMngMapper.insertWhsMgntBaseLoc */
        INSERT INTO whs_mgnt_base_loc
            (brand_id, whs_id, location, location_nm, stck_dvsn, loc_st_cd, loc_base_yn,
             sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.brandId}, #{entity.whsId}, #{entity.location}, #{entity.locationNm}, #{entity.stckDvsn}, #{entity.locStCd}, #{entity.locBaseYn},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updateWhsMgntBaseLoc" >
        /* WhsMngMapper.updateWhsMgntBaseLoc */
        UPDATE whs_mgnt_base_loc SET
            location_nm = #{entity.locationNm},
            stck_dvsn = #{entity.stckDvsn},
            loc_st_cd = #{entity.locStCd},
            loc_base_yn = #{entity.locBaseYn},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{entity.sysChgId}
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
        AND location = #{entity.location}
    </update>

    <delete id="deleteWhsMgntBaseLoc" >
        /* WhsMngMapper.deleteWhsMgntBaseLoc */
        DELETE FROM whs_mgnt_base_loc
        WHERE brand_id = #{entity.brandId}
        AND whs_id = #{entity.whsId}
        AND location = #{entity.location}
    </delete>

    <update id="updateStoreWhsStatus" >
        /* WhsMngMapper.updateStoreWhsStatus */
        UPDATE whs_mgnt_base_info SET
        <if test="whsStCd != null and whsStCd != ''">
            whs_st_cd = #{whsStCd},
        </if>
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE 1=1
        <if test="brandId != null and brandId != ''">
        and brand_id = #{brandId}
        </if>
        <if test="storeId != null and storeId != ''">
        AND store_id = #{storeId}
        </if>
    </update>

    <update id="updateWhsLocStatus" >
        /* WhsMngMapper.updateWhsLocStatus */
        UPDATE whs_mgnt_base_loc SET
        <if test="locStCd != null and locStCd != ''">
            loc_st_cd = #{locStCd},
        </if>
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE  concat(brand_id , whs_id) in (
                                    select concat(brand_id, whs_id)
                                     from whs_mgnt_base_info
                                     WHERE 1=1
                                    <if test="brandId != null and brandId != ''">
                                     and  brand_id = #{brandId}
                                    </if>
                                    <if test="storeId != null and storeId != ''">
                                     AND store_id = #{storeId}
                                    </if>
                                     )
    </update>

    <update id="updateCloseWhs" >
        update whs_mgnt_base_info
         set  sys_chg_dttm	= now()
             ,sys_chg_id	= #{userId}
             ,whs_st_cd = 'W003'
        where 1=1
        <if test="brandId != null and brandId != ''">
            and   brand_id = #{brandId}
        </if>
        <if test="afflShopId != null and afflShopId != ''">
            and   brand_id in (select brand_id from affl_shop_base_info where affl_shop_id = #{afflShopId})
        </if>
    </update>

    <update id="updateCloseWhsLoc" >
        UPDATE whs_mgnt_base_loc
        SET  sys_chg_dttm	= now()
            ,sys_chg_id	= #{userId}
            , loc_st_cd = 'LOC003'
        WHERE 1=1
        and   concat(brand_id , whs_id) in (
                                            select concat(brand_id, whs_id)
                                            from whs_mgnt_base_info
                                            WHERE 1=1
                                            <if test="brandId != null and brandId != ''">
                                                and  brand_id = #{brandId}
                                            </if>
                                            <if test="afflShopId != null and afflShopId != ''">
                                                and   brand_id in (select brand_id from affl_shop_base_info where affl_shop_id = #{afflShopId})
                                            </if>
                                            )
    </update>

    <select id="selectPopWhsLocList" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity">
        select whsInfo.brand_id, fn_brand_nm(whsInfo.brand_id) as brand_nm, whsInfo.whs_id, whsInfo.whs_nm, locInfo.location as 'locationInfo.location', locInfo.location_nm as 'locationInfo.location_nm',
                locInfo.stck_dvsn as 'locationInfo.stck_dvsn', fn_code_nm('STCK_DVSN', locInfo.stck_dvsn) as 'locationInfo.stck_dvsn_nm'
        from  whs_mgnt_base_info whsInfo
        left outer join whs_mgnt_base_loc locInfo on whsInfo.brand_id = locInfo.brand_id and whsInfo.whs_id = locInfo.whs_id and locInfo.loc_st_cd = 'LOC001'
        where 1=1 -- whsInfo.whs_st_cd = 'W001'
        and   whsInfo.brand_id = #{sBrandId}
        <if test="sWhsDvsn != null and sWhsDvsn != ''">
        and   whsInfo.whs_dvsn = #{sWhsDvsn}
        </if>
        <if test="sStoreId != null and sStoreId != ''">
            and   whsInfo.store_id = #{sStoreId}
        </if>
        <if test="sWhsNm != null and sWhsNm != ''">
        and   whsInfo.whs_nm like concat('%',#{sWhsNm},'%')
        </if>
    </select>

    <select id="selectStoreWhsLocList" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity">
        select whs.whs_id, whs.whs_nm, loc.location as 'locationInfo.location', loc.location_nm as 'locationInfo.location_nm'
        from whs_mgnt_base_info whs
        left outer join whs_mgnt_base_loc loc on whs.brand_id = loc.brand_id and whs.whs_id = loc.whs_id and loc.loc_st_cd = 'LOC001'
        where 1=1
        and whs.brand_id = #{brandId}
        and whs.store_id = #{storeId}
        and whs.whs_st_cd = 'W001'
    </select>

    <select id="selectBaseWhsId" resultType="String">
        /* WhsMngMapper.selectBaseWhsId */
        SELECT NVL(whs_id,'N') as whs_id
        FROM whs_mgnt_base_info A
        WHERE A.brand_id = #{brandId}
        AND A.base_yn = 'Y'
        AND (A.store_id IS NULL OR A.store_id = '')
        limit 1
    </select>

    <select id="selectBaseWhsLocationId" resultType="String">
        /* WhsMngMapper.selectBaseWhsLocationId */
        SELECT nvl(location,'N') as location
        FROM whs_mgnt_base_loc A
        WHERE A.brand_id = #{brandId}
        and   A.whs_id = #{whsId}
        AND   A.loc_base_yn = 'Y'
    </select>

    <update id="updateClearBaseWhs">
        UPDATE whs_mgnt_base_info SET
            base_yn = 'N'
        WHERE brand_id = #{brandId}
        AND (store_id IS NULL OR store_id = '')
    </update>

    <update id="updateClearBaseWhsLoc">
        UPDATE whs_mgnt_base_loc SET
            loc_base_yn = 'N'
        WHERE brand_id = #{brandId}
        AND whs_id = #{whsId}
    </update>

    <select id="selectBaseWhsLocationCnt" resultType="int">
        select count(*)
        from   whs_mgnt_base_info info
        , whs_mgnt_base_loc   loc
        where  info.whs_id = loc.whs_id
        and    info.brand_id = #{brandId}
        and    (info.store_id is null or info.store_id = '')
        and    info.whs_st_cd = 'W001'
        and    info.base_yn = 'Y'
        and    loc.loc_st_cd = 'LOC001'
        and    loc.LOC_BASE_YN = 'Y'
    </select>

    <select id="selectBaseWhsLocationInfo" resultType="com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity">
        select info.whs_id, loc.location as 'locationInfo.location'
              ,info.mgr_tel_no
        from   whs_mgnt_base_info info
             , whs_mgnt_base_loc   loc
        where  info.whs_id = loc.whs_id
        and    info.brand_id = #{brandId}
        and    (info.store_id is null or info.store_id = '')
        and    info.whs_st_cd = 'W001'
        and    info.base_yn = 'Y'
        and    loc.loc_st_cd = 'LOC001'
        and    loc.LOC_BASE_YN = 'Y'
        limit 1
    </select>

</mapper>
