<?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.nantian.sc.cloud.admin.mapper.SysUserMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="baseResultMap" type="com.nantian.sc.cloud.admin.api.vo.UserVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="passwords" property="passwords"/>
		<result column="enterprise_code" property="enterpriseCode"/>
		<result column="user_dept" property="userDept"/>
		<result column="user_real_name" property="userRealName"/>
		<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"/>
		<collection property="roleList" ofType="com.nantian.sc.cloud.admin.api.entity.SysRole"
					select="com.nantian.sc.cloud.admin.mapper.SysRoleMapper.listRolesByUserId" column="user_id">
		</collection>
	</resultMap>

	<!-- userVo结果集 -->
	<resultMap id="userVoResultMap" type="com.nantian.sc.cloud.admin.api.vo.UserVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="passwords" property="passwords"/>
		<result column="enterprise_code" property="enterpriseCode"/>
		<result column="user_dept" property="userDept"/>
		<result column="user_real_name" property="userRealName"/>
		<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"/>
		<collection property="roleList" ofType="com.nantian.sc.cloud.admin.api.entity.SysRole">
			<id column="role_id" property="roleId"/>
			<result column="role_name" property="roleName"/>
			<result column="role_code" property="roleCode"/>
			<result column="role_desc" property="roleDesc"/>
			<result column="rcreate_time" property="createTime"/>
			<result column="rupdate_time" property="updateTime"/>
		</collection>
	</resultMap>

	<sql id="userRoleSql">
		`user`.user_id,
		`user`.username,
		`user`.`password`,
		`user`.`passwords`,
		`user`.`enterprise_code` AS enterpriseCode,
		`user`.`user_dept` AS userDept,
		`user`.`user_real_name` AS userRealName,
		`user`.salt,
		`user`.phone,
		`user`.avatar,
		`user`.wx_openid,
		`user`.qq_openid,
		`user`.dept_id,
		`user`.del_flag,
		`user`.lock_flag,
		`user`.tenant_id,
		`user`.create_time AS ucreate_time,
		`user`.update_time AS uupdate_time,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time AS rcreate_time,
		r.update_time AS rupdate_time
	</sql>

	<sql id="userRoleDeptSql">
		`user`.user_id,
		`user`.username,
		`user`.`password`,
		`user`.`passwords`,
		`user`.`enterprise_code` AS enterpriseCode,
		`user`.`user_dept` AS userDept,
		`user`.`user_real_name` AS userRealName,
		`user`.salt,
		`user`.phone,
		`user`.avatar,
		`user`.wx_openid,
		`user`.qq_openid,
		`user`.del_flag,
		`user`.lock_flag,
		`user`.tenant_id,
		`user`.create_time AS ucreate_time,
		`user`.update_time AS uupdate_time,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time AS rcreate_time,
		r.update_time AS rupdate_time,
		d.name AS dept_name,
		d.dept_id
    </sql>
    <insert id="renewLoginLog">
		insert into user_login_log
		select log.id, log.create_by name, log.create_time datetime, log.login_type, log.remote_addr
		from sys_log log,(
			select log_id from user_login_log order by log_id desc limit 0,1
		) t where log.id > t.log_id and title = #{title}
	</insert>

    <select id="getUserVoByUsername" resultMap="userVoResultMap">
		SELECT
			<include refid="userRoleSql"/>
		FROM
		sys_user AS `user`
		LEFT JOIN sys_user_role AS ur ON ur.user_id = `user`.user_id
		LEFT JOIN sys_role AS r ON r.role_id = ur.role_id
			WHERE `user`.username = #{username}
	</select>

	<select id="getUserVoById" resultMap="userVoResultMap">
        SELECT
	   		<include refid="userRoleDeptSql"/>
        FROM
            sys_user AS `user`
            LEFT JOIN sys_user_role AS ur ON ur.user_id = `user`.user_id
            LEFT JOIN sys_role AS r ON r.role_id = ur.role_id
            LEFT JOIN sys_dept AS d ON d.dept_id = `user`.dept_id
        WHERE
           `user`.user_id = #{id}
    </select>

	<!--type 1查询政府用户，2查询企业用户-->
	<select id="getUserVosPage" resultMap="baseResultMap">
		SELECT `user`.user_id, `user`.username, `user`.`password`, `user`.`passwords`, `user`.`enterprise_code` , `user`.`user_dept`,
			`user`.`user_real_name` , `user`.salt, `user`.phone, `user`.avatar, `user`.wx_openid, `user`.qq_openid, `user`.dept_id,
			`user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag, `user`.lock_flag, `user`.tenant_id,  d.name AS dept_name
		FROM sys_user AS `user`
		LEFT JOIN sys_dept AS d ON d.dept_id = `user`.dept_id
		<where>
			`user`.del_flag = '0'
			<if test="query.username != null and query.username != ''">
				AND `user`.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND `user`.dept_id = #{query.deptId}
			</if>
			<if test="query.phone != null and query.phone != ''">
				AND `user`.phone LIKE CONCAT('%',#{query.phone},'%')
			</if>
			<if test="query.userRealName != null and query.userRealName != ''">
				AND `user`.user_real_name LIKE CONCAT('%',#{query.userRealName},'%')
			</if>
			<if test="query.enterpriseCode != null and query.enterpriseCode != ''">
				AND `user`.enterprise_code LIKE CONCAT('%',#{query.enterpriseCode},'%')
			</if>
			<if test="query.type != null and query.type != ''">
				<choose>
					<when test="query.type == 1">
						AND (`user`.enterprise_code is null OR `user`.enterprise_code = '')
						<if test="list!=null and list.size()>0">
							AND (`user`.user_dept = '' <foreach collection="list" item="item" index="index"> or `user`.user_dept like concat('%',#{item},'%')</foreach>)
						</if>
					</when>
					<otherwise>
						AND `user`.enterprise_code is not null AND `user`.enterprise_code != ''
					</otherwise>
				</choose>
			</if>
		</where>
		ORDER BY `user`.create_time DESC
	</select>
	<select id="getAccount" resultType="java.util.Map">
		select count(1) account, count(case when user_dept != '' and user_dept is not null then 1 end) gov, count(case when user_dept = '' or user_dept is null then 1 end) com
		from sys_user where del_flag = 0 and user_real_name not like '%测试%'
	</select>
	<select id="getLoginLog" resultType="java.util.Map">
		select count(1) login, count(case when user_dept != '' and user_dept is not null then 1 end) gov, count(case when user_dept = '' or user_dept is null then 1 end) com
		from user_login_log g left join sys_user s on g.name = s.username
	</select>
	<select id="getActiveAccount" resultType="java.util.Map">
		select count(1) activeAccount, count(case when user_dept != '' and user_dept is not null then 1 end) gov, count(case when user_dept = '' or user_dept is null then 1 end) com
		from sys_user where username in (
		select name from (select name, count(1) num from user_login_log
		<if test="start!=null and start!='' and end!=null and end!=''">
				where left(datetime,10) between #{start} and #{end}
		</if>
		group by name) t where t.num >=3)
	</select>
</mapper>
