<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.business.dao.YearRequestForLeaveInfoDAO">
	<resultMap id="YearRequestForLeaveInfoMap" type="com.jpsoft.shinestar.modules.business.entity.YearRequestForLeaveInfo">
		<id property="id" column="id_" />
			<result property="startTime" column="start_time" />
			<result property="endTime" column="end_time" />
			<result property="imageUrl" column="image_url" />
			<result property="reason" column="reason_" />
			<result property="personId" column="person_id" />
			<result property="personPopedomId" column="person_popedom_id" />
			<result property="remark" column="remark_" />
			<result property="createBy" column="create_by" />
			<result property="createTime" column="create_time" />
			<result property="updateBy" column="update_by" />
			<result property="updateTime" column="update_time" />
			<result property="delFlag" column="del_flag" />
			<result property="status" column="status_" />
			<result property="faceImageUrl" column="face_image_url"/>
			<result property="studentName" column="student_name"/>
			<result property="idCard" column="id_card"/>
			<result property="classId" column="class_company_id"/>
			<result property="className" column="class_company_name"/>
			<result property="approvalExplain" column="approval_explain" />
			<result property="approvedTime" column="approved_time" />
			<result property="startTimeQuantum" column="start_time_quantum" />
			<result property="endTimeQuantum" column="end_time_quantum" />
			<result property="approvalPersonIdStr" column="approval_person_id_str" />
			<result property="approvalStatusStr" column="approval_status_str" />
			<result property="approvalTimeStr" column="approval_time_str" />
			<result property="approvalContentStr" column="approval_content_str" />
			<result property="approvedPersonPopedomId" column="approved_person_popedom_id" />
			<result property="days" column="days_" />
			<result property="procinstId" column="procinst_id" />
			</resultMap>
	<insert id="insert" parameterType="com.jpsoft.shinestar.modules.business.entity.YearRequestForLeaveInfo">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into business_year_request_for_leave_info
	    (id_,start_time,end_time,image_url,reason_,person_id,person_popedom_id,remark_,create_by,create_time,update_by,update_time,del_flag,status_,approval_explain,approved_time,start_time_quantum,end_time_quantum,approval_person_id_str,approval_status_str,approval_time_str,approval_content_str,approved_person_popedom_id,days_,procinst_id)
		values
		(
#{id,jdbcType=VARCHAR}
,#{startTime,jdbcType= TIMESTAMP }
,#{endTime,jdbcType= TIMESTAMP }
,#{imageUrl,jdbcType=VARCHAR}
,#{reason,jdbcType=VARCHAR}
,#{personId,jdbcType= NUMERIC }
,#{personPopedomId,jdbcType=VARCHAR}
,#{remark,jdbcType=VARCHAR}
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
,#{status,jdbcType= NUMERIC }
,#{approvalExplain,jdbcType=VARCHAR}
,#{approvedTime,jdbcType= TIMESTAMP }
,#{startTimeQuantum,jdbcType=VARCHAR}
,#{endTimeQuantum,jdbcType=VARCHAR}
,#{approvalPersonIdStr,jdbcType=VARCHAR}
,#{approvalStatusStr,jdbcType=VARCHAR}
,#{approvalTimeStr,jdbcType=VARCHAR}
,#{approvalContentStr,jdbcType=VARCHAR}
,#{approvedPersonPopedomId,jdbcType=VARCHAR}
,#{days,jdbcType=VARCHAR}
,#{procinstId,jdbcType=VARCHAR}
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from business_year_request_for_leave_info where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="com.jpsoft.shinestar.modules.business.entity.YearRequestForLeaveInfo">
		update business_year_request_for_leave_info
		<set>
				<if test="startTime!=null">
		start_time=#{startTime,jdbcType= TIMESTAMP },
		</if>
				<if test="endTime!=null">
		end_time=#{endTime,jdbcType= TIMESTAMP },
		</if>
				<if test="imageUrl!=null">
		image_url=#{imageUrl,jdbcType=VARCHAR},
		</if>
				<if test="reason!=null">
		reason_=#{reason,jdbcType=VARCHAR},
		</if>
				<if test="personId!=null">
		person_id=#{personId,jdbcType= NUMERIC },
		</if>
				<if test="personPopedomId!=null">
		person_popedom_id=#{personPopedomId,jdbcType=VARCHAR},
		</if>
				<if test="remark!=null">
		remark_=#{remark,jdbcType=VARCHAR},
		</if>
				<if test="createBy!=null">
		create_by=#{createBy,jdbcType=VARCHAR},
		</if>
				<if test="createTime!=null">
		create_time=#{createTime,jdbcType= TIMESTAMP },
		</if>
				<if test="updateBy!=null">
		update_by=#{updateBy,jdbcType=VARCHAR},
		</if>
				<if test="updateTime!=null">
		update_time=#{updateTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</if>
				<if test="status!=null">
		status_=#{status,jdbcType= NUMERIC },
		</if>
				<if test="approvalExplain!=null">
		approval_explain=#{approvalExplain,jdbcType=VARCHAR},
		</if>
				<if test="approvedTime!=null">
		approved_time=#{approvedTime,jdbcType= TIMESTAMP },
		</if>
				<if test="startTimeQuantum!=null">
		start_time_quantum=#{startTimeQuantum,jdbcType=VARCHAR},
		</if>
				<if test="endTimeQuantum!=null">
		end_time_quantum=#{endTimeQuantum,jdbcType=VARCHAR},
		</if>
				<if test="approvalPersonIdStr!=null">
		approval_person_id_str=#{approvalPersonIdStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalStatusStr!=null">
		approval_status_str=#{approvalStatusStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalTimeStr!=null">
		approval_time_str=#{approvalTimeStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalContentStr!=null">
		approval_content_str=#{approvalContentStr,jdbcType=VARCHAR},
		</if>
				<if test="approvedPersonPopedomId!=null">
		approved_person_popedom_id=#{approvedPersonPopedomId,jdbcType=VARCHAR},
		</if>
				<if test="days!=null">
		days_=#{days,jdbcType=VARCHAR},
		</if>
				<if test="procinstId!=null">
		procinst_id=#{procinstId,jdbcType=VARCHAR},
		</if>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="YearRequestForLeaveInfoMap">
		select * from business_year_request_for_leave_info where id_=#{0}
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from business_year_request_for_leave_info where id_=#{0}
	</select>
	<select id="list" resultMap="YearRequestForLeaveInfoMap">
		select * from business_year_request_for_leave_info
	</select>
	<select id="findByPersonIdAndDate" resultMap="YearRequestForLeaveInfoMap">
		<!--考勤时间需要大于出差开始时间，避免从12点请到24点时，把上午的下班卡算进去-->
		<![CDATA[
            SELECT a.* FROM
            business_year_request_for_leave_info a
            LEFT JOIN base_person_popedom b ON a.person_popedom_id = b.id_ AND b.del_flag = 0
            where
            b.person_id=#{personId}
            and
            a.start_time<#{attendanceDate}
            and
            (a.end_time is null or a.end_time>=#{attendanceDate})
            and
            a.status_= 1
            and
            a.del_flag=0
            order by a.create_time desc limit 1
        ]]>
	</select>
	<select id="search" parameterType="hashmap" resultMap="YearRequestForLeaveInfoMap">
		<![CDATA[
			select a.*,
			b.face_image_url,
			b.name_ as student_name,
			b.id_card as id_card,
			d.id_ as class_company_id,
			d.name_ as class_company_name
			from business_year_request_for_leave_info a
			inner join base_person_info b on a.person_id = b.id_
			inner join base_company_info d on b.company_id = d.id_
		]]>
		<where>
			a.del_flag = false
			<if test='searchParams.name!=null'>
				and b.name_ like #{searchParams.name}
			</if>
			<if test='searchParams.status!=null'>
				and a.status_=#{searchParams.status}
			</if>
			<if test="searchParams.studentPersonID != null">
				and a.student_person_id=#{searchParams.studentPersonID}
			</if>
			<if test="searchParams.teacherPersonID != null">
				and a.teacher_person_id=#{searchParams.teacherPersonID}
			</if>
			<if test='searchParams.classId!=null'>
				and b.company_id = #{searchParams.classId}
			</if>
			<if test='searchParams.leaveType!=null'>
				and a.leave_type = #{searchParams.leaveType}
			</if>
			<if test='searchParams.leaveTypeSt!=null'>
				<![CDATA[
					and (CAST(a.days_ AS DECIMAL(10,6)) >= 4)
				]]>
			</if>
			<if test='searchParams.requestStartTime!=null'>
				<![CDATA[
                    and a.create_time >= #{searchParams.requestStartTime}
                ]]>
			</if>
			<if test='searchParams.requestEndTime!=null'>
				<![CDATA[
                    and a.create_time <= #{searchParams.requestEndTime}
                ]]>
			</if>
			<if test='searchParams.startTime!=null'>
				<![CDATA[
                    and a.start_time <= #{searchParams.startTime}
                ]]>
			</if>
			<if test='searchParams.endTime!=null'>
				<![CDATA[
                    and a.end_time >= #{searchParams.endTime}
                ]]>
			</if>
			<if test='searchParams.endTime1!=null'>
				<![CDATA[
                    and a.end_time >= #{searchParams.endTime1}
                ]]>
			</if>
			<if test='searchParams.endTime2!=null'>
				<![CDATA[
                    and a.end_time <= #{searchParams.endTime2}
                ]]>
			</if>

			<if test='searchParams.auditStartTime!=null'>
				<![CDATA[
                    and b.update_time >= #{searchParams.auditStartTime}
                ]]>
			</if>
			<if test='searchParams.requestEndTime!=null'>
				<![CDATA[
                    and a.update_time <= #{searchParams.requestEndTime}
                ]]>
			</if>
			<if test="searchParams.companyCode != null">
				and d.code_ like #{searchParams.companyCode}
			</if>
			<if test="searchParams.hqCompanyCode != null">
				and d.code_ like #{searchParams.hqCompanyCode}
			</if>
			<if test="searchParams.hqStatus != null">
				and b.hq_status = #{searchParams.hqStatus}
			</if>
			<if test="searchParams.hqStatus == null and searchParams.hqPerson != null">
				<![CDATA[
					and ifnull(b.hq_status,0)<>0
				]]>
			</if>
			<if test="searchParams.personInfoList != null ">
				<foreach collection="searchParams.personInfoList" item="item" index="index" open="and b.id_ in(" separator="," close=")">
					#{item.id}
				</foreach>
			</if>
		</where>
		<foreach item="sort" collection="sortList" open="order by" separator=",">
			a.${sort.name} ${sort.order}
		</foreach>
	</select>

	<select id="mobileSearch" parameterType="hashmap" resultMap="YearRequestForLeaveInfoMap">
		<![CDATA[
			select a.*
			from
			business_year_request_for_leave_info a
			left join  base_person_popedom b on a.person_popedom_id = b.id_
			left join base_person_info c on a.person_id = c.id_
		]]>
		<where>
			a.del_flag = false
			and b.del_flag = false
			and c.del_flag = false
			<if test='searchParams.personId!=null'>
				and a.person_id =#{searchParams.personId}
			</if>
			<if test='searchParams.approvedIdList!=null'>
				<foreach collection="searchParams.approvedIdList" index="index" item="item" open="and a.approved_id in(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test='searchParams.type!=null'>
				<if test='searchParams.type == 0'>
					and a.status_ = 0
				</if>
				<if test='searchParams.type == 1'>
					and a.status_ != 0
				</if>
			</if>
			<if test="searchParams.approvalPersonPopedomId != null">
				and a.approved_person_popedom_id = #{searchParams.approvalPersonPopedomId}
			</if>
			<if test="searchParams.inCompanyInfo != null">
				<foreach collection="searchParams.inCompanyInfo" item="companyInfo"  open="and b.company_id in (" separator="," close=")">
					#{companyInfo.id}
				</foreach>
			</if>
		</where>
		<foreach item="sort" collection="sortList" open="order by" separator=",">
			${sort.name} ${sort.order}
		</foreach>
	</select>
    <select id="findByPersonIdAndSEDate" resultMap="YearRequestForLeaveInfoMap">
        <![CDATA[
            SELECT a.* FROM
            business_year_request_for_leave_info a
            LEFT JOIN base_person_popedom b ON a.person_popedom_id = b.id_ AND b.del_flag = 0
            where
            b.person_id=#{personId}
            and
            a.start_time<=#{endDate}
            and
            a.end_time>=#{startDate}
            and
            a.status_=1
            and
            a.del_flag=0
            order by a.create_time desc
        ]]>
    </select>
</mapper>