<?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.ics.system.mapper.UserMapper">

	<resultMap type="com.ics.system.domain.User" id="UserResult">
		<id     property="id"       	column="id"      		/>
		<result property="username"     column="username"   	/>
		<result property="nickname"     column="nickname"    	/>
		<result property="openid"       column="openid"       	/>
		<result property="email"        column="email"        	/>
		<result property="mobile"  		column="mobile"  	  	/>
		<result property="dtype"  		column="dtype"  	  	/>
		<result property="gender"       column="gender"    		/>
		<result property="avatar"       column="avatar"       	/>
		<result property="password"     column="password"     	/>
		<result property="salt"         column="salt"         	/>
		<result property="status"       column="status"       	/>
		<result property="loginIp"      column="login_ip"     	/>
		<result property="loginDate"    column="login_date"   	/>
		<result property="version"      column="version"        />
        <result property="deleteFlag"   column="delete_flag"    />
		<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="deptId"       column="dept_id"      	/>
		<result property="parkId"       column="park_id"      	/>
		<result property="tenantId"       column="tenant_id"    />
		<result property="staffId"       column="staff_id"    />
		<result property="customerId"       column="customer_id"    />
		<association property="dept" column="id" javaType="com.ics.system.domain.Dept" resultMap="deptResult" />
		<collection  property="roles" javaType="java.util.List" resultMap="RoleResult" />
		<collection  property="roleIds" javaType="java.util.List" resultMap="RoleIdsResult" />
	</resultMap>

	<resultMap id="deptResult" type="com.ics.system.domain.Dept">
		<id     property="id"   	column="dept_id"     />
		<result property="parentId" column="parent_id"   />
		<result property="deptName" column="dept_name"   />
		<result property="orderNum" column="order_num"   />
		<result property="status"   column="dept_status" />
	</resultMap>

	<resultMap id="RoleIdsResult" type="java.lang.Long">
		<id     property="roleId"       column="role_id" />
	</resultMap>

	<resultMap id="RoleResult" type="com.ics.system.domain.Role">
		<id     property="id"       	column="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="selectUserVo">
        SELECT u.id,
			   u.dept_id,
			   u.username,
			   u.nickname,
			   u.openid,
			   u.email,
			   u.mobile,
			   u.gender,
			   u.avatar,
			   u.password,
			   u.salt,
			   u.status,
			   u.login_ip,
			   u.login_date,
			   u.create_time,
               u.tenant_id,
               u.customer_id,
               u.park_id,
               u.staff_id,
			   d.id AS dept_id,
			   d.parent_id,
			   d.dept_name,
			   d.order_num,
			   d.status AS dept_status,
			   r.id AS 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 sys_dept d
					  ON u.dept_id = d.id
			   LEFT JOIN sys_user_role ur
					  ON u.id = ur.user_id
			   LEFT JOIN sys_role r
					  ON r.id = ur.role_id
    </sql>

	<select id="selectUserList" parameterType="com.ics.system.domain.User" resultMap="UserResult">
		SELECT u.id,
			   u.dept_id,
			   u.username,
			   u.nickname,
			   u.openid,
			   u.email,
			   u.mobile,
			   u.password,
			   u.gender,
			   u.avatar,
			   u.salt,
			   u.status,
			   u.login_ip,
			   u.customer_id,
			   u.login_date,
			   u.create_by,
			   u.create_time,
		       u.park_id,
		       u.staff_id,
			   d.dept_name,
			   NULL AS role_id
		FROM sys_user u
				 LEFT JOIN sys_dept d
					ON u.dept_id = d.id
		<where>
		<if test="dtype != null">
			AND u.dtype = #{dtype.value}
		</if>
		<if test="username != null and username != ''">
			AND u.username like concat('%', #{username}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="mobile != null and mobile != ''">
			AND u.mobile like concat('%', #{mobile}, '%')
		</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>
		<if test="deptId != null and deptId != 0">
			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.id FROM sys_dept t WHERE FIND_IN_SET (#{deptId}, ancestors) ))
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		</where>
	</select>

	<select id="selectAllocatedList" parameterType="com.ics.system.domain.User" resultMap="UserResult">
		SELECT DISTINCT
		        u.id,
                u.dept_id,
                u.username,
                u.nickname,
                u.email,
                u.mobile,
                u.status,
                u.staff_id,
                u.create_time
		FROM sys_user u
				 LEFT JOIN sys_dept d
						   ON u.dept_id = d.id
				 LEFT JOIN sys_user_role ur
						   ON u.id = ur.user_id
				 LEFT JOIN sys_role r
						   ON r.id = ur.role_id
		WHERE dtype = 'Admin'
		AND r.id = #{roleId}
		<if test="username != null and username != ''">
			AND u.username like concat('%', #{username}, '%')
		</if>
		<if test="mobile != null and mobile != ''">
			AND u.mobile like concat('%', #{mobile}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="com.ics.system.domain.User" resultMap="UserResult">
	    SELECT DISTINCT
	            u.id,
                u.dept_id,
                u.username,
                u.nickname,
                u.email,
                u.mobile,
                u.status,
                u.staff_id,
                u.create_time
		FROM sys_user u
				 LEFT JOIN sys_dept d ON u.dept_id = d.id
				 LEFT JOIN sys_user_role ur ON u.id = ur.user_id
				 LEFT JOIN sys_role r ON r.id = ur.role_id
		WHERE dtype = 'Admin'
		  AND (r.id != #{roleId} OR r.id IS NULL)
		  AND u.id NOT IN (SELECT u.id FROM sys_user u INNER JOIN sys_user_role ur ON u.id = ur.user_id AND ur.role_id = #{roleId})
		<if test="username != null and username != ''">
			AND u.username like concat('%', #{username}, '%')
		</if>
		<if test="mobile != null and mobile != ''">
			AND u.mobile like concat('%', #{mobile}, '%')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUserByUsername" resultMap="UserResult">
	    <include refid="selectUserVo"/>
		WHERE dtype = #{type.value}
		  AND u.username = #{username}
	</select>

	<select id="selectUserByOpenid" parameterType="String" resultMap="UserResult">
		<include refid="selectUserVo"/>
		WHERE u.openid = #{openid}
	</select>

	<select id="selectUserByMobile" parameterType="String" resultMap="UserResult">
		<include refid="selectUserVo"/>
		WHERE u.mobile = #{mobile}
	</select>

	<select id="selectUserByEmail" parameterType="String" resultMap="UserResult">
	    <include refid="selectUserVo"/>
		WHERE u.email = #{email}
	</select>

	<select id="selectUserById" parameterType="Long" resultMap="UserResult">
		<include refid="selectUserVo"/>
      WHERE u.id = #{id}
	</select>

	<select id="checkUsernameUnique" parameterType="String" resultType="com.ics.system.domain.User">
		SELECT id, username FROM sys_user WHERE username=#{username}
	</select>

	<select id="checkNicknameUnique" parameterType="String" resultType="com.ics.system.domain.User">
		SELECT id, nickname FROM sys_user WHERE nickname=#{nickname}
	</select>

	<select id="checkPhoneUnique" parameterType="String" resultType="com.ics.system.domain.User">
		SELECT id, mobile FROM sys_user WHERE mobile=#{mobile}
	</select>

	<select id="checkEmailUnique" parameterType="String" resultType="com.ics.system.domain.User">
		SELECT id, email FROM sys_user WHERE email=#{email}
	</select>

	<select id="selectUserIdsHasRoles" parameterType="Long" resultType="Long">
		SELECT DISTINCT
			u.id
		FROM sys_user u
			LEFT JOIN sys_user_role ur ON u.id = ur.user_id
		WHERE ur.role_id IN
		<foreach collection="array" item="roleId" open="(" separator="," close=")">
			#{roleId}
		</foreach>
	</select>

	<select id="selectUserIdsInDepts" parameterType="Long" resultType="Long">
		SELECT DISTINCT
		    u.id
		FROM sys_user u
			LEFT JOIN sys_dept d ON u.dept_id = d.id
		WHERE d.id IN
		<foreach collection="array" item="deptId" open="(" separator="," close=")">
			#{deptId}
		</foreach>
	</select>
	<select id="selectUserByCustomer"  resultType="com.ics.system.domain.User">
		select u.username,
                u.nickname,
                u.email,
                u.mobile,
                u.status,
                u.staff_id,
                u.customer_id
                from sys_user u
		      where  u.customer_id = #{id}
	</select>
	<select id="getStaffListByUser" resultType="java.lang.Long">
		SELECT staff_id FROM sys_user where staff_id is not null GROUP BY staff_id
	</select>
    <select id="selectCustomerStaffById" resultType="com.ics.common.core.domain.IcsCustomerStaff">
		SELECT id, username, mobile, create_by, create_time, update_by,name,photo,address,email,degree,urgent, update_time, delete_flag, ics_customer_id, openid, avatar, gender, status, park_id,card_no, visit_time,
			   leave_time,visit_content,to_name,to_phone,to_customer,to_customer_id,data_type
		FROM ics_customer_staff
		where id = #{customerId}
	</select>
    <select id="selectUserByStaffId" resultType="com.ics.system.domain.User">
		select u.username,
			   u.nickname,
			   u.email,
			   u.mobile,
			   u.status,
			   u.staff_id,
			   u.customer_id
		from sys_user u
		where  u.staff_id = #{userId}
	</select>


    <delete id="deleteUserById" parameterType="Long">
 		DELETE FROM sys_user WHERE id = #{id}
 	</delete>

 	<delete id="deleteUserByIds" parameterType="Long">
		DELETE FROM sys_user WHERE id IN
 		<foreach collection="array" item="id" open="(" separator="," close=")">
 			#{id}
        </foreach>
 	</delete>

 	<insert id="insertUser" parameterType="com.ics.system.domain.User" useGeneratedKeys="true" keyProperty="id">
 		INSERT INTO sys_user(
 		    dtype,
 			<if test="id != null and id != 0">id,</if>
 			<if test="deptId != null and deptId != 0">dept_id,</if>
		    <if test="parkId != null">park_id,</if>
			<if test="tenantId != null and tenantId != ''">tenant_id,</if>
 			<if test="username != null and username != ''">username,</if>
 			<if test="nickname != null and nickname != ''">nickname,</if>
			<if test="openid != null and openid != ''">openid,</if>
			<if test="avatar != null ">avatar,</if>
 			<if test="email != null and email != ''">email,</if>
 			<if test="mobile != null and mobile != ''">mobile,</if>
 			<if test="gender != null and gender != ''">gender,</if>
 			<if test="password != null and password != ''">password,</if>
 			<if test="customerId != null and customerId != ''">customer_id,</if>
 			<if test="salt != null and salt != ''">salt,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="staffId != null and staffId != ''">staff_id,</if>
 		    version,
            delete_flag,
 			create_time
 		)VALUES(
 		    'Admin',
 			<if test="id != null and id != ''">#{id},</if>
 			<if test="deptId != null and deptId != ''">#{deptId},</if>
		    <if test="parkId != null">#{parkId},</if>
			<if test="tenantId != null">#{tenantId},</if>
 			<if test="username != null and username != ''">#{username},</if>
 			<if test="nickname != null and nickname != ''">#{nickname},</if>
			<if test="openid != null and openid != ''">#{openid},</if>
			<if test="avatar != null and avatar != ''">#{avatar},</if>
 			<if test="email != null and email != ''">#{email},</if>
 			<if test="mobile != null and mobile != ''">#{mobile},</if>
 			<if test="gender != null and gender != ''">#{gender},</if>
 			<if test="password != null and password != ''">#{password},</if>
			<if test="customerId != null and customerId != ''">#{customerId},</if>
 			<if test="salt != null and salt != ''">#{salt},</if>
 			<if test="status != null and status != ''">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="staffId != null and staffId != ''">#{staffId},</if>
 		    0,
 		    0,
 			sysdate()
 		)
	</insert>

	<insert id="insertAppUser" parameterType="com.ics.system.domain.User" useGeneratedKeys="true" keyProperty="id">
		INSERT INTO sys_user(
		dtype,
		<if test="id != null and id != 0">id,</if>
		<if test="deptId != null and deptId != 0">dept_id,</if>
		<if test="parkId != null">park_id,</if>
		<if test="tenantId != null and tenantId != ''">tenant_id,</if>
		<if test="username != null and username != ''">username,</if>
		<if test="nickname != null and nickname != ''">nickname,</if>
		<if test="openid != null and openid != ''">openid,</if>
		<if test="avatar != null ">avatar,</if>
		<if test="email != null and email != ''">email,</if>
		<if test="customerId != null and customerId != ''">customer_id,</if>
		<if test="mobile != null and mobile != ''">mobile,</if>
		<if test="gender != null and gender != ''">gender,</if>
		<if test="password != null and password != ''">password,</if>
		<if test="salt != null and salt != ''">salt,</if>
		<if test="status != null and status != ''">status,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		<if test="staffId != null and staffId != ''">staff_id,</if>
		version,
		delete_flag,
		create_time
		)VALUES(
		'Member',
		<if test="id != null and id != ''">#{id},</if>
		<if test="deptId != null and deptId != ''">#{deptId},</if>
		<if test="parkId != null">#{parkId},</if>
		<if test="tenantId != null">#{tenantId},</if>
		<if test="username != null and username != ''">#{username},</if>
		<if test="nickname != null and nickname != ''">#{nickname},</if>
		<if test="openid != null and openid != ''">#{openid},</if>
		<if test="avatar != null and avatar != ''">#{avatar},</if>
		<if test="email != null and email != ''">#{email},</if>
		<if test="mobile != null and mobile != ''">#{mobile},</if>
		<if test="customerId != null and customerId != ''">#{customerId},</if>
		<if test="gender != null and gender != ''">#{gender},</if>
		<if test="password != null and password != ''">#{password},</if>
		<if test="salt != null and salt != ''">#{salt},</if>
		<if test="status != null and status != ''">#{status},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		<if test="staffId != null and staffId != ''">#{staffId},</if>
		0,
		0,
		sysdate()
		)
	</insert>

	<update id="updateUser" parameterType="com.ics.system.domain.User">
		UPDATE sys_user
		<set>
			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
			<if test="parkId != null">park_id = #{parkId},</if>
			<if test="tenantId != null">tenant_id = #{tenantId},</if>
			<if test="username != null and username != ''">username = #{username},</if>
			<if test="nickname != null and nickname != ''">nickname = #{nickname},</if>
			<if test="openid != null and openid != ''">openid = #{openid},</if>
			<if test="email != null and email != ''">email = #{email},</if>
			<if test="mobile != null and mobile != ''">mobile = #{mobile},</if>
			<if test="gender != null and gender != ''">gender = #{gender},</if>
			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
			<if test="password != null and password != ''">password = #{password},</if>
			<if test="salt != null and salt != ''">salt = #{salt},</if>
			<if test="status != null and status != ''">status = #{status},</if>
			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
			<if test="customerId != null and customerId != ''">customer_id = #{customerId},</if>
			<if test="loginDate != null">login_date = #{loginDate},</if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
			<if test="staffId != null">staff_id = #{staffId},</if>
			update_time = sysdate()
		</set>
		WHERE id = #{id}
	</update>

</mapper>