<?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.workcompanyinfo.dao.CompanyinfoDao">

	<sql id="companyinfoColumns">
		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.master AS "master",
		a.user_id AS "userId",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
	</sql>

	<sql id="companyinfoColumnss">
		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.master AS "master",
		a.user_id AS "userId",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		b.name AS "name",
		c.name AS "name1",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
	</sql>

	<sql id="companyinfoJoins">
		JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = u.office_id
	</sql>

    <update id="updateMaster">
		UPDATE sys_user_office
		SET master = #{masterId}
		WHERE id = #{companyinfoId}
	</update>


	<select id="get" resultType="Useroffice" >
		SELECT 
			<include refid="companyinfoColumns"/>
		FROM sys_user_office a
		<include refid="companyinfoJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="getByCompanyAndUserId" resultType="Useroffice" >
		SELECT
			<include refid="companyinfoColumns"/>
		FROM sys_user_office a
		<include refid="companyinfoJoins"/>
		WHERE a.user_id = #{userId} and a.company_id = #{companyId} and a.status not in ('4','5') AND a.del_flag = '0'
	</select>

	<select id="getSimple" resultType="Useroffice">
		SELECT
			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.master AS "master",
			a.user_id AS "userId",
			a.company_id AS "companyId",
			a.status AS "status",
			a.process_instance_id AS "processInstanceId",
			u.name AS "createBy.name",
			o.id AS "createBy.office.id",
			o.name AS "createBy.office.name"
		FROM sys_user_office a
		    JOIN sys_user u    ON u.id = a.create_by
			JOIN sys_office o  ON o.id = a.company_id
		WHERE a.id = #{id}
	</select>

    <select id="getByProcessInstanceId" resultType="Useroffice">
        SELECT
        <include refid="companyinfoColumns"/>
        FROM sys_user_office a
        <include refid="companyinfoJoins"/>
        WHERE a.process_instance_id = #{processInstanceId}
    </select>
	
	<select id="findList" resultType="Useroffice" >
		SELECT
			<include refid="companyinfoColumnss"/>,
		m.name AS "masterName",
		so.name AS "office.name",
		so.id AS "office.id"
		FROM sys_user_office a
		JOIN sys_office b ON a.company_id = b.id
		JOIN sys_user c ON a.user_id = c.id
		JOIN sys_office so ON c.office_id = so.id
		LEFT JOIN sys_user m
		ON a.master = m.id
		<include refid="companyinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </if>
            <if test="office != null and office.id != null and office.id != ''">
                AND c.office_id = #{office.id}
            </if>
			<if test="name != null and name != ''">
				AND c.name LIKE
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</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="companyinfoColumns"/>
        FROM sys_user_office a
        <include refid="companyinfoJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
			<choose>
				<when test="status != null and status != ''">
					AND a.status = #{status}
				</when>
				<otherwise>
					AND a.status not in ('4','5')
				</otherwise>
			</choose>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </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="findAllList" resultType="Useroffice" >
		SELECT 
			<include refid="companyinfoColumns"/>
		FROM sys_user_office a
		<include refid="companyinfoJoins"/>
		<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>
	
	<insert id="insert">
		INSERT INTO sys_user_office(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			master,
			user_id,
			company_id,
			status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{master},
			#{userId},
			#{companyId},
			#{status}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_user_office SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			office_id = #{officeId},
			remarks = #{remarks},
			master = #{master},
			user_id	 = #{userId},
			del_flag = #{delFlag},
			company_id = #{companyId},
			status = #{status}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		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>

    <update id="updateProcessInstanceId">
        UPDATE sys_user_office SET
        process_instance_id = #{processInstanceId}
        WHERE id = #{id}
    </update>
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Useroffice" statementType="STATEMENT">
		select * FROM sys_user_office  where ${propertyName} = '${value}'
	</select>
	
</mapper>