<?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.kmall.api.dao.ApiStoreMapper">

    <resultMap type="com.kmall.api.entity.StoreVo" id="storeMap">
        <result property="id" column="id"/>
        <result property="storeName" column="store_name"/>
        <result property="storeNumber" column="store_number"/>
        <result property="storeAddress" column="store_address"/>
        <result property="provinceName" column="province_name"/>
        <result property="cityName" column="city_name"/>
        <result property="countyName" column="county_name"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="coverRadius" column="cover_radius"/>
        <result property="remark" column="remark"/>
        <result property="distance" column="distance"/>
        <result column="merch_sn" property="merchSn" jdbcType="VARCHAR" />
        <result column="merch_name" property="merchName" jdbcType="VARCHAR" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />

        <result property="thirdPartyMerchCode" column="third_party_merch_code"/>
        <result property="thirdPartyMerchName" column="third_party_merch_name"/>
        <result property="isValid" column="is_valid"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.api.entity.StoreVo">
		select
			`id`,
			`store_name`,
			`store_number`,
			`store_address`,
			`province_name`,
			`city_name`,
			`county_name`,
			`latitude`,
			`longitude`,
			`cover_radius`,
			`remark`,
			merch_sn,
			merch_name,
			third_party_merch_code,
			third_party_merch_name,
			moder_sn
		from mall_store
		where id = #{id} and is_valid = 0
	</select>

    <select id="queryList" resultType="com.kmall.api.entity.StoreVo">
        select
        `id`,
        `store_name`,
        `store_number`,
        `store_address`,
        `province_name`,
        `city_name`,
        `county_name`,
        `latitude`,
        `longitude`,
        `cover_radius`,
        `remark`,
        merch_sn,
        merch_name,
        third_party_merch_code,
        third_party_merch_name
        from mall_store
        WHERE 1=1  and is_valid = 0
        <if test="province_name != null and province_name.trim() != ''">
            AND province_name = #{province_name}
        </if>
        <if test="city_name != null and city_name.trim() != ''">
            AND city_name = #{city_name}
        </if>
        <if test="county_name != null and county_name.trim() != ''">
            AND county_name = #{county_name}
        </if>
        <if test="latitude != null and longitude!= null">
            AND cover_radius*1000 > round(6378.138*2*asin(sqrt(pow(sin( (a.latitude*pi()/180-#{latitude}*pi()/180)/2),2)
            +cos(a.latitude*pi()/180)*cos(#{latitude}*pi()/180)* pow(sin(
            (a.longitude*pi()/180-#{longitude}*pi()/180)/2),2)))*1000)
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_store
        WHERE 1=1  and is_valid = 0
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>


    <!-- 在配送范围之内查找店铺 -->
    <!--<select id="queryNearbyList" resultMap="storeMap">
        select * from
        ( select
        `id`,
        `store_name`,
        `store_number`,
        `store_address`,
        `province_name`,
        `city_name`,
        `county_name`,
        `latitude`,
        `longitude`,
        `cover_radius`,
        `remark`,
        round(6378.138*2*asin(sqrt(pow(sin((a.latitude*pi()/180-#{latitude}*pi()/180)/2),2)
          +cos(a.latitude*pi()/180)*cos(#{latitude}*pi()/180)
          *pow(sin((a.longitude*pi()/180-#{longitude}*pi()/180)/2),2)))*1000) as distance,
        merch_sn,
        merch_name,
        creater_sn,
        create_time,
        moder_sn,
        mod_time,
        tstm
        from mall_store a
        WHERE 1=1
        <if test="province_name != null and province_name.trim() != ''">
            AND province_name = #{province_name}
        </if>
        <if test="city_name != null and city_name.trim() != ''">
            AND city_name = #{city_name}
        </if>
        <if test="county_name != null and county_name.trim() != ''">
            AND county_name = #{county_name}
        </if>
        ) tmp
        where cover_radius * 1000 > distance
        order by distance asc
    </select>-->

    <!--删除距离限制-->
    <select id="queryNearbyList" resultMap="storeMap">
        select * from
        ( select
        `id`,
        `store_name`,
        `store_number`,
        `store_address`,
        `province_name`,
        `city_name`,
        `county_name`,
        `latitude`,
        `longitude`,
        `cover_radius`,
        `remark`,
        round(6378.138*2*asin(sqrt(pow(sin( (a.latitude*pi()/180-#{latitude}*pi()/180)/2),2)
            +cos(a.latitude*pi()/180)*cos(#{latitude}*pi()/180)
            *pow(sin((a.longitude*pi()/180-#{longitude}*pi()/180)/2),2)))*1000) as distance,
        merch_sn,
        merch_name,
        third_party_merch_code,
        third_party_merch_name
        from mall_store a
        WHERE 1=1  and is_valid = 0
        <if test="province_name != null and province_name.trim() != ''">
            AND province_name = #{province_name}
        </if>
        <if test="city_name != null and city_name.trim() != ''">
            AND city_name = #{city_name}
        </if>
        <if test="county_name != null and county_name.trim() != ''">
            AND county_name = #{county_name}
        </if>
        <if test="storeId != null and storeId != '' ">
            AND a.id = #{storeId}
        </if>
        <if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != '' ">
            AND a.third_party_merch_code = #{thirdPartyMerchCode}
        </if>
        ) tmp
        order by distance asc
        limit 0, 10
    </select>

</mapper>