<?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.zcloud.mapper.datasource.system.UsersMapper">

	<resultMap type="User" id="userAndRoleResultMap">
		<id column="USER_ID" property="USER_ID"/>
		<result column="USERNAME" property="USERNAME"/>
		<result column="PASSWORD" property="PASSWORD"/>
		<result column="NAME" property="NAME"/>
		<result column="LAST_LOGIN" property="LAST_LOGIN"/>
		<result column="IP" property="IP"/>
		<result column="STATUS" property="STATUS"/>
		<result column="SKIN" property="SKIN"/>
		<result column="ROLE_IDS" property="ROLE_IDS"/>
		<association property="role" column="ROLE_ID" javaType="Role">
			<id column="ROLE_ID" property="ROLE_ID"/>
			<result column="ROLE_NAME" property="ROLE_NAME"/>
			<result column="ROLE_RIGHTS" property="RIGHTS"/>
			<result column="ADD_QX" property="ADD_QX"/>
			<result column="DEL_QX" property="DEL_QX"/>
			<result column="EDIT_QX" property="EDIT_QX"/>
			<result column="CHA_QX" property="CHA_QX"/>
		</association>
	</resultMap>
	<resultMap type="User" id="userResultMap">
		<id column="USER_ID" property="USER_ID"/>
		<result column="USERNAME" property="USERNAME"/>
		<result column="PASSWORD" property="PASSWORD"/>
		<result column="NAME" property="NAME"/>
		<result column="LAST_LOGIN" property="LAST_LOGIN"/>
		<result column="IP" property="IP"/>
		<result column="STATUS" property="STATUS"/>
		<result column="ROLE_ID" property="ROLE_ID"/>
		<result column="SKIN" property="SKIN"/>
		<result column="ROLE_IDS" property="ROLE_IDS"/>
	</resultMap>

	<!--用户表名 -->
	<sql id="tableName">
		SYS_USER
	</sql>

	<!--角色表名 -->
	<sql id="roleTableName">
		SYS_ROLE
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		USER_ID,
		USERNAME,
		PASSWORD,
		NAME,
		ROLE_ID,
		LAST_CHANGE_PASSWORD,
		LAST_LOGIN,
		IP,
		STATUS,
		BZ,
		SKIN,
		EMAIL,
		SEX,
		NUMBER,
		PHONE,
		ROLE_IDS,
		DEPARTMENT_ID,
		POST_ID,
		ISMAIN,
<!-- 		CORPINFO_ID, -->
<!-- 		BASICINFO_ID, -->
		FUN_IDS,
		SORT,
		LEARNERCATEGORY,
		USERAVATARPREFIX,
		USERAVATARURL,
		SHIFTDUTYONE,
		SHIFTDUTYTWO,
		DURATION,
		WORKSTATUS,
		WORKPERIOD,
		ISSTUDENT,
		RIGHTS,
		CORPINFO_ID,
		IS_SAFETY,
		ISDELETE,
		ISHEAD,
		CARDNO,
		PLS_ID,
		OFFICE_ID,
		IMEI,
		WORK_ID,
		WORK_NAME,
		ISLEADER
	</sql>



	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USER_ID},
		#{USERNAME},
		#{PASSWORD},
		#{NAME},
		#{ROLE_ID},
		#{LAST_CHANGE_PASSWORD},
		#{LAST_LOGIN},
		#{IP},
		#{STATUS},
		#{BZ},
		#{SKIN},
		#{EMAIL},
		#{SEX},
		#{NUMBER},
		#{PHONE},
		#{ROLE_IDS},
		#{DEPARTMENT_ID},
		#{POST_ID},
		#{ISMAIN},
		#{FUN_IDS},
		#{SORT},
		#{LEARNERCATEGORY},
		#{USERAVATARPREFIX},
		#{USERAVATARURL},
		#{SHIFTDUTYONE},
		#{SHIFTDUTYTWO},
		#{DURATION},
		#{WORKSTATUS},
		#{WORKPERIOD},
		#{ISSTUDENT},
		#{RIGHTS},
		#{CORPINFO_ID},
		#{IS_SAFETY},
		0,
		#{ISHEAD},
		#{CARDNO},
		#{PLS_ID},
		#{OFFICE_ID},
		#{IMEI},
		#{WORK_ID},
		#{WORK_NAME},
		#{ISLEADER}
	</sql>

	<!-- 字段2 -->
	<sql id="Field2">
		f.USER_ID,
		f.USERNAME,
		f.PASSWORD,
		f.NAME,
		f.ROLE_ID,
		f.LAST_LOGIN,
		f.LAST_CHANGE_PASSWORD,
		f.IP,
		f.STATUS,
		f.BZ,
		f.SKIN,
		f.EMAIL,
		f.NUMBER,
		f.SEX,
		f.PHONE,
		f.ROLE_IDS,
		f.DEPARTMENT_ID,
		f.POST_ID,
		f.ISMAIN,
		f.FUN_IDS,
		f.SORT,
		f.LEARNERCATEGORY,
		f.USERAVATARPREFIX,
		f.USERAVATARURL,
		f.SHIFTDUTYONE,
		f.SHIFTDUTYTWO,
		f.DURATION,
		f.WORKSTATUS,
		f.WORKPERIOD,
		f.ISSTUDENT,
		f.IS_SAFETY,
		f.CORPINFO_ID,
		f.ISHEAD,
		f.CARDNO,
		f.PLS_ID,
		f.OPENID,
		f.WORK_ID,
		f.OFFICE_ID,
		f.WORK_NAME,
		f.IMEI,
		f.ISLEADER
	</sql>

	<!-- 通过USERNAME获取数据 -->
	<select id="findByUsername" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where ISDELETE = 0 and
		USERNAME = #{USERNAME}
		<if test="NAME != null and NAME != ''">
			and NAME = #{NAME}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''">
			and CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="VERIFYUSER_ID != null and VERIFYUSER_ID != ''">
			and USER_ID != #{VERIFYUSER_ID}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
			and DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
	</select>
		<select id="findByName" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where ISDELETE = 0 and
			NAME = #{NAME} AND CORPINFO_ID = #{CORPINFO_ID}
	</select>
	<!-- 通过用户ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd" >
		select
			CONVERT (f.USERAVATARURL USING utf8) AS USERAVATARURL_CONVERT,
			<include refid="Field2"></include>,
			p.NAME AS POST_NAME,
			d.NAME AS DEPARTMENT_NAME
		from
			<include refid="tableName"></include>f
			left join OA_DEPARTMENT d on d.DEPARTMENT_ID=f.DEPARTMENT_ID
			left join SYS_POST p on p.POST_ID=f.POST_ID
		where
			USER_ID = #{USER_ID} and f.ISDELETE = 0
	</select>

	<!-- 通过邮箱获取数据 -->
	<select id="findByEmail" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where ISDELETE = 0 and
		EMAIL = #{EMAIL}
		<if test="USERNAME != null and USERNAME != ''">
			and USERNAME != #{USERNAME}
		</if>
	</select>

	<!-- 通过编码获取数据 -->
	<select id="findByNumbe" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where ISDELETE = 0 and
		NUMBER = #{NUMBER}
		<if test="USERNAME != null and USERNAME != ''">
			and USERNAME != #{USERNAME}
		</if>
	</select>

	<!-- 列出某角色下的所有用户 -->
	<select id="listAllUserByRoldId"  parameterType="pd" resultType="pd" >
		select USER_ID
		from
		<include refid="tableName"></include>
		where ISDELETE = 0 and
		ROLE_ID = #{ROLE_ID}
	</select>

	<!-- 新增用户 -->
	<insert id="saveUser" parameterType="pd" >
		insert into <include refid="tableName"></include> (
			<include refid="Field"></include>
		) values (
			<include refid="FieldValue"></include>
		)
	</insert>

	<update id="editUserState" parameterType="pd" >
		update  <include refid="tableName"></include>
		set ISSTUDENT = 'true'
		where
		USER_ID = #{USER_ID}
	</update>
	<!-- 修改 -->
	<update id="editUser" parameterType="pd" >
		update  <include refid="tableName"></include>
			set NAME 					= #{NAME},
				DEPARTMENT_ID	= #{DEPARTMENT_ID},
				POST_ID				=#{POST_ID},
				ROLE_ID 				= #{ROLE_ID},
				ROLE_IDS 				= #{ROLE_IDS},
				BZ						= #{BZ},
				SEX						= #{SEX},
				EMAIL					= #{EMAIL},
				NUMBER					= #{NUMBER},
				SORT					= #{SORT},
				WORK_ID					= #{WORK_ID},
				WORK_NAME				= #{WORK_NAME},
				CARDNO					= #{CARDNO},
				PHONE					= #{PHONE},
				IMEI	 				= #{IMEI}
			<if test="LEARNERCATEGORY != null and LEARNERCATEGORY != ''">
				,LEARNERCATEGORY		= #{LEARNERCATEGORY}
			</if>
			<if test="USERAVATARPREFIX != null and USERAVATARPREFIX != ''">
				,USERAVATARPREFIX				= #{USERAVATARPREFIX}
				,USERAVATARURL				= #{USERAVATARURL}
			</if>
			<if test="DURATION != null and DURATION != ''">
				,DURATION				= #{DURATION}
			</if>
			<if test="WORKSTATUS != null and WORKSTATUS != ''">
				,WORKSTATUS				= #{WORKSTATUS}
			</if>
			<if test="WORKPERIOD != null and WORKPERIOD != ''">
				,WORKPERIOD				= #{WORKPERIOD}
			</if>
			<if test="SHIFTDUTYONE != null and SHIFTDUTYONE != ''">
				,SHIFTDUTYONE				= #{SHIFTDUTYONE}
			</if>
			<if test="SHIFTDUTYTWO != null and SHIFTDUTYTWO != ''">
				,SHIFTDUTYTWO				= #{SHIFTDUTYTWO}
			</if>
			<if test="PASSWORD != null and PASSWORD != ''">
				,PASSWORD = #{PASSWORD}
			</if>
			<if test="ISSTUDENT != null and ISSTUDENT != ''">
				,ISSTUDENT = #{ISSTUDENT}
			</if>
			<if test="IS_SAFETY != null and IS_SAFETY != ''">
				,IS_SAFETY = #{IS_SAFETY}
			</if>
			<if test="ISHEAD != null and ISHEAD != ''">
				,ISHEAD = #{ISHEAD}
			</if>
		<if test="ISLEADER != null and ISLEADER != ''">
				,ISLEADER = #{ISLEADER}
		</if>
		<if test="OFFICE_ID	 != null and OFFICE_ID	 != ''">
			,OFFICE_ID	 = #{OFFICE_ID}
		</if>

		where
				USER_ID = #{USER_ID}
	</update>

	<update id="editPassword" parameterType="pd" >
		update  <include refid="tableName"></include>
			set
			    PASSWORD				= #{PASSWORD},
				LAST_CHANGE_PASSWORD    = #{LAST_CHANGE_PASSWORD}
		where
				USER_ID = #{USER_ID}
	</update>

	<update id="updatePushCid" parameterType="pd" >
		update  <include refid="tableName"></include>
		set PUSH_CID = #{PUSH_CID}
		where
		USER_ID = #{USER_ID}
	</update>

		<!-- 获取获取部门账号 -->
	<select id="listUserbyDep" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where ISDELETE = 0 and
		DEPARTMENT_ID =#{DEPARTMENT_ID}
			and STATUS = '0'
		order by DEPARTMENT_ID, SORT
	</select>


	<!-- 用户列表 -->
	<select id="userlistPage" parameterType="page" resultType="pd" >
		select  u.USER_ID,
				u.USERNAME,
				u.PASSWORD,
				u.LAST_LOGIN,
				u.NAME,
				u.IP,
				u.EMAIL,
				u.NUMBER,
				u.PHONE,
				u.ISMAIN,
				u.SHIFTDUTYONE,
				u.SHIFTDUTYTWO,
				u.DURATION,
				u.WORKSTATUS,
				u.IMEI,
				u.WORKPERIOD,
				r.ROLE_ID,
				r.ROLE_NAME,
				d.NAME as DEPARTMENT_NAME,
				p.NAME AS POST_NAME,
				sw.NAME SHIFTDUTYONENAME,
				swr.NAME SHIFTDUTYTWONAME,
				info.IS_RECORDER,
				info.IS_CHARGE
				<if test="pd.IS_ARCHIVES != null and pd.IS_ARCHIVES != ''">		<!--档案列表 -->
					,(select count(1) from BUS_STAGESTUDENTRELATION s
					LEFT JOIN bus_studytask t on s.STUDYTASK_ID = t.STUDYTASK_ID
					                  where s.ISDELETE = 0 and t.ISDELETE = 0
					                    and s.USER_ID = u.USER_ID)	as TASK_COUNT,
					(select count(1) from BUS_STAGESTUDENTRELATION s
					LEFT JOIN bus_studytask t on s.STUDYTASK_ID = t.STUDYTASK_ID
					where s.ISDELETE = 0 and t.ISDELETE = 0
					                and s.USER_ID = u.USER_ID and s.STAGEEXAMSTATE=3) as COMPLETE_COUNT
				</if>
		from <include refid="tableName"></include> u
		left join SYS_ROLE r on u.ROLE_ID = r.ROLE_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		left join SYS_POST p on p.POST_ID=u.POST_ID
		left join BUS_SHIFTWORKRULES sw on sw.SHIFTWORKRULES_ID = u.SHIFTDUTYONE
		left join BUS_SHIFTWORKRULES swr on swr.SHIFTWORKRULES_ID = u.SHIFTDUTYTWO
		left join SYS_USERINFO info on info.USER_ID = u.USER_ID
		where
		u.ISDELETE = 0
		and u.USERNAME != 'admin'
		<if test="pd.VIPLEVEL != null and pd.VIPLEVEL != ''">		<!-- 角色检索 -->
			and r.PARENT_ID=#{pd.VIPLEVEL}
		</if>
		<if test="pd.VIPLEVEL == null or pd.VIPLEVEL == ''">		<!-- 角色检索 -->
			and r.PARENT_ID = '1'
		</if>
		<if test="pd.KEYWORDS!= null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					 u.USERNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.EMAIL LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NUMBER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.PHONE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.ROLE_ID != null and pd.ROLE_ID != ''">		<!-- 角色检索 -->
			and u.ROLE_ID=#{pd.ROLE_ID}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">		<!-- 角色检索 -->
			and u.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.STARTTIME!=null and pd.STARTTIME!=''">	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME!=null and pd.ENDTIME!=''">	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.SHIFTDUTYONE != null and pd.SHIFTDUTYONE != ''">
			and u.SHIFTDUTYONE=#{pd.SHIFTDUTYONE}
		</if>
		<if test="pd.SHIFTDUTYTWO != null and pd.SHIFTDUTYTWO != ''">
			and u.SHIFTDUTYTWO=#{pd.SHIFTDUTYTWO}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				u.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.IS_ARCHIVES != null and pd.IS_ARCHIVES != ''">
			and (select count(1) from BUS_STAGESTUDENTRELATION s where s.USER_ID = u.USER_ID) &gt; 0
		</if>
		<if test="pd.IS_STUDENT != null and pd.IS_STUDENT != ''">
			and u.ISSTUDENT = #{pd.IS_STUDENT}
		</if>
		<if test="pd.IS_RECORDER != null and pd.IS_RECORDER != '' and pd.IS_RECORDER == 1 ">
			and info.IS_RECORDER =  #{pd.IS_RECORDER}
		</if>
		<if test="pd.IS_CHARGE != null and pd.IS_CHARGE != '' and pd.IS_CHARGE == 1 ">
			and info.IS_CHARGE =  #{pd.IS_CHARGE}
		</if>
		order by  d.LEVEL  asc ,d.DEP_ORDER,d.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>

	<!-- 用户列表 -->
	<select id="userSelectlistPage" parameterType="page" resultType="pd" >
		select  u.USER_ID,
				u.USERNAME,
				u.PASSWORD,
				u.LAST_LOGIN,
				u.NAME,
				u.IP,
				u.EMAIL,
				u.NUMBER,
				u.PHONE,
				u.ISMAIN,
				u.PUSH_CID,
				u.IMEI,
				r.ROLE_ID,
				r.ROLE_NAME,
				d.NAME as DEPARTMENT_NAME,
				p.NAME AS POST_NAME
		from <include refid="tableName"></include> u
		left join SYS_ROLE r on u.ROLE_ID = r.ROLE_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		left join SYS_POST p on p.POST_ID=u.POST_ID
		where
		u.ISDELETE = 0 and
		u.USERNAME != 'admin'
		<if test="pd.KEYWORDS!= null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					 u.USERNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.EMAIL LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NUMBER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.PHONE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			d.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.ROLE_ID != null and pd.ROLE_ID != ''">		<!-- 角色检索 -->
			and u.ROLE_ID=#{pd.ROLE_ID}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">		<!-- 角色检索 -->
			and u.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.STARTTIME!=null and pd.STARTTIME!=''">	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME!=null and pd.ENDTIME!=''">		 	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				u.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		order by d.LEVEL  asc ,d.DEP_ORDER,d.DEPARTMENT_ID asc, u.SORT asc,u.USER_ID
	</select>

	<!-- 获取账号及部门岗位 -->
	<select id="listUserlistPage" parameterType="page" resultType="pd" >
		select
		u.USER_ID,
		u.NAME USERNAME,
		d.NAME DEPARTNAME,
		getFullName(d.DEPARTMENT_ID) DEPARTNAME_ALL,
		p.NAME POSTNAME
		from
		<include refid="tableName"></include> u
		LEFT JOIN oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID and p.ISDELETE = '0' and p.STATUS = '0'
		where
		u.CORPINFO_ID =#{pd.CORPINFO_ID}
		AND u.STATUS = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			p.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<!-- <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">关键词检索
            and d.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if> -->


		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
					and
					d.DEPARTMENT_ID in
					<foreach item="item" index="index"
					         collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
				</if>
			</when>
			<otherwise>
				<choose>
					<when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
								         collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								d.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
								)
							</otherwise>
						</choose>
					</when>
					<when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
						and d.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>

		<if test="pd.NOMAIN != null and pd.NOMAIN != ''"><!-- 关键词检索 -->
			and u.ISMAIN = '0'
		</if>
		order by
		d.LEVEL,d.DEP_ORDER,d.DEPARTMENT_ID,u.SORT
	</select>

	<!-- 用户列表(全部) -->
	<select id="listAllUser" parameterType="pd" resultType="com.zcloud.entity.PageData" >
		select
			u.USER_ID,
			u.USERNAME,
			u.PASSWORD,
			u.LAST_LOGIN,
			u.NAME,
			u.IP,
			u.IMEI,
			u.EMAIL,
			u.NUMBER,
			u.PHONE,
			r.ROLE_ID,
			r.ROLE_NAME
		from <include refid="tableName"></include> u, <include refid="roleTableName"></include> r
		where u.ISDELETE = 0 and  u.ROLE_ID = r.ROLE_ID
		and u.USERNAME != 'admin'
<!-- 		and r.PARENT_ID = '1' -->
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					 u.USERNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.EMAIL LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.NUMBER LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.PHONE LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>
		<if test="ROLE_ID != null and ROLE_ID != ''"><!-- 角色检索 -->
			and u.ROLE_ID=#{ROLE_ID}
		</if>
		<if test="STARTTIME!=null and STARTTIME!=''"><!-- 登录时间检索 -->
			and u.LAST_LOGIN &gt;= #{STARTTIME}
		</if>
		<if test="ENDTIME!=null and ENDTIME!=''"><!-- 登录时间检索 -->
			and u.LAST_LOGIN &lt;= #{ENDTIME}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				u.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 关键词检索 -->
			AND
				u.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="POST_ID != null and POST_ID != ''"><!-- 关键词检索 -->
			AND
				u.POST_ID = #{POST_ID}
		</if>
		<if test="NAME != null and NAME != ''"><!-- 关键词检索 -->
			AND
			u.NAME = #{NAME}
		</if>
		<if test="NOSELF != null and NOSELF != ''"><!-- 关键词检索 -->
			AND
				u.USER_ID != #{LOGINUSERID}
		</if>
		<if test="NOMAIN != null and NOMAIN != ''"><!-- 关键词检索 -->
			AND
				u.ISMAIN != '1'
		</if>
		<if test="ISMAIN != null and ISMAIN != ''"><!-- 关键词检索 -->
			AND
				u.ISMAIN  =#{ISMAIN}
		</if>
		<if test="ATTRIBUTE != null and ATTRIBUTE != '' and ATTRIBUTE == 2 "><!-- 包保清单用的，如果是包保清单这个人必须得有IMEI -->
			AND
			u.IMEI != '' and IMEI is not null
		</if>
		order by u.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>

		<!-- 查询部门下用户 -->
	<select id="listUserByDept" parameterType="pd" resultType="pd" >
		select
			u.USER_ID,
			u.USERNAME,
			u.PASSWORD,
			u.LAST_LOGIN,
			u.NAME,
			u.IP,
			u.EMAIL,
			u.NUMBER,
			u.PHONE,
			r.ROLE_ID,
			r.ROLE_NAME
		from <include refid="tableName"></include> u, <include refid="roleTableName"></include> r
		where u.ISDELETE = 0 and  u.ROLE_ID = r.ROLE_ID
		and u.USERNAME != 'admin'
<!-- 		and r.PARENT_ID = '1' -->
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					 u.USERNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.EMAIL LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.NUMBER LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					 u.PHONE LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>
		<if test="ROLE_ID != null and ROLE_ID != ''"><!-- 角色检索 -->
			and u.ROLE_ID=#{ROLE_ID}
		</if>
		<if test="STARTTIME!=null and STARTTIME!=''"><!-- 登录时间检索 -->
			and u.LAST_LOGIN &gt;= #{STARTTIME}
		</if>
		<if test="ENDTIME!=null and ENDTIME!=''"><!-- 登录时间检索 -->
			and u.LAST_LOGIN &lt;= #{ENDTIME}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				u.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<choose>
			<when test='ISMAIN != null and ISMAIN != "" and ISMAIN == "1"'>
				<if test="DEPT_IDS != null and DEPT_IDS != ''">
	                and
					u.DEPARTMENT_ID in
					<foreach item="item" index="index"
						collection="DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
	            </if>
			</when>
			<otherwise>
				<choose>
		            <when test='ISSUPERVISE != null and ISSUPERVISE != "" and ISSUPERVISE == "1"'>
		            	<choose>
				            <when test="DEPT_IDS != null and DEPT_IDS != ''">
				                and
								u.DEPARTMENT_ID in
								<foreach item="item" index="index"
									collection="DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
				            </when>
				            <otherwise>
				                and
								u.DEPARTMENT_ID in (
									select
										SUB_DEPARTMENT_ID
									from
										oa_supervision_department osd
									where
										osd.SUP_DEPARTMENT_ID = #{DEPARTMENT_ID}
										and osd.ISDELETE = '0'
										and osd.CORPINFO_ID  = #{CORPINFO_ID}
								)
				            </otherwise>
				        </choose>
		            </when>
		            <when test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
		                and u.DEPARTMENT_ID=#{DEPARTMENT_ID}
		            </when>
		            <otherwise>
		            </otherwise>
		        </choose>
			</otherwise>
		</choose>
		<if test="NOSELF != null and NOSELF != ''"><!-- 关键词检索 -->
			AND
				u.USER_ID != #{LOGINUSERID}
		</if>
		order by u.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>

	<!-- 通过用户ID获取用户信息和角色信息 -->
	<select id="getUserAndRoleById" parameterType="String" resultMap="userAndRoleResultMap">
		select  u.USER_ID,
				u.USERNAME,
				u.NAME,
				u.PASSWORD,
				u.SKIN,
				u.NUMBER,
				u.ROLE_IDS,
				r.ROLE_ID,
				r.ROLE_NAME,
				r.RIGHTS as ROLE_RIGHTS,
				r.ADD_QX,
				r.DEL_QX,
				r.EDIT_QX,
				r.CHA_QX
		from
		<include refid="tableName"></include> u
		left join
		<include refid="roleTableName"></include> r
		on u.ROLE_ID=r.ROLE_ID
		where u.ISDELETE = 0 and u.STATUS=0
		and u.USER_ID=#{USER_ID}
	</select>

	<!-- 存入IP -->
	<update id="saveIP" parameterType="pd" >
		update
		<include refid="tableName"></include>
		set
			IP = #{IP},
			LAST_LOGIN = #{LAST_LOGIN}
		where
			USERNAME = #{USERNAME}
	</update>

	<!-- 保存用户皮肤 -->
	<update id="saveSkin" parameterType="pd" >
		update
		<include refid="tableName"></include>
		set
			SKIN = #{SKIN}
		where USERNAME = #{USERNAME}
	</update>

	<!-- 删除用户 -->
	<update id="deleteUser" parameterType="pd">
		update
			<include refid="tableName"></include>
		set ISDELETE = 1,USERNAME = CONCAT(USERNAME,'已删除')
		where
			USER_ID = #{USER_ID}
		and
				USER_ID != '1'
	</update>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		f.USER_ID,
		f.USERNAME,
		un.NAME as POST_NAME
		from
		<include refid="tableName"></include> f
		left join SYS_POST un on un.POST_ID = f.POST_ID
		where f.ISDELETE = '0'
		<if test="POST_ID != null and POST_ID != ''">
			and f.POST_ID=#{POST_ID}
		</if>
	</select>

	<!-- 批量删除用户 -->
	<update id="deleteAllUser" parameterType="String" >
		update <include refid="tableName"></include>
		set ISDELETE = 1
		where
			USER_ID in
  			 <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                  #{item}
             </foreach>
            and
				USER_ID != '1'
	</update>

	<!-- 用户列表(弹窗选择用) -->
	<select id="userBystafflistPage" parameterType="page" resultType="pd" >
		select  u.USER_ID,
				u.USERNAME,
				u.PASSWORD,
				u.LAST_LOGIN,
				u.NAME,
				u.IP,
				u.EMAIL,
				u.NUMBER,
				u.PHONE,
				r.ROLE_ID,
				r.ROLE_NAME
		from <include refid="tableName"></include> u, <include refid="roleTableName"></include> r
		where u.ISDELETE = 0 and u.ROLE_ID = r.ROLE_ID
		and u.USERNAME != 'admin'
		<if test="pd.KEYWORDS!= null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					 u.USERNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.EMAIL LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NUMBER LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					 u.PHONE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.ROLE_ID != null and pd.ROLE_ID != ''"><!-- 角色检索 -->
			and u.ROLE_ID=#{pd.ROLE_ID}
		</if>
		<if test="pd.STARTTIME!=null and pd.STARTTIME!=''">	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME!=null and pd.ENDTIME!=''">		 	<!-- 登录时间检索 -->
			and u.LAST_LOGIN &lt;= #{pd.ENDTIME}
		</if>
		order by u.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>

	<!--设置小程序菜单 -->
	<update id="editUserFuns" parameterType="pd" >
		update  <include refid="tableName"></include>
			set FUN_IDS = #{FUN_IDS}
		where
				USER_ID = #{USER_ID}
	</update>

	<!-- 查询小程序用户 -->
	<select id="findAppUserById" parameterType="pd" resultType="pd" >
		select
			f.ID,
			f.NAME,
			f.APPID,
			f.ISAGREEPROTOCOL,
			f.USER_TYPE,
			bw.BALANCE,
			bp.POINTS
		from
			user_view f
			left join bus_wallet bw on bw.ISDELETE = '0' and bw.USER_ID = f.APPID and bw.USER_TYPE = f.USER_TYPE
			left join bus_points bp on bp.ISDELETE = '0' and bp.USER_ID = f.APPID
		where
			f.APPID = #{APPID}
		<if test="USER_TYPE!=null and USER_TYPE!=''">		 	<!-- 登录时间检索 -->
			and f.USER_TYPE = #{USER_TYPE}
		</if>
	</select>

	<!--用户同意协议 -->
	<update id="agreeProtocol" parameterType="pd" >
		update
			sys_user
		SET
			ISAGREEPROTOCOL = '1'
		where
			APPID = #{APPID}
	</update>
		<!-- 列出某角色下的所有用户 -->
	<select id="findUserNameAll"  parameterType="pd" resultType="pd" >
		select *
		from
		<include refid="tableName"></include>
		where ISDELETE = 0
		order by DEPARTMENT_ID,SORT
	</select>
		<!-- 列出所有用户 -->
	<select id="findAllUser"  parameterType="pd" resultType="pd" >
		select *
		from
		<include refid="tableName"></include>
		where ISDELETE = 0
		order by DEPARTMENT_ID,SORT
	</select>

		<!-- 获取企业的主账号 -->
	<select id="findCorpMain" parameterType="pd" resultType="pd" >
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include>
		where
			ISMAIN = '1'
			and CORPINFO_ID =#{CORPINFO_ID}
			and STATUS = '0'
	</select>


	<!-- 获取账号及部门岗位 -->
	<select id="listUserDepPos" parameterType="pd" resultType="pd" >
		select
			u.USER_ID,
			u.NAME USERNAME,
			d.NAME DEPARTNAME,
			getFullName(d.DEPARTMENT_ID) DEPARTNAME_ALL,
			p.NAME POSTNAME
		from
			<include refid="tableName"></include> u
		left join oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID and p.ISDELETE = '0' and p.STATUS = '0'
		where
			u.ISDELETE = 0 and
			u.CORPINFO_ID =#{CORPINFO_ID}
			and u.STATUS = '0'
			<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					p.NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					 or
					u.NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
			</if>
		<!-- <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">关键词检索
            and d.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if> -->


		<choose>
			<when test='ISMAIN != null and ISMAIN != "" and ISMAIN == "1"'>
				<if test="DEPT_IDS != null and DEPT_IDS != ''">
					and
					d.DEPARTMENT_ID in
					<foreach item="item" index="index"
							 collection="DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
				</if>
			</when>
			<otherwise>
				<choose>
					<when test='ISSUPERVISE != null and ISSUPERVISE != "" and ISSUPERVISE == "1"'>
						<choose>
							<when test="DEPT_IDS != null and DEPT_IDS != ''">
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								d.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{CORPINFO_ID}
								)
							</otherwise>
						</choose>
					</when>
					<when test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
						and d.DEPARTMENT_ID=#{DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>

			<if test="NOMAIN != null and NOMAIN != ''"><!-- 关键词检索 -->
				and u.ISMAIN = '0'
			</if>
		order by
		d.LEVEL,d.DEP_ORDER,d.DEPARTMENT_ID,u.SORT
	</select>

	<select id="listUserBYids" parameterType="String" resultType="pd" >
		select  u.USER_ID,
				u.USERNAME,
				u.PASSWORD,
				u.LAST_LOGIN,
				u.NAME,
				u.IP,
				u.EMAIL,
				u.NUMBER,
				u.PHONE,
				u.ISMAIN,
				r.ROLE_ID,
				r.ROLE_NAME,
				u.DEPARTMENT_ID,
				d.NAME as DEPARTMENT_NAME,
				u.POST_ID,
				p.NAME AS POST_NAME
		from <include refid="tableName"></include> u
		left join SYS_ROLE r on u.ROLE_ID = r.ROLE_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		left join SYS_POST p on p.POST_ID=u.POST_ID
		where
			USER_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
		order by  d.LEVEL  asc ,d.DEP_ORDER  asc ,d.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>

	<!-- 查询公司员工总数 -->
	<select id="getUserCount" parameterType="pd" resultType="int" >
		SELECT
			COUNT( 1 )
		FROM
			sys_user
		WHERE
		 	ISDELETE = 0 and CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<!-- 查询公司检查过清单的员工总数 -->
	<select id="getWorkedUserCount" parameterType="pd" resultType="int" >
		SELECT
			count( 1 )
		FROM
			(
				SELECT
					count( s.USER_ID )
				FROM
					sys_user s
						LEFT JOIN bus_checkuser cu ON s.USER_ID = cu.USER_ID
						LEFT JOIN bus_checkrecord f ON f.CHECKRECORD_ID = cu.CHECKRECORD_ID
				WHERE
					s.ISDELETE = 0 and
					s.CORPINFO_ID = #{CORPINFO_ID}
				  AND f.FINISHED = '1'
				  AND f.isdelete = '0'
				  AND f.CHECKRECORD_ID IS NOT NULL
				GROUP BY
					s.USER_ID
			) q
	</select>

	<!-- 查询用户倒班周期数据 -->
	<select id="getUserWork" parameterType="pd" resultType="pd" >
		SELECT
			s.USER_ID,
			s.NAME,
			s.DURATION,
			s.WORKSTATUS,
			s.WORKPERIOD,
			swp.ONDAY,
			swp.OFFDAY,
			swp.SORT,
			swp.ISEND
		FROM
			SYS_USER s
			LEFT JOIN BUS_SHIFTWORKRULES swr ON swr.SHIFTWORKRULES_ID = s.SHIFTDUTYTWO
			LEFT JOIN BUS_SHIFTWORKPERIOD swp ON swp.SHIFTWORKRULES_ID = swr.SHIFTWORKRULES_ID
		WHERE
			s.ISDELETE = 0 and
			s.WORKPERIOD = swp.SORT
			AND s.CORPINFO_ID = swr.CORPINFO_ID
			AND swr.ISDELETE = '0'
			AND swp.ISDELETE = '0'
			AND swr.ISCUSTOM = '1'
	</select>

	<!--修改用户倒班周期 -->
	<update id="editShiftWork" parameterType="pd" >
		update
		sys_user
		SET
		USER_ID = #{USER_ID}
		<if test="DURATION != null and DURATION != ''"><!-- 关键词检索 -->
			, DURATION = #{DURATION}
		</if>
		<if test="WORKSTATUS != null and WORKSTATUS != ''"><!-- 关键词检索 -->
			, WORKSTATUS = #{WORKSTATUS}
		</if>
		<if test="WORKPERIOD != null and WORKPERIOD != ''"><!-- 关键词检索 -->
			, WORKPERIOD = #{WORKPERIOD}
		</if>
		where
		USER_ID = #{USER_ID}
	</update>


	<!-- 获取人员Cascader数据(人员ID，姓名，部门ID，部门级别) -->
	<select id="listCascader" parameterType="pd" resultType="pd" >
		select
		u.USER_ID,
		u.NAME USERNAME,
		u.LEARNERCATEGORY,
		d.NAME DEPARTMENTNAME,
		d.LEVEL
		from
		<include refid="tableName"></include> u
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		where
		u.ISDELETE = 0 and
		u.CORPINFO_ID = #{CORPINFO_ID}
		and u.STATUS = '0'
		<if test="BIANMA != null and BIANMA != '' and BIANMA != 'departmentLevel0001'" ><!-- 部门级别 -->
			and d.LEVEL = #{BIANMA}
		</if>
		<if test="BIANMA == 'departmentLevel0001'"><!-- 部门级别 -->
			and d.PARENT_ID = '0'
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 部门ID -->
			and u.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="LEARNERCATEGORY != null and LEARNERCATEGORY != ''"><!-- 关键词检索 -->
			and u.LEARNERCATEGORY = #{LEARNERCATEGORY}
		</if>
		order by u.SORT
	</select>

	<!--清除在线学习人员分类 -->
	<update id="clearLearnerCategory" parameterType="pd" >
		UPDATE
		<include refid="tableName"></include> u
		SET
		u.LEARNERCATEGORY = NULL
		WHERE
		u.CORPINFO_ID = #{CORPINFO_ID}
		and u.LEARNERCATEGORY = #{DICTIONARIES_ID}
	</update>
	<!-- 获取账号及部门岗位 -->
	<select id="listUserDepPoslistPage" parameterType="page" resultType="pd" >
		select
		u.USER_ID,
		u.NAME USERNAME,
		d.NAME DEPARTNAME,
		getFullName(d.DEPARTMENT_ID) DEPARTNAME_ALL,
		p.NAME POSTNAME
		from
		<include refid="tableName"></include> u
		left join oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID and p.ISDELETE = '0' and p.STATUS = '0'
		where
		u.ISDELETE = 0 and
		u.CORPINFO_ID =#{pd.CORPINFO_ID}
		and u.STATUS = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			p.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<!-- <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">关键词检索
            and d.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if> -->


		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
					and
					d.DEPARTMENT_ID in
					<foreach item="item" index="index"
							 collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
				</if>
			</when>
			<otherwise>
				<choose>
					<when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								d.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
								)
							</otherwise>
						</choose>
					</when>
					<when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
						and d.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>

		<if test="pd.NOMAIN != null and pd.NOMAIN != ''"><!-- 关键词检索 -->
			and u.ISMAIN = '0'
		</if>
		order by
		d.LEVEL,d.DEP_ORDER,d.DEPARTMENT_ID,u.SORT
	</select>

	<!-- 获取账号及部门岗位 -->
	<select id="listUserDepPoslist" parameterType="page" resultType="pd" >
		select
		u.USER_ID,
		u.NAME USERNAME,
		d.NAME DEPARTNAME,
		getFullName(d.DEPARTMENT_ID) DEPARTNAME_ALL,
		p.NAME POSTNAME
		from
		<include refid="tableName"></include> u
		left join oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID and p.ISDELETE = '0' and p.STATUS = '0'
		where
		u.ISDELETE = 0 and
		u.CORPINFO_ID =#{pd.CORPINFO_ID}
		and u.STATUS = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			p.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<!-- <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">关键词检索
            and d.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if> -->


		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
					and
					d.DEPARTMENT_ID in
					<foreach item="item" index="index"
							 collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
				</if>
			</when>
			<otherwise>
				<choose>
					<when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								d.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
								)
							</otherwise>
						</choose>
					</when>
					<when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
						and d.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>

		<if test="pd.NOMAIN != null and pd.NOMAIN != ''"><!-- 关键词检索 -->
			and u.ISMAIN = '0'
		</if>
		order by
		d.LEVEL,d.DEP_ORDER,d.DEPARTMENT_ID,u.SORT
	</select>
		<update id="updateCornUser" parameterType="pd" >
		update
		<include refid="tableName"></include>
		set
			USERNAME = #{USERNAME},
			NAME = #{NAME},
			PASSWORD = #{PASSWORD}
		where
			USERNAME = #{OLDUSERNAME}
			and
			CORPINFO_ID = #{CORPINFO_ID}
	</update>
	<update id="removeCardNoUser">
		update
		<include refid="tableName"></include>
		set
		CARDNO = null
		where
		CORPINFO_ID = #{CORPINFO_ID} and
		CARDNO in <foreach item="item" collection="USER_IDS" index="index" separator="," open="(" close=")">
		#{item}
	</foreach>
	</update>
	<update id="editUserCardNoByPid">
		update
		<include refid="tableName"></include>
		set
			CARDNO = #{cardNo}
		where
		PLS_ID = #{psnId}
	</update>
	<update id="editUserWorkName">
		update
		<include refid="tableName"></include>
		set
		WORK_NAME = #{officeName}
		WHERE
		CORPINFO_ID = #{CORPINFO_ID} and WORK_ID = #{WORK_ID} and isdelete = 0
	</update>
	<update id="removeUserWorkName">
		update
		<include refid="tableName"></include>
		set
		WORK_NAME = null,
		WORK_ID = null
		WHERE
		CORPINFO_ID = #{CORPINFO_ID}
		and WORK_ID in
		    <foreach item="item" collection="list" index="index" separator="," open="(" close=")">
				#{item}
			</foreach>
	</update>

	<!-- 查询用户名称及部门名称 -->
	<select id="findUserDept" parameterType="pd" resultType="pd" >
		SELECT
			s.USER_ID,
			s.NAME USER_NAME,
			d.NAME DEPARTMENT_NAME
		FROM
			SYS_USER s
			LEFT JOIN OA_DEPARTMENT d ON d.DEPARTMENT_ID = s.DEPARTMENT_ID
		WHERE
			s.USER_ID = #{USER_ID}
	</select>

	<!-- 用户列表(本部门及所有上级部门领导负责人) -->
	<select id="listUserLeader"  parameterType="pd" resultType="pd" >
		SELECT
			f.USER_ID,
			f.NAME USER_NAME,
			d.NAME DEPARTMENT_NAME,
			CONCAT(f.NAME, ' [', d.NAME, ']') NAME
		FROM
			SYS_USER f
				LEFT JOIN OA_DEPARTMENT d ON d.DEPARTMENT_ID = f.DEPARTMENT_ID
		where f.ISDELETE = 0 and f.ISLEADER = '1'
		<if test="DEPT_IDS != null and !DEPT_IDS.isEmpty()">
			and d.DEPARTMENT_ID in
			<foreach item="item" index="index"
					 collection="DEPT_IDS" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
		order by d.DEPARTMENT_ID,d.DEP_ORDER
	</select>

	<!-- 用户列表(全部) -->
	<select id="listUserAndDept" parameterType="pd" resultType="com.zcloud.entity.PageData">
		select u.USER_ID,
		u.USERNAME,
		u.PASSWORD,
		u.LAST_LOGIN,
		u.NAME,
		u.IP,
		u.EMAIL,
		u.NUMBER,
		u.PHONE,
		u.DEPARTMENT_ID,
		d.NAME as deptName
		from <include refid="tableName"></include> u
		left join oa_department d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		where 1=1
		and u.DEPARTMENT_ID is not null and u.DEPARTMENT_ID !='0'
		<if test="userIds != null">
			and u.USER_ID in
			<foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		<if test="corpinfoId != null and corpinfoId != ''"><!--  -->
			AND u.CORPINFO_ID = #{corpinfoId}
		</if>

		<if test="userName!= null and userName != ''"><!-- 关键词检索 -->
			and
			(
			u.USERNAME LIKE CONCAT(CONCAT('%', #{userName}),'%')
			or
			u.NUMBER LIKE CONCAT(CONCAT('%', #{userName}),'%')
			or
			u.NAME LIKE CONCAT(CONCAT('%', #{userName}),'%')
			or
			u.PHONE LIKE CONCAT(CONCAT('%', #{userName}),'%')
			)
		</if>
		<if test="deptId != null and deptId != ''"><!--  -->
			AND u.DEPARTMENT_ID = #{deptId}
		</if>
		order by u.LAST_LOGIN desc
	</select>

	<select id="findByRegulatoryId" parameterType="pd" resultType="com.zcloud.entity.PageData">
		select
			*
		from
			`qa-gwj-regulatory`.SYS_USER
		where USER_ID = #{USER_ID}
	</select>

	<!-- 通过用户ID获取数据 -->
	<select id="findByIdForEmis" parameterType="pd" resultType="com.zcloud.entity.PageData" >
		select
		f.USER_ID,
		f.USERNAME,
		f.JCR,
		d.DJBM,
		d.NAME as deparName
		from
		<include refid="tableName"></include>f
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID= f.DEPARTMENT_ID
		where
		f.USER_ID = #{USER_ID}
	</select>
	<select id="findByCardNo" resultType="com.zcloud.entity.PageData">
		select
			*
		from
			<include refid="tableName"></include>
		where
		CARDNO = #{CARDNO} and CORPINFO_ID = #{CORPINFO_ID}  and ISDELETE = 0 and CARDNO != '' and CARDNO is not null
	</select>
	<select id="getUserCardNoAll" resultType="com.zcloud.entity.PageData">
		select
		USERNAME,
		CARDNO
		from
			<include refid="tableName"></include>
		where CORPINFO_ID = #{CORPINFO_ID}  and ISDELETE = 0
	</select>
	<select id="getUserByCardNo" resultType="com.zcloud.entity.PageData">
		select  u.*,
		r.ROLE_ID,
		r.ROLE_NAME,
		d.NAME as DEPARTMENT_NAME,
		p.NAME AS POST_NAME
		from <include refid="tableName"></include> u
		left join SYS_ROLE r on u.ROLE_ID = r.ROLE_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=u.DEPARTMENT_ID
		left join SYS_POST p on p.POST_ID=u.POST_ID
		where
		u.CARDNO = #{CARDNO} and u.ISDELETE = 0
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''">
			and u.CORPINFO_ID = #{CORPINFO_ID}
		</if>
	</select>

	<select id="selectUserListByUserIds" resultType="com.zcloud.entity.PageData">
		<if test=" ids != null and ids != ''">
			select
			f.NAME,
			f.DEPARTMENT_ID as INSPECTION_DEPARTMENT_ID,
			f.USER_ID as INSPECTION_USER_ID,
			sd.name as punishname
			from sys_user f
			LEFT JOIN oa_department sd ON sd.DEPARTMENT_ID = f.DEPARTMENT_ID
			<foreach item="item" index="index" collection="ids" open="WHERE" separator="or">
				<if test=" item != null and item != ''">
					USER_ID like CONCAT(CONCAT('%', #{item}),'%')
				</if>
			</foreach>
		</if>
	</select>
	<select id="hasUserUseWord" resultType="java.lang.Integer">
		select count(1) from sys_user where CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0 and WORK_ID in
		<foreach item="item" index="index" collection="DATA_IDS" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<!-- 通过编码获取数据 -->
	<select id="findByOPENID" parameterType="pd" resultType="pd" >
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where ISDELETE = 0 and
		OPENID = #{OPENID}
	</select>

	<update id="editOPENID" parameterType="pd" >
		update  <include refid="tableName"></include>
		set OPENID = #{OPENID}
		where
		USERNAME = #{USERNAME}
	</update>

</mapper>
