package com.mosty.wzzx.task;

import com.mosty.base.model.entity.qwzx.TbQwZnzb;
import com.mosty.base.model.entity.wzzx.TbWzSbsswz;
import com.mosty.base.model.entity.wzzx.TbWzXfwz;
import com.mosty.base.utils.Constant;
import com.mosty.base.utils.DistanceUtils;
import com.mosty.base.utils.JtsUtils;
import com.mosty.common.redis.service.RedisService;
import com.mosty.wzzx.service.TbWzXfwzService;
import com.mosty.wzzx.service.impl.TbQwZnzbServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 位置定时器
 */
@Slf4j
@Component
@AllArgsConstructor
public class LocationCenterTask {

    private final RedisService redisService;
    private final RedisTemplate redisTemplate;
    private final TbWzXfwzService tbWzXfwzService;
    private final TbQwZnzbServiceImpl tbQwZnzbService;

    //    @Scheduled(cron = "0 0/2 * * * ? ")
    public void locationCompletion() {
        System.out.println("开始执行位置定时器补全坐标位置");
        //巡防位置
        String key = Constant.WZ_XFWZ + "*";
        Set<String> sets = redisTemplate.keys(key);
        if (!CollectionUtils.isEmpty(sets)) {
            List<TbWzXfwz> zfwzList = redisTemplate.opsForValue().multiGet(sets);
            //设备实时位置
            String sbwzKey = Constant.WZ_SBWZ + "*";
            Set<String> sbwzSets = redisTemplate.keys(sbwzKey);
            if (!CollectionUtils.isEmpty(sbwzSets)) {
                List<TbWzSbsswz> sbwzList = redisTemplate.opsForValue().multiGet(sbwzSets);
                if (!CollectionUtils.isEmpty(zfwzList) && !CollectionUtils.isEmpty(sbwzList)) {
                    List<String> bbList = zfwzList.stream().map(TbWzXfwz::getBbId).collect(Collectors.toList());
                    List<TbQwZnzb> znnbList = tbQwZnzbService.queryListByYwid(bbList);
                    for (TbWzXfwz xfwz : zfwzList) {
                        Date zhgxsj = xfwz.getZhgxsj();
                        //上报时长
                        long sbsc = System.currentTimeMillis() - zhgxsj.getTime();
                        if (sbsc >= 60000) {//超过1分钟离线
                            xfwz.setZxzt("0");
                            if (!CollectionUtils.isEmpty(znnbList)) {
                                //获取当前报备的智能装备
                                List<TbQwZnzb> bbzbList =
                                        znnbList.stream().filter(item -> item.getYwid().equals(xfwz.getBbId())).collect(Collectors.toList());
                                //获取报备装备呼号
                                List<String> hhList = bbzbList.stream().map(TbQwZnzb::getHh).collect(Collectors.toList());
                                //获取缓存报备装备位置
                                List<TbWzSbsswz> bbSbwzList =
                                        sbwzList.stream().filter(entity -> hhList.contains(entity.getSbid())).collect(Collectors.toList());
                                if (!CollectionUtils.isEmpty(bbSbwzList)) {
                                    //对报备装备根据定位时间排序
                                    bbSbwzList.stream().sorted(Comparator.comparing(TbWzSbsswz::getDwsj).reversed());
                                    TbWzSbsswz sswz = bbSbwzList.get(0);
                                    //如果实时位置最新坐标的时间大于
                                    if (sswz.getDwsj().getTime() > xfwz.getZhgxsj().getTime()) {
                                        //更新巡防位置坐标
                                        BigDecimal jd = sswz.getJd();
                                        BigDecimal wd = sswz.getWd();
                                        Integer lc = DistanceUtils.getLc(xfwz.getLc(), xfwz.getJd(), xfwz.getWd(), jd,
                                                wd);
                                        Integer sc = DistanceUtils.getSc(xfwz.getZhgxsj());
                                        xfwz.setSc(sc);
                                        xfwz.setLc(lc);
                                        List<BigDecimal[]> list = JtsUtils.decodeLineString(xfwz.getZb());
                                        if (CollectionUtils.isEmpty(list)) {
                                            list = new ArrayList<>();
                                        }
                                        BigDecimal[] decimals = new BigDecimal[2];
                                        decimals[0] = jd;
                                        decimals[1] = wd;
                                        list.add(decimals);
                                        if (list.size() == 1) {
                                            list.add(decimals);
                                        }
                                        xfwz.setZb(JtsUtils.createLine(list));
                                        xfwz.setZbsl(list.size());
                                        xfwz.setDwrq(sswz.getDwrq());
                                        xfwz.setJd(jd);
                                        xfwz.setWd(wd);
                                        xfwz.setZhgxsj(sswz.getXtZhgxsj());
                                    }
                                }
                            }
                            tbWzXfwzService.update(xfwz);
                            String redisKey = Constant.WZ_XFWZ + xfwz.getBbId();
                            redisService.setCacheObject(redisKey, xfwz);
                        }
                    }
                }
            }
        }
    }
}