<?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.yami.shop.dao.ShopDetailMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.ShopDetail">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="out_station_no" jdbcType="VARCHAR" property="outStationNo"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="intro" jdbcType="VARCHAR" property="intro"/>
        <result column="shop_owner" jdbcType="VARCHAR" property="shopOwner"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="tel" jdbcType="VARCHAR" property="tel"/>
        <result column="shop_lat" jdbcType="VARCHAR" property="shopLat"/>
        <result column="shop_lng" jdbcType="VARCHAR" property="shopLng"/>
        <result column="shop_address" jdbcType="VARCHAR" property="shopAddress"/>
        <result column="province" jdbcType="VARCHAR" property="province"/>
        <result column="province_id" jdbcType="BIGINT" property="provinceId"/>
        <result column="city" jdbcType="VARCHAR" property="city"/>
        <result column="city_id" jdbcType="BIGINT" property="cityId"/>
        <result column="area" jdbcType="VARCHAR" property="area"/>
        <result column="area_id" jdbcType="BIGINT" property="areaId"/>
        <result column="shop_logo" jdbcType="VARCHAR" property="shopLogo"/>
        <result column="shop_status" jdbcType="TINYINT" property="shopStatus"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="is_distribution" jdbcType="TINYINT" property="isDistribution"/>
        <result column="business_license" jdbcType="VARCHAR" property="businessLicense"/>
        <result column="identity_card_front" jdbcType="VARCHAR" property="identityCardFront"/>
        <result column="identity_card_later" jdbcType="VARCHAR" property="identityCardLater"/>
        <result column="shop_code" jdbcType="VARCHAR" property="shopCode"/>
        <result column="map_type" jdbcType="VARCHAR" property="mapType"/>
        <result column="category" jdbcType="VARCHAR" property="category"/>
        <result column="sync" jdbcType="TINYINT" property="sync"/>

    </resultMap>
    <select id="getShopByMobile" resultMap="BaseResultMap">
        select *
        from tz_shop_detail
        where mobile = #{mobile}
    </select>
    <update id="updatePasswordByUserName">
        update tz_shop_detail
        set `password` = #{newPassword}
        where mobile = #{username}

    </update>

    <select id="getShopDetailByUserId" resultMap="BaseResultMap">
        select *
        from tz_shop_detail
        where user_id = #{userId}
    </select>

    <update id="updateStatus">
        UPDATE tz_shop_detail sd
        SET sd.`update_time` = NOW(),
            sd.`shop_status` = #{status}
        WHERE sd.`shop_id` = #{shopId}
    </update>

    <update id="hbUpdateStatus">
        UPDATE tz_shop_detail
        SET update_time = NOW(),
            shop_status = #{shopStatus},
            run_status = #{runStatus}
        WHERE shop_id = #{shopId}
    </update>
    <select id="listHotShopsHead" resultType="com.yami.shop.bean.app.dto.ShopHeadInfoDto">
        SELECT sd.`shop_id`,
               sd.`out_station_no`,
               sd.`shop_name`,
               sd.`shop_logo`,
               sd.shop_status,
               COUNT(ucs.`shop_id`) fansCount,
               sd.`tel`
        FROM tz_shop_detail sd
                 LEFT JOIN `tz_user_collection_shop` ucs
                           ON ucs.`shop_id` = sd.`shop_id`
        GROUP BY sd.`shop_id`
        HAVING sd.shop_status = 1
        ORDER BY fansCount DESC LIMIT 8
    </select>
    <select id="searchShops" resultType="com.yami.shop.bean.app.dto.ShopHeadInfoDto">
        SELECT
        sd.`shop_id`,
        sd.`out_station_no`,
        sd.`shop_name`,
        sd.`shop_logo`,
        sd.shop_status,
        COUNT(ucs.`shop_id`) fansCount,
        sd.`tel`
        FROM
        tz_shop_detail sd
        LEFT JOIN `tz_user_collection_shop` ucs
        ON ucs.`shop_id` = sd.`shop_id`
        GROUP BY sd.`shop_id`
        HAVING sd.shop_status = 1
        <if test="shopHeadInfoDto.shopName != null and shopHeadInfoDto.shopName != ''">
            and sd.`shop_name` like concat('%', #{shopHeadInfoDto.shopName}, '%')
        </if>
        ORDER BY fansCount DESC
    </select>
    <select id="selectShopDetailById" resultType="com.yami.shop.bean.model.ShopDetail">
        select *
        from tz_shop_detail
        where shop_id = #{shopId}
    </select>

    <select id="neighborShop" resultType="com.yami.shop.bean.model.ShopDetail">
        select DISTINCT
        sd.shop_id,
        shop_name,
        sd.shop_lat ,
        sd.shop_lng,
        ( 6371 * acos( cos( radians(#{lat}) ) * cos( radians( shop_lat ) ) * cos( radians( shop_lng ) - radians(#{lon})
        ) + sin( radians(#{lat}) ) * sin( radians( shop_lat ) ) ) ) distance
        from
        tz_shop_detail sd
        right join tz_transport2 tp on tp.shop_id = sd.shop_id
        right join tz_channel_shop tcs on tcs.shop_id = sd.shop_id
        where shop_status = 1
        <if test="null != platform">
         and   tcs.`channel_id`= #{platform} and tcs.`is_delete`=0
        </if>
        order by
        distance
    </select>


    <select id="selectByOutStationNo" resultType="com.yami.shop.bean.model.ShopDetail">
        select shop_id,
               out_station_no,
               shop_name,
               user_id,
               intro,
               shop_owner,
               mobile,
               tel,
               shop_lat,
               shop_lng,
               shop_address,
               province,
               province_id,
               city,
               city_id,
               area,
               area_id,
               shop_logo,
               shop_status,
               create_time,
               update_time,
               is_distribution,
               business_license,
               identity_card_front,
               identity_card_later,
               sync,
               category,
               third_party_code
        from tz_shop_detail
        where out_station_no = #{outStationNo}
    </select>

    <select id="selectByHbStationId" resultType="com.yami.shop.bean.model.ShopDetail">
        select shop_id,
               hb_station_id,
               out_station_no,
               shop_name,
               user_id,
               intro,
               shop_owner,
               mobile,
               tel,
               shop_lat,
               shop_lng,
               shop_address,
               province,
               province_id,
               city,
               city_id,
               area,
               area_id,
               shop_logo,
               shop_status,
               create_time,
               update_time,
               is_distribution,
               business_license,
               identity_card_front,
               identity_card_later,
               sync,
               category,
               third_party_code
        from tz_shop_detail
        where hb_station_id = #{hbStationId}
    </select>

</mapper>
