<?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.amms.mapper.AnnouncementMapper">

    <resultMap type="com.amms.domain.Announcement" id="AnnouncementResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="isTop"    column="is_top"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
    </resultMap>

    <sql id="selectAnnouncementVo">
        select id, title, content, is_top, status, create_time, creator, update_time, updater from announcement
    </sql>

    <select id="selectAnnouncementList" parameterType="com.amms.domain.Announcement" resultMap="AnnouncementResult">
        <include refid="selectAnnouncementVo"/>
        <where>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="creator != null "> and creator = #{creator}</if>
            <if test="updater != null "> and updater = #{updater}</if>
        </where>
    </select>

    <!-- 置顶公告（仅返回置顶且显示的记录），按更新时间倒序 -->
    <select id="selectTopAnnouncements" resultMap="AnnouncementResult">
        <include refid="selectAnnouncementVo"/>
        where is_top = 1 and status = 1
        order by create_time desc
    </select>

    <!-- 普通公告（非置顶且显示的记录），按更新时间倒序，分页在上层处理；支持标题模糊搜索 -->
    <select id="selectNormalAnnouncements" parameterType="com.amms.domain.Announcement" resultMap="AnnouncementResult">
        <include refid="selectAnnouncementVo"/>
        <where>
            (is_top is null or is_top = 0) and status = 1
            <if test="title != null and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAnnouncementById" parameterType="Long" resultMap="AnnouncementResult">
        <include refid="selectAnnouncementVo"/>
        where id = #{id}
    </select>

    <insert id="insertAnnouncement" parameterType="com.amms.domain.Announcement" useGeneratedKeys="true" keyProperty="id">
        insert into announcement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="isTop != null">is_top,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>

    <update id="updateAnnouncement" parameterType="com.amms.domain.Announcement">
        update announcement
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAnnouncementById" parameterType="Long">
        delete from announcement where id = #{id}
    </delete>
</mapper>
