zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/CodeController.java
@@ -110,11 +110,13 @@ @PostMapping("/code/remove/{ids}") @Transactional public R remove(@PathVariable Long[] ids) { List<Long> affectedCodeIds = new ArrayList<>(); for (Long id : ids) { Code code = codeService.getById(id); if (null == code) { continue; } affectedCodeIds.addAll(routeService.getAdjacencyNode(code.getId())); codeGapService.remove(new LambdaQueryWrapper<CodeGap>().eq(CodeGap::getCode0, code.getId()).or().eq(CodeGap::getCode1, code.getId())); routeService.remove(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, code.getId()).or().eq(Route::getEndCode, code.getId())); if (!codeService.removeById(id)) { @@ -124,6 +126,7 @@ CodeServiceImpl.CODE_DATA_CACHE.remove(code.getData()); } } codeService.refreshCornerByCodeIds(affectedCodeIds); return R.ok("Delete Success").add(ids); } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/RouteController.java
@@ -107,6 +107,10 @@ @OperationLog("Update Route") @PostMapping("/route/update") public R update(@RequestBody Route route) { Route origin = routeService.getById(route.getId()); if (origin == null) { return R.error("Update Fail"); } if (route.getStartCode().equals(route.getEndCode())) { return R.error("Update Fail"); } @@ -127,6 +131,20 @@ if (!routeService.updateById(route)) { return R.error("Update Fail"); } List<Long> affectedCodeIds = new ArrayList<>(); if (origin.getStartCode() != null) { affectedCodeIds.add(origin.getStartCode()); } if (origin.getEndCode() != null) { affectedCodeIds.add(origin.getEndCode()); } if (route.getStartCode() != null) { affectedCodeIds.add(route.getStartCode()); } if (route.getEndCode() != null) { affectedCodeIds.add(route.getEndCode()); } codeService.refreshCornerByCodeIds(affectedCodeIds); return R.ok("Update Success").add(route); } @@ -134,9 +152,20 @@ @OperationLog("Delete Route") @PostMapping("/route/remove/{ids}") public R remove(@PathVariable Long[] ids) { List<Route> routes = routeService.listByIds(Arrays.asList(ids)); List<Long> affectedCodeIds = new ArrayList<>(); for (Route route : routes) { if (route.getStartCode() != null) { affectedCodeIds.add(route.getStartCode()); } if (route.getEndCode() != null) { affectedCodeIds.add(route.getEndCode()); } } if (!routeService.removeByIds(Arrays.asList(ids))) { return R.error("Delete Fail"); } codeService.refreshCornerByCodeIds(affectedCodeIds); return R.ok("Delete Success").add(ids); } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/CodeService.java
@@ -24,4 +24,6 @@ List<Code> getAllLocCode(); void refreshCornerByCodeIds(List<Long> codeIds); } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/CodeServiceImpl.java
@@ -18,10 +18,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.*; @Slf4j @Service("codeService") @@ -31,6 +28,8 @@ private CodeGapService codeGapService; @Autowired private RouteService routeService; private static final double STRAIGHT_LINE_SIN_THRESHOLD = 1e-3; public static final Map<Long, Code> CODE_ID_CACHE = new HashMap<>(); public static final Map<String, Code> CODE_DATA_CACHE = new HashMap<>(); @@ -74,6 +73,69 @@ } @Override public void refreshCornerByCodeIds(List<Long> codeIds) { if (Cools.isEmpty(codeIds)) { return; } Date now = new Date(); Set<Long> targets = new HashSet<>(codeIds); for (Long codeId : targets) { Code code = this.getCacheById(codeId); if (null == code) { continue; } boolean corner = shouldMarkAsCorner(code, routeService.getAdjacencyNode(codeId)); Integer cornerVal = corner ? 1 : 0; if (!Objects.equals(code.getCorner(), cornerVal)) { code.setCorner(cornerVal); code.setUpdateTime(now); if (!this.updateById(code)) { throw new BusinessException(code.getData() + "拐角标记更新失败"); } CODE_ID_CACHE.put(code.getId(), code); CODE_DATA_CACHE.put(code.getData(), code); } } } private boolean shouldMarkAsCorner(Code code, List<Long> adjacencyIds) { if (Cools.isEmpty(adjacencyIds)) { return false; } if (adjacencyIds.size() > 2) { return true; } if (adjacencyIds.size() < 2) { return false; } Code first = this.getCacheById(adjacencyIds.get(0)); Code second = this.getCacheById(adjacencyIds.get(1)); if (null == first || null == second) { return false; } return !isStraightLine(code, first, second); } private boolean isStraightLine(Code center, Code first, Code second) { if (center.getX() == null || center.getY() == null || first.getX() == null || first.getY() == null || second.getX() == null || second.getY() == null) { return true; } double v1x = first.getX() - center.getX(); double v1y = first.getY() - center.getY(); double v2x = second.getX() - center.getX(); double v2y = second.getY() - center.getY(); double len1 = Math.hypot(v1x, v1y); double len2 = Math.hypot(v2x, v2y); if (len1 == 0 || len2 == 0) { return true; } double sinValue = Math.abs(v1x * v2y - v1y * v2x) / (len1 * len2); return sinValue < STRAIGHT_LINE_SIN_THRESHOLD; } @Override public void importExecute(List<CodeExcel> excelList) { for (CodeExcel excel : excelList) { Code code = this.selectByData(excel.getCode()); zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/RouteServiceImpl.java
@@ -3,6 +3,7 @@ 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.common.utils.Utils; import com.zy.acs.framework.exception.CoolException; import com.zy.acs.manager.manager.entity.Code; import com.zy.acs.manager.manager.entity.Route; @@ -32,6 +33,7 @@ return null; } Route route = this.findByCodeOfBoth(startCode.getId(), endCode.getId()); boolean created = false; direction = Optional.ofNullable(direction).orElse(0); if (null == route) { Date now = new Date(); @@ -48,6 +50,12 @@ if (!this.save(route)) { throw new CoolException(route.getCodeArr()+" save fail![Route]"); } created = true; } if (created) { List<Long> codes = Utils.singletonList(startCode.getId()); codes.add(endCode.getId()); codeService.refreshCornerByCodeIds(codes); } return route; }