<?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.kym.mapper.AdminUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.kym.entity.AdminUser">
        <id column="id" property="id" />
        <result column="company_id" property="companyId" />
        <result column="username" property="username" />
        <result column="password" property="password" />
        <result column="nickname" property="nickname" />
        <result column="mobile_phone" property="mobilePhone" />
        <result column="open_id" property="openId" />
        <result column="avatar" property="avatar" />
        <result column="status" property="status" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="last_login_time" property="lastLoginTime" />
    </resultMap>

    <resultMap id="AdminUserVoMap" type="com.kym.entity.vo.AdminUserVo">
        <id column="id" property="id" />
        <result column="company_id" property="companyId" />
        <result column="username" property="username" />
        <result column="password" property="password" />
        <result column="nickname" property="nickname" />
        <result column="mobile_phone" property="mobilePhone" />
        <result column="avatar" property="avatar" />
        <result column="status" property="status" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="last_login_time" property="lastLoginTime" />
        <result column="permissions" property="permissions" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, company_id, username, password, nickname, mobile_phone,open_id, avatar, status, create_time, update_time, last_login_time
    </sql>

    <select id="listUserPermissions" resultMap="AdminUserVoMap" parameterType="java.lang.Long">
        SELECT
            t1.*,
            t4.role_name,
            t4.permissions
        FROM
            t_admin_user t1
                LEFT JOIN
            (SELECT
                 t2.admin_user_id,
                 t3.role_name,
                 t3.permissions
             FROM
                 t_admin_user_role t2
                     LEFT JOIN t_role t3
                               ON t2.role_id = t3.id
             WHERE t2.admin_user_id = #{userId}) t4
            ON t1.id = t4.admin_user_id
        WHERE t1.id = #{userId}
    </select>


    <select id="listAdminUser" resultMap="AdminUserVoMap" parameterType="com.kym.entity.queryParams.CommonQueryParam">
        SELECT
        t1.*,
        t4.role_name,
        t4.permissions
        FROM
        t_admin_user t1
        LEFT JOIN
        (SELECT
        t2.admin_user_id,
        t3.role_name,
        t3.permissions
        FROM
        t_admin_user_role t2
        LEFT JOIN t_role t3
        ON t2.role_id = t3.id) t4
        ON t1.id = t4.admin_user_id
        <where>
            <if test="params.username != null">
                and t1.username = #{params.username}
            </if>
            <if test="params.mobilePhone != null">
                and t1.mobile_phone = #{params.mobilePhone}
            </if>
            <if test="params.status != null">
                and t1.status = #{params.mobilePhone}
            </if>
        </where>
    </select>

</mapper>
