<?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.workresignation.dao.WorkResignationDao">
    
	<sql id="workResignationColumns">
		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.process_instance_id AS "processInstanceId",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		a.staff_info_id AS "workStaffBasicInfo.id",
		a.type AS "type",
		a.date AS "date",
		a.reason AS "reason",
		si.no AS "workStaffBasicInfo.no",
		si.name AS "workStaffBasicInfo.name",
		si.status AS "workStaffBasicInfo.status",
		si.id_card AS "workStaffBasicInfo.idCard",
		office.top_company AS "workStaffBasicInfo.office.name",
		office.id AS "workStaffBasicInfo.office.id",
		si.job_grade AS "workStaffBasicInfo.jobGrade",
		si.entry_date AS "workStaffBasicInfo.entryDate",
		j.name AS "jobGrade",
		j.pay  AS "pay",
		a.end_date AS "endDate"
	</sql>
	
	<sql id="workResignationJoins">
		LEFT join work_staff_achives si on si.id = a.staff_info_id
		LEFT JOIN sys_office office ON office.id = si.office_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left join work_job_grade j on j.id=si.job_grade
	</sql>
	<update id="updateProcessInstanceId">
		UPDATE work_resignation SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkResignation">
		SELECT
		<include refid="workResignationColumns"/>
		FROM work_resignation a
		<include refid="workResignationJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
    
	<select id="get" resultType="WorkResignation" >
		SELECT 
			<include refid="workResignationColumns"/>
		FROM work_resignation a
		<include refid="workResignationJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkResignation" >
		SELECT 
			<include refid="workResignationColumns"/>
		FROM work_resignation a
		<include refid="workResignationJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="reason != null and reason != ''">
				AND a.reason = #{reason}
			</if>
			<if test="jobGrade != null and jobGrade != ''">
				AND j.name = #{jobGrade}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="beginContractDates != null and endContractDates != null and beginContractDates != '' and endContractDates != ''">
				AND a.endDate BETWEEN #{beginContractDates} AND #{endContractDates}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.no != null and workStaffBasicInfo.no != ''">
				AND si.no = #{workStaffBasicInfo.no}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.status != null and workStaffBasicInfo.status != ''">
				AND si.status = #{workStaffBasicInfo.status}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.idCard != null and workStaffBasicInfo.idCard != ''">
				AND si.id_card = #{workStaffBasicInfo.idCard}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.name != null and workStaffBasicInfo.name != ''">
				AND si.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.name},'%')</if>
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.office != null
			and workStaffBasicInfo.office.name != null and workStaffBasicInfo.office.name != ''">
				AND office.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.office.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.office.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.office.name},'%')</if>
			</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.date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findLists" resultType="WorkResignation" >
		SELECT
		<include refid="workResignationColumns"/>
		FROM work_resignation a
		<include refid="workResignationJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="reason != null and reason != ''">
				AND a.reason = #{reason}
			</if>
			<if test="jobGrade != null and jobGrade != ''">
				AND j.name = #{jobGrade}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="beginContractDates != null and endContractDates != null and beginContractDates != '' and endContractDates != ''">
				AND a.endDate BETWEEN #{beginContractDates} AND #{endContractDates}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.no != null and workStaffBasicInfo.no != ''">
				AND si.no = #{workStaffBasicInfo.no}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.status != null and workStaffBasicInfo.status != ''">
				AND si.status = #{workStaffBasicInfo.status}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.idCard != null and workStaffBasicInfo.idCard != ''">
				AND si.id_card = #{workStaffBasicInfo.idCard}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.name != null and workStaffBasicInfo.name != ''">
				AND si.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.name},'%')</if>
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.office != null
			and workStaffBasicInfo.office.name != null and workStaffBasicInfo.office.name != ''">
				AND office.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workStaffBasicInfo.office.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workStaffBasicInfo.office.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workStaffBasicInfo.office.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.date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkResignation" >
		SELECT 
			<include refid="workResignationColumns"/>
		FROM work_resignation a
		<include refid="workResignationJoins"/>
		<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_resignation(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			company_id,
			office_id,
			status,
			staff_info_id,
			type,
			date,
			reason,
			end_date
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{companyId},
			#{officeId},
			#{status},
			#{workStaffBasicInfo.id},
			#{type},
			#{date},
			#{reason},
			#{endDate}
		)
	</insert>
	
	<update id="update">
		UPDATE work_resignation SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			company_id = #{companyId},
			office_id = #{officeId},
			status = #{status},
			staff_info_id = #{workStaffBasicInfo.id},
			type = #{type},
			date = #{date},
			reason = #{reason},
			end_date = #{endDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_resignation
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_resignation SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkResignation" statementType="STATEMENT">
		select * FROM work_resignation  where ${propertyName} = '${value}'
	</select>
	
</mapper>