<?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.jeeplus.modules.oa.dao.OaNotifyRecordDao">
    
	<sql id="oaNotifyRecordColumns">
		a.ID AS "id",
		a.OA_NOTIFY_ID AS "oaNotify.id",
		a.USER_ID AS "user.id",
		a.READ_FLAG AS "readFlag",
		a.READ_DATE AS "readDate",
		u.name AS "user.name",
		o.name AS "user.office.name",
		o.name AS "user.officeName",
		so.name AS "officeName"
	</sql>
		<sql id="oaNotifyRecordColumn">
		a.ID AS "id",
		a.OA_NOTIFY_ID AS "oaNotify.id",
		a.USER_ID AS "user.id",
		a.READ_FLAG AS "readFlag",
		a.READ_DATE AS "readDate"
		
	</sql>
	
	
	
	<sql id="oaNotifyRecordJoins">
		LEFT JOIN sys_user u ON u.id = a.user_id
		LEFT JOIN sys_office o ON o.id = u.office_id
		LEFT JOIN sys_office so ON so.id = a.office_id
	</sql>
    
	<select id="get" resultType="OaNotifyRecord">
		SELECT 
			<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="OaNotifyRecord">
		SELECT 
			<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE 1=1
		<if test="oaNotify != null and oaNotify.id != null and oaNotify.id != ''">
			AND oa_notify_id = #{oaNotify.id}
		</if>
		AND a.read_flag=#{readFlag}
		AND a.office_id is NULL
		ORDER BY a.read_flag ASC
	</select>

	<select id="findOffices" resultType="OaNotifyRecord">
		SELECT
		<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE a.user_id is NULL
		<if test="oaNotify != null and oaNotify.id != null and oaNotify.id != ''">
			AND oa_notify_id = #{oaNotify.id}
		</if>
		ORDER BY a.id ASC
	</select>
	<select id="findUsers" resultType="OaNotifyRecord">
		SELECT
		<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE a.office_id is NULL
		<if test="oaNotify != null and oaNotify.id != null and oaNotify.id != ''">
			AND oa_notify_id = #{oaNotify.id}
		</if>
		ORDER BY a.id ASC
	</select>

	<!--PC端使用：包含已读和未读 -->
	<select id="findList2" resultType="OaNotifyRecord">
		SELECT
		<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE 1=1
		<if test="oaNotify != null and oaNotify.id != null and oaNotify.id != ''">
			AND a.oa_notify_id = #{oaNotify.id}
		</if>
		AND a.office_id is NULL
		ORDER BY a.read_flag ASC
	</select>

		<select id="findUserList" resultType="OaNotifyRecord">
		SELECT 
			<include refid="oaNotifyRecordColumn"/>
		FROM oa_notify_record a
		WHERE a.read_flag=#{readFlag}
			AND a.oa_notify_id = #{oaNotify.id}
	</select>
	
	<select id="findAllList" resultType="OaNotifyRecord">
		SELECT 
			<include refid="oaNotifyRecordColumns"/>
		FROM oa_notify_record a
		<include refid="oaNotifyRecordJoins"/>
		WHERE 1=1
		ORDER BY a.read_flag ASC
	</select>
	
	<insert id="insert">
		INSERT INTO oa_notify_record(
			ID,
			OA_NOTIFY_ID,
			USER_ID,
			READ_FLAG,
			READ_DATE
		) VALUES (
			#{id},
			#{oaNotify.id},
			#{user.id},
			#{readFlag},
			#{readDate}
		)
	</insert>
	
	<insert id="insertAll" parameterType="List">
		INSERT INTO oa_notify_record(
			ID,
			OA_NOTIFY_ID,
			USER_ID,
			READ_FLAG,
			READ_DATE
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.oaNotify.id},
				#{e.user.id},
				#{e.readFlag},
				#{e.readDate}
			FROM dual
		</foreach>
	</insert>
	<insert id="insertOfficeAll" parameterType="List">
		INSERT INTO oa_notify_record(
			ID,
			OA_NOTIFY_ID,
			OFFICE_ID
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.oaNotify.id},
				#{e.officeId}
			FROM dual
		</foreach>
	</insert>

	<update id="update">
		UPDATE oa_notify_record SET 	
			READ_FLAG = #{readFlag},
			READ_DATE = #{readDate}
		WHERE OA_NOTIFY_ID = #{oaNotify.id}
			AND USER_ID = #{user.id}
			AND READ_FLAG != '1'
	</update>
	
	<delete id="delete">
		DELETE FROM oa_notify_record  
		WHERE id = #{id}
	</delete>
	
	<update id="deleteByOaNotifyId">
		UPDATE oa_notify_record SET READ_FLAG = ''
		WHERE oa_notify_id = #{oaNotifyId}
	</update>
	
</mapper>