<?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.TbRoleMapperCust">

  <select id="findRoleResourceByRoleId" resultType="java.lang.String">
    select resource_id from tb_role_resource where role_id= #{userId}
  </select>

  <delete id="deleteRoleResource" parameterType="java.lang.Long">
    delete from tb_role_resource where role_id=#{roleId}
  </delete>

  <insert id="saveRoleResource" parameterType="Map">
    insert into tb_role_resource (role_id, resource_id) values (#{roleId}, #{resourceId});
  </insert>

  <insert id="saveRoleUser" parameterType="Map">
    insert into tb_user_role (role_id, user_id) values (#{roleId}, #{userId});
  </insert>

  <delete id="deleteRoleUser" parameterType="java.lang.Long">
    delete from tb_user_role where user_id = #{userId}
  </delete>

  <select id="findRoleList" resultType="com.fdage.respon.ResponRole">
    select id, role_name as roleName, description, state, create_time as createTime, role_key as roleKey from tb_role
  </select>

  <select id="findUserNameByRoleId" parameterType="java.lang.Long" resultType="java.lang.String">
    SELECT a.user_name AS userName 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 c.id = #{roleId}
  </select>

  <select id="findUserByRoleId" parameterType="java.lang.Long" 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
    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 c.id = #{roleId}
  </select>

</mapper>