<?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.frequency.collect.mapper.CollectMapper">

    <resultMap type="com.ruoyi.frequency.collect.entity.Collect" id="CollectResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="userId"    column="user_id"    />
        <result property="type"    column="type"    />
        <result property="objectId"    column="object_id"    />
        <result property="userType"    column="user_type"    />
    </resultMap>

    <sql id="selectCollectVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_id, type, object_id, user_type from t_collect
    </sql>

    <select id="selectCollectList" parameterType="com.ruoyi.frequency.collect.entity.Collect" resultMap="CollectResult">
        <include refid="selectCollectVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="objectId != null "> and object_id = #{objectId}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
        </where>
    </select>

    <select id="selectCollectById" parameterType="Long" resultMap="CollectResult">
        <include refid="selectCollectVo"/>
        where id = #{id}
    </select>

    <insert id="insertCollect" parameterType="com.ruoyi.frequency.collect.entity.Collect">
        insert into t_collect
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="userId != null">user_id,</if>
            <if test="type != null">type,</if>
            <if test="objectId != null">object_id,</if>
            <if test="userType != null">user_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="userId != null">#{userId},</if>
            <if test="type != null">#{type},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="userType != null">#{userType},</if>
        </trim>
    </insert>

    <update id="updateCollect" parameterType="com.ruoyi.frequency.collect.entity.Collect">
        update t_collect
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
            <if test="userType != null">user_type = #{userType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCollectById" parameterType="Long">
        delete from t_collect where id = #{id}
    </delete>

    <delete id="deleteCollectByIds" parameterType="String">
        delete from t_collect where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="collect" parameterType="com.ruoyi.frequency.collect.entity.Collect">
        insert into t_collect
        (id, create_by, create_time, update_by, update_time, type, user_type, user_id, object_id)
        values
            (#{req.id},#{req.updateBy},#{req.updateTime},#{req.updateBy},#{req.updateTime},#{req.type},#{userVo.userType}, #{userVo.userId}, #{req.objectId})
            ON DUPLICATE  KEY UPDATE
                                  del_flag = IF( del_flag = 1, 3, 1 ),
                                  update_time = #{req.updateTime},
                                  update_by = #{req.updateBy};
    </update>

    <select id="worksCollectPage" resultType="com.ruoyi.controller.resp.WorksPageResp">
        SELECT
        t.id,
        c.update_time create_time,
        t.works_type_id,
        t.title,
        t.images,
        t.price,
        t.user_type,
        t.store_id,
        t.source,
        t.order_id,
        t.like_num,
        t.step_num,
        t.comment_num,
        t.collect_num,
        t.browse_num,
        t.remark
        FROM
        t_collect AS c
        LEFT JOIN t_works t on c.object_id = t.id
        WHERE
        c.del_flag = 1
        AND t.del_flag=1
        AND t.is_hide != 2
        AND c.user_type = #{req.userType}
        AND c.user_id = #{req.userId}
        AND c.type = #{req.type}
        <if test="req.typeIds!=null and req.typeIds.size()>0">
            AND t.works_type_id IN
            <foreach collection="req.typeIds" index="index" item="i" open="("
                     separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
            AND (
            ( (t.customer_id = #{userVo.userId}) or (t.user_type = 2 and t.store_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>) )
            or
            (t.user_type = 4 and t.store_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>

        <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
            AND (
            (t.customer_id = #{userVo.userId}) or ( (t.user_type = 2 and t.store_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>) )
            or
            (t.user_type = 4 and t.store_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>

        <if test="req.shieldWorksIdList!=null and req.shieldWorksIdList.size() > 0">
            AND t.id not in
            <foreach collection="req.shieldWorksIdList" item="i" open="(" close=")" separator=",">
                #{i}
            </foreach>
        </if>
        <if test="req.orderSort==null or req.orderSort==''">
            ORDER BY
            c.update_time DESC
        </if>
        <if test="req.orderSort!=null and req.orderSort!=''">
            order by
            <if test="req.orderSort==1">
                t.create_time desc
            </if>
            <if test="req.orderSort==2">
                t.like_num desc
            </if>
            <if test="req.orderSort==3">
                t.comment_num desc
            </if>
        </if>


    </select>


    <select id="postCollectPage" resultType="com.ruoyi.post.controller.resp.PostPageResp">
        SELECT
        t.id,
        1 type,
        c.update_time create_time,
        t.user_id,
        t.user_type,
        t.title,
        t.content,
        t.cover_image,
        t.video_url,
        t.share_num,
        t.like_num,
        t.comment_num,
        t.collect_num,
        t.browse_num,
        NULL isBuy,
        NULL price,
        CASE WHEN fl.id is null THEN 1 ELSE 2 END isLike,
        CASE WHEN col.id is null THEN 1 ELSE 2 END isCollect,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        t.result,
        t.address,
        t.comment_id

        FROM
        t_collect AS c
        LEFT JOIN t_post t on c.object_id = t.id
        LEFT JOIN t_collect col on col.object_id = t.id and col.del_flag = 1  AND col.type = 2  AND col.user_id = #{userVo.userId}
        AND col.user_type = #{userVo.userType}
        LEFT JOIN t_post_like fl on fl.object_id = t.id AND fl.type = 1 AND fl.user_id = #{userVo.userId} AND fl.user_type = #{userVo.userType}
        AND fl.del_flag = 1
        WHERE
        c.del_flag = 1
        AND t.del_flag=1
        AND c.user_type = #{req.userType}
        AND c.user_id = #{req.userId}
        AND c.type = #{req.type}
        <if test="req.typeId!=null and req.typeId!=''">
            AND t.post_type_id = #{req.typeId}
        </if>
        <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>

        <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>

        <if test="req.shieldPostIdList!=null and req.shieldPostIdList.size() > 0">
            AND t.id not in
            <foreach collection="req.shieldPostIdList" item="i" open="(" close=")" separator=",">
                #{i}
            </foreach>
        </if>

        ORDER BY
        c.update_time DESC
    </select>

    <select id="newsCollectPage" resultType="com.ruoyi.frequency.news.entity.News">
        SELECT
            t.id ,
            t.update_time create_time ,
            t.del_flag ,
            t.title ,
            t.image ,
            t.pid ,
            t.sort ,
            t.content,
            t.like_num ,
            t.comment_num ,
            t.collect_num ,
            t.browse_num ,
            t.comment_id,
            CASE WHEN fl.id is null THEN 1 ELSE 2 END isLike,
            2 isCollect,
            1 isSelf
        FROM
            t_collect AS c
                LEFT JOIN t_news t on c.object_id = t.id
                LEFT JOIN t_like fl on fl.link_id = t.id AND fl.type = 1 AND fl.user_id = #{userVo.userId} AND fl.user_type = #{userVo.userType}
                AND fl.del_flag = 1
        WHERE
            c.del_flag = 1
          AND t.del_flag=1
          AND c.user_type = #{req.userType}
          AND c.user_id = #{req.userId}
          AND c.type = #{req.type}
        ORDER BY
            c.update_time DESC
    </select>

    <select id="getUserCollectNum" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        t_collect AS c
        WHERE
        c.del_flag = 1
        AND c.user_type = #{userVo.userType}
        AND c.user_id = #{userVo.userId}
        <if test="req.type!=null and req.type!=''">
            <if test="req.type==1">
                AND c.type = #{req.type}
                AND c.object_id in (select id from t_works where is_hide = 1 and del_flag =1)
                <if test="req.shieldWorksIdList!=null and req.shieldWorksIdList.size() > 0">
                    AND c.object_id not in
                    <foreach collection="req.shieldWorksIdList" item="i" open="(" close=")" separator=",">
                        #{i}
                    </foreach>
                </if>
            </if>
            <if test="req.type==2">
                c.type = #{req.type}
                <if test="req.shieldPostIdList!=null and req.shieldPostIdList.size() > 0">
                    AND c.object_id not in
                    <foreach collection="req.shieldPostIdList" item="i" open="(" close=")" separator=",">
                        #{i}
                    </foreach>
                </if>
            </if>
        </if>
        <if test="req.type==null or req.type==''">
            AND (
            (
            c.type = 1
            AND c.object_id not in (select id from t_works where is_hide = 2 and del_flag =1)
            <if test="req.shieldWorksIdList!=null and req.shieldWorksIdList.size() > 0">
                AND c.object_id not in
                <foreach collection="req.shieldWorksIdList" item="i" open="(" close=")" separator=",">
                    #{i}
                </foreach>
            </if>
            )
            OR (
            c.type = 2
            <if test="req.shieldPostIdList!=null and req.shieldPostIdList.size() > 0">
                AND c.object_id not in
                <foreach collection="req.shieldPostIdList" item="i" open="(" close=")" separator=",">
                    #{i}
                </foreach>
            </if>
            )
            OR c.type = 3
            OR c.type = 4
            )
        </if>
        <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
            AND (
            (c.user_type = 1 and c.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (c.user_type = 2 and c.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (c.user_type = 4 and c.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>

        <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
            AND (
            (c.user_type = 1 and c.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (c.user_type = 2 and c.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (c.user_type = 4 and c.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>
    </select>
</mapper>
