<?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.workofficechange.dao.WorkOfficeChangeDao">
    
	<sql id="workOfficeChangeColumns">
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.change_office AS "changeOffice",
		a.changer AS "changer",
		a.number AS "number",
		a.change_date AS "changeDate",
		a.change_for AS "changeFor",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId",
		office.top_company as "changeOfficeName",
		us.name as "userName",
		a.role_id as "roleId",
		r.name as "roleName"
	</sql>
	
	<sql id="workOfficeChangeJoins">
		/*LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by*/
		LEFT JOIN sys_user us ON us.id = a.changer
		LEFT JOIN sys_office office ON office.id = a.change_office
		LEFT JOIN sys_role r ON r.id = a.role_id
	</sql>
	
    
	<select id="get" resultType="WorkOfficeChange" >
		SELECT 
			<include refid="workOfficeChangeColumns"/>
		FROM work_office_change a
		<include refid="workOfficeChangeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkOfficeChange" >
		SELECT 
			<include refid="workOfficeChangeColumns"/>
		FROM work_office_change a
		<include refid="workOfficeChangeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="userName != null and userName != ''">
				AND us.name LIKE
					<if test="dbName == 'oracle'">'%'||#{userName}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{userName}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.change_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			${sqlMap.dsf}
		</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="queryCount" resultType="int" >
		SELECT
		count(1)
		FROM work_office_change a
		<include refid="workOfficeChangeJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="userName != null and userName != ''">
				AND us.name LIKE
				<if test="dbName == 'oracle'">'%'||#{userName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{userName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.change_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			${sqlMap.dsf}
		</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="WorkOfficeChange" >
		SELECT 
			<include refid="workOfficeChangeColumns"/>
		FROM work_office_change a
		<include refid="workOfficeChangeJoins"/>
		<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 work_office_change(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			change_office,
			changer,
			number,
			change_date,
			change_for,
			state,
			process_instance_id,
			role_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{changeOffice},
			#{changer},
			#{number},
			#{changeDate},
			#{changeFor},
			#{state},
			#{processInstanceId},
			#{roleId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_office_change SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			change_office = #{changeOffice},
			changer = #{changer},
			number = #{number},
			change_date = #{changeDate},
			change_for = #{changeFor},
			state = #{state},
			<if test="roleId != null and roleId != ''">
			role_id = #{roleId},
			</if>
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_office_change
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_office_change SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkOfficeChange" statementType="STATEMENT">
		select * FROM work_office_change  where ${propertyName} = '${value}'
	</select>
	<update id="updateProcessInstanceId" >
        UPDATE  work_office_change SET
        process_instance_id = #{processInstanceId},
        state = #{state}
        WHERE id = #{id}
    </update>
</mapper>