<?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.BaseNewStudentMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.student.dto.BaseNewStudentPageDto" resultType="com.xjrsoft.module.student.vo.BaseNewStudentPageVo">
        SELECT t1.id,t1.graduate_school,t1.name,t1.gender,t2.name AS gender_cn,t1.credential_number,t1.height,t1.weight,
        t1.score,t1.graduate_class,t1.source,t5.name as source_cn,t1.stduy_status,t3.name AS stduy_status_cn,t1.mobile,t7.name as first_ambition,
        t8.name as second_ambition,t1.is_adjust,t1.status FROM base_new_student t1
        LEFT JOIN xjr_dictionary_detail t2 ON t1.gender = t2.code AND t2.item_id = 2023000000000000004
        LEFT JOIN xjr_dictionary_detail t3 ON t1.stduy_status = t3.code AND t3.item_id = 2023000000000000030
        LEFT JOIN xjr_dictionary_detail t5 ON t1.source = t5.code AND t5.item_id = 2023000000000000028
        LEFT JOIN enrollment_plan t4 ON t1.enrollment_plan_id = t4.id
        LEFT JOIN base_major_set t7 ON t1.first_ambition = t7.id
        LEFT JOIN base_major_set t8 ON t1.second_ambition = t8.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.credentialNumber != null and dto.credentialNumber != ''">
            and t1.credential_number like concat('%', #{dto.credentialNumber}, '%')
        </if>
        <if test="dto.graduateSchool != null and dto.graduateSchool != ''">
            and t1.graduate_school like concat('%', #{dto.graduateSchool}, '%')
        </if>
        <if test="dto.isImportScore != null">
            <if test="dto.isImportScore == 1">
                and t1.score is not null
            </if>
            <if test="dto.isImportScore == 0">
                and t1.score is null
            </if>
        </if>
        <if test="dto.firstAmbition != null and dto.firstAmbition != ''">
            and t1.first_ambition = #{dto.firstAmbition}
        </if>
        <if test="dto.secondAmbition != null and dto.secondAmbition != ''">
            and t1.second_ambition = #{dto.secondAmbition}
        </if>

        <if test="dto.startDate != null and dto.endDate != null">
            AND t1.create_date between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            AND t1.gender = #{dto.gender}
        </if>
        <if test="dto.status != null">
            AND t1.status = #{dto.status}
        </if>
        <if test="dto.isAdjust != null">
            AND t1.is_adjust = #{dto.isAdjust}
        </if>
        <if test="dto.enrollmentPlanId != null">
            AND t1.enrollment_plan_id = #{dto.enrollmentPlanId}
        </if>
        <if test="dto.treeId != null and dto.treeType != null">
            <if test="dto.treeType == 1">
                AND t4.grade_id = #{dto.treeId}
            </if>
            <if test="dto.treeType == 2">
                AND t4.enroll_type = #{dto.treeId}
            </if>
        </if>
        <if test="dto.field != null and dto.field != '' and dto.field == 'score'">
            <if test="dto.order == 'descend'">
                order by t1.score desc
            </if>
            <if test="dto.order == 'ascend'">
                order by t1.score asc
            </if>
        </if>
    </select>
    <select id="getEnrollmentPlanList" resultType="com.xjrsoft.module.student.vo.EnrollmentPlanTreeVo">
        SELECT t1.enroll_type, t2.name AS enroll_type_cn,t3.name AS grade_name, t3.id as grade_id,t1.id FROM enrollment_plan t1
        LEFT JOIN xjr_dictionary_detail t2 ON t1.enroll_type = t2.code AND t2.item_id = 2023000000000000027
        LEFT JOIN base_grade t3 ON t1.grade_id = t3.id
        WHERE t1.delete_mark = 0
    </select>

    <select id="getGradeList" resultType="com.xjrsoft.module.student.vo.EnrollmentPlanGradeVo">
        SELECT DISTINCT t3.id, t3.name FROM enrollment_plan t1
        LEFT JOIN xjr_dictionary_detail t2 ON t1.enroll_type = t2.code AND t2.item_id = 2023000000000000027
        LEFT JOIN base_grade t3 ON t1.grade_id = t3.id
        WHERE t1.delete_mark = 0
    </select>

    <select id="getFirstAmbitionStudentCount" resultType="com.xjrsoft.module.outint.vo.IdCountVo">
        SELECT t1.first_ambition_id,COUNT(t1.id) FROM base_new_student t1
        INNER JOIN enrollment_plan t2 ON t1.enrollment_plan_id = t2.id
        WHERE t1.delete_mark = 0 AND t2.grade_id = #{gradeId} AND t2.enroll_type = #{enrollType}
        GROUP BY t1.first_ambition_id
    </select>

    <select id="getSecondAmbitionStudentCount" resultType="com.xjrsoft.module.outint.vo.IdCountVo">
        SELECT t1.second_ambition_id,COUNT(t1.id) FROM base_new_student t1
        INNER JOIN enrollment_plan t2 ON t1.enrollment_plan_id = t2.id
        WHERE t1.delete_mark = 0 AND t2.grade_id = #{gradeId} AND t2.enroll_type = #{enrollType}
        GROUP BY t1.second_ambition_id
    </select>
    <select id="getMajorStudentCount" resultType="com.xjrsoft.module.banding.vo.IdManyCountVo">
        SELECT t1.id,(
        SELECT COUNT(id) FROM base_new_student WHERE delete_mark = 0
        AND (first_ambition_id = t1.id OR second_ambition_id = t1.id)
        AND score >= IFNULL(t2.score,0) AND height >= IFNULL(t2.height,0)
        ) AS count,(
        SELECT COUNT(id) FROM base_new_student WHERE delete_mark = 0
        AND (first_ambition_id = t1.id OR second_ambition_id = t1.id)
        AND score >= IFNULL(t2.score,0) AND height >= IFNULL(t2.height,0)
        AND gender = 'SB10001'
        ) AS male_count,(
        SELECT COUNT(id) FROM base_new_student WHERE delete_mark = 0
        AND (first_ambition_id = t1.id OR second_ambition_id = t1.id)
        AND score >= IFNULL(t2.score,0) AND height >= IFNULL(t2.height,0)
        AND gender = 'SB10002'
        ) AS female_count FROM base_major_set t1
        LEFT JOIN banding_task_major_condition t2 ON t1.id = t2.major_set_id AND t2.banding_task_id = #{id}
        AND t2.delete_mark = 0
        WHERE t1.delete_mark = 0
    </select>

</mapper>