package cn.com.taiji.track.service.impl;

import cn.com.taiji.track.entity.ShipStatusEntity;
import cn.com.taiji.track.mapper.ShipStatusMapper;
import cn.com.taiji.track.service.IShipStatusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author kok20
 */
@Service
@Slf4j
public class ShipStatusServiceImpl extends ServiceImpl<ShipStatusMapper, ShipStatusEntity> implements IShipStatusService {

    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){
        offLineDeviceIds.add(deviceId);
    }

    @Override
    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();
            for (String id : idList) {
                ShipStatusEntity entity = baseMapper.selectById(id);
                if (entity == null) {
                    entity = new ShipStatusEntity();
                    entity.setDeviceId(id);
                    entity.setIsOnline(ShipStatusEntity.OnlineStatus.OFFLINE);
                    entity.setOnlineChangeTime(new Date());
                    saveList.add(entity);
                }else if (ShipStatusEntity.OnlineStatus.ONLINE.equals(entity.getIsOnline())) {
                    entity.setIsOnline(ShipStatusEntity.OnlineStatus.OFFLINE);
                    entity.setOnlineChangeTime(new Date());
                    saveList.add(entity);
                }
            }
        }
        if(onLineDeviceIds.size() > 0){
            List<String> idList = new ArrayList<>(onLineDeviceIds);
            onLineDeviceIds.clear();
            for (String id : idList) {
                ShipStatusEntity entity = baseMapper.selectById(id);
                if (entity == null) {
                    entity = new ShipStatusEntity();
                    entity.setDeviceId(id);
                    entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
                    entity.setOnlineChangeTime(new Date());
                    saveList.add(entity);
                }else if (ShipStatusEntity.OnlineStatus.OFFLINE.equals(entity.getIsOnline())) {
                    entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
                    entity.setOnlineChangeTime(new Date());
                    saveList.add(entity);
                }
            }
        }
        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());
        }
    }
}
