package com.ankaibei.workFlow.webadmin.ankaibei.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ankaibei.workFlow.common.core.object.ResponseResult;
import com.ankaibei.workFlow.common.core.object.TokenData;
import com.ankaibei.workFlow.common.flow.constant.FlowConstant;
import com.ankaibei.workFlow.common.flow.constant.FlowTaskStatus;
import com.ankaibei.workFlow.common.flow.dto.FlowTaskCommentDto;
import com.ankaibei.workFlow.common.flow.model.FlowEntry;
import com.ankaibei.workFlow.common.flow.model.FlowEntryPublish;
import com.ankaibei.workFlow.common.flow.vo.TaskInfoVo;
import com.ankaibei.workFlow.webadmin.ankaibei.entity.*;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.mapper.*;
import com.ankaibei.workFlow.webadmin.ankaibei.service.ApplyForCostService;
import com.ankaibei.workFlow.webadmin.ankaibei.service.CostInfoService;
import com.ankaibei.workFlow.webadmin.ankaibei.utils.FlowTaskStart;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/9/26 16:18
 */
@Service
public class ApplyForCostServiceImpl implements ApplyForCostService {

    @Resource
    private FileUtil fileUtil;
    @Autowired
    private FlowTaskStart flowTaskStart;
    @Resource
    private RestInfoMapper restInfoMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private CostInfoService costInfoService;
    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;
    @Resource
    private CommunicationMapper communicationMapper;
    @Resource
    private RepastExpenseMapper repastExpenseMapper;
    @Resource
    private TravelExpenseMapper travelExpenseMapper;
    @Resource
    private TrafficExpenseMapper trafficExpenseMapper;

    @Override
    public ResMsg addCommunication(CommunicationInfoVo communicationInfoVo) {
        CommunicationInfoEntity communicationInfoEntity = new CommunicationInfoEntity();
        BeanUtils.copyProperties(communicationInfoVo, communicationInfoEntity);
        communicationInfoEntity.setCreateTime(new Date());
        CommunicationInfoEntity save = communicationMapper.save(communicationInfoEntity);

        /**
         * 调用通讯费用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 流程标识。
         */
        String processDefinitionKey = "projectCommunicationAppF";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", communicationInfoVo.getProjectName());
        stringObjectHashMap.put("application_fee", communicationInfoVo.getApplicationFee());
        stringObjectHashMap.put("remark", communicationInfoVo.getRemark());
        stringObjectHashMap.put("communication_id", save.getCommunicationId());
        stringObjectHashMap.put("cost_type", "通讯费");
        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);

        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, null, masterData, null, null).getData();
        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("通讯费");
        UserInfoEntity userInfoEntity = findUser(communicationInfoVo.getCreateUser());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(communicationInfoVo.getProjectName());
        costInfoEntity.setPerformAmount(communicationInfoVo.getTotalMoney());
        costInfoEntity.setHappenDate(communicationInfoVo.getCreateDate());
        costInfoEntity.setPaymentDate(communicationInfoVo.getEndDate());
        costInfoEntity.setProposer(communicationInfoVo.getCreateUser());
        costInfoEntity.setApplyCostId(save.getCommunicationId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg addpurchase(PurchaseInfoVo purchaseInfoVo) {
        PurchaseInfoEntity purchaseInfoEntity = new PurchaseInfoEntity();
        BeanUtils.copyProperties(purchaseInfoVo, purchaseInfoEntity);
        purchaseInfoEntity.setPurchaseMessage(JSON.toJSONString(purchaseInfoVo.getPurchaseMessage()));
        purchaseInfoEntity.setCreateTime(new Date());
        PurchaseInfoEntity save = purchaseInfoMapper.save(purchaseInfoEntity);

        /**
         * 调用采购费用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 项目采购费用报销审批流程--流程标识。
         */
        String processDefinitionKey = "projectPurchaseApp";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", purchaseInfoVo.getProjectName());
        stringObjectHashMap.put("purchase_message", purchaseInfoVo.getPurchaseMessage().toString());
        stringObjectHashMap.put("purchase_date", purchaseInfoVo.getPurchaseDate());
        stringObjectHashMap.put("create_user", purchaseInfoVo.getCreateUser());
        stringObjectHashMap.put("purchase_id", save.getPurchaseId());
        stringObjectHashMap.put("cost_type", "采购费");
        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);
        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, null, masterData, null, null).getData();

        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("采购费");
        UserInfoEntity userInfoEntity = findUser(purchaseInfoVo.getCreateUser());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(purchaseInfoVo.getProjectName());
        costInfoEntity.setPerformAmount(purchaseInfoVo.getTotalMoney());
        costInfoEntity.setHappenDate(purchaseInfoVo.getPurchaseDate());
        costInfoEntity.setProposer(purchaseInfoVo.getCreateUser());
        costInfoEntity.setApplyCostId(save.getPurchaseId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg addRest(RestsInfoVo restsInfoVo) {
        RestsInfoEntity restsInfoEntity = new RestsInfoEntity();
        BeanUtils.copyProperties(restsInfoVo, restsInfoEntity);
        restsInfoEntity.setRestsMessage(JSON.toJSONString(restsInfoVo.getRestsMessage()));
        restsInfoEntity.setCreateTime(new Date());
        RestsInfoEntity save = restInfoMapper.save(restsInfoEntity);
        /**
         * 调用项目其他费用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 项目其他费用报销审批流程--流程标识。
         */
        String processDefinitionKey = "projeRestsInfoApp";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", restsInfoEntity.getProjectName());
        stringObjectHashMap.put("rests_message", restsInfoEntity.getRestsMessage().toString());
        stringObjectHashMap.put("total_money", restsInfoVo.getTotalMoney());
        stringObjectHashMap.put("create_date", restsInfoVo.getCreateDate());
        stringObjectHashMap.put("create_user", restsInfoVo.getCreateUser());
        stringObjectHashMap.put("id", save.getRestsId());
        stringObjectHashMap.put("cost_type", "其他费用");
        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);
        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, null, masterData, null, null).getData();

        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("其他费用");
        UserInfoEntity userInfoEntity = findUser(restsInfoVo.getCreateUser());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(restsInfoVo.getProjectName());
        costInfoEntity.setPerformAmount(restsInfoVo.getTotalMoney());
        costInfoEntity.setHappenDate(restsInfoVo.getCreateDate());
        costInfoEntity.setProposer(restsInfoVo.getCreateUser());
        costInfoEntity.setApplyCostId(save.getRestsId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg trafficExpenseApply(TrafficExpenseVo trafficExpenseVo) {
        TrafficExpenseEntity trafficExpense = new TrafficExpenseEntity();
        BeanUtils.copyProperties(trafficExpenseVo, trafficExpense);
        trafficExpense.setSubject(JSON.toJSONString(trafficExpenseVo.getSubject()));
        trafficExpense.setCreateTime(new Date());
        TrafficExpenseEntity save = trafficExpenseMapper.save(trafficExpense);
        /**
         * 调用项目交通费用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 项目交通费用报销审批流程--流程标识。
         */
        String processDefinitionKey = "projectTrafficExpenseApp";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", trafficExpenseVo.getProjectName());
        stringObjectHashMap.put("applicant", trafficExpenseVo.getApplicant());
        stringObjectHashMap.put("subject", trafficExpenseVo.getSubject().toString());
        stringObjectHashMap.put("total_money", trafficExpenseVo.getTotalMoney());
        stringObjectHashMap.put("id", save.getId());
        stringObjectHashMap.put("cost_type", "交通费");
        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);
        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, null, masterData, null, null).getData();

        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("交通费");
        UserInfoEntity userInfoEntity = findUser(trafficExpenseVo.getApplicant());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(trafficExpenseVo.getProjectName());
        costInfoEntity.setPerformAmount(trafficExpenseVo.getTotalMoney());
        costInfoEntity.setProposer(trafficExpenseVo.getApplicant());
        costInfoEntity.setApplyCostId(save.getId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }

    @Override
    public ResMsg repastExpenseApply(RepastExpenseVo repastExpenseVo) {
        RepastExpenseEntity repastExpense = new RepastExpenseEntity();
        BeanUtils.copyProperties(repastExpenseVo, repastExpense);
        repastExpense.setSubject(JSON.toJSONString(repastExpenseVo.getSubject()));
        repastExpense.setCreateTime(new Date());
        RepastExpenseEntity save = repastExpenseMapper.save(repastExpense);
        /**
         * 调用项目餐饮用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 项目餐饮费用报销审批流程--流程标识。
         */
        String processDefinitionKey = "projectRepastExpenseApp";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", repastExpenseVo.getProjectName());
        stringObjectHashMap.put("applicant", repastExpenseVo.getApplicant());
        stringObjectHashMap.put("subject", repastExpenseVo.getSubject().toString());
        stringObjectHashMap.put("total_money", repastExpenseVo.getTotalMoney());
        stringObjectHashMap.put("id", save.getId());
        stringObjectHashMap.put("cost_type", "餐饮费");
        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);
        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, null, masterData, null, null).getData();

        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("餐饮费");
        UserInfoEntity userInfoEntity = findUser(repastExpenseVo.getApplicant());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(repastExpenseVo.getProjectName());
        costInfoEntity.setPerformAmount(repastExpenseVo.getTotalMoney());
        costInfoEntity.setProposer(repastExpenseVo.getApplicant());
        costInfoEntity.setApplyCostId(save.getId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }


    @Override
    public ResMsg travelExpenseApply(TravelExpenseVo travelExpenseVo) {
        TravelExpenseEntity travelExpense = new TravelExpenseEntity();
        BeanUtils.copyProperties(travelExpenseVo, travelExpense);
        travelExpense.setSubject(JSON.toJSONString(travelExpenseVo.getSubject()));
        travelExpense.setCreateTime(new Date());
        TravelExpenseEntity save = travelExpenseMapper.save(travelExpense);
        /**
         * 调用项目差旅费用申请流程的启动
         * 获取开始节点之后的第一个任务节点的数据
         * param processDefinitionKey 项目差旅费用报销审批流程--流程标识。
         */
        String processDefinitionKey = "projectTravelExpenseApp";
        ResponseResult<FlowEntry> flowEntryResult = flowTaskStart.verifyAndGetFlowEntry(processDefinitionKey);
        if (!flowEntryResult.isSuccess()) {
            return ResMsg.newInstance(CodeEnum.FAIL, flowEntryResult);
        }
        FlowEntryPublish flowEntryPublish = flowEntryResult.getData().getMainFlowEntryPublish();
        String initTaskInfo = flowEntryPublish.getInitTaskInfo();
        com.ankaibei.workFlow.common.flow.vo.TaskInfoVo taskInfo = StrUtil.isBlank(initTaskInfo)
                ? null : JSON.parseObject(initTaskInfo, TaskInfoVo.class);
        if (taskInfo != null) {
            String loginName = TokenData.takeFromRequest().getLoginName();
            taskInfo.setAssignedMe(StrUtil.equalsAny(
                    taskInfo.getAssignee(), loginName, FlowConstant.START_USER_NAME_VAR));
        }
        //用户在表单页面配置的操作按钮
        FlowTaskCommentDto flowTaskCommentDto = new FlowTaskCommentDto();
        //提交后进行到第二个节点
        flowTaskCommentDto.setApprovalType("agree");
        //将申请信息添加到流程引擎控制的表中，并添加关联
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("project_name", travelExpenseVo.getProjectName());
        stringObjectHashMap.put("applicant", travelExpenseVo.getApplicant());
        stringObjectHashMap.put("subject", travelExpenseVo.getSubject().toString());
        stringObjectHashMap.put("total_money", travelExpenseVo.getTotalMoney());
        stringObjectHashMap.put("begin_time", travelExpenseVo.getBeginTime());
        stringObjectHashMap.put("end_time", travelExpenseVo.getEndTime());
        stringObjectHashMap.put("start_site", travelExpenseVo.getStartSite());
        stringObjectHashMap.put("end_site", travelExpenseVo.getEndSite());
        stringObjectHashMap.put("accommodation_fee", travelExpenseVo.getAccommodationFee());
        stringObjectHashMap.put("accommodation_days", travelExpenseVo.getAccommodationDays());
        stringObjectHashMap.put("total_fee", travelExpenseVo.getTotalFee());
        stringObjectHashMap.put("accommodation_remark", travelExpenseVo.getAccommodationRemark());
        stringObjectHashMap.put("accommodation_person", travelExpenseVo.getAccommodationPerson());
        stringObjectHashMap.put("repast_fee", travelExpenseVo.getRepastFee());
        stringObjectHashMap.put("repast_remark", travelExpenseVo.getRepastRemark());
        stringObjectHashMap.put("repast_person", travelExpenseVo.getRepastPerson());

        stringObjectHashMap.put("id", save.getId());
        stringObjectHashMap.put("cost_type", "差旅费");

        JSONObject masterData = (JSONObject) JSONObject.toJSON(stringObjectHashMap);
        HashMap<String, Object> taskVariableDataMap = new HashMap<>();

        //流程的任务变量
        taskVariableDataMap.put("accommodationDays", travelExpenseVo.getAccommodationDays());
        JSONObject taskVariableData = (JSONObject) JSONObject.toJSON(taskVariableDataMap);
        JSONObject copyData = null;
        Map<String, String> map = flowTaskStart.startAndTakeUserTaskSS(processDefinitionKey, flowTaskCommentDto, taskVariableData, masterData, null, copyData).getData();

        CostInfoEntity costInfoEntity = new CostInfoEntity();
        costInfoEntity.setCostType("差旅费");
        UserInfoEntity userInfoEntity = findUser(travelExpenseVo.getApplicant());
        costInfoEntity.setRelevanceUser(userInfoEntity != null ? userInfoEntity.getShowName() : null);
        costInfoEntity.setProjectName(travelExpenseVo.getProjectName());
        costInfoEntity.setPerformAmount(travelExpenseVo.getTotalMoney());
        costInfoEntity.setProposer(travelExpenseVo.getApplicant());
        costInfoEntity.setApplyCostId(save.getId());
        costInfoEntity.setProcessInstanceId(map.get("processInstanceId"));
        costInfoEntity.setProcessDefinitionId(map.get("processDefinitionId"));
        costInfoEntity.setFlowTaskStatus(FlowTaskStatus.SUBMITTED);
        costInfoService.addCostInfo(costInfoEntity);

        return new ResMsg(CodeEnum.OK);
    }

    /**
     * 查询用户信息
     */
    private UserInfoEntity findUser(String userId) {
        Optional<UserInfoEntity> byId = userInfoMapper.findById(userId);
        return byId.orElse(null);
    }
}
