<?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.work.dao.report.WorkReportRecordDao">
    
	<sql id="workReportRecordColumns">
		a.id AS "id",
		a.report_id AS "report.id",
		a.user_id AS "user.id",
		a.group_id AS "group.id",
		a.read_flag AS "readFlag",
		a.read_date AS "readDate",
		u.name AS "user.name",
		o.name AS "user.office.name"
	</sql>
		<sql id="workReportRecordColumn">
		a.id AS "id",
		a.report_id AS "report.id",
		a.user_id AS "user.id",
		a.group_id AS "group.id",
		a.read_flag AS "readFlag",
		a.read_date AS "readDate"
	</sql>
	
	<sql id="workReportRecordJoins">
		LEFT JOIN work_report b ON b.id = a.report_id
		LEFT JOIN sys_user u ON u.id = a.user_id
		JOIN sys_office o ON o.id = u.office_id
	</sql>
	
    
	<select id="get" resultType="WorkReportRecord" >
		SELECT 
			<include refid="workReportRecordColumns"/>
		FROM work_report_record a
		<include refid="workReportRecordJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReportRecord" >
		SELECT 
			<include refid="workReportRecordColumns"/>
		FROM work_report_record a
		<include refid="workReportRecordJoins"/>
		<where>
			
			<if test="report != null and report.id != ''">
				AND a.report_id = #{report.id}
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND a.user_id LIKE 
					<if test="dbName == 'oracle'">'%'||#{user.id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{user.id}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{user.id},'%')</if>
			</if>
			<if test="group != null and group.id != null and group.id != ''">
				AND a.group_id LIKE 
					<if test="dbName == 'oracle'">'%'||#{group.id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{group.id}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{group.id},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<select id="findUserList" resultType="WorkReportRecord" >
		SELECT 
			<include refid="workReportRecordColumn"/>
		FROM work_report_record a
		WHERE a.read_flag =#{readFlag}
			AND a.report_id =#{report.id}
			AND a.user_id is not null
			 
	</select>
	
	<select id="findAllList" resultType="WorkReportRecord" >
		SELECT 
			<include refid="workReportRecordColumns"/>
		FROM work_report_record a
		<include refid="workReportRecordJoins"/>
		<where>
			
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_report_record(
			id,
			report_id,
			user_id,
			group_id,
			read_flag,
			read_date
		) VALUES (
			#{id},
			#{report.id},
			#{user.id},
			#{group.id},
			#{readFlag},
			#{readDate}
		)
	</insert>
	
	<insert id="insertAllUser" parameterType="List" >
		INSERT INTO work_report_record(
			id,
			report_id,
			user_id,
			read_flag,
			read_date
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.report.id},
				#{e.user.id},
				#{e.readFlag},
				#{e.readDate}
			FROM dual
		</foreach>
	</insert>
	
	<insert id="insertAllGroup" parameterType="List" >
		INSERT INTO work_report_record(
		    id,
			report_id,
			group_id,
			read_flag,
			read_date
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.report.id},
				#{e.group.id},
				#{e.readFlag},
				#{e.readDate}
			FROM dual
		</foreach>
	</insert>
	
	
	<update id="update">
		UPDATE work_report_record SET
			READ_FLAG = #{readFlag},
			READ_DATE = #{readDate}
		WHERE REPORT_ID = #{report.id}
			AND USER_ID = #{user.id}
			AND READ_FLAG != '1'
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_report_record
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE report_id = #{report.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_report_record SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE report_id = #{report.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReportRecord" statementType="STATEMENT">
		select * FROM work_report_record  where ${propertyName} = '${value}'
	</select>
	
	
	<delete id="deleteByReportId">
		DELETE FROM work_report_record 
		WHERE report_id = #{report.id}
	</delete>
	
</mapper>