package com.gci.boot.modules.gis.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gci.boot.common.util.RedisKey;
import com.gci.boot.modules.gis.entity.TbGisFenceAlarm;
import com.gci.boot.modules.gis.entity.TbGisSpeedingAlarm;
import com.gci.boot.modules.gis.vo.FenceSpeedBeanVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class GpsConsumerService{

    @Autowired
    private ITbGisSpeedingAlarmService speedingAlarmService;
    @Autowired
    private ITbGisFenceAlarmService fenceAlarmService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //数据入库
    public void saveHandle(FenceSpeedBeanVO vo){
        if(1 == vo.getType()){
            //围栏告警
            TbGisFenceAlarm fenceAlarm = JSONUtil.toBean(vo.getData(),TbGisFenceAlarm.class);
            LambdaQueryWrapper<TbGisFenceAlarm> fenceQuery = new LambdaQueryWrapper<>();
            fenceQuery.eq(TbGisFenceAlarm::getPlateNumber,fenceAlarm.getPlateNumber());
            //围栏id标识。新增和完结告警必传围栏id。 车辆掉线时自动任务推送不传围栏id。处理该车所有的未结束围栏告警。
            if(StringUtils.isNotBlank(fenceAlarm.getFenceId())){
                fenceQuery.eq(TbGisFenceAlarm::getFenceId,fenceAlarm.getFenceId());
            }
            fenceQuery.isNull(TbGisFenceAlarm::getEndTime);
            List<TbGisFenceAlarm> list = fenceAlarmService.list(fenceQuery);
            //类型为开始 且该车辆 该围栏并没有正在进行中的告警记录，则说明是新增，否则不新增
            if(vo.getFlag() == 1 && list.isEmpty()){
                fenceAlarm.setStartTime(fenceAlarm.getAlarmTime());
                fenceAlarmService.save(fenceAlarm);
            }
            //类型为结束的，则说明是修改
            if(vo.getFlag() == 2){
                for(TbGisFenceAlarm one:list){
                    //判断是否为-结束->找到开始的记录，更新结束信息
                    fenceAlarm.setEndTime(fenceAlarm.getAlarmTime());
                    fenceAlarmService.updateById(one);
                    // TODO: 2023/10/18 屏蔽自动关闭逻辑，无数据产生不需要删除
//                    redisTemplate.opsForZSet().remove(RedisKey.FENCE_VEHICLE_SCORE,one.getPlateNumber()+"-"+one.getId()+"-"+one.getFenceId());
                }
            }
        }else if(2 == vo.getType()){
            //超速告警
            TbGisSpeedingAlarm speedingAlarm = JSONUtil.toBean(vo.getData(),TbGisSpeedingAlarm.class);
            LambdaQueryWrapper<TbGisSpeedingAlarm> speedQuery = new LambdaQueryWrapper<>();
            speedQuery.eq(TbGisSpeedingAlarm::getPlateNumber,speedingAlarm.getPlateNumber())
                    .isNull(TbGisSpeedingAlarm::getEndTime);
            TbGisSpeedingAlarm one = speedingAlarmService.getOne(speedQuery);
            if(vo.getFlag() == 1){
                //判断是否为-开始->插入一条
                if(Objects.isNull(one)){
                    speedingAlarmService.save(speedingAlarm);
                }
            }else if(vo.getFlag() == 2){
                //判断是否为-结束->找到开始的记录，更新结束信息
                if(Objects.nonNull(one)){
                    one.setEndLat(speedingAlarm.getEndLat());
                    one.setEndLon(speedingAlarm.getEndLon());
                    one.setEndTime(speedingAlarm.getEndTime());
                    one.setSpeed(speedingAlarm.getSpeed());
                    one.setAverageSpeed(speedingAlarm.getAverageSpeed());
                    speedingAlarmService.updateById(one);
                }
            }
        }
    }

}
