<?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.ColumnNewsMapper">

    <resultMap type="com.ruoyi.common.core.domain.entity.ColumnNews" id="ColumnNewsResult">
        <result property="newsId" column="news_id"/>
        <result property="newsTitle" column="news_title"/>
        <result property="newsImage" column="news_image"/>
        <result property="columnId" column="column_id"/>
        <result property="columnName" column="column_name"/>
        <result property="newsContent" column="news_content"/>
        <result property="isTop" column="is_top"/>
        <result property="isRotation" column="is_rotation"/>
        <result property="viewsNum" column="views_num"/>
        <result property="reprintUrl" column="reprint_url"/>
        <result property="author" column="author"/>
        <result property="status" column="status"/>
        <result property="isDel" column="is_del"/>
        <result property="reason" column="reason"/>
        <result property="releaseTime" column="release_time"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectColumnNewsVo">
        select n.news_id, n.news_title, n.column_id, n.news_content,n.is_top,n.is_rotation,n.views_num,n.reprint_url,n.author, n.status,n.release_time,  n.reason, n.create_by, n.create_time, n.update_by, n.update_time, n.remark, n.news_image,c.column_name from column_news n
        left join column_navigation_bar c on n.column_id = c.column_id
    </sql>

    <select id="selectColumnNewsList" parameterType="ColumnNews" resultMap="ColumnNewsResult">
        <include refid="selectColumnNewsVo"/>
        <where>
            n.is_del = 'N'
            <if test="newsTitle != null  and newsTitle != ''">and n.news_title like concat('%', #{newsTitle}, '%')</if>
            <if test="newsContent != null  and newsContent != ''">and n.news_content like concat('%', #{newsContent},
                '%')
            </if>
            <if test="status != null  and status != ''">and n.status = #{status}</if>
            <if test="isDel != null  and isDel != ''">and n.is_del = #{isDel}</if>
            <if test="columnName != null  and columnName != ''">and c.column_name = #{columnName}</if>
            <if test="isTop != null  and isTop != ''">and n.is_top = #{isTop}</if>
        </where>
        order by n.create_time DESC , n.update_time DESC
    </select>

    <select id="selectColumnNewsListMenHu" parameterType="ColumnNews" resultMap="ColumnNewsResult">
        select n.news_id, n.news_title,n.news_image,n.news_content,n.is_rotation,n.views_num,n.reprint_url,n.author,n.release_time, n.create_time from column_news n
        <where>
            n.status = '3'
            and n.is_del = 'N'
            <if test="newsTitle != null  and newsTitle != ''">and (n.news_title like concat('%', #{newsTitle}, '%') or
                n.news_content like concat('%', #{newsContent}, '%'))
            </if>
            <if test="columnId != null  and columnId != ''">and n.column_id = #{columnId}</if>
            <if test="isTop != null  and isTop != ''">and n.is_top = #{isTop}</if>
        </where>
        order by n.create_time DESC,n.update_time DESC
    </select>

    <select id="selectColumnNewsListMenHuByColumnId" parameterType="Long" resultMap="ColumnNewsResult">
        select n.news_id, n.news_title,n.news_image,n.is_rotation,n.views_num,n.reprint_url,n.author,n.release_time, n.create_time from column_news n
        <where>
            n.status = '3'
            and n.is_del = 'N'
            <if test="columnId != null  and columnId != ''">and n.column_id = #{columnId}</if>
        </where>
        order by n.create_time DESC,n.update_time DESC limit 10
    </select>

    <select id="selectColumnNewsByNewsId" parameterType="Integer" resultMap="ColumnNewsResult">
        <include refid="selectColumnNewsVo"/>
        where n.news_id = #{newsId}
    </select>

    <select id="selectColumnNewsDetailMenHu" parameterType="Integer" resultMap="ColumnNewsResult">
        select n.news_id, n.news_title, n.column_id, n.news_content,n.is_rotation,n.views_num,n.reprint_url,n.author, n.status,n.release_time,  n.reason, n.create_by, n.create_time, n.update_by, n.update_time, n.remark,n.news_image from column_news n
        where n.news_id = #{newsId}  order by n.create_time DESC,n.update_time DESC
    </select>

    <insert id="insertColumnNews" parameterType="ColumnNews" useGeneratedKeys="true" keyProperty="newsId">
        insert into column_news
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="newsTitle != null and newsTitle != ''">news_title,</if>
            <if test="newsImage != null and newsImage != ''">news_image,</if>
            <if test="columnId != null">column_id,</if>
            <if test="newsContent != null and newsContent != ''">news_content,</if>
            <if test="isRotation != null and isRotation != ''">is_rotation,</if>
            <if test="viewsNum != null">views_num,</if>
            <if test="reprintUrl != null and reprintUrl != ''">reprint_url,</if>
            <if test="author != null and author != ''">author,</if>
            <if test="isTop != null">is_top,</if>
            <if test="isDel != null">is_del,</if>
            <if test="reason != null">reason,</if>
            <if test="releaseTime != null">release_time,</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="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="newsTitle != null and newsTitle != ''">#{newsTitle},</if>
            <if test="newsImage != null and newsImage != ''">#{newsImage},</if>
            <if test="columnId != null">#{columnId},</if>
            <if test="newsContent != null and newsContent != ''">#{newsContent},</if>
            <if test="isRotation != null and isRotation != ''">#{isRotation},</if>
            <if test="viewsNum != null">#{viewsNum},</if>
            <if test="reprintUrl != null and reprintUrl != ''">#{reprintUrl},</if>
            <if test="author != null and author != ''">#{author},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="reason != null">#{reason},</if>
            <if test="releaseTime != null">#{releaseTime},</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="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateColumnNews" parameterType="ColumnNews">
        update column_news
        <trim prefix="SET" suffixOverrides=",">
            <if test="newsTitle != null and newsTitle != ''">news_title = #{newsTitle},</if>
            <if test="newsImage != null and newsImage != ''">news_image = #{newsImage},</if>
            <if test="columnId != null">column_id = #{columnId},</if>
            <if test="newsContent != null and newsContent != ''">news_content = #{newsContent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isRotation != null and isRotation != ''">is_rotation = #{isRotation},</if>
            <if test="viewsNum != null"> views_num = #{viewsNum},</if>
            <if test="reprintUrl != null and reprintUrl != ''">reprint_url = #{reprintUrl},</if>
            <if test="author != null and author != ''">author = #{author},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="releaseTime != null">release_time = #{releaseTime},</if>
            <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="remark != null">remark = #{remark},</if>
        </trim>
        where news_id = #{newsId}
    </update>

    <delete id="deleteColumnNewsByNewsId" parameterType="Integer">
        delete from column_news where news_id = #{newsId}
    </delete>

    <update id="deleteColumnNewsByNewsIds" parameterType="String">
        update  column_news set is_del = 'Y' where news_id in
        <foreach item="newsId" collection="array" open="(" separator="," close=")">
            #{newsId}
        </foreach>
    </update>

    <update id="examineColumnNews" parameterType="ColumnNews">
        update column_news set  status = '3'
        where news_id = #{newsId}
    </update>

    <select id="selectColumnNewsCount" parameterType="Date" resultType="int">
        select ifnull(count(1),0) as num from column_news
        <where>
            is_del = 'N'
            <if test="today != null">and DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(#{today},'%Y-%m-%d')</if>
            <if test="startTime != null">and DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d')</if>
            <if test="endTime != null">and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d')</if>
        </where>
    </select>

    <select id="getRotation"  resultMap="ColumnNewsResult">
        select news_id,news_image from column_news where is_rotation = 'Y' and is_del = 'N' and status = '3' limit 5
    </select>
</mapper>