package com.leanwo.activiti.delegate.base;

import java.util.Date;

import javax.inject.Named;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.leanwo.activiti.dto.base.DocumentInfoDto;
import com.leanwo.activiti.service.base.DocumentOperationService;
import com.leanwo.activiti.service.base.impl.UserPreference;
import com.leanwo.activiti.util.SpringUtil;



/**
 * 单据审批通过后执行本程序，程序包括如下流程：
 * <ul>
 * 	<li>
 * 	设置单据状态为审批通过
 * </li>
 * <li>
 *  添加阅览人添加至UserReadDocument
 * </li>
 * <ul>
 * 
 * 
 * @author Jack
 *
 */
@Named
public class DocumentApprovedService implements JavaDelegate {
	private static Logger logger = LoggerFactory.getLogger(DocumentApprovedService.class);
	
	
	@Override
	public void execute(DelegateExecution execution)  {
		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());
	}
}
