<?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.SecondRecommendMapper">
    <resultMap id="BaseResultMap" type="shop.alien.entity.second.vo.SecondGoodsRecommendVo">
        <!--@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="description" property="description" jdbcType="VARCHAR"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="amount" property="amount" 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="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="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="user_image" property="userImage" jdbcType="VARCHAR"/>
        <result column="goods_status" property="goodsStatus" jdbcType="INTEGER"/>
        <result column="delete_flag" property="deleteFlag" jdbcType="INTEGER"/>
        <result column="dist" property="dist" jdbcType="VARCHAR"/>
        <result column="isLike" property="isLike" jdbcType="VARCHAR"/>
        <result column="home_image" property="homeImage" jdbcType="VARCHAR"/>
        <result column="user_phone" property="userPhone" jdbcType="VARCHAR"/>
        <result column="release_time" property="releaseTime" jdbcType="VARCHAR"/>
        <result column="video_first_frame" property="videoFirstFrame" jdbcType="VARCHAR"/>

    </resultMap>

    <!-- 分页查询推荐数据 -->
    <select id="getSecondRecommendByPage" resultType="shop.alien.entity.second.vo.SecondGoodsRecommendVo">
        with shieldUser as (
            select g.id from life_blacklist lb inner join second_goods g
                on lb.blocked_id = g.user_id  and g.delete_flag = '0' and g.goods_status = '3'
            where lb.delete_flag = 0 and lb.blocker_type = 2 and lb.blocked_type = 2
                and lb.blocker_id = #{userId}
        )
        SELECT * FROM (
            SELECT
                g.id,
                g.user_id,
                g.title,
                g.description,
                g.price,
                g.price as amount,
                g.position,
                g.like_count,
                g.collect_count,
                g.category_one_id,
                g.category_two_id,
                g.label,
                g.topic,
                g.trade_id,
                g.created_time,
                g.created_user_id,
                g.updated_time,
                g.updated_user_id,
                u.user_name,
                u.user_image,
                g.goods_status,
                g.delete_flag,
                g.release_time,
                g.video_first_frame,
                ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(g.position, ',', ' '), ')' ))) / 1000, 2) AS dist,
                case when llr.id is null then '0' else '1' end likeStatus,
                g.home_image,
                CONCAT('user_', u.user_phone) as user_phone
            FROM
                second_goods g inner join life_user u on u.id =  g.user_id and u.delete_flag = 0
                left join life_like_record llr on llr.dianzan_id = #{phoneId} and llr.huifu_id = g.id and llr.type = 6 and llr.delete_flag = 0
            where g.delete_flag = 0
                <if test="typeId != null and typeId != '' ">
                    and (g.category_one_id = #{typeId} or g.category_two_id =#{typeId} )
                </if>
                and not exists (select 1 from shieldUser s where s.id = g.id)
                and not exists (select 1 from second_shield s where s.user_id = #{userId} and s.shield_type = 1 and s.shield_id = g.id and s.delete_flag = 0)
                and g.goods_status = 3
                and g.position != '' and g.position is not null
            order by dist, like_count desc, release_time desc
        ) a where a.dist &lt; #{radiusKm}
    </select>

    <!-- 分页查询关注数据concern -->
    <select id="querySecondConcernByPage" resultType="shop.alien.entity.second.vo.SecondGoodsRecommendVo">
        with shieldUser as (
            select g.id from life_blacklist lb inner join second_goods g
            on lb.blocked_id = g.user_id  and g.delete_flag = '0' and g.goods_status = '3'
            where lb.delete_flag = 0 and lb.blocker_type = 2 and lb.blocked_type = 2
            and lb.blocker_id = #{userId}
        )
        select
            g.id,
            g.user_id,
            g.title,
            g.description,
            g.price,
            g.price as amount,
            ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(g.position, ',', ' '), ')' ))) / 1000, 2) AS dist,
            g.like_count,
            g.collect_count,
            g.label,
            g.topic,
            g.trade_id,
            g.created_time,
            g.created_user_id,
            g.updated_time,
            g.updated_user_id,
            g.release_time,
            u.user_name,
            u.user_image,
            CONCAT('user_', u.user_phone) as user_phone,
            g.goods_status,
            g.home_image,
            g.video_first_frame,
            case when llr.id is null then '0' else '1' end likeStatus,
            case when lc.id is null then '0' else '1' end collectStatus,
            (select count(1) from (SELECT id FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0 UNION ALL
            SELECT t.id FROM store_comment t INNER JOIN (SELECT id FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0) d ON t.reply_id = d.id and business_type = 6 and delete_flag = 0) a ) as commentCount
<!--            (SELECT count(1) FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0 ) as commentCount-->
        from
            life_fans f inner join life_user u on
                f.followed_id = CONCAT('user_', u.user_phone) and u.delete_flag = 0
            inner join second_goods g on
                u.id = g.user_id
                and g.goods_status = 3
                and g.delete_flag = 0
            left join life_like_record llr on llr.dianzan_id = #{phoneId} and llr.huifu_id = g.id and llr.type = 6 and llr.delete_flag = 0
            left join life_collect lc on lc.business_id = g.id and lc.user_id = #{phoneId} and lc.delete_flag = 0 and lc.business_type = 1
        where
            f.fans_id = #{phoneId} and f.delete_flag = 0
            and not exists (select 1 from shieldUser s where s.id = g.id)
            and not exists (select 1 from second_shield s where s.user_id = #{userId} and s.shield_type = 1 and s.shield_id = g.id and s.delete_flag = 0)
            and g.position != '' and g.position is not null
        order by g.release_time desc
    </select>

    <select id="querySecondCommentInfo" resultType="shop.alien.entity.second.vo.SecondCommentVo">
        select
            business_id,
            comment_content,
            created_time,
            user_name,
            user_image,
            rn
        from (
            select
                c.business_id,
                c.comment_content,
                c.created_time,
                u.user_name,
                u.user_image,
                row_number() over (partition by c.business_id order by c.created_time desc) as rn
            from
                store_comment c inner join life_user u on c.user_id = u.id and u.delete_flag = 0
            where c.delete_flag = 0 and c.business_type = 7
                and business_id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    ${id}
                </foreach>
        ) a where rn <![CDATA[ <= ]]> 2
    </select>

    <!-- 分页查询新发数据 -->
    <select id="querySecondNewGoodsByPage" resultType="shop.alien.entity.second.vo.SecondGoodsRecommendVo">
        with shieldUser as (
            select g.id from life_blacklist lb inner join second_goods g
                on lb.blocked_id = g.user_id  and g.delete_flag = '0' and g.goods_status = '3'
            where lb.delete_flag = 0 and lb.blocker_type = 2 and lb.blocked_type = 2
                and lb.blocker_id = #{userId}
        )
        SELECT * FROM (
            SELECT
                g.id,
                g.user_id,
                g.title,
                g.description,
                g.price,
                g.price as amount,
                g.position,
                g.like_count,
                g.collect_count,
                g.category_one_id,
                g.category_two_id,
                g.label,
                g.topic,
                g.trade_id,
                g.created_time,
                g.created_user_id,
                g.updated_time,
                g.updated_user_id,
                g.release_time,
                u.user_name,
                u.user_image,
                CONCAT('user_', u.user_phone) as user_phone,
                g.goods_status,
                g.delete_flag,
                ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(g.position, ',', ' '), ')' ))) / 1000, 2) AS dist,
                g.home_image,
                g.video_first_frame,
                case when llr.id is null then '0' else '1' end likeStatus,
                case when lc.id is null then '0' else '1' end collectStatus,
                (select count(1) from (SELECT id FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0 UNION ALL
                SELECT t.id FROM store_comment t INNER JOIN (SELECT id FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0) d ON t.reply_id = d.id and business_type = 6 and delete_flag = 0) a ) as commentCount
    <!--            (SELECT count(1) FROM store_comment c where c.business_id = g.id and c.business_type = 7 and c.delete_flag = 0 ) as commentCount-->
            FROM
                second_goods g inner join life_user u on u.id = g.user_id
                left join life_like_record llr on llr.dianzan_id = #{phoneId} and llr.huifu_id = g.id and llr.type = 6 and llr.delete_flag = 0
                left join life_collect lc on lc.business_id = g.id and lc.user_id = #{phoneId} and lc.delete_flag = 0 and lc.business_type = 1
            where g.delete_flag = 0
                and not exists (select 1 from shieldUser s where s.id = g.id)
                and not exists (select 1 from second_shield s where s.user_id = #{userId} and s.shield_type = 1 and s.shield_id = g.id and s.delete_flag = 0)
                and g.goods_status = 3
                and g.position != '' and g.position is not null
            order by g.release_time desc
        ) a where a.dist &lt; #{radiusKm}
    </select>

    <select id="querySecondGoodsDetail" resultType="shop.alien.entity.second.vo.SecondGoodsRecommendVo">
        select
            g.id,
            g.user_id,
            g.title,
            g.description,
            g.price as amount,
            g.position,
            g.like_count,
            g.collect_count,
            g.category_one_id,
            g.category_two_id,
            g.label,
            g.topic,
            g.home_image,
            g.release_time,
            g.video_first_frame,
            lu.user_name,
            CONCAT('user_', lu.user_phone) as user_phone,
            lu.user_image,
            goods_status,
            ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(g.position, ',', ' '), ')' ))) / 1000, 2) AS dist,
            case when llr.id is null then '0' else '1' end likeStatus,
            case when lc.id is null then '0' else '1' end collectStatus
        from second_goods g inner join life_user lu on g.user_id = lu.id and lu.delete_flag = 0
            left join life_like_record llr on llr.dianzan_id = #{phoneId} and llr.huifu_id = g.id and llr.type = 6 and llr.delete_flag = 0
            left join life_collect lc on lc.business_id = g.id and lc.user_id = #{phoneId} and lc.delete_flag = 0 and lc.business_type = 1
        where g.id = #{goodsId} and g.delete_flag = 0
            and g.position != '' and g.position is not null
    </select>

</mapper>