springboot异步接口

1服务接口类

public interface AsyncService {
    public Future<String> taskAddWorkFlowNodes(Map<String, List<BladePower>> workFlowPower);
    public Future<String> task2();
    public Future<String> task3();
}

2.接口实现类

package org.springblade.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.system.constant.Constant;
import org.springblade.system.entity.BladePower;
import org.springblade.system.entity.BladeWorkflow;
import org.springblade.system.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

/**
 * @Autor zhangjiawen
 * @Date: 2020/5/15 13:20
 */
@Service
@AllArgsConstructor
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    private IBladePowerService powerService;
    private IBladeWorkflowService workflowService;
    @Override
    @Async
    public Future<String> taskAddWorkFlowNodes(Map<String, List<BladePower>> workFlowPower) {
        asyncAdd(workFlowPower);
        return null;
    }

    @Override
    public Future<String> task2() {
        return null;
    }

    @Override
    public Future<String> task3() {
        return null;
    }

    public void asyncAdd(Map<String, List<BladePower>> workFlowPower){

        //返回成功异步处理
        //判断流程id是否存在
        Set<Map.Entry<String, List<BladePower>>> entries = workFlowPower.entrySet();
        String workFlowId = entries.iterator().next().getKey();
        int count = workflowService.count(Wrappers.<BladeWorkflow>query().lambda().eq(BladeWorkflow::getFlowId, workFlowId));
        List<BladePower> bladePowers = entries.iterator().next().getValue();
        if (bladePowers==null || bladePowers.size()==0){
            log.error("流程节点不能为空!workFlowId:"+workFlowId);
           return;
        }
        //不存在
        if(count==0){
            //添加整个流程
            bladePowers.forEach(power -> {
                power.setSort(System.currentTimeMillis());
                power.setPowerType(Constant.WORKFLOW_POWER_TYPE);
                power.setName(power.getNodeName());
                R r =  R.status(powerService.save(power));
                if(r.isSuccess()){
                    BladeWorkflow workflow=new BladeWorkflow();
                    BeanUtils.copyProperties(power,workflow);
                    R.status(workflowService.save(workflow));
                }
            } );
            log.info("工作流添加成功!workFlowId:"+workFlowId);

        }else{
            //存在
            //遍历工作流节点 判断节点是否存在

            bladePowers.forEach(power -> {
                int countNode = workflowService.count(Wrappers.<BladeWorkflow>query().lambda().eq(BladeWorkflow::getNodeId, power.getNodeId()));
                //不存在
                if (countNode==0){
                    //查询最大版本号
                    Integer maxVersion = workflowService.getMaxVersion(workFlowId);
                    //添加节点 最新版本号
                    power.setSort(System.currentTimeMillis());
                    power.setPowerType(Constant.WORKFLOW_POWER_TYPE);
                    power.setName(power.getNodeName());
                    R r=R.status(powerService.save(power));
                    if(r.isSuccess()){
                        BladeWorkflow workflow=new BladeWorkflow();
                        BeanUtils.copyProperties(power,workflow);
                        workflow.setVersion(maxVersion);
                        workflowService.save(workflow);
                    }
                }else{
                    //存在
                    //修改节点 最新版本号
                    //权限表无需更改
                    BladeWorkflow workflow=new BladeWorkflow();
                    BeanUtils.copyProperties(power,workflow);
                    workflowService.updateNode(workflow);
                }
            });
          log.info("工作流升级成功!workFlowId:"+workFlowId);
        }
    }

}

3controller

package org.springblade.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.IResultCode;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.api.ResultCode;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.constant.Constant;
import org.springblade.system.entity.*;
import org.springblade.system.service.*;
import org.springblade.system.user.vo.UserVO;
import org.springblade.system.vo.CheckedTreeVO;
import org.springblade.system.vo.GrantTreeVO;
import org.springblade.system.vo.GrantWorkFlowNodesVO;
import org.springblade.system.vo.WorkFlowVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

import static org.springblade.core.cache.constant.CacheConstant.MENU_CACHE;
import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

@RestController
@RequestMapping("/workflow")
@AllArgsConstructor
@Slf4j
public class WorkFlowNodesController {

   private AsyncService asyncService;

   private IBladeWorkflowService workflowService;

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增或修改工作流节点", notes = "传入 Map<String, List<BladePower>>")
    public R submit(@Valid @RequestParam String strWorkFlow) {
        if (StringUtils.isEmpty(strWorkFlow)){
            log.error("strWorkFlow不能为空!");
           return R.fail("strWorkFlow不能为空!");
        }
        Map<String, List<BladePower>> objectParam = null;
        try {
            objectParam = getObjectParam(strWorkFlow);
        } catch (IOException e) {
            e.printStackTrace();
        }
        asyncService.taskAddWorkFlowNodes(objectParam);
        return R.success("接收成功!");
    }




    private Map<String, List<BladePower>> getObjectParam(String params) throws IOException {
       // String string="{\"xxxxxxx\":[{\"nodeId\":\"asdfaxfg\",\"nodeName\":\"测试\",\"flowId\":\"xxxxxxx\",\"flowName\":\"测试\",\"tenantId\":\"00000\"},{\"nodeId\":\"asdfaxfg1\",\"nodeName\":\"测试1\",\"flowId\":\"xxxxxxx\",\"flowName\":\"测试1\",\"tenantId\":\"00000\"}]}";

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode originJson = objectMapper.readTree(params);
        Map<String, List<BladePower>> bladePowerMap=new HashMap<>();
        List<BladePower> list=new ArrayList<>();
        String key="";
        Iterator<String> stringIterator = originJson.fieldNames();
        if (stringIterator.hasNext()){
            key=stringIterator.next();
        }else {
            return null;
        }
        originJson.forEach(jsonNode ->{ System.out.println(jsonNode);
         //  key=jsonNode.
            jsonNode.forEach(e-> {
                BladePower bladePower=new BladePower();
                bladePower.setNodeId(String.valueOf(e.get("nodeId")).replace("\"",""));
                bladePower.setNodeName(String.valueOf(e.get("nodeName")).replace("\"",""));
                bladePower.setFlowId(String.valueOf(e.get("flowId")).replace("\"",""));
                bladePower.setFlowName(String.valueOf(e.get("flowName")).replace("\"",""));
                bladePower.setTenantId(String.valueOf(e.get("tenantId")).replace("\"",""));
                list.add(bladePower);
            });

        });
        bladePowerMap.put(key,list);
        return bladePowerMap;
    }
}

 

posted @ 2020-11-12 17:41  valar-dohaeris  阅读(1648)  评论(0编辑  收藏  举报