<?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.leaveapply.dao.LeaveCountDao">
    
	<sql id="leaveCountColumns">
		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.staff_id AS "staffId",
		a.reset_state as "resetState",
		a.leave_type_id AS "leaveTypeId",
		a.counts AS "counts",
		a.sums AS "sums",
		a.up_days AS "upDays",
		a.year AS "year",
		a.reset_date AS "resetDate",
		l.name as "leaveType.name",
		l.method as "leaveType.method",
		l.zero as "leaveType.zero"
	</sql>
	
	<sql id="leaveCountJoins">
		left join leave_type l on l.id=a.leave_type_id
	</sql>
	
    
	<select id="get" resultType="LeaveCount" >
		SELECT 
			<include refid="leaveCountColumns"/>
		FROM leave_count a
		<include refid="leaveCountJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="LeaveCount" >
		SELECT 
			<include refid="leaveCountColumns"/>
		FROM leave_count a
		<include refid="leaveCountJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="staffId != null and staffId != ''">
				AND a.staff_id = #{staffId}
			</if>
			<if test="leaveTypeId != null and leaveTypeId != ''">
				AND a.leave_type_id = #{leaveTypeId}
			</if>
			<if test="year != null and year != ''">
				AND a.year = #{year}
			</if>
			<if test="resetState != null and resetState != ''">
				AND a.reset_state = #{resetState}
			</if>
		</where>
	</select>
	
	<select id="findAllList" resultType="LeaveCount" >
		SELECT 
			<include refid="leaveCountColumns"/>
		FROM leave_count a
		<include refid="leaveCountJoins"/>
		<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_count(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			staff_id,
			leave_type_id,
			counts,
			sums,
			reset_date,
			reset_state,
			up_days,
			year
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{staffId},
			#{leaveTypeId},
			#{counts},
			#{sums},
			#{resetDate},
			#{resetState},
			#{upDays},
			#{year}
		)
	</insert>
	
	<update id="update">
		UPDATE leave_count SET
			counts = #{counts},
			sums = #{sums},
			update_date = #{updateDate}
		WHERE id = #{id}
	</update>
	<update id="updateReset" parameterType="java.lang.String">
		UPDATE leave_count SET
			counts ='0',
			sums = '0',
		    reset_date=now(),
		    reset_state='1',
		    update_date=now()
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM leave_count
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE leave_count SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LeaveCount" statementType="STATEMENT">
		select * FROM leave_count  where ${propertyName} = '${value}'
	</select>
	
</mapper>