<?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="cn.iocoder.yudao.module.train.mapper.TrainMapper">

    <select id="queryListPage" resultType="cn.iocoder.yudao.module.train.vo.TrainVO">
        SELECT
        dsrc.*
        FROM
        drive_school_reservation_course dsrc
        where
            dsrc.deleted = 0
            <if test="entity.userId != null and entity.userId != ''">
                and dsrc.user_id =#{entity.userId}
            </if>
            <if test="entity.coachId != null and entity.coachId != ''">
                and dsrc.coach_id =#{entity.coachId}
            </if>
            <if test="entity.courseId != null and entity.courseId != ''">
                and dsrc.course_id =#{entity.courseId}
            </if>
            <if test="entity.subject != null and entity.subject != ''">
                and dsrc.subject =#{entity.subject}
            </if>
            <if test="entity.ifCancel != null">
                and dsrc.if_cancel =#{entity.ifCancel}
            </if>
            <if test="entity.reservDay != null and entity.reservDay != ''">
                and dsrc.reserv_day LIKE CONCAT(#{entity.reservDay},'%')
            </if>
        order by dsrc.create_time desc
    </select>
    <select id="selectTrainByCondition" resultType="cn.iocoder.yudao.module.train.vo.TrainVO">
        SELECT
            dst.*,
            dsc.car_id AS carNo
        FROM
            drive_school_train dst
                LEFT JOIN drive_school_coach dsc ON dst.coach_id = dsc.user_id
        WHERE
            dst.deleted = 0
            <if test="coachId != null and coachId != ''">
              AND dst.coach_id = #{coachId}
            </if>
            <if test="startTime!=null and startTime!=''">
                AND dst.create_time &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!=''">
                AND dst.create_time &lt;= #{endTime}
            </if>
    </select>

    <select id="getTrainStudentCount" resultType="cn.iocoder.yudao.module.train.vo.TrainStudentCountVO">
        SELECT
        COALESCE(COUNT(*), 0) AS totalTrainCount,
        COALESCE(SUM(CASE WHEN subject = 2 THEN 1 ELSE 0 END), 0) AS subject2Count,
        COALESCE(SUM(CASE WHEN subject = 3 THEN 1 ELSE 0 END), 0) AS subject3Count
        FROM
        drive_school_train
        WHERE
        deleted = 0
        AND subject IS NOT NULL
        AND user_id IS NOT NULL
        AND course_id IS NOT NULL
        <if test="coachId != null and coachId != ''">
            AND coach_id = #{coachId}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND create_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!=''">
            AND create_time &lt;= #{endTime}
        </if>
    </select>
    <select id="queryTrainListPage" resultType="cn.iocoder.yudao.module.train.vo.TrainVO">
        SELECT
        dst.*, dsc.type as courseType
        FROM
        drive_school_train dst
        LEFT JOIN drive_school_course dsc ON dst.course_id = dsc.id
        where
        dst.deleted = 0
        <if test="entity.userId != null and entity.userId != ''">
            and dst.user_id =#{entity.userId}
        </if>
        <if test="entity.coachId != null and entity.coachId != ''">
            and dst.coach_id =#{entity.coachId}
        </if>
        <if test="entity.courseId != null and entity.courseId != ''">
            and dst.course_id =#{entity.courseId}
        </if>
        <if test="entity.isEnd != null and entity.isEnd != ''">
            and dst.end_time IS NOT NULL
        </if>
        <if test="entity.ifEvaluate != null ">
            and dst.if_evaluate = #{entity.ifEvaluate}
        </if>
        order by dst.create_time desc
    </select>
    <select id="listJoinBatchByIds" resultType="cn.iocoder.yudao.module.train.vo.TrainVO">
        SELECT
        dst.*, dsc.type as courseType
        FROM
        drive_school_train dst
        LEFT JOIN drive_school_course dsc ON dst.course_id = dsc.id
        where
            dst.deleted = 0
            and dst.id in
            <foreach collection="trainIds" item="trainId" open="(" separator="," close=")">
                #{trainId}
            </foreach>
    </select>

    <select id="noClockInRemind" resultType="cn.iocoder.yudao.module.train.vo.NoClockInRemindVO">
        SELECT
            coach_id,
            coach_name,
            COUNT(DISTINCT user_id) AS student_count,
            GROUP_CONCAT(DISTINCT user_name SEPARATOR '；') AS student_names,
            tenant_id
        FROM drive_school_train
        WHERE
          DATE(create_time) = CURDATE()
          AND DATE(start_time) = CURDATE()
          AND end_time IS NULL
          AND deleted = 0
        GROUP BY coach_id, coach_name
    </select>

    <select id="noClockInRemindByUserId" resultType="cn.iocoder.yudao.module.train.vo.NoClockInRemindVO">
        SELECT
            coach_id,
            coach_name,
            COUNT(DISTINCT user_id) AS student_count,
            GROUP_CONCAT(DISTINCT user_name SEPARATOR '；') AS student_names,
            tenant_id
        FROM drive_school_train
        WHERE
            coach_id = #{userId}
          AND car_no = #{carNo}
          AND DATE(create_time) = CURDATE()
          AND DATE(start_time) = CURDATE()
          AND end_time IS NULL
          AND deleted = 0
        GROUP BY coach_id, coach_name
    </select>

    <select id="selectStudentByCoachClockId" resultType="cn.iocoder.yudao.module.train.entity.Train">
        SELECT
            subject,
            user_name,
            train_time,
            start_time,
            end_time,
            car_no
        FROM drive_school_train
        WHERE
            deleted = 0
          <if test="coachClockId != null and coachClockId != '' ">
            AND coach_clock_id = #{coachClockId}
          </if>
        ORDER BY train_time DESC
    </select>

    <select id="getExportList" resultType="cn.iocoder.yudao.module.train.vo.TrainExportVO">
        SELECT
        id,
        user_id,
        user_name,
        user_mobile,
        course_name,
        coach_id,
        coach_name,
        subject,
        train_day,
        train_time,
        start_time,
        end_time,
        start_remark,
        end_remark,
        car_no,
        addr
        FROM drive_school_train
        WHERE deleted = 0
        AND subject IS NOT NULL
        AND user_id IS NOT NULL
        AND course_id IS NOT NULL
        <if test="entity.coachId != null and entity.coachId != ''">
            AND coach_id = #{entity.coachId}
        </if>
        <if test="entity.userName != null and entity.userName != ''">
            AND user_name like concat('%',#{entity.userName},'%')
        </if>
        <if test="entity.userMobile != null and entity.userMobile != ''">
            AND user_mobile like concat('%',#{entity.userMobile},'%')
        </if>
        <if test="entity.carNo != null and entity.carNo != '' ">
            AND car_no = #{entity.carNo}
        </if>
        <if test="entity.subject != null ">
            AND subject = #{entity.subject}
        </if>
        <if test="entity.trainStartTimeStr != null and entity.trainStartTimeStr != ''">
            AND train_day &gt;= #{entity.trainStartTimeStr}
        </if>
        <if test="entity.trainEndTimeStr != null and entity.trainEndTimeStr != ''">
            AND train_day &lt;= #{entity.trainEndTimeStr}
        </if>
        ORDER BY train_day DESC
    </select>

    <select id="getTodayTrainSituation" resultType="cn.iocoder.yudao.module.train.vo.CloakAndTrainCountVO">
        SELECT
            COALESCE ( COUNT(*), 0 ) AS totalStudentCount,
            COALESCE ( SUM( CASE WHEN dst.subject = 2 THEN 1 ELSE 0 END ), 0 ) AS subject2StudentCount,
            COALESCE ( SUM( CASE WHEN dst.subject = 3 THEN 1 ELSE 0 END ), 0 ) AS subject3StudentCount
        FROM
            drive_school_train dst
        LEFT JOIN drive_school_course dsc ON dst.course_id = dsc.id AND dsc.deleted = 0
        WHERE
            DATE ( dst.create_time ) = CURDATE()
          AND DATE ( dst.start_time ) = CURDATE()
          AND dst.end_time IS NULL
          AND dst.user_id IS NOT NULL
          AND dst.deleted = 0
        <if test="entity.userName != null and entity.userName != '' ">
            AND dst.user_name like concat('%',#{entity.userName},'%')
        </if>
        <if test="entity.subject != null">
            AND dst.subject = #{entity.subject}
        </if>
        <if test="entity.courseType != null and entity.courseType != ''">
            AND dsc.type = #{entity.courseType}
        </if>
    </select>

    <select id="getTodayStudentTrainSituation" resultType="cn.iocoder.yudao.module.train.vo.TodayTrainVO">
        SELECT dst.*
        FROM drive_school_train dst
        LEFT JOIN drive_school_course dsc ON dst.course_id = dsc.id AND dsc.deleted = 0
        WHERE
            DATE (dst.create_time) = CURDATE()
          AND DATE (dst.start_time) = CURDATE()
          AND dst.end_time IS NULL
          AND dst.user_id IS NOT NULL
          AND dst.deleted = 0
        <if test="entity.userName != null and entity.userName != ''">
            AND dst.user_name like concat('%',#{entity.userName},'%')
        </if>
        <if test="entity.subject != null">
            AND dst.subject = #{entity.subject}
        </if>
        <if test="entity.courseType != null and entity.courseType != ''">
            AND dsc.type = #{entity.courseType}
        </if>
    </select>
</mapper>
