<?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.jxy.marketall.mapper.MessageMapper">

	<update id="updateMessageStatusByMsgId" parameterType="long">
		update jxy_message.message set status = 1 where id =#{msgId};
	</update>

	<select id="getPageIdByPageName" resultType="long">
		select id from jxy_message.page p where p.name = #{page} and p.coin_type = #{coinType} limit 1;
	</select>

	<insert id="insertMessageByUser">
 		insert into jxy_message.message(
 			<if test="dto.userId != null and dto.userId != 0">user_id,</if>
 			<if test="dto.pageId != null and dto.pageId != 0">page_id,</if>
 			<if test="dto.message != null and dto.message != ''">content,</if>
 			<if test="dto.pid != null">pid,</if>
 			<if test="dto.ifOrigin != null">if_origin,</if>
			<if test="dto.status != null">status,</if>
			<if test="dto.msgFrom != null">msg_from,</if>
 			send_time)
 		VALUE(
			<if test="dto.userId != null and dto.userId != 0">#{dto.userId},</if>
			<if test="dto.pageId != null and dto.pageId != 0">#{dto.pageId},</if>
			<if test="dto.message != null and dto.message != ''">#{dto.message},</if>
			<if test="dto.pid != null">#{dto.pid},</if>
			<if test="dto.ifOrigin != null">#{dto.ifOrigin},</if>
			<if test="dto.status != null">#{dto.status},</if>
			<if test="dto.msgFrom != null">#{dto.msgFrom},</if>
			SYSDATE())
	</insert>

	<insert id="insertPage" useGeneratedKeys="true" keyProperty="id">
		insert into jxy_message.page (name,coin_type) value (#{vo.name},#{vo.coinType}) on duplicate key UPDATE name = name
	</insert>

	<select id="getPageMsgByCondition" parameterType="com.jxy.marketall.vo.AllMsgVo" resultType="com.jxy.marketall.dto.BackendMsgDto">
		select
			m1.id id,
			m1.send_time userDate,
			u1.user_name userName,
			m1.content userMsg,
			p.`name` msgPage,
			p.coin_type coinType,
			m2.content respon,
			u2.user_name responser,
			m2.send_time responDate,
			m1.`status`
		from
			jxy_message.message m1
			LEFT JOIN `jxy_cloud`.sys_user u1 on m1.user_id = u1.user_id
			LEFT JOIN jxy_message.page p on m1.page_id = p.id
			Left join jxy_message.message m2 on m1.id = m2.pid
			left join `jxy_cloud`.sys_user u2 on m2.user_id = u2.user_id
		<where>
			m1.msg_from = 0
			<if test="vo.start != null and vo.start != ''">
				and m1.send_time &gt;= #{vo.start}
			</if>
			<if test="vo.end != null and vo.end != ''">
				and m1.send_time &lt;=  #{vo.end}
			</if>
			<if test="vo.status != null">
				and m1.status = #{vo.status}
			</if>
		</where>
		order by userName,userDate limit #{vo.pageNum},#{vo.pageSize};
	</select>

	<select id="getPageMsgCountByCondition" resultType="java.lang.Long">
		select
			count(*)
		from
			jxy_message.message m1
			LEFT JOIN `jxy_cloud`.sys_user u1 on m1.user_id = u1.user_id
			LEFT JOIN jxy_message.page p on m1.page_id = p.id
			Left join jxy_message.message m2 on m1.id = m2.pid
			left join `jxy_cloud`.sys_user u2 on m2.user_id = u2.user_id
		<where>
			m1.msg_from = 0
			<if test="vo.start != null and vo.start != ''">
				and m1.send_time &gt;= #{vo.start}
			</if>
			<if test="vo.end != null and vo.end != ''">
				and m1.send_time &lt;=  #{vo.end}
			</if>
			<if test="vo.status != null">
				and m1.status = #{vo.status}
			</if>
		</where>
	</select>

	<select id="getAllMsgByStatusAndTime" parameterType="com.jxy.marketall.vo.AllMsgVo"  resultType="com.jxy.marketall.dto.BackendMsgDto">
		select * from (
			select
				m1.id id,
				m1.send_time userDate,
				u1.user_name userName,
				m1.content userMsg,
				p.`name` msgPage,
				p.coin_type coinType,
				m2.content respon,
				u2.user_name responser,
				m2.send_time responDate,
				m1.`status` status
			from
				jxy_message.message m1
				LEFT JOIN `jxy_cloud`.sys_user u1 on m1.user_id = u1.user_id
				LEFT JOIN jxy_message.page p on m1.page_id = p.id
				Left join jxy_message.message m2 on m1.id = m2.pid
				left join `jxy_cloud`.sys_user u2 on m2.user_id = u2.user_id
			<where>
				m1.msg_from = 0
				<if test="vo.start != null and vo.start != ''">
					and m1.send_time &gt;= #{vo.start}
				</if>
				<if test="vo.end != null and vo.end != ''">
					and m1.send_time &lt;=  #{vo.end}
				</if>
				<if test="vo.status != null">
					and m1.status = #{vo.status}
				</if>
			</where>
			order by m1.id,m2.send_time desc
		) a group by a.id order by a.userName,a.userDate desc limit #{vo.pageNum},#{vo.pageSize};
	</select>

	<select id="getAllMsgCountByCondition" resultType="java.lang.Long">
		select count(*) from (
		  select * from (
			select
				m1.id id,
				m1.send_time userDate,
				u1.user_name userName,
				m1.content userMsg,
				p.`name` msgPage,
				p.coin_type coinType,
				m2.content respon,
				u2.user_name responser,
				m2.send_time responDate,
				m1.`status` status
			from
				jxy_message.message m1
				LEFT JOIN `jxy_cloud`.sys_user u1 on m1.user_id = u1.user_id
				LEFT JOIN jxy_message.page p on m1.page_id = p.id
				Left join jxy_message.message m2 on m1.id = m2.pid
				left join `jxy_cloud`.sys_user u2 on m2.user_id = u2.user_id
			<where>
				m1.msg_from = 0
				<if test="vo.start != null and vo.start != ''">
					and m1.send_time &gt;= #{vo.start}
				</if>
				<if test="vo.end != null and vo.end != ''">
					and m1.send_time &lt;=  #{vo.end}
				</if>
				<if test="vo.status != null">
					and m1.status = #{vo.status}
				</if>
			</where>
			order by m1.id,m2.send_time desc
		) a group by a.id
		) n
	</select>

	<select id="getDailyMsgCount" resultType="com.jxy.marketall.dto.DailyMsgCountDto">
		SELECT
		d.`date`,
		IFNULL( t.total, 0 ) AS total,
		IFNULL( t.solvdCount, 0 ) AS solvdCount,
		IFNULL( t.pendingCount, 0 ) AS pendingCount
		FROM
		(
		SELECT
		datelist AS `date`
		FROM
		jxy_message.calendar,(SELECT DATE( send_time ) AS START FROM jxy_message.message ORDER BY send_time LIMIT 1 ) s
		WHERE
		datelist &gt;= START
		AND datelist &lt;= NOW()
		) d
		LEFT JOIN (
		SELECT
		DATE_FORMAT( send_time, '%Y-%m-%d' ) AS date,
		sum( CASE WHEN msg_from = 0 THEN 1 ELSE 0 END ) total,
		sum( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) solvdCount,
		sum( CASE WHEN STATUS = 0 THEN 1 ELSE 0 END ) pendingCount
		FROM
		jxy_message.message
		WHERE
		msg_from = 0
		GROUP BY
		date
		) t ON d.`date` = t.`date`
		<where>
			<if test="vo.start != null and vo.start != ''">
				and d.`date` &gt;= #{vo.start}
			</if>
			<if test="vo.end != null and vo.end != ''">
				and d.`date` &lt;=  #{vo.end}
			</if>
		</where>
		ORDER BY
		d.`date`
	</select>

	<select id="getMsgByMsgId" parameterType="long" resultType="com.jxy.marketall.entity.Message">
		select id,user_id,page_id,content,send_time,pid,if_origin,status,msg_from
		from jxy_message.message where id = #{id}
	</select>

	<select id="getPrivateMsgByUserIdAndMsgStatus" resultType="com.jxy.marketall.dto.PrivateMsgDto">
		select * from (
			select
			m1.id id,
			m1.send_time userDate,
			u1.user_name userName,
			p.`name` msgPage,
			p.coin_type coinType,
			u2.user_name responser,
			m2.send_time responDate,
			m1.status status
			from
			`jxy_message`.message m1
			left join `jxy_message`.page p on m1.page_id = p.id
			left join `jxy_message`.message m2 on m1.id = m2.pid
			left join `jxy_cloud`.sys_user u1 on u1.user_id = m1.user_id
			left join `jxy_cloud`.sys_user u2 on u2.user_id = m2.user_id
			<where>
				m1.msg_from = 0 and m1.user_id = #{id}
				<if test="vo.status != null">
					and m1.status = #{vo.status}
				</if>
			</where>
		) a
		group by a.id order by a.userDate desc limit #{vo.pageNum},#{vo.pageSize};
	</select>

	<select id="getPrivateMsgCountByUserIdAndMsgStatus" resultType="java.lang.Long">
		select count(*) from
			(select * from (
				select
				m1.id id,
				m1.send_time userDate,
				u1.user_name userName,
				p.`name` msgPage,
				p.coin_type coinType,
				u2.user_name responser,
				m2.send_time responDate,
				m1.status status
				from
				`jxy_message`.message m1
				left join `jxy_message`.page p on m1.page_id = p.id
				left join `jxy_message`.message m2 on m1.id = m2.pid
				left join `jxy_cloud`.sys_user u1 on u1.user_id = m1.user_id
				left join `jxy_cloud`.sys_user u2 on u2.user_id = m2.user_id
				<where>
					m1.msg_from = 0 and m1.user_id = #{id}
					<if test="vo.status != null">
						and m1.status = #{vo.status}
					</if>
				</where>
				order by m1.id,m2.send_time desc
			) a group by a.id) n
	</select>

	<select id="getMsgByPage" resultType="com.jxy.marketall.entity.Message">
		select
			m.id id,m.user_id userId,m.page_id pageId,u.user_name userName,
			m.content,m.send_time sendTime,m.pid,
			m.if_origin ifOrigin,m.status,m.msg_from msgFrom
		from
			 `jxy_message`.message m
			 left join `jxy_cloud`.sys_user u on m.user_id = u.user_id
		where
			page_id = #{pageId}
	</select>


	<select id="getPageChooseByUserId" resultType="java.lang.Long">
		select if_like
		from `jxy_message`.page_like
		where page_id = #{pageId} and user_id = #{userId}
	</select>

	<select id="getPageDetail" resultType="com.jxy.marketall.dto.PageDetailDto">
		select like_num likeNum,dislike_num dislikeNum
		from `jxy_message`.page
		where id = #{pageId}
	</select>

</mapper>
