<?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.vctgo.system.mapper.SysUserMapper">

	<resultMap type="com.vctgo.system.api.domain.SysUser" id="SysUserResult">
		<id     property="tenantId"       column="tenant_id"      />
		<id     property="userId"       column="user_id"      />
		<result property="deptId"       column="dept_id"      />
		<result property="userAccount"  column="user_account"      />
		<result property="userName"     column="user_name"    />
		<result property="nickName"     column="nick_name"    />
		<result property="userType"     column="user_type"   />
		<result property="userStatus"     column="user_status"   />
		<result property="email"        column="email"        />
		<result property="phonenumber"  column="phonenumber"  />
		<result property="isExpert"  	column="is_expert"  />
		<result property="sex"          column="sex"          />
		<result property="avatar"       column="avatar"       />
		<result property="password"     column="password"     />
		<result property="accountStatus"       column="account_status"       />
		<result property="accountDate"       column="account_date"       />
		<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="deptName"       column="dept_name"       />
		<result property="postName"       column="post_name"       />
		<result property="createUserName" column="create_user_name"/>
		<result property="tenantType" column="tenant_type"/>
		<result property="professionalTitleGrade" column="professional_title_grade"/>
		<result property="mainPost" column="main_post"/>
		<association property="dept"    column="dept_id" javaType="com.vctgo.system.api.domain.SysDept" resultMap="deptResult" />
		<association property="post"    column="post_id" javaType="com.vctgo.system.api.domain.SysPost" resultMap="postResult" />
		<collection  property="roles"   javaType="java.util.List"           resultMap="RoleResult" />
	</resultMap>

	<resultMap id="deptResult" type="com.vctgo.system.api.domain.SysDept">
		<id     property="deptId"    column="dept_id"     />
		<result property="parentId"  column="parent_id"   />
		<result property="deptName"  column="dept_name"   />
		<result property="ancestors" column="ancestors"   />
		<result property="orderNum"  column="order_num"   />
		<result property="leader"    column="leader"      />
		<result property="status"    column="dept_status" />
	</resultMap>

	<resultMap id="postResult" type="com.vctgo.system.api.domain.SysPost">
		<id     property="postId"    column="post_id"     />
		<result property="postCode"  column="post_code"   />
		<result property="postName"  column="post_name"   />
		<result property="isSysPost" column="is_sys_post"   />
		<result property="status"    column="post_status" />
	</resultMap>

	<resultMap id="RoleResult" type="com.vctgo.system.api.domain.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"    />
		<result property="tenantId"       column="tenant_id"    />
	</resultMap>

	<sql id="selectUserBaseInfoVo">
		select user_id, dept_id, user_account, nick_name, user_type, user_status,
		       phonenumber, account_status, account_date, tenant_id from sys_user
	</sql>

	<sql id="selectUserVo">
		SELECT
			u.user_id,
			u.dept_id,
			u.user_account,
			u.user_name,
			u.nick_name,
			u.user_type,
			u.user_status,
			u.email,
			u.avatar,
			u.phonenumber,
			u.PASSWORD,
			u.sex,
			u.account_status,
			u.account_date,
			u.del_flag,
			u.login_ip,
			u.login_date,
			u.create_by,
			u.create_time,
			u.remark,
			d.dept_id,
			d.parent_id,
			d.ancestors,
			d.dept_name,
			d.order_num,
			d.leader,
			d.STATUS AS dept_status,
			r.role_id,
			CONCAT(CONCAT(t.tenant_name,"-"),r.role_name) role_name,
			r.role_key,
			r.role_sort,
			r.data_scope,
			r.STATUS AS role_status,
			u.tenant_id,
			u.is_expert
		FROM
			sys_user u
				LEFT JOIN sys_dept d ON u.dept_id = d.dept_id and d.status='0' and d.del_flag='0'
				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 and r.status='0' and r.del_flag='0'
				LEFT JOIN sys_tenant t on t.id = r.tenant_id and t.status='0' and t.del_flag='0'
	</sql>

	<select id="selectUserList"  resultMap="SysUserResult">
		SELECT
			u.user_id,
			u.dept_id,
			u.nick_name,
			u.user_account,
			u.user_name,
			u.email,
			u.avatar,
			u.phonenumber,
			u.is_expert,
			u.sex,
			u.account_status,
			u.account_date,
			u.del_flag,
			u.login_ip,
			u.login_date,
			u2.nick_name create_by,
			u.create_time,
			u.remark,
			u.user_type,
			u.user_status,
			u.tenant_id,
			d.dept_name,
			CASE WHEN d2.leader_id IS NOT NULL THEN '主管' ELSE '普通' END AS leader,
			p.post_name,
			t.tenant_type,
			ts.professional_title_grade
		FROM
		sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join sys_user_post up on up.user_id = u.user_id
		left join sys_dept d2 on u.dept_id = d2.dept_id and u.user_id = d2.leader_id
		left join sys_user u2 on u2.user_account = u.create_by
		left join sys_tenant t on t.id = u.tenant_id
		LEFT JOIN sys_teach_staff ts on ts.user_id = u.user_id and ts.is_deleted ='0'
		left join sys_post p on ts.main_post = p.post_id
		where u.del_flag = '0'
		<if test="query.params.isExcludeTenant == '00'">
			AND u.user_type != #{query.params.isExcludeTenant}
		</if>
		<if test="query.params.tenantIdGroupList != null  and query.params.tenantIdGroupList.size >0 ">
			and u.tenant_id in
			<foreach collection="query.params.tenantIdGroupList" item="tenantId" index="i" open="(" close=")" separator=",">
				#{tenantId}
			</foreach>
		</if>
		<if test="query.params.tenantId != null and query.params.tenantId != ''">
			AND u.tenant_id = #{query.params.tenantId}
		</if>
		<if test="query.userId != null and query.userId != 0">
			AND u.user_id = #{query.userId}
		</if>
		<if test="query.userAccount != null and query.userAccount != ''">
			AND u.user_account like concat(#{query.userAccount}, '%')
		</if>
		<if test="query.nickName != null and query.nickName != ''">
			AND u.nick_name like concat('%', #{query.nickName}, '%')
		</if>
		<if test="query.postId != null">
			AND p.post_id = #{query.postId}
		</if>
		<if test="query.isBackbone != null  and query.isBackbone != ''">
			and ts.is_backbone = #{query.isBackbone}
		</if>
		<if test="query.userStatus != null and query.userStatus != ''">
			AND u.user_status = #{query.userStatus}
		</if>
		<if test="query.accountStatus != null and query.accountStatus != ''">
			AND u.account_status = #{query.accountStatus}
		</if>
		<if test="query.isExpert != null and query.isExpert != ''">
			AND u.is_expert = #{query.isExpert}
		</if>
		<if test="query.phonenumber != null and query.phonenumber != ''">
			AND u.phonenumber like concat('%', #{query.phonenumber}, '%')
		</if>
		<if test="query.accountDate != null">
			AND date_format(u.account_date,'%y%m%d') &gt;= date_format(#{query.accountDate},'%y%m%d')
		</if>
		<if test="query.params.beginTime != null and query.params.beginTime != ''">
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{query.params.beginTime},'%y%m%d')
		</if>
		<if test="query.params.endTime != null and query.params.endTime != ''">
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{query.params.endTime},'%y%m%d')
		</if>
		<if test="query.params.beginAccountDate != null and query.params.beginAccountDate != ''">
			AND date_format(u.account_date,'%y%m%d') &gt;= date_format(#{query.params.beginAccountDate},'%y%m%d')
		</if>
		<if test="query.params.endAccountDate != null and query.params.endAccountDate != ''">
			AND date_format(u.account_date,'%y%m%d') &lt;= date_format(#{query.params.endAccountDate},'%y%m%d')
		</if>
		<!--集团租户的岗位包含全部幼儿园的岗位，每个幼儿园租户有自己的组织架构树，in不适用集团租户的组织架构树-->
		<if test="query.deptId != null and query.deptId != 0 and query.params.tenantType != '1'.toString()">
			AND (u.dept_id = #{query.deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{query.deptId}, ancestors) ))
		</if>
		<choose>
			<when test="query.params.isExistAccount == '1'.toString()">
				and u.user_account is not null
			</when>
			<when test="query.params.isExistAccount == '2'.toString()">
				and u.user_account is null
			</when>
			<otherwise >
			</otherwise>
		</choose>
		<!-- 数据范围过滤 -->
		${query.params.dataScope}
		<choose>
			<when test="query.params.isExistAccount == '1'.toString()">
				order by u.account_date desc
			</when>
			<otherwise>
				order by u.create_time desc
			</otherwise>
		</choose>
	</select>

	<select id="selectAllocatedList"  resultMap="SysUserResult">
		SELECT DISTINCT
			u.user_id,
			u.dept_id,
			u.user_account,
			u.user_name,
			u.nick_name,
			u.email,
			u.phonenumber,
			u.account_status,
			u.account_date,
			u.create_time,
			u.create_by,
			d.dept_name,
			p.post_name
		FROM
			sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_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
		LEFT JOIN sys_user_post up ON u.user_id = up.user_id
		LEFT JOIN sys_post p ON p.post_id = up.post_id
		where u.del_flag = '0' and r.role_id = #{query.roleId} and u.user_account !='admin' and u.user_account is not null
		<if test="query.nickName != null and query.nickName != ''">
			AND u.nick_name like concat('%', #{query.nickName}, '%')
		</if>
		<if test="query.tenantType ==2 or query.tenantType == 3">
			AND u.user_id in (select su.user_id from sys_user su where su.del_flag='0')
		</if>
		<if test="query.userAccount != null and query.userAccount != ''">
			AND u.user_account like concat('%', #{query.userAccount}, '%')
		</if>
		<if test="query.deptId != null and query.deptId != ''">
			AND d.dept_id = #{query.deptId}
		</if>
		<if test="query.postId != null and query.postId != ''">
			AND p.post_id = #{query.postId}
		</if>
		<!-- 数据范围过滤 -->
		${query.params.dataScope}
		order by u.create_time desc
	</select>

	<select id="selectUnallocatedList"  resultMap="SysUserResult">
		SELECT DISTINCT
			u.user_id,
			u.dept_id,
			u.user_account,
			u.user_name,
			u.nick_name,
			u.email,
			u.phonenumber,
			u.account_status,
			u.create_time,
		    u.create_by,
			d.dept_name,
			p.post_name
		FROM
			sys_user u
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_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
		LEFT JOIN sys_user_post up ON u.user_id = up.user_id
		LEFT JOIN sys_post p ON p.post_id = up.post_id
		where u.user_status='0'
		and u.del_flag = '0'
		and (r.role_key != 'admin' or r.role_key is null)
		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 = #{query.roleId})
		<if test="query.tenantIdList.size > 0">
			AND u.user_id in (
			select suu.user_id from sys_user suu where suu.del_flag='0' and suu.user_status='0' and suu.tenant_id in
			<foreach collection="query.tenantIdList" item="tenantId" index="index" open="(" close=")" separator=",">
				#{tenantId}
			</foreach>
			)
		</if>
		and u.user_account is not null
		<if test="query.nickName != null and query.nickName != ''">
			AND u.nick_name like concat('%', #{query.nickName}, '%')
		</if>
		<if test="query.phonenumber != null and query.phonenumber != ''">
			AND u.user_account like concat('%', #{query.phonenumber}, '%')
		</if>
		<if test="query.deptId != null and query.deptId != ''">
			AND d.dept_id = #{query.deptId}
		</if>
		<if test="query.postId != null and query.postId != ''">
			AND p.post_id = #{query.postId}
		</if>
		<if test="query.tenantId != null and query.tenantId != ''">
			AND u.tenant_id = #{query.tenantId}
		</if>
		<!-- 数据范围过滤 -->
		${query.params.dataScope}
		order by u.create_time desc
	</select>

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

	<select id="selectUserByTenantAccount" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.del_flag = '0' and u.tenant_id = #{tenantId} and u.user_type = '00'
	</select>

	<select id="selectUserByUserAccount" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.user_name = #{userName} and u.del_flag = '0' and u.tenant_id = #{tenantId}
	</select>

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

	<select id="selectUserByUserId" resultMap="SysUserResult">
		<include refid="selectUserBaseInfoVo"/>
		where user_id = #{userId}
		<if test="tenantId != null and tenantId != ''">
			and tenant_id = #{tenantId}
		</if>
	</select>

	<select id="checkTenantNameUnique" resultType="com.vctgo.system.api.domain.SysUser">
		<include refid="selectUserBaseInfoVo"/>
		where user_account = #{userName} and user_type = '00'
		and del_flag = '0' and tenant_id <![CDATA[ <> ]]> #{tenantId} limit 1
	</select>

	<select id="checkUserNameUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, user_name from sys_user where user_name = #{userName} and del_flag = '0' limit 1
	</select>

	<select id="checkPhoneUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, phonenumber from sys_user where phonenumber = #{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="selectPointPostUserList" resultType="com.vctgo.system.api.domain.dto.SysUserDto">
		select u.user_id,u.user_account,u.user_name,u.nick_name,u.user_type,
			   u.user_status,u.email, u.phonenumber, u.account_status, u.create_time,
			   p.post_name
		from sys_user u
		LEFT JOIN sys_teach_staff ts on ts.user_id = u.user_id and ts.is_deleted ='0'
		left join sys_post p on ts.main_post = p.post_id
		where u.tenant_id = #{tenantId}
		<if test="nickName != null and nickName != ''">
			and u.nick_name like concat('%', #{nickName}, '%')
		</if>
		<if test="phoneNumber != null and phoneNumber != ''">
			and u.phonenumber like concat('%', #{phoneNumber}, '%')
		</if>
		<if test="postCode != null and postCode != ''">
		  and p.post_code = #{postCode}
		</if>
		and u.del_flag = '0' and u.user_status = '0'
		order by u.create_time desc
	</select>

	<select id="judgeUserIsExistPointPerms" resultType="java.lang.Integer">
		select count(*) from sys_user u
		inner join sys_user_role ur on ur.user_id = u.user_id
		left join sys_role r on r.role_id = ur.role_id and r.del_flag = 0 and r.status = 0
		inner join sys_role_menu rm on rm.role_id = r.role_id
		left join sys_menu m on rm.menu_id = m.menu_id and m.status = 0
		where u.user_id = #{query.userId}
		<if test="query.permsList != null  and query.permsList.size >0 ">
			and m.perms in
			<foreach collection="query.permsList" item="perms" index="i" open="(" close=")" separator=",">
				#{perms}
			</foreach>
		</if>
	</select>

	<select id="selectUserListByParams" resultType="com.vctgo.system.api.domain.SysUser">
		<include refid="selectUserBaseInfoVo"/>
		<where>
			<choose>
				<when test="params.delFlag == 'all'.toString()">
				</when>
				<otherwise >
					del_flag = '0'
				</otherwise>
			</choose>
			<choose>
				<when test="params.tenantType == '1'.toString()">
					and tenant_id in (select id from sys_tenant where garden_id in
			    	(SELECT garden_id FROM sys_tenant where id = #{params.tenantId}))
				</when>
				<when test="params.tenantType == '2'.toString() || params.tenantType == '3'.toString()">
					and tenant_id = #{params.tenantId}
				</when>
				<otherwise >

				</otherwise>
			</choose>
			<if test="params.userStatus != null  and params.userStatus != ''"> and user_status = #{params.userStatus}</if>
		</where>
	</select>

	<select id="selectUserAndRoleList" resultType="com.vctgo.system.api.domain.SysUser">
		SELECT
			u.user_id
		FROM
			sys_user u
				INNER JOIN sys_user_role ur ON u.user_id = ur.user_id
				INNER JOIN sys_role r ON r.role_id = ur.role_id
		WHERE
			r.role_key = #{roleKey}
	</select>

	<insert id="insertUser"  useGeneratedKeys="true" keyProperty="userId">
		insert into sys_user(
		<if test="userId != null and userId != 0">user_id,</if>
		<if test="deptId != null and deptId != 0">dept_id,</if>
		<if test="userAccount != null and userAccount != ''">user_account,</if>
		<if test="userName != null and userName != ''">user_name,</if>
		<if test="nickName != null and nickName != ''">nick_name,</if>
		<if test="userStatus != null and userStatus != ''">user_status,</if>
		<if test="email != null and email != ''">email,</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="accountStatus != null and accountStatus != ''">account_status,</if>
		<if test="isExpert != null and isExpert != ''">is_expert,</if>
		<if test="remark != null and remark != ''">remark,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		<if test="createById != null and createById != ''">create_by_id,</if>
		<if test="tenantId != null and tenantId != ''">tenant_id,</if>
		create_time
		)values(
		<if test="userId != null and userId != ''">#{userId},</if>
		<if test="deptId != null and deptId != ''">#{deptId},</if>
		<if test="userAccount != null and userAccount != ''">#{userAccount},</if>
		<if test="userName != null and userName != ''">#{userName},</if>
		<if test="nickName != null and nickName != ''">#{nickName},</if>
		<if test="userStatus != null and userStatus != ''">#{userStatus},</if>
		<if test="email != null and email != ''">#{email},</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="accountStatus != null and accountStatus != ''">#{accountStatus},</if>
		<if test="isExpert != null and isExpert != ''">#{isExpert},</if>
		<if test="remark != null and remark != ''">#{remark},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		<if test="createById != null and createById != ''">#{createById},</if>
		<if test="tenantId != null and tenantId != ''">#{tenantId},</if>
		sysdate()
		)
	</insert>

	<update id="updateUser" >
		update sys_user
		<set>
			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
			<if test="userAccount != null and userAccount != ''">user_account = #{userAccount},</if>
			<if test="userName != null and userName != ''">user_name = #{userName},</if>
			<if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
			<if test="userStatus != null and userStatus != '' ">user_status = #{userStatus},</if>
			<if test="email != null ">email = #{email},</if>
			<if test="phonenumber != null ">phonenumber = #{phonenumber},</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="accountStatus != null and accountStatus != ''">account_status = #{accountStatus},</if>
			<if test="accountDate != null">account_date = #{accountDate},</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="updateById != null and updateById != ''">update_by_id = #{updateById},</if>
			<if test="remark != null">remark = #{remark},</if>
			<if test="tenantId != null">tenant_id = #{tenantId},</if>
			<if test="userType != null and userType != ''">user_type = #{userType},</if>
			<if test="isExpert != null">is_expert = #{isExpert},</if>
			update_time = sysdate()
		</set>
		where user_id = #{userId}
	</update>

	<update id="updateTenantAccount" >
		update sys_user set user_account = #{userAccount}, user_name = #{userAccount},
		                    phonenumber = #{phonenumber}
		where tenant_id = #{tenantId} and del_flag = '0' and user_type='00'
	</update>

	<update id="updateUserAccount" >
		update sys_user set user_account = #{userAccount}, user_name = #{userAccount},
		                    phonenumber = #{phonenumber}
		where tenant_id = #{tenantId} and user_name = #{userAccount} and del_flag = '0'
	</update>

	<update id="updateGardenTenantName">
		update sys_user set nick_name = #{nickName}
		where del_flag = '0' and tenant_id = (select id from sys_tenant where del_flag = '0' and garden_id = #{deptId} and tenant_type = 1) and user_type = '00'
	</update>

	<update id="updateBranchTenantName">
		update sys_user set nick_name = #{nickName}
		where del_flag = '0' and tenant_id = (select id from sys_tenant where del_flag = '0' and branch_id = #{deptId} and tenant_type in (2, 3)) and user_type = '00'
	</update>

	<update id="updateUserAvatar" >
		update sys_user set avatar = #{avatar} where user_name = #{userName}
	</update>

	<update id="resetTenantPwd" >
		update sys_user set password = #{password} where user_account = #{userName} and del_flag = '0'
	</update>

	<update id="resetUserPwd" >
		update sys_user set password = #{password} where user_account = #{userName} and del_flag = '0'
	</update>

	<delete id="deleteUserById" parameterType="Long">
		update sys_user set del_flag = '1' where user_id = #{userId}
	</delete>

	<delete id="deleteUserByIds" parameterType="Long">
		update sys_user set del_flag = '1' where user_id in
		<foreach collection="array" item="userId" open="(" separator="," close=")">
			#{userId}
		</foreach>
	</delete>

	<update id="clearUserAccountInfo" parameterType="Long">
		update sys_user set user_account = null, user_name = null, password = null,
		                    account_status = null, account_date = null
		where user_id = #{userId}
	</update>

    <update id="updateReTableNickName">
		update sys_re_class_user cu
		inner join sys_user u on u.user_id = cu.bind_user_id
		set cu.bind_user_name = #{nickName}
		where u.user_id = #{userId}
	</update>

    <delete id="deleteUserByTenantId">
		delete from sys_user where tenant_id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>
