<?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.lutao.cloud.admin.mapper.SysUserMapper">
	<!-- userVo结果集 -->
	<resultMap id="userVoResultMap" type="com.lutao.cloud.admin.api.vo.UserVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="ucreate_time" property="createTime"/>
		<result column="uupdate_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="udel_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="user_from" property="userFrom"/>
		<result column="email" property="email"/>
		<result column="nickname" property="nickname"/>
		<collection property="roleList"
                    select="com.lutao.cloud.admin.mapper.SysRoleMapper.listRolesByUserId" column="user_id">
		</collection>
	</resultMap>
	<resultMap id="userAllMap" type="com.lutao.cloud.admin.api.vo.UserPageVo">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="ucreate_time" property="createTime"/>
		<result column="uupdate_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="udel_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="user_from" property="userFrom"/>
		<result column="email" property="email"/>
		<result column="nickname" property="nickname"/>
		<result column="position" property="position"/>
		<result column="description" property="description"/>
	</resultMap>
	<resultMap id="userPersonMap" type="com.lutao.cloud.admin.api.vo.UserByIdVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="ucreate_time" property="createTime"/>
		<result column="uupdate_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="udel_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="user_from" property="userFrom"/>
		<result column="email" property="email"/>
		<result column="nickname" property="nickname"/>
		<result column="position" property="position"/>
		<result column="description" property="description"/>
	</resultMap>

	<sql id="userRoleSql">
		u.user_id,
		u.username,
		u.salt,
		u.phone,
		u.avatar,
		u.email,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		u.user_from,
		u.nickname,
		u.position,
	    u.description
	</sql>

	<sql id="userRolePasswordSql">
		u.password
	</sql>
	<sql id="userRoleDeptSql">
		d.name  dept_name
    </sql>
	
	<sql id="userNationalStanStandardSql">
		suns.sip_no,
		suns.sip_region,
		suns.sip_ip,
		suns.sip_port,
		suns.device_no,
		suns.register_pwd,
		suns.local_sip_port,
		suns.register_indate,
		suns.heartbeat_cycle,
		suns.beat_max_time,
		suns.area_code,
		suns.access_identification_code,
		suns.channel_no
	</sql>

	<select id="getUserVoByUsername" resultMap="userVoResultMap">
		SELECT
			<include refid="userRoleSql"/>,
			<include refid="userRolePasswordSql"/>
		FROM
		sys_user_info  u
			WHERE u.username = #{username}
	</select>

	<select id="getUserVoById" resultMap="userVoResultMap">
        SELECT
			<include refid="userRoleSql"/>,
			<include refid="userRolePasswordSql"/>,
	   		<include refid="userRoleDeptSql"/>
        FROM
            sys_user_info  u
            LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
        WHERE
           u.user_id = #{id}
    </select>
	<select id="getPersonInfo" resultMap="userPersonMap">
		SELECT
		<include refid="userRoleSql"/>,
		<include refid="userRolePasswordSql"/>,
		<include refid="userRoleDeptSql"/>
		FROM
		sys_user_info  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		WHERE
		u.user_id = #{id}
	</select>

	<select id="getUserVosPage" resultMap="userVoResultMap">
		SELECT
		<include refid="userRoleSql"/>,
		<include refid="userRoleDeptSql"/>
		FROM
		sys_user_info  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		<where>
			u.del_flag = '0' and u.user_from != 4
			<if test="query.username != null and query.username != ''">
				AND u.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND u.dept_id = #{query.deptId}
			</if>
			<if test="query.searchStr!=null and query.searchStr!=''">
				AND (u.username LIKE CONCAT ('%', #{query.searchStr},'%')
				or u.nickname LIKE CONCAT ('%', #{query.searchStr},'%'))
			</if>
		</where>
	</select>

	<select id="selectVoListByScope" resultMap="userVoResultMap">
		SELECT
		u.user_id,
		u.username,
		u.nickname,
		u.password,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		u.user_from,
		u.email,
		d.name  dept_name
		FROM
		sys_user_info  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		<where>
			u.del_flag = '0'
			<if test="query.username != null and query.username != ''">
				AND u.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND u.dept_id = #{query.deptId}
			</if>
		</where>
		ORDER BY u.create_time DESC
	</select>

	<select id="selectUserAll" resultMap="userAllMap">
		SELECT u.*
		FROM
		sys_user_info u
		<where>
			u.del_flag = '0'
		</where>
		ORDER BY u.create_time DESC
	</select>
</mapper>
