<?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.ym.mec.biz.dal.dao.ImGroupNoticeDao">

    <resultMap type="com.ym.mec.biz.dal.entity.ImGroupNotice" id="ImGroupNotice">
        <result column="id_" property="id"/>
        <result column="im_group_id_" property="imGroupId"/>
        <result column="title_" property="title"/>
        <result column="content_" property="content"/>
        <result column="real_name_" property="realName"/>
        <result column="avatar_" property="avatar"/>
        <result column="is_top_" property="isTop"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="create_time_" property="createTime"/>
        <result column="operator_id_" property="operatorId"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="ImGroupNotice">
		SELECT * FROM im_group_notice WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="ImGroupNotice">
		SELECT * FROM im_group_notice ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImGroupNotice" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO im_group_notice (im_group_id_,title_,content_,is_top_,
        del_flag_,create_time_,update_time_,operator_id_)
        VALUES(#{imGroupId},#{title},#{content},#{isTop},
        #{delFlag},now(),now(),#{operatorId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ImGroupNotice">
        UPDATE im_group_notice
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="title != null">
                title_ = #{title},
            </if>
            <if test="content != null">
                content_ = #{content},
            </if>
            <if test="isTop != null">
                is_top_ = #{isTop},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="operatorId != null">
                operator_id_ = #{operatorId}
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
		UPDATE im_group_notice SET del_flag_ = 1 WHERE id_ = #{id}
	</update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ImGroupNotice" parameterType="map">
        SELECT ign.*,su.real_name_,su.avatar_ FROM im_group_notice ign
        LEFT JOIN sys_user su ON su.id_ = ign.operator_id_
        <where>
            ign.del_flag_ = 0
            <if test="groupId != null">
                AND ign.im_group_id_ = #{groupId}
            </if>
        </where>
        ORDER BY ign.is_top_ DESC,ign.update_time_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_group_notice
        <where>
            del_flag_ = 0
            <if test="groupId != null">
                AND im_group_id_ = #{groupId}
            </if>
        </where>
	</select>
    <select id="getNewNotice" resultMap="ImGroupNotice">
        SELECT * FROM im_group_notice ign
        WHERE ign.im_group_id_ = #{groupId} AND ign.del_flag_ = 0
        ORDER BY ign.is_top_ DESC,ign.update_time_ DESC LIMIT 1
    </select>
</mapper>
