<?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.koobietech.eas.dao.mapper.AdminLoginMapper">
    <resultMap id="permissionResultMap" type="com.koobietech.eas.dao.pojo.PermissionPojo">
        <!-- 映射查询结果到实体类属性 -->
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="is_active" property="isActive"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="create_uid" property="createUid"/>
        <result column="description" property="description"/>
    </resultMap>

    <resultMap id="departmentResultMap" type="com.koobietech.eas.dao.pojo.DepartmentPojo">
        <!-- 映射查询结果到实体类属性 -->
        <id column="id" property="id"/>
        <result column="depname" property="depname"/>
        <result column="address" property="address"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="manager" property="manager"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="create_uid" property="createUid"/>
    </resultMap>

    <resultMap id="userDetailResultMap" type="com.koobietech.eas.dao.pojo.UserDetailPojo">
        <!-- 映射查询结果到实体类属性 -->
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
    </resultMap>

    <resultMap id="permissionResult" type="com.koobietech.eas.dao.pojo.PermissionPojo">
        <!-- 映射查询结果到实体类属性 -->
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="is_active" property="isActive"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="create_uid" property="createUid"/>
        <result column="description" property="description"/>
    </resultMap>

    <resultMap id="departmentResult" type="com.koobietech.eas.dao.pojo.DepartmentPojo">
        <!-- 映射查询结果到实体类属性 -->
        <id column="id" property="id"/>
        <result column="depname" property="depname"/>
        <result column="address" property="address"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="manager" property="manager"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="create_uid" property="createUid"/>
    </resultMap>

    <resultMap id="studentDetailResult" type="com.koobietech.eas.dao.pojo.UserDetailPojo">
        <!-- 映射查询结果到实体类属性 -->
        <result column="id" property="id"/>
        <result column="student_name" property="username"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
    </resultMap>

    <select id="getUserPermissionsById" resultMap="permissionResultMap">
        SELECT p.id, p.name, p.is_active, p.create_time, p.modify_time, p.create_uid, p.description
        FROM eas_sys_userinfo u
                 JOIN eas_sys_pes_relation pr ON u.id = pr.department_id
                 JOIN eas_sys_permission p ON pr.permission_id = p.id
        WHERE u.id = #{adminId};
    </select>

    <select id="getUserDepartmentsById" resultMap="departmentResultMap">
        SELECT DISTINCT d.id, d.depname, d.address, d.phone, d.email, d.manager, d.create_time, d.modify_time, d.create_uid
        FROM eas_sys_userinfo u
                 JOIN eas_sys_pes_relation pr ON u.id = pr.department_id
                 JOIN eas_sys_department d ON pr.department_id = d.id
        WHERE u.id = #{adminId}
    </select>

    <select id="getUserDetailById" resultMap="userDetailResultMap">
        SELECT id, username, phone, email
        FROM eas_sys_userinfo
        WHERE id = #{adminId}
    </select>

    <select id="getStudentDetailById" resultMap="studentDetailResult">
        SELECT
            id,
            student_name,
            phone,
            email
        FROM
            eas_sys_student
        WHERE
            id = #{adminId}
    </select>

    <select id="getStudentPermissionsById" resultMap="permissionResult">
        SELECT p.id, p.name, p.is_active, p.create_time, p.modify_time, p.create_uid, p.description
        FROM eas_sys_student s
                 JOIN eas_sys_pes_relation pr ON s.id = pr.department_id
                 JOIN eas_sys_permission p ON pr.permission_id = p.id
        WHERE s.id = #{adminId};
    </select>

    <select id="getStudentDepartmentsById" resultMap="departmentResult">
        SELECT DISTINCT d.id, d.depname, d.address, d.phone, d.email, d.manager, d.create_time, d.modify_time, d.create_uid
        FROM eas_sys_student s
                 JOIN eas_sys_pes_relation pr ON s.id = pr.department_id
                 JOIN eas_sys_department d ON pr.department_id = d.id
        WHERE s.id = #{adminId}
    </select>

</mapper>