package com.njcn.bpm.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.bpm.pojo.vo.TodoVO;
import com.njcn.bpm.pojo.vo.instance.BpmProcessInstanceVO;
import com.njcn.bpm.pojo.vo.task.BpmTaskVO;
import com.njcn.supervision.enums.SupervisionKeyEnum;
import com.njcn.web.utils.RequestUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现将各类型的数据转为待办事项
 * @author hongawen
 */
public class TodoVOConvert {

    /**
     * 将BpmTaskVO 转为 TodoVO
     *
     * @param bpmTaskVOS 待办流程列表
     */
    public static List<TodoVO> convertByBpmTaskVO(List<BpmTaskVO> bpmTaskVOS) {
        if (CollectionUtil.isEmpty(bpmTaskVOS)) {
            return new ArrayList<>();
        }
        return bpmTaskVOS.stream().map(temp -> {
            TodoVO todoVO = new TodoVO();
            todoVO.setId(temp.getId());
            String processDefinitionId = temp.getProcessInstance().getProcessDefinitionId();
            String key = processDefinitionId.substring(0, processDefinitionId.indexOf(":"));
            todoVO.setTaskName(SupervisionKeyEnum.getBusinessNameByKey(key));
            todoVO.setTaskContent(temp.getInstanceSign());
            todoVO.setTaskCreateTime(temp.getCreateTime());
            // 1. 待办流程
            todoVO.setSource(1);
            todoVO.setRoutePath("");
            todoVO.setStartUser(temp.getProcessInstance().getStartUser().getName());
            todoVO.setProcessInstanceId(temp.getProcessInstanceId());
            todoVO.setHistoryInstanceId(temp.getHistoryInstanceId());
            return todoVO;
        }).collect(Collectors.toList());

    }

    /**
     * 将审核不通过的流程转为 TodoVO
     */
    public static List<TodoVO> convertByBpmProcessInstance(List<BpmProcessInstanceVO> rejectProcessInstanceList) {
        if (CollectionUtil.isEmpty(rejectProcessInstanceList)) {
            return new ArrayList<>();
        }
        return rejectProcessInstanceList.stream().map(temp -> {
            TodoVO todoVO = new TodoVO();
            todoVO.setId(temp.getBusinessKey());

            todoVO.setTaskContent(temp.getInstanceSign());
            todoVO.setTaskCreateTime(temp.getEndTime());
            // 1. 待办流程
            todoVO.setSource(2);
            // 处理路由
            String processDefinitionId = temp.getProcessDefinitionId();
            String key = processDefinitionId.substring(0, processDefinitionId.indexOf(":"));
            todoVO.setRoutePath(SupervisionKeyEnum.getRoutePathByKey(key));
            todoVO.setTaskName(SupervisionKeyEnum.getBusinessNameByKey(key));
            todoVO.setStartUser(RequestUtil.getUserNickname());
            todoVO.setProcessInstanceId(temp.getId());
            todoVO.setHistoryInstanceId(temp.getHistoryInstanceId());
            todoVO.setBusinessKey(key);
            return todoVO;
        }).collect(Collectors.toList());

    }
}
