package cn.com.taiji.track.listener;

import cn.com.taiji.track.dto.HistoryTrackQueryDTO;
import cn.com.taiji.track.entity.BeidouTrackEntity;
import cn.com.taiji.track.event.HistoryTrackChannelAddEvent;
import cn.com.taiji.track.handler.HistoryTrackHandler;
import cn.com.taiji.track.service.IBeidouTrackService;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author chenfangchao
 * @title: BeidouChannelListener
 * @projectName ax-geomesa-redis
 * @description: TODO
 * @date 2022/9/22 6:00 PM
 */
@Component
@Slf4j
public class HistoryTrackChannelListener {
    @Autowired
    private HistoryTrackHandler historyTrackHandler;
    @Autowired
    private IBeidouTrackService beidouTrackService;

    private static final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-pool-channel-history-track").build();

    ThreadPoolExecutor threadPoolExecutor  = new ThreadPoolExecutor(2,2,5,
            TimeUnit.MINUTES,new ArrayBlockingQueue<>(10),threadFactory,new ThreadPoolExecutor.AbortPolicy());
    /**
     * 监听新增用户信道
     *
     * @param event
     */
    @EventListener
    public void listenerNettyAddChannel(HistoryTrackChannelAddEvent event) {
        log.info("当前需要创建的线程名称为 {}", event.getChannelId());
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                queryHistoryTrack(event);
            }
        });
    }
    /**
     * 查询
     */
    private void queryHistoryTrack(HistoryTrackChannelAddEvent event)  {
        List<BeidouTrackEntity> results = new ArrayList<>();
        AttributeKey<String> key = AttributeKey.valueOf("history-track");
        String dtoStr = event.getCtx().attr(key).get();
        HistoryTrackQueryDTO dto = JSONObject.parseObject(dtoStr, HistoryTrackQueryDTO.class);
        if (null != dto) {
            QueryWrapper<BeidouTrackEntity> wrapper = new QueryWrapper();
            if(StringUtils.hasText(dto.getShipId())){
                wrapper.eq("device_id",dto.getShipId());
            }
            if(StringUtils.hasText(dto.getStartTime())&&StringUtils.hasText(dto.getEndTime())){
                wrapper.between("location_time",dto.getStartTime(),dto.getEndTime());
            }
            wrapper.orderByAsc("location_time");
            results = beidouTrackService.list(wrapper);
        }
        historyTrackHandler.sendMsgToSpecifyChannel(event.getChannelId(), JSONUtil.toJsonStr(results));
    }

}
