| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.xiaopeng.api.dto.operation.ElectronicFenceAddDto; |
| | | import com.xiaopeng.api.dto.operation.ElectronicFenceAreaUpdateStatusDto; |
| | | import com.xiaopeng.api.dto.operation.ElectronicFenceOperationStrategyValidateDto; |
| | | import com.xiaopeng.api.dto.operation.ElectronicFenceUpdateDto; |
| | | import com.xiaopeng.api.feignClient.operation.XpElectronicClient; |
| | | import com.xiaopeng.api.model.operation.XpElectronicFence; |
| | | import com.xiaopeng.api.model.operation.XpElectronicFenceArea; |
| | | import com.xiaopeng.api.model.operation.XpPoint; |
| | |
| | | import com.xiaopeng.common.core.exception.ServiceException; |
| | | import com.xiaopeng.common.core.utils.bean.BeanUtils; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.common.security.service.TokenService; |
| | | import com.xiaopeng.map.constant.I18nKeyConstant; |
| | | import com.xiaopeng.map.service.XpElectronicFenceAreaService; |
| | | import com.xiaopeng.map.service.XpElectronicFenceService; |
| | | import com.xiaopeng.map.service.XpPointService; |
| | | import com.xiaopeng.map.service.XpRegionService; |
| | | import com.xiaopeng.map.utils.OperationStrategyIndex; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType; |
| | | import org.apache.shardingsphere.transaction.core.TransactionType; |
| | | import org.locationtech.jts.geom.Coordinate; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.validation.Valid; |
| | | import javax.validation.constraints.NotNull; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.xiaopeng.map.service.serviceimpl.XpElectronicFenceServiceImpl.getPinyinInitials; |
| | | import com.xiaopeng.common.log.annotation.AdminOperationLog; |
| | | import com.xiaopeng.common.log.enums.AdminBusinessType; |
| | | import com.xiaopeng.common.log.enums.AdminOperationType; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | private XpRegionService regionService; |
| | | @Autowired |
| | | private XpRegionService xpRegionService; |
| | | |
| | | @Autowired |
| | | private XpPointService xpPointService; |
| | | /** |
| | | * 查询省市管理 |
| | | */ |
| | |
| | | */ |
| | | @PostMapping("/add") |
| | | @ApiOperation(value = "新增电子围栏") |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ELECTRONIC_FENCE_MANAGEMENT, |
| | | operationType = AdminOperationType.ADD, |
| | | content = "新增电子围栏", |
| | | appendRequestData = true |
| | | ) |
| | | public R add(@Validated @RequestBody ElectronicFenceAddDto dto) { |
| | | validateFenceTime(dto.getStatus(), dto.getEffectType(), dto.getEffectTime(), dto.getFailureType(), dto.getFailureTime()); |
| | | return xpElectronicFenceService.addElectronic(dto); |
| | | } |
| | | |
| | |
| | | @PostMapping("/updateStatus") |
| | | @ResponseBody |
| | | @ApiOperation(value = "修改状态") |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ELECTRONIC_FENCE_MANAGEMENT, |
| | | operationType = AdminOperationType.EDIT, |
| | | content = "修改电子围栏状态", |
| | | appendRequestData = true |
| | | ) |
| | | public R updateStatus(@RequestBody ElectronicFenceAreaUpdateStatusDto dto) { |
| | | XpElectronicFenceArea electronicFenceArea = xpElectronicFenceAreaService.getById(dto.getId()); |
| | | if (electronicFenceArea == null) { |
| | |
| | | */ |
| | | @PostMapping("/edit") |
| | | @ApiOperation(value = "编辑电子围栏") |
| | | @Transactional |
| | | @ShardingSphereTransactionType(TransactionType.BASE) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ELECTRONIC_FENCE_MANAGEMENT, |
| | | operationType = AdminOperationType.EDIT, |
| | | content = "编辑电子围栏", |
| | | appendRequestData = true |
| | | ) |
| | | public R edit(@Validated @RequestBody ElectronicFenceUpdateDto dto) { |
| | | validateFenceTime(dto.getStatus(), dto.getEffectType(), dto.getEffectTime(), dto.getFailureType(), dto.getFailureTime()); |
| | | return xpElectronicFenceService.editElectronic(dto); |
| | | } |
| | | |
| | | /** |
| | | * 校验编辑围栏时运营时间段缩短影响的点位数量 |
| | | * <p> |
| | | * 统计:围栏下 operation_type=2(自定义运营时间段) 的点位中, |
| | | * 当围栏运营时间范围发生缩短时,不在新运营时间范围(Union)内的点位数量(需要停用)。 |
| | | * </p> |
| | | */ |
| | | @PostMapping("/validateOperationStrategyShorten") |
| | | @ApiOperation(value = "校验围栏运营时间段缩短影响点位数量") |
| | | public R<Integer> validateOperationStrategyShorten(@Valid @RequestBody ElectronicFenceOperationStrategyValidateDto dto) { |
| | | XpElectronicFence fence = xpElectronicFenceService.getById(dto.getId()); |
| | | if (fence == null) { |
| | | throw new ServiceException("围栏不存在", I18nKeyConstant.FENCE_NOT_EXIST); |
| | | } |
| | | |
| | | OperationStrategyIndex oldIndex = OperationStrategyIndex.parse(fence.getOperationStrategy()); |
| | | OperationStrategyIndex newIndex = OperationStrategyIndex.parse(dto.getOperationStrategy()); |
| | | if (oldIndex.equalsNormalized(newIndex)) { |
| | | return R.ok(0); |
| | | } |
| | | if (!oldIndex.hasAnyShrinkComparedTo(newIndex)) { |
| | | return R.ok(0); |
| | | } |
| | | |
| | | List<Long> fenceAreaIds = xpElectronicFenceAreaService.lambdaQuery() |
| | | .eq(XpElectronicFenceArea::getElectronicId, dto.getId()) |
| | | .list() |
| | | .stream() |
| | | .map(XpElectronicFenceArea::getId) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | if (CollUtil.isEmpty(fenceAreaIds)) { |
| | | return R.ok(0); |
| | | } |
| | | |
| | | List<XpPoint> customPoints = xpPointService.lambdaQuery() |
| | | .in(XpPoint::getElectronicAreaId, fenceAreaIds) |
| | | .eq(XpPoint::getPointType, 1) |
| | | .eq(XpPoint::getOperationType, 2) |
| | | .list(); |
| | | if (CollUtil.isEmpty(customPoints)) { |
| | | return R.ok(0); |
| | | } |
| | | |
| | | int disableCount = 0; |
| | | for (XpPoint point : customPoints) { |
| | | if (point == null || point.getEffectiveTimePeriod() == null || point.getEffectiveTimePeriod().isEmpty()) { |
| | | continue; |
| | | } |
| | | List<OperationStrategyIndex.TimeInterval> pointIntervals = OperationStrategyIndex.parsePointEffectiveTimePeriod(point.getEffectiveTimePeriod()); |
| | | if (pointIntervals.isEmpty()) { |
| | | continue; |
| | | } |
| | | |
| | | boolean shouldDisable = !newIndex.isPointScheduleWithin(pointIntervals); |
| | | if (shouldDisable) { |
| | | disableCount++; |
| | | } |
| | | } |
| | | return R.ok(disableCount); |
| | | } |
| | | |
| | | /** |
| | |
| | | @ApiOperation(value = "复制电子围栏") |
| | | @Transactional |
| | | @ShardingSphereTransactionType(TransactionType.BASE) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.ELECTRONIC_FENCE_MANAGEMENT, |
| | | operationType = AdminOperationType.ADD, |
| | | contentExpression = "'复制电子围栏,id=' + #p0", |
| | | appendRequestData = false |
| | | ) |
| | | public R<Long> copy(@RequestParam("id") Integer id) { |
| | | XpElectronicFence electronicFence = xpElectronicFenceService.getById(id); |
| | | if (electronicFence.getStatus()==2){ |
| | |
| | | I18nKeyConstant.FENCE_STATUS_CANNOT_COPIED |
| | | ); |
| | | } |
| | | electronicFence.setElectronicName("副本_"+electronicFence.getElectronicName()); |
| | | electronicFence.setElectronicName(buildCopyFenceName(electronicFence.getElectronicName())); |
| | | electronicFence.setId(null); |
| | | electronicFence.setStatus(0); |
| | | List<XpElectronicFenceArea> electronicFenceAreaList = xpElectronicFenceAreaService.lambdaQuery().eq(XpElectronicFenceArea::getElectronicId, id) |
| | |
| | | } |
| | | xpElectronicFenceAreaService.saveBatch(electronicFenceAreaList); |
| | | return R.ok(electronicFence.getId()); |
| | | } |
| | | |
| | | private String buildCopyFenceName(String sourceName) { |
| | | Set<String> existingNames = new HashSet<>(xpElectronicFenceService.lambdaQuery() |
| | | .likeRight(XpElectronicFence::getElectronicName, sourceName) |
| | | .list() |
| | | .stream() |
| | | .map(XpElectronicFence::getElectronicName) |
| | | .collect(Collectors.toSet())); |
| | | |
| | | String candidateName = sourceName + "_副本"; |
| | | while (existingNames.contains(candidateName)) { |
| | | candidateName = candidateName + "_副本"; |
| | | } |
| | | return candidateName; |
| | | } |
| | | |
| | | /** |
| | |
| | | XpElectronicFence xpElectronicFence = xpElectronicFenceService.getById(fenceId); |
| | | return R.ok(xpElectronicFence); |
| | | } |
| | | |
| | | /** |
| | | * 校验围栏生效时间和失效时间的合法性: |
| | | * <ul> |
| | | * <li>生效类型不是立即生效(effectType != 1)时,生效时间不能早于当前时间</li> |
| | | * <li>失效类型不是永不失效(failureType != 1)时,失效时间不能早于当前时间</li> |
| | | * </ul> |
| | | * |
| | | * @param status |
| | | * @param effectType 生效类型(1=立即生效 2=定时生效) |
| | | * @param effectTime 生效时间 |
| | | * @param failureType 失效类型(1=永不失效 2=定时失效) |
| | | * @param failureTime 失效时间 |
| | | */ |
| | | private void validateFenceTime(@NotNull Integer status, Integer effectType, LocalDateTime effectTime, |
| | | Integer failureType, LocalDateTime failureTime) { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | // 发布/启用时,定时生效的生效时间不能早于当前时间 |
| | | if (requiresScheduleValidation(status) && effectType != null && effectType != 1) { |
| | | if (effectTime == null || effectTime.isBefore(now)) { |
| | | throw new ServiceException("围栏生效时间不合法,定时生效时生效时间不能早于当前时间", |
| | | I18nKeyConstant.FENCE_EFFECT_TIME_INVALID); |
| | | } |
| | | } |
| | | // 发布/启用时,定时失效的失效时间不能早于当前时间 |
| | | if (requiresScheduleValidation(status) && failureType != null && failureType != 1) { |
| | | if (failureTime == null || failureTime.isBefore(now)) { |
| | | throw new ServiceException("围栏失效时间不合法,定时失效时失效时间不能早于当前时间", |
| | | I18nKeyConstant.FENCE_FAILURE_TIME_INVALID); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean requiresScheduleValidation(Integer status) { |
| | | return status != null && (status == 1 || status == 2); |
| | | } |
| | | } |