package com.ard.tianyu.service.impl; import com.ard.common.utils.DateUtils; import com.ard.tianyu.domain.UavBox; import com.ard.tianyu.domain.receiver.BoxInfoReceiver; import com.ard.tianyu.mapper.UavBoxMapper; import com.ard.tianyu.service.ITyService; import com.ard.tianyu.service.IUavBoxService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** * 无人机盒子Service业务层处理 * * @author ruoyi * @date 2025-03-04 */ @Service public class UavBoxServiceImpl implements IUavBoxService { @Resource private UavBoxMapper uavBoxMapper; @Resource private ITyService tyService; /** * 查询无人机盒子 * * @param id 无人机盒子主键 * @return 无人机盒子 */ @Override public UavBox selectUavBoxById(Long id) { return uavBoxMapper.selectUavBoxById(id); } /** * 查询无人机盒子列表 * * @param uavBox 无人机盒子 * @return 无人机盒子 */ @Override public List selectUavBoxList(UavBox uavBox) { return uavBoxMapper.selectUavBoxList(uavBox); } /** * 新增无人机盒子 * * @param uavBox 无人机盒子 * @return 结果 */ @Override public int insertUavBox(UavBox uavBox) { uavBox.setCreateTime(DateUtils.getNowDate()); return uavBoxMapper.insertUavBox(uavBox); } /** * 修改无人机盒子 * * @param uavBox 无人机盒子 * @return 结果 */ @Override public int updateUavBox(UavBox uavBox) { uavBox.setUpdateTime(DateUtils.getNowDate()); return uavBoxMapper.updateUavBox(uavBox); } /** * 批量删除无人机盒子 * * @param ids 需要删除的无人机盒子主键 * @return 结果 */ @Override public int deleteUavBoxByIds(Long[] ids) { return uavBoxMapper.deleteUavBoxByIds(ids); } /** * 删除无人机盒子信息 * * @param id 无人机盒子主键 * @return 结果 */ @Override public int deleteUavBoxById(Long id) { return uavBoxMapper.deleteUavBoxById(id); } /** * 同步盒子属性 * * @return 结果 */ @Scheduled(fixedRate = 1000 * 60) public void syncBoxInfo() { List uavBoxeList = selectUavBoxList(new UavBox()); // 将本地列表转换为Map,方便快速查找 Map localUavBoxMap = uavBoxeList.stream() .collect(Collectors.toMap(UavBox::getBoxSn, drone -> drone)); //遍历接口最新获取的无人机盒子信息 tyService.getBoxList().forEach(remoteUavBox -> { UavBox localUavBox = localUavBoxMap.get(remoteUavBox.getBoxSn()); if (localUavBox != null) { if (!localUavBox.isSameAs(remoteUavBox)) { // 修改无人机(只更新变化的属性) updateChangedAttributes(localUavBox, remoteUavBox); updateUavBox(localUavBox); } } }); } // 更新变化的属性 private void updateChangedAttributes(UavBox localBoxInfo, BoxInfoReceiver remoteUavBox) { if (!Objects.equals(localBoxInfo.getBoxName(), remoteUavBox.getBoxName())) { localBoxInfo.setBoxName(remoteUavBox.getBoxName()); } if (!Objects.equals(localBoxInfo.getBoxVersion(), remoteUavBox.getBoxVersion())) { localBoxInfo.setBoxVersion(remoteUavBox.getBoxVersion()); } if (!Objects.equals(localBoxInfo.getLastTaskId(), remoteUavBox.getLastTaskId())) { localBoxInfo.setLastTaskId(remoteUavBox.getLastTaskId()); } if (!Objects.equals(localBoxInfo.getOnLine(), remoteUavBox.getOnLine())) { localBoxInfo.setOnLine(remoteUavBox.getOnLine()); } // 更新其他属性 } }