<?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.api.manage.dao.operationManagement.SysNewsManagementDao">

    <select id="list" parameterType="com.api.model.operationManagement.SearchNewsManagement" resultType="com.api.vo.operationManagement.VoNewsManagement">
        select sn.id,sn.code,sn.title,snc.name as news_category_name,sn.is_rotation,sn.rotation_weight,(case su.actual_name is null when true then '外部发布' else su.actual_name end ) as create_name,sn.create_date
        from sys_news_category as snc,sys_news as sn
        LEFT JOIN sys_user as su on sn.create_id = su.id
        <where>
            sn.news_category_id = snc.id
            <if test="code != null and code != ''">
                and sn.code like concat('%',#{code},'%')
            </if>
            <if test="title != null and title != ''">
                and sn.title like concat('%',#{title},'%')
            </if>
            <if test="newsCategoryId != null">
                and sn.news_category_id = #{newsCategoryId}
            </if>
            <if test="createName != null and createName != ''">
                and su.actual_name like concat('%',#{createName},'%')
            </if>
            <if test="createDateStart != null">
                and sn.create_date <![CDATA[ >= ]]> #{createDateStart}
            </if>
            <if test="createDateEnd != null">
                and sn.create_date <![CDATA[ <= ]]> #{createDateEnd}
            </if>
            <if test="isRotation != null">
                and sn.is_rotation = #{isRotation}
            </if>
        </where>
        order by sn.create_date desc
    </select>

    <insert id="insert" parameterType="com.api.model.operationManagement.SysNewsManagement">
        insert into sys_news(
            code,
            title,
            content,
            news_category_id,
            create_id,
            create_date,
            is_rotation
        )values (
            #{code},
            #{title},
            #{content},
            #{newsCategoryId},
            #{createId},
            #{createDate},
            #{isRotation}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoFBINewsManagement">
        select sn.id,sn.code,sn.title,sn.content,snc.name as news_category_name,sn.news_category_id
        from sys_news as sn,sys_news_category as snc
        where sn.news_category_id = snc.id and sn.id = #{newsId}
    </select>

    <update id="update" parameterType="com.api.model.operationManagement.SysNewsManagement">
        update sys_news
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="newsCategoryId != null">
                news_category_id = #{newsCategoryId},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>
    <update id="settingRotation" parameterType="com.api.model.operationManagement.SettingNewsRotation">
        update sys_news
        <set>
            <if test="isRotation != null">
                is_rotation = #{isRotation},
            </if>
            <if test="rotationWeight != null">
                rotation_weight = #{rotationWeight},
            </if>
        </set>
        where id = #{newsId}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from sys_news where id = #{id}
    </delete>

    <select id="countByTitle" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(*) from sys_news where title like concat('%',#{title},'%')
    </select>
    <select id="findSettingRotation" resultType="java.lang.Integer">
        select count(*) from sys_news where is_rotation = 1
    </select>

</mapper>