<?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.xjrsoft.module.assessment.mapper.AssessmentTemplatePlanMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto" resultType="com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo">
        SELECT t1.id,t1.name,t1.score,t1.start_time,t1.status,t1.end_time,t2.name AS assessment_template_name,
        t1.assessment_template_id,t1.type,t1.random_number FROM assessment_template_plan t1
        LEFT JOIN assessment_template t2 ON t1.assessment_template_id = t2.id
        WHERE t1.delete_mark = 0
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.assessmentTemplateName != null and dto.assessmentTemplateName != ''">
            and t2.name like concat('%', #{dto.assessmentTemplateName}, '%')
        </if>
        <if test="dto.keyword != null and dto.keyword != ''">
            and (t2.name like concat('%', #{dto.keyword}, '%') or t1.name like concat('%', #{dto.keyword}, '%'))
        </if>
        <if test="dto.assessmentTemplateId != null">
            and t2.id = #{dto.assessmentTemplateId}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
        ORDER BY t1.create_date DESC
    </select>
    <select id="getStudentPage" parameterType="com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerPageDto" resultType="com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentPageVo">
        SELECT t1.id,t1.name,t1.score,t1.start_time,t1.status,t1.end_time,t2.name AS assessment_template_name,
        t1.assessment_template_id,t3.is_confirm,t3.submit_status FROM assessment_template_plan t1
        LEFT JOIN assessment_template t2 ON t1.assessment_template_id = t2.id
        LEFT JOIN assessment_plan_answer_student t3 ON t3.assessment_template_plan_id = t1.id
        WHERE t1.delete_mark = 0 AND t1.status = 1 AND t3.student_user_id = #{dto.studentUserId}
        and t3.is_confirm = 1
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.assessmentTemplateName != null and dto.assessmentTemplateName != ''">
            and t2.name like concat('%', #{dto.assessmentTemplateName}, '%')
        </if>
        <if test="dto.keyword != null and dto.keyword != ''">
            and (t2.name like concat('%', #{dto.keyword}, '%') or t1.name like concat('%', #{dto.keyword}, '%'))
        </if>
        <if test="dto.assessmentTemplateId != null">
            and t2.id = #{dto.assessmentTemplateId}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
        ORDER BY t1.create_date asc
    </select>
    <select id="getTeacherPage" parameterType="com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerPageDto" resultType="com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerTeacherPageVo">
        SELECT t1.id,t1.name,t1.score,t1.start_time,t1.status,t1.end_time,t2.name AS assessment_template_name,
        t1.assessment_template_id,t3.is_confirm,t3.class_id,t4.name as class_name,
        (select count(*) from assessment_plan_answer_student a1
        left join base_student_school_roll a2 on a1.student_user_id = a2.user_id
        where a1.delete_mark = 0 and a1.assessment_template_plan_id = t1.id and a2.class_id = t3.class_id and a1.submit_status = 1) as submitted_count,
        (select count(*) from assessment_plan_answer_student a1
        left join base_student_school_roll a2 on a1.student_user_id = a2.user_id
        where a1.delete_mark = 0 and a1.assessment_template_plan_id = t1.id and a2.class_id = t3.class_id and a1.submit_status = 0) as unsubmitted_count FROM assessment_template_plan t1
        LEFT JOIN assessment_template t2 ON t1.assessment_template_id = t2.id
        LEFT JOIN assessment_plan_answer_class t3 ON t3.assessment_template_plan_id = t1.id
        LEFT JOIN base_class t4 ON t3.class_id = t4.id
        WHERE t1.delete_mark = 0 AND t1.status = 1
        <if test="dto.teacherId != null">
            AND t4.teacher_id = #{dto.teacherId}
        </if>
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.assessmentTemplateName != null and dto.assessmentTemplateName != ''">
            and t2.name like concat('%', #{dto.assessmentTemplateName}, '%')
        </if>
        <if test="dto.keyword != null and dto.keyword != ''">
            and (t2.name like concat('%', #{dto.keyword}, '%') or t1.name like concat('%', #{dto.keyword}, '%'))
        </if>
        <if test="dto.assessmentTemplateId != null">
            and t2.id = #{dto.assessmentTemplateId}
        </if>
        <if test="dto.assessmentTemplatePlanId != null">
            and t1.id = #{dto.assessmentTemplatePlanId}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
        ORDER BY t1.create_date asc
    </select>
    <select id="getMobilePage" parameterType="com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto" resultType="com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo">
        SELECT t1.id,t1.name,t1.score,t1.start_time,t1.status,t1.end_time,t2.name AS assessment_template_name,
        t1.assessment_template_id,
        (SELECT COUNT(*) FROM assessment_plan_answer_student WHERE delete_mark = 0 AND assessment_template_plan_id = t1.id AND submit_status = 1) AS submittedCount,
        (SELECT COUNT(*) FROM assessment_plan_answer_student WHERE delete_mark = 0 AND assessment_template_plan_id = t1.id AND (submit_status = 0 OR submit_status IS NULL)) AS unsubmittedCount,
        (SELECT COUNT(*) FROM assessment_plan_answer_student WHERE delete_mark = 0 AND assessment_template_plan_id = t1.id) AS allCount
        FROM assessment_template_plan t1
        LEFT JOIN assessment_template t2 ON t1.assessment_template_id = t2.id
        WHERE t1.delete_mark = 0 AND t1.status = 1
        <if test="dto.keyword != null and dto.keyword != ''">
            and t1.name like concat('%', #{dto.keyword}, '%')
        </if>
        ORDER BY t1.create_date asc
    </select>

    <select id="getAnswerStudent" parameterType="com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerStudentDto" resultType="com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentVo">
        SELECT t1.student_user_id,t2.name,t1.submit_status,t1.assessment_template_plan_id FROM assessment_plan_answer_student t1
        LEFT JOIN xjr_user t2 ON t1.student_user_id = t2.id
        LEFT JOIN base_student_school_roll t3 ON t1.student_user_id = t3.user_id
        LEFT JOIN base_class t4 ON t3.class_id = t4.id
        WHERE t1.delete_mark = 0 AND t1.assessment_template_plan_id = #{dto.assessmentTemplatePlanId}
        <if test="dto.keyword != null and dto.keyword != ''">
            and t2.name like concat('%', #{dto.keyword}, '%')
        </if>
        <if test="dto.teacherId != null">
            AND t4.teacher_id = #{dto.teacherId}
        </if>
    </select>

    <select id="getSemesterClass" resultType="java.lang.Long">
        SELECT distinct t1.class_id FROM assessment_plan_answer_class t1
        INNER JOIN assessment_template_plan t2 ON t1.assessment_template_plan_id = t2.id
        WHERE t1.delete_mark = 0 AND t2.delete_mark = 0 AND t2.status in (0, 1) AND t2.base_semester_id = #{id}
    </select>
</mapper>