<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.WorkOverDAO">
	<resultMap id="WorkOverMap" type="com.jpsoft.shinestar.modules.base.entity.WorkOver">
		<id property="id" column="id_" />
			<result property="personPopedomId" column="person_popedom_id" />
			<result property="startTime" column="start_time" />
			<result property="endTime" column="end_time" />
			<result property="type" column="type_"/>
			<result property="status" column="status_" />
			<result property="hours" column="hours_"/>
			<result property="procinstId" column="procinst_id"/>
			<result property="approvalPersonPopedomId" column="approval_person_popedom_id" />
			<result property="reason" column="reason_" />
			<result property="approvalNo" column="approval_no" />
			<result property="approvalTime" column="approval_time" />
			<result property="approvalContent" column="approval_content" />
			<result property="approvalPersonIdStr" column="approval_person_id_str" />
			<result property="approvalStatusStr" column="approval_status_str" />
			<result property="approvalTimeStr" column="approval_time_str" />
			<result property="approvalContentStr" column="approval_content_str" />
			<result property="createBy" column="create_by" />
			<result property="createTime" column="create_time" />
			<result property="updateBy" column="update_by" />
			<result property="updateTime" column="update_time" />
			<result property="delFlag" column="del_flag" />
			<result property="remark" column="remark_" />
			<result property="companyName" column="company_name" />
			<result property="personName" column="person_name" />
			<result property="approvalPersonName" column="approval_person_name" />
			</resultMap>
	<insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.WorkOver">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into base_work_over
	    (id_,person_popedom_id,start_time,end_time,hours_,procinst_id,type_,status_,approval_person_popedom_id,reason_,approval_no,approval_time,approval_content,approval_person_id_str,approval_status_str,approval_time_str,approval_content_str,create_by,create_time,update_by,update_time,del_flag,remark_)
		values
		(
#{id,jdbcType=VARCHAR}
,#{personPopedomId,jdbcType=VARCHAR}
,#{startTime,jdbcType= TIMESTAMP }
,#{endTime,jdbcType= TIMESTAMP }
,#{hours,jdbcType=INTEGER}
,#{procinstId,jdbcType=VARCHAR}
,#{type,jdbcType=VARCHAR}
,#{status,jdbcType=VARCHAR}
,#{approvalPersonPopedomId,jdbcType=VARCHAR}
,#{reason,jdbcType=VARCHAR}
,#{approvalNo,jdbcType=VARCHAR}
,#{approvalTime,jdbcType= TIMESTAMP }
,#{approvalContent,jdbcType=VARCHAR}
,#{approvalPersonIdStr,jdbcType=VARCHAR}
,#{approvalStatusStr,jdbcType=VARCHAR}
,#{approvalTimeStr,jdbcType=VARCHAR}
,#{approvalContentStr,jdbcType=VARCHAR}
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
,#{remark,jdbcType=VARCHAR}
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from base_work_over where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.WorkOver">
		update base_work_over
		<set>
				<if test="personPopedomId!=null">
		person_popedom_id=#{personPopedomId,jdbcType=VARCHAR},
		</if>
				<if test="startTime!=null">
		start_time=#{startTime,jdbcType= TIMESTAMP },
		</if>
				<if test="endTime!=null">
		end_time=#{endTime,jdbcType= TIMESTAMP },
		</if>
			<if test="hours!=null">
				hours_=#{hours,jdbcType=INTEGER },
			</if>
			<if test="procinstId != null">
				procinst_id = #{procinstId,jdbcType=VARCHAR},
			</if>
			<if test="type != null">
				type_ = #{type,jdbcType=VARCHAR},
			</if>
				<if test="status!=null">
		status_=#{status,jdbcType=VARCHAR},
		</if>
				<if test="approvalPersonPopedomId!=null">
		approval_person_popedom_id=#{approvalPersonPopedomId,jdbcType=VARCHAR},
		</if>
				<if test="reason!=null">
		reason_=#{reason,jdbcType=VARCHAR},
		</if>
				<if test="approvalNo!=null">
		approval_no=#{approvalNo,jdbcType=VARCHAR},
		</if>
				<if test="approvalTime!=null">
		approval_time=#{approvalTime,jdbcType= TIMESTAMP },
		</if>
				<if test="approvalContent!=null">
		approval_content=#{approvalContent,jdbcType=VARCHAR},
		</if>
				<if test="approvalPersonIdStr!=null">
		approval_person_id_str=#{approvalPersonIdStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalStatusStr!=null">
		approval_status_str=#{approvalStatusStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalTimeStr!=null">
		approval_time_str=#{approvalTimeStr,jdbcType=VARCHAR},
		</if>
				<if test="approvalContentStr!=null">
		approval_content_str=#{approvalContentStr,jdbcType=VARCHAR},
		</if>
				<if test="createBy!=null">
		create_by=#{createBy,jdbcType=VARCHAR},
		</if>
				<if test="createTime!=null">
		create_time=#{createTime,jdbcType= TIMESTAMP },
		</if>
				<if test="updateBy!=null">
		update_by=#{updateBy,jdbcType=VARCHAR},
		</if>
				<if test="updateTime!=null">
		update_time=#{updateTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</if>
				<if test="remark!=null">
		remark_=#{remark,jdbcType=VARCHAR},
		</if>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="WorkOverMap">
		select * from base_work_over where id_=#{0} and del_flag = 0
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from base_work_over where id_=#{0}
	</select>
	<select id="list" resultMap="WorkOverMap">
		select * from base_work_over
	</select>
	<select id="search" parameterType="hashmap" resultMap="WorkOverMap">
		<![CDATA[
			SELECT
				a.* ,
				g.name_ as company_name,
				c.name_ as person_name,
				f.name_ as approval_person_name
			FROM
				base_work_over a
				left join base_person_popedom b on a.person_popedom_id = b.id_
				left join base_person_info c on b.person_id = c.id_
				left join base_person_popedom e on a.approval_person_popedom_id = e.id_
				left join base_person_info f on e.person_id = f.id_
				left join base_company_info g on c.company_id = g.id_
		]]>
		<where>
			and a.del_flag = 0
			<if test="searchParams.id != null">
				and a.ID_ like #{searchParams.id}
			</if>
			<if test='searchParams.companyId!=null'>
				and g.id_ = #{searchParams.companyId}
			</if>
			<if test='searchParams.personName!=null'>
				and c.name_ like #{searchParams.personName}
			</if>
			<if test='searchParams.jobNumber!=null'>
				and c.job_number like #{searchParams.jobNumber}
			</if>
			<if test='searchParams.status!=null'>
				and a.status_=#{searchParams.status}
			</if>
			<if test='searchParams.startTime!=null'>
				<![CDATA[
                    and a.create_time >= #{searchParams.startTime}
                ]]>
			</if>
			<if test='searchParams.endTime!=null'>
				<![CDATA[
                    and a.create_time <= #{searchParams.endTime}
                ]]>
			</if>
			<if test='searchParams.auditStartTime!=null'>
				<![CDATA[
                    and b.update_time >= #{searchParams.auditStartTime}
                ]]>
			</if>
			<if test='searchParams.requestEndTime!=null'>
				<![CDATA[
                    and a.update_time <= #{searchParams.requestEndTime}
                ]]>
				and a.id_ = #{searchParams.id}
			</if>
			<if test="searchParams.approvalPersonPopedomId != null">
				and a.approval_person_popedom_id = #{searchParams.approvalPersonPopedomId}
			</if>
			<if test="searchParams.personPopedomId != null">
				and a.person_popedom_id = #{searchParams.personPopedomId}
			</if>
			<if test="searchParams.type != null">
				and a.type_ = #{searchParams.type}
			</if>
			<if test="searchParams.notStatus != null">
				and a.status_ != #{searchParams.notStatus}
			</if>
			<if test="searchParams.inCompanyInfo != null">
				<foreach collection="searchParams.inCompanyInfo" item="companyInfo"  open="and b.company_id in (" separator="," close=")">
					#{companyInfo.id}
				</foreach>
			</if>
			<if test="searchParams.companyCode != null">
				and g.code_ like #{searchParams.companyCode}
			</if>
			<if test="searchParams.hqCompanyCode != null">
				and g.code_ like #{searchParams.hqCompanyCode}
			</if>
			<if test="searchParams.hqStatus != null">
				and c.hq_status = #{searchParams.hqStatus}
			</if>
			<if test="searchParams.hqStatus == null and searchParams.hqPerson != null">
				<![CDATA[
					and ifnull(c.hq_status,0)<>0
				]]>
			</if>
			<if test="searchParams.hours != null">
				<![CDATA[
					and a.hours_ >= #{searchParams.hours}
				]]>
			</if>
			<if test="searchParams.workOverPersonName != null">
				<![CDATA[
					and a.id_ in (
						select wop.work_over_id from base_work_over_person wop
						inner join base_person_popedom bpp on wop.person_popedom_id = bpp.id_
						inner join base_person_info bpi on bpp.person_id = bpi.id_
						where bpi.name_ like #{searchParams.workOverPersonName}
					)
				]]>
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="findSumHoursByParams" resultType="int">
		SELECT COALESCE
		( sum( a.hours_ ), 0 )
		FROM
			base_work_over a
			LEFT JOIN base_person_popedom b ON a.person_popedom_id = b.id_
		WHERE
		a.del_flag = 0
		<![CDATA[
		AND b.person_id = #{personId}
		and start_time >= #{startDate}
		and start_time <= #{endDate}
		]]>
		<if test="searchParams.status != null">
			AND a.status_ = #{searchParams.status}
		</if>
	</select>

	<select id="findPeopdomId" resultType="String">
		SELECT DISTINCT
			a.person_popedom_id
		FROM
			base_work_over_person a
			LEFT JOIN base_work_over b ON a.work_over_id = b.id_
		WHERE
		a.del_flag = 0
		AND b.del_flag = 0
		AND ( b.status_ = '1' OR b.status_ = '4' )
		<![CDATA[
		and b.start_time >= #{startDate}
		and b.start_time <= #{endDate}
		]]>
	</select>
	<select id="findCountByPersonId" resultType="int">
		SELECT
			count(a.id_)
		FROM
			base_work_over_person a
			LEFT JOIN base_work_over b ON a.work_over_id = b.id_
			LEFT JOIN base_person_popedom c ON a.person_popedom_id = c.id_
		WHERE
			a.del_flag = 0
			AND
			(b.status_ = '1' or b.status_ = '4')
			and c.person_id =#{personId}
		<![CDATA[
			and b.start_time >= #{startDate}
			and b.start_time <= #{endDate}
		]]>
	</select>
	<select id="findTopCreateTimeByPersonId" resultType="Date">
		SELECT
			a.create_time
		FROM
			base_work_over a
			LEFT JOIN base_person_popedom b ON a.person_popedom_id = b.id_
		where a.del_flag = 0
			and b.person_id =#{personId}
		<![CDATA[
			and a.start_time >= #{startDate}
			and a.start_time <= #{endDate}
		]]>
		ORDER BY
			a.create_time DESC
		LIMIT 1
	</select>

	<select id="findTopApprovalTimeByPersonId" resultType="Date">
		SELECT
			a.approval_time
		FROM
			base_work_over a
			LEFT JOIN base_person_popedom b ON a.person_popedom_id = b.id_
		where a.del_flag = 0
			and b.person_id =#{personId}
			<![CDATA[
				and a.start_time >= #{startDate}
				and a.start_time <= #{endDate}
			]]>
		ORDER BY
			a.approval_time DESC
		LIMIT 1
	</select>
</mapper>