package com.leanwo.activiti.listener.base;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;

import com.leanwo.activiti.dto.base.KeyValueObject;
import com.leanwo.activiti.dto.base.NodeApprovedRequest;
import com.leanwo.activiti.dto.base.NodeApprovedResponse;
import com.leanwo.activiti.dto.base.TaskCommand;
import com.leanwo.activiti.service.base.DocumentOperationService;
import com.leanwo.activiti.service.base.impl.UserPreference;
import com.leanwo.activiti.util.SpringUtil;

/**
 * 用户任务审批通过的监听器
 * @author YangZhiJie
 *
 */
public class UserTaskContinueListener implements TaskListener {

	@Override
	public void notify(DelegateTask delegateTask) {
		
		String command = (String) delegateTask.getVariable(UserPreference.COMMAND);
		
		if(command != null && command.equals(TaskCommand.APPROVE.toString())){

			Long recordId = (Long) delegateTask.getVariable(UserPreference.RECORD_ID);	
			Long accountId = (Long) delegateTask.getVariable(UserPreference.ACCOUNT_ID);
			String className = (String) delegateTask.getVariable(UserPreference.CLASS_NAME);		
			
			String assignee = delegateTask.getAssignee();

			RuntimeService runtimeService = (RuntimeService) SpringUtil.getSingleBean(RuntimeService.class);
	        ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
			Execution execution = executionQuery.executionId(delegateTask.getExecutionId()).singleResult();
	        String activityId = execution.getActivityId();
	        
			NodeApprovedRequest nodeApprovedRequest = new NodeApprovedRequest();
			nodeApprovedRequest.setRecordId(recordId);
			nodeApprovedRequest.setClassName(className);
			nodeApprovedRequest.setAccountId(accountId);
			nodeApprovedRequest.setNodeId(activityId);
			nodeApprovedRequest.setUserId(Long.parseLong(assignee));

			DocumentOperationService documentOperationService = (DocumentOperationService) SpringUtil.getSingleBean(DocumentOperationService.class);
			NodeApprovedResponse nodeApprovedResponse = documentOperationService.afterNodeApproved(nodeApprovedRequest);
			
			if(nodeApprovedResponse != null) {
				if(nodeApprovedResponse.getModelData() != null) {
					delegateTask.setVariable(UserPreference.MODEL_DATA, nodeApprovedResponse.getModelData());
				}
				
				if (nodeApprovedResponse.getKeyValueObjects() != null) {
					for (KeyValueObject keyValue : nodeApprovedResponse.getKeyValueObjects()) {
						if(keyValue.getValueString() != null && keyValue.getValueString().length() > 0) {
							delegateTask.setVariable(keyValue.getKey(), keyValue.getValueString());
						} else if(keyValue.getValueBoolean() != null) {
							delegateTask.setVariable(keyValue.getKey(), keyValue.getValueBoolean());
						} else if(keyValue.getValueDouble() != null) {
							delegateTask.setVariable(keyValue.getKey(), keyValue.getValueDouble());
						}
					}
				}
			}
		}
	}

}
