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

/**
 * 当任务被分配给某个办理人之后创建钉钉待办任务
 * @author shenchong 2020-09-07 20:11:47
 *
 */
public class DingDingUserTaskAssignmentListener implements TaskListener {
	
	private static Logger logger = LoggerFactory.getLogger(DingDingUserTaskAssignmentListener.class);
		
	@Override
	public void notify(DelegateTask delegateTask) {
		ProdogServer prodogServer = SpringUtil.getBean(ProdogServer.class.getName());
		DocumentForm documentForm = null;
		String title = (String)delegateTask.getVariable(UserPreference.APPROVE_TITLE);
		String content = (String)delegateTask.getVariable(UserPreference.APPROVE_CONTENT);
		String windowNo = (String)delegateTask.getVariable(UserPreference.WINDOW_NO);
		Long recordId = (Long)delegateTask.getVariable(UserPreference.RECORD_ID);
		String userId = delegateTask.getAssignee();
		String taskId = (String) delegateTask.getId();
		if (content != null) {
			documentForm = FormatUtil.getGson().fromJson(content,DocumentForm.class);
		}
		
		String processInstanceId = delegateTask.getProcessInstanceId();
		
		WorkrecordDto workrecordDto = new WorkrecordDto();	
		workrecordDto.setUserId(userId);
		workrecordDto.setTitle(title);
		workrecordDto.setCreateTime((new Date()).getTime());
		workrecordDto.setWindowNo(windowNo);
		workrecordDto.setRecordId(recordId.toString());
		if (documentForm != null && documentForm.getParentForm() != null) {
			workrecordDto.setParentForm(documentForm.getParentForm());
		}
		workrecordDto.setTaskId(taskId);
		workrecordDto.setProcessInstanceId(processInstanceId);
		prodogServer.createWorkrecord(workrecordDto);
	}
}
