<?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.fdage.dao.cust.TbUserMapperCust">
  <select id="findByUserName" parameterType="java.lang.String" resultMap="com.fdage.dao.base.TbUserMapper.BaseResultMap">
    select 
    <include refid="com.fdage.dao.base.TbUserMapper.Base_Column_List" />
    from tb_user
    where user_name = #{userName}
  </select>

  <select id="findUserList" parameterType="com.fdage.pojo.TbUser" resultType="com.fdage.respon.ResponUser">
    select
      a.id, a.user_name as userName, a.password, a.role_id as roleId, a.phone, a.head, a.state,
      a.create_time AS createTime, c.id as roleId, c.role_name as roleName
    from tb_user a left join tb_user_role b on a.id = b.user_id
    left join tb_role c on b.role_id = c.id
    where 1 = 1
    <if test="state != null">
      and a.state = #{state}
    </if>
    <if test="userName != null">
      and a.user_name like CONCAT('%', #{userName}, '%')
    </if>

    ORDER by a.create_time DESC
  </select>

  <select id="roleList" resultType="com.fdage.pojo.TbRole">
  select id, role_name as roleName, description, state
  from tb_role where state = 0
</select>

  <select id="findUserRoleByUserId" resultType="java.lang.Integer">
    select role_id from tb_user_role where user_id= #{userId}
  </select>

  <select id="findRoleByUserId" parameterType="java.lang.Long" resultType="java.lang.String">
    select r.role_key from
    (tb_user u left join tb_user_role ur on u.id = ur.user_id) left join tb_role r on r.id = ur.role_id
    WHERE u.id = #{id}
  </select>
</mapper>