package com.blueland.bpm.unitework;

import java.util.Date;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blueland.bpm.common.util.StringUtils;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.core.model.TaskEntityDataShare;
import com.blueland.bpm.engine.util.WorkFlowException;
import com.blueland.unitework.model.UniteworkTask;
import com.blueland.unitework.service.UniteworkService;

/**
 * 统一待办消息同步监听类-重新分配人员（转办）
 * 
 * @version: 1.0
 * @author lw
 */
public class TaskAssigneTransferEvent extends TaskCommonEvent {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	public static final String UNITEWORK_CATEGORY_BANJIAN = "1";

	@Override
	public void onEntityEvent(ActivitiEvent event) {
		if (!ActivitiEventType.TASK_ASSIGNED_TRANSFER.equals(event.getType())) {
			return;
		}
		
		TaskEntity taskEntity = (TaskEntity) ((ActivitiEntityEvent) event)
				.getEntity();
		if (taskEntity == null) {
			return;
		}
		try {
			UniteworkService uniteworkService = (UniteworkService) com.blueland.bpm.common.util.ApplicationContextHolder
					.getBean("uniteworkService");
			ProcessInstanceModel processInfo = buildProcessInstanceModel(taskEntity);
			ProcessInputModel processInputModel=processInfo.getProcessInputModel();
			//补充历史processInputModel变量的桌面端/手机端协议字段
			try{
				processInfo.getCurrentActivity().setCreateTime(new Date());
				if(StringUtils.isEmpty(processInputModel.getWf_mportalUrl())){
					UniteworkTask unitework=uniteworkService.getByActInstId(taskEntity.getId());
					if(unitework!=null){
						processInputModel.setWf_mportalUrl(unitework.getMportalUrl());
						processInputModel.setWf_mportalProtocol(unitework.getMportalProtocol());
						processInputModel.setWf_otherSysDealStatus(unitework.getOtherSysDealStatus());
						processInputModel.setWf_cportalProtocol(unitework.getCportalProtocol());
					}
				}
			}catch(Exception e){
				logger.error("转派补全统一待办mportalUrl失败！",e);
			}
			UniteworkTask delTask=uniteworkService.deleteTask(taskEntity.getId());
			if(isSendESB(taskEntity)&&!delTask.getBatchFlag()){
				TaskEntityDataShare.setHisTask(delTask);
			}
			processInfo.getCurrentActivity().setCreateTime(new Date());
			UniteworkTask uniteworkTask = buildUniteworkTaskByTodo(processInfo, processInfo.getCurrentActivity());
			uniteworkTask.setCurActLimitTime(taskEntity.getLimitTime());
			uniteworkTask=uniteworkService.insertTask(uniteworkTask);
			taskEntity.setCreateTime(uniteworkTask.getSendTime());
			if(isSendESB(taskEntity)&&!delTask.getBatchFlag()){
				TaskEntityDataShare.setTask(uniteworkTask);
			}
			/*uniteworkService.transfer(taskEntity.getId(),
					taskEntity.getAssigneeUserId(),
					taskEntity.getAssigneeUserName(),
					taskEntity.getAssigneeOrgId(),
					taskEntity.getAssigneeOrgName());*/

		} catch (Exception e) {
			throw new WorkFlowException(ExceptionErrorCode.C3004,"消息类型[转办],同步统一待办消息失败", e);
		}
	}

	@Override
	public boolean isFailOnException() {
		// The logic in the onEvent method of this listener is not critical,
		// exceptions
		// can be ignored if logging fails...
		return true;
	}

}