<?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.LeaveDao">

	<sql id="leaveColumns">
		a.*,
		u.id AS "createBy.id",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
	</sql>

	<sql id="leaveJoins">
		JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = u.office_id
    </sql>

	<select id="get" resultType="Leave">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getCreateDateList" resultType="string">
		SELECT create_date FROM oa_leave
		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="getByProcessInstanceId" resultType="Leave">
        SELECT
        <include refid="leaveColumns"/>
        FROM oa_leave a
        <include refid="leaveJoins"/>
        WHERE a.process_instance_id = #{processInstanceId}
    </select>

	<select id="findList" resultType="Leave">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		<if test="createDateStart != null and createDateStart != ''">
			AND a.create_date &gt;= #{createDateStart}
		</if>
		<if test="createDateEnd != null and createDateEnd != ''">
			AND a.create_date &lt;= #{createDateEnd}
		</if>
		<if test="leaveType != null and leaveType != ''">
			AND a.leave_type = #{leaveType}
		</if>
		<if test="status != null and status != ''">
			AND a.status = #{status}
		</if>
        <if test="updateBy != null and updateBy.id != null and updateBy.id != ''">
            AND a.update_by = #{updateBy.id}
        </if>
        <if test="createBy != null and createBy.id != null and createBy.id != ''">
            AND a.create_by = #{createBy.id}
        </if>
		<if test="ids != null and ids != ''">
			AND a.id IN (${ids})
		</if>
		<if test="companyId != null and companyId != ''">
			AND a.company_id = #{companyId}
		</if>
		${sqlMap.dsf}
		ORDER BY a.update_date DESC
	</select>

	<select id="findAllList" resultType="Leave">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		ORDER BY a.update_date DESC
	</select>

	<insert id="insert">
		INSERT INTO oa_leave(
			id, 
			process_instance_id, 
			start_time, 
			end_time, 
			leave_days,
			leave_time,
			leave_type, 
			reason, 
			apply_time, 
			reality_start_time, 
			reality_end_time, 
			create_by, 
			create_date, 
			update_by, 
			update_date, 
			remarks, 
			del_flag,
			leave_Details,
			company_id,
			office_id,
			files,
			status
		) VALUES (
			#{id},
			#{processInstanceId},
			#{startTime},
			#{endTime},
			#{leaveDays},
			#{leaveTime},
			#{leaveType},
			#{reason},
			#{createDate},
			#{realityStartTime},
			#{realityEndTime},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{leaveDetails},
			#{companyId},
			#{officeId},
			#{files},
			#{status}
		)
	</insert>

	<update id="update">
		UPDATE oa_leave SET
		leave_type = #{leaveType},
		start_time = #{startTime},
		end_time = #{endTime},
		leave_days = #{leaveDays},
		leave_time = #{leaveTime},
		reason = #{reason},
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		remarks = #{remarks},
		del_flag=#{delFlag},
		leave_Details=#{leaveDetails},
		company_id=#{companyId},
		office_id = #{officeId},
		files=#{files},
		status=#{status}
		WHERE id = #{id}
	</update>

	<update id="updateRealityTime">
		UPDATE oa_leave SET
		reality_start_time = #{realityStartTime},
		reality_end_time = #{realityEndTime},
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		remarks = #{remarks}
		WHERE id = #{id}
	</update>

	<update id="updateProcessInstanceId">
		UPDATE oa_leave SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<update id="delete">
		DELETE FROM oa_leave
		WHERE id = #{id}
	</update>

	<update id="deleteByLogic">
		UPDATE oa_leave SET
		del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

	<select id="findByCompany2" resultType="Leave" parameterType="java.util.Map">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		<where>
			a.del_flag = #{leave.DEL_FLAG_NORMAL}
			<if test="date != null and date != ''">
				AND #{date} LIKE  LEFT (a.create_date , 7)
			</if>
			<if test="leave.companyId != null and leave.companyId != ''">
				AND a.company_id = #{leave.companyId}
			</if>
		</where>
		<choose>
			<when test="leave.page !=null and leave.page.orderBy != null and leave.page.orderBy != ''">
				ORDER BY ${leave.page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findByCompany" resultType="Leave" >
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="createDate != null and createDate != ''">
				AND Date(a.create_date) = #{createDate}
			</if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </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_leave 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="Leave">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		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_leave 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="Leave">
		SELECT
		a.*
		FROM oa_leave 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="Leave">
		SELECT
		<include refid="leaveColumns"/>
		FROM oa_leave a
		<include refid="leaveJoins"/>
		where
		a.del_flag = '0'
		<if test="time != null and time != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		</if>
		<if test="startDate != null and endDate != null and startDate != '' and endDate != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') BETWEEN #{startDate} AND #{endDate}
		</if>
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByManage" resultType="Leave">
		SELECT
		a.*,
		u.id AS "createBy.id",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
		FROM oa_leave 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'
		<if test="time != null and time != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		</if>
		<if test="startDate != null and endDate != null and startDate != '' and endDate != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') BETWEEN #{startDate} AND #{endDate}
		</if>
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>
</mapper>