<?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.workregularapply.dao.WorkRegularApplyDao">
    
	<sql id="workRegularApplyColumns">
		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.basic_info_id AS "basicInfoId",
		a.user_id AS "userId",
		a.summary AS "summary",
		a.score AS "score",
		a.comment AS "comment",
		a.office_id AS "office.id",
		a.company_id AS "companyId",
		a.apply_date AS "applyDate",
		a.process_instance_id AS "processInstanceId",
		a.state AS "state",
		b.no as "workStaffBasicInfo.no",
		b.name as "workStaffBasicInfo.name",
		b.status as "workStaffBasicInfo.status",
		b.id_card as "workStaffBasicInfo.idCard",
		b.job_grade as "workStaffBasicInfo.jobGrade",
		DATE_FORMAT(b.entry_date, '%Y-%m-%d') as "workStaffBasicInfo.entryDate",
		/*(select DATE_FORMAT(MAX(r.try_end_time), '%Y-%m-%d')
		from work_relationship r
		where r.staff_info_id=a.basic_info_id) "workStaffBasicInfo.tryEndTime",*/
		ifnull(b.try_end_time,(select DATE_FORMAT(MAX(r.try_end_time), '%Y-%m-%d')
		from work_relationship r
		where r.staff_info_id=a.basic_info_id)) as "workStaffBasicInfo.tryEndTime",
		bo.top_company as "workStaffBasicInfo.office.name",
		j.name as jobName,
		j.pay as pay,
		ifnull(a.correct_opinion,1) as "correctOpinion",
		ifnull(g.id,b.job_grade) as "jobGrade.id",
		ifnull(g.name,j.name) as "jobGrade.name",
		a.correct_date as "correctDate"

	</sql>
	
	<sql id="workRegularApplyJoins">
		/*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 work_staff_achives b ON b.id = a.basic_info_id
		LEFT JOIN sys_office bo ON bo.id = b.office_id
		left join work_job_grade j on j.id=b.job_grade
		left join work_job_grade g on g.id=a.job_grade
	</sql>
	
    
	<select id="get" resultType="WorkRegularApply" >
		SELECT 
			<include refid="workRegularApplyColumns"/>
		FROM work_regular_apply a
		<include refid="workRegularApplyJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkRegularApply" >
		SELECT 
			<include refid="workRegularApplyColumns"/>
		FROM work_regular_apply a
		<include refid="workRegularApplyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="userName != null and userName != ''">
				AND b.name like <if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.apply_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
			AND a.state = #{state}
		</if>
			${sqlMap.dsf} OR  a.user_id=#{currentUser.id}
		</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_regular_apply a
		<include refid="workRegularApplyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="userName != null and userName != ''">
				AND b.name like <if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.apply_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			${sqlMap.dsf} OR  a.user_id=#{currentUser.id}
		</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="WorkRegularApply" >
		SELECT 
			<include refid="workRegularApplyColumns"/>
		FROM work_regular_apply a
		<include refid="workRegularApplyJoins"/>
		<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_regular_apply(
			id,
			user_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			basic_info_id,
			score,
			summary,
			comment,
			office_id,
			company_id,
			apply_date,
			process_instance_id,
			state,
			correct_opinion,
			job_grade,
			correct_date
		) VALUES (
			#{id},
			#{userId},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{basicInfoId},
			#{score},
			#{summary},
			#{comment},
			#{officeId},
			#{companyId},
			#{applyDate},
			#{processInstanceId},
			#{state},
			#{correctOpinion},
			#{jobGrade.id},
			#{correctDate}
		)
	</insert>
	
	<update id="update">
		UPDATE work_regular_apply SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			basic_info_id = #{basicInfoId},
			user_id=#{userId},
			score=#{score},
			summary = #{summary},
			comment = #{comment},
			office_id = #{office.id},
			company_id = #{companyId},
			apply_date = #{applyDate},
			process_instance_id = #{processInstanceId},
			state = #{state},
			correct_opinion = #{correctOpinion},
			job_grade = #{jobGrade.id},
			correct_date = #{correctDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_regular_apply
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_regular_apply SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkRegularApply" statementType="STATEMENT">
		select * FROM work_regular_apply  where ${propertyName} = '${value}'
	</select>

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

</mapper>