<?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.WorkOfficeChangeUserDao">
    
	<sql id="workOfficeChangeUserColumns">
		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_id AS "changeId",
		a.user_id AS "userId",
		a.basic_info_id AS "basicInfoId",
		a.oldOfficeName AS "oldOfficeName",
		a.oldOfficeId AS "oldOfficeId",
		o.top_company as "workStaffBasicInfo.office.name",
		b.name as "workStaffBasicInfo.name",
		b.no as "workStaffBasicInfo.no",
		b.id_card as "workStaffBasicInfo.idCard",
		b.status as "workStaffBasicInfo.status",
        a.before_job as "jobGrade1.id",
        a.after_job as "jobGrade2.id",
        j1.name as "jobGrade1.name",
        j2.name as "jobGrade2.name"
	</sql>
	
	<sql id="workOfficeChangeUserJoins">
		LEFT JOIN work_office_change w ON w.id = a.change_id
		LEFT JOIN work_staff_achives b ON b.id = a.basic_info_id
		LEFT JOIN sys_office o ON o.id = b.office_id
		LEFT JOIN work_job_grade j1 ON j1.id = a.before_job
		LEFT JOIN work_job_grade j2 ON j2.id = a.after_job
	</sql>
	
    
	<select id="get" resultType="WorkOfficeChangeUser" >
		SELECT 
			<include refid="workOfficeChangeUserColumns"/>
		FROM work_office_change_user a
		<include refid="workOfficeChangeUserJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkOfficeChangeUser" >
		SELECT 
			<include refid="workOfficeChangeUserColumns"/>
		FROM work_office_change_user a
		<include refid="workOfficeChangeUserJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="changeId != null and changeId != ''">
				AND a.change_id = #{changeId}
			</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="WorkOfficeChangeUser" >
		SELECT 
			<include refid="workOfficeChangeUserColumns"/>
		FROM work_office_change_user a
		<include refid="workOfficeChangeUserJoins"/>
		<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_user(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			change_id,
			user_id,
			basic_info_id,
			before_job,
			after_job,
			oldOfficeName,
			oldOfficeId
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{changeId},
			#{userId},
			#{basicInfoId},
			#{jobGrade1.id},
			#{jobGrade2.id},
			#{oldOfficeName},
			#{oldOfficeId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_office_change_user SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			del_flag = #{delFlag},
			company_id = #{companyId},
			change_id = #{changeId},
			user_id = #{userId},
			basic_info_id = #{basicInfoId},
			before_job = #{jobGrade1.id},
			after_job = #{jobGrade2.id},
			oldOfficeName = #{oldOfficeName},
			oldOfficeId = #{oldOfficeId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_office_change_user
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_office_change_user SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkOfficeChangeUser" statementType="STATEMENT">
		select * FROM work_office_change_user  where ${propertyName} = '${value}'
	</select>
	
</mapper>