<?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.SysMessageDao">

    <resultMap type="com.ym.mec.biz.dal.entity.SysMessage" id="message">
        <result column="id_" property="id"/>
        <result column="title_" property="title"/>
        <result column="content_" property="content"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="receiver_" property="receiver"/>
        <result column="send_time_" property="sendTime"/>
        <result column="error_msg_" property="errorMsg"/>
        <result column="create_on_" property="createOn"/>
        <result column="modify_on_" property="modifyOn"/>
        <result column="user_id_" property="userId"/>
        <result column="memo_" property="memo"/>
        <result column="read_status_" property="readStatus"/>
        <result column="group_" property="group"/>
    </resultMap>
    
    <resultMap type="com.ym.mec.biz.dal.dto.Mapper" id="Mapper">
    	<result column="key_" property="key"/>
        <result column="value_" property="value"/>
    </resultMap>

	<sql id="queryCondition">
		<where>
			<if test="userId != null">
				and user_id_ = #{userId}
			</if>
			<if test="receiver != null">
				and receiver_ like '%' #{receiver} '%'
			</if>
			<if test="status != null">
				and status_ = #{status,jdbcType=TINYINT}
			</if>
			<if test="readStatus != null">
				and read_status_ = #{readStatus,jdbcType=TINYINT}
			</if>
			<if test="title != null">
				and title_ like '%' #{title} '%'
			</if>
			<if test="type != null">
				and type_ = #{type}
			</if>
			<if test="group != null">
				and group_ = #{group}
			</if>
			<if test="readStatus != null">
				and read_status_ = #{readStatus}
			</if>
		</where>
	</sql>

    <select id="get" resultMap="message" parameterType="java.lang.Long">
		select *
		from sys_message
		where id_ = #{id,jdbcType=BIGINT}
	</select>

	<delete id="delete" parameterType="java.lang.Long">
		delete from
		sys_message
		where
		id_ = #{id,jdbcType=BIGINT}
	</delete>

	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysMessage">
		insert into sys_message (user_id_,title_, content_,type_, receiver_,
		send_time_, error_msg_,memo_, create_on_,modify_on_,group_)
		values (#{userId}, #{title},#{content},#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{receiver},now(),#{errorMsg},#{memo},now(),now(),#{group})
	</insert>

	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.SysMessage">
		insert into sys_message (id_, user_id_,
		title_, content_,
		type_, status_, receiver_,
		send_time_, error_msg_,read_status_,memo_, create_on_,
		modify_on_,group_)
		values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(
			#{item.id,jdbcType=BIGINT},#{item.userId,jdbcType=BIGINT}, #{item.title,jdbcType=VARCHAR},
			#{item.content,jdbcType=VARCHAR},
			#{item.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			#{item.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{item.receiver,jdbcType=VARCHAR},
			#{item.sendTime},#{item.errorMsg,jdbcType=VARCHAR},#{item.readStatus},#{item.memo},
			#{item.createOn},
			#{item.modifyOn},#{item.group}
			)
		</foreach>
	</insert>

	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysMessage">
		update sys_message
		<set>
			<if test="userId != null">
				user_id_ = #{userId,jdbcType=BIGINT},
			</if>
			<if test="title != null">
				title_ = #{title,jdbcType=VARCHAR},
			</if>
			<if test="content != null">
				content_ = #{content,jdbcType=VARCHAR},
			</if>
			<if test="type != null">
				type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="status != null">
				status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="receiver != null">
				receiver_ = #{receiver,jdbcType=VARCHAR},
			</if>
			<if test="sendTime != null">
				send_time_ = #{sendTime},
			</if>
			<if test="errorMsg != null">
				error_msg_ = #{errorMsg,jdbcType=VARCHAR},
			</if>
			<if test="readStatus != null">
				read_status_ = #{readStatus,jdbcType=TINYINT},
			</if>
			<if test="memo != null">
				memo_ = #{memo,jdbcType=VARCHAR},
			</if>
			<if test="createOn != null">
				create_on_ = #{createOn},
			</if>
			<if test="modifyOn != null">
				modify_on_ = #{modifyOn},
			</if>
			<if test="group != null">
				group_ = #{group},
			</if>
		</set>
		where id_ = #{id,jdbcType=BIGINT}
	</update>
	<update id="updateStatus">
		update sys_message set read_status_=#{status}
		where user_id_ =#{userId}
	</update>

    <update id="updateOneStatus">
       update sys_message set read_status_=#{status}
		where id_ =#{id}
    </update>

    <select id="queryCount" parameterType="map" resultType="int">
		select count(*) from sys_message
		<include refid="queryCondition" />
		order by create_on_ desc
	</select>

	<select id="queryPage" parameterType="map" resultMap="message">
		select * from sys_message
		<include refid="queryCondition" />
		order by create_on_ desc
		<include refid="global.limit" />
	</select>

	<select id="queryByStatusAndTime" resultMap="message">
		select *
		from sys_message where status_ = #{status} and <![CDATA[DATE_ADD(CURDATE(),
		INTERVAL #{recentMin} MINUTE) >= date(send_time_)]]>
	</select>

	<select id="queryCountOfUnread" resultMap="Mapper" parameterType="map">
		SELECT group_ key_,COUNT(*) value_ FROM sys_message WHERE user_id_ = #{userId} AND read_status_ = 0 and status_ = 2
		<if test="type != null">
		and type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		</if>
		group by group_
	</select>

	<select id="queryUserInRecentMinList" resultMap="message" parameterType="map">
		select *
		from sys_message where receiver_ = #{mobile} and type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and <![CDATA[DATE_ADD(send_time_,
		INTERVAL #{recentMin} MINUTE) >= now()]]>
	</select>
</mapper>
