zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/RouteController.java
@@ -106,6 +106,7 @@ @PreAuthorize("hasAuthority('manager:route:update')") @OperationLog("Update Route") @PostMapping("/route/update") @Transactional public R update(@RequestBody Route route) { Route origin = routeService.getById(route.getId()); if (origin == null) { @@ -131,6 +132,14 @@ if (!routeService.updateById(route)) { return R.error("Update Fail"); } Code startCode = codeService.getCacheById(route.getStartCode()); Code endCode = codeService.getCacheById(route.getEndCode()); if (startCode != null && endCode != null) { codeGapService.createCodeGapByCode(startCode, endCode, getLoginUserId()); } if (!isSameCode(origin, route)) { codeGapService.removeCodeGapIfUnused(origin.getStartCode(), origin.getEndCode()); } List<Long> affectedCodeIds = new ArrayList<>(); if (origin.getStartCode() != null) { affectedCodeIds.add(origin.getStartCode()); @@ -144,9 +153,6 @@ if (route.getEndCode() != null) { affectedCodeIds.add(route.getEndCode()); } Code startCode = codeService.getCacheById(route.getStartCode()); Code endCode = codeService.getCacheById(route.getEndCode()); codeGapService.createCodeGapByCode(startCode, endCode, getLoginUserId()); codeService.refreshCornerByCodeIds(affectedCodeIds); return R.ok("Update Success").add(route); } @@ -154,6 +160,7 @@ @PreAuthorize("hasAuthority('manager:route:remove')") @OperationLog("Delete Route") @PostMapping("/route/remove/{ids}") @Transactional public R remove(@PathVariable Long[] ids) { List<Route> routes = routeService.listByIds(Arrays.asList(ids)); List<Long> affectedCodeIds = new ArrayList<>(); @@ -167,6 +174,9 @@ } if (!routeService.removeByIds(Arrays.asList(ids))) { return R.error("Delete Fail"); } for (Route route : routes) { codeGapService.removeCodeGapIfUnused(route.getStartCode(), route.getEndCode()); } codeService.refreshCornerByCodeIds(affectedCodeIds); return R.ok("Delete Success").add(ids); @@ -222,4 +232,11 @@ return R.ok(); } private boolean isSameCode(Route route0, Route route1) { return Objects.equals(route0.getStartCode(), route1.getStartCode()) && Objects.equals(route0.getEndCode(), route1.getEndCode()) || Objects.equals(route0.getStartCode(), route1.getEndCode()) && Objects.equals(route0.getEndCode(), route1.getStartCode()); } } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/CodeGapService.java
@@ -8,6 +8,8 @@ CodeGap createCodeGapByCode(Code code0, Code code1, Long userId); void removeCodeGapIfUnused(Long code0, Long code1); CodeGap findByCodeOfBoth(Long code0, Long code1); Double findDistanceByCode(Long code0, Long code1); zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/CodeGapServiceImpl.java
@@ -7,13 +7,16 @@ import com.zy.acs.manager.core.service.MapService; import com.zy.acs.manager.manager.entity.Code; import com.zy.acs.manager.manager.entity.CodeGap; import com.zy.acs.manager.manager.entity.Route; import com.zy.acs.manager.manager.mapper.CodeGapMapper; import com.zy.acs.manager.manager.service.CodeGapService; import com.zy.acs.manager.manager.service.RouteService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.Serializable; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -26,6 +29,8 @@ @Autowired private MapService mapService; @Autowired private RouteService routeService; @PostConstruct public void init() { @@ -95,6 +100,51 @@ return 0.0; } @Override public void removeCodeGapIfUnused(Long code0, Long code1) { if (code0 == null || code1 == null) { return; } long count = routeService.count(new LambdaQueryWrapper<Route>() .eq(Route::getStartCode, code0) .eq(Route::getEndCode, code1)) + routeService.count(new LambdaQueryWrapper<Route>() .eq(Route::getStartCode, code1) .eq(Route::getEndCode, code0)); if (count > 0) { return; } CodeGap codeGap = findByCodeOfBoth(code0, code1); if (codeGap != null && !this.removeById(codeGap.getId())) { throw new CoolException("Delete Fail"); } } @Override public boolean save(CodeGap entity) { boolean result = super.save(entity); if (result) { String gapKey = generateGapKey(entity.getCode0(), entity.getCode1()); if (!Cools.isEmpty(gapKey)) { GAP_DISTANCE_CACHE.put(gapKey, entity); } } return result; } @Override public boolean removeById(Serializable id) { CodeGap codeGap = this.getById(id); boolean result = super.removeById(id); if (result && codeGap != null) { String gapKey = generateGapKey(codeGap.getCode0(), codeGap.getCode1()); if (!Cools.isEmpty(gapKey)) { GAP_DISTANCE_CACHE.remove(gapKey); } } return result; } private static String generateGapKey(Long code0, Long code1) { if (Cools.isEmpty(code0, code1)) { return null;