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

    <select id="queryListPage" resultType="cn.iocoder.yudao.module.train.vo.DriveSchoolCoachClockVO">
        SELECT
        dscc.*
        FROM
        drive_school_coach_clock dscc
        where
        dscc.deleted = 0
        <if test="entity.userId!= null and entity.userId!= ''">
            and dscc.user_id = #{entity.userId}
        </if>
        <if test="entity.carNo!= null and entity.carNo!= ''">
            and dscc.car_no like concat('%', #{entity.carNo},'%')
        </if>
        <if test="entity.subject!= null and entity.subject!= ''">
            and dscc.subject = #{entity.subject}
        </if>
        <if test="entity.id!= null and entity.id!= ''">
            and dscc.id = #{entity.id}
        </if>
        <if test="entity.startTimeStr != null and entity.startTimeStr != ''">
            AND dscc.train_day &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr != null and entity.endTimeStr != ''">
            AND dscc.train_day &lt;= #{entity.endTimeStr}
        </if>
        ORDER BY dscc.train_day DESC
    </select>

    <select id="countTrainCar" resultType="cn.iocoder.yudao.module.train.vo.TrainCarCountVO">
        SELECT
        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,
        COALESCE(COUNT(*), 0) AS totalCount
        FROM
        drive_school_coach_clock
        WHERE
        deleted = 0
        AND subject IN (2, 3)
        <if test="entity.userId != null">
            AND user_id = #{entity.userId}
        </if>
        <if test="entity.carNo != null and entity.carNo != '' ">
            AND car_no = #{entity.carNo}
        </if>
        <if test="entity.trainDayStartTime != null and entity.trainDayStartTime != '' ">
            AND train_day &gt;= #{entity.trainDayStartTime}
        </if>
        <if test="entity.trainDayEndTime != null and entity.trainDayEndTime != '' ">
            AND train_day &lt;= #{entity.trainDayEndTime}
        </if>
    </select>

    <select id="trainClockByCar" resultType="cn.iocoder.yudao.module.train.vo.TrainCarCountVO">
        SELECT
        dscc.user_name,
        dscc.car_id,
        dscc.car_no,
        COUNT(*) AS totalCount,
        SUM(CASE WHEN dscc.subject = 2 THEN 1 ELSE 0 END) AS subject2Count,
        SUM(CASE WHEN dscc.subject = 3 THEN 1 ELSE 0 END) AS subject3Count,
        SUM(dscc.train_time) AS trainTimeCount,
        SUM(IFNULL(dst.student_count, 0)) AS studentCount,
        SUM(CASE WHEN dscc.subject = 2 THEN dscc.train_time ELSE 0 END) AS subject2TrainTime,
        SUM(CASE WHEN dscc.subject = 3 THEN dscc.train_time ELSE 0 END) AS subject3TrainTime
        FROM
        drive_school_coach_clock dscc
        LEFT JOIN
        drive_school_car car ON dscc.car_id = car.id AND car.deleted = 0
        LEFT JOIN (
        SELECT
        coach_clock_id,
        COUNT(*) AS student_count
        FROM
        drive_school_train
        WHERE
        deleted = 0
        AND coach_clock_id IS NOT NULL
        GROUP BY
        coach_clock_id
        ) dst ON dst.coach_clock_id = dscc.id
        WHERE
        dscc.deleted = 0
        AND dscc.subject IN (2, 3)
        <if test="entity.userId != null">
            AND dscc.user_id = #{entity.userId}
        </if>
        <if test="entity.carNo != null and entity.carNo != '' ">
            AND dscc.car_no = #{entity.carNo}
        </if>
        <if test="entity.subject != null">
            AND dscc.subject = #{entity.subject}
        </if>
        <if test="entity.courseType != null and entity.courseType != '' ">
            AND car.course_type = #{entity.courseType}
        </if>
        <if test="entity.trainDayStartTime != null and entity.trainDayStartTime != '' ">
            AND dscc.train_day &gt;= #{entity.trainDayStartTime}
        </if>
        <if test="entity.trainDayEndTime != null and entity.trainDayEndTime != '' ">
            AND dscc.train_day &lt;= #{entity.trainDayEndTime}
        </if>
        GROUP BY
        dscc.car_no, dscc.car_id, dscc.user_name
        ORDER BY
        totalCount DESC
    </select>

    <!--    <select id="trainClockByCar" resultType="cn.iocoder.yudao.module.train.vo.TrainCarCountVO">
            SELECT
            dscc.user_name,
            dscc.car_id,
            dscc.car_no,
            COUNT(*) AS totalCount,
            SUM(CASE WHEN dscc.subject = 2 THEN 1 ELSE 0 END) AS subject2Count,
            SUM(CASE WHEN dscc.subject = 3 THEN 1 ELSE 0 END) AS subject3Count,
            SUM(dscc.train_time) AS trainTimeCount,
            COUNT(dst.coach_clock_id) AS studentCount,
            SUM(CASE WHEN dscc.subject = 2 THEN dscc.train_time ELSE 0 END) AS subject2TrainTime,
            SUM(CASE WHEN dscc.subject = 3 THEN dscc.train_time ELSE 0 END) AS subject3TrainTime
            FROM
            drive_school_coach_clock dscc
            LEFT JOIN
            drive_school_car car ON dscc.car_id = car.id AND car.deleted = 0
            LEFT JOIN
            drive_school_train dst ON dst.coach_clock_id = dscc.id AND dst.deleted = 0
            WHERE
            dscc.deleted = 0
            AND dscc.subject IN (2, 3)
            <if test="entity.userId != null">
                AND dscc.user_id = #{entity.userId}
            </if>
            <if test="entity.carNo != null and entity.carNo != '' ">
                AND dscc.car_no = #{entity.carNo}
            </if>
            <if test="entity.subject != null">
                AND dscc.subject = #{entity.subject}
            </if>
            <if test="entity.courseType != null and entity.courseType != '' ">
                AND car.course_type = #{entity.courseType}
            </if>
            <if test="entity.trainDayStartTime != null and entity.trainDayStartTime != '' ">
                AND dscc.train_day &gt;= #{entity.trainDayStartTime}
            </if>
            <if test="entity.trainDayEndTime != null and entity.trainDayEndTime != '' ">
                AND dscc.train_day &lt;= #{entity.trainDayEndTime}
            </if>
            GROUP BY
            dscc.car_no, dscc.car_id, dscc.user_name
            ORDER BY
            totalCount DESC
        </select>-->

    <select id="selectCoachClockIdsByCarId" resultType="cn.iocoder.yudao.module.train.entity.DriveSchoolCoachClock">
        SELECT
        id,
        subject,
        user_id,
        user_name,
        train_day,
        start_time,
        end_time,
        car_id,
        car_no,
        train_time
        FROM drive_school_coach_clock
        WHERE
        car_id = #{entity.carId}
        AND deleted = 0
        <if test="entity.subject != null">
            AND subject = #{entity.subject}
        </if>
        <if test="entity.trainDayStartTime != null and entity.trainDayStartTime != '' ">
            AND train_day &gt;= #{entity.trainDayStartTime}
        </if>
        <if test="entity.trainDayEndTime != null and entity.trainDayEndTime != '' ">
            AND train_day &lt;= #{entity.trainDayEndTime}
        </if>
        ORDER BY train_day DESC
    </select>

    <select id="noClockInRemind" resultType="cn.iocoder.yudao.module.train.entity.DriveSchoolCoachClock">
        /*SELECT user_id, car_no, tenant_id
        FROM drive_school_coach_clock
        WHERE
            DATE (create_time) = CURDATE()
          AND DATE (start_time) = CURDATE()
          AND end_time IS NULL
          AND deleted = 0*/
        SELECT
            user_id,
            GROUP_CONCAT(car_no SEPARATOR '；') AS carNo,
            tenant_id
        FROM drive_school_coach_clock
        WHERE
            DATE (create_time) = CURDATE()
          AND DATE (start_time) = CURDATE()
          AND end_time IS NULL
          AND deleted = 0
        GROUP BY
            user_id, tenant_id
    </select>


    <select id="coachClockExport" resultType="cn.iocoder.yudao.module.train.vo.CoachClockExportVO">
        SELECT
        id,
        user_name AS coachName,
        user_mobile AS coachPhone,
        car_no,
        subject,
        addr,
        train_day,
        start_time,
        start_remark,
        end_time,
        end_remark,
        train_time,
        start_images,
        end_images
        FROM drive_school_coach_clock
        WHERE deleted = 0
        <if test="entity.coachId != null">
            AND user_id = #{entity.coachId}
        </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="getTrainStudentCount" resultType="cn.iocoder.yudao.module.train.vo.CloakAndTrainCountVO">
        SELECT
        COALESCE(COUNT(*), 0) AS totalStudentCount,
        COALESCE(SUM(CASE WHEN subject = 2 THEN 1 ELSE 0 END), 0) AS subject2StudentCount,
        COALESCE(SUM(CASE WHEN subject = 3 THEN 1 ELSE 0 END), 0) AS subject3StudentCount
        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 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.subject!= null">
            AND subject = #{entity.subject}
        </if>
        <if test="entity.carNo != null and entity.carNo != '' ">
            AND car_no = #{entity.carNo}
        </if>
        <if test="entity.trainStartTimeStr!=null and entity.trainStartTimeStr!=''">
            AND create_time &gt;= #{entity.trainStartTimeStr}
        </if>
        <if test="entity.trainEndTimeStr!=null and entity.trainEndTimeStr!=''">
            AND create_time &lt;= #{entity.trainEndTimeStr}
        </if>
    </select>

    <select id="getTrainCarCount" resultType="cn.iocoder.yudao.module.train.vo.CloakAndTrainCountVO">
        SELECT
        COALESCE(SUM(CASE WHEN subject = 2 THEN 1 ELSE 0 END), 0) AS subject2CarCount,
        COALESCE(SUM(CASE WHEN subject = 3 THEN 1 ELSE 0 END), 0) AS subject3CarCount,
        COALESCE(COUNT(*), 0) AS totalCarCount
        FROM
        drive_school_coach_clock
        WHERE
        deleted = 0
        AND subject IN (2, 3)
        <if test="entity.coachId != null">
            AND user_id = #{entity.coachId}
        </if>
        <if test="entity.subject!= null">
            AND subject = #{entity.subject}
        </if>
        <if test="entity.carNo != null and entity.carNo != '' ">
            AND car_no = #{entity.carNo}
        </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>
    </select>


    <select id="getTodayTrainSituation" resultType="cn.iocoder.yudao.module.train.vo.CloakAndTrainCountVO">
        SELECT COALESCE(COUNT(*), 0)                                     AS totalCarCount,
               COALESCE(SUM(CASE WHEN subject = 2 THEN 1 ELSE 0 END), 0) AS subject2CarCount,
               COALESCE(SUM(CASE WHEN subject = 3 THEN 1 ELSE 0 END), 0) AS subject3CarCount
        FROM drive_school_coach_clock
        WHERE train_day = CURDATE()
          AND DATE (start_time) = CURDATE()
          AND end_time IS NULL
          AND deleted = 0
    </select>


    <select id="getTodayCarTrainSituation" resultType="cn.iocoder.yudao.module.train.vo.DriveSchoolCoachClockVO">
        SELECT
            dscc.*,
            COALESCE(COUNT(dst.coach_clock_id), 0) AS studentTotal
        FROM
            drive_school_coach_clock dscc
                LEFT JOIN
            drive_school_train dst ON dst.coach_clock_id = dscc.id AND dst.deleted = 0
        WHERE
            dscc.train_day = CURDATE()
          AND DATE(dscc.start_time) = CURDATE()
          AND dscc.end_time IS NULL
          AND dscc.deleted = 0
        <if test="entity.subject != null and entity.subject != '' ">
            AND dscc.subject = #{entity.subject}
        </if>
        GROUP BY
            dscc.id
    </select>

    <select id="getClockCarNoByUserId" resultType="string">
        SELECT car_No
        FROM drive_school_coach_clock
        WHERE user_id = #{userId}
          AND train_day = CURDATE()
          AND end_time IS NULL
        AND deleted = 0
    </select>

    <select id="getClockCarInfoByUserId" resultType="cn.iocoder.yudao.module.train.entity.DriveSchoolCoachClock">
        SELECT *
        FROM drive_school_coach_clock
        WHERE user_id = #{userId}
          AND car_no = #{carNo}
          AND train_day = CURDATE()
          AND end_time IS NULL
        AND deleted = 0
    </select>

    <!--<select id="getTodayCarTrainSituation" resultType="cn.iocoder.yudao.module.train.entity.DriveSchoolCoachClock">
        SELECT *
        FROM drive_school_coach_clock
        WHERE train_day = CURDATE()
        AND DATE (start_time) = CURDATE()
        AND end_time IS NULL
        AND deleted = 0
        <if test="entity.subject != null and entity.subject != '' ">
            AND subject = #{entity.subject}
        </if>
    </select>-->


    <select id="queryErrorClockListPage" resultType="cn.iocoder.yudao.module.train.vo.DriveSchoolCoachClockVO">
        SELECT
        dscc.*
        FROM
        drive_school_coach_clock dscc
        WHERE
        dscc.end_time IS NULL
        AND dscc.train_day &lt; CURDATE()
        AND dscc.deleted = 0
        <if test="entity.userId!= null and entity.userId!= ''">
            and dscc.user_id = #{entity.userId}
        </if>
        <if test="entity.carNo!= null and entity.carNo!= ''">
            and dscc.car_no like concat('%', #{entity.carNo},'%')
        </if>
        <if test="entity.subject!= null and entity.subject!= ''">
            and dscc.subject = #{entity.subject}
        </if>
        <if test="entity.id!= null and entity.id!= ''">
            and dscc.id = #{entity.id}
        </if>
        <if test="entity.startTimeStr != null and entity.startTimeStr != ''">
            AND dscc.train_day &gt;= #{entity.startTimeStr}
        </if>
        <if test="entity.endTimeStr != null and entity.endTimeStr != ''">
            AND dscc.train_day &lt;= #{entity.endTimeStr}
        </if>
        ORDER BY dscc.train_day DESC
    </select>
</mapper>
