<?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="shop.alien.mapper.second.SecondGoodsMapper">
    <resultMap id="BaseResultMap" type="shop.alien.entity.second.SecondGoods">
        <!--@mbggenerated-->
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="user_id" property="userId" jdbcType="INTEGER"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="describe" property="describe" jdbcType="VARCHAR"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="position" property="position" jdbcType="VARCHAR"/>
        <result column="like_count" property="likeCount" jdbcType="INTEGER"/>
        <result column="collect_count" property="collectCount" jdbcType="INTEGER"/>
        <result column="category_one_id" property="categoryOneId" jdbcType="INTEGER"/>
        <result column="category_two_id" property="categoryTwoId" jdbcType="INTEGER"/>
        <result column="label" property="label" jdbcType="VARCHAR"/>
        <result column="topic" property="topic" jdbcType="VARCHAR"/>
        <result column="trade_id" property="tradeId" jdbcType="INTEGER"/>
        <result column="goods_status" property="goodsStatus" jdbcType="INTEGER"/>
        <result column="failed_reason" property="failedReason" jdbcType="VARCHAR"/>
        <result column="delete_flag" property="deleteFlag" jdbcType="INTEGER"/>
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
        <result column="created_user_id" property="createdUserId" jdbcType="INTEGER"/>
        <result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP"/>
        <result column="updated_user_id" property="updatedUserId" jdbcType="INTEGER"/>
        <result column="release_time" property="releaseTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 自定义分页查询 -->
    <select id="getSecondGoodsByPage" resultType="shop.alien.entity.second.SecondGoods">
        SELECT *
        FROM second_goods
        WHERE delete_flag = 0
    </select>

    <!-- 自定义分页查询并按距离排序 Haversine公式 d=rarccos(cos(ϕ1)cos(ϕ2)cos(Δλ)+sin(ϕ1)sin(ϕ2)) -->
    <select id="getSecondGoodsByPageAndDistance" resultType="shop.alien.entity.second.SecondGoods">
        SELECT *,
        (6371 * acos(cos(radians(#{currentLatitude})) * cos(radians(SUBSTRING_INDEX(position, ',', 1))) * cos(radians(SUBSTRING_INDEX(position, ',', -1)) - radians(#{currentLongitude})) + sin(radians(#{currentLatitude})) * sin(radians(SUBSTRING_INDEX(position, ',', 1))))) AS distance
        FROM second_goods
        WHERE delete_flag = 0
        ORDER BY distance ASC
    </select>

    <!-- 查询商品热卖排行榜 -->
    <select id="getHotSellingRanking" resultType="shop.alien.entity.second.SecondGoods">
        SELECT *
        FROM second_goods
        WHERE delete_flag = 0
        ORDER BY like_count DESC
    </select>

    <!-- 查询商品热卖排行榜（前10名），按标签聚合计算总点赞量 -->
    <select id="getHotSellingRankingTop10" resultType="shop.alien.entity.second.SecondGoods">
        SELECT label, SUM(like_count) AS like_count
        FROM second_goods
        WHERE delete_flag = 0 and label IS NOT NULL and goods_status = 3
        GROUP BY label
        ORDER BY like_count DESC
        LIMIT 10
    </select>

    <!-- 获取商品收藏排行榜（前10名），按标签聚合计算总收藏量 -->
    <select id="getCollectTop10" resultType="shop.alien.entity.second.SecondGoods">
        SELECT label, SUM(collect_count) AS collect_count
        FROM second_goods
        WHERE delete_flag = 0 and label IS NOT NULL and goods_status = 3
        GROUP BY label
        ORDER BY collect_count DESC
            LIMIT 10
    </select>

    <!-- 查询用户屏蔽的商品列表 -->
    <select id="getShieldedGoodsList" resultType="shop.alien.entity.second.SecondGoods">
        SELECT sg.*
        FROM second_goods sg
        INNER JOIN second_shield ss ON
            (sg.id = ss.shield_id AND ss.shield_type = 1)
        WHERE ss.user_id = #{userId} AND ss.delete_flag = 0 AND sg.delete_flag = 0
    </select>

    <!-- 搜索商品列表（包含商品信息、图片、用户信息），按距离和创建时间倒序 -->
    <!--<select id="searchGoodsList" resultType="shop.alien.entity.second.SecondGoods">
        SELECT 
            sg.*, 
            (6371 * acos(cos(radians(#{searchVo.currentLatitude})) * cos(radians(SUBSTRING_INDEX(sg.position, ',', 1))) * cos(radians(SUBSTRING_INDEX(sg.position, ',', -1)) - radians(#{searchVo.currentLongitude})) + sin(radians(#{searchVo.currentLatitude})) * sin(radians(SUBSTRING_INDEX(sg.position, ',', 1))))) AS distance
        FROM second_goods sg
        WHERE sg.delete_flag = 0
        <if test="searchVo.shieldedGoodsIds != null and !searchVo.shieldedGoodsIds.isEmpty()">
            AND sg.id NOT IN
            <foreach collection="searchVo.shieldedGoodsIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="searchVo.userIdList != null and !searchVo.userIdList.isEmpty()">
            AND sg.user_id NOT IN
            <foreach collection="searchVo.userIdList" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="searchVo.searchData != null and searchVo.searchData != ''">
            AND (
            sg.label LIKE CONCAT('%', #{searchVo.searchData}, '%')
            OR sg.title LIKE CONCAT('%', #{searchVo.searchData}, '%')
            OR sg.describe LIKE CONCAT('%', #{searchVo.searchData}, '%')
            OR sg.category_one_id LIKE CONCAT('%', #{searchVo.searchData}, '%')
            OR sg.category_two_id LIKE CONCAT('%', #{searchVo.searchData}, '%')
            )
        </if>
        ORDER BY distance ASC,
        <if test="searchVo.orderData != null and searchVo.orderType != null">
            CASE
                WHEN #{searchVo.orderData} = 'distance' THEN distance
                WHEN #{searchVo.orderData} = 'price' THEN sg.price
                WHEN #{searchVo.orderData} = 'likeCount' THEN sg.like_count
                WHEN #{searchVo.orderData} = 'collectCount' THEN sg.collect_count
                ELSE sg.created_time
            END
            <if test="searchVo.orderType == 1">ASC</if>
            <if test="searchVo.orderType == 2">DESC</if>
        </if>
    </select>-->
</mapper>