<?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.UserInterface">
	<select id="getUserCount" parameterType="com.fuzamei.entity.User" resultType="com.fuzamei.entity.User">
		SELECT
			a.user_id,
			b.role_id,
			c.role_name,
			a.account,
			a.password,
			a.person_name,
			a.account_balance,
			a.random
		FROM
			t_user a
		LEFT JOIN user_role b ON a.user_id = b.user_id
		LEFT JOIN t_role c ON b.role_id = c.role_id 
		where a.account = #{account} and a.password = #{password} and a.user_id = #{user_id};
	</select>
	<insert id="insertUsers">
		INSERT INTO t_user (
			user_id,
			user_name,
			password,
			random,
			public_key,
			private_key,
			organization_name,
			person_name
		)
		VALUES
		<foreach collection="list" item = "item" index="index" separator=",">
			(#{item.user_id},
			#{item.user_name},
			#{item.password},
			#{item.random},
			#{item.public_key},
			#{item.private_key},
			#{item.organization_name},
			#{item.person_name})
		</foreach>
	</insert>
	<insert id="addUserToUser" parameterType="com.fuzamei.entity.User">
		insert into t_user(user_id,account,password,person_name,random,public_key,private_key,account_balance,create_time,update_time)values(#{user_id},#{account},#{password},#{person_name},#{random},#{public_key},#{private_key},#{account_balance},#{create_time},#{update_time});
	</insert>
	<insert id="addUserToUserRole" parameterType="com.fuzamei.entity.User">
		insert into user_role(user_id,role_id)values(#{user_id},#{role_id});
	</insert>
	<update id="updateUserById" parameterType="com.fuzamei.entity.User">
		update t_user set account = #{account},password = #{password},person_name = #{person_name},account_balance = #{account_balance},update_time = #{update_time} where user_id = #{user_id};
	</update>
	<update id="updataRoleById" parameterType="com.fuzamei.entity.User">
		update user_role set role_id = #{role_id} where user_id = #{user_id};
	</update>
	<select id="getUsers" parameterType="java.util.Map" resultType="int">
		SELECT
			count(1)
		FROM
			t_user a
		LEFT JOIN user_role b ON a.user_id = b.user_id
		LEFT JOIN t_role c ON b.role_id = c.role_id
		<where>
			<if test="role_name!=null and role_name!=''">
				and c.role_name = #{role_name}
			</if>
			<if test="account!=null and account!=''">
				and a.account = #{account}
			</if>
			<if test="person_name!=null and person_name!=''">
				and a.person_name = #{person_name}
			</if>
		</where>
	</select>
	<select id="getUsersByPage" parameterType="java.util.Map" resultType="com.fuzamei.entity.User">
		SELECT
			a.user_id,
			c.role_name,
			a.account,
			a.password,
			a.person_name,
			a.create_time,
			a.update_time
		FROM
			t_user a
		LEFT JOIN user_role b ON a.user_id = b.user_id
		LEFT JOIN t_role c ON b.role_id = c.role_id
		<where>
			<if test="role_name!=null and role_name!=''">
				and c.role_name = #{role_name}
			</if>
			<if test="account!=null and account!=''">
				and a.account = #{account}
			</if>
			<if test="person_name!=null and person_name!=''">
				and a.person_name = #{person_name}
			</if>
		</where>
		limit #{startNum},#{endNum};
	</select>
	<select id="findUserById" parameterType="com.fuzamei.entity.User" resultType = "com.fuzamei.entity.User">
		select * from t_user where user_id = #{user_id} or account = #{account};
	</select>
	<select id="findUserById1" parameterType="java.lang.String" resultType = "com.fuzamei.entity.User">
		select a.user_id,b.role_id,c.role_name,a.account,a.password,a.person_name,a.account_balance from t_user a LEFT JOIN user_role b ON a.user_id = b.user_id LEFT JOIN t_role c ON b.role_id = c.role_id
		 where a.user_id = #{user_id};
	</select>
</mapper>