/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.flowable.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.extension.domain.NodeSetting;
import com.jeeplus.extension.service.NodeSettingService;
import com.jeeplus.flowable.model.ActRuTaskInfo;
import com.jeeplus.flowable.model.Flow;
import com.jeeplus.flowable.model.MyNoticeList;
import com.jeeplus.flowable.model.TaskComment;
import com.jeeplus.flowable.service.FlowTaskService;
import com.jeeplus.flowable.service.MyNoticeService;
import com.jeeplus.flowable.vo.HisTaskVo;
import com.jeeplus.flowable.vo.ProcessVo;
import com.jeeplus.mail.feign.IMailApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.TenantUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 流程个人任务相关Controller
 *
 * @author jeeplus
 * @version 2021-09-03
 */
@RestController
@RequestMapping("/flowable/task")
public class FlowableTaskController {

    @Autowired
    private MyNoticeService noticeServicel;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RuntimeService runtimeService;


    @Autowired
    private TaskService taskService;

    @Autowired
    private NodeSettingService nodeSettingService;

    @Autowired
    private IUserApi userApi;

    @Autowired
    private IMailApi mailApi;

    @GetMapping("todo")
    public ResponseEntity todoListData(Page <ProcessVo> page, Flow flow) {
        page = flowTaskService.todoList ( page, flow );
        return ResponseEntity.ok ( page );
    }

    @GetMapping("getList")
    @ResponseBody
    public List<Map<String, Object>> getList(Page <ProcessVo> page, Flow flow,HttpServletRequest request){
        String cookie = request.getHeader("cookie");
        page = flowTaskService.todoList2 ( page, flow,cookie );
        for (ProcessVo record : page.getRecords()) {
            record.setBelongProject("sz");
            record.setType("212");
        }
        List<Map<String, Object>> maps = new ArrayList<>();
        if (StringUtils.isNotBlank(flow.getBelongProject()) && "sz".equals(flow.getBelongProject())) {
            JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(
                    JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.SkipTransientField, false);
            maps.addAll((List) JSONArray.parseArray(JSON.toJSONString(page.getRecords())));
        }
        return maps;
    }

    /**
     * 获取已办任务
     *
     * @return
     */
    @GetMapping("historic")
    public ResponseEntity historicListData(Page <HisTaskVo> page, Flow flow) {
        page = flowTaskService.historicList ( page, flow );
        return ResponseEntity.ok ( page );
    }
    @GetMapping("getBackLogList")
    @ResponseBody
    public List<Map<String, Object>> getBackLogList(Page <HisTaskVo> page, Flow flow,HttpServletRequest request){
        String cookie = request.getHeader("cookie");
        page = flowTaskService.getBackLogList ( page, flow,cookie );
        for (HisTaskVo record : page.getRecords()) {
            record.setBelongProject("sz");
            record.setDefKeyCcpm("212");
        }
        List<Map<String, Object>> maps = new ArrayList<>();
        if (StringUtils.isNotBlank(flow.getBelongProject()) && "sz".equals(flow.getBelongProject())) {
            JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(
                    JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.SkipTransientField, false);
            maps.addAll((List) JSONArray.parseArray(JSON.toJSONString(page.getRecords())));
        }
        return maps;
    }


    /**
     * 获取我的申请列表
     *
     * @return
     */
    @GetMapping("myApplyed")
    public ResponseEntity myApplyedListData(Page <ProcessVo> page, Flow flow) throws Exception {
        page = flowTaskService.getMyStartedProcIns ( userApi.getByToken ( TokenProvider.getCurrentToken ( ) ), page, flow );
        return ResponseEntity.ok ( page );
    }


    /**
     * 获取任务流转历史列表
     */
    @GetMapping("historicTaskList")
    public ResponseEntity historicTaskList(Flow flow) throws Exception {
        List <Flow> historicTaskList = flowTaskService.historicTaskList ( flow.getProcInsId ( ) );
        return ResponseEntity.ok ( historicTaskList );
    }


    /**
     * 获取流程表单
     */
    @GetMapping("getTaskDef")
    public ResponseEntity getTaskDef(Flow flow) {
        // 获取流程XML上的表单KEY
        String formKey = flowTaskService.getFormKey ( flow.getProcDefId ( ), flow.getTaskDefKey ( ) );

        NodeSetting typeNode = nodeSettingService.queryByKey ( flow.getProcDefKey ( ), flow.getTaskDefKey ( ), "formType" );
        NodeSetting ReadOnlyNode = nodeSettingService.queryByKey ( flow.getProcDefKey ( ), flow.getTaskDefKey ( ), "formReadOnly" );
        String formType = "1";
        boolean formReadOnly = false;
        if ( typeNode != null ) {
            formType = typeNode.getValue ( );
            formReadOnly = "true".equals ( ReadOnlyNode.getValue ( ) );
        } else {
            if ( StringUtils.isBlank ( formKey ) ) {
                formType = "1";
            } else if ( formKey.indexOf ( "/" ) >= 0 ) {
                formType = "2";
            }
        }

        // 获取流程实例对象
        if ( flow.getProcInsId ( ) != null ) {
            if ( flowTaskService.getProcIns ( flow.getProcInsId ( ) ) != null ) {
                flow.setProcIns ( flowTaskService.getProcIns ( flow.getProcInsId ( ) ) );
            } else {
                flow.setFinishedProcIns ( flowTaskService.getFinishedProcIns ( flow.getProcInsId ( ) ) );
            }
        }

        flow.setFormUrl ( formKey );
        flow.setFormReadOnly ( formReadOnly );
        flow.setFormType ( formType );
        return ResponseEntity.ok ( flow );
    }


    /**
     * 启动流程
     */
    @PostMapping("start")
    public ResponseEntity start(@RequestBody Flow flow) {
        String procInsId = flowTaskService.startProcess ( flow.getProcDefKey ( ), flow.getBusinessTable ( ), flow.getBusinessId ( ), flow.getTitle ( ),flow.getRecordType(), flow.getProcDefId() );

        //指定下一步处理人
        if ( StringUtils.isNotBlank ( flow.getAssignee ( ) ) ) {
            Task task = taskService.createTaskQuery ( ).processInstanceId ( procInsId ).active ( ).singleResult ( );
            if(task != null){
                String[] split = flow.getAssignee().split(",");
                if (split.length == 1) {
                    taskService.setAssignee(task.getId(), flow.getAssignee ());
                } else {
                    for (String i : split) {
                        taskService.addCandidateUser(task.getId(),i);
                    }
                }
            }
        }
        return ResponseEntity.ok ( procInsId );
    }

    /**
     * 启动流程
     */
    @PostMapping("startForFen")
    public String startForFen(@RequestBody Map<String ,String > map) {

        String procDefKey = map.get("procDefKey");
        String procDefId = map.get("procDefId");
        String businessTable = map.get("businessTable");
        String businessId = map.get("businessId");
        String title = map.get("title");
        String recordType = map.get("recordType");
        String assignee = map.get("assignee");
        String userId = map.get("userId");

        if ( StrUtil.isBlank ( procDefKey ) && StrUtil.isNotBlank ( procDefId ) ) {
            procDefKey = StrUtil.split ( procDefId, ":" ).get ( 0 );
        }

        String procInsId = flowTaskService.startProcessForFen ( procDefKey, businessTable, businessId, title,recordType, procDefId ,userId);

        //指定下一步处理人
        if ( StringUtils.isNotBlank ( assignee ) ) {
            Task task = taskService.createTaskQuery ( ).processInstanceId ( procInsId ).active ( ).singleResult ( );
            if(task != null){
                String[] split = assignee.split(",");
                if (split.length == 1) {
                    taskService.setAssignee(task.getId(), assignee);
                } else {
                    for (String i : split) {
                        taskService.addCandidateUser(task.getId(),i);
                    }
                }
            }
        }
        return procInsId ;
    }

    /**
     * 启动流程
     */
    @PostMapping("startForFenNew")
    public Map<String ,String > startForFenNew(@RequestBody Map<String ,Map<String ,String > > map) {

        Map<String ,String > map1 = new HashMap();

        for (Map.Entry<String ,Map<String ,String >> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.equals("底稿")){
                String procInsId = getProcInsId(entry);
                map1.put("底稿", procInsId);
            } else if (key.equals("电脑")){
                String procInsId = getProcInsId(entry);
                map1.put("电脑", procInsId);
            } else if (key.equals("应收账款")){
                String procInsId = getProcInsId(entry);
                map1.put("应收账款", procInsId);
            } else if (key.equals("社保欠款")){
                String procInsId = getProcInsId(entry);
                map1.put("社保欠款", procInsId);
            } else if (key.equals("备用金")){
                String procInsId = getProcInsId(entry);
                map1.put("备用金", procInsId);
            } else if (key.equals("实习")){
                String procInsId = getProcInsId(entry);
                map1.put("实习", procInsId);
            } else if (key.equals("入职")){
                String procInsId = getProcInsId(entry);
                map1.put("入职", procInsId);
            } else if (key.equals("完善个人信息")){
                String procInsId = getProcInsId(entry);
                map1.put("完善个人信息", procInsId);
            }
        }
        return map1 ;
    }

    public String getProcInsId(Map.Entry<String ,Map<String ,String >> entry){
        Map<String ,String > valueMap = entry.getValue();
        String procDefKey = valueMap.get("procDefKey");
        String procDefId = valueMap.get("procDefId");
        String businessTable = valueMap.get("businessTable");
        String businessId = valueMap.get("businessId");
        String title = valueMap.get("title");
        String recordType = valueMap.get("recordType");
        String assignee = valueMap.get("assignee");
        String userId = valueMap.get("userId");
        if ( StrUtil.isBlank ( procDefKey ) && StrUtil.isNotBlank ( procDefId ) ) {
            procDefKey = StrUtil.split ( procDefId, ":" ).get ( 0 );
        }

        String procInsId = flowTaskService.startProcessForFen ( procDefKey, businessTable, businessId, title,recordType, procDefId ,userId);

        //指定下一步处理人
        if ( StringUtils.isNotBlank ( assignee ) ) {
            Task task = taskService.createTaskQuery ( ).processInstanceId ( procInsId ).active ( ).singleResult ( );
            if(task != null){
                String[] split = assignee.split(",");
                if (split.length == 1) {
                    taskService.setAssignee(task.getId(), assignee);
                } else {
                    for (String i : split) {
                        taskService.addCandidateUser(task.getId(),i);
                    }
                }
            }
        }
        return procInsId;
    }

    /**
     * 设置下一节点审核人
     */
    @PostMapping("updateAssignee")
    public String updateAssignee(@RequestBody Map<String ,String > map) {

        String procInsId = map.get("procInsId");
        String assignee = map.get("assignee");

        //指定下一步处理人
        if ( StringUtils.isNotBlank ( assignee ) ) {
            Task task = taskService.createTaskQuery ( ).processInstanceId ( procInsId ).active ( ).singleResult ( );
            if(task != null){
                String[] split = assignee.split(",");
                if (split.length == 1) {
                    taskService.setAssignee(task.getId(), assignee);
                } else {
                    for (String i : split) {
                        taskService.addCandidateUser(task.getId(),i);
                    }
                }
            }
        }
        return procInsId ;
    }

    /**
     * 签收任务
     */
    @PostMapping("claim")
    public ResponseEntity claim(@RequestBody Flow flow) {
        String userId = userApi.getByToken ( TokenProvider.getCurrentToken ( ) ).getId ( );//ObjectUtils.toString(UserUtils.getUser().getId());
        flowTaskService.claim ( flow.getTaskId ( ), userId );
        return ResponseEntity.ok ( "签收成功!" );
    }

    /**
     * 完成任务
     */
    @PostMapping(value = "complete")
    public ResponseEntity complete(@RequestBody Flow flow) {
        flowTaskService.complete ( flow, flow.getVars ( ).getVariableMap ( ) );
        return ResponseEntity.ok ( "完成任务!" );
    }

    /**
     * 读取流程历史数据，用于渲染流程图
     */
    @GetMapping("getFlowChart")
    public Map getFlowChart(String processInstanceId) throws Exception {
        return flowTaskService.getDiagram ( processInstanceId );
    }

    /**
     * 删除任务
     *
     * @param taskId 流程实例ID
     * @param reason 删除原因
     */
    @DeleteMapping("deleteTask")
    public ResponseEntity deleteTask(String taskId, String reason) {
        if ( StringUtils.isBlank ( reason ) ) {
            return ResponseEntity.badRequest ( ).body ( "请填写删除原因" );
        } else {
            flowTaskService.deleteTask ( taskId, reason );
            return ResponseEntity.ok ( "删除任务成功，任务ID=" + taskId );
        }
    }

    /**
     * 加签
     */
    @PostMapping("addSignTask")
    public ResponseEntity addSignTask(String taskId, String userIds, String comment, Boolean flag) throws Exception {
        flowTaskService.addSignTask ( taskId, Arrays.asList ( userIds.split ( "," ) ), comment, flag );
        return ResponseEntity.ok ( "加签成功!" );
    }

    ;


    /**
     * 审批
     *
     * @param flow
     */
    @PostMapping("audit")
    public ResponseEntity auditTask(HttpServletRequest request, Flow flow) {
        Map <String, Object> vars = Maps.newHashMap ( );
        Map <String, String[]> map = request.getParameterMap ( );
        for (Map.Entry <String, String[]> entry : map.entrySet ( )) {
            if ( entry.getKey ( ).startsWith ( "vars." ) ) {
                String key = entry.getKey ( ).substring ( 5 );
                String value = entry.getValue ( )[0];
                if ( "true".equals ( value ) || "false".equals ( value ) ) {
                    vars.put ( key, Boolean.valueOf ( value ).booleanValue ( ) );
                } else {
                    vars.put ( key, value );
                }
            }
        }

        flowTaskService.auditSave ( flow, vars );
        //指定下一步处理人
        if ( StringUtils.isNotBlank ( flow.getAssignee ( ) ) ) {
            Task task = taskService.createTaskQuery ( ).processInstanceId ( flow.getProcInsId ( ) ).active ( ).singleResult ( );
            if ( task != null ) {
                taskService.setAssignee ( task.getId ( ), flow.getAssignee ( ) );
            }
        }
        return ResponseEntity.ok ( flow.getProcInsId ( ) );
    }

    /**
     * 取回已经执行的任务，只有在下一任务节点未执行或者未签收时才能取回
     */
    @RequestMapping("callback")
    public ResponseEntity callback(@Param("preTaskId") String preTaskId,
                                   @Param("currentTaskId") String currentTaskId,
                                   @Param("processInstanceId") String processInstanceId,
                                   @Param("preTaskDefKey") String preTaskDefKey,
                                   @Param("currentTaskDefKey") String currentTaskDefKey) {
        try {
            // 取得流程实例
            ProcessInstance instance = runtimeService
                    .createProcessInstanceQuery ( )
                    .processInstanceId ( processInstanceId )
                    .singleResult ( );
            if ( instance == null ) {
                return ResponseEntity.badRequest ( ).body ( "流程已经结束" );
            }

            //在已办任务列表中清除该任务信息
            historyService.deleteHistoricTaskInstance ( preTaskId );

            List currTasks = Lists.newArrayList ( );
            currTasks.add ( currentTaskDefKey );
            //回退到上一节点
            runtimeService.createChangeActivityStateBuilder ( )
                    .processInstanceId ( instance.getId ( ) )
                    .moveActivityIdsToSingleActivityId ( currTasks, preTaskDefKey ).changeState ( );
            historyService.deleteHistoricTaskInstance ( currentTaskId );
            return ResponseEntity.ok ( "取回成功" );
        } catch (Exception e) {
            e.printStackTrace ( );
            return ResponseEntity.badRequest ( ).body ( "流程取回失败，未知错误." );
        }
    }


    /**
     * 委托任务
     *
     * @param taskId 任务ID
     */
    @RequestMapping("delegate")
    public ResponseEntity delegate(String taskId, String userId) {
        if ( StringUtils.isBlank ( taskId ) || StringUtils.isBlank ( userId ) ) {
            return ResponseEntity.badRequest ( ).body ( "参数异常" );
        }
        taskService.setOwner ( taskId, userApi.getByToken ( TokenProvider.getCurrentToken ( ) ).getId ( ) );// 委托人为任务的拥有者
        taskService.delegateTask ( taskId, userId );
        return ResponseEntity.ok ( "委托成功" );
    }

    /**
     * 取消签收任务
     *
     * @param taskId 任务ID
     */
    @RequestMapping("unclaim")
    public ResponseEntity unclaim(String taskId) {
        taskService.unclaim ( taskId );
        return ResponseEntity.ok ( "取消签收成功" );
    }

    /**
     * 转派任务
     *
     * @param taskId 任务ID
     * @param userId 接收人
     */
    @PutMapping("transfer")
    public ResponseEntity transferTask(String taskId, String userId) {
        if ( StringUtils.isBlank ( userId ) || StringUtils.isBlank ( taskId ) ) {
            return ResponseEntity.badRequest ( ).body ( "转派失败, 参数异常" );
        }
        // 设置当前流程任务办理人
        Authentication.setAuthenticatedUserId ( userApi.getByToken ( TokenProvider.getCurrentToken ( ) ).getId ( ) );
        taskService.setAssignee ( taskId, userId );
        return ResponseEntity.ok ( "转派成功!" );
    }

    /**
     * 获取可退回任务节点
     *
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/backNodes")
    public ResponseEntity backNodes(@RequestParam String taskId) {
        List <Flow> nodes = flowTaskService.getBackNodes ( taskId );
        return ResponseEntity.ok ( nodes );
    }

    /**
     * 驳回任务到指定节点
     */
    @RequestMapping(value = "/back")
    public ResponseEntity back(String backTaskDefKey, String taskId, TaskComment comment) {
        String procInsId = flowTaskService.backTask ( backTaskDefKey, taskId, comment );
        return ResponseEntity.ok ( procInsId );
    }

    /**
     * 催办任务
     *
     * @param taskId 任务ID
     */
    @PostMapping("urge")
    public ResponseEntity <String> urge(String taskId, String content) {
        List <UserDTO> assignList = Lists.newArrayList ( );
        taskService.getIdentityLinksForTask ( taskId ).forEach ( identityLink -> {
            assignList.add ( userApi.getById ( identityLink.getUserId ( ) ) );
        } );

        mailApi.sendEmail ( assignList, "你收到一条流程催办任务!", content + "<p><a href=\"/#/flowable/task/TodoList\" target=\"_blank\">点击处理</a><br></p>" );
        return ResponseEntity.ok ( "催办成功!" );
    }

    @GetMapping("getTaskAuditUsers")
    public List<String> getTaskAuditUsers(String taskId) {
        return flowTaskService.getTaskAuditUsers(taskId);
    }

    @GetMapping("getCurrentTaskName")
    public String getCurrentTaskName(String processInstanceId) {
        return flowTaskService.getCurrentTaskName(processInstanceId);
    }

    @GetMapping("auditByProcInsIdAndFlag")
    public void auditByProcInsIdAndFlag(String procInsId,String flag,String assignee,String comm) throws Exception {
        flowTaskService.auditByProcInsIdAndFlag(procInsId,flag,assignee,comm);
    }


    @GetMapping("checkIsLastTask")
    public boolean checkIsLastTask(String procInsId) {
        return flowTaskService.checkIsLastTask(procInsId);
    }

    /**
     * 获取流程表单
     */
    @GetMapping("getTaskDefInfo")
    public ResponseEntity getTaskDefInfo(Flow flow) {
        if(StringUtils.isNotBlank(flow.getTaskId()) && StringUtils.isBlank(flow.getTaskName()) && StringUtils.isBlank(flow.getTaskDefKey())){
            ActRuTaskInfo taskInfoByTaskId = flowTaskService.getTaskInfoByTaskId(flow.getTaskId());
            if(null != taskInfoByTaskId){
                flow.setTaskId(taskInfoByTaskId.getId());
                flow.setProcInsId(taskInfoByTaskId.getProcInstId());
                flow.setTaskName(taskInfoByTaskId.getName());
                flow.setTaskDefKey(taskInfoByTaskId.getTaskDefKey());
            }
            //当前任务信息
            Task task =taskService.createTaskQuery().processInstanceId(flow.getProcInsId()).active().singleResult();
            if(null != task){
                if(StringUtils.isNotBlank(task.getProcessDefinitionId())){
                    flow.setProcDefId(task.getProcessDefinitionId());
                }
                if(null != task.getProcessVariables ()){
                    String title = (String) task.getProcessVariables().get("title");
                    if(StringUtils.isNotBlank(title)){
                        flow.setTitle(title);
                    }else{
                        flow.setTitle("");
                    }
                }
            }

            Process process = SpringUtil.getBean (RepositoryService.class).getBpmnModel (taskInfoByTaskId.getProcDefId ()).getMainProcess ();
            if(null != process && StringUtils.isNotBlank(process.getId())){
                flow.setProcDefKey(process.getId());
            }
        }
        // 获取流程XML上的表单KEY
        String formKey = flowTaskService.getFormKey(flow.getProcDefId(), flow.getTaskDefKey());

        NodeSetting typeNode = nodeSettingService.queryByKey (flow.getProcDefKey(), flow.getTaskDefKey(), "formType");
        NodeSetting ReadOnlyNode = nodeSettingService.queryByKey (flow.getProcDefKey(), flow.getTaskDefKey(), "formReadOnly");
        String formType = "1";
        boolean formReadOnly = false;
        if(typeNode != null){
            formType = typeNode.getValue ();
            formReadOnly = "true".equals(ReadOnlyNode.getValue ());
        }else{
            if(StringUtils.isBlank(formKey)){
                formType = "1";
            }else if(formKey.indexOf("/")>=0){
                formType = "2";
            }
        }

        // 获取流程实例对象
        if (flow.getProcInsId() != null) {
            if (flowTaskService.getProcIns(flow.getProcInsId()) != null) {
                flow.setProcIns(flowTaskService.getProcIns(flow.getProcInsId()));
            } else {
                flow.setFinishedProcIns(flowTaskService.getFinishedProcIns(flow.getProcInsId()));
            }
        }

        flow.setFormUrl(formKey);
        flow.setFormReadOnly(formReadOnly);
        flow.setFormType(formType);
        return ResponseEntity.ok (flow);
    }

    /**
     * 根据procInsId获取当前流程节点名称
     * @param procInsId
     * @return
     */
    @GetMapping("/getTaskNameByProcInsId")
    public ResponseEntity<String> getTaskNameByProcInsId(@RequestParam("procInsId") String procInsId) {
        String res = getTaskName(procInsId);
        return ResponseEntity.ok(res);
    }

    /**
     * 根据procInsId获取当前流程节点名称
     * @param procInsId
     * @return
     */
    public String getTaskName(String procInsId) {
        if (StringUtils.isNotBlank(procInsId)) {
            Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
            if (task!=null) {
                //获取流程发布Id信息
                String definitionId = runtimeService.createProcessInstanceQuery().processInstanceId(procInsId).singleResult().getProcessDefinitionId();
                //获取bpm对象
                BpmnModel bpmnModel = SpringUtil.getBean (RepositoryService.class).getBpmnModel(definitionId);
                //传节点定义key 获取当前节点
                FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(task.getTaskDefinitionKey());
                if (ObjectUtil.isNotEmpty(flowNode)) {
                    if (StringUtils.isNotBlank(flowNode.getName())) {
                        return flowNode.getName();
                    }
                }
            }
        }
        return "";
    }

    @GetMapping("getTaskIdByprocInstId")
    public String getTaskIdByprocInstId(String procInsId){
        return flowTaskService.getTaskIdByprocInstId(procInsId);
    }

    /**
     * 获取当前节点信息
     * @param processInstanceId
     * @return
     */
    @GetMapping("getCurrentTask")
    public FlowNode getCurrentTask(String processInstanceId){
        return flowTaskService.getCurrentTask(processInstanceId);
    }

    /**
     * 获取流转历史任务列表
     *
     * @param procInsId 流程实例
     */
    @PostMapping("historicTaskList2")
    public Map historicTaskList2(String procInsId) throws Exception {
        return flowTaskService.historicTaskList2(procInsId);
    }

    /**
     * 获取流转历史任务列表
     *
     * @param procInsId 流程实例
     */
    @PostMapping("historicTaskList3")
    public Map historicTaskList3(String procInsId) throws Exception {
        return flowTaskService.historicTaskList3(procInsId);
    }

    /**
     * 根据通知信息查询数据
     * @param myNoticeList
     * @return
     */
    @GetMapping("getRepetitionCountBymyNotice")
    public MyNoticeList getRepetitionCountBymyNotice(MyNoticeList myNoticeList){
        return noticeServicel.getRepetitionCountBymyNotice(myNoticeList);
    }
    /**
     * 根据通知信息查询数据
     * @param myNoticeList
     * @return
     */
    @PostMapping("insertMyNotice")
    public void insertMyNotice(MyNoticeList myNoticeList){
        noticeServicel.insertMyNotice(myNoticeList);
    }
    /**
     * 根据通知信息查询数据
     * @param myNoticeList
     * @return
     */
    @PostMapping("updateMyNotice")
    public void updateMyNotice(MyNoticeList myNoticeList){
        noticeServicel.updateMyNotice(myNoticeList);
    }

    /**
     * 保存审核意见
     *
     * @param flow
     */
    @PostMapping("auditSave")
    public void auditSave(Flow flow, Map vars) {
        flowTaskService.auditSave(flow,vars);

    }


    @ApiOperation(value = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestBody Map<String ,String > map) throws Exception{
        //taskId
        String taskId = map.get("taskId");
        //标题
        String title = map.get("title");
        //流程id
        String defId = map.get("defId");
        //流程名称
        String taskName = map.get("taskName");
        //创建人
        String createUser = map.get("createUser");
        //创建时间
        String createTime = map.get("createTime");
        //通知人名称
        String noticeName = map.get("noticeName");
        //通知人id
        String noticeId = map.get("noticeId");
        //创建人id
        String createById = map.get("createById");
        return noticeServicel.add(taskId, title, defId, taskName, createUser, createTime, noticeName, noticeId,createById);
    }

    @RequestMapping(value = "/taskDispose", method = RequestMethod.POST)
    public String taskDispose(@RequestBody Map<String ,String > map) {
        String data = map.get("data");
        String currentToken = map.get("currentToken");
        return flowTaskService.taskDispose(data,currentToken);
    }

    @GetMapping("/list")
    @ApiOperation(value = "待办列表")
    public ResponseEntity noticeList(Page<MyNoticeList> page, String type, String title, String taskName) {
        Page<MyNoticeList> voPage = noticeServicel.noticeList(page, type, title, taskName);
        return ResponseEntity.ok (voPage);
    }

    @ApiOperation(value = "根据通知信息查询数据")
    @RequestMapping(value = "/insertMyNotice2", method = RequestMethod.POST)
    public void insertMyNotice2(@RequestBody Map<String ,String > map) throws Exception{
        String myNoticeInfo = map.get("myNoticeInfo");
        MyNoticeList jsonObject = JSON.parseObject(myNoticeInfo, new TypeReference<MyNoticeList>() {});
        noticeServicel.insertMyNotice(jsonObject);
    }

    @ApiOperation(value = "根据通知信息查询数据")
    @RequestMapping(value = "/updateMyNotice2", method = RequestMethod.POST)
    public void updateMyNotice2(@RequestBody Map<String ,String > map) throws Exception{
        String myNoticeInfo = map.get("myNoticeInfo");
        MyNoticeList jsonObject = JSON.parseObject(myNoticeInfo, new TypeReference<MyNoticeList>() {});
        noticeServicel.updateMyNotice(jsonObject);
    }

    @ApiOperation(value = "根据通知信息查询数据")
    @RequestMapping(value = "/getRepetitionCountBymyNoticeTitle", method = RequestMethod.POST)
    public String getRepetitionCountBymyNoticeTitle(@RequestBody Map<String ,String > map) throws Exception{
        String myNoticeInfo = map.get("myNoticeInfo");
        MyNoticeList jsonObject = JSON.parseObject(myNoticeInfo, new TypeReference<MyNoticeList>() {});
        MyNoticeList noticeList = noticeServicel.getRepetitionCountBymyNoticeTitle(jsonObject);
        return JSON.toJSONString(noticeList);
    }

}
