<?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.supServer.project.mapper.UserInfoMapper">

	<resultMap type="com.supServer.project.entity.UserInfo" id="DissentInfoResult">
		<id     property="userId"       column="user_id"      />
		<result property="userName"     column="user_name"    />
		<result property="account"     	column="account"    />
		<result property="password" 	column="password"        />
		<result property="phone"  		column="phone"  />
		<result property="type" 		column="type"/>
		<result property="email"       	column="email"       />
		<result property="provinceCode"     column="province_code"     />
		<result property="cityCode"     column="city_code"       />
		<result property="areaCode"     column="area_code"       />
		<result property="createTime"   column="create_time"  />
		<result property="updateTime"   column="update_time"  />
		<result property="accountStatus" column="account_status" />
		<result property="loginTime" column="login_time" />
		<result property="sex" column="sex" />
	</resultMap>

	<select id="selectUserByUserName" parameterType="String" resultMap="DissentInfoResult">
		select user_id,user_name,password,phone,type,account,email,province_code,city_code,area_code,create_time,account_status,login_time,sex
		from sup_user_info
		where BINARY account = #{userName} and account_status = '1'
	</select>

	<select id="checkUserNameUnique" parameterType="String" resultType="int">
		select count(1) from sup_user_info where BINARY user_name = #{userName} and account_status = '1' limit 1
	</select>
	<select id="checkUserPhoneUnique" resultType="java.lang.Integer">
		select count(1) from sup_user_info where phone = #{phone} and account_status = '1' limit 1
	</select>

	<insert id="registerUser" parameterType="com.supServer.project.entity.UserInfo">
		insert into sup_user_info(user_id,user_name,account,password,phone,`type`,email,province_code,city_code,area_code,create_time,update_time,account_status,sex)
		values (#{userId},#{userName},#{account},#{password},#{phone},#{type},#{email},#{provinceCode},#{cityCode},#{areaCode},now(),now(),#{accountStatus},#{sex});
	</insert>

	<select id="selectUserByPhone" resultMap="DissentInfoResult">
		select user_id,user_name,password,phone,type,account,email,province_code,city_code,area_code,create_time,account_status,sex
		from sup_user_info
		where phone = #{phone} and account_status = '1'
	</select>

	<select id="selectUserByNumber" resultMap="DissentInfoResult">
		select user_id,user_name,password,phone,type,account,email,province_code,city_code,area_code,create_time,account_status,sex
		from sup_user_info
		where account = #{userName}
	</select>

	<update id="updateUserInfo" parameterType="com.supServer.project.entity.UserInfo">
		update sup_user_info
		<set>
			update_time = now(),
			<if test="userName != null and userName !=''">user_name = #{userName},</if>
			<if test="account != null and account !=''">account = #{account},</if>
			<if test="password != null and password !=''">password = #{password},</if>
			<if test="phone != null and phone !=''">phone = #{phone},</if>
			<if test="email != null and email !=''">email = #{email},</if>
			<if test="provinceCode != null and provinceCode !=''">province_code = #{provinceCode},</if>
			<if test="cityCode != null and cityCode !=''">city_code = #{cityCode},</if>
			<if test="areaCode != null and areaCode !=''">area_code = #{areaCode},</if>
			<if test="accountStatus != null and accountStatus !=''">account_status = #{accountStatus},</if>
			<if test="sex != null and sex !=''">sex = #{sex},</if>
		</set>
		where user_id = #{userId}
	</update>

	<update id="updateLoginTime">
		update sup_user_info set login_time = now() where user_id = #{userId}
	</update>

	<select id="getByUserId" resultType="com.supServer.project.entity.dto.UserDto">
		SELECT
			cui.user_id userId,
			cui.user_name userName,
			cui.phone,
			cui.type,
			cui.account,
			cui.email,
			cui.province_code provinceCode,
			cui.city_code cityCode,
			cui.area_code areaCode,
			cui.create_time createTime,
			cui.update_time updateTime,
			cui.account_status  accountStatus,
			cui.login_time loginTime,
		    cui.sex
		FROM
			sup_user_info cui
		WHERE
			cui.user_id = #{userId}
	</select>

	<update id="forgotPassword" >
		update sup_user_info
		<set>
			update_time = now(),
			<if test="passWord != null and passWord !=''">password = #{passWord},</if>
		</set>
		where account = #{phone}
	</update>


</mapper> 