<?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="cn.ezhizao.project.system.mapper.SysUserMapper">

	<resultMap type="SysUser" id="SysUserResult">
		<id     property="userId"              column="user_id"      />
		<result property="tenantId"            column="tenant_id"      />
		<result property="tenantName"          column="tenant_name"      />
		<result property="userName"            column="user_name"    />
		<result property="userType"            column="user_type"    />
		<result property="nickName"            column="nick_name"    />
		<result property="contractCompanyName" column="contract_company_name" />
		<result property="contractCompanyId"   column="contract_company_id" />
		<result property="email"               column="email"        />
		<result property="phoneNumber"         column="phone_number"  />
		<result property="sex"                 column="sex"          />
		<result property="avatar"       	   column="avatar"       />
		<result property="password"     	   column="password"     />
		<result property="status"       	   column="status"       />
		<result property="delFlag"      	   column="del_flag"     />
		<result property="loginIp"      	   column="login_ip"     />
		<result property="loginDate"    	   column="login_date"   />
		<result property="createBy"     	   column="create_by"    />
		<result property="createTime"  		   column="create_time"  />
		<result property="updateBy"            column="update_by"    />
		<result property="updateTime"          column="update_time"  />
		<result property="remark"              column="remark"       />
		<result property="usageCompanyId"      column="usage_company_id"       />
		<result property="usageCompanyName"    column="usage_company_name"       />
		<result property="isLeader"            column="is_leader"/>
		<result property="isPartTime"          column="is_part_time"/>
		<result property="isDispatcher"        column="is_dispatcher"/>
		<result property="isFormman"           column="is_formman"/>
		<result property="isYardman"           column="is_yardman"/>
		<result property="isEdit"              column="is_edit"/>
		<collection  property="roles"   javaType="java.util.List"  resultMap="RoleResult" />
	</resultMap>

	<resultMap id="RoleResult" type="SysRole">
		<id     property="roleId"       column="role_id"        />
		<result property="roleName"     column="role_name"      />
		<result property="roleKey"      column="role_key"       />
		<result property="roleSort"     column="role_sort"      />
		<result property="dataScope"    column="data_scope"    />
		<result property="status"       column="role_status"    />
	</resultMap>

	<sql id="selectUserTenantVo">
		SELECT u.user_id, u.tenant_id, IFNULL(t.org_name, '超级管理员') AS tenant_name, u.user_type,u.user_name, u.nick_name, u.email,u.contract_company_name, u.contract_company_id, u.avatar, u.phone_number, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, u.usage_company_id,
		u.usage_company_name,
			   r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
		FROM sys_user u
		         LEFT JOIN biz_tenant t ON t.id = u.tenant_id
				 LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
				 LEFT JOIN sys_role r ON r.role_id = ur.role_id
	</sql>
	<select id="getListByDeptId" parameterType="java.util.List" resultMap="SysUserResult">
        SELECT u.*,
            -- 用于前端控制是否进入编辑状态
            0 AS is_edit,
            deptUser.is_leader,
            deptUser.is_part_time,
            deptUser.is_dispatcher,
			deptUser.is_formman,
			deptUser.is_yardman,
		    deptUser.id
        FROM sys_user u
		JOIN (SELECT * FROM sys_dept_user WHERE deleted = 0 AND dept_id in
		<foreach collection="ids" item="deptId" open="(" separator="," close=")">
		#{deptId}
	</foreach>) AS deptUser
			ON u.user_id = deptUser.user_id
        WHERE
			u.del_flag = '0' AND
			u.user_type = '02'
    </select>

	<select id="selectUserItemByDeptId"  resultMap="SysUserResult">
		SELECT u.* FROM sys_user u left join sys_dept_user d on u.user_id = d.user_id
		WHERE u.del_flag = '0' AND d.deleted ='0' AND u.user_type = '02'
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>

	</select>
	<select id="getUserByCode" parameterType="SysUser" resultMap="SysUserResult">
		SELECT * FROM sys_user WHERE del_flag = 0 AND user_name = #{userName}
	</select>
	<select id="selectNotAddList" resultMap="SysUserResult">
		SELECT distinct u.user_id,
		u.nick_name,
		u.user_name,
		u.email,
		u.avatar,
		u.phone_number,
		u.sex,
		u.status,
		u.del_flag,
		u.login_ip,
		u.login_date,
		u.create_by,
		u.create_time,
		u.usage_company_id,
		u.usage_company_name,
		u.contract_company_id,
		u.contract_company_name,
		u.remark FROM sys_user u left join sys_dept_user d on u.user_id = d.user_id
		WHERE u.del_flag = '0' and d.deleted = 0 and u.user_id not in
		(select user_id from biz_package_resource_group_detail where deleted = 0)
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="tenantId != null and tenantId != 0">
			AND u.tenant_id = #{tenantId}
		</if>
		<if test="deptId != null and deptId != 0">
			AND d.dept_id = #{deptId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="userType != null and userType != ''">
			AND u.user_type = #{userType}
		</if>
		<if test="phoneNumber != null and phoneNumber != ''">
			AND u.phone_number LIKE concat('%', #{phoneNumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<!-- 用户列表 selectUserList -->
	<select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
		SELECT distinct u.user_id,
		       u.nick_name,
		       u.user_name,
		       u.email,
		       u.avatar,
		       u.phone_number,
		       u.sex,
		       u.status,
		       u.del_flag,
		       u.login_ip,
		       u.login_date,
		       u.create_by,
		       u.create_time,
		       u.usage_company_id,
		       u.usage_company_name,
		       u.contract_company_id,
		       u.contract_company_name,
		       u.remark FROM sys_user u left join sys_dept_user d on u.user_id = d.user_id
		WHERE u.del_flag = '0' and d.deleted = 0
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="tenantId != null and tenantId != 0">
			AND u.tenant_id = #{tenantId}
		</if>
		<if test="deptId != null and deptId != 0">
			AND d.dept_id = #{deptId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="userType != null and userType != ''">
			AND u.user_type = #{userType}
		</if>
		<if test="phoneNumber != null and phoneNumber != ''">
			AND u.phone_number LIKE concat('%', #{phoneNumber}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>
	<select id="selectTenantList" parameterType="SysUser" resultMap="SysUserResult">
		SELECT distinct u.user_id,
		u.nick_name,
		u.user_name,
		u.email,
		u.avatar,
		u.phone_number,
		u.sex,
		u.status,
		u.del_flag,
		u.login_ip,
		u.login_date,
		u.create_by,
		u.create_time,
		u.usage_company_id,
		u.usage_company_name,
		u.contract_company_id,
		u.contract_company_name,
		u.remark FROM sys_user u
		WHERE u.del_flag = '0'
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="tenantId != null and tenantId != 0">
			AND u.tenant_id = #{tenantId}
		</if>
		<if test="deptId != null and deptId != 0">
			AND d.dept_id = #{deptId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="userType != null and userType != ''">
			AND u.user_type = #{userType}
		</if>
		<if test="phoneNumber != null and phoneNumber != ''">
			AND u.phone_number LIKE concat('%', #{phoneNumber}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>
	<select id="getUserList" resultType="cn.ezhizao.project.system.domain.SysUser">
		select distinct * from sys_user
		<where>
			<if test="userName != null and userName != ''">
				AND user_name like concat('%', #{userName}, '%')
			</if>
			<if test="nickName != null and nickName != ''">
				AND nick_name like concat('%', #{nickName}, '%')
			</if>
			and del_flag = 0 and user_id != 1 and user_id !=2
		</where>
	</select>
	<select id="selectUserPermissionList" resultType="cn.ezhizao.project.system.domain.SysMenu">
		select t1.perms from sys_menu t1 left join sys_role_menu t2 on t1.menu_id = t2.menu_id left join sys_user_role t3 on t2.role_id = t3.role_id
		left join sys_user t4 on t3.user_id = t4.user_id where t4.user_id = #{userId}
	</select>
	<select id="getListByUserName" resultType="cn.ezhizao.project.system.domain.SysUser">
		select distinct * from sys_user
		<where>
				AND user_name =  #{userName}
				AND nick_name = #{nickName}

			and del_flag = 0
		</where>
	</select>
	<!-- 员工列表 selectEmployeeList -->
	<select id="selectEmployeeList" parameterType="SysUser" resultMap="SysUserResult">
		SELECT
		   u.tenant_id,
		   u.user_type,
		   u.user_id,
		   u.nick_name,
		   u.user_name,
		   u.contract_company_name,
		   u.contract_company_id,
		   u.email,
		   u.avatar,
		   u.phone_number,
		   u.sex,
		   u.status,
		   u.del_flag,
		   u.login_ip,
		   u.login_date,
		   u.create_by,
		   u.create_time,
		   u.remark,
		u.usage_company_id,
		u.usage_company_name
		FROM sys_user u
		WHERE u.del_flag = '0' AND u.user_type='02'
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="tenantId != null and tenantId != 0">
			AND tenant_id = #{tenantId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="phoneNumber != null and phoneNumber != ''">
			AND u.phone_number LIKE concat('%', #{phoneNumber}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectAllocatedList" parameterType="SysUser" resultMap="SysUserResult">
		SELECT distinct u.user_id,u.user_name, u.nick_name, u.email, u.phone_number, u.status, u.create_time
		FROM sys_user u
		LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
		WHERE u.del_flag = '0' AND r.role_id = #{roleId}
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="tenantId != null  and tenantId != 0"> AND u.tenant_id = #{tenantId}</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="SysUser" resultMap="SysUserResult">
		SELECT distinct u.user_id, u.user_name, u.nick_name, u.email, u.phone_number, u.status, u.create_time
		FROM sys_user u
		LEFT JOIN biz_user_tenant ut ON ut.user_id = u.user_id
		LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
		WHERE u.del_flag = '0' AND (r.role_id != #{roleId} or r.role_id IS NULL) and u.user_id != 1 and u.user_id !=2 and u.user_type = '02'
		AND u.user_id NOT IN (SELECT u.user_id FROM sys_user u INNER JOIN sys_user_role ur ON u.user_id = ur.user_id AND ur.role_id = #{roleId})
		<if test="userName != null and userName != ''">
			AND u.user_name LIKE concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name LIKE concat('%', #{nickName}, '%')
		</if>
		<if test="tenantId != null  and tenantId != 0"> AND u.tenant_id = #{tenantId}</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserTenantVo"/>
		WHERE u.user_name = #{userName} AND u.del_flag = '0'
	</select>

	<select id="selectUserByUserNameAndTenantId" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserTenantVo"/>
		WHERE u.user_name = #{userName} AND u.tenant_id = #{tenantId} AND u.del_flag = '0'
	</select>

	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserTenantVo"/>
		WHERE u.user_id = #{userId}
	</select>

	<select id="checkUserNameUnique"  resultMap="SysUserResult">
		SELECT user_id, user_name FROM sys_user WHERE user_name = #{userName} AND del_flag = '0'
		LIMIT 1
	</select>

	<select id="selectUserListByIds" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserTenantVo"/>
		WHERE u.user_id in
		<foreach collection="list" item="userId" open="(" separator="," close=")">
			#{userId}
		</foreach>
	</select>

	<select id="checkPhoneUnique" parameterType="String" resultMap="SysUserResult">
		SELECT user_id, phone_number FROM sys_user WHERE phone_number = #{phoneNumber} AND del_flag = '0' limit 1
	</select>

	<select id="checkEmailUnique" parameterType="String" resultMap="SysUserResult">
		SELECT user_id, email FROM sys_user WHERE email = #{email} AND del_flag = '0' limit 1
	</select>
	<select id="listByLikeNickNameOrUserName" resultMap="SysUserResult">
        SELECT * FROM sys_user
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
			del_flag = '0' and user_type='02' and user_id !=1 and user_id != 2
			<if test="userName != null  and userName != ''"> AND user_name like concat('%', #{userName}, '%')</if>
			<if test="nickName != null  and nickName != ''"> AND nick_name like concat('%', #{nickName}, '%')</if>
        </trim>
    </select>

	<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId" >
		INSERT INTO sys_user(
		<if test="userId != null and userId != 0">user_id,</if>
		<if test="userName != null and userName != ''">user_name,</if>
		<if test="nickName != null and nickName != ''">nick_name,</if>
		<if test="email != null and email != ''">email,</if>
		<if test="tenantId != null and tenantId != ''">tenant_id,</if>
		<if test="avatar != null and avatar != ''">avatar,</if>
		<if test="phoneNumber != null and phoneNumber != ''">phone_number,</if>
		<if test="sex != null and sex != ''">sex,</if>
		<if test="password != null and password != ''">password,</if>
		<if test="status != null and status != ''">status,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		<if test="remark != null and remark != ''">remark,</if>
		<if test="userType != null and userType != ''">user_type,</if>
		<if test="contractCompanyName != null and contractCompanyName != ''">contract_company_name,</if>
		<if test="contractCompanyId != null and contractCompanyId != ''">contract_company_id,</if>
		<if test="usageCompanyId != null and usageCompanyId != 0">usage_company_id,</if>
		<if test="usageCompanyName != null and usageCompanyName != ''">usage_company_name,</if>
		create_time
		)VALUES(
		<if test="userId != null and userId != ''">#{userId},</if>
		<if test="userName != null and userName != ''">#{userName},</if>
		<if test="nickName != null and nickName != ''">#{nickName},</if>
		<if test="email != null and email != ''">#{email},</if>
		<if test="tenantId != null and tenantId != ''">#{tenantId},</if>
		<if test="avatar != null and avatar != ''">#{avatar},</if>
		<if test="phoneNumber != null and phoneNumber != ''">#{phoneNumber},</if>
		<if test="sex != null and sex != ''">#{sex},</if>
		<if test="password != null and password != ''">#{password},</if>
		<if test="status != null and status != ''">#{status},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		<if test="remark != null and remark != ''">#{remark},</if>
		<if test="userType != null and userType != ''">#{userType},</if>
		<if test="contractCompanyName != null and contractCompanyName != ''">#{contractCompanyName},</if>
		<if test="contractCompanyId != null and contractCompanyId != ''">#{contractCompanyId},</if>
		<if test="usageCompanyId != null and usageCompanyId != 0">#{usageCompanyId},</if>
		<if test="usageCompanyName != null and usageCompanyName != ''">#{usageCompanyName},</if>
		sysdate()
		)
	</insert>

	<update id="updateUser" parameterType="SysUser">
		UPDATE sys_user
		<set>
			<if test="userName != null and userName != ''">user_name = #{userName},</if>
			<if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
			<if test="email != null ">email = #{email},</if>
			<if test="phoneNumber != null ">phone_number = #{phoneNumber},</if>
			<if test="tenantId != null ">tenant_id=#{tenantId},</if>
			<if test="sex != null and sex != ''">sex = #{sex},</if>
			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
			<if test="password != null and password != ''">password = #{password},</if>
			<if test="status != null and status != ''">status = #{status},</if>
			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
			<if test="loginDate != null">login_date = #{loginDate},</if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="remark != null">remark = #{remark},</if>
			<if test="contractCompanyName != null and contractCompanyName != ''">contract_company_name = #{contractCompanyName},</if>
			<if test="contractCompanyId != null and contractCompanyId != ''">contract_company_id = #{contractCompanyId},</if>
			<if test="usageCompanyId != null and usageCompanyId != 0">usage_company_id = #{usageCompanyId},</if>
			<if test="usageCompanyName != null and usageCompanyName != ''">usage_company_name = #{usageCompanyName},</if>
			update_time = sysdate()
		</set>
		WHERE user_id = #{userId}
	</update>

	<update id="updateUserStatus" parameterType="SysUser">
		UPDATE sys_user SET status = #{status} WHERE user_id = #{userId}
	</update>

	<update id="updateUserAvatar" parameterType="SysUser">
		UPDATE sys_user SET avatar = #{avatar} WHERE user_name = #{userName}
	</update>

	<update id="resetUserPwd" parameterType="SysUser">
		UPDATE sys_user SET password = #{password} WHERE user_name = #{userName}
	</update>

	<delete id="deleteUserById" parameterType="Long">
		UPDATE sys_user SET del_flag = '2' WHERE user_id = #{userId}
	</delete>

	<delete id="deleteUserByIds" parameterType="Long">
		UPDATE sys_user SET del_flag = '2' WHERE user_id in
		<foreach collection="list" item="userId" open="(" separator="," close=")">
			#{userId}
		</foreach>
	</delete>

</mapper>
