<?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.stinfo.StoreBaseInfoMapper">

    <select id="selectPopStoreGridList" resultType="com.oqpo.api.entity.stinfo.StoreBaseInfoEntity">
        select /* selectPopStoreGridList */
            A.brand_id, fn_brand_nm(A.brand_id) as brand_nm,
            A.store_id, A.store_nm, A.store_st_cd, fn_code_nm('STORE_ST_CD', A.store_st_cd) as store_st_nm,
            A.loan_dvsn
        from store_base_info A
        where 1=1
        and A.brand_id = #{sBrandId}
        <if test="sStoreNm != null and sStoreNm != ''">
            and A.store_nm like concat('%',#{sStoreNm},'%')
        </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.store_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by A.store_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by A.store_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectPopStoreGridCnt" resultType="int">
        select /*selectPopStoreGridCnt*/
                count(*)
        from store_base_info
        where 1=1
        and brand_id = #{sBrandId}
        <if test="sStoreNm != null and sStoreNm != ''">
            and store_nm like concat('%',#{sStoreNm},'%')
        </if>
    </select>

    <select id="selectStoreGridList" resultType="com.oqpo.api.entity.stinfo.StoreBaseInfoEntity">
        select  /* selectStoreGridList */
                brand_id, fn_brand_nm(brand_id) as brand_nm, store_id, store_nm, fn_get_regno(1,bsns_reg_no) as bsns_reg_no,
                store_dvsn, fn_code_nm('STORE_DVSN', store_dvsn) as store_dvsn_nm, store_st_cd, fn_code_nm('STORE_ST_CD',store_st_cd) as store_st_nm ,
                date_format(sys_reg_dttm,'%Y.%m.%d') as sys_reg_dttm
        from store_base_info
        where 1=1
        and  brand_id = #{sBrandId}
        <if test="searchDateType == '10'">
            and   open_dt between #{fromDt} and #{toDt}
        </if>
        <if test="searchDateType == '20'">
            and   sys_reg_dttm between date_format(concat(#{fromDt},'000000'),'%Y%m%d%H%s%i') and date_format(concat(#{toDt},'000000'),'%Y%m%d%H%s%i')
        </if>
        <if test="sStoreDvsn != null and sStoreDvsn != ''">
        and store_dvsn = #{sStoreDvsn}
        </if>
        <if test="sStoreStCd != null and sStoreStCd != ''">
        and store_st_cd = #{sStoreStCd}
        </if>
        <if test="sLoanDvsn != null and sLoanDvsn != ''">
        and loan_dvsn = #{sLoanDvsn}
        </if>
        <if test="sStoreNm != null and sStoreNm != ''">
        and store_nm like concat('%',#{sStoreNm},'%')
        </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 brand_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by brand_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by brand_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStoreGridCnt" resultType="int">
        select /*selectStoreGridCnt*/
                count(*)
        from store_base_info
        where 1=1
        and  brand_id = #{sBrandId}
        <if test="searchDateType == '10'">
            and   open_dt between #{fromDt} and #{toDt}
        </if>
        <if test="searchDateType == '20'">
            and   sys_reg_dttm between date_format(concat(#{fromDt},'000000'),'%Y%m%d%H%s%i') and date_format(concat(#{toDt},'000000'),'%Y%m%d%H%s%i')
        </if>
        <if test="sStoreDvsn != null and sStoreDvsn != ''">
            and store_dvsn = #{sStoreDvsn}
        </if>
        <if test="sStoreStCd != null and sStoreStCd != ''">
            and store_st_cd = #{sStoreStCd}
        </if>
        <if test="sLoanDvsn != null and sLoanDvsn != ''">
            and loan_dvsn = #{sLoanDvsn}
        </if>
        <if test="sStoreNm != null and sStoreNm != ''">
            and store_nm like concat('%',#{sStoreNm},'%')
        </if>
    </select>

    <select id="selectStoreInfo" resultType="com.oqpo.api.entity.stinfo.StoreBaseInfoEntity">
        select store.brand_id, brand.brand_nm as 'brandInfo.brand_nm', store.store_id,store.store_nm,store.store_dvsn,fn_code_nm('STORE_DVSN',store.store_dvsn) as store_dvsn_nm
                ,fn_get_telno(store.mbl_no) as mbl_no,date_format(store.open_dt,'%Y.%m.%d') as open_dt,date_format(store.close_dt,'%Y.%m.%d') as close_dt,store.loan_mgnt_unq_no,store.loan_dvsn
                ,fn_code_nm('LOAN_DVSN',store.loan_dvsn) as loan_dvsn_nm, store.limit_amt, store.rtn_proc_dvsn,fn_code_nm('RTN_PROC_DVSN',store.rtn_proc_dvsn) as rtn_proc_dvsn_nm,store.zip_no,store.addr1
                ,store.addr2,fn_get_telno(store.tel_no) as tel_no,fn_get_telno(store.fax_no) as fax_no,store.emal_addr,store.store_st_cd
                ,fn_code_nm('STORE_ST_CD',store.store_st_cd) as store_st_nm,fn_get_regno(1, store.bsns_reg_no) as bsns_reg_no,fn_get_regno(2, store.corp_reg_no) as corp_reg_no
                ,store.ctgr_bsns,store.bsns_cndt,store.owner_nm,store.del_yn,store.area_cd,fn_code_nm('AREA_CD',store.area_cd) as area_nm
                ,store.lat_x,store.long_y,brand.oq_brand_id,store.oq_store_id,date_format(store.sys_reg_dttm,'%Y.%m.%d') as sys_reg_dttm
                ,store.sys_reg_id,date_format(store.sys_chg_dttm,'%Y.%m.%d') as sys_chg_dttm,store.sys_chg_id
                ,case when store.loan_dvsn = 'LD01' then loan.acct_bal - loan.use_amt_total
                      when store.loan_dvsn = 'LD02' then loan.acct_bal +  loan.credt_limit_amt - loan.use_amt_total
                      else 0 end as 'loanInfo.acct_bal'
                ,store.bnk_cd , store.acct_no, store.owac_nm
                ,brand.bnk_cd as 'brandInfo.bnk_cd', fn_code_nm('BNK_CD',brand.bnk_cd) as 'brandInfo.bnk_nm',  brand.acct_no as 'brandInfo.acct_no', brand.owac_nm as 'brandInfo.owac_nm'
                ,store.whs_use_yn
        from store_base_info store
        join brand_base_info brand on store.brand_id = brand.brand_id
        left outer join loan_mgnt_base_info loan on store.loan_mgnt_unq_no = loan.loan_mgnt_unq_no and store.brand_id = loan.brand_id and store.store_id = loan.store_id and loan.use_yn = 'Y'
        where 1=1
        and store.brand_id = #{brandId}
        and store.store_id = #{storeId}
    </select>

    <insert id="insertStoreInfo" >
        insert into store_base_info
        (
            brand_id,store_id,store_nm,store_dvsn,mbl_no
            ,open_dt,close_dt,loan_mgnt_unq_no,loan_dvsn,limit_amt, rtn_proc_dvsn
            ,zip_no,addr1,addr2,tel_no,fax_no
            ,emal_addr,store_st_cd,bsns_reg_no,corp_reg_no,ctgr_bsns
            ,bsns_cndt, bnk_cd, acct_no, owac_nm, owner_nm,del_yn,area_cd,lat_x, whs_use_yn
            ,long_y,oq_brand_id,oq_store_id,sys_reg_dttm,sys_reg_id,sys_chg_dttm,sys_chg_id
        )
        values
        (
            #{entity.brandId},#{entity.storeId},#{entity.storeNm},#{entity.storeDvsn},#{entity.mblNo},
            #{entity.openDt},#{entity.closeDt},#{entity.loanMgntUnqNo},#{entity.loanDvsn},#{entity.limitAmt},#{entity.rtnProcDvsn},
            #{entity.zipNo},#{entity.addr1},#{entity.addr2},#{entity.telNo},#{entity.faxNo},
            #{entity.emalAddr},#{entity.storeStCd},#{entity.bsnsRegNo},#{entity.corpRegNo},#{entity.ctgrBsns},
            #{entity.bsnsCndt},#{entity.bnkCd}, #{entity.acctNo}, #{entity.owacNm}, #{entity.ownerNm},'N',#{entity.areaCd},#{entity.latX},#{entity.whsUseYn},
            #{entity.longY},#{entity.oqBrandId},#{entity.oqStoreId},now(),#{userId},now(),#{userId}
        )
    </insert>

    <update id="updateStoreInfo">
        update store_base_info
        set store_nm	= #{entity.storeNm}
            ,store_dvsn	= #{entity.storeDvsn}
            ,mbl_no		= #{entity.mblNo}
            ,open_dt	= #{entity.openDt}
            ,close_dt	= case when #{entity.storeStCd} = 'ST03' then date_format(now(),'%Y%m%d') else null end
            ,loan_mgnt_unq_no	= #{entity.loanMgntUnqNo}
            ,loan_dvsn	= #{entity.loanDvsn}
            ,rtn_proc_dvsn	= #{entity.rtnProcDvsn}
            ,limit_amt = #{entity.limitAmt}
            ,zip_no		= #{entity.zipNo}
            ,addr1		= #{entity.addr1}
            ,addr2		= #{entity.addr2}
            ,tel_no		= #{entity.telNo}
            ,fax_no		= #{entity.faxNo}
            ,emal_addr	= #{entity.emalAddr}
            ,store_st_cd	= #{entity.storeStCd}
            ,ctgr_bsns	= #{entity.ctgrBsns}
            ,bsns_cndt	= #{entity.bsnsCndt}
            ,bnk_cd	= #{entity.bnkCd}
            ,acct_no	= #{entity.acctNo}
            ,owner_nm	= #{entity.ownerNm}
            ,owac_nm	= #{entity.owacNm}
            ,area_cd	= #{entity.areaCd}
            ,lat_x		= #{entity.latX}
            ,long_y		= #{entity.longY}
            ,whs_use_yn = #{entity.whsUseYn}
            ,oq_brand_id = #{entity.oqBrandId}
            ,oq_store_id = #{entity.oqStoreId}
            ,sys_chg_dttm	= now()
            ,sys_chg_id	= #{userId}
        where brand_id = #{entity.brandId}
        and   store_id = #{entity.storeId}
    </update>

    <select id="selectStoreInitPw" resultType="String">
        select bsns_reg_no
         from store_base_info
         where brand_id  = #{brandId}
         and  store_id = #{storeId}
    </select>

    <select id="selectStoreLoanInfo" resultType="com.oqpo.api.entity.stinfo.StoreBaseInfoEntity">
        select loan_mgnt_unq_no,loan_dvsn,nvl(limit_amt,0) as limit_amt, nvl(whs_use_yn,'N') as whs_use_yn
        from store_base_info
        where 1=1
        and brand_id = #{brandId}
        and store_id = #{storeId}
    </select>

    <update id="updateCloseStore" >
        update  store_base_info
        set  sys_chg_dttm	= now()
            ,sys_chg_id	= #{userId}
            ,store_st_cd = 'ST03'
        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="selectStoreLoanMgntUnqNo" resultType="String">
         select loan_mgnt_unq_no
         from loan_mgnt_base_info
         where store_id = #{storeId}
         and   use_yn = 'Y'
    </select>

    <update id="updateBoStoreChgInfo">
        update store_base_info
        set sys_chg_dttm	= now()
            ,sys_chg_id	    = #{userId}
            ,store_dvsn     = #{entity.storeDvsn}
            ,store_st_cd    = #{entity.storeStCd}
            ,store_nm       = #{entity.storeNm}
            ,bsns_reg_no    = #{entity.bsnsRegNo}
            ,bsns_cndt      = #{entity.bsnsCndt}
            ,ctgr_bsns      = #{entity.ctgrBsns}
            ,owner_nm       = #{entity.ownerNm}
            ,tel_no         = #{entity.telNo}
            ,mbl_no         = #{entity.mblNo}
            ,fax_no         = #{entity.faxNo}
            ,emal_addr      = #{entity.emalAddr}
            ,zip_no         = #{entity.zipNo}
            ,addr1          = #{entity.addr1}
            ,addr2          = #{entity.addr2}
            ,area_cd        = #{entity.areaCd}
            ,open_dt        = #{entity.openDt}
            ,close_dt       = #{entity.closeDt}
            ,del_yn         = #{entity.delYn}
            ,oq_brand_id    = #{entity.oqBrandId}
            ,oq_store_id    = #{entity.oqStoreId}
        where brand_id = #{entity.brandId}
        and   store_id = #{entity.storeId}
    </update>

    <select id="selectStoreLoanDvsn" resultType="String">
         select loan_dvsn
         from store_base_info
         where store_id = #{storeId}
    </select>


    <select id="selectStoreWhsUseYn" resultType="String">
         select whs_use_yn
         from store_base_info
         where store_id = #{storeId}
    </select>

</mapper>
