<?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.leavetype.dao.LeaveTypeDao">
    
	<sql id="leaveTypeColumns">
		a.id AS "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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.branch_office AS "branchOffice",
		a.name AS "name",
		a.no AS "no",
		a.state AS "state",
		a.model AS "model",
		a.unit AS "unit",
		a.method AS "method",
		a.zero AS "zero",
		a.max_days AS "maxDays",
		a.is_attendance AS "isAttendance",
		a.is_subsidized AS "isSubsidized",
		a.is_next AS "isNext",
		a.set_days AS "setDays",
		a.is_sys AS "isSys",
		a.parent_id AS "parentId",
		a.parent_ids AS "parentIds",
		a.is_end AS "isEnd",
		b.name as "parentName",
		a.set_key as "key"
	</sql>
	
	<sql id="leaveTypeJoins">
		left join leave_type b on b.id=a.parent_id
	</sql>
	
    
	<select id="get" resultType="LeaveType" >
		SELECT 
			<include refid="leaveTypeColumns"/>
		FROM leave_type a
		<include refid="leaveTypeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="LeaveType" >
		SELECT 
			<include refid="leaveTypeColumns"/>
		FROM leave_type a
		<include refid="leaveTypeJoins"/>
		<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>
			<if test="no != null and no != ''">
				AND a.no LIKE 
					<if test="dbName == 'oracle'">'%'||#{no}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{no}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{no},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="model != null and model != ''">
				AND a.model = #{model}
			</if>
			<if test="unit != null and unit != ''">
				AND a.unit = #{unit}
			</if>
			<if test="method != null and method != ''">
				AND a.method = #{method}
			</if>
			<if test="zero != null and zero != ''">
				AND a.zero = #{zero}
			</if>
			<if test="maxDays != null and maxDays != ''">
				AND a.max_days = #{maxDays}
			</if>
			<if test="isAttendance != null and isAttendance != ''">
				AND a.is_attendance = #{isAttendance}
			</if>
			<if test="isSubsidized != null and isSubsidized != ''">
				AND a.is_subsidized = #{isSubsidized}
			</if>
			<if test="isNext != null and isNext != ''">
				AND a.is_next = #{isNext}
			</if>
			<if test="setDays != null and setDays != ''">
				AND a.set_days = #{setDays}
			</if>
			<if test="isSys != null and isSys != ''">
				AND a.is_sys = #{isSys}
			</if>
			<if test="isEnd != null and isEnd != ''">
				AND a.is_end = #{isEnd}
			</if>
		</where>
		ORDER BY a.parent_ids ASC,a.no asc
	</select>
	
	<select id="findAllList" resultType="LeaveType" >
		SELECT 
			<include refid="leaveTypeColumns"/>
		FROM leave_type a
		<include refid="leaveTypeJoins"/>
		<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 leave_type(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			branch_office,
			name,
			no,
			state,
			model,
			unit,
			method,
			zero,
			max_days,
			is_attendance,
			is_subsidized,
			is_next,
			set_days,
			is_sys,
			parent_id,
			parent_ids,
			is_end,
			set_key
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{branchOffice},
			#{name},
			#{no},
			#{state},
			#{model},
			#{unit},
			#{method},
			#{zero},
			#{maxDays},
			#{isAttendance},
			#{isSubsidized},
			#{isNext},
			#{setDays},
			#{isSys},
			#{parentId},
			#{parentIds},
			#{isEnd},
			#{key}
		)
	</insert>
	
	<update id="update">
		UPDATE leave_type SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			branch_office = #{branchOffice},
			name = #{name},
			no = #{no},
			model = #{model},
			unit = #{unit},
			method = #{method},
			zero = #{zero},
			max_days = #{maxDays},
			is_attendance = #{isAttendance},
			is_subsidized = #{isSubsidized},
			is_next = #{isNext},
			set_days = #{setDays},
			is_sys = #{isSys},
			parent_id = #{parentId},
			parent_ids = #{parentIds},
			is_end = #{isEnd}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM leave_type
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE leave_type SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LeaveType" statementType="STATEMENT">
		select * FROM leave_type  where ${propertyName} = '${value}'
	</select>
	<!-- 修改末级标志 -->
	<update id="upateEndFlag">
		UPDATE leave_type SET
			is_end = #{isEnd}
		WHERE id = #{id}
	</update>
	<update id="updateDelFlag">
		UPDATE leave_type SET
			state = #{flat}
		WHERE id = #{id}
	</update>
	<select id="getNameByIdAndKey" resultType="LeaveType" >
		SELECT
		<include refid="leaveTypeColumns"/>
		FROM leave_type a
		<include refid="leaveTypeJoins"/>
		WHERE a.set_key = #{key}
		and a.company_id=#{companyId}
	</select>

</mapper>