<?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.WorkAttendanceCountDao">
    
	<sql id="workAttendanceCountColumns">
		a.id AS "id",
		a.company_id AS "companyId",
		a.office_id AS "office.id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.office_count AS "officeCount",
		a.attendance_count AS "attendanceCount",
		a.attendance_month AS "attendanceMonth",
		a.attendance_end_date AS "attendanceEndDate",
		a.attendance_date AS "attendanceDate",
		a.user_id AS "user.id",
		a.status AS "status",
		office.name AS "office.name",
		user.name AS "user.name"
	</sql>
	
	<sql id="workAttendanceCountJoins">
		LEFT JOIN sys_office office ON office.id = a.office_id
		LEFT JOIN sys_user user ON user.id = a.user_id
	</sql>
	
    
	<select id="get" resultType="WorkAttendanceCount" >
		SELECT 
			<include refid="workAttendanceCountColumns"/>
		FROM work_attendance_count a
		<include refid="workAttendanceCountJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkAttendanceCount" >
		SELECT 
			<include refid="workAttendanceCountColumns"/>
		FROM work_attendance_count a
		<include refid="workAttendanceCountJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<choose>
				<when test="office != null and office.id != null and office.id != ''">
					AND a.office_id = #{office.id}
				</when>
				<otherwise>
					AND (office.id =  #{branchOffice} or find_in_set(#{branchOffice},office.parent_ids))
				</otherwise>
			</choose>

			<if test="attendanceMonth != null and attendanceMonth != ''">
				AND a.attendance_month = #{attendanceMonth}
			</if>

		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findListByMonth" resultType="WorkAttendanceCount" >
		SELECT
			<include refid="workAttendanceCountColumns"/>
		FROM work_attendance_count a
		<include refid="workAttendanceCountJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="attendanceMonth != null and attendanceMonth != ''">
				AND a.attendance_month = #{attendanceMonth}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="groupByOfficeList" resultType="WorkAttendanceCount" >
		SELECT
            a.office_id AS "office.id",
            count(*) AS "officeCount",
            a.company_id AS "companyId"
		FROM work_staff_achives a
		<where>
			a.del_flag = '0'
			AND a.company_id = #{company.id}
			AND (a.workEnd_date >=  #{workEndDate} or a.workEnd_date is NULL)
		</where>
		GROUP BY a.office_id
	</select>
	
	<select id="findAllList" resultType="WorkAttendanceCount" >
		SELECT 
			<include refid="workAttendanceCountColumns"/>
		FROM work_attendance_count a
		<include refid="workAttendanceCountJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_attendance_count(
			id,
			company_id,
			office_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_count,
			attendance_count,
			attendance_month,
			attendance_end_date,
			attendance_date,
			status,
			user_id
		) VALUES (
			#{id},
			#{companyId},
			#{office.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeCount},
			#{attendanceCount},
			#{attendanceMonth},
			#{attendanceEndDate},
			#{attendanceDate},
			#{status},
			#{user.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_attendance_count SET 	
			company_id = #{companyId},
			office_id = #{office.id},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_count = #{officeCount},
			attendance_count = #{attendanceCount},
			attendance_month = #{attendanceMonth},
			attendance_end_date = #{attendanceEndDate},
			attendance_date = #{attendanceDate},
			status = #{status},
			user_id = #{user.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_attendance_count
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_attendance_count SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAttendanceCount" statementType="STATEMENT">
		select * FROM work_attendance_count  where ${propertyName} = '${value}'
	</select>
	
</mapper>