<?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.flowable.mapper.FlowMapper">

	<update id="updateProcInsIdByBusinessId">
		UPDATE ${businessTable} SET
			proc_ins_id = #{procInsId}
		<if test="procDefId != null and procDefId != ''">
			,process_definition_id = #{procDefId}
		</if>
		WHERE id = #{businessId}
	</update>

	<select id="getActHiIdentitylinkList" resultType="com.jeeplus.flowable.model.ActHiIdentitylink">
		select
		  ID_ as "id",
		  REV_ as "rev",
		  GROUP_ID_ as "groupId",
		  TYPE_ as "type",
		  USER_ID_ as "userId",
		  TASK_ID_ as "taskId",
		  PROC_INST_ID_ as "procInstId",
		  PROC_DEF_ID_ as "procDefId",
		  SCOPE_ID_ as "scopeId",
		  SCOPE_TYPE_ as "scopeType",
		  SCOPE_DEFINITION_ID_ as "scopeDefinitionId"
		from
		  act_ru_identitylink
		where PROC_INST_ID_= #{processInstanceId}
	</select>

	<select id="getTaskInfoByTaskId" resultType="com.jeeplus.flowable.model.ActRuTaskInfo">
		select
		  ID_ as "id",
		  REV_ as "rev",
		  EXECUTION_ID_ as "executionId",
		  PROC_INST_ID_ as "procInstId",
		  PROC_DEF_ID_ as "procDefId",
		  TASK_DEF_ID_ as "taskDefId",
		  SCOPE_ID_ as "scopeId",
		  SUB_SCOPE_ID_ as "subScopeId",
		  SCOPE_TYPE_ as "scopeType",
		  SCOPE_DEFINITION_ID_ as "scopeDefinitionId",
		  NAME_ as "name",
		  PARENT_TASK_ID_ as "parentTaskId",
		  DESCRIPTION_ as "description",
		  TASK_DEF_KEY_ as "taskDefKey",
		  OWNER_ as "owner",
		  ASSIGNEE_ as "assignee",
		  DELEGATION_ as "delegation",
		  PRIORITY_ as "priority",
		  CREATE_TIME_ as "createTime",
		  DUE_DATE_ as "dueDate",
		  CATEGORY_ as "category",
		  SUSPENSION_STATE_ as "suspensionState",
		  TENANT_ID_ as "tenantId",
		  FORM_KEY_ as "formKey",
		  CLAIM_TIME_ as "claimTime",
		  IS_COUNT_ENABLED_ as "isCountEnabled",
		  VAR_COUNT_ as "varCount",
		  ID_LINK_COUNT_ as "idLinkCount",
		  SUB_TASK_COUNT_  as "subTaskCount"
		from
		  act_ru_task
		where ID_= #{taskId}
		order by CREATE_TIME_ desc
		limit 1
	</select>

	<select id="getTaskIdByprocInstId" resultType="java.lang.String">
		select ID_ from act_hi_procinst where PROC_INST_ID_ = #{procInstId}
	</select>

	<select id="getAssigneeDtoByTaskId" resultType="com.jeeplus.flowable.vo.AssigneeDto">
		select
			aea.type,
			aea.assign_value
		from act_ru_task art
		left join act_extension_taskdef aet on art.TASK_DEF_KEY_ = aet.task_def_id and aet.del_flag = 0
		left join act_extension_assignee aea on aet.id = aea.task_def_id and aet.del_flag = 0
		where art.ID_ = #{taskId}
	</select>

	<select id="getStarterByProcInsId" resultType="string">
		select
			ari.USER_ID_
		from act_ru_identitylink ari
		where ari.PROC_INST_ID_ = #{procInsId} and ari.TYPE_ = 'starter'
	</select>

	<select id="getHistoricList" resultType="com.jeeplus.flowable.vo.HisTaskVo">
		SELECT
			<include refid="ACT_HI_TASKINST"></include>,
			var.TEXT_ as title,
			su2.NAME as startUserName,
			hi_id.USER_ID_ as startUserId,
			su.NAME as assigneeName
		FROM
			act_hi_taskinst aht
		LEFT JOIN sys_user su ON su.id = aht.ASSIGNEE_
		LEFT JOIN act_hi_varinst var ON var.PROC_INST_ID_ = aht.PROC_INST_ID_ AND var.NAME_ = 'title'
		LEFT JOIN act_hi_identitylink hi_id ON hi_id.PROC_INST_ID_ = aht.PROC_INST_ID_ AND hi_id.TYPE_ = 'starter'
		LEFT JOIN sys_user su2 ON su2.id = hi_id.USER_ID_
		${ew.customSqlSegment}
		ORDER BY
			aht.END_TIME_ DESC
	</select>
	<sql id="ACT_HI_TASKINST">
        aht.ID_ as ID,
        aht.REV_ as REV,
        aht.PROC_DEF_ID_ as processDefinitionId,
        aht.TASK_DEF_ID_ as TASK_DEF_ID,
        aht.TASK_DEF_KEY_ as taskDefinitionKey,
        aht.PROC_INST_ID_ as processInstanceId,
        aht.EXECUTION_ID_ as EXECUTION_ID,
        aht.SCOPE_ID_ as SCOPE_ID,
        aht.SUB_SCOPE_ID_ as SUB_SCOPE_ID,
        aht.SCOPE_TYPE_ as SCOPE_TYPE,
        aht.SCOPE_DEFINITION_ID_ as SCOPE_DEFINITION_ID,
        aht.NAME_ as `NAME`,
        aht.PARENT_TASK_ID_ as PARENT_TASK_ID,
        aht.DESCRIPTION_ as DESCRIPTION,
        aht.OWNER_ as OWNER,
        aht.ASSIGNEE_ as ASSIGNEE,
        aht.START_TIME_ as createTime,
        aht.CLAIM_TIME_ as CLAIM_TIME,
        aht.END_TIME_ as END_TIME,
        aht.DURATION_ as DURATION,
        aht.DELETE_REASON_ as DELETE_REASON,
        aht.PRIORITY_ as PRIORITY,
        aht.DUE_DATE_ as DUE_DATE,
        aht.FORM_KEY_ as FORM_KEY,
        aht.CATEGORY_ as CATEGORY,
        aht.TENANT_ID_ as TENANT_ID,
        aht.LAST_UPDATED_TIME_ as LAST_UPDATED_TIME
    </sql>
</mapper>
