package com.blueland.bpm.manage.service;
/**
 * @作者     lw
 * @创建时间  2018年10月11日
 * @说明     
 */
public class RecoveryProcessService {

	/**
	 * 流程恢复，恢复至最后一个节点
	 * 场景：正常办结
	 */
	public void endRecovery(String procInstId,String lastTaskId,String lastActId) {
		/*String lastTaskId="593f6340-c1f3-11e8-ad75-005056b6289c";
		String lastActId="593f3c2f-c1f3-11e8-ad75-005056b6289c";
		String procInstId="68e79036-c076-11e8-ad75-005056b6289c";
		*//*select *from ACT_HI_TASKINST a where a.proc_inst_id_ ='9d8ae48d-199b-11e7-a9d2-005056b64a7d' order by start_time_


		select *from Act_Hi_Actinst aa where  aa.task_id_='08549ade-19ca-11e7-a9d2-005056b64a7d' */
		StringBuffer buf =new StringBuffer();
		//---插入执行路径
		String addExeSql="insert into ACT_RU_EXECUTION  (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_,  IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, PARENT_ID_, SUPER_EXEC_, SUSPENSION_STATE_,  CACHED_ENT_STATE_, TENANT_ID_, NAME_,PROC_TITLE,TOP_PROCESS_INSTANCE_ID_)  "
				+ "select EXECUTION_ID_, '1', PROC_INST_ID_, '', PROC_DEF_ID_, TASK_DEF_KEY_, 1, 0, 1, 0, NULL, NULL, 1, 2, tenant_id_, NULL,proc_title,PROC_INST_ID_ from ACT_HI_TASKINST a where a.id_='"+lastTaskId+"'";


		//---插入任务对象


		String addTaskSql="insert into ACT_RU_TASK(ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_,  OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_,  DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_,PROC_TITLE,SENDER, PRE_TASK_DEF_KEY,PRE_TASK_DEF_NAME,PRE_TASK_ID,ACTION_TYPE,SEND_USER_NAME,SENDER_ORG_ID,SENDER_ORG_NAME,ASSIGNEE_USER_NAME,ASSIGNEE_ORG_ID,ASSIGNEE_ORG_NAME,ASSIGNEE_USER_ID,SEND_USER_ID,PROC_DEF_NAME)   "
				+ "select ID_, 1, NAME_, PARENT_TASK_ID_,DESCRIPTION_, PRIORITY_, START_TIME_,  OWNER_, ASSIGNEE_, '', PROC_INST_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_,  DUE_DATE_, CATEGORY_, 1, TENANT_ID_, FORM_KEY_,PROC_TITLE,SENDER, PRE_TASK_DEF_KEY,PRE_TASK_DEF_NAME,PRE_TASK_ID,ACTION_TYPE,SEND_USER_NAME,SENDER_ORG_ID,SENDER_ORG_NAME,ASSIGNEE_USER_NAME,ASSIGNEE_ORG_ID,ASSIGNEE_ORG_NAME,ASSIGNEE_USER_ID,SEND_USER_ID,PROC_DEF_NAME "
				+ "from ACT_HI_TASKINST a where a.id_='"+lastTaskId+"'";

		//--删除最后一步
		String delTaskSql="delete  from act_hi_actinst a where a.proc_inst_id_ ='"+procInstId+"' and act_type_='endEvent'";
		//--修改流程状态
		String updProcSql="update  ACT_HI_PROCINST a set a.end_time_=null,a.end_act_id_=null,proc_state=1 where a.proc_inst_id_ ='"+procInstId+"'";

		//--恢复变量
		String addVarSql="insert into act_ru_variable(id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) "
				+ "select id_, rev_, var_type_, name_, execution_id_, proc_inst_id_, null, bytearray_id_, double_, long_, text_, text2_  "
				+ "from act_hi_varinst a where a.proc_inst_id_ ='"+procInstId+"' and task_id_='"+lastTaskId+"'";

		
		//--同步待办数据
		String upUnieHisSql="update t_unitework_history h set h.unite_status=1,h.unite_deal_time=null where h.unite_cur_actinstid='"+lastTaskId+"'";
/*		--update t_unitework_task h set h.unite_status=1,h.unite_deal_time=null where h.unite_cur_actinstid='"+lastTaskId+"';
		--select *from t_unitework_task where unite_cur_actinstid='"+lastTaskId+"';*/
		String addUnieHisSql="insert into t_unitework_task(unite_id, unite_title, unite_abstract, unite_content, unite_category, unite_rec_userid, unite_rec_username, unite_rec_orgid, unite_rec_orgname, unite_send_userid, unite_send_username, unite_send_orgid, unite_send_orgname, unite_status, unite_url, unite_tenant_id, unite_tenant_name, unite_send_time, unite_deal_time, unite_start_userid, unite_start_orgid, unite_start_username, unite_start_orgname, unite_start_time, unite_top_procinstid, unite_parent_procinstid, unite_procinstid, unite_procdefid, unite_procdefname, unite_cur_actinstid, unite_cur_actdefid, unite_cur_actdefname, unite_prev_actinstid, unite_prev_actdefid, unite_prev_actdefname, unite_service_code, unite_batch_id, unite_data_souce, unite_other_sys_deal_status, unite_procdefkey, unite_cur_actlimittime, unite_esb_messageid) select unite_id, unite_title, unite_abstract, unite_content, unite_category, unite_rec_userid, unite_rec_username, unite_rec_orgid, unite_rec_orgname, unite_send_userid, unite_send_username, unite_send_orgid, unite_send_orgname, unite_status, unite_url, unite_tenant_id, unite_tenant_name, unite_send_time, unite_deal_time, unite_start_userid, unite_start_orgid, unite_start_username, unite_start_orgname, unite_start_time, unite_top_procinstid, unite_parent_procinstid, unite_procinstid, unite_procdefid, unite_procdefname, unite_cur_actinstid, unite_cur_actdefid, unite_cur_actdefname, unite_prev_actinstid, unite_prev_actdefid, unite_prev_actdefname, unite_service_code, unite_batch_id, unite_data_souce, unite_other_sys_deal_status, unite_procdefkey, unite_cur_actlimittime, unite_esb_messageid "
				+ " from t_unitework_history h  where h.unite_cur_actinstid='"+lastTaskId+"'"; 


		//--修改环节时间
		String uptActIstSql="update ACT_HI_ACTINST aat set aat.end_time_=null,aat.duration_=null  "
				+ "where aat.proc_inst_id_ ='"+procInstId+"' and id_='"+lastActId+"'";
		String uptTaskSql="update ACT_HI_TASKINST tt set tt.claim_time_=null,tt.end_time_=null,tt.duration_=null,tt.delete_reason_=null "
				+ " where tt.proc_inst_id_ ='"+procInstId+"'  and id_='"+lastTaskId+"'";


		//--修改意见数据 --select *from ACT_HI_COMMENT where proc_inst_id_ ='9d8ae48d-199b-11e7-a9d2-005056b64a7d'   and task_id_='08549ade-19ca-11e7-a9d2-005056b64a7d';
		String delComSql="delete from  ACT_HI_COMMENT where proc_inst_id_ ='"+procInstId+"'   and task_id_='"+lastTaskId+"'";
		//---插入人员对象
		
		buf.append(addExeSql).append(";\n").append(addTaskSql).append(";\n").append(delTaskSql).append(";\n").append(updProcSql).append(";\n")
		.append(addVarSql).append(";\n").append(upUnieHisSql).append(";\n").append(addUnieHisSql)
		.append(";\n").append(uptActIstSql).append(";\n").append(uptTaskSql).append(";\n").append(delComSql);
		System.out.println(buf.toString());
	}
	
	public static void main(String[] args) {
		String lastTaskId="593f6340-c1f3-11e8-ad75-005056b6289c";
		String lastActId="593f3c2f-c1f3-11e8-ad75-005056b6289c";
		String procInstId="68e79036-c076-11e8-ad75-005056b6289c";
		/*select *from ACT_HI_TASKINST a where a.proc_inst_id_ ='9d8ae48d-199b-11e7-a9d2-005056b64a7d' order by start_time_


		select *from Act_Hi_Actinst aa where  aa.task_id_='08549ade-19ca-11e7-a9d2-005056b64a7d' */
		StringBuffer buf =new StringBuffer();
		//---插入执行路径
		String addExeSql="insert into ACT_RU_EXECUTION  (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_,  IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, PARENT_ID_, SUPER_EXEC_, SUSPENSION_STATE_,  CACHED_ENT_STATE_, TENANT_ID_, NAME_,PROC_TITLE,TOP_PROCESS_INSTANCE_ID_)  "
				+ "select EXECUTION_ID_, '1', PROC_INST_ID_, '', PROC_DEF_ID_, TASK_DEF_KEY_, 1, 0, 1, 0, NULL, NULL, 1, 2, tenant_id_, NULL,proc_title,PROC_INST_ID_ from ACT_HI_TASKINST a where a.id_='"+lastTaskId+"'";


		//---插入任务对象


		String addTaskSql="insert into ACT_RU_TASK(ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_,  OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_,  DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_,PROC_TITLE,SENDER, PRE_TASK_DEF_KEY,PRE_TASK_DEF_NAME,PRE_TASK_ID,ACTION_TYPE,SEND_USER_NAME,SENDER_ORG_ID,SENDER_ORG_NAME,ASSIGNEE_USER_NAME,ASSIGNEE_ORG_ID,ASSIGNEE_ORG_NAME,ASSIGNEE_USER_ID,SEND_USER_ID,PROC_DEF_NAME)   "
				+ "select ID_, 1, NAME_, PARENT_TASK_ID_,DESCRIPTION_, PRIORITY_, START_TIME_,  OWNER_, ASSIGNEE_, '', PROC_INST_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_,  DUE_DATE_, CATEGORY_, 1, TENANT_ID_, FORM_KEY_,PROC_TITLE,SENDER, PRE_TASK_DEF_KEY,PRE_TASK_DEF_NAME,PRE_TASK_ID,ACTION_TYPE,SEND_USER_NAME,SENDER_ORG_ID,SENDER_ORG_NAME,ASSIGNEE_USER_NAME,ASSIGNEE_ORG_ID,ASSIGNEE_ORG_NAME,ASSIGNEE_USER_ID,SEND_USER_ID,PROC_DEF_NAME "
				+ "from ACT_HI_TASKINST a where a.id_='"+lastTaskId+"'";

		//--删除最后一步
		String delTaskSql="delete  from act_hi_actinst a where a.proc_inst_id_ ='"+procInstId+"' and act_type_='endEvent'";
		//--修改流程状态
		String updProcSql="update  ACT_HI_PROCINST a set a.end_time_=null,a.end_act_id_=null,proc_state=1 where a.proc_inst_id_ ='"+procInstId+"'";

		//--恢复变量
		String addVarSql="insert into act_ru_variable(id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) "
				+ "select id_, rev_, var_type_, name_, execution_id_, proc_inst_id_, null, bytearray_id_, double_, long_, text_, text2_  "
				+ "from act_hi_varinst a where a.proc_inst_id_ ='"+procInstId+"' and task_id_='"+lastTaskId+"'";

		
		//--同步待办数据
		String upUnieHisSql="update t_unitework_history h set h.unite_status=1,h.unite_deal_time=null where h.unite_cur_actinstid='"+lastTaskId+"'";
/*		--update t_unitework_task h set h.unite_status=1,h.unite_deal_time=null where h.unite_cur_actinstid='"+lastTaskId+"';
		--select *from t_unitework_task where unite_cur_actinstid='"+lastTaskId+"';*/
		String addUnieHisSql="insert into t_unitework_task(unite_id, unite_title, unite_abstract, unite_content, unite_category, unite_rec_userid, unite_rec_username, unite_rec_orgid, unite_rec_orgname, unite_send_userid, unite_send_username, unite_send_orgid, unite_send_orgname, unite_status, unite_url, unite_tenant_id, unite_tenant_name, unite_send_time, unite_deal_time, unite_start_userid, unite_start_orgid, unite_start_username, unite_start_orgname, unite_start_time, unite_top_procinstid, unite_parent_procinstid, unite_procinstid, unite_procdefid, unite_procdefname, unite_cur_actinstid, unite_cur_actdefid, unite_cur_actdefname, unite_prev_actinstid, unite_prev_actdefid, unite_prev_actdefname, unite_service_code, unite_batch_id, unite_data_souce, unite_other_sys_deal_status, unite_procdefkey, unite_cur_actlimittime, unite_esb_messageid) select unite_id, unite_title, unite_abstract, unite_content, unite_category, unite_rec_userid, unite_rec_username, unite_rec_orgid, unite_rec_orgname, unite_send_userid, unite_send_username, unite_send_orgid, unite_send_orgname, unite_status, unite_url, unite_tenant_id, unite_tenant_name, unite_send_time, unite_deal_time, unite_start_userid, unite_start_orgid, unite_start_username, unite_start_orgname, unite_start_time, unite_top_procinstid, unite_parent_procinstid, unite_procinstid, unite_procdefid, unite_procdefname, unite_cur_actinstid, unite_cur_actdefid, unite_cur_actdefname, unite_prev_actinstid, unite_prev_actdefid, unite_prev_actdefname, unite_service_code, unite_batch_id, unite_data_souce, unite_other_sys_deal_status, unite_procdefkey, unite_cur_actlimittime, unite_esb_messageid "
				+ " from t_unitework_history h  where h.unite_cur_actinstid='"+lastTaskId+"'"; 


		//--修改环节时间
		String uptActIstSql="update ACT_HI_ACTINST aat set aat.end_time_=null,aat.duration_=null  "
				+ "where aat.proc_inst_id_ ='"+procInstId+"' and id_='"+lastActId+"'";
		String uptTaskSql="update ACT_HI_TASKINST tt set tt.claim_time_=null,tt.end_time_=null,tt.duration_=null,tt.delete_reason_=null "
				+ " where tt.proc_inst_id_ ='"+procInstId+"'  and id_='"+lastTaskId+"'";


		//--修改意见数据 --select *from ACT_HI_COMMENT where proc_inst_id_ ='9d8ae48d-199b-11e7-a9d2-005056b64a7d'   and task_id_='08549ade-19ca-11e7-a9d2-005056b64a7d';
		String delComSql="delete from  ACT_HI_COMMENT where proc_inst_id_ ='"+procInstId+"'   and task_id_='"+lastTaskId+"'";
		//---插入人员对象
		
		buf.append(addExeSql).append(";\n").append(addTaskSql).append(";\n").append(delTaskSql).append(";\n").append(updProcSql).append(";\n")
		.append(addVarSql).append(";\n").append(upUnieHisSql).append(";\n").append(addUnieHisSql)
		.append(";\n").append(uptActIstSql).append(";\n").append(uptTaskSql).append(";\n").append(delComSql);
		System.out.println(buf.toString());
	}
}
