|
@@ -7,10 +7,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Vector;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
* @author kok20
|
|
@@ -19,9 +17,9 @@ import java.util.Vector;
|
|
|
@Slf4j
|
|
|
public class ShipStatusServiceImpl extends ServiceImpl<ShipStatusMapper, ShipStatusEntity> implements IShipStatusService {
|
|
|
|
|
|
- private Vector<ShipStatusEntity> entities= new Vector<>();
|
|
|
private Vector<String> offLineDeviceIds= new Vector<>();
|
|
|
private Vector<String> onLineDeviceIds= new Vector<>();
|
|
|
+ private Map<String, String> anchorMap= new ConcurrentHashMap<>();
|
|
|
|
|
|
@Override
|
|
|
public void pushOffLineDeviceId(String deviceId){
|
|
@@ -32,9 +30,16 @@ public class ShipStatusServiceImpl extends ServiceImpl<ShipStatusMapper, ShipSta
|
|
|
public void pushOnLineDeviceId(String deviceId) {
|
|
|
onLineDeviceIds.add(deviceId);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void pushAnchorDeviceId(String deviceId, String start) {
|
|
|
+ anchorMap.put(deviceId, start);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void cacheToMySql() {
|
|
|
Date begin = new Date();
|
|
|
+ List<ShipStatusEntity> saveList = new ArrayList<>();
|
|
|
if(offLineDeviceIds.size() > 0){
|
|
|
List<String> idList = new ArrayList<>(offLineDeviceIds);
|
|
|
offLineDeviceIds.clear();
|
|
@@ -45,11 +50,11 @@ public class ShipStatusServiceImpl extends ServiceImpl<ShipStatusMapper, ShipSta
|
|
|
entity.setDeviceId(id);
|
|
|
entity.setIsOnline(ShipStatusEntity.OnlineStatus.OFFLINE);
|
|
|
entity.setOnlineChangeTime(new Date());
|
|
|
- entities.add(entity);
|
|
|
- }else if (!ShipStatusEntity.OnlineStatus.OFFLINE.equals(entity.getIsOnline())) {
|
|
|
+ saveList.add(entity);
|
|
|
+ }else if (ShipStatusEntity.OnlineStatus.ONLINE.equals(entity.getIsOnline())) {
|
|
|
entity.setIsOnline(ShipStatusEntity.OnlineStatus.OFFLINE);
|
|
|
entity.setOnlineChangeTime(new Date());
|
|
|
- entities.add(entity);
|
|
|
+ saveList.add(entity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -63,15 +68,47 @@ public class ShipStatusServiceImpl extends ServiceImpl<ShipStatusMapper, ShipSta
|
|
|
entity.setDeviceId(id);
|
|
|
entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
|
|
|
entity.setOnlineChangeTime(new Date());
|
|
|
- entities.add(entity);
|
|
|
- }else if (!ShipStatusEntity.OnlineStatus.ONLINE.equals(entity.getIsOnline())) {
|
|
|
+ saveList.add(entity);
|
|
|
+ }else if (ShipStatusEntity.OnlineStatus.OFFLINE.equals(entity.getIsOnline())) {
|
|
|
entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
|
|
|
entity.setOnlineChangeTime(new Date());
|
|
|
- entities.add(entity);
|
|
|
+ saveList.add(entity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- saveBatch(entities);
|
|
|
- log.info("船舶状态写入数据库完成!耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), entities.size());
|
|
|
+ if(!saveList.isEmpty()){
|
|
|
+ saveBatch(saveList);
|
|
|
+ log.info("船舶状态写入数据库完成!耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void anchorCacheToMySql() {
|
|
|
+ Date begin = new Date();
|
|
|
+ List<ShipStatusEntity> saveList = new ArrayList<>();
|
|
|
+ if(anchorMap.size() > 0){
|
|
|
+ Map<String,String> map = new HashMap<>(anchorMap);
|
|
|
+ anchorMap.clear();
|
|
|
+ for (String id : anchorMap.keySet()) {
|
|
|
+ String start = map.get(id);
|
|
|
+ ShipStatusEntity entity = baseMapper.selectById(id);
|
|
|
+ ShipStatusEntity.AnchorStatus status = ShipStatusEntity.AnchorStatus.getStatus(start);
|
|
|
+ if (entity == null) {
|
|
|
+ entity = new ShipStatusEntity();
|
|
|
+ entity.setDeviceId(id);
|
|
|
+ entity.setIsAnchor(status);
|
|
|
+ entity.setAnchorChangeTime(new Date());
|
|
|
+ saveList.add(entity);
|
|
|
+ }else if (!status.equals(entity.getIsAnchor())) {
|
|
|
+ entity.setIsAnchor(status);
|
|
|
+ entity.setAnchorChangeTime(new Date());
|
|
|
+ saveList.add(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!saveList.isEmpty()){
|
|
|
+ saveBatch(saveList);
|
|
|
+ log.info("抛锚状态写入数据库完成!耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
|
|
|
+ }
|
|
|
}
|
|
|
}
|