| | |
| | | package com.ard.work.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ard.common.exception.ServiceException; |
| | | import com.ard.common.utils.DateUtils; |
| | | import com.ard.common.utils.uuid.IdUtils; |
| | | import com.ard.system.authorize.service.AuthorizeService; |
| | |
| | | import com.ard.work.service.IArdDroneInfoService; |
| | | import com.ard.work.service.ITyService; |
| | | import com.ard.work.util.UtilMethod; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | * @author ruoyi |
| | | * @date 2025-03-04 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class ArdDroneInfoServiceImpl implements IArdDroneInfoService { |
| | | |
| | |
| | | |
| | | @Resource |
| | | private ITyService tyService; |
| | | |
| | | @Resource |
| | | private AuthorizeService authorizeService; |
| | | |
| | | UtilMethod utilMethod = new UtilMethod(); |
| | | |
| | |
| | | /** |
| | | * 批量新增无人机盒子 |
| | | * |
| | | * @param ids 无人机盒子主键集合 |
| | | * @return 结果 |
| | | * @param boxSn 盒子编码数组 |
| | | * @param deptId 部门ID |
| | | * @return 成功插入的记录数 |
| | | */ |
| | | @Override |
| | | public int insertArdDroneLs(String[] ids) { |
| | | // 获取无人机盒子信息 |
| | | List<ArdDroneInfo> drone = new ArrayList<>(); |
| | | for (String id : ids) { |
| | | ArdDroneInfo ardDrone = ArdDroneInfoMapper.selectArdDroneById(id); |
| | | drone.add(ardDrone); |
| | | public int insertArdDroneLs(String[] boxSn, Long deptId) { |
| | | // 参数校验 |
| | | if (boxSn == null || boxSn.length == 0) { |
| | | log.warn("传入的盒子编码为空"); |
| | | return 1; |
| | | } |
| | | // 检验数量droneNum是否合规。 |
| | | utilMethod.authorizationVerification(drone.size()); |
| | | // 插入 |
| | | if (!drone.isEmpty()) { |
| | | return ArdDroneInfoMapper.insertArdDroneLs(drone); |
| | | if (deptId == null) { |
| | | throw new IllegalArgumentException("部门ID不能为空"); |
| | | } |
| | | try { |
| | | // 1. 检查已存在的无人机盒子 |
| | | List<ArdDroneInfo> existingDrones = getExistingDrones(boxSn); |
| | | // 2. 授权数量验证 |
| | | validateAuthorization(existingDrones.size(), boxSn.length); |
| | | // 3. 获取并组装新的无人机信息 |
| | | List<ArdDroneInfo> newDrones = assembleNewDrones(boxSn, deptId); |
| | | // 4. 批量插入 |
| | | return batchInsertDrones(newDrones); |
| | | } catch (Exception e) { |
| | | log.error("批量新增无人机盒子失败, boxSn: {}, deptId: {}", Arrays.toString(boxSn), deptId, e); |
| | | throw new ServiceException("批量新增无人机盒子失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取已存在的无人机信息 |
| | | */ |
| | | private List<ArdDroneInfo> getExistingDrones(String[] boxSn) { |
| | | return Arrays.stream(boxSn) |
| | | .map(ArdDroneInfoMapper::selectArdDroneByBoxSn) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 验证授权数量 |
| | | */ |
| | | private void validateAuthorization(int existingCount, int newCount) { |
| | | int totalCount = existingCount + newCount; |
| | | |
| | | JSONObject authInfo = authorizeService.getAuthorizationInfo(); |
| | | String authType = authInfo.getString("Type"); |
| | | |
| | | switch (authType) { |
| | | case "normal": |
| | | int allowedDroneNum = authInfo.getIntValue("droneNum"); |
| | | if (totalCount > allowedDroneNum) { |
| | | throw new RuntimeException(String.format( |
| | | "授权数量%d,当前操作将导致总数达到%d,超过授权限制", |
| | | allowedDroneNum, totalCount)); |
| | | } |
| | | break; |
| | | case "expired": |
| | | throw new RuntimeException("授权文件已过期"); |
| | | case "mismatch": |
| | | throw new RuntimeException("授权文件不匹配"); |
| | | default: |
| | | throw new RuntimeException("未知的授权类型: " + authType); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 组装新的无人机信息 |
| | | */ |
| | | private List<ArdDroneInfo> assembleNewDrones(String[] boxSn, Long deptId) { |
| | | List<BoxInfoReceiver> allBoxes = tyService.getBoxList(); |
| | | Set<String> targetBoxSnSet = new HashSet<>(Arrays.asList(boxSn)); |
| | | return allBoxes.stream() |
| | | .filter(box -> targetBoxSnSet.contains(box.getBoxSn())) |
| | | .map(box -> createDroneInfo(box, deptId)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 创建无人机信息对象 |
| | | */ |
| | | private ArdDroneInfo createDroneInfo(BoxInfoReceiver box, Long deptId) { |
| | | ArdDroneInfo drone = new ArdDroneInfo(); |
| | | drone.setId(IdUtils.simpleUUID()); |
| | | drone.setBoxSn(box.getBoxSn()); |
| | | drone.setBoxName(box.getBoxName()); |
| | | drone.setBoxAliasName(box.getBoxName()); |
| | | drone.setStartTime(new Date()); |
| | | drone.setOnLine(box.getOnLine()); |
| | | drone.setVideoServer(box.getVideoServer()); |
| | | drone.setVideoPort(box.getVideoPort()); |
| | | drone.setLastTaskId(box.getLastTaskId()); |
| | | drone.setDeptId(deptId); |
| | | |
| | | // 设置创建信息和更新信息 |
| | | setCreateAndUpdateInfo(drone); |
| | | |
| | | return drone; |
| | | } |
| | | |
| | | /** |
| | | * 设置创建和更新信息 |
| | | */ |
| | | private void setCreateAndUpdateInfo(ArdDroneInfo drone) { |
| | | Date currentTime = new Date(); |
| | | drone.setCreateTime(currentTime); |
| | | drone.setUpdateTime(currentTime); |
| | | } |
| | | |
| | | /** |
| | | * 批量插入无人机信息 |
| | | */ |
| | | private int batchInsertDrones(List<ArdDroneInfo> drones) { |
| | | if (drones.isEmpty()) { |
| | | return 0; |
| | | } |
| | | return ArdDroneInfoMapper.insertArdDroneLs(drones); |
| | | } |
| | | |
| | | /** |
| | | * 同步盒子属性 |