/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.flow.entity.ProcessInstance;
import com.tofly.flow.entity.ProcessInstanceNodeInfo;
import com.tofly.flow.entity.ProcessTemplateNode;
import com.tofly.flow.exception.BusinessException;
import com.tofly.flow.mapper.ProcessInstanceMapper;
import com.tofly.flow.service.ProcessInstanceNodeInfoService;
import com.tofly.flow.service.ProcessInstanceService;
import com.tofly.flow.service.ProcessTemplateNodeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author zhoux
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("processInstanceService")
public class ProcessInstanceServiceImpl extends ServiceImpl<ProcessInstanceMapper, ProcessInstance> implements ProcessInstanceService {

	
	@Autowired
	public ProcessInstanceMapper processInstanceMapper;
	
	
	@Autowired
	public ProcessTemplateNodeService processTemplateNodeService;
	
	
	@Autowired
	public ProcessInstanceNodeInfoService processInstanceNodeInfoService;
	
	


	@Override
	public Map<String, String> executeProcessInstance(ProcessInstanceNodeInfo processInstanceNodeInfo,Integer approvalStatus) {
				
		Map<String,String> returnMap =new HashMap<String,String>();	
		try {						
			ProcessInstance instance=  processInstanceMapper.selectById(processInstanceNodeInfo.getProcessId());	
			List<ProcessTemplateNode> templateNodeList = processTemplateNodeService.list
					(Wrappers.<ProcessTemplateNode>lambdaQuery()
					.eq(ProcessTemplateNode::getProcessTemplateId,instance.getProcessTemplateId()).orderByAsc(ProcessTemplateNode::getSortNum));
			Integer currentNodeSort = instance.getCurrentNodeSort();	
			//申请请求				
			if(approvalStatus==1) {								
				if(currentNodeSort==templateNodeList.size()) {					
					instance.setCurrentNodeSort(0);
					instance.setCurrentNode("");
					instance.setProcessStatus(4);
					processInstanceMapper.updateById(instance);						
				}else {
					instance.setCurrentNode(templateNodeList.get(currentNodeSort).getProcessNodeAlias());
					instance.setCurrentNodeSort(currentNodeSort+1);
					processInstanceMapper.updateById(instance);					
				}				
			//回退请求				
			}else if((approvalStatus==2)) {				
				if(currentNodeSort!=1) {
					instance.setCurrentNode(templateNodeList.get(currentNodeSort-2).getProcessNodeAlias());
					instance.setCurrentNodeSort(currentNodeSort-1);
					processInstanceMapper.updateById(instance);			
				}				
			}
			returnMap.put("flag", "true");
			returnMap.put("msg", "流程执行成功");					
		}catch (Exception e) {
			e.printStackTrace();
			returnMap.put("flag", "false");
			returnMap.put("msg", e.getMessage());
		}		
		return returnMap;		
	}
}