<?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.sunxung.factoring.mapper.sys.systemMsg.SystemMsgMapper">
	<resultMap type="SystemMsg" id="systemMsgResult">
		<id column="id" property="id" />
		<result column="business_id" property="businessId" />
		<result column="business_type" property="businessType.id"/>
		<result column="msg_type_id" property="msgType.id" />
		<result column="send_type" property="sendType"/>
		<result column="sender_id" property="senderUser.id" />
		<result column="sender_name" property="senderName" />
		<result column="title" property="title" />
		<result column="content" property="content" />
		<result column="push_state" property="pushState" />
		<result column="push_time" property="pushTime" />
		<result column="remove" property="remove" />
		<result column="simple_pinyin" property="simplePinYin" />
		<result column="full_pinyin" property="fullPinYin" />
		<result column="task_id" property="taskId"/>
		<result column="user_create" property="userCreate" />
		<result column="user_modified" property="userModified" />
		<result column="gmt_create" property="gmtCreate" />
		<result column="gmt_modified" property="gmtModified" />
	</resultMap>
	<resultMap type="SystemMsg" extends="systemMsgResult" id="systemMsgMap" >
		<result column="readState" property="readState"/>
	</resultMap>

	<resultMap type="SystemMsgUserRel"  id="systemMsgUserRelMap" >
		<id column="id" property="id"/>
		<result column="system_msg_id" property="systemMsgId"/>
		<result column="receiver_user_id" property="receiverUserId"/>
		<result column="receiver_remove" property="receiverRemove"/>
		<result column="readState" property="readState"/>
	</resultMap>
	
	<sql id="fields">
		s.id,
		s.business_id,
		s.business_type,
		s.msg_type_id,
		s.send_type,
		s.sender_id,
		s.sender_name,
		s.title,
		s.content,
		s.push_state,
		s.push_time,
		s.remove,
		s.simple_pinyin,
		s.full_pinyin,
		s.task_id,
		s.user_create,
		s.user_modified,
		s.gmt_create,
		s.gmt_modified,
		m.readState
	</sql>

	<insert id="add" parameterType="SystemMsg" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
		insert into system_msg
		(business_id,business_type,msg_type_id,send_type,sender_id,sender_name,title,content,push_state,push_time,remove,simple_pinyin,full_pinyin,
			task_id,user_create,user_modified)
		values
		(#{businessId},#{businessType.id},#{msgType.id},#{sendType},#{senderUser.id},#{senderName},#{title},#{content},#{pushState},#{pushTime},#{remove},#{simplePinYin},#{fullPinYin},
			#{taskId},#{userCreate},#{userModified})
	</insert>

	<sql id="sql_where">
		<where>
			<if test="null != senderId">
				and s.sender_id=#{senderId}
			</if>
			<if test="null != pushState">
				and s.push_state=#{pushState}
			</if>
			<if test="null != remove">
				and s.remove=#{remove}
			</if>
			<if test="null != keyWord and keyWord != ''">
				and (s.title like CONCAT('%',#{keyWord},'%') or s.content like
				CONCAT('%',#{keyWord},'%')
				or s.simple_pinyin like CONCAT('%',#{keyWord},'%') or s.full_pinyin
				like CONCAT('%',#{keyWord},'%'))
			</if>
			<if test="null != receiverUserId">
				and m.receiver_user_id=#{receiverUserId}
			</if>
			<if test="null != msgType and null != msgType.id">
				and s.msg_type_id=#{msgType.id}
			</if>
			<if test="null != readState">
				and m.readState=#{readState}
			</if>
			<if test="null != receiverRemove">
				and m.receiver_remove=#{receiverRemove}
			</if>
		</where>
	</sql>

	<select id="findBySearch" parameterType="SearchSystemMsg" resultMap="systemMsgMap">
		select
		<include refid="fields" />
		from system_msg s
		left join system_msg_user m
		on s.id=m.system_msg_id
		<include refid="sql_where" />
		order by s.push_state asc,s.push_time desc,CONVERT(s.title using gbk) asc
	</select>

	<select id="countBySearch" parameterType="SearchSystemMsg" resultType="Integer">
		select count(*) from system_msg s
		left join system_msg_user m
		on s.id=m.system_msg_id
		<include refid="sql_where" />
	</select>

	<select id="findSendMsgBySearch" parameterType="SearchSystemMsg" resultMap="systemMsgResult">
		select * from system_msg s
		<include refid="sql_where" />
		order by s.push_state asc,s.push_time desc,CONVERT(s.title using gbk) asc
	</select>
	
	<select id="countSendMsgBySearch" parameterType="SearchSystemMsg" resultType="Integer">
		select count(*) from system_msg s
		<include refid="sql_where" />
	</select>
	
	<select id="get" parameterType="Long" resultMap="systemMsgResult">
		select *
		from system_msg s where s.id=#{id}
	</select>

	<delete id="delete" parameterType="Long">
		delete from system_msg where
		id=#{id}
	</delete>
	
	<select id="findKnownewsByBusiness" resultMap="systemMsgResult">
		SELECT *
		FROM system_msg s 
		WHERE s.business_id = #{businessId} 
			AND s.business_type = #{businessTypeId}
			AND s.msg_type_id = #{msgTypeId}
	</select>

	<insert id="addSystemMsgUser">
		insert into
		system_msg_user(system_msg_id,receiver_user_id,readState,receiver_remove)
		values(#{systemMsgId},#{receiverUserId},#{readState},#{receiverRemove})
	</insert>

	<delete id="deleteBySystemMsgUserId">
		delete from
		system_msg_user where system_msg_id=#{systemMsgId} and receiver_user_id=#{receiverUserId}
	</delete>

	<update id="updateReadState">
		update system_msg_user set readState=#{readState}
		where system_msg_id=#{id} and receiver_user_id=#{userId}
	</update>

	<select id="countBySystemMsgId" resultType="Integer">
		select count(*)
		from system_msg_user where system_msg_id=#{systemMsgId}
		<if test="null != readState">
			and readState=#{readState}
		</if>
	</select>
	
	<select id="findReceiverUserIdBysystemMsgId" resultType="Long">
		select receiver_user_id from system_msg_user where system_msg_id=#{systemMsgId}
		<if test="null != readState">
			and readState=#{readState}
		</if>
	</select>
	
	<delete id="deleteSystemMsgUserByMsgId" parameterType="Long">
		delete from system_msg_user where system_msg_id=#{systemMsgId}
	</delete>
	
	<update id="updateSystemMsgRemove">
		update system_msg set remove=#{remove} where id=#{id}
	</update>
	
	<select id="findTimingMessage" resultType="Long">
		select id from system_msg where push_state=0 and send_type=#{sendType} 
		<![CDATA[ and push_time >= #{pushTimeStart}]]>
		<![CDATA[ and push_time <= #{pushTimeEnd}]]>
	</select>
	
	<update id="updatePushStateById">
		update system_msg set push_state=#{pushState} where id=#{id}
	</update>
	
	<update id="updateReceiverRemoveByMsgId">
		update system_msg_user set receiver_remove=#{receiverRemove} where system_msg_id=#{systemMsgId} and receiver_user_id=#{userId}
	</update>
	
	<select id="findBySystemMsgId" parameterType="Long" resultMap="systemMsgUserRelMap">
		select * from system_msg_user where system_msg_id=#{systemMsgId}
	</select>
	
	<select id="countBySystemMsgIdAndReceiverUserId" resultType="Integer">
		select count(*) from system_msg_user where system_msg_id=#{systemMsgId} and receiver_user_id=#{receiverUserId}
	</select>
	
</mapper>