<?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.qmth.exam.reserve.dao.StudentApplyDao">
    <select id="page"  resultType="com.qmth.exam.reserve.bean.stdapply.StudentApplyVO">
        SELECT
        a.id,
        a.time_period_id timePeriodId,
        s.NAME,
        s.identity_number identityNumber,
        s.student_code studentCode,
        p.start_time startTime,
        p.end_time endTime,
        a.cancel,
        a.update_time updateTime,
        a.seat_number seatNumber
        FROM
        t_student_apply a,
        t_student s,
        t_time_period p
        WHERE
        a.cancel = 0
        AND a.student_id = s.id
        AND a.time_period_id = p.id
        <if test="req.teachingId != null">
            and a.exam_site_id in
            <foreach collection="req.examSiteIds" item="item" index="index" separator="," open="(" close=")">
           	 	#{item}
        	</foreach>
        </if>
        <if test="req.agentId != null">
            and a.exam_site_id=#{req.agentId}
        </if>
        <if test="req.name != null and req.name !=''">
            and s.name like concat('%',#{req.name},'%')
        </if>
        <if test="req.identityNumber != null and req.identityNumber !=''">
            and s.identity_number like concat('%',#{req.identityNumber},'%')
        </if>
        <if test="req.studentCode != null and req.studentCode !=''">
            and s.student_code like concat('%',#{req.studentCode},'%')
        </if>
        <if test="req.startDate != null and req.startDate !=''">
            and p.start_time&gt;=#{req.startDate}
        </if>
        <if test="req.endDate != null and req.endDate !=''">
            and p.start_time&lt;= #{req.endDate}
        </if>
        <if test="req.taskId != null and req.taskId !=''">
            and s.apply_task_id=#{req.taskId}
        </if>
        order by a.update_time desc
    </select>

    <select id="getHaveApplyCount" resultType="int">
        select count(sa.id) from t_student_apply sa, t_exam_site s where sa.exam_site_id=s.id and sa.cancel=0 and
        sa.exam_site_id in
        <foreach collection="examSiteIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        and sa.time_period_id in
        <foreach collection="timePeriodIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="countApplyFinishForExamSiteAndTimePeriod" resultType="java.lang.Integer">
        select count(1)
        from t_student_apply
        where exam_site_id = #{examSiteId}
          and time_period_id = #{timePeriodId}
          and cancel = 0
    </select>

    <select id="getStudentApplyList" resultType="com.qmth.exam.reserve.bean.apply.ApplyVO">
        select sa.id applyId,sa.cancel,sa.ticket_number,
        sa.time_period_id,tp.start_time timePeriodStart,tp.end_time timePeriodEnd,
        sa.exam_site_id,es.name examSiteName,es.address examSiteAddress,
        c.id categoryId,c.name categoryName
        from t_student_apply sa
        inner join t_time_period tp on tp.id=sa.time_period_id
        inner join t_exam_site es on es.id=sa.exam_site_id
        inner join t_category c on c.id=es.category_id
        where sa.student_id = #{studentId}
        <if test="cancel != null">
            and sa.cancel = #{cancel}
        </if>
    </select>

    <select id="getStudentApplyTicket" resultType="com.qmth.exam.reserve.bean.apply.TicketInfo">
        select sa.id         applyId,
               sa.cancel,
               sa.ticket_number,
               sa.seat_number,
               sa.student_id,
               stu.name      studentName,
               stu.student_code,
               stu.identity_number,
               stu.gender,
               stu.photo_path,
               stu.apply_task_id,
               sa.time_period_id,
               tp.start_time timePeriodStart,
               tp.end_time   timePeriodEnd,
               sa.exam_site_id,
               es.name       examSiteName,
               es.address    examSiteAddress,
               sa.exam_room_id,
               ec.name       examRoomName,
               ec.code       examRoomCode,
               ec.address    examRoomAddress
        from t_student_apply sa
                 inner join t_student stu on stu.id = sa.student_id
                 inner join t_time_period tp on tp.id = sa.time_period_id
                 inner join t_exam_site es on es.id = sa.exam_site_id
                 left join t_exam_room ec on ec.id = sa.exam_room_id
        where sa.exam_site_id = #{examSiteId}
          and sa.time_period_id = #{timePeriodId}
          and sa.student_id = #{studentId}
    </select>

    <select id="listTimePeriod" resultType="com.qmth.exam.reserve.entity.StudentApplyEntity">
        SELECT
        sa.*
        FROM
        t_student_apply sa,
        t_student s
        WHERE
        sa.student_id = s.id
        AND sa.cancel = #{cancel}
        AND sa.time_period_id IN
        <foreach collection="timePeriodIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        order by s.student_code
    </select>
	
	<select id="listStudentApply" resultType="com.qmth.exam.reserve.bean.stdapply.StudentApplyVO">
		select s.name,s.student_code studentCode,sa.seat_number seatNumber from t_student_apply sa,t_student s where sa.student_id=s.id and seat_number is not null
		and cancel=0
		<if test="timePeriodId!=null">
			and sa.time_period_id=#{timePeriodId}
		</if>
		<if test="roomId!=null">
			and sa.exam_room_id=#{roomId}
		</if>
		order by sa.seat_number
	
	</select>

    <select id="getNoApplyCount" resultType="int">
        SELECT
        a.total - b.have_total noApplyNum
        FROM
        ( SELECT sum( apply_number ) total FROM t_student ts WHERE apply_task_id = #{taskId} AND category_id = #{categoryId} ) a,
        (
        SELECT
        count( sa.id ) have_total
        FROM
        t_student_apply sa,
        t_exam_site s
        WHERE
        sa.exam_site_id = s.id
        AND sa.cancel = 0
        AND sa.student_id IN ( SELECT id FROM t_student ts WHERE apply_task_id = #{taskId} AND category_id = #{categoryId} )
        AND sa.time_period_id in
        <foreach collection="timePeriodIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        ) b

    </select>

    <select id="getAgentAndRoom" resultType="com.qmth.exam.reserve.bean.stdapply.AgentAndRoomVO">
        SELECT
            s.NAME agentName,
            g.NAME applyTeachingName,
            cat.name teachingName,
            r.NAME roomName,
            u.NAME userName
        FROM
            t_exam_site s,
            t_category g,
            t_student stu,
            t_category cat,
            t_student_apply a
            LEFT JOIN t_exam_room r ON r.id = a.exam_room_id
            LEFT JOIN t_user u ON u.id = a.operate_id
        WHERE
            a.exam_site_id = s.id
          AND g.id = s.category_id
          AND a.student_id = stu.id
          AND cat.id = stu.category_id
          AND a.id = #{id}
    </select>

    <select id="listStudentApplyExport" resultType="com.qmth.exam.reserve.bean.stdapply.StudentApplyExport">
        SELECT
            t.student_code studentCode,
            t.NAME studentName,
            t.identity_number identityNumber,
            s.NAME siteName,
            concat( p.start_time, '-', p.end_time ) timePeriod
        FROM
            t_student t,
            t_student_apply sa,
            t_exam_site s,
            t_time_period p
        WHERE
            t.category_id = #{teachingId}
          AND t.apply_task_id = #{taskId}
          AND sa.cancel = 0
          AND sa.student_id = t.id
          AND sa.exam_site_id = s.id
          AND sa.time_period_id = p.id
        ORDER BY p.start_time
    </select>

    <select id="listStudentNoApply" resultType="com.qmth.exam.reserve.bean.stdapply.StudentExport">
        SELECT
            stu.student_code studentCode,
            stu.NAME studentName,
            stu.identity_number identityNumber,
            stu.apply_number - ifnull( rr.nums, 0 ) remainApplyNumber,
            #{teachingName} teachingName
        FROM
            t_student stu
                LEFT JOIN (
                SELECT
                    student_id,
                    count( 1 ) nums
                FROM
                    t_student s,
                    t_student_apply a
                WHERE
                    s.category_id = #{teachingId}
                  AND s.apply_task_id = #{taskId}
                  AND s.id = a.student_id
                  AND cancel = 0
                GROUP BY
                    student_id
            ) rr ON stu.id = rr.student_id
        WHERE
            stu.category_id = #{teachingId}
          AND stu.apply_task_id = #{taskId}
          AND stu.apply_number - ifNUll( rr.nums, 0 ) > 0
        ORDER BY stu.student_code
    </select>
    <select id="listStudentApplyDetail" resultType="com.qmth.exam.reserve.bean.stdapply.StudentApplyExport" parameterType="java.lang.Long">
        SELECT
            s.NAME studentName,
            s.identity_number identityNumber,
            s.student_code studentCode,
            c.CODE teachingCode,
            c.NAME teachingName,
            ct.NAME applyTeachingName,
            es.CODE examSiteCode,
            es.NAME examSiteName,
            concat(
                    date_format( FROM_UNIXTIME( SUBSTR( tp.start_time, 1, 10 )), '%Y-%m-%d %H:%i' ),
                    date_format( FROM_UNIXTIME( SUBSTR( tp.end_time, 1, 10 )), '-%H:%i' )
            ) timePeriod
        FROM
            t_student s,
            t_student_apply sa,
            t_category c,
            t_exam_site es,
            t_category ct,
            t_time_period tp
        WHERE
            s.id = sa.student_id
          AND s.category_id = c.id
          AND sa.exam_site_id = es.id
          AND es.category_id = ct.id
          AND tp.id = sa.time_period_id
          AND sa.cancel = 0
          AND s.apply_task_id = #{taskId}
          <if test="teachingId != null">
              AND s.category_id= #{teachingId}
          </if>
        ORDER BY
            tp.start_time
    </select>

</mapper>
