package com.mosty.wzzx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.wzzx.LocationInfo;
import com.mosty.base.model.entity.qwzx.TbQwXfbb;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSbsswz;
import com.mosty.base.model.entity.wzzx.TbWzXfwz;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.jcgl.TbJcglSbdwVo;
import com.mosty.base.model.vo.yjzl.TbFzJlVo;
import com.mosty.base.model.vo.yjzl.TbFzycPzVo;
import com.mosty.base.utils.*;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.common.redis.service.RedisService;
import com.mosty.wzzx.mapper.TbWzSbsswzMapper;
import com.mosty.wzzx.mapper.TbWzXfwzMapper;
import com.mosty.wzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.remote.TbQwzxAdaptRemoteService;
import com.mosty.wzzx.remote.TbWebSocketAdaptRemoteService;
import com.mosty.wzzx.remote.TbYjzlAdaptRemoteService;
import com.mosty.wzzx.service.TbQwXfbbService;
import com.mosty.wzzx.service.TbWzSbsswzService;
import com.mosty.wzzx.udp.UdpLzEntity;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

@Service
@RequiredArgsConstructor
public class TbWzSbsswzServiceImpl extends ServiceImpl<TbWzSbsswzMapper, TbWzSbsswz> implements TbWzSbsswzService {

    private final RedisService redisService;
    private final TbQwzxAdaptRemoteService tbQwzxAdaptRemoteService;
    private final TbWzXfwzMapper tbWzXfwzMapper;
    private final TbYjzlAdaptRemoteService tbYjzlAdaptRemoteService;
    private final TbQwXfbbService tbQwXfbbService;

    // 查询巡组实时位置,如果5分钟没有GPS信号，则保存设备的位置信息
    private void saveXzwz(TbQwXfbb bb, TbWzSbsswz sbsswz) {
        String redisKey = Constant.WZ_XFWZ + sbsswz.getYwid();
        TbWzXfwz xfwz = redisService.getCacheObject(redisKey);
//        TbWzXfwz xfwz = this.tbWzXfwzMapper.queryByBbid(bb.getId());
        if (ObjectUtils.isNotEmpty(xfwz)) {
            Integer lc = DistanceUtils.getLc(bb.getXflc(), xfwz.getJd(), xfwz.getWd(), sbsswz.getJd(), sbsswz.getWd());
            List<BigDecimal[]> list = xfwz.getZbList();
//            if (sbsswz.getDwsj().getTime() - xfwz.getZhgxsj().getTime() >= 5 * 60 * 1000) {
            xfwz.setJd(sbsswz.getJd());
            xfwz.setWd(sbsswz.getWd());
            xfwz.setZhgxsj(new Date());
            if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            BigDecimal[] decimals = new BigDecimal[2];
            decimals[0] = sbsswz.getJd();
            decimals[1] = sbsswz.getWd();
            list.add(decimals);
            if (list.size() == 1) {
                list.add(decimals);
            }
            if (list.size() >= 3) {
                BigDecimal[] decimal0 = list.get(0);
                BigDecimal[] decimal1 = list.get(1);
                if (decimal0 == decimal1) {
                    list.remove(0);
                }
            }
            xfwz.setZb(JtsUtils.createLine(list));
            xfwz.setZbsl(list.size());
            xfwz.setZbList(list);
            xfwz.setZxzt("1");
            this.tbWzXfwzMapper.update(xfwz);
            xfwz.setZb(null);
            //修改数据 报备数据
            this.tbQwXfbbService.updateXfbbUdp(bb, sbsswz, lc);
            redisService.setCacheObject(redisKey, xfwz);
//            }
        } else {
            xfwz = new TbWzXfwz();
            xfwz.setJd(sbsswz.getJd());
            xfwz.setWd(sbsswz.getWd());
            xfwz.setZhgxsj(new Date());
            List<BigDecimal[]> list = new ArrayList<>();
            BigDecimal[] decimals = new BigDecimal[2];
            decimals[0] = sbsswz.getJd();
            decimals[1] = sbsswz.getWd();
            list.add(decimals);
            if (list.size() == 1) {
                list.add(decimals);
            }
            if (list.size() >= 3) {
                BigDecimal[] decimal0 = list.get(0);
                BigDecimal[] decimal1 = list.get(1);
                if (decimal0 == decimal1) {
                    list.remove(0);
                }
            }
            xfwz.setZb(JtsUtils.createLine(list));
            xfwz.setZbsl(list.size());
            xfwz.setZxzt("1");
            xfwz.setZbList(list);
            xfwz.setBbId(bb.getId());
            xfwz.setYhId(bb.getFzrId());
            xfwz.setYhSfzh(bb.getFzrSfzh());
            xfwz.setYhXm(bb.getFzrXm());
            xfwz.setJzId(bb.getJzId());
            xfwz.setJzMc(bb.getJzId());
            xfwz.setId(UUIDGenerator.getUUID());
            xfwz.setZbsl(list.size());
            xfwz.setDwrq(new Date());
            xfwz.setKssj(new Date());
            xfwz.setXtSjly("1");
            xfwz.setSsbm(bb.getSsbm());
            xfwz.setSsbmdm(bb.getSsbmdm());
            xfwz.setSsbmid(bb.getSsbmid());
            xfwz.setSsxgaj(bb.getSsxgaj());
            xfwz.setSsxgajid(bb.getSsxgajid());
            xfwz.setSsxgajdm(bb.getSsxgajdm());
            xfwz.setSssgajid(bb.getSssgajid());
            xfwz.setSssgaj(bb.getSssgaj());
            xfwz.setSssgajdm(bb.getSssgajdm());

            this.tbWzXfwzMapper.insert(xfwz);
            xfwz.setZb(null);
            //修改数据 报备数据
            this.tbQwXfbbService.updateXfbbUdp(bb, sbsswz, 0);
            redisService.setCacheObject(redisKey, xfwz);
        }


    }

    @Override
    public TbWzSbsswz saveSbsswz(UdpLzEntity lzEntity) {
        TbJcglSbdwVo sbdwVo = redisService.getCacheObject(Constant.PGIS_SB + lzEntity.getPgisId());
        //udp数量太多 如果设备里面没有数据 udp数据就先不保存
        if (ObjectUtils.isNotEmpty(sbdwVo)) {
            String redisKey = Constant.WZ_SBWZ + lzEntity.getPgisId() + "-" +
                    DateUtils.getQueryDateString(new Date(), "01");
            BigDecimal jd = BigDecimal.valueOf(lzEntity.getLng());
            BigDecimal wd = BigDecimal.valueOf(lzEntity.getLat());
            TbWzSbsswz oldSbsswz = redisService.getCacheObject(redisKey);
            BigDecimal oldJd = null;
            BigDecimal oldWd = null;
            Timestamp oldTime = null;
            if (oldSbsswz != null) {
                oldJd = oldSbsswz.getJd();
                oldWd = oldSbsswz.getWd();
                oldTime = new Timestamp(oldSbsswz.getDwsj().getTime());
            }

            TbWzSbsswz sbsswz = new TbWzSbsswz();
            Integer lc = DistanceUtils.getLc(0, oldJd, oldWd, jd, wd);
            Integer sc = DistanceUtils.getSc(oldTime);
            sbsswz.setSbid(lzEntity.getPgisId());
            sbsswz.setSbmc("LZ-" + lzEntity.getPgisId());
            sbsswz.setSjly("UDP推送位置信息");
            sbsswz.setSjlydm("86");
            sbsswz.setDwrq(lzEntity.getTime());
            sbsswz.setDwsj(lzEntity.getTime());
            sbsswz.setJd(jd);
            sbsswz.setWd(wd);
            sbsswz.setXfsc(sc);
            sbsswz.setXflc(lc);
            sbsswz.setZb(JtsUtils.getPoint(jd, wd));
            sbsswz.setZbhash(GeoHashKit.encode(jd, wd));
            sbsswz.setWztgz("03");
            sbsswz.setXtSjly("1");
            sbsswz.setDwbz("03");
            sbsswz.setSsbm("四川省德阳市公安局");
            sbsswz.setSsbmdm("510600000000");
            sbsswz.setSsbmid("44015");
            sbsswz.setSsxgaj("四川省德阳市公安局");
            sbsswz.setSsxgajid("44015");
            sbsswz.setSsxgajdm("510600000000");
            sbsswz.setSssgajid("44015");
            sbsswz.setSssgaj("四川省德阳市公安局");
            sbsswz.setSssgajdm("510600000000");

            List<TbQwXfbb> bbList = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(sbdwVo)) {
                sbsswz.setSbmc(sbdwVo.getSbmc());
                sbsswz.setSjlydm(sbdwVo.getScode());
                sbsswz.setSsbm(sbdwVo.getSsbm());
                sbsswz.setSsbmdm(sbdwVo.getSsbmdm());
                sbsswz.setSsbmid(sbdwVo.getSsbmid());
                sbsswz.setSsxgaj(sbdwVo.getSsxgaj());
                sbsswz.setSsxgajid(sbdwVo.getSsxgajid());
                sbsswz.setSsxgajdm(sbdwVo.getSsxgajdm());
                sbsswz.setSssgajid(sbdwVo.getSssgajid());
                sbsswz.setSssgaj(sbdwVo.getSssgaj());
                sbsswz.setSssgajdm(sbdwVo.getSssgajdm());
            }
            // 查询是否有该设备的报备信息
            if (StringUtils.isNotEmpty(sbsswz.getSjlydm()) && sbsswz.getSjlydm().equals("05")) {
                bbList = this.tbQwzxAdaptRemoteService.getClBbList(lzEntity.getPgisId());
            } else {
                bbList = this.tbQwzxAdaptRemoteService.getBbList(lzEntity.getPgisId());
            }
            if (!CollectionUtils.isEmpty(bbList)) {
                for (TbQwXfbb bb : bbList) {
//                TbQwXfbb bb = bbList.get(0);
                    sbsswz.setYhId(bb.getFzrId());
                    sbsswz.setYhXm(bb.getFzrXm());
                    sbsswz.setYhSfzh(bb.getFzrSfzh());
                    sbsswz.setJzId(bb.getJzId());
                    sbsswz.setJzMc(bb.getJzMc());
                    sbsswz.setYwid(bb.getId());
                    sbsswz.setSsbm(bb.getSsbm());
                    sbsswz.setSsbmdm(bb.getSsbmdm());
                    sbsswz.setSsbmid(bb.getSsbmid());
                    sbsswz.setSssgaj(bb.getSssgaj());
                    sbsswz.setSssgajdm(bb.getSssgajdm());
                    sbsswz.setSssgajid(bb.getSssgajid());
                    // 查询巡组实时位置,如果5分钟没有GPS信号，则保存设备的位置信息 （直接判断bb是否在线 在线不添加 不在线 写入数据）
                    if (bb.getXfzt().equals("2")) {
                        this.saveXzwz(bb, sbsswz);
                    }
                }
            }
            this.baseMapper.insertEntity(sbsswz);
            sbsswz.setZb(null);
            redisService.setCacheObject(redisKey, sbsswz);
            return sbsswz;
        }
        return null;
    }


    public void delHistory() {
        this.baseMapper.delHistory();
    }



}
