zhang
2025-05-20 1313906bb1eb983d3beece810035e7fc28d6a92f
zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/RouteServiceImpl.java
@@ -1,9 +1,9 @@
package com.zy.acs.manager.manager.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.Route;
import com.zy.acs.manager.manager.mapper.RouteMapper;
@@ -12,9 +12,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service("routeService")
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService {
@@ -23,17 +22,31 @@
    private CodeService codeService;
    @Override
    public Route createRouteByCode(Code code0, Code code1) {
        Route route = this.findByCodeOfBoth(code0.getId(), code1.getId());
    public Route createRouteByCode(Code startCode, Code endCode) {
        return createRouteByCode(startCode, endCode, 0, null);
    }
    @Override
    public Route createRouteByCode(Code startCode, Code endCode, Integer direction, Long userId) {
        if (startCode.getData().equals(endCode.getData())) {
            return null;
        }
        Route route = this.findByCodeOfBoth(startCode.getId(), endCode.getId());
        direction = Optional.ofNullable(direction).orElse(0);
        if (null == route) {
            Date now = new Date();
            route = new Route();
            route.setUuid(code0.getData()+"-"+code1.getData());
            route.setStartCode(code0.getId());
            route.setEndCode(code1.getId());
            route.setDirection(0);
            route.setCodeArr(JSON.toJSONString(Arrays.asList(code0.getId(), code1.getId())));
            route.setUuid(startCode.getData() + "-" + endCode.getData());
            route.setStartCode(startCode.getId());
            route.setEndCode(endCode.getId());
            route.setDirection(direction);
            route.setCodeArr(GsonUtils.toJson(Arrays.asList(startCode.getId(), endCode.getId())));
            route.setCreateBy(userId);
            route.setCreateTime(now);
            route.setUpdateBy(userId);
            route.setUpdateTime(now);
            if (!this.save(route)) {
                throw new BusinessException(route.getCodeArr()+" save fail![Route]");
                throw new CoolException(route.getCodeArr()+" save fail![Route]");
            }
        }
        return route;
@@ -44,22 +57,43 @@
        List<String> neighborCodeList = new ArrayList<>();
        for (Route route : this.list(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, code))) {
            neighborCodeList.add(codeService.getById(route.getEndCode()).getData());
            neighborCodeList.add(codeService.getCacheById(route.getEndCode()).getData());
        }
        for (Route route : this.list(new LambdaQueryWrapper<Route>().eq(Route::getEndCode, code))) {
            neighborCodeList.add(codeService.getById(route.getStartCode()).getData());
            neighborCodeList.add(codeService.getCacheById(route.getStartCode()).getData());
        }
        return neighborCodeList;
    }
    @Override
    public Route findByCodeOfBoth(Long code0, Long code1) {
        Route route = getOne(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, code0).eq(Route::getEndCode, code1).eq(Route::getStatus, 1));
    public Route findByCodeOfBoth(Long startCode, Long endCode) {
        Route route = getOne(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, startCode).eq(Route::getEndCode, endCode).eq(Route::getStatus, 1));
        if (null == route) {
            route = getOne(new LambdaQueryWrapper<Route>().eq(Route::getEndCode, code0).eq(Route::getStartCode, code1).eq(Route::getStatus, 1));
            route = getOne(new LambdaQueryWrapper<Route>().eq(Route::getEndCode, startCode).eq(Route::getStartCode, endCode).eq(Route::getStatus, 1));
        }
        return route;
    }
    @Override
    public List<Long> getAdjacencyNode(Long codeId) {
        List<Long> result = new ArrayList<>();
        result.addAll(this.list(new LambdaQueryWrapper<Route>()
                .eq(Route::getStartCode, codeId)
                .in(Route::getDirection, 0, 1)
                .eq(Route::getStatus, 1)
        ).stream().map(Route::getEndCode).distinct().collect(Collectors.toList()));
        result.addAll(this.list(new LambdaQueryWrapper<Route>()
                .eq(Route::getEndCode, codeId)
                .in(Route::getDirection, 0, 2)
                .eq(Route::getStatus, 1)
        ).stream().map(Route::getStartCode).distinct().collect(Collectors.toList()));
        return result.stream().distinct().collect(Collectors.toList());
    }
}