package com.ankaibei.workFlow.webadmin.ankaibei.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ankaibei.workFlow.common.core.annotation.DisableDataFilter;
import com.ankaibei.workFlow.common.core.object.MyPageData;
import com.ankaibei.workFlow.common.core.object.ResponseResult;
import com.ankaibei.workFlow.common.core.object.TokenData;
import com.ankaibei.workFlow.common.core.util.MyPageUtil;
import com.ankaibei.workFlow.common.flow.dao.ProjectInfoFlowMapper;
import com.ankaibei.workFlow.common.flow.model.FlowCategory;
import com.ankaibei.workFlow.common.flow.model.FlowEntry;
import com.ankaibei.workFlow.common.flow.model.FlowWorkOrder;
import com.ankaibei.workFlow.common.flow.model.ProjectInfoEntityOfFlow;
import com.ankaibei.workFlow.common.flow.service.FlowApiService;
import com.ankaibei.workFlow.common.flow.service.FlowCategoryService;
import com.ankaibei.workFlow.common.flow.service.FlowEntryService;
import com.ankaibei.workFlow.common.flow.service.FlowWorkOrderService;
import com.ankaibei.workFlow.common.flow.util.FlowOperationHelper;
import com.ankaibei.workFlow.common.flow.vo.FlowTaskVo;
import com.ankaibei.workFlow.common.flow.vo.ListRuntimeTaskRequestVo;
import com.ankaibei.workFlow.common.flow.vo.TaskInfoVo;
import com.ankaibei.workFlow.common.online.dto.OnlineFilterDto;
import com.ankaibei.workFlow.common.online.model.OnlineColumn;
import com.ankaibei.workFlow.common.online.model.OnlineDatasource;
import com.ankaibei.workFlow.common.online.model.OnlineDatasourceRelation;
import com.ankaibei.workFlow.common.online.model.OnlineTable;
import com.ankaibei.workFlow.common.online.model.constant.FieldFilterType;
import com.ankaibei.workFlow.common.online.model.constant.RelationType;
import com.ankaibei.workFlow.common.online.service.OnlineOperationService;
import com.ankaibei.workFlow.common.online.util.OnlineOperationHelper;
import com.ankaibei.workFlow.webadmin.ankaibei.api.CostInFoApi;
import com.ankaibei.workFlow.webadmin.ankaibei.entity.ProjectInfoEntity;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.mapper.ProjectInfoMapper;
import com.ankaibei.workFlow.webadmin.ankaibei.mapper.UserInfoMapper;
import com.ankaibei.workFlow.webadmin.ankaibei.utils.FlowTaskStart;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.CostInfoVo;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.ResMsg;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lwq
 * @version 1.0
 * @description:费用管理
 * @date 2022/9/26 14:43
 */
@RestController
@RequestMapping("/cost")
public class CostInfoController {

    @Autowired
    private CostInFoApi costInFoApi;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FlowApiService flowApiService;
    @Autowired
    private FlowEntryService flowEntryService;
    @Autowired
    private FlowCategoryService categoryService;
    @Autowired
    private ProjectInfoFlowMapper projectInfoFlowMapper;
    @Autowired
    private ProjectInfoMapper projectInfoMapper;
    @Autowired
    private FlowOperationHelper flowOperationHelper;
    @Autowired
    private OnlineOperationHelper onlineOperationHelper;
    @Autowired
    private OnlineOperationService onlineOperationService;
    @Autowired
    private FlowTaskStart flowTaskStart;
    @Autowired
    private FlowWorkOrderService flowWorkOrderService;

    /**
     * 查询所有费用信息
     *
     * @param
     * @return ResMsg
     */
    @RequestMapping("/findCostInfoAll")
    public ResMsg findCostInfoAll(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.findCostInfoAll(costInfoVo);
    }

    /**
     * 删除
     *
     * @param costInfoVoVos
     * @return ResMsg
     */
    @RequestMapping("/deleteCostInfo")
    public ResMsg deleteCostInfo(@RequestBody List<CostInfoVo> costInfoVoVos) {

        if (costInFoApi.deleteCostInfo(costInfoVoVos)) {
            return new ResMsg(CodeEnum.OK);
        }
        return new ResMsg(CodeEnum.FAIL);
    }

    /**
     * 编辑
     *
     * @param costInfoVo
     * @return ResMsg
     */
    @RequestMapping("/updateCostInfo")
    public ResMsg updateCostInfo(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.updateCostInfo(costInfoVo);
    }

    /**
     * 查询费用详情  参数为类型和id
     *
     * @param costInfoVo
     * @return ResMsg
     */
    @RequestMapping("/findCostInfoDetails")
    public ResMsg findCostInfoDetails(@RequestBody CostInfoVo costInfoVo) {

        return costInFoApi.findCostInfoDetails(costInfoVo);
    }

    /**
     * 查询项目审核列表
     *
     * @param
     * @return ResMsg
     */
    @RequestMapping("/findProjectAuditList")
    public ResMsg findProjectAuditList(@RequestBody UserInfoVo userInfoVo) {
        String id = TokenData.takeFromRequest().getUserId();
        return costInFoApi.findProjectAuditList(id, userInfoVo);
    }


    /**
     * 返回当前用户待办的任务列表。
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTask")
    public ResMsg listRuntimeTask(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        MyPageData<Task> pageData = flowApiService.getTaskListByUserName(
                username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageData.getDataList());
        //加上流程分类名称和关联的表单信息中的项目名称
        List<FlowTaskVo> flowTaskVoListAd = new ArrayList<>();

        for (FlowTaskVo taskVo : flowTaskVoList) {
            String categoryName = categoryService.getById(flowEntryService.getById(taskVo.getEntryId()).getCategoryId()).getName();
            taskVo.setCategoryName(categoryName);
            //拿到任务所在表单中数据字段和对应的值(包含每个申请记录的项目名称和流程的任务变量)
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            if (masterAndOneToOne.get("project_id") != null) {
                Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                taskVo.setProjectId(projectId);
                String projectName = projectInfoFlowMapper.getById(projectId).getProjectName();
                taskVo.setProjectName(projectName == null ? "" : projectName);
            } else if (masterAndOneToOne.get("project_name") != null) {
                Integer projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                ProjectInfoEntityOfFlow byId = projectInfoFlowMapper.getById(projectId);
                if (byId != null) {
                    taskVo.setProjectId(projectId);
                    taskVo.setProjectName(byId.getProjectName());
                }
            }
            if (masterAndOneToOne.get("total_money") != null) {
                taskVo.setApplicationFee(masterAndOneToOne.get("total_money").toString());
            }
            //取每条费用申请记录中的费用项，费用类型的直接取，工时和新建项目使用流程分类名称即可
            if (masterAndOneToOne.get("cost_type") != null) {
                taskVo.setCostType(masterAndOneToOne.get("cost_type").toString());
            } else {
                taskVo.setCostType(categoryName);
            }
            taskVo.setFlowTaskStatus(flowWorkOrderService.getFlowWorkOrderByProcessInstanceId(taskVo.getProcessInstanceId()).getFlowStatus() + "");
            if (StringUtils.isNotBlank(listRuntimeTaskRequestVo.getProjectName())) {
                if (taskVo.getProjectName().contains(listRuntimeTaskRequestVo.getProjectName())) {
                    flowTaskVoListAd.add(taskVo);
                }
            } else {
                flowTaskVoListAd.add(taskVo);
            }
        }
        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListAd, pageData.getTotalCount()));
    }

    /**
     * 返回当前用户待办的任务列表。(费用报销分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByExpense")
    public ResMsg listRuntimeTaskByExpense(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        //---查询到所有的任务量
        final Integer pageNum = listRuntimeTaskRequestVo.getPageNum();
        listRuntimeTaskRequestVo.setPageNum(1);
        listRuntimeTaskRequestVo.setPageSize(flowWorkOrderService.getCountByCondition(""));
        MyPageData<Task> pageDataTwo = flowApiService.getTaskListByUserName(username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageDataTwo.getDataList());
        String queryType = "费用报销";
        return PackageAgain(flowTaskVoList, pageNum, queryType);
    }


    /**
     * 返回当前用户待办的任务列表。(员工工时分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */

    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByManHour")
    public ResMsg listRuntimeTaskByManHour(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        //---查询到所有的任务量
        final Integer pageNum = listRuntimeTaskRequestVo.getPageNum();
        listRuntimeTaskRequestVo.setPageNum(1);
        listRuntimeTaskRequestVo.setPageSize(flowWorkOrderService.getCountByCondition(""));
        MyPageData<Task> pageDataTwo = flowApiService.getTaskListByUserName(username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageDataTwo.getDataList());
        String queryType = "人员工时";
        return PackageAgain(flowTaskVoList, pageNum, queryType);
    }

    /**
     * 返回当前用户待办的任务列表。(操作项目信息分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByProjectAdd")
    public ResMsg listRuntimeTaskByProjectAdd(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        //---查询到所有的任务量
        final Integer pageNum = listRuntimeTaskRequestVo.getPageNum();
        listRuntimeTaskRequestVo.setPageNum(1);
        listRuntimeTaskRequestVo.setPageSize(flowWorkOrderService.getCountByCondition(""));
        MyPageData<Task> pageDataTwo = flowApiService.getTaskListByUserName(username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageDataTwo.getDataList());
        String queryType = "新建项目";
        return PackageAgain(flowTaskVoList, pageNum, queryType);
    }

    /**
     * 返回当前用户待办的任务列表。(添加任务分类)
     * param processDefinitionName 流程定义名 (模糊查询)。
     * param taskName              任务名称 (模糊查询)。
     * param pageParam             分页对象。
     *
     * @return 返回当前用户待办的任务列表。如果指定流程标识，则仅返回该流程的待办任务列表。
     */
    @DisableDataFilter
    @PostMapping("/listRuntimeTaskByProjectTaskAdd")
    public ResMsg listRuntimeTaskByProjectTaskAdd(
            @RequestBody ListRuntimeTaskRequestVo listRuntimeTaskRequestVo) {
        String username = TokenData.takeFromRequest().getLoginName();
        //---查询到所有的任务量
        final Integer pageNum = listRuntimeTaskRequestVo.getPageNum();
        listRuntimeTaskRequestVo.setPageNum(1);
        listRuntimeTaskRequestVo.setPageSize(flowWorkOrderService.getCountByCondition(""));
        MyPageData<Task> pageDataTwo = flowApiService.getTaskListByUserName(username, listRuntimeTaskRequestVo);
        List<FlowTaskVo> flowTaskVoList = flowApiService.convertToFlowTaskList(pageDataTwo.getDataList());
        String queryType = "新建任务";
        return PackageAgain(flowTaskVoList, pageNum, queryType);
    }

    /**
     * 获取当前流程实例的详情数据。包括主表数据、一对一从表数据、一对多从表数据列表等。
     * 该接口无需数据权限过滤，因此用DisableDataFilter注解标注。如果当前系统没有支持数据权限过滤，该注解不会有任何影响。
     *
     * @param processInstanceId 当前运行时的流程实例Id。
     * @param taskId            流程任务Id。
     * @return 当前流程实例的详情数据。
     */
    public ResMsg<JSONObject> viewUserTaskDatasource(
            String processInstanceId, String taskId) {
        String errorMessage;
        // 验证流程任务的合法性。
        Task task = flowApiService.getProcessInstanceActiveTask(processInstanceId, taskId);
        ProcessInstance instance = flowApiService.getProcessInstance(processInstanceId);
        // 如果业务主数据为空，则直接返回。
        if (StrUtil.isBlank(instance.getBusinessKey())) {
            return ResMsg.newInstance(CodeEnum.OK);
        }
        ResponseResult<TaskInfoVo> taskInfoResult = flowOperationHelper.verifyAndGetRuntimeTaskInfo(task);
        if (!taskInfoResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, taskInfoResult);
        }
        TaskInfoVo taskInfo = taskInfoResult.getData();
        // 验证在线表单及其关联数据源的合法性。
        ResponseResult<OnlineDatasource> datasourceResult = flowTaskStart.verifyAndGetOnlineDatasource(taskInfo.getFormId());
        if (!datasourceResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, datasourceResult);
        }

        //datasource中columnMap为提交的申请的各个字段
        OnlineDatasource datasource = datasourceResult.getData();
        ResponseResult<List<OnlineDatasourceRelation>> relationListResult =
                onlineOperationHelper.verifyAndGetRelationList(datasource.getDatasourceId(), null);
        if (!relationListResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, relationListResult);
        }
        JSONObject jsonData = this.buildUserTaskData(
                instance.getBusinessKey(), datasource.getMasterTable(), relationListResult.getData());
        return ResMsg.newInstance(CodeEnum.OK, jsonData);
    }


    private JSONObject buildUserTaskData(
            String businessKey, OnlineTable masterTable, List<OnlineDatasourceRelation> relationList) {
        JSONObject jsonData = new JSONObject();
        List<OnlineDatasourceRelation> oneToOneRelationList = relationList.stream()
                .filter(r -> r.getRelationType().equals(RelationType.ONE_TO_ONE)).collect(Collectors.toList());
        Map<String, Object> result =
                onlineOperationService.getMasterData(masterTable, oneToOneRelationList, relationList, businessKey);
        if (MapUtil.isEmpty(result)) {
            return jsonData;
        }
        jsonData.put("masterAndOneToOne", result);
        List<OnlineDatasourceRelation> oneToManyRelationList = relationList.stream()
                .filter(r -> r.getRelationType().equals(RelationType.ONE_TO_MANY)).collect(Collectors.toList());
        if (CollUtil.isEmpty(oneToManyRelationList)) {
            return jsonData;
        }
        JSONObject oneToManyJsonData = new JSONObject();
        jsonData.put("oneToMany", oneToManyJsonData);
        for (OnlineDatasourceRelation relation : oneToManyRelationList) {
            OnlineFilterDto filterDto = new OnlineFilterDto();
            filterDto.setTableName(relation.getSlaveTable().getTableName());
            OnlineColumn slaveColumn = relation.getSlaveTable().getColumnMap().get(relation.getSlaveColumnId());
            filterDto.setColumnName(slaveColumn.getColumnName());
            filterDto.setFilterType(FieldFilterType.EQUAL_FILTER);
            OnlineColumn masterColumn = masterTable.getColumnMap().get(relation.getMasterColumnId());
            Object columnValue = result.get(masterColumn.getColumnName());
            filterDto.setColumnValue(columnValue);
            List<Map<String, Object>> slaveResultList =
                    onlineOperationService.getSlaveDataList(relation, CollUtil.newLinkedList(filterDto), null);
            if (CollUtil.isNotEmpty(slaveResultList)) {
                oneToManyJsonData.put(relation.getVariableName(), slaveResultList);
            }
        }
        return jsonData;
    }


    private ResMsg PackageAgain(List<FlowTaskVo> flowTaskVoList, Integer pageNum, String queryType) {
        //---从所有的任务里取出一个分类的任务
        //先拿到任务所在的全部流程
        List<FlowTaskVo> flowTaskVoListByQueryType = new ArrayList<>();
        Set<Long> entryIdList = new HashSet<>();
        for (FlowTaskVo taskVo : flowTaskVoList) {
            entryIdList.add(Long.valueOf(taskVo.getEntryId()));
        }
        List<FlowEntry> entryList = flowEntryService.getInList(entryIdList);
        //获取流程所在的分类
        Set<Long> categoryIdList = new HashSet<>();
        for (FlowEntry entry : entryList) {
            categoryIdList.add(Long.valueOf(entry.getCategoryId()));
        }
        List<FlowCategory> flowCategoryList = categoryService.getInList(categoryIdList);
        //给流程任务对象的流程分类字段赋值
        for (FlowTaskVo taskVo : flowTaskVoList) {
            String entryId = taskVo.getEntryId();
            for (FlowEntry entry : entryList) {
                if (entry.getEntryId().equals(entryId)) {
                    for (FlowCategory category : flowCategoryList) {
                        if (category.getCategoryId().equals(entry.getCategoryId())) {
                            if (category.getName().equals(queryType)) {
                                taskVo.setCostType(category.getName());
                                flowTaskVoListByQueryType.add(taskVo);
                            }
                        }
                    }
                }
            }
        }
        //重新进行包装分页
        if (flowTaskVoListByQueryType.size() < 1) {
            return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByQueryType, Long.valueOf(flowTaskVoListByQueryType.size())));
        }
        List<FlowTaskVo> flowTaskVoListByQueryTypeRsg = new ArrayList<>();
        if (pageNum == 1) {
            for (int i = 0; i < 10; i++) {
                flowTaskVoListByQueryTypeRsg.add(flowTaskVoListByQueryType.get(i));
                if (i == flowTaskVoListByQueryType.size() - 1) {
                    break;
                }
            }
        } else {
            for (int i = (pageNum - 1) * 10; i < pageNum * 10 - 1; i++) {
                flowTaskVoListByQueryTypeRsg.add(flowTaskVoListByQueryType.get(i));
                if (i == flowTaskVoListByQueryType.size() - 1) {
                    break;
                }
            }
        }
        //获取任务中的全部项目id和流程实例id
        Set<Integer> projectIds = new HashSet<>();
        Set<String> processInstanceIds = new HashSet<>();
        //查询的任务分类是新建项目时，需要筛选新建项目的表单数据
        if (queryType.equals("新建项目")) {
            Map<String, Object> byTaskAdd = flowTaskAssignmentByProjectAdd(flowTaskVoListByQueryTypeRsg, queryType);
            projectIds = (Set<Integer>) byTaskAdd.get("projectIds");
            processInstanceIds = (Set<String>) byTaskAdd.get("processInstanceIds");
            flowTaskVoListByQueryTypeRsg = (List<FlowTaskVo>) byTaskAdd.get("flowTaskVoListByQueryTypeRsg");
        }
        //查询的任务分类是新建项目任务时，需要筛选新建任务的表单数据
        if (queryType.equals("新建任务")) {
            Map<String, Object> byTaskAdd = flowTaskAssignmentByTaskAdd(flowTaskVoListByQueryTypeRsg, queryType);
            projectIds = (Set<Integer>) byTaskAdd.get("projectIds");
            processInstanceIds = (Set<String>) byTaskAdd.get("processInstanceIds");
            flowTaskVoListByQueryTypeRsg = (List<FlowTaskVo>) byTaskAdd.get("flowTaskVoListByQueryTypeRsg");
        }
        //查询的任务分类是人员工时时，需要筛选人员工时的表单数据
        if (queryType.equals("人员工时")) {
            Map<String, Object> byTaskAdd = flowTaskAssignmentByManhour(flowTaskVoListByQueryTypeRsg, queryType);
            projectIds = (Set<Integer>) byTaskAdd.get("projectIds");
            processInstanceIds = (Set<String>) byTaskAdd.get("processInstanceIds");
            flowTaskVoListByQueryTypeRsg = (List<FlowTaskVo>) byTaskAdd.get("flowTaskVoListByQueryTypeRsg");
        }
        //查询的任务分类是费用报销时，需要筛选费用报销的表单数据
        if (queryType.equals("费用报销")) {
            Map<String, Object> byTaskAdd = flowTaskAssignmentByExpense(flowTaskVoListByQueryTypeRsg, queryType);
            projectIds = (Set<Integer>) byTaskAdd.get("projectIds");
            processInstanceIds = (Set<String>) byTaskAdd.get("processInstanceIds");
            flowTaskVoListByQueryTypeRsg = (List<FlowTaskVo>) byTaskAdd.get("flowTaskVoListByQueryTypeRsg");
        }
        //查询到任务信息中包含的全部项目和表单数据
        List<ProjectInfoEntity> projectInfos = projectInfoMapper.findAllById(projectIds);
        List<FlowWorkOrder> instanceIdIn = flowWorkOrderService.findAllByProcessInstanceIdIn(processInstanceIds);
        for (FlowTaskVo taskVo : flowTaskVoListByQueryTypeRsg) {
            for (ProjectInfoEntity info : projectInfos) {
                if (info.getProjectId().equals(taskVo.getProjectId())) {
                    taskVo.setProjectName(info.getProjectName() == null ? "" : info.getProjectName());
                    break;
                }
            }
            for (FlowWorkOrder order : instanceIdIn) {
                if (order.getProcessInstanceId().equals(taskVo.getProcessInstanceId())) {
                    taskVo.setProcessInstanceInitiator(order.getSubmitUsername());
                    String showName = userInfoMapper.findByLoginNameEqualsAndDeletedFlagIs(order.getSubmitUsername(), 1).getShowName();
                    taskVo.setShowName(showName == null ? "" : showName);
                    taskVo.setFlowTaskStatus(order.getFlowStatus() + "");
                    break;
                }
            }
        }

        return ResMsg.newInstance(CodeEnum.OK, MyPageUtil.makeResponseData(flowTaskVoListByQueryTypeRsg, Long.valueOf(flowTaskVoListByQueryType.size())));
    }

    //查询的任务分类是新建项目时，需要筛选项目流程记录的表单数据
    private Map<String, Object> flowTaskAssignmentByProjectAdd(List<FlowTaskVo> flowTaskVoListByQueryTypeRsg, String queryType) {
        Set<Integer> projectIds = new HashSet<>();
        Set<String> processInstanceIds = new HashSet<>();
        for (FlowTaskVo taskVo : flowTaskVoListByQueryTypeRsg) {
            Integer projectId = null;
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            if (masterAndOneToOne.get("project_id") != null) {
                projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                taskVo.setProjectId(projectId);
            }
            taskVo.setCostType(queryType);
            taskVo.setCategoryName(queryType);
            projectIds.add(projectId);
            processInstanceIds.add(taskVo.getProcessInstanceId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectIds", projectIds);
        map.put("processInstanceIds", processInstanceIds);
        map.put("flowTaskVoListByQueryTypeRsg", flowTaskVoListByQueryTypeRsg);
        return map;
    }

    //查询的任务分类是新建项目任务时，需要筛选新建任务的表单数据
    private Map<String, Object> flowTaskAssignmentByTaskAdd(List<FlowTaskVo> flowTaskVoListByQueryTypeRsg, String queryType) {
        Set<Integer> projectIds = new HashSet<>();
        Set<String> processInstanceIds = new HashSet<>();
        for (FlowTaskVo taskVo : flowTaskVoListByQueryTypeRsg) {
            Integer projectId = null;
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            if (masterAndOneToOne.get("project_id") != null) {
                projectId = Integer.parseInt(masterAndOneToOne.get("project_id").toString());
                taskVo.setProjectId(projectId);
            }
            //新建任务分类中，获取的项目任务的id
            if (masterAndOneToOne.get("task_id") != null) {
                taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("task_id").toString()));
            }
            taskVo.setCostType(queryType);
            taskVo.setCategoryName(queryType);
            projectIds.add(projectId);
            processInstanceIds.add(taskVo.getProcessInstanceId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectIds", projectIds);
        map.put("processInstanceIds", processInstanceIds);
        map.put("flowTaskVoListByQueryTypeRsg", flowTaskVoListByQueryTypeRsg);
        return map;
    }

    //查询的任务分类是新建项目任务时，需要筛选新建任务的表单数据
    private Map<String, Object> flowTaskAssignmentByManhour(List<FlowTaskVo> flowTaskVoListByQueryTypeRsg, String queryType) {
        Set<Integer> projectIds = new HashSet<>();
        Set<String> processInstanceIds = new HashSet<>();
        for (FlowTaskVo taskVo : flowTaskVoListByQueryTypeRsg) {
            Integer projectId = null;
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            //人员工时分类中，获取的项目id和工时流程表中保存的工时id
            if (masterAndOneToOne.get("project_name") != null) {
                projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                taskVo.setProjectId(projectId);
            }
            if (masterAndOneToOne.get("man_id") != null) {
                taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("man_id").toString()));
            }
            taskVo.setCostType(queryType);
            taskVo.setCategoryName(queryType);
            projectIds.add(projectId);
            processInstanceIds.add(taskVo.getProcessInstanceId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectIds", projectIds);
        map.put("processInstanceIds", processInstanceIds);
        map.put("flowTaskVoListByQueryTypeRsg", flowTaskVoListByQueryTypeRsg);
        return map;
    }

    //查询的任务分类是费用报销时，需要筛选费用报销的表单数据
    private Map<String, Object> flowTaskAssignmentByExpense(List<FlowTaskVo> flowTaskVoListByQueryTypeRsg, String queryType) {
        Set<Integer> projectIds = new HashSet<>();
        Set<String> processInstanceIds = new HashSet<>();
        for (FlowTaskVo taskVo : flowTaskVoListByQueryTypeRsg) {
            Integer projectId = null;
            JSONObject jsonData = viewUserTaskDatasource(taskVo.getProcessInstanceId(), taskVo.getTaskId()).getData();
            Map<String, Object> masterAndOneToOne = (Map<String, Object>) jsonData.get("masterAndOneToOne");
            //获取的项目id
            if (masterAndOneToOne.get("project_name") != null) {
                projectId = Integer.parseInt(masterAndOneToOne.get("project_name").toString());
                taskVo.setProjectId(projectId);
            }
            //费用报销分类中，获取费用申请记录流程表中的【申请费用，总价，住宿天数】
            if (masterAndOneToOne.get("application_fee") != null) {
                taskVo.setApplicationFee(masterAndOneToOne.get("application_fee").toString());
            }
            if (masterAndOneToOne.get("total_money") != null) {
                taskVo.setApplicationFee(masterAndOneToOne.get("total_money").toString());
            }
            if (masterAndOneToOne.get("accommodation_days") != null) {
                taskVo.setAccommodationDays(masterAndOneToOne.get("accommodation_days").toString());
            }
            //费用报销分类中，每个审批项在各自记录表中的主键id
            if (masterAndOneToOne.get("id") != null) {
                taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("id").toString()));
            } else if (masterAndOneToOne.get("communication_id") != null) {
                taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("communication_id").toString()));
            } else if (masterAndOneToOne.get("purchase_id") != null) {
                taskVo.setApplyDataId(Integer.parseInt(masterAndOneToOne.get("purchase_id").toString()));
            }
            //费用报销分类中，每个审批项的费用类别
            if (masterAndOneToOne.get("cost_type") != null) {
                taskVo.setCostType(masterAndOneToOne.get("cost_type").toString());
            }
            taskVo.setCategoryName(queryType);
            projectIds.add(projectId);
            processInstanceIds.add(taskVo.getProcessInstanceId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectIds", projectIds);
        map.put("processInstanceIds", processInstanceIds);
        map.put("flowTaskVoListByQueryTypeRsg", flowTaskVoListByQueryTypeRsg);
        return map;
    }
}
