<?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.tuoheng.mapper.UserMapper">

    <resultMap type="com.tuoheng.model.dto.UserBaseInfoDto" id="UserBaseInfoMap">
        <id column="userId" jdbcType="INTEGER" property="userId"/>
        <result column="userName" jdbcType="VARCHAR" property="userName"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="isAble" jdbcType="INTEGER" property="isAble"/>
        <result column="isExpire" jdbcType="INTEGER" property="isExpire"/>
        <collection property="authorityList" ofType="java.lang.String" javaType="java.util.List">
            <result column="authority" jdbcType="VARCHAR"/>
        </collection>
        <collection property="clientRoleDtoList" ofType="com.tuoheng.model.dto.ClientRoleDto" javaType="java.util.List">
            <result column="clientId" jdbcType="VARCHAR" property="clientId"/>
            <result column="roleId" jdbcType="INTEGER" property="roleId"/>
        </collection>
    </resultMap>

    <insert id="insertUser" parameterType="com.tuoheng.model.po.UserPo" keyProperty="id" useGeneratedKeys="true">
        insert into users (username, password)
        values (#{username}, #{password})
    </insert>

    <select id="getUserBaseInfo" resultMap="UserBaseInfoMap">
        select a.id        as userId,
               a.username  as userName,
               a.password,
               b.authority,
               c.client_id as clientId,
               c.role_id   as roleId,
               a.is_able   as isAble,
               a.is_expire as isExpire
        from users a
                 left join authorities b on a.id = b.user_id
                 left join t_client_user_role c on a.id = c.user_id
        where a.username = #{username}
    </select>

    <select id="getMpUserInfo" resultMap="UserBaseInfoMap">
        select a.id        as userId,
               a.username  as userName,
               a.password,
               b.authority,
               c.client_id as clientId,
               c.role_id   as roleId,
               a.is_able   as isAble,
               a.is_expire as isExpire
        from users a
                 left join authorities b on a.id = b.user_id
                 left join t_client_user_role c on a.id = c.user_id
        where a.username = #{username}
    </select>

</mapper>
