<?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.news.mapper.NewsMapper">

    <resultMap type="com.ruoyi.frequency.news.entity.News" id="NewsResult">
        <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="title"    column="title"    />
        <result property="image"    column="image"    />
        <result property="pid"    column="pid"    />
        <result property="sort"    column="sort"    />
        <result property="content"    column="content"    />
        <result property="likeNum"    column="like_num"    />
        <result property="commentNum"    column="comment_num"    />
        <result property="collectNum"    column="collect_num"    />
        <result property="browseNum"    column="browse_num"    />
        <result property="commentId"    column="comment_id"    />
    </resultMap>



    <sql id="selectNewsVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_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
        from t_news AS t

    </sql>

    <select id="selectNewsList" parameterType="com.ruoyi.frequency.news.entity.News" resultMap="NewsResult">
        <include refid="selectNewsVo"/>
        <where> AND t.del_flag != 2 and t.pid = 0
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectNewsById" parameterType="Long" resultMap="NewsResult">
        <include refid="selectNewsVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertNews" parameterType="com.ruoyi.frequency.news.entity.News">
        insert into t_news
        <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="title != null">title,</if>
            <if test="image != null">image,</if>
            <if test="pid != null">pid,</if>
            <if test="sort != null">sort,</if>
            <if test="content != null">content,</if>
            <if test="likeNum != null">like_num,</if>
            <if test="commentNum != null">comment_num,</if>
            <if test="collectNum != null">collect_num,</if>
            <if test="browseNum != null">browse_num,</if>
            <if test="commentId != null">comment_id,</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="title != null">#{title},</if>
            <if test="image != null">#{image},</if>
            <if test="pid != null">#{pid},</if>
            <if test="sort != null">#{sort},</if>
            <if test="content != null">#{content},</if>
            <if test="likeNum != null">#{likeNum},</if>
            <if test="commentNum != null">#{commentNum},</if>
            <if test="collectNum != null">#{collectNum},</if>
            <if test="browseNum != null">#{browseNum},</if>
            <if test="commentId != null">#{commentId},</if>
        </trim>
    </insert>

    <update id="updateNews" parameterType="com.ruoyi.frequency.news.entity.News">
        update t_news
        <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="title != null">title = #{title},</if>
            <if test="image != null">image = #{image},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="content != null">content = #{content},</if>
            <if test="likeNum != null">like_num = like_num + #{likeNum},</if>
            <if test="commentNum != null">comment_num = comment_num + #{commentNum},</if>
            <if test="collectNum != null">collect_num = collect_num + #{collectNum},</if>
            <if test="browseNum != null">browse_num = browse_num + #{browseNum},</if>
            <if test="commentId != null">comment_id = #{commentId},</if>
        </trim>
        where id = #{id}
        <if test="likeNum != null">and like_num + #{likeNum} >= 0</if>
        <if test="commentNum != null">and comment_num + #{commentNum} >= 0</if>
        <if test="collectNum != null">and collect_num + #{collectNum} >= 0</if>
        <if test="browseNum != null">and  browse_num + #{browseNum} >= 0</if>
    </update>

    <select id="newsPage" resultType="com.ruoyi.frequency.news.entity.News">
        select
        t.id ,
        t.create_by ,
        t.create_time ,
        t.update_by ,
        t.update_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,
        CASE WHEN col.id is null THEN 1 ELSE 2 END isCollect,
        1 isSelf
        from t_news AS t
        LEFT JOIN t_collect col on col.object_id = t.id and col.del_flag = 1  AND col.type = 4  AND col.user_id = #{userVo.userId}
        AND col.user_type = #{userVo.userType}
        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>
            AND t.del_flag = 1 and t.pid = 0
            <if test="req.keyword != null  and req.keyword != ''">
                and t.title like concat('%',#{req.keyword},'%')
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="newsDetail" resultType="com.ruoyi.frequency.news.entity.News">
        select
        t.id ,
        t.create_by ,
        t.create_time ,
        t.update_by ,
        t.update_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,
        CASE WHEN col.id is null THEN 1 ELSE 2 END isCollect,
        1 isSelf
        from t_news AS t
        LEFT JOIN t_collect col on col.object_id = t.id and col.del_flag = 1  AND col.type = 4  AND col.user_id = #{userVo.userId}
        AND col.user_type = #{userVo.userType}
        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 t.id = #{id}
    </select>
    <select id="statistics" resultType="com.ruoyi.frequency.news.model.result.NewsCount">
        SELECT
            count(1) num
        FROM
            t_news t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>