<?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.hotent.uc.dao.DIngUserDao">



	<select id="queryUserComplexDingToSystem"  resultType="java.util.Map">

		select
		ueu.EXTERNAL_USER_ID_ "dingUserId",
		ueu.EXTERNAL_USER_NAME_ "realName",
		(select ACCOUNT_ from uc_user where ID_=ueu.USER_ID_) "userNumber",
		ueu.EXTERNAL_MOBILE_ "mobile",
		DATE_FORMAT(ueu.CREATE_TIME_,'%Y-%m-%d %H:%i:%s') "createTime",
		ueu.ORG_ID_ "orgId"
		from
		uc_external_user ueu
		where
		ueu.EXTERNAL_USER_ID_ in (
		select
		ueud.EXTERNAL_USER_ID_
		from
		uc_external_user_dept ueud
		where
		ueud.EXTERNAL_DEPT_ID_ = #{extDeptId}
		and ueud.ORG_ID_ = #{orgId})
		<if test="type!=null and type!=''">
			<if test="type=='0' or type==0">
				and (ueu.EXTERNAL_USER_ID_ is not null and ueu.EXTERNAL_USER_ID_ !='')
				and (ueu.USER_ID_ is not null and ueu.USER_ID_ != '' )
			</if>
			<if test="type=='1' or type==1">
				and (ueu.USER_ID_ is  null or ueu.USER_ID_ ='')
			</if>
		</if>
		<if test="userName!=null and userName!=''">
			and  ueu.EXTERNAL_USER_NAME_ like  concat( "%",#{userName}, "%" )
		</if>
		<if test="mobile!=null and mobile!=''">
			and ueu.EXTERNAL_MOBILE_  like concat( "%",#{mobile}, "%" )
		</if>
	</select>

	<select id="queryUserComplexSystemToDing"  resultType="java.util.Map">
		select
		org.ID_ "orgId",
		u.FULLNAME_ "realName",
		u.MOBILE_ "mobile",
		DATE_FORMAT(u.CREATE_TIME_,'%Y-%m-%d %H:%i:%s') "createTime",
		ifnull(extu.EXTERNAL_USER_ID_,"") "dingUserId",
		ifnull(u.ID_,"") "userId",
		u.ACCOUNT_ "userNumber"
		from
		uc_user u
		left join (
		select
		distinct ORG_ID_,
		USER_ID_
		from
		UC_ORG_USER
		where
		IS_DELE_ != '1') orguser on
		u.ID_ = orguser.USER_ID_
		left join UC_ORG org on
		org.ID_ = orguser.ORG_ID_
		and org.ID_ in (select
		distinct(case
		when ueud.EXTERNAL_DEPT_ID_ = '1' then ueud.ORG_ID_
		else (select DEPT_ID_ from uc_external_dept ued where ued.EXTERNAL_DEPT_ID_=ueud.EXTERNAL_DEPT_ID_)
		end) "deptId"
		from
		uc_external_user_dept ueud
		where
		1=1
		<if test="extDeptId!=null and extDeptId!=''">
		and ueud.EXTERNAL_DEPT_ID_ =#{extDeptId}
		</if>
		and ueud.ORG_ID_ = #{orgId})
		left join uc_external_user extu on
		extu.USER_ID_ = u.ID_
		and extu.DING_DELETE_ = '0'
		where
		org.IS_DELE_ != '1'
		and u.IS_DELE_ != '1'
		and u.STATUS_ = 1

		<if test="type!=null and type!=''">
			<if test="type=='0' or type==0">
				and ((extu.EXTERNAL_USER_ID_ is not null) and (extu.EXTERNAL_USER_ID_ !=''))
			</if>
			<if test="type=='1' or type==1">
				and ((extu.EXTERNAL_USER_ID_ is  null) or (extu.EXTERNAL_USER_ID_ =''))
			</if>
		</if>

		<if test="userName!=null and userName!=''">
			and u.FULLNAME_ like concat( "%",#{userName}, "%" )
		</if>
		<if test="mobile!=null and mobile!=''">
			and u.MOBILE_ like concat( "%",#{mobile}, "%" )
		</if>
	</select>

	<select id="queryUserDingNo"  resultType="java.util.Map">

		select
		org.ID_ "orgId",
		org.NAME_ "orgName" ,
		u.FULLNAME_ "userName",
		u.MOBILE_ "mobile",
		u.CREATE_TIME_ "createTime",
		extu.EXTERNAL_USER_ID_ "dingUserId"
		from
		uc_user u
		left join (
		select
		distinct ORG_ID_,
		USER_ID_
		from
		UC_ORG_USER
		where
		IS_DELE_ != '1') orguser on
		u.ID_ = orguser.USER_ID_
		left join UC_ORG org on
		org.ID_ = orguser.ORG_ID_
		and org.ID_ =#{orgId}
		left  join uc_external_user extu on
		extu.USER_ID_ = u.ID_ and extu.DING_DELETE_='0' and extu.ORG_ID_=#{orgId}
		where
		org.IS_DELE_ != '1'
		and u.IS_DELE_ != '1'
		and u.STATUS_ = 1
		and extu.ORG_ID_ is NULL
		<if test="userName!=null and userName!=''">
			and u.FULLNAME_ like  concat( "%",#{userName}, "%" )
		</if>
		<if test="mobile!=null and mobile!=''">
			and u.MOBILE_ like concat( "%",#{mobile}, "%" )
		</if>

	</select>

	<select id="queryUserSysteNo"  resultType="java.util.Map">
		select
		extu.EXTERNAL_USER_ID_,
		extu.EXTERNAL_USER_NAME_,
		extu.USER_ID_ ,
		extu.CREATE_TIME_,
		extu.EXTERNAL_MOBILE_
		from
		uc_external_user extu
		where
		extu.DING_DELETE_ = '0'
		and extu.USER_ID_ is null
		<if test="userName!=null and userName!=''">
			and extu.EXTERNAL_USER_NAME_ like  concat( "%",#{userName}, "%" )
		</if>
		<if test="mobile!=null and mobile!=''">
			and extu.EXTERNAL_MOBILE_ like concat( "%",#{mobile}, "%" )
		</if>
		and extu.ORG_ID_=#{orgId}

	</select>

	<select id="queryExtUserInfo"  resultType="java.util.Map">
select
	ueu.EXTERNAL_USER_NAME_ "realName",
	ueu.EXTERNAL_USER_ID_ "dingUserId",
	ueu.EXTERNAL_MOBILE_ "mobile",
	DATE_FORMAT(ueu.CREATE_TIME_,'%Y-%m-%d %H:%i:%s') "createTime",
	ifnull((select PATH_NAME_ from uc_external_dept ued where ued.EXTERNAL_DEPT_ID_ = ueud.EXTERNAL_DEPT_ID_),(select org.PATH_NAME_ from uc_org org where orguser.ORG_ID_=org.ID_)) "dingDeptName",
	(select ACCOUNT_ from uc_user uuser where uuser.ID_=ueu.USER_ID_) "userNumber",
	(select org.PATH_NAME_ from uc_org org where orguser.ORG_ID_=org.ID_) "systemDeptName"
from
	uc_external_user ueu
inner join uc_external_user_dept ueud on ueud.EXTERNAL_USER_ID_=ueu.EXTERNAL_USER_ID_
inner join uc_org_user orguser on orguser.USER_ID_=ueu.USER_ID_
where
	ueu.EXTERNAL_USER_ID_ = #{extUserId}
	and ueu.ORG_ID_ =#{orgId}
	</select>

	<select id="queryExtUserInfoSystem"  resultType="java.util.Map">
select
	uu.FULLNAME_ "realName",
	uu.MOBILE_ "mobile",
	DATE_FORMAT(ueu.CREATE_TIME_,'%Y-%m-%d %H:%i:%s') "createTime",
	ifnull(ueu.EXTERNAL_USER_ID_, "") "dingUserId",
	group_concat(DISTINCT ifnull(ifnull((select PATH_NAME_ from uc_external_dept ued where ued.EXTERNAL_DEPT_ID_ = ueud.EXTERNAL_DEPT_ID_), (select org.PATH_NAME_ from uc_org org where ueu.ORG_ID_ = org.ID_)),(
	select
		org.PATH_NAME_
	from
		uc_org org
	where
		orguser.ORG_ID_ = org.ID_)))  "dingDeptName",
	uu.ACCOUNT_ "userNumber",
		group_concat((
	select
		org.PATH_NAME_
	from
		uc_org org
	where
		orguser.ORG_ID_ = org.ID_)) "systemDeptName"
from
	uc_user uu
left join uc_external_user ueu on ueu.USER_ID_=uu.ID_
left join uc_external_user_dept ueud  on ueud.EXTERNAL_USER_ID_ =ueu.EXTERNAL_USER_ID_
left join uc_org_user orguser on orguser.USER_ID_=uu.ID_
where
	uu.ID_ = #{userId}
	group by uu.MOBILE_
	</select>
</mapper>
