<?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.srs.routine.mapper.RtStuLeaveApplicationMapper">

    <resultMap type="RtStuLeaveApplication" id="RtStuLeaveApplicationResult">
        <result property="leaveApplicationId"    column="leave_application_id"    />
        <result property="stuId"    column="stu_id"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="reason"    column="reason"    />
        <result property="leaveType"    column="leave_type"    />
        <result property="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="leaveDays"    column="leave_days"    />
        <result property="destination"    column="destination"    />
        <result property="destinationDetails"    column="destination_details"    />
        <result property="safetyPromise"    column="safety_promise"    />
        <result property="applicantSignature"    column="applicant_signature"    />
        <result property="attachment"    column="attachment"    />
        <result property="counselorComments"    column="counselor_comments"    />
        <result property="cancellation"    column="cancellation"    />
        <result property="leaveStatus"    column="leave_status"    />
        <result property="leavingGx"    column="leaving_gx"    />
        <result property="status"    column="status"    />
        <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="remark"    column="remark"    />
        <result property="processInstanceId"    column="process_instance_id"    />
        <result property="deployId"    column="deploy_id"    />
        <result property="cancellationAddress"    column="cancellation_address"    />
        <result property="fatherName"    column="father_name"    />
        <result property="fatherRelation"    column="father_relation"    />
    </resultMap>

    <sql id="selectRtStuLeaveApplicationVo">
        select leave_application_id, stu_id, reason, leave_type, start_date, end_date, leave_days, destination, destination_details,
               safety_promise, applicant_signature, attachment, counselor_comments, cancellation, leave_status, leaving_gx, status,
               create_by, create_time, update_by, update_time, remark ,process_instance_id,cancellation_address,stu_no,deploy_id,father_name,father_relation from rt_stu_leave_application
    </sql>

    <select id="selectRtStuLeaveApplicationList" parameterType="RtStuLeaveApplication" resultMap="RtStuLeaveApplicationResult">
        <include refid="selectRtStuLeaveApplicationVo"/>
        <where>
            <if test="stuId != null "> and stu_id = #{stuId}</if>
            <if test="stuNo != null and stuNo !='' "> and stu_no = #{stuNo}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="leaveType != null  and leaveType != ''"> and leave_type = #{leaveType}</if>
            <if test="startDate != null "> and start_date = #{startDate}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
            <if test="leaveDays != null "> and leave_days = #{leaveDays}</if>
            <if test="destination != null  and destination != ''"> and destination = #{destination}</if>
            <if test="destinationDetails != null  and destinationDetails != ''"> and destination_details = #{destinationDetails}</if>
            <if test="safetyPromise != null  and safetyPromise != ''"> and safety_promise = #{safetyPromise}</if>
            <if test="applicantSignature != null  and applicantSignature != ''"> and applicant_signature = #{applicantSignature}</if>
            <if test="attachment != null  and attachment != ''"> and attachment = #{attachment}</if>
            <if test="counselorComments != null  and counselorComments != ''"> and counselor_comments = #{counselorComments}</if>
            <if test="cancellation != null "> and cancellation = #{cancellation}</if>
            <if test="leaveStatus != null  and leaveStatus != ''"> and leave_status = #{leaveStatus}</if>
            <if test="leavingGx != null  and leavingGx != ''"> and leaving_gx = #{leavingGx}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="selectRtStuLeaveApplicationByLeaveApplicationId" parameterType="Long" resultMap="RtStuLeaveApplicationResult">
        <include refid="selectRtStuLeaveApplicationVo"/>
        where leave_application_id = #{leaveApplicationId}
    </select>

    <select id="selectRtStuLeaveApplicationListByDto"
            resultType="com.srs.routine.domain.vo.StuLeaveApplicationVo">
        SELECT a.leave_application_id leaveApplicationId,b.stu_id stuId,b.stu_no stuNo,b.name,c.class_name className,a.reason,
               a.leave_type leaveType,a.start_date startDate,a.end_date endDate,a.leave_days leaveDays,a.leave_status leaveStatus,
               a.`status`,a.create_time createTime,a.cancellation,a.process_instance_id processInstanceId,a.deploy_id deployId  FROM rt_stu_leave_application a
          LEFT JOIN srs_student b ON a.stu_id=b.stu_id
          LEFT JOIN srs_class c ON b.class_id=c.class_id
          LEFT JOIN cph_teacher d ON c.teacher_id=d.teacher_id
        <where>
            a.leave_status='1'
            <if test="leaveApplicationId != null and leaveApplicationId != ''">AND a.leave_application_id = #{leaveApplicationId}</if>
            <if test="stuNo!=null and stuNo != '' ">AND b.stu_no = #{stuNo}</if>
            <if test="name != null and name != '' ">AND b.name = #{name}</if>
            <if test="className != null and className != '' ">AND c.class_name = #{className}</if>
            <if test="classId != null and classId != '' ">AND c.class_id = #{classId}</if>
            <if test="userName != null and userName != '' ">AND d.employee_id = #{userName}</if>
        </where>
        ORDER BY a.create_time desc, c.class_name desc
    </select>

    <select id="selectRtStuLeaveApplicationDetailsByLeaveApplicationId"
            resultType="com.srs.routine.domain.vo.StuLeaveApplicationDetailsVo">
        SELECT a.leave_application_id AS leaveApplicationId,
               b.stu_id AS stuId,
               b.stu_no AS stuNo,
               b.`name` AS studentName,
               b.gender AS gender,
               c.dept_name AS departmentName,
               e.class_name AS className,
               b.phone AS phoneNumber,
               a.father_name AS fatherName,
               a.father_relation AS fatherRelation,
               a.reason AS reason,
               a.leave_type AS leaveType,
               a.start_date AS startDate,
               a.end_date AS endDate,
               a.leave_days AS leaveDays,
               a.destination AS destination,
               a.destination_details AS destinationDetails,
               a.safety_promise AS safetyPromise,
               a.applicant_signature AS applicantSignature,
               a.attachment AS attachment,
               a.`status` AS status,
               a.leaving_gx AS leavingGx,
               a.cancellation AS cancellation,
               a.create_time AS createTime,
               a.process_instance_id AS processInstanceId,
               a.cancellation_address AS cancellationAddress
        FROM rt_stu_leave_application a
                 LEFT JOIN srs_student b ON a.stu_id = b.stu_id
                 LEFT JOIN srs_class e ON b.class_id = e.class_id
                 LEFT JOIN srs_majors d ON e.major_id = d.major_id
                 LEFT JOIN sys_dept c ON d.college_id = c.dept_id
        where a.leave_application_id = #{leaveApplicationId}
    </select>

    <select id="selectStuInfoReturn" parameterType="String" resultType="com.srs.routine.domain.vo.StuInfoReturnVo">
        SELECT
            b.stu_no AS stuNo,
            b.`name` AS studentName,
            b.gender AS gender,
            b.birthday AS birthday,
            h.grade_name AS gradeName,
            c.dept_name AS departmentName,
            e.class_name AS className,
            b.phone AS phoneNumber,
            f.father_name AS fatherName,
            f.father_relation AS fatherRelation,
            g.jg,g.hksz1,g.hksz2,g.hksz3,g.mz,
            j.signature assigneeSign
        FROM srs_student b
                 LEFT JOIN srs_class e ON b.class_id = e.class_id
                 LEFT JOIN srs_majors d ON e.major_id = d.major_id
                 LEFT JOIN sys_dept c ON d.college_id = c.dept_id
                 LEFT JOIN srs_parents_detal f ON b.stu_id = f.student_id
                 LEFT JOIN cph_stu_extra_info g ON b.stu_no=g.stu_no
                 LEFT JOIN srs_grade h ON e.grade_id=h.grade_id
                 LEFT JOIN sys_user j ON b.stu_no = j.user_name
        where b.stu_no  = #{stuNo}
    </select>

    <select id="getCounselorInfo" resultType="com.srs.common.doman.vo.TeacherVo">
        SELECT d.user_id userId, d.dept_id deptId, d.user_name userName, d.nick_name nickName
        FROM srs_student a
                 LEFT JOIN srs_class b ON a.class_id = b.class_id
                 LEFT JOIN cph_teacher c ON b.teacher_id = c.teacher_id
                 LEFT JOIN sys_user d ON c.employee_id = d.user_name
        WHERE a.stu_id = #{stuId}
    </select>

    <select id="selectLeaveApplicationByProcInsId"
            resultType="com.srs.routine.domain.vo.StuLeaveApplicationDetailsVo">
        SELECT a.leave_application_id AS leaveApplicationId,
               b.stu_id AS stuId,
               b.stu_no AS stuNo,
               b.`name` AS studentName,
               b.gender AS gender,
               c.dept_name AS departmentName,
               e.class_name AS className,
               b.phone AS phoneNumber,
               a.father_name AS fatherName,
               a.father_relation AS fatherRelation,
               a.reason AS reason,
               a.leave_type AS leaveType,
               a.start_date AS startDate,
               a.end_date AS endDate,
               a.leave_days AS leaveDays,
               a.destination AS destination,
               a.destination_details AS destinationDetails,
               a.safety_promise AS safetyPromise,
               a.applicant_signature AS applicantSignature,
               a.attachment AS attachment,
               a.`status` AS status,
               a.leaving_gx AS leavingGx,
               a.cancellation AS cancellation,
               a.create_time AS createTime,
               a.process_instance_id AS processInstanceId,
               a.cancellation_address AS cancellationAddress
        FROM rt_stu_leave_application a
                 LEFT JOIN srs_student b ON a.stu_id = b.stu_id
                 LEFT JOIN srs_class e ON b.class_id = e.class_id
                 LEFT JOIN srs_majors d ON e.major_id = d.major_id
                 LEFT JOIN sys_dept c ON d.college_id = c.dept_id
        where a.process_instance_id = #{processInstanceId}
    </select>

    <select id="selectClassLeaveNumber" resultType="com.srs.routine.domain.vo.ClassStuLeaveDataStatistics">
        SELECT a.class_id classId, a.class_name className,  COUNT(CASE WHEN d.leave_type = '1' THEN 1 END) AS personalLeaveCount,
           COUNT(CASE WHEN d.leave_type = '0' THEN 1 END) AS sickLeaveCount FROM srs_class a
         LEFT JOIN  cph_teacher b ON a.teacher_id=b.teacher_id
         LEFT JOIN srs_student c ON a.class_id = c.class_id
         LEFT JOIN rt_stu_leave_application d ON c.stu_no=d.create_by
        WHERE b.employee_id=#{username}
        GROUP BY a.class_id , a.class_name
    </select>

    <select id="selectRtStuLeaveApplicationListVo"
            resultType="com.srs.routine.domain.vo.StuLeaveApplicationVo">
        SELECT a.leave_application_id leaveApplicationId,b.stu_id stuId,b.stu_no stuNo,b.name,c.class_name className,a.reason, a.father_name AS fatherName,
        a.father_relation AS fatherRelation,a.leave_type leaveType,a.start_date startDate,a.end_date endDate,a.leave_days leaveDays,a.leave_status leaveStatus,
        a.`status`,a.create_time createTime,a.cancellation,a.process_instance_id processInstanceId,a.deploy_id deployId FROM rt_stu_leave_application a
        LEFT JOIN srs_student b ON a.stu_id=b.stu_id
        LEFT JOIN srs_class c ON b.class_id=c.class_id
        LEFT JOIN sys_user u ON b.stu_no=u.user_name
        <where>
            <if test="leaveApplicationId != null and leaveApplicationId != ''">AND a.leave_application_id = #{leaveApplicationId}</if>
            <if test="stuNo!=null and stuNo != '' ">AND b.stu_no = #{stuNo}</if>
            <if test="name != null and name != '' ">AND a.name = #{name}</if>
            <if test="classId != null and classId != '' ">AND c.class_id = #{classId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        ORDER BY a.create_time desc, c.class_name desc
    </select>

    <select id="getRtStuLeaveApplicationListByXW" resultType="com.srs.routine.domain.vo.StuLeaveApplicationVo">
        SELECT a.leave_application_id leaveApplicationId,b.stu_id stuId,b.stu_no stuNo,b.name,c.class_name className,a.reason, a.father_name AS fatherName,
        a.father_relation AS fatherRelation,a.leave_type leaveType,a.start_date startDate,a.end_date endDate,a.leave_days leaveDays,a.leave_status leaveStatus,
               a.`status`,a.create_time createTime,a.cancellation,a.process_instance_id processInstanceId,a.deploy_id deployId FROM rt_stu_leave_application a
            LEFT JOIN srs_student b ON a.stu_id=b.stu_id
            LEFT JOIN srs_class c ON b.class_id=c.class_id
            LEFT JOIN sys_user u ON b.stu_no=u.user_name
            LEFT JOIN cph_teacher f ON c.teacher_id = f.teacher_id
        <where>
            <if test="leaveApplicationId != null and leaveApplicationId != ''">AND a.leave_application_id = #{leaveApplicationId}</if>
            <if test="stuNo!=null and stuNo != '' ">AND b.stu_no = #{stuNo}</if>
            <if test="name != null and name != '' ">AND a.name = #{name}</if>
            <if test="classId != null and classId != '' ">AND c.class_id = #{classId}</if>
            <if test="deptId != null and deptId != '' ">AND f.dept_id = #{deptId}</if>
        </where>
        ORDER BY a.create_time desc, c.class_name desc
    </select>

    <insert id="insertRtStuLeaveApplication" parameterType="RtStuLeaveApplication" useGeneratedKeys="true" keyProperty="leaveApplicationId">
        insert into rt_stu_leave_application
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stuId != null">stu_id,</if>
            <if test="stuNo != null and stuNo != '' ">stu_no,</if>
            <if test="reason != null and reason != ''">reason,</if>
            <if test="leaveType != null and leaveType != ''">leave_type,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="leaveDays != null">leave_days,</if>
            <if test="destination != null and destination != ''">destination,</if>
            <if test="destinationDetails != null">destination_details,</if>
            <if test="safetyPromise != null and safetyPromise != ''">safety_promise,</if>
            <if test="applicantSignature != null and applicantSignature != ''">applicant_signature,</if>
            <if test="attachment != null">attachment,</if>
            <if test="counselorComments != null">counselor_comments,</if>
            <if test="cancellation != null">cancellation,</if>
            <if test="leaveStatus != null and leaveStatus != ''">leave_status,</if>
            <if test="leavingGx != null and leavingGx != ''">leaving_gx,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="processInstanceId != null">process_instance_id,</if>
            <if test="deployId != null">deploy_id,</if>
            <if test="cancellationAddress != null">cancellation_address,</if>
            <if test="fatherName != null">father_name,</if>
            <if test="fatherRelation != null">father_relation,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stuId != null">#{stuId},</if>
            <if test="stuNo != null and stuNo != '' ">#{stuNo},</if>
            <if test="reason != null and reason != ''">#{reason},</if>
            <if test="leaveType != null and leaveType != ''">#{leaveType},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="leaveDays != null">#{leaveDays},</if>
            <if test="destination != null and destination != ''">#{destination},</if>
            <if test="destinationDetails != null">#{destinationDetails},</if>
            <if test="safetyPromise != null and safetyPromise != ''">#{safetyPromise},</if>
            <if test="applicantSignature != null and applicantSignature != ''">#{applicantSignature},</if>
            <if test="attachment != null">#{attachment},</if>
            <if test="counselorComments != null">#{counselorComments},</if>
            <if test="cancellation != null">#{cancellation},</if>
            <if test="leaveStatus != null and leaveStatus != ''">#{leaveStatus},</if>
            <if test="leavingGx != null and leavingGx != ''">#{leavingGx},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="processInstanceId != null">#{processInstanceId},</if>
            <if test="deployId != null">#{deployId},</if>
            <if test="cancellationAddress != null">#{cancellationAddress},</if>
            <if test="fatherName != null">#{fatherName},</if>
            <if test="fatherRelation != null">#{fatherRelation},</if>
         </trim>
    </insert>

    <update id="updateRtStuLeaveApplication" parameterType="RtStuLeaveApplication">
        update rt_stu_leave_application
        <trim prefix="SET" suffixOverrides=",">
            <if test="stuId != null">stu_id = #{stuId},</if>
            <if test="stuNo != null and stuNo != '' ">stu_no = #{stuNo},</if>
            <if test="reason != null and reason != ''">reason = #{reason},</if>
            <if test="leaveType != null and leaveType != ''">leave_type = #{leaveType},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="leaveDays != null">leave_days = #{leaveDays},</if>
            <if test="destination != null and destination != ''">destination = #{destination},</if>
            <if test="destinationDetails != null">destination_details = #{destinationDetails},</if>
            <if test="safetyPromise != null and safetyPromise != ''">safety_promise = #{safetyPromise},</if>
            <if test="applicantSignature != null and applicantSignature != ''">applicant_signature = #{applicantSignature},</if>
            <if test="attachment != null">attachment = #{attachment},</if>
            <if test="counselorComments != null">counselor_comments = #{counselorComments},</if>
            <if test="cancellation != null">cancellation = #{cancellation},</if>
            <if test="leaveStatus != null and leaveStatus != ''">leave_status = #{leaveStatus},</if>
            <if test="leavingGx != null and leavingGx != ''">leaving_gx = #{leavingGx},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="processInstanceId != null and processInstanceId != '' ">process_instance_id = #{processInstanceId},</if>
            <if test="deployId != null and deployId != '' ">deploy_id = #{deployId},</if>
            <if test="cancellationAddress != null">cancellation_address = #{cancellationAddress},</if>
            <if test="fatherName != null">father_name = #{fatherName},</if>
            <if test="fatherRelation != null">father_relation = #{fatherRelation},</if>
        </trim>
        where leave_application_id = #{leaveApplicationId}
    </update>

    <delete id="deleteRtStuLeaveApplicationByLeaveApplicationId" parameterType="Long">
        delete from rt_stu_leave_application where leave_application_id = #{leaveApplicationId}
    </delete>

    <delete id="deleteRtStuLeaveApplicationByLeaveApplicationIds" parameterType="String">
        delete from rt_stu_leave_application where leave_application_id in
        <foreach item="leaveApplicationId" collection="array" open="(" separator="," close=")">
            #{leaveApplicationId}
        </foreach>
    </delete>
</mapper>
