<?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.ruoyi.invest.mapper.FlowBaseInfoMapper">
    <resultMap id="baseResultMap" type="com.ruoyi.invest.domain.FlowBaseInfo" >
	    <id column="id" property="id" jdbcType="VARCHAR" />
	    <result column="procDefId" property="procDefId" jdbcType="VARCHAR" />
	    <result column="procInsId" property="procInsId" jdbcType="VARCHAR" />
	    <result column="taskDefKey" property="taskDefKey" jdbcType="VARCHAR" />
	    <result column="taskName" property="taskName" jdbcType="VARCHAR" />
	    <result column="taskId" property="taskId" jdbcType="VARCHAR" />
	    <result column="flag" property="flag" jdbcType="BIT" />
	    <result column="comment" property="comment" jdbcType="VARCHAR" />
	    <result column="createBy" property="createBy" jdbcType="VARCHAR" />
	    <result column="createDate" property="createDate" jdbcType="VARCHAR" />
	    <result column="formProperties" property="formProperties" jdbcType="VARCHAR" />
	    <result column="status" property="status" jdbcType="VARCHAR" />
	</resultMap>
    
    <sql id="actBaseColumns">
		a.id, 
		a.procDefId, 
		a.procInsId, 
		a.taskDefKey, 
		a.taskName, 
		a.taskId, 
		a.flag, 
		a.comment, 
		a.createBy, 
		date_format(a.createDate, '%Y-%m-%d %H:%i:%S') as createDate, 
		a.formProperties,
		a.status
	</sql>
	
	<resultMap id="ActUserResultMap" type="com.ruoyi.invest.domain.FlowBaseInfo" extends="baseResultMap">
	  <result column="headImage" property="headImage" jdbcType="VARCHAR" />  
  	</resultMap>  
	
    <select id="selectByProcInsId" resultMap="ActUserResultMap" >
		SELECT
		    a.id,
			a.procDefId,
			a.procInsId,
			a.taskDefKey,
			a.taskName,
			a.taskId,
			a.flag,
			a.comment,
			a.createBy as createById,
			u.nick_name as createBy,
			date_format(a.createDate, '%Y-%m-%d %H:%i:%S') as createDate,
			a.formProperties,
			a.status
		FROM flow_base_info a
		left join sys_user u on a.createBy = u.user_id
		WHERE a.procInsId = #{procInsId} order by a.createDate asc
	</select>

	<select id="selectByProcInsIdAndTaskDefKey" resultMap="ActUserResultMap" >
		SELECT
			a.id,
			a.procDefId,
			a.procInsId,
			a.taskDefKey,
			a.taskName,
			a.taskId,
			a.flag,
			a.comment,
			a.createBy,
			u.userName as createBy,
			date_format(a.createDate, '%Y-%m-%d %H:%i:%S') as createDate,
			a.formProperties,
			a.status,
			u.headImage
		FROM flow_base_info a left join user_info u on a.createBy = u.loginId
		WHERE a.procInsId = #{procInsId} and a.taskDefKey = #{taskDefKey}
	</select>

	<select id="selectLasterByProcInsId" resultMap="ActUserResultMap" >
		SELECT
			a.id,
			a.procDefId,
			a.procInsId,
			a.taskDefKey,
			a.taskName,
			a.taskId,
			a.flag,
			a.comment,
			a.createBy,
			date_format(a.createDate, '%Y-%m-%d %H:%i:%S') as createDate,
			a.formProperties,
			a.status
		FROM flow_base_info a
		WHERE a.procInsId = #{procInsId} order by a.createDate desc limit 0,1
	</select>
	
	<insert id="insert">
		INSERT INTO flow_base_info(
			id, 
			procDefId, 
			procInsId, 
			taskDefKey, 
			taskName, 
			taskId, 
			flag, 
			comment, 
			createBy, 
			createDate, 
			formProperties,
			status
		) VALUES (
			#{id}, 
			#{procDefId}, 
			#{procInsId}, 
			#{taskDefKey}, 
			#{taskName}, 
			#{taskId}, 
			#{flag}, 
			#{comment}, 
			#{createBy}, 
			now(), 
			#{formProperties},
			#{status}
		)
	</insert>
	
	<delete id="delete">
		delete from flow_base_info where procInsId=#{procInsId}
	</delete>
	
	<delete id="deleteByProAndtaskDefkey">
			  delete 
				from 
				flow_base_info 
				where 
				id in (
			 	 	select i.id id  from (
						select id from flow_base_info
						where 
						procInsId=#{actBaseInfo.procInsId} 
						and taskDefKey=#{actBaseInfo.taskDefKey}
						ORDER BY createDate DESC
						LIMIT 1
					) i
				)	
	</delete>
	
	<select id="checkReStatus" resultType="java.lang.Integer">
			select
			 CASE
 			 WHEN ac.taskDefKey is null THEN
     		 '0'
   			 WHEN ac.taskDefKey = 'startevent1' THEN
    		  '0'
   			 WHEN ac.taskDefKey = 'modifyApply' THEN
      		  '0'
   			 ELSE
     		 '1'
			END  restatus
			from flow_base_info ac 
			where ac.procInsId=#{actBaseInfo.procInsId} 
			ORDER BY createDate DESC
			LIMIT 1
	</select>
	<select id="selectCurrentApproval" resultType="com.ruoyi.invest.domain.FlowBaseInfo">
		SELECT
         id,
         t.PROC_DEF_ID_ AS procDefId,
         t.PROC_INST_ID_ AS procInsId,
         t.TASK_DEF_KEY_ AS taskDefKey,
         t.NAME_ AS taskName,
         t.ID_ AS taskId,
			  u.userName as createBy,
			date_format(t.CREATE_TIME_, '%Y-%m-%d %H:%i:%S') as createDate,
			u.headImage
    FROM ACT_RU_TASK t
    left join user_info u on t.ASSIGNEE_ = u.loginId
   WHERE t.PROC_INST_ID_=#{procInsId}
	</select>


	<select id="searchFlowBaseInfoByProcInsIdAndTaskName" resultMap="ActUserResultMap" >
		SELECT
			a.id,
			a.procDefId,
			a.procInsId,
			a.taskDefKey,
			a.taskName,
			a.taskId,
			a.flag,
			a.comment,
			a.createBy,
			date_format(a.createDate, '%Y-%m-%d %H:%i:%S') as createDate,
			a.formProperties,
			a.status
		FROM flow_base_info a
		WHERE a.procInsId = #{procInsId} and a.taskName like CONCAT('%',#{taskName},'%')
	</select>
	<select id="queryByFinance" resultType="java.lang.String">
		 SELECT createBy FROM flow_base_info
		 where procInsId=#{procInsId} and taskDefKey='finance'
		 ORDER BY createDate desc
		 LIMIT 1
	</select>
</mapper>