<?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.student.mapper.BaseStudentAssessmentInspectionMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.student.dto.BaseStudentAssessmentInspectionPageDto" resultType="com.xjrsoft.module.student.vo.BaseStudentAssessmentInspectionPageVo">
        SELECT t.id,t.score,t.total_score,t.score_type,t.assessment_date,t.reason,t1.name AS assessment_category_name,t2.name AS assessment_project_name,t3.name AS semester_name,t4.name AS grade_name,t5.name AS assessment_user_name,t6.name AS score_type_cn,
        (
        SELECT NAME FROM base_class
        WHERE id = t.class_ids
        ) AS assessment_class_names,
        (
        (LENGTH(personal_student_user_ids) - LENGTH(REPLACE(personal_student_user_ids, ',', '')))
        ) + 1 AS student_count,t.enabled_mark,t.assessment_type
        FROM base_student_assessment_inspection t
        LEFT JOIN base_student_assessment_category t1 ON (t1.id = t.base_student_assessment_category_id)
        LEFT JOIN base_student_assessment_project t2 ON (t2.id = t.base_student_assessment_project_id)
        LEFT JOIN base_semester t3 ON t.base_semester_id = t3.id
        LEFT JOIN base_grade t4 ON t.grade_id = t4.id
        LEFT JOIN xjr_user t5 ON t.assessment_user_id = t5.id
        LEFT JOIN xjr_dictionary_detail t6 ON t6.code= t.score_type
        WHERE  t.delete_mark = 0 AND t1.delete_mark = 0  AND t2.delete_mark = 0  AND t3.delete_mark = 0
        <if test="dto.projectIds != null">
            and t.base_student_assessment_category_id = #{dto.projectIds}
        </if>
        <if test="dto.gradeId != null">
            and t.grade_id = #{dto.gradeId}
        </if>
        <if test="dto.semesterId != null">
            and t.base_semester_id = #{dto.semesterId}
        </if>
        <if test="dto.startDate != null">
            and t.assessment_date &gt;= #{dto.startDate}
        </if>
        <if test="dto.endDate != null">
            and t.assessment_date &lt;= #{dto.endDate}
        </if>
        <if test="dto.assessmentUserName != null and dto.assessmentUserName != ''">
            and t5.name like concat('%',#{dto.assessmentUserName},'%')
        </if>
        <if test="dto.assessmentType != null and dto.assessmentType != ''">
            and t.assessment_type like concat('%',#{dto.assessmentType},'%')
        </if>
    </select>

    <select id="getInfo" resultType="com.xjrsoft.module.student.vo.BaseStudentAssessmentInspectionVo">
        select t.id,t.score,t.score_type,t.assessment_date,t.reason,t1.name as assessment_category_name,t2.name as assessment_project_name,
        t3.name as semester_name,t4.name as grade_name,t5.name as assessment_user_name,t6.name as score_type_cn,t.file_id,t7.name as create_user_name
        ,t.total_score
        from base_student_assessment_inspection t
        left join base_student_assessment_category t1 on (t1.id = t.base_student_assessment_category_id)
        left join base_student_assessment_project t2 on (t2.id = t.base_student_assessment_project_id)
        left join base_semester t3 on t.base_semester_id=t3.id
        left join base_grade t4 on t.grade_id=t4.id
        left join xjr_user t5 on t.assessment_user_id=t5.id
        left join xjr_dictionary_detail t6 on t6.code=t.score_type
        left join xjr_user t7 on t.create_user_id=t7.id
        where t.id=#{id} and t.delete_mark=0 and t1.delete_mark=0  and t2.delete_mark=0  and t3.delete_mark=0;
    </select>

    <select id="getMobilePage" parameterType="com.xjrsoft.module.student.dto.BaseStudentAssessmentInspectionMobilePageDto" resultType="com.xjrsoft.module.student.vo.BaseStudentAssessmentInspectionMobilePageVo">
        select t.id,t1.assessment_date,t2.name as assessment_category_name,t3.name as assessment_project_name,t5.name as
        assessment_class_name,t1.reason,t1.score,t1.score_type,t4.name as score_type_cn,
        (
            SELECT count(*) FROM base_student_assessment_student_relation WHERE base_student_assessment_inspection_id =
            t1.id and class_id=t.class_id
        ) as student_count
        from base_student_assessment_class_relation t
        left join base_student_assessment_inspection t1 on t1.id=t.base_student_assessment_inspection_id
        left join base_student_assessment_category t2 on (t2.id = t1.base_student_assessment_category_id)
        left join base_student_assessment_project t3 on (t3.id = t1.base_student_assessment_project_id)
        left join xjr_dictionary_detail t4 on t4.code=t1.score_type
        left join base_class t5 on t5.id=t.class_id
        where t1.delete_mark = 0 and t.class_id in
        <foreach item="classId" index="index" collection="dto.classIds" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        <if test="dto.projectIds != null and dto.projectIds.size() > 0">
            and t1.base_student_assessment_project_id in
            <foreach item="projectId" index="index" collection="dto.projectIds" open="(" close=")" separator=",">
                #{projectId}
            </foreach>
        </if>
        <if test="dto.startDate != null and dto.startDate != ''">
            and t1.assessment_date &gt;= #{dto.startDate}
        </if>
        <if test="dto.endDate != null and dto.endDate != ''">
            and t1.assessment_date &lt;= #{dto.endDate}
        </if>
    </select>

    <select id="getMobileInfo" resultType="com.xjrsoft.module.student.vo.BaseStudentAssessmentInspectionMobileVo">
        select t.id,t.class_id,t.base_student_assessment_inspection_id,t1.score,t5.name as assessment_class_name,t2.name as assessment_category_name,t3.name as assessment_project_name,t1.assessment_date,t1.reason,
        t6.name as assessment_user_name,t7.name as create_user_name,t1.create_date,t1.file_id,t1.score_type,t4.name as score_type_cn
        from base_student_assessment_class_relation t
        left join base_student_assessment_inspection t1 on t1.id=t.base_student_assessment_inspection_id
        left join base_student_assessment_category t2 on (t2.id = t1.base_student_assessment_category_id)
        left join base_student_assessment_project t3 on (t3.id = t1.base_student_assessment_project_id)
        left join xjr_dictionary_detail t4 on t4.code=t1.score_type
        left join base_class t5 on t5.id=t.class_id
        left join xjr_user t6 on t1.assessment_user_id=t6.id
        left join xjr_user t7 on t1.create_user_id=t7.id
        where t.id=#{id};
    </select>

    <select id="getStudentIndividualBehaviorExcelVoList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentExcelDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSubTableExcelVo">
        with total_score as (
        select
        t.user_id as studentUserId,
        sum(if(t1.score_type = 'score_add', t1.score, -1 * t1.score)) as finalScore
        from base_student_assessment_student_relation t
        left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
        where t1.delete_mark = 0
        <if test="dto.yearAndMonth != null and dto.yearAndMonth != ''">
            and date_format(t1.assessment_date, '%Y-%m') = #{dto.yearAndMonth}
        </if>
        group by t.user_id
        )
        select
        t4.id as headTeacherUserId,
        t4.user_name as headTeacherUserName,
        t4.name as headTeacherName,
        t.class_id as classId,
        t.class_name as className,
        t.user_id as studentUserId,
        t.student_id as studentId,
        t.name as studentName,
        t5.base_score as baseScore,
        t2.id as baseStudentAssessmentProjectId,
        t2.name as baseStudentAssessmentProjectIdCN,
        t1.assessment_date as assessmentDate,
        if(t1.score_type = 'score_add', t1.score, -1 * t1.score) as projectScore,
        t5.base_score + t6.finalScore as finalScore
        from base_student_assessment_student_relation t
        left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
        left join base_student_assessment_project t2 on t2.id = t1.base_student_assessment_project_id
        left join base_class t3 on t3.id = t.class_id
        left join xjr_user t4 on t4.id = t3.teacher_id
        left join base_student_assessment_base_score t5 on t5.base_semester_id = t1.base_semester_id
        left join total_score t6 on t6.studentUserId = t.user_id
        where t1.delete_mark = 0
        <if test="dto.yearAndMonth != null and dto.yearAndMonth != ''">
            and date_format(t1.assessment_date, '%Y-%m') = #{dto.yearAndMonth}
        </if>
        order by t.class_id, t.user_id
    </select>

    <select id="getQuantitativeAssessmentSubTableExcelVoList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentExcelDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSubTableExcelVo">
        with total_score as (select t.user_id                                                     as studentUserId,
        sum(if(t1.score_type = 'score_add', t1.score, -1 * t1.score)) as finalScore
        from base_student_assessment_student_relation t
        left join base_student_assessment_inspection t1
        on t1.id = t.base_student_assessment_inspection_id
        where t1.delete_mark = 0
        and t1.base_student_assessment_category_id = #{dto.baseStudentAssessmentCategoryId}
        <if test="dto.classIdList != null and !dto.classIdList.isEmpty()">
            and t1.class_ids in
            <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
                #{classId}
            </foreach>
        </if>
        <if test="dto.baseStudentAssessmentCategoryId != null and dto.baseStudentAssessmentCategoryId > 0">
            and t1.base_student_assessment_category_id = #{dto.baseStudentAssessmentCategoryId}
        </if>
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t1.base_semester_id &gt; #{dto.baseSemesterId}
        </if>
        <if test="dto.startTime != null">
            and t1.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t1.create_date &lt; #{dto.endTime}
        </if>
        group by t.user_id)
        select t4.id                                                    as headTeacherUserId,
        t4.user_name                                             as headTeacherUserName,
        t4.name                                                  as headTeacherName,
        t.class_id                                               as classId,
        t.class_name                                             as className,
        t.user_id                                                as studentUserId,
        t.student_id                                             as studentId,
        t.name                                                   as studentName,
        t5.base_score                                            as baseScore,
        t2.id                                                    as baseStudentAssessmentProjectId,
        t2.name                                                  as baseStudentAssessmentProjectIdCN,
        t1.assessment_date                                       as assessmentDate,
        if(t1.score_type = 'score_add', t1.score, -1 * t1.score) as projectScore,
        t5.base_score + t6.finalScore                            as finalScore
        from base_student_assessment_student_relation t
        left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
        left join base_student_assessment_project t2 on t2.id = t1.base_student_assessment_project_id
        left join base_class t3 on t3.id = t.class_id
        left join xjr_user t4 on t4.id = t3.teacher_id
        left join base_student_assessment_base_score t5 on t5.base_semester_id = t1.base_semester_id
        left join total_score t6 on t6.studentUserId = t.user_id
        where t1.delete_mark = 0
        and t1.base_student_assessment_category_id = #{dto.baseStudentAssessmentCategoryId}
        <if test="dto.classIdList != null and !dto.classIdList.isEmpty()">
            and t1.class_ids in
            <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
                #{classId}
            </foreach>
        </if>
        <if test="dto.baseStudentAssessmentCategoryId != null and dto.baseStudentAssessmentCategoryId > 0">
            and t1.base_student_assessment_category_id = #{dto.baseStudentAssessmentCategoryId}
        </if>
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t1.base_semester_id &gt; #{dto.baseSemesterId}
        </if>
        <if test="dto.startTime != null">
            and t1.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t1.create_date &lt; #{dto.endTime}
        </if>
        order by t.class_id, t.user_id
    </select>

    <select id="getAssessmentInspectionByClassIdList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentSingleScoreDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSingleScoreVo">
        select
        t.class_ids as class_id,
        SUM(t.score * (LENGTH(personal_student_user_ids) - LENGTH(REPLACE(personal_student_user_ids, ',', '')) + 1)) AS sumScore
        from base_student_assessment_inspection t
        where t.delete_mark = 0
        <if test="dto.classIdList != null and !dto.classIdList.isEmpty()">
            and t.class_ids in
            <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
                #{classId}
            </foreach>
        </if>
        <if test="dto.baseStudentAssessmentCategoryId != null and dto.baseStudentAssessmentCategoryId > 0">
            and t.base_student_assessment_category_id = #{dto.baseStudentAssessmentCategoryId}
        </if>
        <if test="dto.baseSemesterId != null and dto.baseSemesterId > 0">
            and t.base_semester_id &gt; #{dto.baseSemesterId}
        </if>
        <if test="dto.startTime != null">
            and t.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t.create_date &lt; #{dto.endTime}
        </if>
        group by t.class_ids;
    </select>

    <select id="getConventionalManagementList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentSingleScoreDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSingleScoreVo">
        select
            t.class_id as classId,
            sum(t1.score) as score
        from base_student_assessment_class_relation t
                 left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
                 left join base_student_assessment_category t2 on t2.id = t1.base_student_assessment_category_id
        where t1.base_student_assessment_category_id = 1754411705233838080
          and t.class_id in
        <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        <if test="dto.startTime != null">
            and t1.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t1.create_date &lt; #{dto.endTime}
        </if>
        group by t.class_id;
    </select>

    <select id="getAwardsAndHonorList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentSingleScoreDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSingleScoreVo">
        select
            t.class_id as classId,
            sum(t1.score) as score
        from base_student_assessment_student_relation t
                left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
                left join base_student_assessment_category t2 on t2.id = t1.base_student_assessment_category_id
        where t1.base_student_assessment_category_id = 1754412347985760256
        and t.class_id in
        <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        <if test="dto.startTime != null">
            and t1.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t1.create_date &lt; #{dto.endTime}
        </if>
        group by t.class_id;
    </select>

    <select id="getParamilitaryList" parameterType="com.xjrsoft.module.student.dto.QuantitativeAssessmentSingleScoreDto" resultType="com.xjrsoft.module.student.vo.QuantitativeAssessmentSingleScoreVo">
        select
        t.class_id as classId,
        sum(t1.score) as score
        from base_student_assessment_student_relation t
        left join base_student_assessment_inspection t1 on t1.id = t.base_student_assessment_inspection_id
        left join base_student_assessment_category t2 on t2.id = t1.base_student_assessment_category_id
        where t1.base_student_assessment_category_id = 1754412347985760256
        and t.class_id in
        <foreach item="classId" index="index" collection="dto.classIdList" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        <if test="dto.startTime != null">
            and t1.create_date &gt; #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and t1.create_date &lt; #{dto.endTime}
        </if>
        group by t.class_id;
    </select>
    <select id="getStudentListByInspectionId"
            resultType="com.xjrsoft.module.student.entity.BaseStudentAssessmentStudentRelation">
        SELECT t1.id,t1.user_id,t1.class_id,t3.student_id,t2.name,t6.name AS nation,t4.name AS class_name,
               t5.name AS major_name,t7.name as gender,t1.score
        FROM base_student_assessment_student_relation t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        INNER JOIN base_student t3 ON t2.id = t3.user_id
        INNER JOIN base_class t4 ON t4.id = t1.class_id
        INNER JOIN base_major_set t5 ON t4.major_set_id = t5.id
        LEFT JOIN xjr_dictionary_detail t6 ON t3.nation = t6.code
        LEFT JOIN xjr_dictionary_detail t7 ON t2.gender = t7.code
        WHERE t1.base_student_assessment_inspection_id = #{id}
    </select>
</mapper>