<?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.banding.mapper.BandingTaskClassMapper">
    <select id="getList" parameterType="com.xjrsoft.module.banding.dto.BandingTaskClassPageDto" resultType="com.xjrsoft.module.banding.vo.BandingTaskClassPageVo">
        SELECT t1.id, t1.major_set_id,t2.name AS major_set_name, t1.name, t1.number,t1.sort_code,t1.is_order_class,t1.class_type,t1.status,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN enrollment_plan a2 ON a1.enrollment_plan_id = a2.id
        WHERE a1.delete_mark = 0 AND a1.first_ambition_id = t1.major_set_id
        AND a2.enroll_type = #{dto.enrollType} AND a2.grade_id =#{dto.gradeId} ) AS first_ambition_count,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN enrollment_plan a2 ON a1.enrollment_plan_id = a2.id
        WHERE a1.delete_mark = 0 AND a1.second_ambition_id = t1.major_set_id
        AND a2.enroll_type = #{dto.enrollType} AND a2.grade_id =#{dto.gradeId} ) AS second_ambition_count,
        t5.height, t5.score, (
            SELECT count(a1.id) FROM base_new_student a1
            INNER JOIN banding_task_class_student t5 ON a1.id = t5.new_student_id
            WHERE a1.delete_mark = 0 AND t5.delete_mark = 0 AND t5.banding_task_class_id = t1.id
        ) as satisfy_count, (
            SELECT count(a1.id) FROM base_new_student a1
            LEFT JOIN enrollment_plan a2 ON a1.enrollment_plan_id = a2.id
            LEFT JOIN banding_task a3 ON a2.grade_id = a3.grade_id AND a2.enroll_type = a3.enroll_type
            WHERE a1.delete_mark = 0 AND a3.id = t1.banding_task_id
            AND (a1.first_ambition_id = t1.major_set_id or a1.second_ambition_id = t1.major_set_id)
            and a1.id not in (SELECT new_student_id FROM banding_task_class_student c1
            INNER JOIN banding_task_class c2 ON c1.banding_task_class_id = c2.id
            WHERE c1.delete_mark = 0 AND c2.delete_mark = 0 AND c2.banding_task_id = t1.banding_task_id
            )
        ) as surplus_count, t3.name as teacher_name,t3.id as teacher_id, t4.name as classroom_name,t4.id as classroom_id FROM banding_task_class t1
        LEFT JOIN base_major_set t2 ON t1.major_set_id = t2.id
        LEFT JOIN xjr_user t3 ON t1.teacher_id = t3.id
        LEFT JOIN base_classroom t4 ON t1.classroom_id = t4.id
        LEFT JOIN banding_task_major_condition t5 ON t1.major_set_id = t5.major_set_id AND t5.delete_mark = 0
        AND t5.banding_task_id = #{dto.bandingTaskId}
        WHERE t1.delete_mark = 0 and t1.banding_task_id = #{dto.bandingTaskId}
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name},'%')
        </if>
        <if test="dto.majorSetName != null and dto.majorSetName != ''">
            and t2.name like concat('%', #{dto.majorSetName},'%')
        </if>
        ORDER BY t1.sort_code IS NULL, t1.sort_code ASC
    </select>

    <select id="getClassSure" parameterType="com.xjrsoft.module.banding.dto.BandingTaskClassPageDto" resultType="com.xjrsoft.module.banding.vo.BandingTaskClassSureListVo">
        SELECT t1.id, t1.major_set_id,t2.name AS major_set_name, t1.name,t1.sort_code,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN banding_task_class_student a2 ON a1.id = a2.new_student_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a2.banding_task_class_id = t1.id) AS number,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN banding_task_class_student a2 ON a1.id = a2.new_student_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a2.banding_task_class_id = t1.id AND a1.gender = 'SB10001') AS male_count,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN banding_task_class_student a2 ON a1.id = a2.new_student_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a2.banding_task_class_id = t1.id AND a1.gender = 'SB10002') AS female_count,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN banding_task_class_student a2 ON a1.id = a2.new_student_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a2.banding_task_class_id = t1.id AND a1.stduy_status = 'FB3001') AS not_stay_count,
        (SELECT COUNT(*) FROM base_new_student a1
        INNER JOIN banding_task_class_student a2 ON a1.id = a2.new_student_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a2.banding_task_class_id = t1.id AND a1.stduy_status = 'FB3002') AS stay_count,
        t3.name AS teacher_name,t4.name as classroom_name, t1.is_order_class FROM banding_task_class t1
        LEFT JOIN base_major_set t2 ON t1.major_set_id = t2.id
        LEFT JOIN xjr_user t3 ON t1.teacher_id = t3.id
        LEFT JOIN base_classroom t4 ON t1.classroom_id = t4.id
        WHERE t1.delete_mark = 0 AND t1.banding_task_id = #{dto.bandingTaskId}
    </select>

    <select id="getClassStudent" parameterType="com.xjrsoft.module.banding.dto.BandingTaskClassStudentPageDto" resultType="com.xjrsoft.module.banding.vo.BandingTaskClassStudentListVo">
        SELECT t1.id,t1.credential_number,t1.name,t4.name AS gender_cn,t1.graduate_school,t1.graduate_class,
        t5.name AS first_ambition, t6.name AS second_ambition,t7.name AS stduy_status_cn,t1.height,t1.weight,t1.score,t3.name AS class_name,
        t8.name AS teacher_name FROM base_new_student t1
        LEFT JOIN banding_task_class_student t2 ON t1.id = t2.new_student_id AND t2.delete_mark = 0
        LEFT JOIN banding_task_class t3 ON t2.banding_task_class_id = t3.id
        AND t3.delete_mark = 0 AND t3.banding_task_id = #{dto.bandingTaskId}
        LEFT JOIN xjr_dictionary_detail t4 ON t1.gender = t4.code
        LEFT JOIN base_major_set t5 ON t1.first_ambition = t5.id
        LEFT JOIN base_major_set t6 ON t1.second_ambition = t6.id
        LEFT JOIN xjr_dictionary_detail t7 ON t1.stduy_status = t7.code
        LEFT JOIN xjr_user t8 ON t3.teacher_id = t8.id
        INNER JOIN enrollment_plan t9 ON t1.enrollment_plan_id = t9.id
        WHERE t1.delete_mark = 0
        and t9.grade_id = #{dto.gradeId} and t9.enroll_type = #{dto.enrollType}
        <if test="dto.credentialNumber != null and dto.credentialNumber != ''">
            AND t1.credential_number like concat('%', #{dto.credentialNumber}, '%')
        </if>
        <if test="dto.name != null and dto.name != ''">
            AND t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.graduateSchool != null and dto.graduateSchool != ''">
            AND t1.graduate_school like concat('%', #{dto.graduateSchool}, '%')
        </if>
        <if test="dto.graduateClass != null and dto.graduateClass != ''">
            AND t1.graduate_class like concat('%', #{dto.graduateClass}, '%')
        </if>
        <if test="dto.stduyStatus != null and dto.stduyStatus != ''">
            AND t1.stduy_status = #{dto.stduyStatus}
        </if>
        <if test="dto.secondAmbition != null">
            AND t1.second_ambition = #{dto.secondAmbition}
        </if>
        <if test="dto.firstAmbition != null">
            AND t1.first_ambition = #{dto.firstAmbition}
        </if>
        <if test="dto.status != null">
            <if test="dto.status == 1">
                AND t3.name is not null
            </if>
            <if test="dto.status == 0">
                AND t3.name is null
            </if>
        </if>
        <if test="dto.className != null and dto.className != ''">
            AND t3.name like concat('%', #{dto.className}, '%')
        </if>
        <if test="dto.order != null">
            <if test="dto.order == 'ascend'">
                <if test="dto.field != null and dto.field == 'height'">
                    order by t1.height asc
                </if>
                <if test="dto.field != null and dto.field == 'weight'">
                    order by t1.weight asc
                </if>
                <if test="dto.field != null and dto.field != '' and dto.field == 'score'">
                    order by t1.score asc
                </if>
            </if>
            <if test="dto.order == 'descend'">
                <if test="dto.field != null and dto.field == 'height'">
                    order by t1.height desc
                </if>
                <if test="dto.field != null and dto.field == 'weight'">
                    order by t1.weight desc
                </if>
                <if test="dto.field != null and dto.field != '' and dto.field == 'score'">
                    order by t1.score desc
                </if>
            </if>
        </if>

    </select>

    <select id="getMajorClassCount" resultType="com.xjrsoft.module.outint.vo.IdCountVo">
        SELECT major_set_id as id,COUNT(*) as count FROM banding_task_class WHERE banding_task_id = #{id} AND delete_mark = 0 GROUP BY major_set_id
    </select>

    <select id="getMajorClassStudentCount" resultType="com.xjrsoft.module.outint.vo.IdCountVo">
        SELECT major_set_id as id,sum(number) as count FROM banding_task_class WHERE banding_task_id = #{id} AND delete_mark = 0 GROUP BY major_set_id
    </select>

    <select id="getListOrderByAsc" resultType="com.xjrsoft.module.banding.entity.BandingTaskClass">
        SELECT * FROM banding_task_class WHERE delete_mark = 0 and banding_task_id = #{id} and status = 0 ORDER BY sort_code IS NULL, sort_code ASC
    </select>
</mapper>