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

import cn.com.taiji.track.dto.IGeomesaTrackDTO;
import cn.com.taiji.track.entity.BeidouLocationEntity;
import cn.com.taiji.track.entity.BeidouTrackEntity;
import cn.com.taiji.track.mapper.BeidouTrackMapper;
import cn.com.taiji.track.service.IBeidouTrackService;
import cn.hutool.json.JSONUtil;
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.geotools.data.FeatureReader;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.identity.FeatureIdImpl;
import org.geotools.util.factory.Hints;
import org.locationtech.geomesa.redis.data.RedisDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description:
 * @Author: hujie@umisoft.cn
 * @Date: 2021/11/30 9:59 下午
 */
@Service
@Slf4j
public class BeidouTrackServiceImpl extends ServiceImpl<BeidouTrackMapper, BeidouTrackEntity> implements IBeidouTrackService {

    private  Vector<BeidouTrackEntity> trackEntities= new Vector<>();

    @Override
    public void beidouDynamicShipToCache(List<ConsumerRecord<?, ?>> records){
        try {
            for (ConsumerRecord<?, ?> record : records) {
                Optional message = Optional.ofNullable(record.value());
                if (message.isPresent()) {
                    Object msg = message.get();
                    BeidouTrackEntity entity = JSONObject.parseObject(msg.toString(), BeidouTrackEntity.class);
                    trackEntities.add(entity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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