package com.leanwo.activiti.listener.base;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.leanwo.activiti.dto.base.LeaderUserDto;
import com.leanwo.activiti.exception.WorkflowException;
import com.leanwo.activiti.service.base.impl.ProdogServer;
import com.leanwo.activiti.util.FormatUtil;
import com.leanwo.activiti.util.SpringUtil;
import com.leanwo.activiti.util.StringUtil;

/**
 * 多实例任务（部门主管）创建监听器
 * <ul>
 * <li>获取设置的部门</li>
 * <li>获取部门的主管人员</li>
 * <li>设置审批人</li>
 * </ul>
 * .
 *
 * @author YangZhiJie
 */
public class MultiInstanceUserTaskByOrganizationExecutionListener implements ExecutionListener {

	/**  */
	private static Logger logger = LoggerFactory.getLogger(MultiInstanceUserTaskByOrganizationExecutionListener.class);

	/** 任务接受人(String). */
	private Expression assignListVariableName;

	/** 部门Id集合. */
	private Expression organizationIdList;

	/** 节点名称. */
	private Expression nodeName;

	/**
	 * 
	 *
	 * @param execution
	 */
	@Override
	public void notify(DelegateExecution execution) {
		String assignListName = assignListVariableName.getExpressionText();
		String organizationIdListText = organizationIdList.getExpressionText();
		String node = nodeName.getExpressionText();

		if (StringUtil.isEmpty(assignListName) || StringUtil.isEmpty(organizationIdListText)) {
			throw new WorkflowException(String.format("多实例任务(%s)未定义变量assignListVariableName 和 organizationIdsText。", node));
		}

		List<String> organizationIdList = FormatUtil.getGson().fromJson(organizationIdListText,  new TypeToken<List<String>>() {}.getType());
		if(organizationIdList == null || organizationIdList.size() == 0) {
			throw new WorkflowException(String.format("部门主管审批节点(%s)，未设置部门。", node));
		}
		
		List<Long> organizationIds = new ArrayList<Long>();
		organizationIdList.forEach(organizationIdStr -> {
			organizationIds.add(Long.parseLong(organizationIdStr));
		});
		
		ProdogServer prodogServer = SpringUtil.getBean(ProdogServer.class.getName());

		List<LeaderUserDto> userDtos = prodogServer.listLeaderUserByOrganizationIds(organizationIds);

		// 审批人为空时抛出异常
		if (userDtos == null || userDtos.size() == 0) {
			String message = String.format("部门主管审批节点(%s)，选择的部门无部门主管，请设置选择部门的部门主管。", node);
			throw new WorkflowException(message);
		}

		// 提前设置好审批人
		List<String> userIdStrings = new ArrayList<String>();
		userDtos.forEach(userDto -> {
			userIdStrings.add(userDto.getId().toString());
		});
		execution.setVariable(assignListName, userIdStrings);
	}

	/**
	 * Gets the 任务接受人(String).
	 *
	 * @return the 任务接受人(String)
	 */
	public Expression getAssignListVariableName() {
		return assignListVariableName;
	}

	/**
	 * Sets the 任务接受人(String).
	 *
	 * @param assignListVariableName the new 任务接受人(String)
	 */
	public void setAssignListVariableName(Expression assignListVariableName) {
		this.assignListVariableName = assignListVariableName;
	}

	/**
	 * Gets the 部门Id集合.
	 *
	 * @return the 部门Id集合
	 */
	public Expression getOrganizationIdList() {
		return organizationIdList;
	}

	/**
	 * Sets the 部门Id集合.
	 *
	 * @param organizationIdList the new 部门Id集合
	 */
	public void setOrganizationIdList(Expression organizationIdList) {
		this.organizationIdList = organizationIdList;
	}

	/**
	 * Gets the 节点名称.
	 *
	 * @return the 节点名称
	 */
	public Expression getNodeName() {
		return nodeName;
	}

	/**
	 * Sets the 节点名称.
	 *
	 * @param nodeName the new 节点名称
	 */
	public void setNodeName(Expression nodeName) {
		this.nodeName = nodeName;
	}

}
