<?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.workattendance.dao.WorkAttendanceRuleRecordDao">
    
	<sql id="workAttendanceRuleRecordColumns">
		a.ID AS "id",
		a.work_attendance_id AS "workAttendanceRule.id",
		u.name AS "user.name",
		so.name AS "officeName"
	</sql>
	
		<sql id="workAttendanceRuleRecordColumn">
		a.ID AS "id",
		a.work_attendance_id AS "workAttendanceRule.id",
		a.USER_ID AS "user.id"
		
	</sql>
	
	
	
	<sql id="workAttendanceRuleRecordJoins">
		LEFT JOIN sys_user u ON u.id = a.user_id
		LEFT JOIN sys_office so ON so.id = a.office_id
	</sql>
    
	<select id="get" resultType="workAttendanceRuleRecord">
		SELECT 
			<include refid="workAttendanceRuleRecordColumns"/>
		FROM work_attendance_record a
		<include refid="workAttendanceRuleRecordJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findUserCount" resultType="java.lang.Integer" >
		SELECT
		count(*)
		FROM work_attendance_record a
        LEFT JOIN work_attendance_rule r ON a.work_attendance_id = r.id
		WHERE a.user_id = #{userId}
        AND r.branch_office = #{branchOffice}
        <if test="id != null and id != ''">
            AND a.work_attendance_id = #{id}
        </if>
	</select>
	<select id="findOfficeCount" resultType="java.lang.Integer" >
		SELECT
		count(*)
		FROM work_attendance_record a
        LEFT JOIN work_attendance_rule r ON a.work_attendance_id = r.id
		WHERE a.office_id = #{officeId}
        AND r.branch_office = #{branchOffice}
        <if test="id != null and id != ''">
            AND a.work_attendance_id = #{id}
        </if>
	</select>

	<select id="findList" resultType="workAttendanceRuleRecord">
		SELECT
			<include refid="workAttendanceRuleRecordColumns"/>
		FROM work_attendance_record a
		<include refid="workAttendanceRuleRecordJoins"/>
		WHERE 1=1
		<if test="workAttendanceRule != null and workAttendanceRule.id != null and workAttendanceRule.id != ''">
			AND a.work_attendance_id = #{workAttendanceRule.id}
		</if>
		AND a.office_id is NULL
		ORDER BY a.id DESC
	</select>

	<select id="findOffices" resultType="workAttendanceRuleRecord">
		SELECT
            a.ID AS "id",
            a.work_attendance_id AS "workAttendanceRule.id",
            so.top_company AS "officeName",
            so.id AS "officeId"
		FROM work_attendance_record a
        LEFT JOIN sys_office so ON so.id = a.office_id
		WHERE a.user_id is NULL
		<if test="workAttendanceRule != null and workAttendanceRule.id != null and workAttendanceRule.id != ''">
			AND a.work_attendance_id = #{workAttendanceRule.id}
		</if>
		ORDER BY a.id ASC
	</select>
	<select id="findUsers" resultType="workAttendanceRuleRecord">
		SELECT
            a.ID AS "id",
            a.work_attendance_id AS "workAttendanceRule.id",
            u.name AS "user.name",
            a.user_id AS "user.id",
            so.top_company AS "user.officeName",
            so.id AS "user.officeId"
		FROM work_attendance_record a
        LEFT JOIN sys_user u ON u.id = a.user_id
        LEFT JOIN sys_office so ON so.id = u.office_id
		WHERE a.office_id is NULL
		<if test="workAttendanceRule != null and workAttendanceRule.id != null and workAttendanceRule.id != ''">
			AND a.work_attendance_id = #{workAttendanceRule.id}
		</if>
		ORDER BY a.id ASC
	</select>
	
	<insert id="insert">
		INSERT INTO work_attendance_record(
			ID,
			work_attendance_id,
			USER_ID
		) VALUES (
			#{id},
			#{workAttendanceRule.id},
			#{user.id}
		)
	</insert>
	
	<insert id="insertAll" parameterType="List">
		INSERT INTO work_attendance_record(
			ID,
			work_attendance_id,
			USER_ID
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.workAttendanceRule.id},
				#{e.user.id}
			FROM dual
		</foreach>
	</insert>
	<insert id="insertOfficeAll" parameterType="List">
		INSERT INTO work_attendance_record(
			ID,
			work_attendance_id,
			OFFICE_ID
		)
		<foreach collection="list" item="e" separator=" UNION ALL ">
			SELECT
				#{e.id},
				#{e.workAttendanceRule.id},
				#{e.officeId}
			FROM dual
		</foreach>
	</insert>

	<update id="update">
		UPDATE work_attendance_record SET 	
			READ_DATE = #{readDate}
		WHERE work_attendance_id = #{workAttendanceRule.id}
			AND USER_ID = #{user.id}
	</update>
	
	<delete id="delete">
		DELETE FROM work_attendance_record  
		WHERE id = #{id}
	</delete>
	<delete id="deleteByRuleId">
		DELETE FROM work_attendance_record
		WHERE work_attendance_id = #{ruleId}
	</delete>
	<delete id="deleteByworkAttendanceRuleId">
		DELETE FROM work_attendance_record
		WHERE work_attendance_id = #{workAttendanceRule.id}
	</delete>

</mapper>