<?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.oa.dao.OaAttendanceDao">
    
	<sql id="oaAttendanceColumns">
		a.id AS "id",
		a.company_id AS "company.id",
		a.rule_id AS "rule.id",
		a.user_id AS "user.id",
		a.sign_date AS "signDate",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		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_id AS "office.id",
		company.name AS "company.name",
		office.name AS "office.name",
		rule.name AS "rule.name",
		user.name AS "user.name",
		rule.workdays AS "rule.workdays",
		rule.start_time AS "rule.startTime",
		rule.end_time AS "rule.endTime"
	</sql>
	
	<sql id="oaAttendanceJoins">
		LEFT JOIN sys_office company ON company.id = a.company_id
		LEFT JOIN sys_office office ON  office.id = a.office_id
		LEFT JOIN oa_attendance_rule rule ON rule.id = a.rule_id
		LEFT JOIN sys_user user ON user.id = a.user_id
	</sql>

	<select id="getOaAttendanceByCompanyIdAndUserIdAndSignDate" resultType="OaAttendance">
		SELECT <include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		WHERE a.del_flag = '0'
		AND a.company_id = #{companyId}
		AND a.user_id = #{userId}
		AND a.sign_date = #{signDate}
	</select>

	<select id="getOaAttendanceList" resultType="OaAttendance">
		SELECT <include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		WHERE  a.del_flag = '0'
		AND a.company_id = #{companyId}
		AND a.sign_date = #{signDate}
		limit 1
	</select>

	<select id="getCompanyList" resultType="string">
		SELECT DISTINCT company_id
		FROM oa_attendance a
		WHERE a.del_flag = '0'
	</select>
	
	<select id="getCreateDateList" resultType="string">
		SELECT create_date FROM oa_attendance
		WHERE  #{date} LIKE  LEFT (create_date,7)
		<if test="companyId !=null and companyId !=''">
			AND    company_id = #{companyId}
		</if>
		ORDER BY  create_date asc
	</select>
    
	<select id="get" resultType="OaAttendance" >
		SELECT 
			<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="OaAttendance" >
		SELECT
			<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="company != null and company.id != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
			<if test="user !=null and user.name != null and user.name != ''">
				AND user.name LIKE
				<if test="dbName == 'oracle'">'%'||#{user.name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{user.name}, '%')</if>
			</if>
            <if test="user != null and user.id != null and user.id != ''">
                AND a.user_id = #{user.id}
            </if>
			<if test="fromThisDay != null  and fromThisDay != ''">
				AND a.sign_date <![CDATA[>=]]> #{fromThisDay}
			</if>
			<if test="toThisDay !=null and toThisDay !=''">
				AND a.sign_date <![CDATA[<=]]> #{toThisDay}
			</if>
			<if test="signDate !=null and signDate !=''">
				AND a.sign_date = #{signDate}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sign_date DESC ,a.create_date ASC
			</otherwise>
		</choose>
	</select>

	<select id="findListByUser" resultType="OaAttendance" >
		SELECT
		<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="user != null and user.id != null and user.id != ''">
				AND a.user_id = #{user.id}
			</if>
			<if test="company != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
			<if test="user != null and user.name != null and user.name != ''">
				AND user.name LIKE
				<if test="dbName == 'oracle'">'%'||#{user.name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{user.name}, '%')</if>
			</if>
			<if test="signDate != null  and signDate != ''">
				AND a.sign_date = #{signDate}
			</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="findByCompany2" resultType="OaAttendance" parameterType="java.util.Map">
		SELECT
		<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		<where>
			a.del_flag = #{attendance.DEL_FLAG_NORMAL}

			<if test="date != null and date != ''">
				AND #{date} LIKE  LEFT (a.create_date,7)
			</if>
			<if test="attendance.company != null and attendance.company.id != ''">
				AND a.company_id = #{attendance.company.id}
			</if>
		</where>
		<choose>
			<when test="attendance.page !=null and attendance.page.orderBy != null and attendance.page.orderBy != ''">
				ORDER BY ${attendance.page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findByCompany" resultType="OaAttendance" >
		SELECT
		<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}

			<if test="createDate != null and createDate != ''">
				AND Date(a.create_date) = #{createDate}
			</if>
			<if test="company != null and company.id != ''">
				AND a.company_id = #{company.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="OaAttendance" >
		SELECT 
			<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		<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 oa_attendance(
			id,
			company_id,
			office_id,
			rule_id,
			user_id,
			sign_date,
			start_time,
			end_time,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{company.id},
			#{office.id},
			#{rule.id},
			#{user.id},
			#{signDate},
			#{startTime},
			#{endTime},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE oa_attendance SET 	
			company_id = #{company.id},
			office_id = #{office.id},
			rule_id = #{rule.id},
			user_id = #{user.id},
			sign_date = #{signDate},
			start_time = #{startTime},
			end_time = #{endTime},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM oa_attendance
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE oa_attendance SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="OaAttendance" statementType="STATEMENT">
		select * FROM oa_attendance  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByrule" resultType="OaAttendanceRule">
		SELECT 
			*
		FROM oa_attendance_rule a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id == #{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="findListByComapny" resultType="Office">
		SELECT 
			*
		FROM oa_attendance_rule a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
			AND a.id == #{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="getCreateDateList1" resultType="string">
		SELECT a.create_date FROM oa_attendance a
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	       and a.create_by=#{userId}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompany1" resultType="OaAttendance">
		SELECT
		<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="getCreateDateListAll" resultType="string">
		SELECT a.create_date
		FROM oa_attendance a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	      ${sqlStr}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompanyAll" resultType="OaAttendance">
		SELECT
		a.*
		FROM oa_attendance a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByCompanySelf" resultType="OaAttendance">
		SELECT
		<include refid="oaAttendanceColumns"/>
		FROM oa_attendance a
		<include refid="oaAttendanceJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByManage" resultType="OaAttendance">
		SELECT
		a.id AS "id",
		a.company_id AS "company.id",
		a.rule_id AS "rule.id",
		a.user_id AS "user.id",
		a.sign_date AS "signDate",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		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_id AS "office.id",
		s.name AS "company.name",
		o.name AS "office.name",
		rule.name AS "rule.name",
		u.name AS "user.name",
		rule.workdays AS "rule.workdays",
		rule.start_time AS "rule.startTime",
		rule.end_time AS "rule.endTime"
		FROM oa_attendance a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN oa_attendance_rule rule ON rule.id = a.rule_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>
</mapper>