<?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.modules.sysuseroffice.dao.UserofficeDao">
    
	<sql id="userofficeColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.user_id AS "userId",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.master AS "master",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		tuser.id AS  "user.id",
		tuser.name AS  "user.name",
		tuser.photo AS  "user.photo",
		tuser.mobile AS  "user.mobile",
		tuser.email AS  "user.email",
		tuser.login_name AS  "user.loginName",
		o.name AS "user.officeName"
	</sql>
	
	<sql id="userofficeJoins">
        LEFT JOIN sys_user tuser ON tuser.id = a.user_id
        LEFT JOIN sys_office o ON o.id = a.office_id
	</sql>
	
    
	<select id="get" resultType="Useroffice" >
		SELECT 
			<include refid="userofficeColumns"/>
		FROM sys_user_office a
		<include refid="userofficeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="Useroffice" >
		SELECT 
			<include refid="userofficeColumns"/>
		FROM sys_user_office a
		<include refid="userofficeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </if>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListByCompany" resultType="Useroffice" >
		SELECT
		<include refid="userofficeColumns"/>
		FROM sys_user_office a
		<include refid="userofficeJoins"/>
		LEFT JOIN sys_office s ON o.id = a.company_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.company_id = #{companyId}
			AND a.status = '3'
		</where>
	</select>
	
	<select id="findAllList" resultType="Useroffice" >
		SELECT 
			<include refid="userofficeColumns"/>
		FROM sys_user_office a
		<include refid="userofficeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

    <!-- 根据条件检索用户，添加到好友列表 -->
    <select id="searchUsers"  resultType="Useroffice">
        SELECT
        <include refid="userofficeColumns"/>
        FROM sys_user_office a
        <include refid="userofficeJoins"/>
        WHERE a.del_flag = #{DEL_FLAG_NORMAL} and a.status = 3
        <if test="mobile != null and mobile != ''">
            AND tuser.login_name like
            <if test="dbName == 'oracle'">'%'||#{mobile}||'%'</if>
            <if test="dbName == 'mysql'">CONCAT('%', #{mobile}, '%')</if>
        </if>
        <if test="name != null and name != ''">
            AND tuser.name like
            <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
            <if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
        </if>
        <if test="companyIds != null and companyIds != ''">
            AND a.company_id IN
            <foreach item="item" index="index" collection="companyIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <!--AND a.company_id IN (#{companyIds})-->
        <!-- 根据手机号查询 -->
    </select>

	<!-- 根据条件检索用户，添加到好友列表 -->
	<select id="searchUsersGroupByUserId"  resultType="Useroffice">
		SELECT DISTINCT
		a.del_flag AS "delFlag",
		a.user_id AS "userId",
		tuser.id AS  "user.id",
		tuser.name AS  "user.name",
		tuser.photo AS  "user.photo",
		tuser.office_id AS  "user.office.id",
		tuser.company_id AS  "user.company.id",
		o.name AS "user.officeName"
		FROM sys_user_office a
		LEFT JOIN sys_user tuser ON tuser.id = a.user_id
		LEFT JOIN sys_office o ON o.id = tuser.office_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} and a.status = 3
		<if test="mobile != null and mobile != ''">
			AND tuser.login_name like
			<if test="dbName == 'oracle'">'%'||#{mobile}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{mobile}, '%')</if>
		</if>
		<if test="name != null and name != ''">
			AND tuser.name like
			<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
		</if>
		<if test="companyIds != null and companyIds != ''">
			AND a.company_id IN
			<foreach item="item" index="index" collection="companyIds"
					 open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		ORDER BY a.update_date
		<!--AND a.company_id IN (#{companyIds})-->
		<!-- 根据手机号查询 -->
	</select>

	<insert id="insert">
		INSERT INTO sys_user_office(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			user_id,
			company_id,
			office_id,
			master,
			status,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{userId},
			#{companyId},
			#{officeId},
			#{master},
			#{status},
			#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_user_office SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			user_id = #{userId},
			company_id = #{companyId},
			office_id = #{officeId},
			master = #{master},
			status = #{status},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_user_office
		WHERE id = #{id}
	</update>

    <!--物理删除-->
    <update id="deleteByUserId">
        DELETE FROM sys_user_office
        WHERE id = #{id}
    </update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_user_office SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Useroffice" statementType="STATEMENT">
		select * FROM sys_user_office  where ${propertyName} = '${value}'
	</select>

	<update id="updateOffice">
		UPDATE sys_user_office u SET
			u.office_id = #{officeId}
		WHERE u.user_id = #{userId} and u.company_id=#{companyId}
	</update>

	<select id="findOfficeId" resultType="Useroffice" >
		SELECT
		<include refid="userofficeColumns"/>
		FROM sys_user_office a
		<include refid="userofficeJoins"/>
		WHERE a.user_id = #{userId} and a.company_id=#{companyId}
	</select>

    <select id="countByOffice" resultType="int">
        SELECT count(1) FROM sys_user_office a WHERE a.office_id = #{officeId} AND a.del_flag='0'
    </select>
    
    <update id="deleteByCompany">
        UPDATE sys_user_office SET del_flag = '1' WHERE company_id=#{companyId};
    </update>
</mapper>