<?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.jeeplus.sys.mapper.UserMapper">

	<resultMap id="userResult" type="com.jeeplus.sys.service.dto.UserDTO">
		<id property="id" column="id" />
		<result property="loginName" column="loginName" />
		<result property="password" column="password" />
		<result property="no" column="no" />
		<result property="name" column="name" />
		<result property="email" column="email" />
		<result property="phone" column="phone" />
		<result property="mobile" column="mobile" />
		<result property="photo" column="photo"/>
		<result property="sign" column="sign" />
		<result property="remarks" column="remarks" />
		<result property="loginFlag" column="login_flag"/>
		<result property="otherServiceFlag" column="other_service_flag"/>
		<result property="manageOfficeIds" column="manage_office_ids"/>
        	<result property="loginIp" column="loginIp"/>
		<result property="isAdmin" column="isAdmin"/>
        	<result property="loginDate" column="loginDate"/>
		<result property="companyDTO.id" column="companyDTO.id" />
		<result property="companyDTO.name" column="companyDTO.name" />
		<result property="officeDTO.id" column="officeDTO.id" />
		<result property="officeDTO.name" column="officeDTO.name" />
		<result property="officeDTO.parentIds" column="officeDTO.parentIds" />
		<result property="officeDTO.isPublic" column="officeDTO.isPublic"></result>
		<result property="rosterId" column="roster_id"></result>
		<collection property="roleDTOList" javaType="java.util.List" ofType="com.jeeplus.sys.service.dto.RoleDTO">
			<id property="id" column="roleDTO.id" />
			<result property="name" column="roleDTO.name"/>
			<result property="enName" column="roleDTO.enName"/>
		</collection>
		<collection property="postDTOList" javaType="java.util.List" ofType="com.jeeplus.sys.service.dto.PostDTO">
			<id property="id" column="postDTO.id" />
			<result property="name" column="postDTO.name"/>
		</collection>
		<collection property="certDTOList" ofType="com.jeeplus.sys.service.dto.CertDTO" column="id" select="getCertListByUserId"></collection>
	</resultMap>

	<sql id="userColumns">
    	a.id,
    	a.company_id AS "companyDTO.id",
    	a.office_id AS "officeDTO.id",
    	a.login_name AS "loginName",
		a.is_admin AS "isAdmin",
    	a.password,
    	a.no,
		a.name,
		a.email,
		a.phone,
		a.mobile,
		a.login_ip AS "loginIp",
		a.login_date AS "loginDate",
		a.remarks,
		a.login_flag,
		a.other_service_flag,
		a.photo,
		a.qr_code,
		a.sign,
		a.create_by AS "createBy.id",
		a.create_date,
		a.update_by AS "updateBy.id",
		a.update_date,
		a.manage_office_ids,
    	c.name AS "companyDTO.name",
    	c.parent_id AS "companyDTO.parent.id",
    	c.parent_ids AS "companyDTO.parentIds",
    	o.name AS "officeDTO.name",
    	o.parent_id AS "officeDTO.parent.id",
    	o.parent_ids AS "officeDTO.parentIds",
    	o.is_public AS "officeDTO.isPublic",
    	a.roster_id
    </sql>
	<sql id="Cert_Column_List">
        cert.id,
        cert.create_by,
        cert.create_date,
        cert.update_by,
        cert.update_date,
        cert.del_flag,
        cert.remarks,
        cert.user_id,
        cert.type,
        cert.no,
        cert.authorities,
        cert.issued_date,
        cert.enroll_date,
        cert.expire_date,
        cert.enroll_cert_no,
        cert.profession,
        cert.file_url
    </sql>

	<select id="getCertListByUserId" resultType="com.jeeplus.sys.service.dto.CertDTO">
		select
		<include refid="Cert_Column_List"></include>
		from sys_cert cert
		where cert.del_flag = '0' and cert.user_id = #{id}
	</select>

	<sql id="userOfficeColumns">
    	a.id,
    	a.login_name AS "loginName",
		a.is_admin AS "isAdmin",
    	a.password,
    	a.no,
		a.name,
		a.email,
		a.phone,
		a.mobile,
		a.login_ip AS "loginIp",
		a.login_date AS "loginDate",
		a.remarks,
		a.login_flag,
		a.other_service_flag,
		a.photo,
		a.qr_code,
		a.sign,
		a.create_by AS "createBy.id",
		a.create_date,
		a.update_by AS "updateBy.id",
		a.update_date,
    	o.id AS "parent.id",
    	o.parent_ids AS "parentIds"
    </sql>

    <sql id="userJoins">
		LEFT JOIN sys_office c ON c.id = a.company_id
		LEFT JOIN sys_office o ON o.id = a.office_id
    </sql>



	<!-- 获得用户 -->
	<select id="get" resultMap="userResult">
		SELECT
			<include refid="userColumns"/>,
			r.id AS "roleDTO.id",
			r.name AS "roleDTO.name",
			r.en_name AS "roleDTO.enname",
			p.id AS "postDTO.id",
		    p.name AS "postDTO.name"
		FROM sys_user a
		<include refid="userJoins"/>
		LEFT JOIN sys_user_role ur ON ur.user_id = a.id
		LEFT JOIN sys_role r ON r.id = ur.role_id
		LEFT JOIN sys_user_post up ON up.user_id = a.id
		LEFT JOIN sys_post p ON p.id = up.post_id
		${ew.customSqlSegment}
	</select>

	<select id="getUserIdByLogin" resultType="string">
		select id from sys_user where login_name = #{loginName}
	</select>

	<select id="getUserIdByName" resultType="string">
		select id from sys_user su where su.name = #{name} limit 1
	</select>

	<!-- 分页查询用户信息 -->
	<select id="findList" resultMap="userResult">
		SELECT
		<include refid="userColumns"/>
		FROM sys_user a
		left join sys_cert sc on a.id = sc.user_id and sc.del_flag = '0'
		<include refid="userJoins"/>
			${ew.customSqlSegment}
	</select>


	<!-- 查询角色下的用户 -->
	<select id="findListByRole" resultMap="userResult">
		SELECT
		<include refid="userColumns"/>,
		r.id AS "roleDTO.id",
		r.name AS "roleDTO.name",
		r.en_name AS "roleDTO.enname"
		FROM sys_user a
		<include refid="userJoins"/>
		LEFT JOIN sys_user_role ur ON ur.user_id = a.id
		LEFT JOIN sys_role r ON r.id = ur.role_id
		${ew.customSqlSegment}
	</select>

	<!-- 查询岗位下的用户 -->
	<select id="findListByPostId" resultMap="userResult">
		SELECT
		<include refid="userColumns"/>,
		p.id AS "postDTO.id",
		p.name AS "postDTO.name"
		FROM sys_user a
		<include refid="userJoins"/>
		LEFT JOIN sys_user_post up ON up.user_id = a.id
		LEFT JOIN sys_post p ON p.id = up.post_id
		<where>
			p.id = #{postId} and a.del_flag = 0
		</where>
	</select>


	<!--删除用户角色关联数据-->
	<delete id="deleteUserRole">
		DELETE FROM sys_user_role WHERE user_id = #{userId}
	</delete>

	<!--插入用户角色关联数据-->
	<insert id="insertUserRole">
		INSERT INTO sys_user_role(user_id, role_id) VALUES (#{userId}, #{roleId})
	</insert>

	<!--删除用户岗位关联数据-->
	<delete id="deleteUserPost">
		DELETE FROM sys_user_post WHERE user_id = #{userId}
	</delete>

	<!--插入用户角色关联数据-->
	<insert id="insertUserPost">
		INSERT INTO sys_user_post(user_id, post_id) VALUES (#{userId}, #{postId})
	</insert>

	<!-- 查询岗位下的用户 -->
	<select id="findListByCompanyId" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
		SELECT
		<include refid="userOfficeColumns"/>
		,#{type} as "type"
		FROM sys_user a
		<include refid="userJoins"/>
		<where>
			a.del_flag = 0
		</where>
	</select>

	<!-- 查询岗位下的用户 -->
	<select id="getUserByOffice" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
		SELECT
		<include refid="userOfficeColumns"/>
		,#{type} as "type"
		FROM sys_user a
		<include refid="userJoins"/>
		<where>
			a.del_flag = 0
			<if test="officeIds != null and officeIds.size>0">
				and a.office_id in
				<foreach collection="officeIds" item="officeId" separator="," open="(" close=")">
					#{officeId}
				</foreach>
			</if>
			and a.is_admin IS NULL
		</where>
	</select>

	<!-- 根据部门名称查询部门下所有有效用户信息 -->
	<select id="getUserByOfficeIds" resultType="com.jeeplus.sys.service.dto.UserDTO">
		SELECT
		<include refid="userOfficeColumns"/>
		FROM sys_user a
		<include refid="userJoins"/>
		<where>
			a.del_flag = 0
			<if test="officeIds != null and officeIds.size>0">
				and a.office_id in
				<foreach collection="officeIds" item="officeId" separator="," open="(" close=")">
					#{officeId}
				</foreach>
			</if>
		</where>
	</select>


	<select id="getUserIdByLoginOrMobile" resultType="com.jeeplus.sys.service.dto.UserDTO">
		SELECT
		<include refid="userOfficeColumns"/>
		FROM sys_user a
		<include refid="userJoins"/>
		<where>
			a.del_flag = 0
			and (login_name = #{userName} or mobile = #{userName})
		</where>
	</select>


	<select id="getUserInfoByMobile" resultType="com.jeeplus.sys.service.dto.UserDTO">
		SELECT
		<include refid="userOfficeColumns"/>
		FROM sys_user a
		<include refid="userJoins"/>
		<where>
			a.del_flag = 0
			and a.mobile = #{mobile}
			and a.id != #{id}
		</where>
	</select>


	<select id="getUserInfoByRoleName" resultType="com.jeeplus.sys.service.dto.UserDTO">
		select a.id, a.company_id as "companyDTO.id", a.office_id as "officeDTO.id", a.login_name as "loginName", a.is_admin as "isAdmin", a.password, a.no, a.name, a.email, a.phone, a.mobile, a.login_ip as "loginIp", a.login_date as "loginDate", a.remarks, a.login_flag, a.photo, a.qr_code, a.sign, a.create_by as "createBy.id", a.create_date, a.update_by as "updateBy.id", a.update_date, c.name as "companyDTO.name", c.parent_id as "companyDTO.parent.id", c.parent_ids as "companyDTO.parentIds", o.name as "officeDTO.name", o.parent_id as "officeDTO.parent.id", o.parent_ids as "officeDTO.parentIds", r.id as "roleDTO.id", r.name as "roleDTO.name", r.en_name as "roleDTO.enname"
		from sys_user a
		left join sys_office c
		on c.id = a.company_id
		left join sys_office o
		on o.id = a.office_id
		left join sys_user_role ur
		on ur.user_id = a.id
		left join sys_role r
		on r.id = ur.role_id
		<where>
			r.id = (
			select id from sys_role where
			<if test="roleName != null and roleName != ''">
				name = #{roleName}
			</if>
			and del_flag= 0
			) and a.del_flag = 0
		</where>
		 order by a.id
	</select>


	<select id="getUserInfoByEnName" resultType="com.jeeplus.sys.service.dto.UserDTO">
		select a.id, a.company_id as "companyDTO.id", a.office_id as "officeDTO.id", a.login_name as "loginName", a.is_admin as "isAdmin", a.password, a.no, a.name, a.email, a.phone, a.mobile, a.login_ip as "loginIp", a.login_date as "loginDate", a.remarks, a.login_flag, a.photo, a.qr_code, a.sign, a.create_by as "createBy.id", a.create_date, a.update_by as "updateBy.id", a.update_date, c.name as "companyDTO.name", c.parent_id as "companyDTO.parent.id", c.parent_ids as "companyDTO.parentIds", o.name as "officeDTO.name", o.parent_id as "officeDTO.parent.id", o.parent_ids as "officeDTO.parentIds", r.id as "roleDTO.id", r.name as "roleDTO.name", r.en_name as "roleDTO.enname"
		from sys_user a
		left join sys_office c
		on c.id = a.company_id
		left join sys_office o
		on o.id = a.office_id
		left join sys_user_role ur
		on ur.user_id = a.id
		left join sys_role r
		on r.id = ur.role_id
		<where>
			r.id = (
			select id from sys_role where
			<if test="enName != null and enName != ''">
				en_name = #{enName}
			</if>
			and del_flag= 0
			) and a.del_flag = 0
		</where>
		 order by a.id
	</select>

	<select id="getUserByName" resultType="com.jeeplus.sys.service.dto.UserDTO">
		select a.id, a.company_id as "companyDTO.id", a.office_id as "officeDTO.id", a.login_name as "loginName", a.is_admin as "isAdmin",
		a.password, a.no, a.name, a.email, a.phone, a.mobile, a.login_ip as "loginIp", a.login_date as "loginDate", a.remarks,
		a.login_flag, a.photo, a.qr_code, a.sign, a.create_by as "createBy.id", a.create_date, a.update_by as "updateBy.id",
		a.update_date
		from sys_user a
		where `name` = #{name}
	</select>

	<select id="isUpdatePassword" resultType="string">
		select up_password from sys_user where del_flag = '0' and id = #{userId}
	</select>

	<update id="updateUserUpPassword">
		update sys_user set
		up_password = 1
		where id = #{id}
	</update>

	<delete id="deleteUserManageOffice">
		delete from sys_user_manage_office where user_id = #{id}
	</delete>

	<insert id="insertUserManageOffice">
		insert sys_user_manage_office(user_id, office_id) VALUES (#{userId}, #{officeId})
	</insert>

	<update id="updateUserCompany">
		update sys_user set company_id = #{companyId} where id = #{userId}
	</update>

	<select id="getUserPost" resultType="string">
		select
		sup.post_id
		from sys_user_post sup
		inner join sys_post p on sup.post_id = p.id and p.del_flag = 0
		where sup.user_id = #{userId}
	</select>

	<select id="getUserRole" resultType="string">
		select
		sur.role_id
		from sys_user_role sur
		inner join sys_role r on sur.role_id = r.id and r.del_flag = 0
		where sur.user_id = #{userId}
	</select>

	<update id="updateUserManageOffice">
		update sys_user set manage_office_ids = #{officeIds} where id = #{userId}
	</update>

	<select id="getUserByPost" resultType="com.jeeplus.sys.domain.User">
		select
		distinct u.id,
		u.roster_id
		from sys_user_post sup
		left join sys_user u on u.id = sup.user_id and u.del_flag = 0
		where sup.post_id = #{postId}
	</select>

	<update id="updateUser">
		update sys_user set
		office_id = #{user.officeId},login_name = #{user.loginName},name = #{user.name},mobile = #{user.mobile},password = #{user.password}
		where id = #{user.id}
	</update>

	<insert id="insertUser">
		insert into sys_user(id,office_id,login_name,name,mobile,password)
		values(#{user.id},#{user.officeId},#{user.loginName},#{user.name},#{user.mobile},#{user.password})
	</insert>

	<delete id="removeByIdPhysical">
		delete from sys_user where id = #{id}
	</delete>
</mapper>
