<?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.exam.mapper.ExamBatchItemMapper">

    <select id="queryListPage" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO">
        SELECT
            dsebi.*,dseb.course_id,dseb.course_name,dseb.subject,dsp.user_mobile AS userMobile,dseb.batch_name AS batchName,dsp.train_time AS trainTime
        FROM
            drive_school_exam_batch_item dsebi
                LEFT JOIN drive_school_exam_batch dseb ON dsebi.batch_id = dseb.id
                LEFT JOIN drive_school_process dsp ON dsebi.user_id = dsp.user_id
                AND dseb.course_id = dsp.course_id AND dseb.coach_id = dsp.coach_id
                AND dseb.subject = dsp.subject     AND dsp.deleted = 0 AND dsp.exam_status = '9'
        WHERE
            dseb.deleted = 0
          AND dseb.course_id = #{entity.courseId}
          AND dseb.coach_id = #{entity.coachId}
          AND dseb.`subject` = #{entity.subject}
        <if test="entity.userName != null and entity.userName != ''">
            AND dsebi.user_name LIKE CONCAT('%',#{entity.userName},'%')
        </if>
          AND dsebi.fraction IS NULL
    </select>
    <select id="selectByBatchId" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO">
        SELECT
            dseb.*,dss.phone AS userMobile
        FROM
            drive_school_exam_batch_item dseb
                left join drive_school_student dss ON dseb.user_id = dss.user_id AND dss.deleted=0
        WHERE
            dseb.deleted = 0
          AND dseb.batch_id = #{batchId}
    </select>
    <select id="selectByUserIdAndCoachId" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO">
        SELECT
            dsebi.*,
            dseb.batch_name,
            dseb.start_time,
            dseb.end_time,
            dseb.`subject`,
            dseb.addr,
            dseb.trans_way
        FROM
            drive_school_exam_batch_item dsebi
                LEFT JOIN drive_school_exam_batch dseb ON dsebi.batch_id = dseb.id
        WHERE
            dsebi.user_id = #{userId}
        <if test="coachId != null and coachId != ''">
            AND dseb.coach_id=#{coachId}
        </if>
        ORDER BY dseb.create_time DESC
    </select>
    <select id="selectByCoachId" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO">
        SELECT
        dsebi.*,
        dseb.`subject`
        FROM
        drive_school_exam_batch_item dsebi
        LEFT JOIN drive_school_exam_batch dseb ON dsebi.batch_id = dseb.id
        WHERE
        dsebi.deleted = 0
        AND dseb.deleted = 0
        <if test="coachId != null and coachId != ''">
            AND dseb.coach_id= #{coachId}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND dseb.start_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!=''">
            AND dseb.start_time &lt;= #{endTime}
        </if>
    </select>

    <select id="selectExamByUserIdAndCoachId" resultType="cn.iocoder.yudao.module.exam.vo.ExamVO">

        SELECT
        dseb.*,
        dseb.start_time AS examStartTime,

        dsp.course_type AS courseType,
        CASE
        WHEN dsebi.fraction IS NULL AND dsebi.if_pass IS NULL
        THEN dsp.exam_status
        ELSE NULL
        END AS ifPassStatus,

        dsebi.batch_id,
        dsebi.user_name,
        dsebi.fraction,
        dsebi.fraction AS examScore,
        dsebi.if_pass,
        dsebi.if_pass AS examStatus,
        dsebi.if_evaluate,
        dsebi.evaluate_id,
        dsebi.id AS batchItemId
        FROM drive_school_exam_batch dseb
        LEFT JOIN drive_school_exam_batch_item dsebi ON dseb.id = dsebi.batch_id
        LEFT JOIN drive_school_process dsp ON dseb.course_id = dsp.course_id AND dseb.subject = dsp.subject AND dsebi.user_id = dsp.user_id
        WHERE
        dsebi.user_id = #{userId}
        AND (dseb.deleted = 0 OR dseb.deleted IS NULL)
        AND (dsebi.deleted = 0 OR dsebi.deleted IS NULL)
        order by dseb.start_time DESC;
        <!--SELECT
        dsp.*,

        dseb.batch_name,
        dseb.start_time,
        dseb.end_time,
        dseb.addr,
        dseb.trans_way,
        dseb.id AS batchId,

        dsebi.fraction,
        dsebi.if_pass,
        dsebi.if_evaluate,
        dsebi.evaluate_id,
        dsebi.id AS batchItemId

        FROM
        drive_school_process dsp
        LEFT JOIN
        drive_school_exam_batch dseb ON dsp.course_id = dseb.course_id AND dsp.subject = dseb.subject
        LEFT JOIN
        drive_school_exam_batch_item dsebi ON dseb.id = dsebi.batch_id AND dsp.user_id = dsebi.user_id
        WHERE
        dsebi.user_id = #{userId}
        AND dsp.deleted = 0
        AND (dseb.deleted = 0 OR dseb.deleted IS NULL)
        AND (dsebi.deleted = 0 OR dsebi.deleted IS NULL)
        <if test="coachId != null and coachId != ''">
            AND dseb.coach_id=#{coachId}
        </if>
        ORDER BY
        dseb.start_time DESC;-->
    </select>

    <select id="selectExamByUserIdAndCoachIdNew" resultType="cn.iocoder.yudao.module.exam.vo.ExamVO">

        SELECT
        dseb.*,
        dseb.start_time AS examStartTime,

        dsp.course_type AS courseType,
        CASE
        WHEN dsebi.fraction IS NULL AND dsebi.if_pass IS NULL
        THEN dsp.exam_status
        ELSE NULL
        END AS ifPassStatus,

        dsebi.batch_id,
        dsebi.user_name,
        dsebi.fraction,
        dsebi.fraction AS examScore,
        dsebi.if_pass,
        dsebi.if_pass AS examStatus,
        dsebi.if_evaluate,
        dsebi.evaluate_id,
        dsebi.id AS batchItemId
        FROM drive_school_exam_batch dseb
        LEFT JOIN drive_school_exam_batch_item dsebi ON dseb.id = dsebi.batch_id AND dsebi.if_evaluate = 0
        LEFT JOIN drive_school_process dsp ON dseb.course_id = dsp.course_id AND dseb.subject = dsp.subject AND dsebi.user_id = dsp.user_id
        WHERE
        dsebi.user_id = #{userId}
        AND (dseb.deleted = 0 OR dseb.deleted IS NULL)
        AND (dsebi.deleted = 0 OR dsebi.deleted IS NULL)
        order by dseb.start_time DESC;
    </select>
    <select id="listJoinBatchByIds" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO">
        SELECT
        dsebi.*,
        dseb.batch_name,
        dseb.start_time,
        dseb.end_time,
        dseb.`subject`,
        dseb.addr,
        dseb.trans_way,
        dseb.coach_id,
        dseb.coach_name,
        dsc.type AS courseType
        FROM
        drive_school_exam_batch_item dsebi
        LEFT JOIN drive_school_exam_batch dseb ON dsebi.batch_id = dseb.id
        LEFT JOIN drive_school_course dsc ON dseb.course_id = dsc.id
        WHERE
        dsebi.id IN
        <foreach collection="examIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="batchItemListByCoach" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchVO">
        SELECT
        c.user_id AS coach_id,
        c.name AS coach_name,
        c.image,
        COUNT(i.id) AS student_count,
        SUM(CASE WHEN i.if_pass = 1 THEN 1 ELSE 0 END) AS pass_count,
        ROUND(
        IFNULL(SUM(CASE WHEN i.if_pass = 1 THEN 1 ELSE 0 END) / NULLIF(COUNT(i.id), 0) * 100, 0),
        2
        ) AS pass_rate
        FROM
        drive_school_coach c
        LEFT JOIN drive_school_exam_batch b
        ON b.coach_id = c.user_id AND b.deleted = 0
        <if test="entity.subject != null and entity.subject != '' ">
            AND b.subject = #{entity.subject}
        </if>
        LEFT JOIN drive_school_exam_batch_item i
        ON i.batch_id = b.id AND i.deleted = 0
        <if test="entity.startTimeSearch != null and entity.startTimeSearch != '' ">
            AND b.start_time &gt;= #{entity.startTimeSearch}
        </if>
        <if test="entity.endTimeSearch != null and entity.endTimeSearch != '' ">
            AND b.start_time &lt;= #{entity.endTimeSearch}
        </if>
        WHERE
        c.deleted = 0
        AND c.type = 'jl'
        <if test="entity.coachId != null">
            AND c.user_id = #{entity.coachId}
        </if>
        GROUP BY
        c.id, c.name
        ORDER BY
        student_count DESC
        <!--SELECT
        b.coach_id,
        b.coach_name,
        COUNT(i.id) AS student_count,
        SUM(CASE WHEN i.if_pass = 1 THEN 1 ELSE 0 END) AS pass_count,
        ROUND(
        SUM(CASE WHEN i.if_pass = 1 THEN 1 ELSE 0 END) / NULLIF(COUNT(i.id), 0) * 100,
        2
        ) AS pass_rate
        FROM
        drive_school_exam_batch b
        LEFT JOIN drive_school_exam_batch_item i
        ON b.id = i.batch_id AND i.deleted = 0
        WHERE
        b.deleted = 0
        <if test="entity.subject != null">
            AND b.subject = #{entity.subject}
        </if>
        <if test="entity.startTimeSearch != null and entity.startTimeSearch != '' ">
            AND i.create_time &gt;= #{entity.startTimeSearch}
        </if>
        <if test="entity.endTimeSearch != null and entity.endTimeSearch != '' ">
            AND i.create_time &lt;= #{entity.endTimeSearch}
        </if>
        <if test="entity.coachId != null">
            AND b.coach_id = #{entity.coachId}
        </if>
        GROUP BY
        b.coach_id, b.coach_name
        ORDER BY
        student_count DESC-->
    </select>

    <select id="getBatchItemCount" resultType="cn.iocoder.yudao.module.exam.vo.ExamBatchItemVO" parameterType="java.lang.String">
        SELECT dsebi.* from drive_school_exam_batch dseb
                                left join drive_school_exam_batch_item dsebi ON dseb.id = dsebi.batch_id
        where dseb.id = #{batchId}
    </select>
</mapper>
