package com.ard.tianyu.service.impl;
|
|
import com.ard.common.utils.DateUtils;
|
import com.ard.tianyu.domain.entity.UavBoxEntity;
|
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 UavBoxEntity selectUavBoxById(Long id) {
|
return uavBoxMapper.selectUavBoxById(id);
|
}
|
|
/**
|
* 查询无人机盒子列表
|
*
|
* @param uavBoxEntity 无人机盒子
|
* @return 无人机盒子
|
*/
|
@Override
|
public List<UavBoxEntity> selectUavBoxList(UavBoxEntity uavBoxEntity) {
|
return uavBoxMapper.selectUavBoxList(uavBoxEntity);
|
}
|
|
/**
|
* 新增无人机盒子
|
*
|
* @param uavBoxEntity 无人机盒子
|
* @return 结果
|
*/
|
@Override
|
public int insertUavBox(UavBoxEntity uavBoxEntity) {
|
uavBoxEntity.setCreateTime(DateUtils.getNowDate());
|
return uavBoxMapper.insertUavBox(uavBoxEntity);
|
}
|
|
/**
|
* 修改无人机盒子
|
*
|
* @param uavBoxEntity 无人机盒子
|
* @return 结果
|
*/
|
@Override
|
public int updateUavBox(UavBoxEntity uavBoxEntity) {
|
uavBoxEntity.setUpdateTime(DateUtils.getNowDate());
|
return uavBoxMapper.updateUavBox(uavBoxEntity);
|
}
|
|
/**
|
* 批量删除无人机盒子
|
*
|
* @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<UavBoxEntity> uavBoxeListEntity = selectUavBoxList(new UavBoxEntity());
|
// 将本地列表转换为Map,方便快速查找
|
Map<String, UavBoxEntity> localUavBoxMap = uavBoxeListEntity.stream()
|
.collect(Collectors.toMap(UavBoxEntity::getBoxSn, drone -> drone));
|
//遍历接口最新获取的无人机盒子信息
|
tyService.getBoxList().forEach(remoteUavBox -> {
|
UavBoxEntity localUavBoxEntity = localUavBoxMap.get(remoteUavBox.getBoxSn());
|
if (localUavBoxEntity != null) {
|
if (!localUavBoxEntity.isSameAs(remoteUavBox)) {
|
// 修改无人机(只更新变化的属性)
|
updateChangedAttributes(localUavBoxEntity, remoteUavBox);
|
updateUavBox(localUavBoxEntity);
|
}
|
}
|
});
|
}
|
|
// 更新变化的属性
|
private void updateChangedAttributes(UavBoxEntity 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());
|
}
|
// 更新其他属性
|
}
|
}
|