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

    <insert id="insertPgKisBaseInfo" >
        INSERT INTO pg_kis_base_info
        (brand_id, mid, merchant_key, dev_url, op_url, del_yn
        , sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
        (#{entity.brandId}, #{entity.mid}, #{entity.merchantKey}, #{entity.devUrl}, #{entity.opUrl}, #{entity.delYn}
        , NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updatePgKisBaseInfo" >
        update  pg_kis_base_info
        set  sys_chg_dttm	= now()
            ,sys_chg_id	= #{userId}
        where 1=1
        and   brand_id = #{brandId}
    </update>

    <!--
        <select id="selectTargetBrandGridList" resultType="com.oqpo.api.entity.stinfo.PgKisBaseInfoEntity">
            select brand_id, brand_nm, affl_shop_id, FN_AFFL_NM(affl_shop_id) AS affl_shop_nm
            from brand_base_info
            where st_cd = 'S000'
            <if test="authTpCd == '20' ">
            and   affl_shop_id in (select affl_shop_id from sale_mgr_mgnt where sale_mgr_id = #{userId} )
            </if>
            <if test="authTpCd == '30' ">
            and   affl_shop_id = (select affl_shop_id from np_user_info where user_id = #{userId} )
            </if>
            <if test="authTpCd == '40' or authTpCd == '50' or authTpCd == '60' ">
                and   brand_id = (select brand_id from np_user_info where user_id = #{userId})
            </if>

        </select>


        <select id="selectPopBrandGridList" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select /*selectPopBrandGridList*/
                    brand_id, brand_nm, affl_shop_id, FN_AFFL_NM(affl_shop_id) AS affl_shop_nm
            from brand_base_info
            where st_cd = 'S000'
            <if test="authTpCd == '20' ">
                and   affl_shop_id in (select affl_shop_id from sale_mgr_mgnt where sale_mgr_id = #{userId} )
            </if>
            <if test="authTpCd == '30' ">
                and   affl_shop_id = (select affl_shop_id from np_user_info where user_id = #{userId} )
            </if>
            <if test="authTpCd == '40' or authTpCd == '50' or authTpCd == '60' ">
                and   brand_id = (select brand_id from np_user_info where user_id = #{userId} )
            </if>
            <if test="afflShopId != null and afflShopId != ''">
                and   affl_shop_id = #{afflShopId}
            </if>
            <if test="brandNm != null and brandNm != ''">
                and brand_nm like concat('%',#{brandNm},'%')
            </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="selectPopBrandGridCnt" resultType="int">
            select /*selectPopBrandGridCnt*/
                    count(*)
            from brand_base_info
            where st_cd = 'S000'
            <if test="authTpCd == '20' ">
                and   affl_shop_id in (select affl_shop_id from sale_mgr_mgnt where sale_mgr_id = #{userId} )
            </if>
            <if test="authTpCd == '30' ">
                and   affl_shop_id = (select affl_shop_id from np_user_info where user_id = #{userId} )
            </if>
            <if test="authTpCd == '40' or authTpCd == '50' or authTpCd == '60' ">
                and   brand_id = (select brand_id from np_user_info where user_id = #{userId} )
            </if>
            <if test="brandNm != null and brandNm != ''">
                and brand_nm like concat('%',#{brandNm},'%')
            </if>
        </select>


        <select id="selectAfflBrandList" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select brand_id, brand_nm
            from brand_base_info
            where st_cd = 'S000'
            and affl_shop_id = #{afflShopId}
        </select>

        <select id="selectBrandGridList" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select /*selectBrandGridList*/
                    affl.affl_shop_id, affl.affl_shop_nm, brand.brand_id, brand.brand_nm
                    , fn_code_nm(upper('st_cd'), brand.st_cd) as st_nm
                    , date_format(brand.reg_dt, '%y.%m.%d') as reg_dt
                    , fn_code_nm(upper('brand_type'), brand.brand_type) as brand_type_nm
            from brand_base_info brand
                , affl_shop_base_info affl
            where 1 = 1
            and brand.affl_shop_id = affl.affl_shop_id
            <if test="sAfflShopId != null and sAfflShopId != ''">
                and affl.affl_shop_id = #{sAfflShopId}
            </if>
            and brand.del_yn = 'N'
            and affl.del_yn = 'N'
            <if test="sBrandId != null and sBrandId != ''">
                and brand.brand_id =  #{sBrandId}
            </if>
            <if test="sBrandNm != null and sBrandNm != ''">
            and brand.brand_nm like concat('%', #{sBrandNm}, '%')
            </if>
            <if test="sBrandType != null and sBrandType != ''">
            and brand.brand_type = #{sBrandType}
            </if>
            <if test="sStCd != null and sStCd != ''">
                and brand.st_cd = #{sStCd}
            </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="selectBrandGridCnt" resultType="int">
            select /*selectBrandGridCnt*/
                    count(*)
            from brand_base_info brand
            , affl_shop_base_info affl
            where 1 = 1
            and brand.affl_shop_id = affl.affl_shop_id
            <if test="sAfflShopId != null and sAfflShopId != ''">
                and affl.affl_shop_id = #{sAfflShopId}
            </if>
            and brand.del_yn = 'N'
            and affl.del_yn = 'N'
            <if test="sBrandId != null and sBrandId != ''">
                and brand.brand_id =  #{sBrandId}
            </if>
            <if test="sBrandNm != null and sBrandNm != ''">
                and brand.brand_nm like concat('%', #{sBrandNm}, '%')
            </if>
            <if test="sBrandType != null and sBrandType != ''">
                and brand.brand_type = #{sBrandType}
            </if>
            <if test="sStCd != null and sStCd != ''">
                and brand.st_cd = #{sStCd}
            </if>
        </select>

        <select id="selectBrandInfo" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select brand_id, brand_nm, st_cd, fn_code_nm(upper('st_cd'), st_cd) as st_nm, brand_type, fn_code_nm(upper('brand_type'), brand_type) as brand_type_nm, zip_no
                    , addr1, addr2, lat_x, long_y, fn_get_telno(tel_no) as tel_no, fn_get_telno(fax_no) as fax_no, date_format(reg_dt, '%y.%m.%d') as reg_dt
                    , reg_id, emal_addr, del_yn, bnk_cd, fn_code_nm(upper('bnk_cd'), bnk_cd) as bnk_cd_nm, acct_no
                    , owac_nm, affl_shop_id, fn_affl_nm(affl_shop_id) as affl_shop_nm, brand_logo_file_no, fn_get_regno(1, bsns_reg_no) as bsns_reg_no, fn_get_regno(2, corp_reg_no) as corp_reg_no
                    , oq_brand_id, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id, owner_nm
                    , pg_id, fn_code_nm('PG_ID', pg_id) as pgNm, pg_key, pg_affl_id
            from brand_base_info
            where brand_id = #{brandId}
        </select>

        <insert id="insertBrandInfo" >
            insert into brand_base_info
                ( brand_id, brand_nm, st_cd, brand_type, zip_no
                , addr1, addr2, tel_no, fax_no, reg_dt
                , reg_id, emal_addr, del_yn, bnk_cd, acct_no, owner_nm
                , owac_nm, affl_shop_id, brand_logo_file_no, bsns_reg_no, corp_reg_no
                , pg_id, pg_key, pg_affl_id , lat_x, long_y
                , oq_brand_id, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id )
            values
                ( #{entity.brandId}, #{entity.brandNm}, #{entity.stCd}, #{entity.brandType}, #{entity.zipNo}
                , #{entity.addr1}, #{entity.addr2}, #{entity.telNo}, #{entity.faxNo}, date_format(now(),'%Y%m%d')
                , #{userId}, #{entity.emalAddr}, 'N', #{entity.bnkCd}, #{entity.acctNo}, #{entity.ownerNm}
                , #{entity.owacNm}, #{entity.afflShopId}, #{entity.brandLogoFileNo}, #{entity.bsnsRegNo}, #{entity.corpRegNo}
                , #{entity.pgId} , #{entity.pgKey} , #{entity.pgAfflId} , #{entity.latX},#{entity.longY}
                , #{entity.oqBrandId}, now(), #{userId}, now(), #{userId})
        </insert>

        <update id="updateBrandInfo">
            update brand_base_info
            set brand_nm = #{entity.brandNm}
              , st_cd = #{entity.stCd}
              , brand_type = #{entity.brandType}
              , zip_no = #{entity.zipNo}
              , addr1 = #{entity.addr1}
              , addr2 = #{entity.addr2}
              , tel_no = #{entity.telNo}
              , fax_no = #{entity.faxNo}
              , emal_addr = #{entity.emalAddr}
              , bnk_cd = #{entity.bnkCd}
              , acct_no = #{entity.acctNo}
              , owac_nm = #{entity.owacNm}
              , owner_nm = #{entity.ownerNm}
              , brand_logo_file_no = #{entity.brandLogoFileNo}
              , bsns_reg_no = #{entity.bsnsRegNo}
              , corp_reg_no = #{entity.corpRegNo}
              , oq_brand_id = #{entity.oqBrandId}
              , pg_id = #{entity.pgId}
              , pg_key = #{entity.pgKey}
              , pg_affl_id = #{entity.pgAfflId}
              , lat_x = #{entity.latX}
              , long_y = #{entity.longY}
              , sys_chg_dttm = NOW()
              , sys_chg_id = #{userId}
            where brand_id = #{entity.brandId}
        </update>

        <select id="selectBrandInitPw" resultType="String">
            select bsns_reg_no
             from brand_base_info
             where brand_id  = #{brandId}
        </select>

        <select id="selectBsnsBrandList" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select info.brand_id, fn_brand_nm(info.brand_id) as brand_nm
            from (
                    select brand_id
                    from brand_base_info
                    where bsns_reg_no = #{bsnsRegNo}
                    or affl_shop_id in (select affl_shop_id
                                        from affl_shop_base_info
                                        where bsns_reg_no = #{bsnsRegNo})
                    union all
                    select brand_id
                    from store_base_info
                    where bsns_reg_no = #{bsnsRegNo}
                    union all
                    select brand_id
                    from spply_base_info
                    where bsns_reg_no = #{bsnsRegNo}
            ) info
            order by brand_nm	asc
        </select>

        <update id="updateCloseBrand" >
            update  brand_base_info
            set  sys_chg_dttm	= now()
                ,sys_chg_id	= #{userId}
                ,st_cd = 'S001'
            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="selectBrandNo" resultType="String">
            select affl_shop_id
            from brand_base_info
            where brand_id = #{brandId}
        </select>

        <select id="selectBrandNmInfo" resultType="com.oqpo.api.entity.stinfo.BrandEntity">
            select brand_id, brand_nm
            from brand_base_info
            where brand_id = #{brandId}
        </select>

        <select id="selectAfflBrandExist" resultType="int">
            select count(*)
            from  brand_base_info
            where brand_id = #{brandId}
            and   affl_shop_id = #{afflShopId}
        </select>
    -->

</mapper>
