package com.leanwo.activiti.listener.base;

import java.util.Date;

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.TaskCreateEmailDto;
import com.leanwo.activiti.service.base.impl.ProdogServer;
import com.leanwo.activiti.service.base.impl.UserPreference;
import com.leanwo.activiti.util.SpringUtil;

/**
 * 发送邮件消息给审批人
 * @author GuoZhiBo 2021-11-18
 *
 */
public class EmailMessageAssignmentListener implements TaskListener {
	
	private static Logger logger = LoggerFactory.getLogger(EmailMessageAssignmentListener.class);
		
	@Override
	public void notify(DelegateTask delegateTask) {
		ProdogServer prodogServer = SpringUtil.getBean(ProdogServer.class.getName());
		// 标题
		String title = (String)delegateTask.getVariable(UserPreference.APPROVE_TITLE);
		// 审批摘要
		String content = (String)delegateTask.getVariable(UserPreference.APPROVE_CONTENT);
		// 编号
		String windowNo = (String)delegateTask.getVariable(UserPreference.WINDOW_NO);
		// 单据id
		Long recordId = (Long)delegateTask.getVariable(UserPreference.RECORD_ID);
		// 单据号
		String documentNo = (String)delegateTask.getVariable(UserPreference.DOCUMENT_NO);
		// 页签
		Integer tabIndex = (Integer)delegateTask.getVariable(UserPreference.TAB_INDEX);
		// 类名称
		String className = (String)delegateTask.getVariable(UserPreference.CLASS_NAME);
				
				
		//用户id
		String userId = delegateTask.getAssignee();
		//任务id
		String taskId = (String) delegateTask.getId();
		//创建时间
		Date createTime = (Date)delegateTask.getCreateTime();
		//审批标题
		String appvoreTitle = (String)delegateTask.getVariable(UserPreference.APPROVE_TITLE);
		
		TaskCreateEmailDto emailDto = new TaskCreateEmailDto();
		emailDto.setTitle(title);
		emailDto.setClassName(className);
		emailDto.setCreateTime(createTime);
		emailDto.setDocumentNo(documentNo);
		emailDto.setTabIndex(Long.valueOf(tabIndex));
		emailDto.setTaskInfoId(taskId);
		emailDto.setUserId(userId);
		emailDto.setWindowNo(windowNo);
		emailDto.setRecordId(recordId);
		emailDto.setAppvoreTitle(appvoreTitle);
		
		prodogServer.createEmailByTask(emailDto);
	}
}
