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

import cn.com.taiji.track.constants.WarningCodeConstants;
import cn.com.taiji.track.entity.BeidouTrackEntity;
import cn.com.taiji.track.entity.WarningRecordEntity;
import cn.com.taiji.track.mapper.WarningAreaMapper;
import cn.com.taiji.track.mapper.WarningRecordMapper;
import cn.com.taiji.track.service.IShipStatusService;
import cn.com.taiji.track.service.IWarningRecordService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author kok20
 */
@Service
@Slf4j
public class WarningRecordServiceImpl extends ServiceImpl<WarningRecordMapper, WarningRecordEntity> implements IWarningRecordService {

    @Autowired
    protected IShipStatusService shipStatusService;

    private  Vector<WarningRecordEntity> entities= new Vector<>();

    @Override
    public void warningRecordToCache(List<ConsumerRecord<?, ?>> records){
        try {
            for (ConsumerRecord<?, ?> record : records) {
                Optional message = Optional.ofNullable(record.value());
                if (message.isPresent()) {
                    Object msg = message.get();
                    WarningRecordEntity entity = JSONObject.parseObject(msg.toString(), WarningRecordEntity.class);
                    BeidouTrackEntity track = JSONObject.parseObject(entity.getDynamicShip(), BeidouTrackEntity.class);
                    entity.setDeviceId(track.getDeviceId());
                    entity.setCreateTime(new Date());
                    entity.setUpdateTime(new Date());
                    entities.add(entity);
                    if (WarningCodeConstants.OFFLINE_WARNING.equals(entity.getModelCode())) {
                        shipStatusService.pushOnLineDeviceId(entity.getDeviceId(),entity.getStart());
                    }
                    if (WarningCodeConstants.ANCHOR_WARNING.equals(entity.getModelCode())) {
                        shipStatusService.pushAnchorDeviceId(entity.getDeviceId(),entity.getStart());
                    }
                    //不再使用预警判断在港状态
//                    if (WarningCodeConstants.INPORT_WARNING.equals(entity.getModelCode())) {
//                        shipStatusService.pushinportDeviceId(entity.getDeviceId(),WarningCodeConstants.INPORT_WARNING);
//                    }
//                    if (WarningCodeConstants.OUTPROT_WARNING.equals(entity.getModelCode())) {
//                        shipStatusService.pushinportDeviceId(entity.getDeviceId(),WarningCodeConstants.OUTPROT_WARNING);
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void cacheToMySql() {
        Date begin = new Date();
        if(entities.size() > 0){
            List<WarningRecordEntity> saveList = new ArrayList<>(entities);
            entities.clear();
            saveBatch(saveList);
            log.info("预警数据写入数据库完成！耗时 {} 毫秒, 合计保存 {} 条记录", (new Date()).getTime() - begin.getTime(), saveList.size());
        }
    }
}
