<?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.ruoyi.web.userLogin.mapper.WechatUserLoginMapper">

	<!--验证登录用户信息-->
	<select id="findUserInfo" parameterType="String" resultType="map">
		SELECT
			su.*,
			IFNULL(GROUP_CONCAT( sur.role_id ),'') AS roleId,
			IFNULL(GROUP_CONCAT( sr.role_name ),'') AS roleName,
			d.dept_name,
			MIN(sur.role_id) AS minRoleId,
			IFNULL( spu.project_id, '' ) AS projectId
		FROM
			sys_user su
		LEFT JOIN sys_user_role sur ON su.user_id = sur.user_id
		LEFT JOIN sys_role sr ON sur.role_id = sr.role_id
		LEFT JOIN sur_project_userinfo spu ON su.user_id = spu.user_id
		left join sys_dept d on d.dept_id = su.dept_id
		WHERE
			1 = 1
			AND su.phonenumber=#{loginName,jdbcType=VARCHAR}
			AND su.del_flag=0
			AND su.status=0
		GROUP BY su.user_id
	</select>

	<!--添加用户openid信息-->
	<insert id="addUserOpenId" parameterType="map">
		insert into sys_user_openid
		(
		<if test="openId != null and openId != ''">openId,</if>
		<if test="userId != null and userId != ''">userId,</if>
		<if test="loginName != null and loginName != ''">loginName,</if>
		<if test="nickname != null and nickname != ''">nickname,</if>
		<if test="unionid != null and unionid != ''">unionid,</if>
		creatTime
		) values
		(
		<if test="openId != null and openId != ''">#{openId},</if>
		<if test="userId != null and userId != ''">#{userId},</if>
		<if test="loginName != null and loginName != ''">#{loginName},</if>
		<if test="nickname != null and nickname != ''">#{nickname},</if>
		<if test="unionid != null and unionid != ''">#{unionid},</if>
		now()
		)
	</insert>

	<!--更新用户openid-->
	<update id="updateUserOpenId" parameterType="String">
		update sys_user_openid set userId=#{userId},loginName=#{loginName},nickname=#{nickname},unionid=#{unionid} where openId =#{openId} and isDel = 0
	</update>

	<!--根据电话查询用户id-->
	<select id="getUserIdByPhone" parameterType="String" resultType="String">
		select user_id from sys_user where 1=1 and del_flag=0 and phonenumber=#{phone} and status=0
	</select>

	<!--根据用户openid查询用户信息-->
	<select id="getUserInfoByOpenId" parameterType="String" resultType="map">
		SELECT
		IFNULL(su.user_id,'') AS userId,
		IFNULL(su.phonenumber,'') AS loginName,
		IFNULL(su.user_name,'') AS userName,
		IFNULL(su.nick_name,'') AS nickName,
		IFNULL(su.nick_name,'') AS nickName,
		IFNULL(su.dept_id,'') AS deptId,
		IFNULL(d.dept_name,'') AS deptName,
		IFNULL(su.remark,'') AS  remark,
		IFNULL(GROUP_CONCAT( sur.role_id ),'') AS roleId,
		IFNULL(GROUP_CONCAT( sr.role_name ),'') AS roleName,
		'' AS projectId
		FROM
		sys_user_openid suo
		LEFT JOIN sys_user su ON suo.userId = su.user_id
		LEFT JOIN sys_user_role sur ON su.user_id = sur.user_id
		LEFT JOIN sys_role sr ON sur.role_id = sr.role_id
		left join sys_dept d on d.dept_id = su.dept_id
		WHERE
		1 = 1
		AND suo.isDel = 0
		AND su.del_flag = 0
		AND su.STATUS = 0
		AND suo.openId =#{openId}
		GROUP BY suo.openId
	</select>

	<!--根据用户openid查询用户名称-->
	<select id="getUserNameByOpenId" parameterType="String" resultType="map">
		SELECT
			id,
			openId,
			userId,
			loginName,
			nickname
		FROM
			sys_user_openid
		WHERE isDel = 0
			AND openId =#{openId}
		GROUP BY openId
	</select>

	<!--验证手机号码是否已经绑定-->
	<select id="checkBindByPhone" parameterType="String" resultType="int">
		select count(1) from sys_user_openid where 1=1 and loginName=#{phone} and isDel=0
	</select>

	<!--验证当前openId是否已绑定-->
	<select id="checkBindByOpenId" parameterType="String" resultType="int">
		select count(1) from sys_user_openid where openId=#{openId} and isDel=0
	</select>

	<!--查询角色相关菜单-->
	<select id="selectRoleMenuList" parameterType="String" resultType="map">
		SELECT
			DISTINCT smc.id,
			smc.menu_name,
			smc.menu_identi,
			smc.menu_img,
			smc.menu_url,
			smc.menu_sort
		FROM
			sur_menu_config smc
			LEFT JOIN sur_menu_config_role smcr ON smcr.smcid = smc.id
			LEFT JOIN sys_role sr ON sr.role_id = smcr.role_id
			LEFT JOIN sys_user_role ur ON sr.role_id = ur.role_id
			LEFT JOIN sys_user su ON ur.user_id = su.user_id
		WHERE
			smc.del_flag = 0
			AND su.phonenumber = #{username}
			<if test="type != null and type != ''"> and smc.menu_type=#{type}</if>
			<if test="type == null or type == ''"> and (smc.menu_type = '' or smc.menu_type is null) </if>
		order by smc.menu_sort asc
	</select>

	<!--修改账号密码-->
	<update id="updataUser" parameterType="map">
		update sys_user set password =#{password} where phonenumber = #{loginName}
	</update>

</mapper>