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

import cn.com.taiji.track.constants.WarningCodeConstants;
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 Map<String, String> onlineMap= new ConcurrentHashMap<>();
    private Map<String, String> anchorMap= new ConcurrentHashMap<>();
    private Map<String, String> inportMap= new ConcurrentHashMap<>();

    @Override
    public void pushOnLineDeviceId(String deviceId, String start) {
        onlineMap.put(deviceId, start);
    }

    @Override
    public void pushAnchorDeviceId(String deviceId, String start) {
        anchorMap.put(deviceId, start);
    }

    @Override
    public void pushinportDeviceId(String deviceId, String inportCode) {
        inportMap.put(deviceId, inportCode);
    }

    @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();
            List<ShipStatusEntity> statusEntityList = baseMapper.selectBatchIds(map.keySet());
            if(statusEntityList.size() > 0) {
                for(ShipStatusEntity statusEntity : statusEntityList){
                    String start = map.get(statusEntity.getDeviceId());
                    ShipStatusEntity.AnchorStatus status = ShipStatusEntity.AnchorStatus.getStatus(start);
                    if(!status.equals(statusEntity.getIsAnchor())){
                        statusEntity.setIsAnchor(status);
                        statusEntity.setAnchorChangeTime(new Date());
                        saveList.add(statusEntity);
                    }
                    map.remove(statusEntity.getDeviceId());
                }
            }
            for (String id : map.keySet()) {
                String start = map.get(id);
                ShipStatusEntity.AnchorStatus status = ShipStatusEntity.AnchorStatus.getStatus(start);
                ShipStatusEntity entity = new ShipStatusEntity();
                entity.setDeviceId(id);
                entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
                entity.setOnlineChangeTime(new Date());
                entity.setIsAnchor(status);
                entity.setAnchorChangeTime(new Date());
                saveList.add(entity);
            }
        }
        if(!saveList.isEmpty()){
            saveBatch(saveList);
            log.info("抛锚状态写入数据库完成！耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
        }
    }
    @Override
    public void InportCacheToMySql() {
        Date begin = new Date();
        List<ShipStatusEntity> saveList = new ArrayList<>();
        if (inportMap.size() > 0) {
            Map<String, String> map = new HashMap<>(inportMap);
            inportMap.clear();
            List<ShipStatusEntity> statusEntityList = baseMapper.selectBatchIds(map.keySet());
            if(statusEntityList.size() > 0) {
                for(ShipStatusEntity statusEntity : statusEntityList){
                    String inportCode = map.get(statusEntity.getDeviceId());
                    ShipStatusEntity.InportStatus status = ShipStatusEntity.InportStatus.getStatus(inportCode);
                    if(!status.equals(statusEntity.getIsAnchor())){
                        statusEntity.setIsInport(status);
                        statusEntity.setInportChangeTime(new Date());
                        saveList.add(statusEntity);
                    }
                    map.remove(statusEntity.getDeviceId());
                }
            }
            for (String id : map.keySet()) {
                String inportCode = map.get(id);
                ShipStatusEntity.InportStatus status = ShipStatusEntity.InportStatus.getStatus(inportCode);
                ShipStatusEntity entity = new ShipStatusEntity();
                entity.setDeviceId(id);
                entity.setIsOnline(ShipStatusEntity.OnlineStatus.ONLINE);
                entity.setOnlineChangeTime(new Date());
                entity.setIsInport(status);
                entity.setInportChangeTime(new Date());
                saveList.add(entity);
            }
            if (!saveList.isEmpty()) {
                saveBatch(saveList);
                log.info("进出港状态写入数据库完成！耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
            }
        }
    }

    @Override
    public void onlineCacheToMySql() {
        Date begin = new Date();
        List<ShipStatusEntity> saveList = new ArrayList<>();
        if(onlineMap.size() > 0) {
            Map<String, String> map = new HashMap<>(onlineMap);
            onlineMap.clear();
            List<ShipStatusEntity> statusEntityList = baseMapper.selectBatchIds(map.keySet());
            if(statusEntityList.size() > 0){
                for(ShipStatusEntity statusEntity : statusEntityList){
                    String start = map.get(statusEntity.getDeviceId());
                    ShipStatusEntity.OnlineStatus status = ShipStatusEntity.OnlineStatus.getStatus(start);
                    if(!ShipStatusEntity.OnlineStatus.MARK.equals(statusEntity.getIsOnline()) && !status.equals(statusEntity.getIsOnline())){
                        statusEntity.setIsOnline(status);
                        statusEntity.setOnlineChangeTime(new Date());
                        saveList.add(statusEntity);
                    }
                    map.remove(statusEntity.getDeviceId());
                }
            }
            for (String id : map.keySet()) {
                String start = map.get(id);
                ShipStatusEntity.OnlineStatus status = ShipStatusEntity.OnlineStatus.getStatus(start);
                ShipStatusEntity entity = new ShipStatusEntity();
                entity.setDeviceId(id);
                entity.setIsOnline(status);
                entity.setOnlineChangeTime(new Date());
                saveList.add(entity);
            }
        }
        if(!saveList.isEmpty()){
            saveBatch(saveList);
            log.info("在线状态写入数据库完成！耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
        }
    }
}
