| package com.zy.acs.fake.service.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
| import com.zy.acs.common.utils.GsonUtils; | 
| import com.zy.acs.fake.entity.Code; | 
| import com.zy.acs.fake.entity.Route; | 
| import com.zy.acs.fake.mapper.RouteMapper; | 
| import com.zy.acs.fake.service.CodeService; | 
| import com.zy.acs.fake.service.RouteService; | 
| import com.zy.acs.framework.exception.CoolException; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| @Service("routeService") | 
| public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService { | 
|   | 
|     @Autowired | 
|     private CodeService codeService; | 
|   | 
|     @Override | 
|     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(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 CoolException(route.getCodeArr()+" save fail![Route]"); | 
|             } | 
|         } | 
|         return route; | 
|     } | 
|   | 
|     @Override | 
|     public List<String> findCodeDataOfSingle(Long code) { | 
|         List<String> neighborCodeList = new ArrayList<>(); | 
|   | 
|         for (Route route : this.list(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, code))) { | 
|             neighborCodeList.add(codeService.getById(route.getEndCode()).getData()); | 
|         } | 
|         for (Route route : this.list(new LambdaQueryWrapper<Route>().eq(Route::getEndCode, code))) { | 
|             neighborCodeList.add(codeService.getById(route.getStartCode()).getData()); | 
|         } | 
|   | 
|         return neighborCodeList; | 
|     } | 
|   | 
|     @Override | 
|     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, 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()); | 
|     } | 
|   | 
| } |