<?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.workfield.dao.WorkFieldDao">
    
	<sql id="workFieldColumns">
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.num AS "num",
		a.name AS "name",
		a.start_date AS "startDate",
		a.start_type AS "startType",
		a.end_date AS "endDate",
		a.end_type AS "endType",
		a.reason AS "reason",
		a.achives_id AS "workStaffBasicInfo.id",
		sa.no AS "workStaffBasicInfo.no",
		sa.status AS "workStaffBasicInfo.status",
		o.top_company AS "workStaffBasicInfo.office.name",
		jg.name AS "workStaffBasicInfo.jobGrade.name",
		su.name AS "createBy.name",
		a.status AS "status",
		a.days AS "days"
	</sql>
	
	<sql id="workFieldJoins">
		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 su on su.id = a.create_by
		LEFT JOIN work_staff_achives sa on sa.id = a.achives_id
		LEFT JOIN work_job_grade jg ON jg.id = sa.job_grade
	</sql>

	<update id="updateProcessInstanceId">
		UPDATE work_field SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkField">
		SELECT
		<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<select id="get" resultType="WorkField" >
		SELECT 
			<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByStaffIdAndTime" resultType="WorkField" >
		SELECT
		<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		WHERE a.achives_id = #{staffId}
		AND a.status = '5'
		AND #{time} BETWEEN a.start_date AND a.end_date
	</select>
	
	<select id="findList" resultType="WorkField" >
		SELECT 
			<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.no !=null and workStaffBasicInfo.no != ''">
				AND sa.no = #{workStaffBasicInfo.no}
			</if>
			<if test="workStaffBasicInfo != null and workStaffBasicInfo.id !=null and workStaffBasicInfo.id != ''">
				AND a.achives_id = #{workStaffBasicInfo.id}
			</if>
			<if test="num != null and num != ''">
				AND a.num = #{num}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</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.create_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListValidateTime" resultType="WorkField" parameterType="java.lang.String">
		SELECT
		<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		<where>
			a.del_flag = '0'
			AND a.achives_id=#{staffId}
			AND a.status not in('1','3','4')
		</where>
	</select>

	<select id="findAllList" resultType="WorkField" >
		SELECT 
			<include refid="workFieldColumns"/>
		FROM work_field a
		<include refid="workFieldJoins"/>
		<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_field(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			num,
			name,
			start_date,
			start_type,
			end_date,
			end_type,
			reason,
			achives_id,
			status,
			days
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{num},
			#{name},
			#{startDate},
			#{startType},
			#{endDate},
			#{endType},
			#{reason},
			#{workStaffBasicInfo.id},
			#{status},
			#{days}
		)
	</insert>
	
	<update id="update">
		UPDATE work_field SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			num = #{num},
			name = #{name},
			start_date = #{startDate},
			start_type = #{startType},
			end_date = #{endDate},
			end_type = #{endType},
			reason = #{reason},
			achives_id = #{workStaffBasicInfo.id},
			status = #{status},
			days = #{days}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_field
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_field SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkField" statementType="STATEMENT">
		select * FROM work_field  where ${propertyName} = '${value}'
	</select>
	
</mapper>