<?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.sqx.modules.sys.dao.SysUserDao">
	<!-- 查询用户的所有权限 -->
	<select id="queryAllPerms" resultType="string">
		select m.perms from sys_user_role ur 
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id 
			LEFT JOIN sys_menu m on rm.menu_id = m.menu_id 
		where ur.user_id = #{userId}
	</select>
	
	<!-- 查询用户的所有菜单ID --> 
	<select id="queryAllMenuId" resultType="long">
		select distinct rm.menu_id from sys_user_role ur 
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id 
		where ur.user_id = #{userId}
	</select>
	
	<select id="queryByUserName" resultType="com.sqx.modules.sys.entity.SysUserEntity">
		select * from sys_user where username = #{username}
	</select>

	<insert id="insertSysUser" parameterType="com.sqx.modules.sys.entity.SysUserEntity" useGeneratedKeys="true" keyProperty="userId">
		insert into sys_user (username, password, salt, email, mobile, status, create_time, user_type) values
		(#{username}, #{password}, #{salt}, #{email}, #{mobile}, #{status}, #{createTime}, #{userType})
	</insert>

	<select id="selectSysUserList" resultType="com.sqx.modules.sys.entity.SysUserEntity">
		select * from sys_user where user_type=1
		<if test="username!=null and username!=''">
			and username like concat('%',#{username},'%')
		</if>
		<if test="shopName!=null and shopName!=''">
			and user_id in (select user_id from sys_user_shop where shop_id in (select shop_id from goods_shop where shop_name like concat('%',#{shopName},'%')))
		</if>
	</select>

	<select id="selectUserShop" resultType="com.sqx.modules.datacentre.entity.SysUserShop">
		select * from sys_user_shop where user_id = #{adminUserId}
	</select>

	<select id="selectUserByShopId" resultType="com.sqx.modules.datacentre.entity.SysUserShop">
		select * from sys_user_shop where shop_id = #{shopId} and user_id in (select user_id from sys_user where user_type=2)
	</select>

</mapper>