<?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.gameplatform.server.mapper.admin.AnnouncementMapper">
    <resultMap id="AnnouncementMap" type="com.gameplatform.server.model.entity.admin.Announcement">
        <id property="id" column="id" />
        <result property="title" column="title" />
        <result property="content" column="content" />
        <result property="enabled" column="enabled" />
        <result property="jumpUrl" column="jump_url" />
        <result property="belongId" column="belong_id" />
        <result property="createdAt" column="created_at" />
        <result property="updatedAt" column="updated_at" />
    </resultMap>

    <select id="findById" parameterType="long" resultMap="AnnouncementMap">
        SELECT id, title, content, enabled, jump_url, belong_id, created_at, updated_at
        FROM announcement
        WHERE id = #{id}
        LIMIT 1
    </select>

    <insert id="insert" parameterType="com.gameplatform.server.model.entity.admin.Announcement" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO announcement (title, content, enabled, jump_url, belong_id)
        VALUES (#{title}, #{content}, #{enabled}, #{jumpUrl}, #{belongId})
    </insert>

    <update id="update" parameterType="com.gameplatform.server.model.entity.admin.Announcement">
        UPDATE announcement
        <set>
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="jumpUrl != null">jump_url = #{jumpUrl},</if>
            <if test="belongId != null">belong_id = #{belongId},</if>
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="long">
        DELETE FROM announcement WHERE id = #{id}
    </delete>

    <select id="findAll" resultMap="AnnouncementMap">
        SELECT id, title, content, enabled, jump_url, belong_id, created_at, updated_at
        FROM announcement
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countAll" resultType="long">
        SELECT COUNT(1) FROM announcement
    </select>

    <select id="findByEnabled" resultMap="AnnouncementMap">
        SELECT id, title, content, enabled, jump_url, belong_id, created_at, updated_at
        FROM announcement
        WHERE enabled = #{enabled}
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countByEnabled" resultType="long">
        SELECT COUNT(1) FROM announcement WHERE enabled = #{enabled}
    </select>

    <update id="updateEnabled">
        UPDATE announcement SET enabled = #{enabled} WHERE id = #{id}
    </update>
    
    <select id="findByBelongId" resultMap="AnnouncementMap">
        SELECT id, title, content, enabled, jump_url, belong_id, created_at, updated_at
        FROM announcement
        WHERE belong_id = #{belongId}
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>
    
    <select id="countByBelongId" resultType="long">
        SELECT COUNT(1) FROM announcement WHERE belong_id = #{belongId}
    </select>
    
    <select id="findByBelongIdAndEnabled" resultMap="AnnouncementMap">
        SELECT id, title, content, enabled, jump_url, belong_id, created_at, updated_at
        FROM announcement
        WHERE belong_id = #{belongId} AND enabled = #{enabled}
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>
    
    <select id="countByBelongIdAndEnabled" resultType="long">
        SELECT COUNT(1) FROM announcement WHERE belong_id = #{belongId} AND enabled = #{enabled}
    </select>
</mapper>
