<?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.UserMapper">
	<!--查询所有角色  -->
	<select id="getAllRoles" resultType="com.fuzamei.entity.Role">
		select role_id ,role_name  from role;
	</select>
	
	<!--添加（id前端传来的）角色，账号，密码，名称  -->
	<insert id="insertUsers" parameterType="com.fuzamei.entity.User">
		insert into  users(user_id,role_id,username,password,person_name,random,create_time,update_time)  values(#{userIdb},#{roleId},#{username},#{password},#{personName},#{random},#{createTime},#{updateTime})
	</insert>
	
	<!--先去数据库查询 看是否已有的账号不能在重复添加了    -->
	<select id="findUserById" parameterType="com.fuzamei.entity.User" resultType = "com.fuzamei.entity.User">
		select * from users where user_id = #{userIdb} or  username = #{username}
	</select>
	
	<!--先去数据库查询 看是否已有的账号不能在修改了  -->
	<select id="findUserByuserIdandusername" parameterType="com.fuzamei.entity.User" resultType = "com.fuzamei.entity.User">
		select * from users where user_id != #{userId} and  username = #{username}
	</select>
	<!--根据用户id  查询角色名称  --><!-- ？？？？？？？？？？待待删？？？ -->
	<select id="selectRoleNameByuserId">
		select r.role_name from   users y left  join  role r on r.role_id=y.role_id  where user_id=1003
	</select>
	<!-- 查询账号设置的列表 -->
	<select id="queryUsers" parameterType="java.util.Map" resultType="com.fuzamei.entity.User">
		select   y.user_id,
		         r.role_name,
		         y.role_id,
		         y.username,
		         y.password,
		         y.person_name,
		         y.create_time,
		         y.update_time
		 from    users y  
		 left  join  role  r on  r.role_id=y.role_id
		 <where>
		      <if test="roleId!=null and roleId!=''">
		      	   y.role_id=#{roleId}
		      </if>
		       <if test="username!=null and username!=''">
		      	and  y.username=#{username}
		      </if>
		      <if test="personName!=null and personName!=''">
		      	and  y.person_name=#{personName}
		      </if>
		 </where>
		      order by create_time desc
			  limit #{startPage},#{rowNum}
	</select>
	
	<select id="queryUserscount" parameterType="java.util.Map" resultType="int">
		  select count(*)  from (
		     select      y.user_id,
				         r.role_name,
				         y.role_id,
				         y.username,
				         y.password,
				         y.person_name,
				         y.create_time,
				         y.update_time
		         from    users y  
		 left  join  role  r on  r.role_id=y.role_id
		 <where>
		      <if test="roleId!=null and roleId!=''">
		      	   y.role_id=#{roleId}
		      </if>
		       <if test="username!=null and username!=''">
		      	and  y.username=#{username}
		      </if>
		      <if test="personName!=null and personName!=''">
		      	and  y.person_name=#{personName}
		      </if>
		 </where>)te 
	</select>
	
	<!--根据用户id修改角色，账号，密码，名称  -->
	<update id="updateAccountByUserId" parameterType="com.fuzamei.entity.User">
		update users set  username=#{username} , password=#{password} , person_name=#{personName},update_time=#{updateTime} where user_id=#{userId}
	</update>
	
	<!--参数类型为1001,1002,1003等， 批量删除 -->
	<delete id="deleteFromUserId" parameterType="string">
		delete  from   users   where  user_id in 
		<foreach item="userids" collection="array" open="(" separator="," close=")">
            #{userids}
        </foreach>
	</delete>
	
	<select id="queryUserIdMax" resultType="int">
	  select max(user_id) from users
	</select>
	
	<!-- 查询数据库得随机数是不是又是一样得 -->
	<select id="queryRondomRepetition" parameterType="com.fuzamei.entity.User" resultType="int" >
			select count(*)  from  users where random=#{random}
	</select>
	
	<!--插入，删除，修改 ， 全 往数据库插入操作记录信息-->
	<insert id="insertOperationHistory">
		insert into  
		           operation_history_admin(operator_id,operation_type_id,be_operated_id,operation_time,hash)
		    values(
		            #{userId},
		            #{operationTypeId},
		            #{userIdb},
		            #{operationTime},
		            #{hash})
	</insert>
	

</mapper>