<?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="org.springblade.modules.groupon.mapper.SelfTakeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="selfTakeResultMap" type="org.springblade.modules.groupon.entity.SelfTake">
        <result column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
        <result column="name" property="name"/>
        <result column="address" property="address"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="bus_start_time" property="busStartTime"/>
        <result column="bus_end_time" property="busEndTime"/>
        <result column="source" property="source"/>
        <result column="status" property="status"/>
    </resultMap>


    <select id="selectSelfTakePage" resultMap="selfTakeResultMap">
        select * from t_self_take where is_delete = 0
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(selfTake.name)">
            and instr(`name`, #{selfTake.name}) > 0
        </if>
    </select>

    <select id="getByGroupIds" resultType="org.springblade.modules.groupon.vo.SelfTakeVO">
        SELECT st.id, gst.groupon_id grouponId, st.address, st.`name`, st.latitude, st.longitude FROM t_groupon_self_take gst JOIN t_self_take st on gst.self_take_id = st.id
        where gst.groupon_id in
        <foreach item="item" index="index" collection="ids"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectGrouponAllSelfTake" resultType="org.springblade.modules.groupon.vo.SelfTakeVO">
        SELECT
            st.*,
            (
                6371 * ACOS(
                    COS(
                        RADIANS( #{latitude} )) * COS(
                        RADIANS( st.latitude )) * COS(
                        RADIANS( st.longitude ) - RADIANS( #{longitude} )) + SIN(
                        RADIANS( #{latitude} )) * SIN(
                    RADIANS( st.latitude ))
                )
            ) AS distance
        FROM
            t_groupon_self_take gst
            JOIN t_self_take st ON gst.self_take_id = st.id
        WHERE
            st.address LIKE concat(#{address}, '%') and gst.groupon_id = #{grouponId}
        ORDER BY
            distance
    </select>

</mapper>
