<?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.ruoyi.system.mapper.PosStoreMapper">

    <resultMap type="PosStore" id="PosStoreResult">
        <result property="id"    column="id"    />
        <result property="posName"    column="pos_name"    />
        <result property="image"    column="image"    />
        <result property="posPrice"    column="pos_price"    />
        <result property="area"    column="area"    />
        <result property="address"    column="address"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="briefIntroduction"    column="brief_introduction"    />
        <result property="type"    column="type"    />
        <result property="hygienePermit"    column="hygiene_permit"    />
        <result property="hygieneLicence"    column="hygiene_licence"    />
        <result property="openBusiness"    column="open_business"    />
        <result property="windingUp"    column="winding_up"    />
        <result property="sort"    column="sort"    />
        <result property="userId"    column="user_id"    />
        <result property="zhitsj"    column="zhitsj"    />
        <result property="tgpaixv"    column="tgpaixv"    />
        <result property="state"    column="state"    />
        <result property="serverType"    column="serverType"    />
        <result property="telephone"    column="telephone"    />
        <result property="logo"    column="logo"    />
        <result property="offShelf"    column="off_shelf"    />
        <result property="isStall"    column="is_stall"    />
        <result property="isNightMarket"    column="is_night_market"    />
        <result property="nightMarketId"    column="night_market_id"    />
        <result property="invoiceExempt"    column="invoice_exempt"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPosStoreVo">
        select * from pos_store
    </sql>

    <select id="selectPosStoreList" parameterType="PosStore" resultMap="PosStoreResult">
        <include refid="selectPosStoreVo"/>
        <where>
            and del_flag = '0'
            <if test="posName != null  and posName != ''"> and BINARY pos_name like concat('%', #{posName}, '%')</if>
            <if test="posPrice != null  and posPrice != ''"> and pos_price = #{posPrice}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="offShelf != null "> and off_shelf = #{offShelf}</if>
            <if test="isStall != null "> and is_stall = #{isStall}</if>
            <if test="isNightMarket != null "> and is_night_market = #{isNightMarket}</if>
            <if test="nightMarketId != null "> and night_market_id = #{nightMarketId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
        order by id desc
    </select>

    <select id="selectPosStoreById" parameterType="Long" resultMap="PosStoreResult">
        <include refid="selectPosStoreVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertPosStore" parameterType="PosStore" useGeneratedKeys="true" keyProperty="id">
        insert into pos_store
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="posName != null and posName != ''">pos_name,</if>
            <if test="image != null and image != ''">image,</if>
            <if test="posPrice != null">pos_price,</if>
            <if test="area != null and area != ''">area,</if>
            <if test="address != null and address != ''">address,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="briefIntroduction != null">brief_introduction,</if>
            <if test="type != null">type,</if>
            <if test="hygienePermit != null and hygienePermit != ''">hygiene_permit,</if>
            <if test="hygieneLicence != null and hygieneLicence != ''">hygiene_licence,</if>
            <if test="openBusiness != null">open_business,</if>
            <if test="windingUp != null">winding_up,</if>
            <if test="sort != null">sort,</if>
            <if test="logo != null">logo,</if>
            <if test="userId != null">user_id,</if>
            <if test="isStall != null">is_stall,</if>
            <if test="isNightMarket != null">is_night_market,</if>
            <if test="nightMarketId != null">night_market_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="posName != null and posName != ''">#{posName},</if>
            <if test="image != null and image != ''">#{image},</if>
            <if test="posPrice != null">#{posPrice},</if>
            <if test="area != null and area != ''">#{area},</if>
            <if test="address != null and address != ''">#{address},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="briefIntroduction != null">#{briefIntroduction},</if>
            <if test="type != null">#{type},</if>
            <if test="hygienePermit != null and hygienePermit != ''">#{hygienePermit},</if>
            <if test="hygieneLicence != null and hygieneLicence != ''">#{hygieneLicence},</if>
            <if test="openBusiness != null">#{openBusiness},</if>
            <if test="windingUp != null">#{windingUp},</if>
            <if test="sort != null">#{sort},</if>
            <if test="logo != null">#{logo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="isStall != null">#{isStall},</if>
            <if test="isNightMarket != null">#{isNightMarket},</if>
            <if test="nightMarketId != null">#{nightMarketId},</if>
         </trim>
    </insert>

    <update id="updatePosStore" parameterType="PosStore">
        update pos_store
        <trim prefix="SET" suffixOverrides=",">
            <if test="posName != null and posName != ''">pos_name = #{posName},</if>
            <if test="image != null and image != ''">image = #{image},</if>
            <if test="posPrice != null">pos_price = #{posPrice},</if>
            <if test="area != null and area != ''">area = #{area},</if>
            <if test="address != null and address != ''">address = #{address},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="briefIntroduction != null">brief_introduction = #{briefIntroduction},</if>
            <if test="type != null">type = #{type},</if>
            <if test="hygienePermit != null and hygienePermit != ''">hygiene_permit = #{hygienePermit},</if>
            <if test="hygieneLicence != null and hygieneLicence != ''">hygiene_licence = #{hygieneLicence},</if>
            <if test="openBusiness != null">open_business = #{openBusiness},</if>
            <if test="windingUp != null">winding_up = #{windingUp},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="logo != null">logo = #{logo},</if>
            <if test="isStall != null">is_stall = #{isStall},</if>
            <if test="isNightMarket != null">is_night_market = #{isNightMarket},</if>
            <if test="nightMarketId != null">night_market_id = #{nightMarketId},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updateStoreShelf" parameterType="PosStore">
        update pos_store set off_shelf = #{offShelf} where id = #{id}
    </update>
    <update id="deletePosStoreById" parameterType="Long">
        update pos_store set del_flag = '1' where id = #{id}
    </update>

    <update id="deletePosStoreByIds" parameterType="String">
        update pos_store set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getStoreSum" resultType="int">
        SELECT IFNULL(COUNT(*),0) type FROM pos_store
        <where>
            and del_flag = '0'
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                and cretim between #{startDate} and #{endDate}
            </if>
            <if test="startDate != null and startDate != '' and (endDate == null or endDate == '')">
                and cretim >= #{startDate}
            </if>
            <if test="(startDate == null or startDate == '') and (endDate != null and endDate != '')">
                and cretim &lt;= #{endDate}
            </if>
        </where>
    </select>
    <!-- 根据商品关键词联表查询店铺（分页） -->
    <select id="selectStoresByFoodKeyword" resultMap="PosStoreResult">
        SELECT DISTINCT ps.*
        FROM pos_store ps
                 INNER JOIN pos_food pf ON ps.id = pf.mdid
        WHERE ps.del_flag = '0'
          AND ps.off_shelf = '0'
          AND pf.to_examine = '1'
          AND pf.language = #{language}
          AND  pf.name LIKE CONCAT('%', #{keyword}, '%') COLLATE utf8mb4_unicode_ci
        ORDER BY ps.id DESC
    </select>

    <!-- 夜市摊位列表（含搜索） -->
    <select id="getNightMarketStalls" resultMap="PosStoreResult">
        SELECT *
        FROM pos_store
        WHERE del_flag='0' AND off_shelf='0' AND is_stall=1 AND night_market_id=#{nightMarketId}
        <if test="keyword != null and keyword != ''">
            AND pos_name LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY id DESC
        LIMIT #{offset}, 20
    </select>

    <!-- 夜市摊位总数（含搜索） -->
    <select id="getNightMarketStallsCount" resultType="int">
        SELECT COUNT(*) FROM pos_store
        WHERE del_flag='0' AND off_shelf='0' AND is_stall=1 AND night_market_id=#{nightMarketId}
        <if test="keyword != null and keyword != ''">
            AND pos_name LIKE CONCAT('%', #{keyword}, '%')
        </if>
    </select>

    <!-- 夜市下用户关注的摊位 -->
    <select id="getFollowedStalls" resultMap="PosStoreResult">
        SELECT ps.*, (st_distance(point(ps.longitude,ps.latitude),point(#{longitude},#{latitude}))*111195/1000) as juli
        FROM pos_store ps
        INNER JOIN pos_collect pc ON ps.id = pc.md_id
        WHERE ps.del_flag='0' AND ps.off_shelf='0' AND ps.is_stall=1
          AND ps.night_market_id=#{nightMarketId}
          AND pc.user_id=#{userId}
        ORDER BY pc.cretim DESC
        LIMIT #{offset}, 20
    </select>

    <!-- 夜市下用户常购的摊位（按近30天下单次数排序） -->
    <select id="getFrequentStalls" resultMap="PosStoreResult">
        SELECT ps.*, (st_distance(point(ps.longitude,ps.latitude),point(#{longitude},#{latitude}))*111195/1000) as juli
        FROM pos_store ps
        INNER JOIN (
            SELECT md_id, COUNT(*) as order_count
            FROM pos_order
            WHERE user_id=#{userId}
              AND DATE_FORMAT(cretim,'%Y-%m-%d') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
            GROUP BY md_id
            ORDER BY order_count DESC
        ) po ON ps.id = po.md_id
        WHERE ps.del_flag='0' AND ps.off_shelf='0' AND ps.is_stall=1
          AND ps.night_market_id=#{nightMarketId}
        ORDER BY po.order_count DESC
        LIMIT #{offset}, 20
    </select>
</mapper>
