<?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.xluobo.business.sc.student.repo.mapper.ScStudentMapper">

    <select id="selectForSearchTable" resultType="cn.xluobo.business.sc.student.domain.resp.RespSearchStudent">
        select student_id, a.school_id, student_name, birth_day, TIMESTAMPDIFF(YEAR, a.birth_day, CURDATE()) as age, sex, phone, in_time,
               b.school_name,
               (select group_concat(sc.contact_nick,'(',fcn_dict_name(sc.contact_relation,'contact_relation'),') ',sc.contact_phone separator ';') from sc_student_contact sc where sc.student_id=a.student_id) as contact_info
        from sc_student a left join sc_school b on a.school_id=b.school_id
        where a.delete_flag='0'
        <if test="reqSearchScStudent.schoolId != null and reqSearchScStudent.schoolId != ''">
            and a.school_id = #{reqSearchScStudent.schoolId}
        </if>
        <if test="reqSearchScStudent.studentName != null and reqSearchScStudent.studentName != ''">
            and a.student_name like concat('%',#{reqSearchScStudent.studentName} ,'%')
        </if>
        <if test="reqSearchScStudent.sex != null and reqSearchScStudent.sex != ''">
            and a.sex = #{reqSearchScStudent.sex}
        </if>
        <if test="reqSearchScStudent.phone != null and reqSearchScStudent.phone != ''">
            and a.phone = #{reqSearchScStudent.phone}
        </if>
        order by a.create_time desc
    </select>
    <select id="selectForSelect" resultType="cn.xluobo.business.sc.student.domain.resp.RespSearchStudent">
        select student_id,student_name from sc_student where delete_flag='0'
        <if test="studentSelect.search != null and studentSelect.search != ''">
            and student_name like concat('%',#{studentSelect.search} ,'%')
        </if>
        order by create_time desc, student_id desc
    </select>
    <select id="selectAllStudent" resultType="cn.xluobo.business.sc.student.domain.resp.RespSearchStudent">
        select student_id,student_name from sc_student a
        where delete_flag='0'
        <if test="deptId != null">
            and exists(select 1 from sc_student_course b where a.student_id=b.student_id and b.dept_id=#{deptId})
        </if>
        <if test="deptId == null and userId != null and userId != ''">
            and exists(select 1 from sc_student_course b,sys_user_dept ud
            where a.student_id=b.student_id and ud.user_id=#{userId}
            and (ud.dept_id=b.dept_id or ud.dept_id=-1))
        </if>
        order by create_time desc, student_id desc
    </select>
    <select id="selectStudentCount" resultType="java.lang.Integer">
        select count(1) from sc_student s
        where delete_flag='0'
        <if test="deptId != null">
            and exists(select 1 from sc_student_course sc where sc.dept_id=#{deptId} and s.student_id=sc.student_id and sc.status in ('1', '2'))
        </if>
        <if test="deptId == null and userId != null and userId != ''">
            and exists(select 1 from sc_student_course sc, sys_user_dept ud
            where ud.user_id=#{userId}
              and (ud.dept_id=sc.dept_id or ud.dept_id=-1)
              and s.student_id=sc.student_id
              and sc.status in ('1', '2'))
        </if>
    </select>
    <select id="selectTenantStudentCount" resultType="java.lang.Integer">
        select count(1) from sc_student where tenant_id=#{tenantId} and delete_flag='0'
    </select>
</mapper>
