List<Map<String,String>> 结构 拓扑图 点 线 去重

该博客主要展示了如何处理包含List<Map<String,String>>结构的拓扑图中节点和边的去重问题。通过Java代码实现,包括合并节点Map,获取线列表并进行去重操作,以及构建拓扑图的过程。" 130859025,18873772,Python的%g格式说明符详解,"['Python开发', '编程语言', '格式化', '数据输出']
package com.yonyougov.dep.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyougov.bd.framework.annotation.AddOrModify;
import com.yonyougov.bd.framework.cron.CronModel;
import com.yonyougov.bd.framework.utils.UtilTools;
import com.yonyougov.dep.common.bean.ExchangeParam;
import com.yonyougov.dep.common.bean.ExchangeParam.Consumer;
import com.yonyougov.dep.common.bean.ExchangeParam.Exchange;
import com.yonyougov.dep.common.constants.DepConstant;
import com.yonyougov.dep.common.enums.AppIdentityTypeEnum;
import com.yonyougov.dep.common.enums.ExchangeStatusEnum;
import com.yonyougov.dep.common.enums.NodeTypeEnum;
import com.yonyougov.dep.common.service.IDepAgentService;
import com.yonyougov.dep.common.service.IDepApplicationService;
import com.yonyougov.dep.common.service.IDepConHistoryService;
import com.yonyougov.dep.common.service.IDepConsumerService;
import com.yonyougov.dep.common.service.IDepExchangeService;
import com.yonyougov.dep.common.service.IDepLocationService;
import com.yonyougov.dep.common.service.IDepProHistoryService;
import com.yonyougov.dep.common.service.IDepRulesService;
import com.yonyougov.dep.common.service.IDepTopologyService;
import com.yonyougov.dep.common.vo.DepAgentVO;
import com.yonyougov.dep.common.vo.DepApplicationVO;
import com.yonyougov.dep.common.vo.DepConHistoryVO;
import com.yonyougov.dep.common.vo.DepConsumerVO;
import com.yonyougov.dep.common.vo.DepExchangeVO;
import com.yonyougov.dep.common.vo.DepLocationVO;
import com.yonyougov.dep.common.vo.DepRulesVO;
import com.yonyougov.dep.common.vo.DepTopologyVO;
import com.yonyougov.dep.server.bean.NodeLocation;
import com.yonyougov.dep.server.bean.TopologyParamList;
import com.yonyougov.dep.server.service.IDepManagerService;
import com.yonyougov.dep.server.utils.PropertiesContext;

import lombok.Data;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author wenxing
 *
 */
@RestController
@RequestMapping(value="/api/v1/exchange")
public class ExchangeController {
    
    @Autowired
    private IDepConsumerService consumerService;
    
    @Autowired
    private IDepExchangeService exchangeService;
    
    @Autowired
    private IDepAgentService agentService;
    
    @Autowired
    private IDepApplicationService appService;
    
    @Autowired
    private IDepRulesService ruleService;
    
    @Autowired
    private IDepProHistoryService proHistoryService;
    
    @Autowired
    private IDepConHistoryService conHistoryService;
    
    @Autowired
    private IDepManagerService managerService;
    
    @Autowired
    private IDepTopologyService topologyService;
    
    @Autowired
    private IDepLocationService locationService;
    
    @RequestMapping(path="consumerInfo",method=RequestMethod.GET)
    public void consumerInfo() {
        DepConsumerVO con1 = new DepConsumerVO();
        con1.setStatus("ok11");
        //con1.setPk("a613caaa-e5f3-4de1-b26c-49446730defd");
        con1.setPkExchange("a613caaa-e5f3-4de1-b26c-49446730defd");
        con1.setPkApp("a613caaa-e5f3-4de1-b26c-49446730defd");
        con1.setPkAgent("a613caaa-e5f3-4de1-b26c-49446730defd");
        con1.setTopic("test11");
        DepConsumerVO con2 = new DepConsumerVO();
        //con2.setPk("a613caaa-e5f3-4de1-b26c-49446730deff");
        con2.setStatus("ok22");
        con2.setPkExchange("a613caaa-e5f3-4de1-b26c-49446730defd");
        con2.setPkApp("a613caaa-e5f3-4de1-b26c-49446730defd");
        con2.setPkAgent("a613caaa-e5f3-4de1-b26c-49446730defd");
        con2.setTopic("test22");
        DepConsumerVO con3 = new DepConsumerVO();
        //con3.setPk("a613caaa-we23-4de1-b26c-49446730defe");
        con3.setStatus("ok11");
        con3.setPkExchange("a613caaa-e5f3-4de1-b26c-49446730defd");
        con3.setPkApp("a613caaa-e5f3-4de1-b26c-49446730defd");
        con3.setPkAgent("a613caaa-e5f3-4de1-b26c-49446730defd");
        con3.setTopic("test11");
        
        DepExchangeVO exchangeVO = new DepExchangeVO();
        //exchangeVO.setPk("a613caaa-e5f3-4de1-b26c-49446730defd");
        exchangeVO.setStatus("test1");
        exchangeVO.setName("jeowf");
        //exchangeVO.setPkProapp("a613caaa-e5f3-4de1-b26c-49446730defd");
        exchangeVO.setPkProagent("a613caaa-e5f3-4de1-b26c-49446730defd");
//        DepConsumerVO[] cons = new DepConsumerVO[3];
//        cons[0] = con1;
//        cons[1] = con2;
//        cons[2] = con3;        
//        consumerService.addBatchConsumers(cons,true);
//        consumerService.addConsumer(con1);
//        exchangeService.addExchange(exchangeVO);
        //exchangeService.test();
    }
    
    /**
     * 获取所有流程
     * @return
     */
    @RequestMapping(path="getAllExchanges",method=RequestMethod.GET)
    public Object getAllExchanges() {
        return exchangeService.getAllExchanges();
    }
    
    /**
     * 获取所有流程拓扑图
     * @return
     */
    @RequestMapping(path="getAllExchangeGraph",method=RequestMethod.GET)
    public Object getAllExchangeGraph(String type) {
        return getExchangeGraphsByExchangeList("a0a1a1d2-4578-477a-ae6a-5c9f4f66a95c",exchangeService.getAllExchanges(),"0");
    }
    
    /**
     * 根据节点获取流程拓扑图
     * @return
     */
    @RequestMapping(path="getAllExchangeGraphByPkAgent",method=RequestMethod.GET)
    public Object getAllExchangeGraphByPkAgent(String pkAgent,String type) {
        List<DepExchangeVO> exchangeList = exchangeService.getExchangesByPkAgent(pkAgent);
        return getExchangeGraphsByExchangeList(pkAgent,exchangeList,"2");
    }
    
    //获得多个流程图
    public Map<String,Object> getExchangeGraphsByExchangeList(String pkGraph,List<DepExchangeVO> exchangeList,String type){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> nodeMap = new HashMap<String,Object>();
        
        List<Object> finalNodeList = new ArrayList<Object>();
        List<Map<String,String>> finalLineList = new ArrayList<Map<String,String>>();
        for(DepExchangeVO exchangeVo : exchangeList){
            
            if(type.equals("0")){
                //合并节点map
                nodeMap.putAll(getNodeListByPkExchange("a0a1a1d2-4578-477a-ae6a-5c9f4f66a95c",exchangeVo.getPkExchange(),type));
            }
            if(type.equals("2")){
                //合并节点map
                nodeMap.putAll(getNodeListByPkExchange(pkGraph,exchangeVo.getPkExchange(),type));
            }
            
            List<Map<String,String>> lineList = getLineListByPkExchange(exchangeVo.getPkExchange());
            for(int j=0;j<lineList.size();j++){
                finalLineList.add(lineList.get(j));
            }
        }
        for (Map.Entry<String, Object> entry : nodeMap.entrySet()) {
            finalNodeList.add(entry.getValue());
        }
        List<Map<String,String>> LineMapList = new ArrayList<Map<String,String>>();
        String mqId = PropertiesContext.getProperties().getProperty(DepConstant.MQID);
        //线去重
        for(int i=0;i<finalLineList.size();i++){
            Map<String,String> lineMap = finalLineList.get(i);
            if(lineMap.get("fromId").equals(mqId)){
                LineMapList.add(lineMap);
                finalLineList.remove(i);
                i--;
            }
            if(lineMap.get("toId").equals(mqId)){
                LineMapList.add(lineMap);
                finalLineList.remove(i);
                i--;
            }
        }
        LineMapList=mapListDuplicateRemove(mapListDuplicateRemove(LineMapList));
        for(int i=0;i<LineMapList.size();i++){
            finalLineList.add(LineMapList.get(i));
        }
        Map<String,String> mq = getMQNode();
        DepLocationVO mqLocation = null;
        if(type.equals("0")){
            mqLocation=locationService.getLocationByPkNode("a0a1a1d2-4578-477a-ae6a-5c9f4f66a95c",mq.get("id"), type);
        }
        if(type.equals("2")){
            mqLocation=locationService.getLocationByPkNode(pkGraph,mq.get("id"), type);
        }
        
        if(null!=mqLocation){
            mq.put("x", mqLocation.getGuiLocationX());
            mq.put("y", mqLocation.getGuiLocationY());
        }else{
            mq.put("x", null);
            mq.put("y", null);
        }
        
        finalNodeList.add(mq);
//        finalLineList=mapListDuplicateRemove(finalLineList);
        resultMap.put("node", finalNodeList);
        resultMap.put("line", finalLineList);
        
        return resultMap;
    }
    
    /**
     * 目录树搜索
     * @param name
     * @return
     */
    @RequestMapping(path="searchExchangeByName",method=RequestMethod.GET)
    public Object searchExchangeByName(String name) {
        List<DepExchangeVO> result = new ArrayList<DepExchangeVO>();
        try {
            result = exchangeService.getExchangeByName(name);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return result;
    }
    
    /**
     * 应用下拉
     * @param proOrcon 生产 或者消费
     * @return
     */
    @RequestMapping(path="getAllApp",method=RequestMethod.GET)
    public Object getAllApp(String proOrcon) {
        List<DepApplicationVO> result = new ArrayList<DepApplicationVO>();
        try {
            if(proOrcon.equals(AppIdentityTypeEnum.PROVIDER.getCode()))
                result = appService.getAllProviders();
            else if(proOrcon.equals(AppIdentityTypeEnum.CONSUMER.getCode()))
                result = appService.getAllConsumers();
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return result;
    }
    
    
    /**
     * 获取过滤字段名称
     * @param appPk
     * @return
     */
    @RequestMapping(path="getAppReturnFieldName",method=RequestMethod.GET)
    public Object getAppReturnFieldName(String appPk) {
        List<String> fieldName = new ArrayList<String>();
        try {
            if(!UtilTools.isEmptyStr(appPk)) {
                DepApplicationVO appVO = appService.getAppByPk(appPk);
                if(!UtilTools.isEmptyStr(appVO.getReturnType())) {
                    String[] fields = appVO.getReturnType().split("&");
                    for(String field : fields) {
                        fieldName.add(field.split("=")[0]);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return fieldName;
    }
    
    /**
     * 流程图
     * @param exchangeVO
     * @return
     */
    @RequestMapping(path="getExchangeChart",method=RequestMethod.GET)
    public Object getExchangeChart(String pkExchange,String type) {
        return getExchangeByPkExchange(pkExchange,pkExchange, "1");
    }
    
    /**
     * 保存拓扑图位置
     *
     * @return
     */
    @RequestMapping(path = "saveAllTopologies", method = RequestMethod.POST)
    public @ResponseBody Object saveAllTopologies(@RequestBody @Validated(AddOrModify.class) TopologyParamList topologyParamList) {
        try {
            String pkGraph = topologyParamList.getPkGraph();
            String type = topologyParamList.getType();
            List<NodeLocation> nodePkList = topologyParamList.getNodePkList();
            
            DepTopologyVO topologyVo = new DepTopologyVO();
            if(type.equals("0")||null==pkGraph||pkGraph.equals("")){
                pkGraph="a0a1a1d2-4578-477a-ae6a-5c9f4f66a95c";
            }
            topologyVo.setPkGraph(pkGraph);
            topologyVo.setSignature(type);
            List<DepTopologyVO> topoVoList = topologyService.getTopologyByPk(pkGraph);
            if(topoVoList.size()>0){
                topologyService.removeTopologyByPkGraph(topoVoList.get(0).getPkGraph());
                topologyService.addTopology(topologyVo);
                locationService.removeLocationByGraph(topoVoList.get(0).getPkGraph());
                for(int i=0;i<nodePkList.size();i++){
                    DepLocationVO locationVo = new DepLocationVO();
                    locationVo.setPkGraph(pkGraph);
                    locationVo.setPkNode(nodePkList.get(i).getPkNode());
                    locationVo.setNodeType(type);
                    locationVo.setGuiLocationX(nodePkList.get(i).getGuiLocationX());
                    locationVo.setGuiLocationY(nodePkList.get(i).getGuiLocationY());
                    locationService.addLocation(locationVo);
                }
            }else{
                topologyService.addTopology(topologyVo);
                for(int i=0;i<nodePkList.size();i++){
                    DepLocationVO locationVo = new DepLocationVO();
                    locationVo.setPkGraph(pkGraph);
                    locationVo.setPkNode(nodePkList.get(i).getPkNode());
                    locationVo.setNodeType(type);
                    locationVo.setGuiLocationX(nodePkList.get(i).getGuiLocationX());
                    locationVo.setGuiLocationY(nodePkList.get(i).getGuiLocationY());
                    locationService.addLocation(locationVo);
                }
            }
            
            return "success";
        } catch(Exception e) {
            e.printStackTrace();
            HashMap<String, String> errorMap = new HashMap<String, String>();
            errorMap.put("error", e.getMessage());
            return errorMap;
        }
    }
    
    /**
     * 根据pkexchange获得流程
     * @return
     */
    public Map<String,Object> getExchangeByPkExchange(String pkGraph,String pkExchange,String type){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        
        List<Object> nodeList = new ArrayList<Object>();
        Map<String,Object> nodeMap = getNodeListByPkExchange(pkExchange,pkExchange, type);
        
        List<Map<String,String>> lineList = getLineListByPkExchange(pkExchange);
        
        Map<String,String> mq = getMQNode();
        DepLocationVO mqLocation = locationService.getLocationByPkNode(pkGraph,mq.get("id"), type);
        if(null!=mqLocation){
            mq.put("x", String.valueOf(mqLocation.getGuiLocationX()));
            mq.put("y", String.valueOf(mqLocation.getGuiLocationY()));
        }else{
            mq.put("x", null);
            mq.put("y", null);
        }
        
        for (Map.Entry<String, Object> entry : nodeMap.entrySet()) {
        nodeList.add(entry.getValue());
        }
        
        nodeList.add(mq);
            
        resultMap.put("node", nodeList);
        resultMap.put("line", lineList);
        return resultMap;
    }
    
    /**
     * 根据pkexchange获得nodeList
     * @return
     */
    public Map<String,Object> getNodeListByPkExchange(String pkGraph,String pkExchange,String type){
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        Map<String,Object> nodeMap = new HashMap<String,Object>();
        
        //生产应用节点
        DepApplicationVO proAppVO = appService.getAppByPk(exchangeVO.getPkProapp());
        proAppVO.setId(proAppVO.getPk());
        proAppVO.setText(proAppVO.getName());
        proAppVO.setType(proAppVO.getIdentityType());
        DepLocationVO appLocationVo = locationService.getLocationByPkNode(pkGraph,proAppVO.getPk(), type);
        if(null!=appLocationVo){
            proAppVO.setX(appLocationVo.getGuiLocationX());
            proAppVO.setY(appLocationVo.getGuiLocationY());
        }else{
            proAppVO.setX(null);
            proAppVO.setY(null);
        }
        nodeMap.put(proAppVO.getId(), proAppVO);
        
        //生产节点节点
        DepAgentVO proAgentVO = agentService.getAgentByPk(exchangeVO.getPkProagent());
        proAgentVO.setId(proAgentVO.getPk());
        proAgentVO.setText(proAgentVO.getName());
        proAgentVO.setType(NodeTypeEnum.AGENT);
        DepLocationVO agentLocationVo = locationService.getLocationByPkNode(pkGraph,proAgentVO.getPk(), type);
        if(null!=agentLocationVo){
            proAgentVO.setX(agentLocationVo.getGuiLocationX());
            proAgentVO.setY(agentLocationVo.getGuiLocationY());
        }else{
            proAgentVO.setX(null);
            proAgentVO.setY(null);
        }
        nodeMap.put(proAgentVO.getId(), proAgentVO);
        
        
        //消费流程
        List<DepConsumerVO> consumerList = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
        List<DepApplicationVO> conAppList = appService.getAllConsumers();
        List<DepAgentVO> agentList = agentService.getAllAgents();
        //消费节点
        for(DepConsumerVO consumerVO : consumerList) {
            for(DepApplicationVO appVO : conAppList) {
                //消费节点
                if(consumerVO.getPkApp().equals(appVO.getPk())) {
                    appVO.setId(appVO.getPk());
                    appVO.setText(appVO.getName());
                    appVO.setType(appVO.getIdentityType());
                    DepLocationVO conAppLocationVo = locationService.getLocationByPkNode(pkGraph,appVO.getPk(), type);
                    if(null!=conAppLocationVo){
                        appVO.setX(conAppLocationVo.getGuiLocationX());
                        appVO.setY(conAppLocationVo.getGuiLocationY());
                    }else{
                        appVO.setX(null);
                        appVO.setY(null);
                    }
                    nodeMap.put(appVO.getId(), appVO);
                }
            }
            for(DepAgentVO agentVO : agentList) {
                //消费节点
                if(consumerVO.getPkAgent().equals(agentVO.getPk())) {
                    agentVO.setId(agentVO.getPk());
                    agentVO.setText(agentVO.getName());
                    agentVO.setType(NodeTypeEnum.AGENT);
                    DepLocationVO conAgentLocationVo = locationService.getLocationByPkNode(pkGraph,agentVO.getPk(), type);
                    if(null!=conAgentLocationVo){
                        agentVO.setX(conAgentLocationVo.getGuiLocationX());
                        agentVO.setY(conAgentLocationVo.getGuiLocationY());
                    }else{
                        agentVO.setX(null);
                        agentVO.setY(null);
                    }
                    nodeMap.put(agentVO.getId(), agentVO);
                }
            }
            
        }
        
        return nodeMap;
    }
    
    /**
     * 获得MQ
     * @return
     */
    public Map<String,String> getMQNode(){
        //mq节点
        String mqId = PropertiesContext.getProperties().getProperty(DepConstant.MQID);
        Map<String,String> mq = new HashMap<String,String>();
        mq.put("id", mqId);
        mq.put("text", "MQ");
        mq.put("type", NodeTypeEnum.MQ.toString());
        return mq;
    }
    
    /**
     * 根据pkexchange获得lineList
     * @return
     */
    public List<Map<String,String>> getLineListByPkExchange(String pkExchange){
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        List<Map<String,String>> lineList = new ArrayList<Map<String,String>>();
        //mq节点
        Map<String,String> mq = getMQNode();
        String mqId = mq.get("id");
        
        //生产线
        Map<String,String> appProLine = new HashMap<String,String>();
        appProLine.put("fromId", exchangeVO.getPkProapp());
        appProLine.put("toId",exchangeVO.getPkProagent());
        lineList.add(appProLine);
//        exchangeVO.setFromId(exchangeVO.getPkProapp());
//        exchangeVO.setToId(exchangeVO.getPkProagent());
//        lineList.add(exchangeVO);
        
        Map<String,String> exchangeLine = new HashMap<String,String>();
        exchangeLine.put("fromId", exchangeVO.getPkProagent());
        exchangeLine.put("toId", mqId);
        lineList.add(exchangeLine);
        
        //消费流程
        List<DepConsumerVO> consumerList = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
                
        //消费节点
        for(DepConsumerVO consumerVO : consumerList) {
            //消费线
//            consumerVO.setFromId(mqId);
//            consumerVO.setToId(consumerVO.getPkAgent());
//            lineList.add(consumerVO);
            Map<String,String> mqConLine = new HashMap<String,String>();
            mqConLine.put("fromId", mqId);
            mqConLine.put("toId", consumerVO.getPkAgent());
            lineList.add(mqConLine);
            Map<String,String> consumerLine = new HashMap<String,String>();
            consumerLine.put("fromId", consumerVO.getPkAgent());
            consumerLine.put("toId", consumerVO.getPkApp());
            lineList.add(consumerLine);
        }
        return lineList;
    }
    
    //listMap去重
    public List<Map<String,String>> mapListDuplicateRemove(List<Map<String,String>> mapList){

        for (int i = 0; i < mapList.size(); i++) {
            Map<String, String> m1 = mapList.get(i);

            for (int j = i + 1; j < mapList.size(); j++) {
                Map<String, String> m2 = mapList.get(j);
//                if (m1.entrySet().equals(m2)) {
//                    mapList.remove(j);
//                    continue;
//                }
//            for(Map.Entry<String, String> entry1:m1.entrySet()){
//                 String m1value = entry1.getValue() == null?"":entry1.getValue();
//                 String m2value = m2.get(entry1.getKey())==null?"":m2.get(entry1.getKey());
//                 if (m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
//                    //其他操作...
//                     mapList.remove(j);
//                }
//             }
            if(m1.get("fromId").equals(m2.get("fromId")) && m1.get("toId").equals(m2.get("toId"))){
                mapList.remove(j);
            }

        }

        }
//         System.out.println(mapList.size());
//         System.out.println(mapList);  
         return mapList;
    }
    
    /**
     * 左下角的基本信息
     * @param exchangeVO
     * @return
     */
    @RequestMapping(path="getBaseExchangeInfo",method=RequestMethod.GET)
    public Object getBaseExchangeInfo(String pkExchange) {
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        BaseExchangeInfo exchangeInfo = new BaseExchangeInfo();
        try {
            String proApp = appService.getAppByPk(exchangeVO.getPkProapp()).getName();
            exchangeInfo.setProAppName(proApp);
            exchangeInfo.setTopic(exchangeVO.getTopic());
            exchangeInfo.setCreateTime(exchangeVO.getCreateTime());
            exchangeInfo.setModifyTime(exchangeVO.getModifyTime());
            List<String> agents =  new ArrayList<String>();
            List<String> conApps =  new ArrayList<String>();
            agents.add(agentService.getAgentByPk(exchangeVO.getPkProagent()).getName());
            List<DepApplicationVO> appLists = appService.getAllConsumers();
            for(DepConsumerVO consumerVO : consumerService.getConsumerByPkExchange(exchangeVO.getPk())) {
                for(DepApplicationVO appVO : appLists) {
                    if(appVO.getPk().equals(consumerVO.getPkApp())) {
                        conApps.add(appVO.getName());                        
                    }
                }
                agents.add(agentService.getAgentByPk(consumerVO.getPkAgent()).getName());
            }
            exchangeInfo.setAgents(agents);
            exchangeInfo.setConAppNames(conApps);
            exchangeInfo.setProData(proHistoryService.getProducedDataByPkExchange(exchangeVO.getPk()));
            exchangeInfo.setConData(conHistoryService.getConsumerDataByPkExchange(exchangeVO.getPk()));
        } catch (Exception e) {
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return exchangeInfo;
    }
    
    /**
     * 生产历史记录
     * @param exchangeVO
     * @return
     */
    @RequestMapping(path="getProHistoryByDepExchangeVO",method=RequestMethod.GET)
    public Object getProHistoryByDepExchangeVO(String pkExchange) {
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        return proHistoryService.getProHistoryByPkExchange(exchangeVO.getPk());
    }
    
    /**
     * 消费历史记录
     * @param exchangeVO
     * @return
     */
    @RequestMapping(path="getConHistoryByDepExchangeVO",method=RequestMethod.GET)
    public Object getConHistoryByDepExchangeVO(String pkExchange) {
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        List<DepConsumerVO> consumerList = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
        List<DepConHistoryVO> conHistoryList = new ArrayList<DepConHistoryVO>();
        for(DepConsumerVO consumerVO : consumerList) {
            conHistoryList.addAll(conHistoryService.getConHistorysByPkConsumer(consumerVO.getPk()));
        }
        return conHistoryList;
    }
    
    /**
     * 查看,修改流程信息
     * @param exchangePk
     * @return
     */
    @RequestMapping(path="getExchangeByDepExchangeVO",method=RequestMethod.GET)
    public Object getExchangeByDepExchangeVO(String pkExchange) {
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        //根据DepExchangeVO获取相应的参数封装给ExchangeParam返回
        ExchangeParam exchangeParam = new ExchangeParam();
        Exchange exchange = new ExchangeParam().new Exchange();
        Consumer[] consumers = null;
        try {
            List<DepConsumerVO> consumerVOList = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
            consumers = new Consumer[consumerVOList.size()];
            //定时
            CronModel cronModel = new CronModel(exchangeVO.getCron(),exchangeVO.getFre(),exchangeVO.isCronAdvanced());                        
            //消费
            int i=0;
            for(DepConsumerVO consumerVO : consumerVOList) {
                Consumer consumer = new ExchangeParam().new Consumer();                
                consumer.setConsumer(consumerVO);
                List<DepRulesVO> ruleList = ruleService.getRuleByPkConsumer(consumerVO.getPk());
                consumer.setRulerList(ruleList.toArray(new DepRulesVO[ruleList.size()]));
                consumers[i++] = consumer;
            }
            //生产
            exchange.setProvider(exchangeVO);
            List<DepRulesVO> ruleList = ruleService.getRuleByPkExchange(exchangeVO.getPk());
            exchange.setRulerList(ruleList.toArray(new DepRulesVO[ruleList.size()]));
            exchangeParam.setExchange(exchange);
            exchangeParam.setConsumerList(consumers);
            exchangeParam.setCronModel(cronModel);
        } catch (Exception e) {
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return exchangeParam;
    }
    
    /**
     * 删除流程
     * @param exchangeVO
     * @return
     */
    @RequestMapping(path="removeExchange",method=RequestMethod.GET)
    public Object removeExchange(String pkExchange) {
        DepExchangeVO exchangeVO = exchangeService.getExchangesByPk(pkExchange);
        try {
            if(exchangeVO != null) {
                exchangeService.removeAllExchange(exchangeVO);
            }    
        } catch (Exception e) {
            Map<String,Object> errorMap = new HashMap<String,Object>();
            errorMap.put("error", e);
            return errorMap;
        }
        return exchangeVO;
    }
    
    /**
     * 流程保存修改----保存
     * @param exchangeParam
     * @return
     */
    @RequestMapping(path="addOrUpdateExchangeInfo",method=RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object addOrUpdateExchangeInfo(@RequestBody @Validated(AddOrModify.class) String data) {    
        DepExchangeVO exchangeVO = new DepExchangeVO();
        //1.持久化数据
        try {
            exchangeVO = addOrUpdateData(data);
            List<DepConsumerVO> consumerss = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
            //2.创建路由
            if(consumerss != null)
                for(DepConsumerVO consumerVO : consumerss) {
                    managerService.createConExchange(consumerVO);
                }
            managerService.createProExchange(exchangeVO);
        } catch (Exception e) {            
            e.printStackTrace();
            HashMap<String, String> errorMap = new HashMap<String, String>();
            errorMap.put("error", e.getMessage());
            return errorMap;
        }
        return exchangeVO;
    }
    
    /**
     * 流程保存修改----保存运行
     * @param exchangeParam
     * @return
     */
    @RequestMapping(path="addOrUpdateAndRunExchangeInfo",method=RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object addOrUpdateAndRunExchangeInfo(@RequestBody @Validated(AddOrModify.class) String data) {    
        DepExchangeVO exchangeVO = new DepExchangeVO();
        //1.持久化数据
        try {
            exchangeVO = addOrUpdateData(data);
            List<DepConsumerVO> consumerss = consumerService.getConsumerByPkExchange(exchangeVO.getPk());
            //2.创建路由
            if(consumerss != null)
                for(DepConsumerVO consumerVO : consumerss) {
                    managerService.createConExchange(consumerVO);
                }
            managerService.createProExchange(exchangeVO);            
            //3.启动路由  先启动消费端,再启动生产端
            managerService.operateExchange(exchangeVO.getPkExchange(), DepConstant.START);
        } catch (Exception e) {            
            e.printStackTrace();
            HashMap<String, String> errorMap = new HashMap<String, String>();
            errorMap.put("error", e.getMessage());
            return errorMap;
        }
        return exchangeVO;
    }
    
    /**
     * 启动或停止流程
     * @param pk
     * @param options
     * @return
     */
    @RequestMapping(value="/operate",method=RequestMethod.GET)
    public Object optsRoute(String pk,String options)  {
        try {
            String res = ExchangeStatusEnum.OK.toString();
            res=managerService.operateExchange(pk, options);
            return res;
        }catch(Exception e) {
            e.printStackTrace();
            HashMap<String, String> errorMap = new HashMap<String, String>();
            errorMap.put("error", e.getMessage());
            return errorMap;
        }
    }
    
    private DepExchangeVO addOrUpdateData(String data) {
        ExchangeParam exchangeParam = new ExchangeParam();
        Exchange exchange = new ExchangeParam().new Exchange();
        Consumer[] consumers = null;
        //1.持久化数据
        JSONObject jsonObject = JSONObject.fromObject(data);
        //解析生产流程数据
        JSONObject provider = JSONObject.fromObject(jsonObject.get("exchange")).getJSONObject("provider");
        DepExchangeVO exchangeVO = (DepExchangeVO)JSONObject.toBean(provider, DepExchangeVO.class);
        exchange.setProvider(exchangeVO);
        JSONArray jsonarrayPro = JSONObject.fromObject(jsonObject.get("exchange")).getJSONArray("rulerList");
        if(jsonarrayPro.size()>0) {
            DepRulesVO[] rules = new DepRulesVO[jsonarrayPro.size()];
            for(int i=0;i<jsonarrayPro.size();i++) {
                JSONObject job = jsonarrayPro.getJSONObject(i);
                DepRulesVO rule = (DepRulesVO)JSONObject.toBean(job, DepRulesVO.class);
                if(i==(jsonarrayPro.size()-1))
                    rule.setLogicOperator(null);
                rules[i] = rule;
            }
            exchange.setRulerList(rules);
        }
        exchangeParam.setExchange(exchange);
        //解析消费流程数据
        JSONArray jsonarray = jsonObject.getJSONArray("consumerList");
        if(jsonarray.size()>0) {
            consumers =new Consumer[jsonarray.size()];
            Consumer consumer =new ExchangeParam().new Consumer();
            for(int i=0;i<jsonarray.size();i++) {
                JSONObject job = jsonarray.getJSONObject(i);
                JSONObject jsonCon = job.getJSONObject("consumer");
                DepConsumerVO consumerVO = (DepConsumerVO)JSONObject.toBean(jsonCon, DepConsumerVO.class);
                consumer.setConsumer(consumerVO);
                JSONArray jsonRuler = job.getJSONArray("rulerList");
                if(jsonRuler.size()>0) {
                    DepRulesVO[] rules = new DepRulesVO[jsonRuler.size()];
                    for(int j=0;j<jsonRuler.size();j++) {
                        JSONObject job1 = jsonRuler.getJSONObject(j);
                        DepRulesVO rule = (DepRulesVO)JSONObject.toBean(job1, DepRulesVO.class);
                        if(j==(jsonRuler.size()-1))
                            rule.setLogicOperator(null);
                        rules[j] = rule;
                    }
                    consumer.setRulerList(rules);
                }
                consumers[i] = consumer;
            }                
        }
        exchangeParam.setConsumerList(consumers);
        //解析cron数据        
        CronModel cronModel = (CronModel)JSONObject.toBean(jsonObject.getJSONObject("cronModel"), CronModel.class);
        exchangeParam.setCronModel(cronModel);
        exchangeVO = exchangeService.addOrUpdateExchangeInfo(exchangeParam);
        return exchangeVO;    
    }
    
    
    /**
     * 流程页面需要的基本信息
     * @author wenxing
     *
     */
    @Data
    class BaseExchangeInfo{
        private String proAppName;
        private List<String> conAppNames;
        private List<String> agents;
        private String topic;
        private long proData;
        private long conData;
        private String createTime;
        private String modifyTime;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值