<?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.hotent.portal.persistence.dao.SysMessageDao">
	<resultMap id="SysMessage" type="com.hotent.portal.model.SysMessage">
		<id property="id" column="id_" jdbcType="VARCHAR"/>
		<result property="subject" column="subject_" jdbcType="VARCHAR"/>
		<result property="ownerId" column="owner_id_" jdbcType="VARCHAR"/>
		<result property="owner" column="owner_" jdbcType="VARCHAR"/>
		<result property="messageType" column="message_type_" jdbcType="VARCHAR"/>
		<result property="messageTag" column="message_tag_" jdbcType="VARCHAR"/>
		<result property="canReply" column="can_reply_" jdbcType="NUMERIC"/>
		<result property="isPublic" column="is_public_" jdbcType="NUMERIC"/>
		<result property="content" column="content_" jdbcType="LONGVARCHAR"/>
		<result property="fileMsg" column="file_msg_" jdbcType="VARCHAR"/>
		<result property="receiverName" column="receiver_name_" jdbcType="VARCHAR"/>
		<result property="createBy" column="CREATE_BY_" jdbcType="VARCHAR"/>
		<result property="createOrgId" column="CREATE_ORG_ID_" jdbcType="VARCHAR"/>
		<result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="updateBy" column="UPDATE_BY_" jdbcType="VARCHAR"/>
		<result property="updateTime" column="UPDATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="isDele" column="is_dele_" jdbcType="NUMERIC"/>
		<result property="ownerSource" column="owner_source_" jdbcType="VARCHAR"/>
		<result property="ownerSourceId" column="owner_source_id_" jdbcType="VARCHAR"/>

		<result property="description" column="description_" jdbcType="VARCHAR"/>
		<result property="sysCode" column="sys_code_" jdbcType="VARCHAR"/>
		<result property="messageSourceType" column="message_source_type_" jdbcType="VARCHAR"/>
		<result property="supportMobile" column="support_mobile_" jdbcType="VARCHAR"/>
		<result property="messageUrl" column="message_url_" jdbcType="VARCHAR"/>
		<result property="messageParams" column="message_params_" jdbcType="VARCHAR"/>
		<result property="resourceId" column="resource_id_" jdbcType="VARCHAR"/>
		<result property="appId" column="app_id_" jdbcType="VARCHAR"/>
		<result property="appMobileId" column="app_mobile_id_" jdbcType="VARCHAR"/>
		<result property="flowId" column="flow_id_" jdbcType="VARCHAR"/>
		<result property="templateId" column="template_id_" jdbcType="VARCHAR"/>
		<result property="templateData" column="template_data_" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="SysMessageAll" type="com.hotent.portal.model.SysMessage">
		<id property="id" column="id_" jdbcType="VARCHAR"/>
		<result property="subject" column="subject_" jdbcType="VARCHAR"/>
		<result property="ownerId" column="owner_id_" jdbcType="VARCHAR"/>
		<result property="owner" column="owner_" jdbcType="VARCHAR"/>
		<result property="messageType" column="message_type_" jdbcType="VARCHAR"/>
		<result property="messageTag" column="message_tag_" jdbcType="VARCHAR"/>
		<result property="canReply" column="can_reply_" jdbcType="NUMERIC"/>
		<result property="isPublic" column="is_public_" jdbcType="NUMERIC"/>
		<result property="content" column="content_" jdbcType="LONGVARCHAR"/>
		<result property="fileMsg" column="file_msg_" jdbcType="VARCHAR"/>
		<result property="receiverName" column="receiver_name_" jdbcType="VARCHAR"/>
		<result property="createBy" column="CREATE_BY_" jdbcType="VARCHAR"/>
		<result property="createOrgId" column="CREATE_ORG_ID_" jdbcType="VARCHAR"/>
		<result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="updateBy" column="UPDATE_BY_" jdbcType="VARCHAR"/>
		<result property="updateTime" column="UPDATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="isDele" column="is_dele_" jdbcType="NUMERIC"/>
		<result property="ownerSource" column="owner_source_" jdbcType="VARCHAR"/>
		<result property="ownerSourceId" column="owner_source_id_" jdbcType="VARCHAR"/>

		<result property="description" column="description_" jdbcType="VARCHAR"/>
		<result property="sysCode" column="sys_code_" jdbcType="VARCHAR"/>
		<result property="messageSourceType" column="message_source_type_" jdbcType="VARCHAR"/>
		<result property="supportMobile" column="support_mobile_" jdbcType="VARCHAR"/>
		<result property="messageUrl" column="message_url_" jdbcType="VARCHAR"/>
		<result property="messageParams" column="message_params_" jdbcType="VARCHAR"/>
		<result property="resourceId" column="resource_id_" jdbcType="VARCHAR"/>
		<result property="resourceName" column="resource_name_" jdbcType="VARCHAR"/>
		<result property="appId" column="app_id_" jdbcType="VARCHAR"/>
		<result property="appName" column="app_name_" jdbcType="VARCHAR"/>
		<result property="appMobileId" column="app_mobile_id_" jdbcType="VARCHAR"/>
		<result property="appMobileName" column="app_mobile_name_" jdbcType="VARCHAR"/>
		<result property="flowId" column="flow_id_" jdbcType="VARCHAR"/>
		<result property="flowName" column="flow_name_" jdbcType="VARCHAR"/>
		<result property="templateId" column="template_id_" jdbcType="VARCHAR"/>
		<result property="templateName" column="template_name_" jdbcType="VARCHAR"/>
		<result property="templateData" column="template_data_" jdbcType="VARCHAR"/>
	</resultMap>
	
	<select id="getMsgByUserId" parameterType="java.util.Map" resultType="com.hotent.portal.model.SysMessage">
		SELECT
		   id, subject, owner,  messageType,  createTime,
		   description, messageSourceType, messageUrl, messageParams,
		   resourceId, resourceName, appId, appName, appMobileId, appMobileName, flowId, flowName, templateId, templateName, templateData,
		   ownerSource, ownerSourceId, ownerSourceName, ownerSourceIcon, ownerSourceIconColor,
		   canReply,  isPublic,  rid, receiverId, receiverName, receivetime
		FROM(
			<if test="map.isPublic == null || map.isPublic == 0">
				SELECT
				   send.id_ id,send.subject_ subject, send.owner_ as owner,
				   send.description_ description, send.message_source_type_ messageSourceType, send.message_url_ messageUrl, send.message_params_ messageParams,
				send.resource_id_ resourceId, notice.title_ resourceName,
				send.app_id_ appId, app.name_ appName,
				send.app_mobile_id_ appMobileId, mapp.name_ appMobileName,
				send.flow_id_ flowId, flow.name_ flowName,
				send.template_id_ templateId, mb.F_mb templateName, send.template_data_ templateData,
				send.owner_source_ ownerSource, send.owner_source_id_ ownerSourceId,
				case
					WHEN send.owner_source_ = '0' THEN sapp.NAME_
					WHEN send.owner_source_ = '1' THEN smapp.NAME_
					WHEN send.owner_source_ = '2' THEN sflow.NAME_
					ELSE null END AS ownerSourceName,
				case
					WHEN send.owner_source_ = '0' THEN sapp.ICON_
					WHEN send.owner_source_ = '1' THEN smapp.ICON_
					WHEN send.owner_source_ = '2' THEN sflow.ICON_
					ELSE null END AS ownerSourceIcon,
				case
					WHEN send.owner_source_ = '0' THEN sapp.ICON_COLOR_
					WHEN send.owner_source_ = '1' THEN smapp.ICON_COLOR_
					WHEN send.owner_source_ = '2' THEN sflow.ICON_COLOR_
					ELSE null END AS ownerSourceIconColor,
				send.message_type_ messageType, send.create_time_ createTime,
				   send.can_reply_ canReply, send.is_public_ isPublic, rece.id_ rid, rece.receiver_id_ receiverId, rece.receiver_ receiverName,
				red.receiver_time_ receivetime
				FROM
				   portal_sys_msg send
				JOIN portal_sys_msg_receiver rece ON send.id_=rece.msg_id_
				LEFT JOIN portal_news_notice_ notice ON send.resource_id_=notice.id_ and send.message_source_type_=2
				LEFT JOIN portal_sys_app app ON send.app_id_ = app.id_ and send.message_source_type_=3
				LEFT JOIN bpm_definition flow ON send.flow_id_ = flow.def_id_ and send.message_source_type_=4
				LEFT JOIN portal_app mapp ON send.app_mobile_id_ = mapp.id_ and send.message_source_type_=5
				LEFT JOIN w_qzent_xxmb mb ON send.template_id_ = mb.id_ and send.message_source_type_=6
				LEFT JOIN portal_sys_msg_read red ON send.id_=red.msg_id_ and red.receiver_id_=#{map.receiverId}

				LEFT JOIN portal_sys_app sapp ON send.owner_source_id_ = sapp.id_ and send.owner_source_=0
				LEFT JOIN portal_app smapp ON send.owner_source_id_ = smapp.id_ and send.owner_source_=1
				LEFT JOIN bpm_definition sflow ON send.owner_source_id_ = sflow.def_id_ and send.owner_source_=2
				WHERE send.is_public_ = 0 and (send.is_dele_ is null or send.is_dele_ = 0)
				<if test="map.receiverId != null"> and rece.receiver_id_=#{map.receiverId}</if>
				<if test="map.receiverName != null"> and rece.receiver_ LIKE CONCAT('%', #{map.receiverName}, '%')</if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) or @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)">
					AND (
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject)">send.subject_ LIKE CONCAT('%', #{map.subject}, '%') </if>
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) and @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> or </if>
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> send.content_ LIKE #{map.content} </if>
					)
				</if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.messageType)"> AND send.message_type_ LIKE #{map.messageType} </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.beginreceiveTime)"> AND send.create_time_>=#{map.beginreceiveTime,jdbcType=TIMESTAMP}</if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.endreceiveTime)"> AND send.create_time_ &lt;=#{map.endreceiveTime,jdbcType=TIMESTAMP}</if>
				<if test="map.receiveTime == 1"> AND red.receiver_time_ is null  </if>
				<if test="map.receiveTime == 2"> AND red.receiver_time_ is not null </if>
			</if>
			--UNION
			<if test="map.isPublic != null and map.isPublic == 1">
				SELECT
				   send1.id_ id,send1.subject_ subject, send1.owner_ as owner,
				   send1.message_type_ messageType, send1.create_time_ createTime,
				send1.description_ description, send1.message_source_type_ messageSourceType, send1.message_url_ messageUrl, send1.message_params_ messageParams,
				send1.resource_id_ resourceId, notice.title_ resourceName,
				send1.app_id_ appId, app.name_ appName,
				send1.app_mobile_id_ appMobileId, mapp.name_ appMobileName,
				send1.flow_id_ flowId, flow.name_ flowName,
				send1.template_id_ templateId, mb.F_mb templateName, send1.template_data_ templateData,
				send1.owner_source_ ownerSource, send1.owner_source_id_ ownerSourceId,
				case
					WHEN send1.owner_source_ = '0' THEN sapp.NAME_
					WHEN send1.owner_source_ = '1' THEN smapp.NAME_
					WHEN send1.owner_source_ = '2' THEN sflow.NAME_
					ELSE null END AS ownerSourceName,
				case
					WHEN send1.owner_source_ = '0' THEN sapp.ICON_
					WHEN send1.owner_source_ = '1' THEN smapp.ICON_
					WHEN send1.owner_source_ = '2' THEN sflow.ICON_
					ELSE null END AS ownerSourceIcon,
				case
					WHEN send1.owner_source_ = '0' THEN sapp.ICON_COLOR_
					WHEN send1.owner_source_ = '1' THEN smapp.ICON_COLOR_
					WHEN send1.owner_source_ = '2' THEN sflow.ICON_COLOR_
					ELSE null END AS ownerSourceIconColor,
				   send1.can_reply_ canReply, send1.is_public_ isPublic, send1.id_ rid, null receiverId, null receiverName,
				   red1.receiver_time_ receivetime FROM
				   portal_sys_msg send1
				LEFT JOIN portal_news_notice_ notice ON send1.resource_id_=notice.id_ and send1.message_source_type_=2
				LEFT JOIN portal_sys_app app ON send1.app_id_ = app.id_ and send1.message_source_type_=3
				LEFT JOIN bpm_definition flow ON send1.flow_id_ = flow.def_id_ and send1.message_source_type_=4
				LEFT JOIN portal_app mapp ON send1.app_mobile_id_ = mapp.id_ and send1.message_source_type_=5
				LEFT JOIN w_qzent_xxmb mb ON send1.template_id_ = mb.id_ and send1.message_source_type_=6
				LEFT JOIN portal_sys_msg_read red1 ON send1.id_=red1.msg_id_ and red1.receiver_id_=#{map.receiverId}

				LEFT JOIN portal_sys_app sapp ON send1.owner_source_id_ = sapp.id_ and send1.owner_source_=0
				LEFT JOIN portal_app smapp ON send1.owner_source_id_ = smapp.id_ and send1.owner_source_=1
				LEFT JOIN bpm_definition sflow ON send1.owner_source_id_ = sflow.def_id_ and send1.owner_source_=2
				WHERE
					 send1.is_public_=1 and (send1.is_dele_ is null or send1.is_dele_ = 0)
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) or @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)">
					AND (
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject)">send1.subject_ LIKE CONCAT('%', #{map.subject}, '%') </if>
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) and @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> or </if>
						<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> send1.content_ LIKE #{map.content} </if>
					)
				</if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.messageType)"> AND send1.message_type_ LIKE #{map.messageType} </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.beginreceiveTime)"> AND send1.create_time_>=#{map.beginreceiveTime,jdbcType=TIMESTAMP}</if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.endreceiveTime)"> AND send1.create_time_ &lt;=#{map.endreceiveTime,jdbcType=TIMESTAMP}</if>
				<if test="map.receiveTime == 1"> AND red1.receiver_time_ is null  </if>
				<if test="map.receiveTime == 2"> AND red1.receiver_time_ is not null </if>
			</if>
		)temp
		<choose>
			<when test= 'map.receiveTime == 2 and map.receiveTimeOrder == "asc"'>
				ORDER BY receivetime asc
			</when>
			<when test= 'map.receiveTime == 2 and map.receiveTimeOrder == "desc"'>
				ORDER BY receivetime desc
			</when>
			<otherwise>
				ORDER BY createTime desc
			</otherwise>
		</choose>
	</select>

	<select id="getMsgListNoUser" parameterType="java.util.Map" resultType="com.hotent.portal.model.SysMessage">
		SELECT
		id, subject, owner,  messageType,  createTime,
		description, messageSourceType, messageUrl, messageParams,
		resourceId, resourceName, appId, appName, appMobileId, appMobileName, flowId, flowName, templateId, templateName, templateData,
		ownerSource, ownerSourceId, ownerSourceName, ownerSourceIcon, ownerSourceIconColor,
		canReply,  isPublic,  rid, receiverId, receiverName, receivetime
		FROM(
		<if test="map.isPublic == null || map.isPublic == 0">
			SELECT
			send.id_ id,send.subject_ subject, send.owner_ as owner,
			send.description_ description, send.message_source_type_ messageSourceType, send.message_url_ messageUrl, send.message_params_ messageParams,
			send.resource_id_ resourceId, notice.title_ resourceName,
			send.app_id_ appId, app.name_ appName,
			send.app_mobile_id_ appMobileId, mapp.name_ appMobileName,
			send.flow_id_ flowId, flow.name_ flowName,
			send.template_id_ templateId, mb.F_mb templateName, send.template_data_ templateData,
			send.owner_source_ ownerSource, send.owner_source_id_ ownerSourceId,
			case
			WHEN send.owner_source_ = '0' THEN sapp.NAME_
			WHEN send.owner_source_ = '1' THEN smapp.NAME_
			WHEN send.owner_source_ = '2' THEN sflow.NAME_
			ELSE null END AS ownerSourceName,
			case
			WHEN send.owner_source_ = '0' THEN sapp.ICON_
			WHEN send.owner_source_ = '1' THEN smapp.ICON_
			WHEN send.owner_source_ = '2' THEN sflow.ICON_
			ELSE null END AS ownerSourceIcon,
			case
			WHEN send.owner_source_ = '0' THEN sapp.ICON_COLOR_
			WHEN send.owner_source_ = '1' THEN smapp.ICON_COLOR_
			WHEN send.owner_source_ = '2' THEN sflow.ICON_COLOR_
			ELSE null END AS ownerSourceIconColor,
			send.message_type_ messageType, send.create_time_ createTime,
			send.can_reply_ canReply, send.is_public_ isPublic, rece.id_ rid, rece.receiver_id_ receiverId, rece.receiver_ receiverName,
			red.receiver_time_ receivetime
			FROM portal_sys_msg send
			JOIN portal_sys_msg_receiver rece ON send.id_=rece.msg_id_
			LEFT JOIN portal_news_notice_ notice ON send.resource_id_=notice.id_ and send.message_source_type_=2
			LEFT JOIN portal_sys_app app ON send.app_id_ = app.id_ and send.message_source_type_=3
			LEFT JOIN bpm_definition flow ON send.flow_id_ = flow.def_id_ and send.message_source_type_=4
			LEFT JOIN portal_app mapp ON send.app_mobile_id_ = mapp.id_ and send.message_source_type_=5
			LEFT JOIN w_qzent_xxmb mb ON send.template_id_ = mb.id_ and send.message_source_type_=6
			LEFT JOIN portal_sys_msg_read red ON send.id_=red.msg_id_

			LEFT JOIN portal_sys_app sapp ON send.owner_source_id_ = sapp.id_ and send.owner_source_=0
			LEFT JOIN portal_app smapp ON send.owner_source_id_ = smapp.id_ and send.owner_source_=1
			LEFT JOIN bpm_definition sflow ON send.owner_source_id_ = sflow.def_id_ and send.owner_source_=2
			WHERE send.is_public_ = 0 and (send.is_dele_ is null or send.is_dele_ = 0)
			<if test="map.receiverId != null"> and rece.receiver_id_=#{map.receiverId}</if>
			<if test="map.receiverName != null"> and rece.receiver_ LIKE CONCAT('%', #{map.receiverName}, '%')</if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) or @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)">
				AND (
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject)">send.subject_ LIKE CONCAT('%', #{map.subject}, '%') </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) and @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> or </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> send.content_ LIKE #{map.content} </if>
				)
			</if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.messageType)"> AND send.message_type_ LIKE #{map.messageType} </if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.beginreceiveTime)"> AND send.create_time_>=#{map.beginreceiveTime,jdbcType=TIMESTAMP}</if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.endreceiveTime)"> AND send.create_time_ &lt;=#{map.endreceiveTime,jdbcType=TIMESTAMP}</if>
			<if test="map.receiveTime == 1"> AND red.receiver_time_ is null  </if>
			<if test="map.receiveTime == 2"> AND red.receiver_time_ is not null </if>
		</if>
		--UNION
		<if test="map.isPublic != null and map.isPublic == 1">
			SELECT
			send1.id_ id,send1.subject_ subject, send1.owner_ as owner,
			send1.message_type_ messageType, send1.create_time_ createTime,
			send1.description_ description, send1.message_source_type_ messageSourceType, send1.message_url_ messageUrl, send1.message_params_ messageParams,
			send1.resource_id_ resourceId, notice.title_ resourceName,
			send1.app_id_ appId, app.name_ appName,
			send1.app_mobile_id_ appMobileId, mapp.name_ appMobileName,
			send1.flow_id_ flowId, flow.name_ flowName,
			send1.template_id_ templateId, mb.F_mb templateName, send1.template_data_ templateData,
			send1.owner_source_ ownerSource, send1.owner_source_id_ ownerSourceId,
			case
			WHEN send1.owner_source_ = '0' THEN sapp.NAME_
			WHEN send1.owner_source_ = '1' THEN smapp.NAME_
			WHEN send1.owner_source_ = '2' THEN sflow.NAME_
			ELSE null END AS ownerSourceName,
			case
			WHEN send1.owner_source_ = '0' THEN sapp.ICON_
			WHEN send1.owner_source_ = '1' THEN smapp.ICON_
			WHEN send1.owner_source_ = '2' THEN sflow.ICON_
			ELSE null END AS ownerSourceIcon,
			case
			WHEN send1.owner_source_ = '0' THEN sapp.ICON_COLOR_
			WHEN send1.owner_source_ = '1' THEN smapp.ICON_COLOR_
			WHEN send1.owner_source_ = '2' THEN sflow.ICON_COLOR_
			ELSE null END AS ownerSourceIconColor,
			send1.can_reply_ canReply, send1.is_public_ isPublic, send1.id_ rid, null receiverId, null receiverName,
			null receivetime
			FROM portal_sys_msg send1
			LEFT JOIN portal_news_notice_ notice ON send1.resource_id_=notice.id_ and send1.message_source_type_=2
			LEFT JOIN portal_sys_app app ON send1.app_id_ = app.id_ and send1.message_source_type_=3
			LEFT JOIN bpm_definition flow ON send1.flow_id_ = flow.def_id_ and send1.message_source_type_=4
			LEFT JOIN portal_app mapp ON send1.app_mobile_id_ = mapp.id_ and send1.message_source_type_=5
			LEFT JOIN w_qzent_xxmb mb ON send1.template_id_ = mb.id_ and send1.message_source_type_=6

			LEFT JOIN portal_sys_app sapp ON send1.owner_source_id_ = sapp.id_ and send1.owner_source_=0
			LEFT JOIN portal_app smapp ON send1.owner_source_id_ = smapp.id_ and send1.owner_source_=1
			LEFT JOIN bpm_definition sflow ON send1.owner_source_id_ = sflow.def_id_ and send1.owner_source_=2
			WHERE
			send1.is_public_=1 and (send1.is_dele_ is null or send1.is_dele_ = 0)
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) or @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)">
				AND (
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject)">send1.subject_ LIKE CONCAT('%', #{map.subject}, '%') </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.subject) and @com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> or </if>
				<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.content)"> send1.content_ LIKE #{map.content} </if>
				)
			</if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.messageType)"> AND send1.message_type_ LIKE #{map.messageType} </if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.beginreceiveTime)"> AND send1.create_time_>=#{map.beginreceiveTime,jdbcType=TIMESTAMP}</if>
			<if test="@com.hotent.base.ognl.Ognl@isNotEmpty(map.endreceiveTime)"> AND send1.create_time_ &lt;=#{map.endreceiveTime,jdbcType=TIMESTAMP}</if>
			<if test="map.receiveTime == 1"> AND red1.receiver_time_ is null  </if>
			<if test="map.receiveTime == 2"> AND red1.receiver_time_ is not null </if>
		</if>
		)temp
		<choose>
			<when test= 'map.receiveTime == 2 and map.receiveTimeOrder == "asc"'>
				ORDER BY receivetime asc
			</when>
			<when test= 'map.receiveTime == 2 and map.receiveTimeOrder == "desc"'>
				ORDER BY receivetime desc
			</when>
			<otherwise>
				ORDER BY createTime desc
			</otherwise>
		</choose>
	</select>

	<select id="getMsgById" resultMap="SysMessageAll">
			SELECT send.*,
				notice.title_ resource_name_,
				app.name_ app_name_,
				mapp.name_ app_mobile_name_,
				flow.name_ flow_name_,
				mb.F_mb template_name_,
				send.owner_source_ ownerSource, send.owner_source_id_ ownerSourceId,
				case
					WHEN send.owner_source_ = '0' THEN sapp.NAME_
					WHEN send.owner_source_ = '1' THEN smapp.NAME_
					WHEN send.owner_source_ = '2' THEN sflow.NAME_
					ELSE null END AS ownerSourceName,
				case
					WHEN send.owner_source_ = '0' THEN sapp.ICON_
					WHEN send.owner_source_ = '1' THEN smapp.ICON_
					WHEN send.owner_source_ = '2' THEN sflow.ICON_
					ELSE null END AS ownerSourceIcon,
				case
					WHEN send.owner_source_ = '0' THEN sapp.ICON_COLOR_
					WHEN send.owner_source_ = '1' THEN smapp.ICON_COLOR_
					WHEN send.owner_source_ = '2' THEN sflow.ICON_COLOR_
					ELSE null END AS ownerSourceIconColor,
				case
					WHEN send.owner_source_ = '0' THEN sapp.CALLBACK_URL_
					WHEN send.owner_source_ = '1' THEN smapp.CALLBACK_URL_
					WHEN send.owner_source_ = '2' THEN sflow.CALLBACK_URL_
					ELSE null END AS ownerSourceCurAppCallback,
				rece.id_ rid, rece.RECEIVER_ID_ receiverId, rece.RECEIVER_ receiverName
			FROM portal_sys_msg send
			LEFT JOIN portal_news_notice_ notice ON send.resource_id_=notice.id_ and send.message_source_type_=2
			LEFT JOIN portal_sys_app app ON send.app_id_ = app.id_ and send.message_source_type_=3
			LEFT JOIN bpm_definition flow ON send.flow_id_ = flow.def_id_ and send.message_source_type_=4
			LEFT JOIN portal_app mapp ON send.app_mobile_id_ = mapp.id_ and send.message_source_type_=5
			LEFT JOIN w_qzent_xxmb mb ON send.template_id_ = mb.id_ and send.message_source_type_=6
			LEFT JOIN portal_sys_msg_receiver rece ON send.id_=rece.msg_id_ and send.is_public_=0

			LEFT JOIN portal_sys_app sapp ON send.owner_source_id_ = sapp.id_ and send.owner_source_=0
			LEFT JOIN portal_app smapp ON send.owner_source_id_ = smapp.id_ and send.owner_source_=1
			LEFT JOIN bpm_definition sflow ON send.owner_source_id_ = sflow.def_id_ and send.owner_source_=2
			where send.ID_ = ${id} and (send.is_dele_ is null or send.is_dele_ = 0)
	</select>
	
	<select id="getNotReadMsgByUserId" resultMap="SysMessage">
	     <if test="isPublish == 0">
	    	SELECT a.id_,a.subject_,a.owner_id_,a.message_type_
	    	FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
			and b.receiver_id_=#{receiverId}
			and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0 order by a.create_time_ desc
		</if>
		<if test="isPublish == 1">
		    SELECT a.id_,a.subject_,a.owner_id_,a.message_type_
	    	FROM portal_sys_msg a
			where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
			and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})  order by a.create_time_ desc
		</if>
		
	</select>
	
	
		
	<select id="getOneNotReadMsgByUserIdOracle" resultMap="SysMessage">
		    SELECT * 
		    FROM(
			    SELECT  *
					FROM(
						SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
				    	FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
						and b.receiver_id_=#{receiverId}
						and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0 
						UNION
						SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
				    	FROM portal_sys_msg a
						where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
						and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})
					) temp
				order by create_time_ desc 
			) temp1 where rownum &lt;=1
		
	</select>
	
	<select id="getOneNotReadMsgByUserIdMysql" resultMap="SysMessage">
		    SELECT  *
			FROM(
				SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
		    	FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
				and b.receiver_id_=#{receiverId}
				and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0 
				UNION
				SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
		    	FROM portal_sys_msg a
				where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
				and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})
			) temp
			order by create_time_ desc limit 1
		
	</select>
	
	<select id="getOneNotReadMsgByUserIdMssql" resultMap="SysMessage">
		    SELECT top 1 *
			FROM(
				SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
		    	FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
				and b.receiver_id_=#{receiverId}
				and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0 
				UNION
				SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
		    	FROM portal_sys_msg a
				where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
				and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})
			) temp
			order by create_time_ desc
		
	</select>
	
	<select id="getOneNotReadMsgByUserIdDb2" resultMap="SysMessage">
	      SELECT * 
		    FROM(
			    SELECT  *
				FROM(
					SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
			    	FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
					and b.receiver_id_=#{receiverId}
					and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0 
					UNION
					SELECT a.id_,a.subject_,a.owner_,a.message_type_,a.content_,a.create_time_,a.can_reply_
			    	FROM portal_sys_msg a
					where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
					and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})
				) temp
				order by create_time_ desc
			) temp1 where rownum &lt;=1
	</select>

	<select id="getNoReadMsgbadge" resultType="map">
		SELECT
		    ( SELECT count(a.id_)
			FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
			and (b.receiver_id_=#{receiverId})
			and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0) as userBadge,
		    ( SELECT count(a.id_)
			FROM portal_sys_msg a
			where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
			and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})) as sysBadge
		FROM DUAL
	</select>
	
	 <select id="getNotReadMsgNum" resultMap="SysMessage">
	    SELECT  *
			FROM(
		 		<if test="isPublic == null || isPublic == 0">
					SELECT a.id_
					FROM portal_sys_msg a,portal_sys_msg_receiver b where a.id_=b.msg_id_ and (a.is_dele_ is null or a.is_dele_ = 0)
					and (b.receiver_id_=#{receiverId})
					and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId}) and a.is_public_=0
				</if>
				--UNION
		 		<if test="isPublic != null and isPublic == 1">
					SELECT a.id_
					FROM portal_sys_msg a
					where a.is_public_=1 and (a.is_dele_ is null or a.is_dele_ = 0)
					and not exists (SELECT 1 FROM portal_sys_msg_read d where a.id_=d.msg_id_ and d.receiver_id_=#{receiverId})
				</if>
			) temp
	</select>
	
	<select id="getMsgSize" parameterType="java.lang.String" resultType="com.hotent.portal.model.SysMessage">
		SELECT *
		FROM(
			<if test="isPublic == null || isPublic == 0">
				SELECT
				   send.id_ id,send.subject_ subject, send.owner_ owner,
				   send.message_type_ messageType, send.create_time_ createTime,
				   send.can_reply_ canReply, send.is_public_ isPublic, rece.id_ rid,
				   red.receiver_time_ receivetime FROM
				   portal_sys_msg send
				LEFT JOIN portal_sys_msg_read red ON send.id_=red.msg_id_ and red.receiver_id_=#{receiverId} ,
				portal_sys_msg_receiver rece
				WHERE
					 send.id_=rece.msg_id_ and (send.is_dele_ is null or send.is_dele_ = 0)
				   and   rece.receiver_id_=#{receiverId}
			</if>
			--UNION
			<if test="isPublic != null and isPublic == 1">
				SELECT
				   send1.id_ id,send1.subject_ subject, send1.owner_ owner,
				   send1.message_type_ messageType, send1.create_time_ createTime,
				   send1.can_reply_ canReply, send1.is_public_ isPublic, send1.id_ rid,
				   red1.receiver_time_ receivetime FROM
				   portal_sys_msg send1
				LEFT JOIN portal_sys_msg_read red1 ON send1.id_=red1.msg_id_ and red1.receiver_id_=#{receiverId}
				WHERE
					 send1.is_public_=1 and (send1.is_dele_ is null or send1.is_dele_ = 0)
			</if>
		)temp
		 where  receivetime is null
			ORDER BY rid DESC
	</select>

	<update id="updateDelFlagByIds" parameterType="map">
		UPDATE portal_sys_msg
		SET is_dele_ = 1
		WHERE ID_ IN
		<foreach collection="ids" item="id" open="(" separator="," close=")">
			#{id}
		</foreach>
	</update>


	<resultMap id="SysMsgTempletMap" type="com.hotent.portal.vo.SysMsgTempletVo">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="mb" column="F_mb" jdbcType="VARCHAR"/>
		<result property="yycj" column="F_yycj" jdbcType="VARCHAR"/>
		<result property="cjms" column="F_cjms" jdbcType="VARCHAR"/>
		<result property="mbbm" column="F_mbbm" jdbcType="VARCHAR"/>
		<result property="mbnr" column="F_mbnr" jdbcType="VARCHAR"/>
		<result property="cjsj" column="F_cjsj" jdbcType="TIMESTAMP"/>
		<result property="delFlag" column="F_del_flag" jdbcType="NUMERIC"/>
	</resultMap>
	<resultMap id="SysMsgTempletDetailMap" type="com.hotent.portal.vo.SysMsgTempletDetailVo">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="refId" column="REF_ID_" jdbcType="VARCHAR"/>
		<result property="blmc" column="F_blmc" jdbcType="VARCHAR"/>
		<result property="blbm" column="F_blbm" jdbcType="VARCHAR"/>
		<result property="bllx" column="F_bllx" jdbcType="VARCHAR"/>
		<result property="sl" column="F_sl" jdbcType="VARCHAR"/>
		<result property="zdxz" column="F_zdxz" jdbcType="VARCHAR"/>
		<result property="sortNo" column="F_sortNo" jdbcType="NUMERIC"/>
	</resultMap>
	<select id="querySysMsgTempletList" parameterType="java.lang.String" resultMap="SysMsgTempletMap">
		select * from w_qzent_xxmb
		where F_del_flag = 0
		<if test="ew.customSqlSegment!=null">
			${ew.customSqlSegment}
		</if>
	</select>
	<select id="querySysMsgTempletDetailList" parameterType="java.lang.String" resultMap="SysMsgTempletDetailMap">
		select * from w_qzsub_csbl
		where REF_ID_ = #{refId}
		order by F_sortNo asc, ID_ asc
	</select>

	<select id="getSysMsgTempletByCode" parameterType="java.lang.String"  resultType="map">
		select t.* from w_qzent_xxmb t
		where t.F_del_flag = 0 and t.F_mbbm = #{templetCode}
	</select>
	<select id="getSysMsgTempletChildById" parameterType="java.lang.String"  resultType="map">
		select s.* from w_qzsub_csbl s
		where s.REF_ID_ = #{refId}
		order by s.F_sortNo asc, s.ID_ asc
	</select>

	<select id="queryMsgUserCount" parameterType="java.lang.String" resultType="java.lang.Long">
		select count(m.ID_)  from portal_sys_msg m, portal_sys_msg_receiver r
		where m.ID_ = r.MSG_ID_ and m.IS_DELE_ != '1'
		and m.MESSAGE_TAG_  = 'user'
		and r.RECEIVER_ID_  in (
		  select u.ID_ from uc_user u, uc_org_user ou, uc_org o
		  where u.ID_ = ou.USER_ID_  and ou.ORG_ID_ = o.ID_
		  and u.IS_DELE_ != '1' and o.IS_DELE_ != '1'
		  and o.PATH_  like '${path}%'
		)
		and m.CREATE_TIME_ <![CDATA[>=]]> #{startDate}
		and m.CREATE_TIME_ <![CDATA[<=]]> #{endDate}
	</select>

	<select id="queryMsgUserCountBySource" parameterType="java.lang.String" resultType="map">
		select m.OWNER_SOURCE_ as ownerSource,
		count(m.ID_) num
		from portal_sys_msg m
		INNER JOIN portal_sys_msg_receiver r ON m.ID_ = r.MSG_ID_ and m.IS_DELE_ != '1'
		where m.MESSAGE_TAG_  = 'user'
		and r.RECEIVER_ID_  in (
		  select u.ID_ from uc_user u, uc_org_user ou, uc_org o
		  where u.ID_ = ou.USER_ID_  and ou.ORG_ID_ = o.ID_
		  and u.IS_DELE_ != '1' and o.IS_DELE_ != '1'
		  and o.PATH_  like '${path}%'
		)
		and m.CREATE_TIME_ <![CDATA[>=]]> #{startDate}
		and m.CREATE_TIME_ <![CDATA[<=]]> #{endDate}
		group by m.OWNER_SOURCE_
	</select>

	<select id="queryMsgUserByAppRank" parameterType="java.lang.String" resultType="map">
		select * from (
			select m.OWNER_SOURCE_ as ownerSource , m.OWNER_SOURCE_ID_ as ownerSourceId,
			case
			WHEN m.owner_source_ = '-1' THEN '管理平台'
			WHEN m.owner_source_ = '0' THEN sapp.NAME_
			WHEN m.owner_source_ = '1' THEN smapp.NAME_
			WHEN m.owner_source_ = '2' THEN sflow.NAME_
			ELSE null END AS ownerSourceName,
			count(m.ID_) num
			from portal_sys_msg m
			INNER JOIN portal_sys_msg_receiver r ON m.ID_ = r.MSG_ID_ and m.IS_DELE_ != '1'
			LEFT JOIN portal_sys_app sapp ON m.OWNER_SOURCE_ID_ = sapp.id_ and m.OWNER_SOURCE_=0
			LEFT JOIN portal_app smapp ON m.OWNER_SOURCE_ID_ = smapp.id_ and m.OWNER_SOURCE_=1
			LEFT JOIN bpm_definition sflow ON m.OWNER_SOURCE_ID_ = sflow.def_id_ and m.OWNER_SOURCE_=2
			where m.MESSAGE_TAG_  = 'user'
			and r.RECEIVER_ID_  in (
			  select u.ID_ from uc_user u, uc_org_user ou, uc_org o
			  where u.ID_ = ou.USER_ID_  and ou.ORG_ID_ = o.ID_
			  and u.IS_DELE_ != '1' and o.IS_DELE_ != '1'
			  and o.PATH_  like '${path}%'
			)
			and m.CREATE_TIME_ <![CDATA[>=]]> #{startDate}
			and m.CREATE_TIME_ <![CDATA[<=]]> #{endDate}
			group by m.OWNER_SOURCE_, m.OWNER_SOURCE_ID_
		) t
		where not t.ownerSourceName is null
		order by t.num desc
		limit 10
	</select>

	<select id="queryMsgUserChartByDays" parameterType="java.lang.String" resultType="map">
		select date_format(m.CREATE_TIME_ ,'%Y-%m-%d') as dateStr, count(m.ID_) as num  from portal_sys_msg m, portal_sys_msg_receiver r
		where m.ID_ = r.MSG_ID_ and m.IS_DELE_ != '1'
		and m.MESSAGE_TAG_  = 'user'
		and r.RECEIVER_ID_  in (
		  select u.ID_ from uc_user u, uc_org_user ou, uc_org o
		  where u.ID_ = ou.USER_ID_  and ou.ORG_ID_ = o.ID_
		  and u.IS_DELE_ != '1' and o.IS_DELE_ != '1'
		  and o.PATH_  like '${path}%'
		)
		and m.CREATE_TIME_ <![CDATA[>=]]> #{startDate}
		and m.CREATE_TIME_ <![CDATA[<=]]> #{endDate}
		group by date_format(m.CREATE_TIME_ ,'%Y-%m-%d')
		order by date_format(m.CREATE_TIME_ ,'%Y-%m-%d')
	</select>

	<select id="queryMsgUserChartByMonth" parameterType="java.lang.String" resultType="map">
		select date_format(m.CREATE_TIME_ ,'%Y-%m') as dateStr, count(m.ID_) as num  from portal_sys_msg m, portal_sys_msg_receiver r
		where m.ID_ = r.MSG_ID_ and m.IS_DELE_ != '1'
		and m.MESSAGE_TAG_  = 'user'
		and r.RECEIVER_ID_  in (
		  select u.ID_ from uc_user u, uc_org_user ou, uc_org o
		  where u.ID_ = ou.USER_ID_  and ou.ORG_ID_ = o.ID_
		  and u.IS_DELE_ != '1' and o.IS_DELE_ != '1'
		  and o.PATH_  like '${path}%'
		)
		and m.CREATE_TIME_ <![CDATA[>=]]> #{startDate}
		and m.CREATE_TIME_ <![CDATA[<=]]> #{endDate}
		group by date_format(m.CREATE_TIME_ ,'%Y-%m')
		order by date_format(m.CREATE_TIME_ ,'%Y-%m')
	</select>


</mapper>