<?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.boot.security.server.dao.NoticeDao">

	<sql id="where">
		<where>
			<if test="params.title != null and params.title != ''">
				and t.title like concat('%', #{params.title}, '%')
			</if>
			<if test="params.status != null and params.status != ''">
				and t.status = #{params.status}
			</if>
			<if test="params.beginTime != null and params.beginTime != ''">
				and t.updateTime &gt;= #{params.beginTime}
			</if>

			<if test="params.endTime != null and params.endTime != ''">
				and DATE(t.updateTime) &lt;= #{params.endTime}
			</if>
		</where>
	</sql>

	<select id="count" resultType="int">
		select count(1) from t_notice t
		<include refid="where" />
	</select>

	<select id="list" resultType="Notice">
		select * from t_notice t
		<include refid="where" />
		${params.orderBy}
		limit #{offset}, #{limit}
	</select>

	<select id="listReadUsers" resultType="SysUser">
		select u.* from
		t_notice_read r inner join sys_user u
		on u.id = r.userId
		where
		r.noticeId = #{noticeId}
		order by r.createTime desc
	</select>

	<sql id="whereNotice">
		<where>
			t.status = 1
			<if test="params.title != null and params.title != ''">
				and t.title like concat('%', #{params.title}, '%')
			</if>
			
			<if test="params.beginTime != null and params.beginTime != ''">
				and t.updateTime &gt;= #{params.beginTime}
			</if>

			<if test="params.endTime != null and params.endTime != ''">
				and DATE(t.updateTime) &lt;= #{params.endTime}
			</if>
			<if test="params.isRead != null and params.isRead != ''">
				<if test="params.isRead == 0">
					and r.createTime is null
				</if>
				<if test="params.isRead == 1">
					and r.createTime is not null
				</if>
			</if>
		</where>
	</sql>

	<select id="countNotice" resultType="int">
		select count(1) from t_notice t left join
		t_notice_read r on
		r.noticeId = t.id and r.userId = #{params.userId}
		<include refid="whereNotice" />
	</select>

	<select id="listNotice" resultType="com.boot.security.server.dto.NoticeReadVO">
		select t.*,r.createTime readTime,
		if(r.createTime is null, 0, 1) isRead,r.userId
		from t_notice t left join
		t_notice_read r on r.noticeId = t.id and r.userId = #{params.userId}
		<include refid="whereNotice" />
		${params.orderBy}
		limit #{offset}, #{limit}
	</select>
</mapper>