<?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.WorkAttendanceRuleDao">
    
	<sql id="workAttendanceRuleColumns">
		a.id AS "id",
		a.company_id AS "companyId",
		a.branch_office AS "branchOffice.id",
		b.name AS "branchOffice.name",
		a.type AS "type",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		a.work_count AS "workCount",
		a.work_need AS "workNeed",
		a.work_type AS "workType",
		a.work_start_time AS "workStartTime",
		a.work_end_time AS "workEndTime",
		a.not_work_count AS "notWorkCount",
		a.not_work_need AS "notWorkNeed",
		a.not_work_type AS "notWorkType",
		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.number AS "number",
		a.del_flag AS "delFlag",
		a.leave_type AS "leaveType",
		a.name AS "name"
	</sql>
	
	<sql id="workAttendanceRuleJoins">
		LEFT JOIN sys_office b ON b.id = a.branch_office
	</sql>
	
    
	<select id="get" resultType="WorkAttendanceRule" >
		SELECT 
			<include refid="workAttendanceRuleColumns"/>
		FROM work_attendance_rule a
		<include refid="workAttendanceRuleJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="findAttendanceRuleByCompany" resultType="WorkAttendanceRule" >
		SELECT
			<include refid="workAttendanceRuleColumns"/>
		FROM work_attendance_rule a
		<include refid="workAttendanceRuleJoins"/>
		LEFT JOIN work_attendance_record r ON r.work_attendance_id = a.id
		WHERE
		a.del_flag = '0'
		AND a.company_id = #{companyId}
		<if test="userId != null and userId != ''">
			AND r.user_id = #{userId}
		</if>
		<if test="officeId != null and officeId != ''">
			AND r.office_id = #{officeId}
		</if>
	</select>


	<select id="findList" resultType="WorkAttendanceRule" >
		SELECT 
			<include refid="workAttendanceRuleColumns"/>
		FROM work_attendance_rule a
		<include refid="workAttendanceRuleJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<choose>
				<when test="branchOffice !=null and branchOffice.id != null and branchOffice.id != ''">
					AND a.branch_office = #{branchOffice.id}
				</when>
				<otherwise>
					AND (b.id =  #{branchOfficeId} or find_in_set(#{branchOfficeId},b.parent_ids))
				</otherwise>
			</choose>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.number ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkAttendanceRule" >
		SELECT 
			<include refid="workAttendanceRuleColumns"/>
		FROM work_attendance_rule a
		<include refid="workAttendanceRuleJoins"/>
		<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.number ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_attendance_rule(
			id,
			company_id,
			branch_office,
			type,
			start_time,
			end_time,
			work_count,
			work_need,
			work_type,
			work_start_time,
			work_end_time,
			not_work_count,
			not_work_need,
			not_work_type,
			create_by,
			create_date,
			update_by,
			update_date,
			number,
			remarks,
			leave_type,
			del_flag,
			name
		) VALUES (
			#{id},
			#{companyId},
			#{branchOffice.id},
			#{type},
			#{startTime},
			#{endTime},
			#{workCount},
			#{workNeed},
			#{workType},
			#{workStartTime},
			#{workEndTime},
			#{notWorkCount},
			#{notWorkNeed},
			#{notWorkType},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{number},
			#{remarks},
			#{leaveType},
			#{delFlag},
			#{name}
		)
	</insert>
	
	<update id="update">
		UPDATE work_attendance_rule SET 	
			company_id = #{companyId},
			branch_office = #{branchOffice.id},
			type = #{type},
			start_time = #{startTime},
			end_time = #{endTime},
			work_count = #{workCount},
			work_need = #{workNeed},
			work_type = #{workType},
			work_start_time = #{workStartTime},
			work_end_time = #{workEndTime},
			not_work_count = #{notWorkCount},
			not_work_need = #{notWorkNeed},
			not_work_type = #{notWorkType},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			number = #{number},
			leave_type = #{leaveType},
			name = #{name}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_attendance_rule
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_attendance_rule SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAttendanceRule" statementType="STATEMENT">
		select * FROM work_attendance_rule  where ${propertyName} = '${value}'
	</select>
	
</mapper>