<?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.fuzamei.mapperInterface.UserAuthoricationMapper">
	
	<!-- 根据userId和角色名称查询用户所有详细信息 -->
	<select id="queryUserDetail" resultMap="UserDetailRM">
		select 
			temp.id,
			temp.user_id,
			temp.username,
			temp.password,
			temp.p_id,
			temp.role_id,
			temp.role_name,
			temp.person_name,
			temp.random,
			temp.create_time,
			temp.update_time,
			m.id as id2,
			m.module_id,
			m.module_name,
			supplier.id as id3,
			supplier.user_id as s_user_id,
			supplier.username as s_username,
			supplier.person_name as s_person_name
		from 
			(select 
				u.id,
				u.user_id,
				u.username,
				u.password,
				u.p_id,
				u.role_id,
				r.role_name,
				u.person_name,
				u.random,
				u.create_time,	
				u.update_time
			from 
				users u
			left join 
				role r
			on 
				u.role_id=r.role_id
			where u.user_id=#{userId})temp
		left join 
			role_module rm
		on 
			rm.role_id=temp.role_id
		left join 
			module m
		on 
			m.module_id=rm.module_id
		left join 
			planner_supplier ps
		on 
			temp.user_id=ps.planner_id
		left join 
			users supplier
		on 
			ps.supplier_id=supplier.user_id
	</select>
	
	<resultMap type="UserDetail" id="UserDetailRM" autoMapping="true">
		<id column="id" property="id"/>
		<collection property="modules" ofType="Module" javaType="java.util.List">
			<id column="id2" property="id"/>
			<result column="module_name" property="module_name"/>
		</collection>
		<collection property="users" ofType="UserDetail" javaType="java.util.List">
			<id column="id3" property="id"/>
			<result column="s_user_id" property="userId"/>
			<result column="s_person_name" property="personName"/>
			<result column="s_username" property="username"/>
		</collection>
	</resultMap>
	
	<!-- 根据id和角色名称查询用户是否存在来判断权限 -->
	<select id="queryUserAuthority" resultType="UserDetail">
		select 
			u.id,
			u.user_id,
			u.username,
			u.password,
			u.p_id,
			u.role_id,
			r.role_name,
			u.person_name,
			u.random,
			u.create_time,	
			u.update_time 
		from
			users u
		left join 
			role r
		on 
			u.role_id=r.role_id
		where 
			u.user_id=#{userId}
	</select>
	
	<select id="showAllCarriersUnderSupplier" resultType="UserDetail">
		select 
			u.user_id,
			u.person_name
		from 
			users u
		where 
			u.role_id=3
	</select>
	
	<select id="verificationToken" resultType="int">
		select 
			count(*) 
		from
			users u
		left join 
			token t
		on 
			u.user_id=t.user_id
		where 
			u.user_id=#{userId}
		and 
			t.token=#{token}
	</select>
	
	
</mapper>