<?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="is_top_" property="isTop" />
		<result column="is_sent_to_new_member_" property="isSentToNewMember" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="del_flag_" property="delFlag" />
		<result column="operator_id_" property="operatorId" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<resultMap type="com.ym.mec.biz.dal.dto.ImGroupNoticeDto" id="ImGroupNoticeDto" extends="ImGroupNotice">
		<result column="real_name_" property="user.realName"/>
        <result column="avatar_" property="user.avatar"/>
	</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 where tenant_id_ = #{tenantId} 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_,is_sent_to_new_member_,create_time_,update_time_,del_flag_,operator_id_,tenant_id_)
		VALUES(#{imGroupId},#{title},#{content},#{isTop},#{isSentToNewMember},NOW(),NOW(),#{delFlag},#{operatorId},#{tenantId})
	</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="operatorId != null">
		operator_id_ = #{operatorId},
		</if>
		<if test="isTop != null">
		is_top_ = #{isTop},
		</if>
		<if test="isSentToNewMember != null">
		is_sent_to_new_member_ = #{isSentToNewMember},
		</if>
		<if test="imGroupId != null">
		im_group_id_ = #{imGroupId},
		</if>
		<if test="title != null">
		title_ = #{title},
		</if>
		<if test="content != null">
		content_ = #{content},
		</if>
		update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM im_group_notice WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryForPage" resultMap="ImGroupNoticeDto" parameterType="map">
		SELECT ign.*,u.real_name_,u.avatar_ FROM im_group_notice ign left join sys_user u on ign.operator_id_ = u.id_
		<where>
            ign.del_flag_ = 0 and ign.tenant_id_ = #{tenantId}
            <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(ign.id_) FROM im_group_notice ign
		<where>
            ign.del_flag_ = 0 and ign.tenant_id_ = #{tenantId}
            <if test="groupId != null">
                AND ign.im_group_id_ = #{groupId}
            </if>
        </where>
	</select>
	
	<select id="queryLatestNotice" resultMap="ImGroupNoticeDto">
		SELECT * FROM im_group_notice ign
        WHERE ign.im_group_id_ = #{imGroupId} AND ign.del_flag_ = 0
        ORDER BY ign.is_top_ DESC,ign.update_time_ DESC LIMIT 1
	</select>
	
	<delete id="deleteByImGroupId" >
		DELETE FROM im_group_notice WHERE im_group_id_ = #{imGroupId}
	</delete>
</mapper>
