package com.leanwo.activiti.delegate.base;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.leanwo.activiti.dto.base.TaskCommand;
import com.leanwo.activiti.service.base.impl.UserPreference;

/**
 * 审批前判断是否有相同的用户审批过
 * @author GuoZhiBo 20180918
 *
 */
@Named
public class UserTaskListener4 implements TaskListener {
	private static Logger logger = LoggerFactory.getLogger(UserTaskListener4.class);
	
	@Inject
	private TaskService taskService;
	
	@Override
	public void notify(DelegateTask delegateTask) {
		DelegateExecution execution = delegateTask.getExecution();
		String userId = (String) execution.getVariable("USER_ID");
		List<String> listStr =  (List<String>)execution.getVariable("approvedUserList");
		if(listStr != null && listStr.size() > 0) {
			//如果用户相同审批直接结束
			if(listStr.get(0).toString().equals(userId)) {
				execution.setVariable("continue", true);
				
				execution.setVariableLocal(UserPreference.COMMENT, "审批通过");
				execution.setVariable(UserPreference.COMMAND, TaskCommand.APPROVE);

				taskService.complete(delegateTask.getId(),null);

//				logger.debug("PASS = "+ execution.getVariable(UserPreference.COMMAND));
//				Long recordId = (Long) execution.getVariable(UserPreference.RECORD_ID);	
//				Long accountId = (Long) execution.getVariable(UserPreference.ACCOUNT_ID);
//				String className = (String) execution.getVariable(UserPreference.CLASS_NAME);		
//				
//				DocumentInfoDto documentInfoDto = new DocumentInfoDto();
//				documentInfoDto.setRecordId(recordId);
//				documentInfoDto.setClassName(className);
//
//				DocumentOperationService documentOperationService = (DocumentOperationService) SpringUtil.getSingleBean(DocumentOperationService.class);
//				documentOperationService.approveDocument(accountId, documentInfoDto);
//				
//				execution.setVariable(UserPreference.PROCESS_INSTANCE_END_DATE, new Date());
			}
		}
	}
}
