<?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.organization.mapper.UserMapper">
    <select id="getResetUserPage" parameterType="com.xjrsoft.module.organization.dto.UserPageDto" resultType="com.xjrsoft.module.organization.vo.ResetUserPageVo">
        SELECT DISTINCT t1.*,t3.name AS gender_cn FROM xjr_user t1
        INNER JOIN xjr_user_role_relation t2 ON t2.user_id = t1.id
        LEFT JOIN xjr_dictionary_detail t3 ON t1.gender = t3.code
        WHERE t1.delete_mark = 0
        <if test="dto.keyword != null and dto.keyword !=''">
            and (t1.name like concat('%', #{dto.keyword}, '%') or t1.mobile like concat('%', #{dto.keyword}, '%'))
        </if>
        <if test="dto.roleId != null">
            and t2.role_id = #{dto.roleId}
        </if>
    </select>
    <select id="getInfosByParam" parameterType="com.xjrsoft.module.student.dto.BaseStudentSimpleInfoDto" resultType="com.xjrsoft.module.student.vo.BaseStudentSompleInfoVo">
        SELECT t1.id, t1.name as student_name, t1.enabled_mark, t1.credential_number, t4.name AS class_name,t1.mobile,
        (SELECT name FROM base_student_family_member WHERE delete_mark = 0 AND user_id = t1.id ORDER BY create_date ASC LIMIT 0,1) as parent_name,
        (SELECT mobile FROM base_student_family_member WHERE delete_mark = 0 AND user_id = t1.id ORDER BY create_date ASC LIMIT 0,1) as parent_mobile,
        t1.gender,t4.id as class_id, t4.teacher_id,t6.name as teacher_name,t6.user_name as teacher_user_name,t6.mobile as teacher_mobile,t5.name as classroom_name
        FROM xjr_user t1
        INNER JOIN base_student t2 ON t1.id = t2.user_id
        INNER JOIN base_student_school_roll t3 ON t1.id = t3.user_id
        INNER JOIN base_class t4 ON t4.id = t3.class_id
        left join base_classroom t5 on t4.classroom_id = t5.id
        left join xjr_user t6 on t4.teacher_id = t6.id
        WHERE t1.delete_mark = 0
        <if test="dto.keyword != null and dto.keyword != ''">
            AND (t1.name like concat('%', #{dto.keyword}, '%') OR t1.credential_number like concat('%', #{dto.keyword}, '%'))
        </if>
        <if test="dto.name != null and dto.name != '' and dto.idNumber != null and dto.idNumber != ''">
            AND t1.name = #{dto.name} and t1.credential_number = #{dto.idNumber}
        </if>
        <if test="dto.userId != null">
            AND t1.id = #{dto.userId}
        </if>
    </select>
    <select id="personPage" parameterType="com.xjrsoft.module.organization.dto.PersonPageDto" resultType="com.xjrsoft.module.organization.vo.PersonPageVo">
        SELECT t1.id,t1.name,t1.user_name,t1.mobile,t3.name AS gender_cn,t2.employ_type,t2.employ_way FROM xjr_user t1
        LEFT JOIN base_teacher t2 ON t1.id = t2.user_id
        LEFT JOIN xjr_dictionary_detail t3 ON t1.gender = t3.code
        LEFT JOIN xjr_user_dept_relation t4 ON t1.id = t4.user_id
        WHERE t1.delete_mark = 0
        AND (
            t1.id IN (SELECT user_id FROM base_student WHERE delete_mark = 0)
            OR t1.id IN (SELECT user_id FROM base_teacher WHERE delete_mark = 0)
        )
        <if test="dto.employType != null and dto.employType != ''">
            AND t2.employ_type = #{dto.employType}
        </if>
        <if test="dto.employWay != null and dto.employWay != ''">
            AND t2.employ_way = #{dto.employWay}
        </if>
        <if test="dto.mobile != null and dto.mobile != ''">
            AND t1.mobile like concat('%', #{dto.mobile}, '%')
        </if>
        <if test="dto.userName != null and dto.userName != ''">
            AND t1.user_name like concat('%', #{dto.userName}, '%')
        </if>
        <if test="dto.name != null and dto.name != ''">
            AND t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.departmentId != null">
            AND t4.dept_id = #{dto.departmentId}
        </if>
    </select>
    <select id="getTeacherDeptName" resultType="com.xjrsoft.module.organization.vo.UserIdDeptNameVo">
        SELECT t1.id,(
            SELECT GROUP_CONCAT(c1.name) FROM xjr_department c1
            INNER JOIN xjr_user_dept_relation c2 ON c1.id = c2.dept_id
            WHERE c2.user_id = t1.id
        ) AS dept_name,(
            SELECT GROUP_CONCAT(c1.id) FROM xjr_department c1
            INNER JOIN xjr_user_dept_relation c2 ON c1.id = c2.dept_id
            WHERE c2.user_id = t1.id
        ) as dept_ids FROM xjr_user t1
        INNER JOIN xjr_user_role_relation t2 ON t1.id = t2.user_id
        WHERE t2.role_id = 2
    </select>
</mapper>