<?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.sunyard.ssp.modules.user.mapper.ScUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.sunyard.ssp.modules.user.entity.ScUser">
        <result column="ID" property="id"/>
        <result column="USERNAME" property="username"/>
        <result column="PASSWORD" property="password"/>
        <result column="REALNAME" property="realname"/>
        <result column="ORGANIZATION_ID" property="organizationId"/>
        <result column="TEL" property="tel"/>
        <result column="EMAIL" property="email"/>
        <result column="STATUS" property="status"/>
        <result column="UPDATE_TIME" property="updateTime"/>
        <result column="PASS_UPDATE_TIME" property="passUpdateTime"/>
        <result column="POSITION_ID" property="positionId"/>
        <result column="AVATAR" property="avatar"/>
        <result column="SEX" property="sex"/>
    </resultMap>

    <resultMap id="UserResultMap" type="com.sunyard.ssp.modules.user.entity.ScUser">
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="realname" property="realname"/>
        <result column="organization_id" property="organizationId"/>
        <result column="tel" property="tel"/>
        <result column="email" property="email"/>
        <result column="status" property="status"/>
        <result column="update_time" property="updateTime"/>
        <result column="pass_update_time" property="passUpdateTime"/>
        <result column="position_id" property="positionId"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="u_publickey" property="uPublickey"/>

        <association property="positionName" select="com.sunyard.ssp.modules.user.mapper.ScPositionMapper.getNameById"
                     column="position_id"/>

        <collection property="roles" ofType="com.sunyard.ssp.modules.user.entity.ScRole">
            <result column="role_id" property="id"/>
            <result column="role_name" property="name"/>
            <result column="role_default_role" property="defaultRole"/>
            <result column="role_data_type" property="dataType"/>
            <result column="role_description" property="description"/>
        </collection>
        <collection property="permissions" ofType="com.sunyard.ssp.modules.user.entity.ScPermission">
            <result column="permission_id" property="id"/>
            <result column="permission_name" property="name"/>
            <result column="permission_parent_id" property="parentId"/>
            <result column="permission_type" property="pType"/>
            <result column="permission_sort_order" property="sortOrder"/>
            <result column="permission_component" property="component"/>
            <result column="permission_path" property="path"/>
            <result column="permission_title" property="title"/>
            <result column="permission_icon" property="icon"/>
            <result column="permission_level" property="pLevel"/>
            <result column="permission_button_type" property="buttonType"/>
            <result column="permission_status" property="pStatus"/>
            <result column="permission_url" property="url"/>
            <result column="permission_need_verify" property="needVerify"/>
        </collection>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID
        , USERNAME, PASSWORD, REALNAME, ORGANIZATION_ID, TEL, EMAIL, STATUS, UPDATE_TIME, POSITION_ID, AVATAR, SEX, PASS_UPDATE_TIME
    </sql>

    <sql id="User_Vo_Column_List">
        tu
        .
        id
        ,
        tu.username,
        tu.realname,
        tu.organization_id,
        tu.tel,
        tu.email,
        tu.status,
        tu.update_time,
        tu.pass_update_time,
        tu.position_id,
        tu.password,
        tu.sex,
        tu.avatar,
        tu.u_publickey,
        tr.id          as role_id,
        tr.name          as role_name,
        tr.default_role          as role_default_role,
        tr.data_type          as role_data_type,
        tr.description          as role_description,
        tp.id     as  permission_id,
        tp.name     as  permission_name,
        tp.parent_id     as  permission_parent_id,
        tp.p_type     as  permission_type,
        tp.sort_order     as  permission_sort_order,
        tp.component     as  permission_component,
        tp.path     as  permission_path,
        tp.title     as  permission_title,
        tp.icon     as  permission_icon,
        tp.p_level     as  permission_level,
        tp.button_type     as  permission_button_type,
        tp.p_status     as  permission_status,
        tp.url     as  permission_url,
        tp.need_verify as permission_need_verify
    </sql>


    <select id="findByUsername" resultMap="UserResultMap">
        select DISTINCT
        <include refid="User_Vo_Column_List"/>
        from sc_user tu
        left join sc_user_role tur on tu.id = tur.user_id
        left join sc_role_permission trp on tur.role_id = trp.role_id
        left join sc_permission tp on tp.id = trp.permission_id
        left join sc_role tr on tr.id = tur.role_id
        where username = #{username}
    </select>

    <select id="findall" resultMap="UserResultMap">
        select
        <include refid="Base_Column_List"/>
        from sc_user
    </select>
</mapper>
