<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pig4cloud.pigx.admin.mapper.SysUserMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="baseResultMap" type="com.pig4cloud.pigx.admin.api.entity.SysUser">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="phone" property="phone"/>
		<result column="avatar" property="avatar"/>
		<result column="salt" property="salt"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="create_time" property="createTime"/>
		<result column="update_time" property="updateTime"/>
		<result column="del_flag" property="delFlag"/>
	</resultMap>

	<!-- userVo结果集 -->
	<resultMap id="userVoResultMap" type="com.pig4cloud.pigx.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="udel_flag" property="delFlag"/>
		<result column="deptId" property="deptId"/>
		<result column="tenantId" property="tenantId"/>
		<result column="deptName" property="deptName"/>
		<collection property="roleList" ofType="com.pig4cloud.pigx.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="selectUserVo">
        SELECT
            `user`.user_id,
            `user`.username,
            `user`.`password`,
            `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 AS udel_flag,
            `user`.dept_id AS deptId,
            `user`.tenant_id AS tenantId,
            r.role_id,
            r.role_name,
            r.role_code,
            r.role_desc,
            r.create_time AS rcreate_time,
            r.update_time AS rupdate_time
        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
    </sql>

	<select id="selectUserVoByUsername" resultMap="userVoResultMap">
		<include refid="selectUserVo"/>
		WHERE `user`.username = #{username}
	</select>

	<select id="selectUserVoById" resultMap="userVoResultMap">
         SELECT
            `user`.user_id,
            `user`.username,
            `user`.`password`,
            `user`.salt,
            `user`.phone,
            `user`.avatar,
			`user`.wx_openid,
            `user`.qq_openid,
            `user`.create_time AS ucreate_time,
            `user`.update_time AS uupdate_time,
            `user`.del_flag AS udel_flag,
            `user`.tenant_id AS tenantId,
            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 deptName,
            d.dept_id AS deptId
        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>

	<select id="selectUserVoPage" resultMap="userVoResultMap">
		SELECT
		`user`.user_id,
		`user`.username,
		`user`.`password`,
		`user`.salt,
		`user`.phone,
		`user`.avatar,
		`user`.wx_openid,
		`user`.qq_openid,
		`user`.create_time AS ucreate_time,
		`user`.update_time AS uupdate_time,
		`user`.del_flag AS udel_flag,
		`user`.tenant_id AS tenantId,
		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 deptName,
		d.dept_id AS deptId
		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
		r.del_flag = 0
		<if test="username != null and username != ''">
			and `user`.username LIKE CONCAT('%',#{username},'%')
		</if>
		ORDER BY `user`.create_time DESC
	</select>
</mapper>
