<?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.hzs.member.store.mapper.CuStoreMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.store.CuStore">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="STORE_CODE" property="storeCode"/>
        <result column="STORE_NAME" property="storeName"/>
        <result column="PROVINCE" property="province"/>
        <result column="CITY" property="city"/>
        <result column="COUNTY" property="county"/>
        <result column="ADDRESS" property="address"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="SHOPOWNER" property="shopowner"/>
        <result column="PK_STORE_LEVEL" property="pkStoreLevel"/>
        <result column="AMOUNT" property="amount"/>
        <result column="ACHIEVE" property="achieve"/>
        <result column="STORE_PERSON" property="storePerson"/>
        <result column="STORE_PHONE" property="storePhone"/>
        <result column="BINDING_PROVINCE" property="bindingProvince"/>
        <result column="BINDING_COUNTRY" property="bindingCountry"/>


    </resultMap>

    <resultMap id="CuStoreVO" type="com.hzs.member.store.vo.CuStoreVO">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PICTURE" property="picture"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="STORE_CODE" property="storeCode"/>
        <result column="STORE_NAME" property="storeName"/>
        <result column="PROVINCE" property="province"/>
        <result column="CITY" property="city"/>
        <result column="COUNTY" property="county"/>
        <result column="ADDRESS" property="address"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="SHOPOWNER" property="shopowner"/>
        <result column="PK_STORE_LEVEL" property="pkStoreLevel"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="PK_TRANSACTION" property="pkTransaction"/>
        <result column="AMOUNT" property="amount"/>
        <result column="ACHIEVE" property="achieve"/>
    </resultMap>

    <!--   查询店铺列表-->
    <select id="queryStoreList"
            parameterType="com.hzs.member.store.vo.CuStoreVO"
            resultMap="CuStoreVO">
        select cs.*,
        cm.MEMBER_CODE,
        sl.PK_TRANSACTION,
        sl.PICTURE,
        pr.NAME provinceVal,
        ci.NAME cityVal,
        co.NAME countyVal,
        prbd.NAME bindingProvinceName,
        bc.SHORT_NAME bindingCountryName
        from CU_STORE cs
        INNER JOIN CU_MEMBER cm on cs.PK_MEMBER=cm.PK_ID
        INNER JOIN BD_STORE_LEVEL sl on sl.PK_ID=cs.PK_STORE_LEVEL
        left JOIN BD_AREA pr on pr.PK_ID=cs.PROVINCE
        left JOIN BD_AREA ci on ci.PK_ID=cs.CITY
        left JOIN BD_AREA co on co.PK_ID=cs.COUNTY
        left JOIN BD_AREA prbd on prbd.PK_ID=cs.BINDING_PROVINCE
        left join BD_COUNTRY bc on bc.pk_id=cs.BINDING_COUNTRY
        WHERE
        cs.DEL_FLAG = 0
        AND cm.DEL_FLAG =0
        and sl.DEL_FLAG =0
        and sl.PK_COUNTRY =#{pkCountry}
        <if test="storeCode != null and storeCode !=''">
            and cs.STORE_CODE like #{storeCode} || '%'
        </if>
        <if test="storeName != null and storeName !=''">
            and cs.STORE_NAME like #{storeName} || '%'
        </if>
        <if test="shopowner != null and shopowner !=''">
            and cs.SHOPOWNER like #{shopowner} || '%'
        </if>
        <if test="memberCode != null and memberCode !=''">
            and cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="pkStoreLevel != null ">
            AND cs.PK_STORE_LEVEL = #{pkStoreLevel}
        </if>
        <if test="pkMember != null ">
            AND cs.PK_MEMBER = #{pkMember}
        </if>
        <if test="province != null ">
            AND cs.PROVINCE = #{province}
        </if>
        <if test="amount != null ">
            AND cs.AMOUNT = #{amount}
        </if>
        <if test="achieve != null ">
            AND cs.ACHIEVE = #{achieve}
        </if>
        <if test="city != null ">
            AND cs.CITY = #{city}
        </if>
        <if test="address != null and address !=''">
            and cs.ADDRESS like #{address} || '%'
        </if>
        <if test="county != null ">
            AND cs.COUNTY = #{county}
        </if>
        <if test="pkId != null ">
            AND cs.pk_Id = #{pkId}
        </if>
        <if test="startDate != null and startDate != ''">
            and cs.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and cs.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        order by cs.CREATION_TIME desc
    </select>

    <!--    校验店铺订单-->
    <select id="checkOrder"
            parameterType="com.hzs.member.store.vo.CuStoreVO"
            resultType="com.hzs.member.store.vo.CuStoreVO">
        select cs.STORE_CODE storeCode,
        cs.STORE_NAME storeName,
        so.ORDER_CODE orderCode
        from CU_STORE cs
        LEFT JOIN SA_ORDER so on cs.PK_ID=so.PK_MEMBER_STORE
        where cs.DEL_FLAG=0
        and so.DEL_FLAG=0
        and so.ORDER_TYPE=20
        and cs.PK_COUNTRY =#{pkCountry}
        <if test="pkId != null ">
            and cs.pk_id = #{pkId}
        </if>
    </select>

    <!-- 查询最新店铺编号-->
    <select id="selectMaxList" resultType="com.hzs.common.domain.member.store.CuStore">
        select *
        from (select * from CU_STORE where PK_COUNTRY = #{pkCountry} ORDER BY STORE_CODE DESC)
        WHERE ROWNUM = 1
    </select>

    <select id="queryAllMemberStore" resultType="com.hzs.common.domain.member.store.ext.CuStoreExt">
        select ct.*, bl.level_value, bd.ratio, bl.store_level_name
        from cu_store ct
                 inner join bd_store_level bl
                            on ct.pk_store_level = bl.pk_id
                 inner join bd_store_level_detail bd
                            on bd.pk_store_level = bl.pk_id
        where ct.del_flag = 0
          and bl.del_flag = 0
          and bd.del_flag = 0
    </select>
    <!-- 查询会员店铺列表 -->
    <select id="queryStoreApiList" resultType="com.hzs.common.domain.member.store.ext.CuStoreExt">
        select cs.pk_id,
        cs.STORE_CODE storeCode,
        cs.STORE_NAME storeName,
        cs.store_person,
        cs.store_phone,
        pr.NAME provinceVal,
        ci.NAME cityVal,
        cs.address,
        cm.MEMBER_CODE memberCode
        from CU_STORE cs
        inner join CU_MEMBER cm
        on cs.PK_MEMBER = cm.PK_ID
        left JOIN BD_AREA pr
        on pr.PK_ID = cs.PROVINCE
        left JOIN BD_AREA ci
        on ci.PK_ID = cs.CITY
        WHERE cs.DEL_FLAG = 0
        AND cm.DEL_FLAG = 0
        and cs.pk_country = #{param.pkCountry}
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.MEMBER_CODE like '%' || #{param.memberCode} || '%'
        </if>
        <if test="param.storeCode != null and param.storeCode != ''">
            and cs.STORE_CODE like '%' || #{param.storeCode} || '%'
        </if>
        <if test="param.province != null">
            and cs.PROVINCE = #{param.province}
        </if>
        <if test="param.city != null">
            and cs.CITY = #{param.city}
        </if>
        order by cs.province, cs.city
    </select>
    <!--    查询店铺等级值-->
    <select id="queryStoreLevelById" resultType="com.hzs.member.store.vo.CuStoreVO">
        select
        pk_id pkId,
        store_level_name storeLevelName,
        level_value as levelvalue,
        pk_transaction pkTransaction
        from BD_STORE_LEVEL bsl
        WHERE bsl.DEL_FLAG = 0
        and ENABLE_STATE=0
        and bsl.pk_country = #{param.pkCountry}
        <if test="param.pkStoreLevel != null">
            and bsl.pk_id = #{param.pkStoreLevel}
        </if>
    </select>
<!--     修改店铺信息-->
    <update id="updateStore">
        update CU_STORE
        <set>
            <if test="storeName != null and storeName != ''">STORE_NAME = #{storeName},</if>
            <if test="province != null">PROVINCE = #{province},</if>
            <if test="city != null">city = #{city},</if>
            <if test="county != null">county = #{county},</if>
            <if test="address != null and address != ''">address = #{address},</if>
            <if test="shopowner != null and shopowner != ''">shopowner = #{shopowner},</if>
            <if test="storePerson != null and storePerson != ''">STORE_PERSON = #{storePerson},</if>
            <if test="storePhone != null and storePhone != ''">STORE_PHONE = #{storePhone},</if>
            <if test="pkMember != null">PK_MEMBER = #{pkMember},</if>
            <if test="pkStoreLevel != null">PK_STORE_LEVEL = #{pkStoreLevel},</if>
            <if test="bindingProvince != null">BINDING_PROVINCE = #{bindingProvince},</if>
            <if test="bindingCountry != null">BINDING_COUNTRY = #{bindingCountry},</if>
            <if test="bindingCountry == null">BINDING_COUNTRY =null,</if>
            <if test="bindingProvince == null">BINDING_PROVINCE = null,</if>
            MODIFIED_TIME = sysdate,
            PK_MODIFIED=#{pkModified}
        </set>
        where PK_ID = #{pkId}
    </update>


</mapper>
