<?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.TimePeriodDao">

    <select id="listTimePeriod" resultType="java.lang.String" parameterType="java.lang.Long">
        SELECT DISTINCT concat(
                                date_format(FROM_UNIXTIME(SUBSTR(start_time, 1, 10)), '%H:%i'),
                                '-',
                                date_format(FROM_UNIXTIME(SUBSTR(end_time, 1, 10)), '%H:%i')
                        ) time
        FROM t_time_period
        WHERE apply_task_id = #{taskId}
        ORDER BY time
    </select>

    <select id="listTimePeriodDate" resultType="java.lang.String" parameterType="java.lang.Long">
        SELECT distinct date_format(FROM_UNIXTIME(SUBSTR(start_time, 1, 10)), '%Y-%m-%d') date
        FROM t_time_period tp
        WHERE apply_task_id = #{taskId}
        ORDER BY date
    </select>

    <select id="listTimePeriodByExamSiteId" resultType="com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteBean">
        SELECT
        tpes.id,
        tp.id timePeriodId,
        tp.start_time startTime,
        tp.end_time endTime,
        tpes.enable,
        tpes.exam_site_id examSiteId
        FROM
        t_time_period_exam_site tpes,
        t_time_period tp
        WHERE
        tpes.time_period_id = tp.id
        AND tp.apply_task_id = #{taskId}
        <if test="examSiteId != null">
            AND tpes.exam_site_id = #{examSiteId}
        </if>
        order by tp.start_time
    </select>

    <select id="listTimePeriodByTask" resultType="com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteBean" parameterType="java.lang.Long">
        SELECT
            tp.id timePeriodId,
            tp.start_time startTime,
            tp.end_time endTime,
            1 enable
        FROM
            t_time_period tp
        WHERE
          tp.apply_task_id = #{taskId}
        order by tp.start_time
    </select>

    <select id="listTimePeriodByExamRoomId" resultType="com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteBean">
        SELECT
        tper.id,
        tp.id timePeriodId,
        tp.start_time startTime,
        tp.end_time endTime,
        tper.enable,
        tper.exam_room_id examSiteId
        FROM
        t_time_period_exam_room tper,
        t_time_period tp
        WHERE
        tper.time_period_id = tp.id
        AND tp.apply_task_id = #{taskId}
        <if test="examRoomId != null">
            AND tper.exam_room_id = #{examRoomId}
        </if>
        order by tp.start_time
    </select>
</mapper>
