<?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.WorkAttendanceMonthDao">
    
	<sql id="workAttendanceMonthColumns">
		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.staff_id AS "staffId",
		a.is_attendance AS "isAttendance",
		a.answer_attendance_day AS "answerAttendanceDay",
		a.real_attendance_day AS "realAttendanceDay",
		a.internal_duty AS "internalDuty",
		a.field_personnel AS "fieldPersonnel",
		a.go_out AS "goOut",
		a.over_time AS "overTime",
		a.miner AS "miner",
		a.late AS "late",
		a.early AS "early",
		a.leave_info AS "leave",
		a.attendance_month AS "attendanceMonth",
		office.name AS "office.name",
		sa.no AS "workStaffBasicInfo.no",
		sa.name AS "workStaffBasicInfo.name"
	</sql>
	
	<sql id="workAttendanceMonthJoins">
		LEFT JOIN sys_office office ON office.id = a.office_id
		LEFT JOIN work_staff_achives sa on sa.id = a.staff_id
	</sql>
	
    
	<select id="get" resultType="WorkAttendanceMonth" >
		SELECT 
			<include refid="workAttendanceMonthColumns"/>
		FROM work_attendance_month a
		<include refid="workAttendanceMonthJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkAttendanceMonth" >
		SELECT 
			<include refid="workAttendanceMonthColumns"/>
		FROM work_attendance_month a
		<include refid="workAttendanceMonthJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="branchOffice != null and branchOffice != ''">
				AND office.branch_office = #{branchOffice}
			</if>
			<if test="attendanceMonth != null and attendanceMonth != ''">
				AND a.attendance_month = #{attendanceMonth}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</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="findAllList" resultType="WorkAttendanceMonth" >
		SELECT 
			<include refid="workAttendanceMonthColumns"/>
		FROM work_attendance_month a
		<include refid="workAttendanceMonthJoins"/>
		<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_month(
			id,
			company_id,
			office_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			staff_id,
			is_attendance,
			answer_attendance_day,
			real_attendance_day,
			internal_duty,
			field_personnel,
			go_out,
			over_time,
			miner,
			late,
			early,
			attendance_month,
			leave_info
		) VALUES (
			#{id},
			#{companyId},
			#{office.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{staffId},
			#{isAttendance},
			#{answerAttendanceDay},
			#{realAttendanceDay},
			#{internalDuty},
			#{fieldPersonnel},
			#{goOut},
			#{overTime},
			#{miner},
			#{late},
			#{early},
			#{attendanceMonth},
			#{leave}
		)
	</insert>
	
	<update id="update">
		UPDATE work_attendance_month SET 	
			company_id = #{companyId},
			office_id = #{office.id},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			staff_id = #{staffId},
			is_attendance = #{isAttendance},
			answer_attendance_day = #{answerAttendanceDay},
			real_attendance_day = #{realAttendanceDay},
			internal_duty = #{internalDuty},
			field_personnel = #{fieldPersonnel},
			go_out = #{goOut},
			over_time = #{overTime},
			miner = #{miner},
			late = #{late},
			early = #{early},
			attendance_month = #{attendanceMonth},
			leave_info = #{leave}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_attendance_month
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="deleteByMonth">
		DELETE FROM work_attendance_month
		WHERE attendance_month = #{attendanceMonth} AND office_id = #{office.id} AND company_id = #{companyId}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_attendance_month SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAttendanceMonth" statementType="STATEMENT">
		select * FROM work_attendance_month  where ${propertyName} = '${value}'
	</select>
	
</mapper>