package com.mosty.wzzx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.wzzx.AppLocationReceiveDto;
import com.mosty.base.model.entity.qwzx.TbQwXfbb;
import com.mosty.base.model.entity.wzzx.*;
import com.mosty.base.model.query.wzzx.TbWzLswzSearchDto;
import com.mosty.base.model.query.wzzx.TbWzSbLswzSearchDto;
import com.mosty.base.utils.*;
import com.mosty.base.utils.mongo.core.wrapper.Wrappers;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.wzzx.mapper.*;
import com.mosty.wzzx.mongodb.TbWzSblswzMongoService;
import com.mosty.wzzx.mongodb.entity.TbWzSblswzMo;
import com.mosty.wzzx.service.TbWzXfwzService;
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 TbWzXfwzServiceImpl extends ServiceImpl<TbWzXfwzMapper, TbWzXfwz> implements TbWzXfwzService {

    private final TbWzRywzMapper tbWzRywzMapper;
    private final TbWzSswzMapper tbWzSswzMapper;
    private final TbWzLswzMapper tbWzLswzMapper;
    private final TbQwXfbbMapper tbQwXfbbMapper;
    private final TbWzSblswzMapper tbWzSblswzMapper;
    private final TbWzSblswzMongoService tbWzSblswzMongoService;


    public List<TbWzXfwz> queryAll() {
        return this.baseMapper.queryAll();
    }

    public TbWzXfwz queryById(String id) {
        return this.baseMapper.queryById(id);
    }

    public TbWzXfwz insert(TbWzXfwz tbWzXfwz) {
        this.baseMapper.insert(tbWzXfwz);
        return tbWzXfwz;
    }

    public TbWzXfwz update(TbWzXfwz tbWzXfwz) {
        this.baseMapper.update(tbWzXfwz);
        return this.queryById(tbWzXfwz.getId());
    }

    public boolean deleteById(String id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    public Object selectTrack(TbWzXfwz tbWzXfwz) {
        TbWzXfwz xfwz = this.baseMapper.queryByBbid(tbWzXfwz.getBbId());
        if (ObjectUtils.isNotEmpty(xfwz)) {
            List<BigDecimal[]> list = JtsUtils.decodeLineString(xfwz.getZb());
          /*  if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            if (list.size() >= 3) {
                BigDecimal[] decimal0 = list.get(0);
                BigDecimal[] decimal1 = list.get(1);
                if (decimal0 == decimal1) {
                    list.remove(0);
                }
            }*/
            xfwz.setZb(null);
            xfwz.setZbList(list);
        }
        return xfwz;
    }

    @Override
    public Object selectLswz(TbWzLswzSearchDto dto) {
        if (StringUtils.isEmpty(dto.getBbId())) throw new BusinessException("报备业务ID为空！");
        if (StringUtils.isEmpty(dto.getKssj())) {
            dto.setKssj(DateUtils.getSystemDateString());
        }
        List<TbWzLswz> tbWzLswzs = this.tbWzLswzMapper.selectList(new LambdaQueryWrapper<TbWzLswz>()
                .eq(StringUtils.isNotBlank(dto.getBbId()), TbWzLswz::getYwid, dto.getBbId())
                .ge(StringUtils.isNotBlank(dto.getKssj()), TbWzLswz::getDwsj, dto.getKssj())
                .le(StringUtils.isNotBlank(dto.getJssj()), TbWzLswz::getDwsj, dto.getJssj())
                .orderByAsc(TbWzLswz::getDwsj)
        );
        return tbWzLswzs;
    }

    @Override
    public Object selectSbLswz(TbWzSbLswzSearchDto dto) {
        if (StringUtils.isEmpty(dto.getGpsId())) throw new BusinessException("设备id为空！");
        if (StringUtils.isEmpty(dto.getKssj())) {
            dto.setKssj(DateUtils.getSystemDateString());
        }
        List<TbWzSblswz> tbWzLswzs = this.tbWzSblswzMapper.selectList(new LambdaQueryWrapper<TbWzSblswz>()
                .eq(StringUtils.isNotBlank(dto.getGpsId()), TbWzSblswz::getSbid, dto.getGpsId())
                .ge(StringUtils.isNotBlank(dto.getKssj()), TbWzSblswz::getXtCjsj, dto.getKssj())
                .le(StringUtils.isNotBlank(dto.getJssj()), TbWzSblswz::getXtCjsj, dto.getJssj())
                .orderByAsc(TbWzSblswz::getXtCjsj)
        );
        return tbWzLswzs;
    }

    @Override
    public Object selectSbLswzMo(TbWzSbLswzSearchDto dto) {
        if (StringUtils.isEmpty(dto.getGpsId())) throw new BusinessException("设备id为空！");
        if (StringUtils.isEmpty(dto.getKssj())) {
            dto.setKssj(DateUtils.getSystemDateString() + " 00:00:00");
        }
        com.mosty.base.utils.mongo.core.wrapper.LambdaQueryWrapper<TbWzSblswzMo> query = Wrappers.<TbWzSblswzMo>lambdaQuery()
                .eq(StringUtils.isNotBlank(dto.getGpsId()), TbWzSblswzMo::getSbid, dto.getGpsId())
                .ge(StringUtils.isNotBlank(dto.getKssj()), TbWzSblswzMo::getXtCjsj, DateUtils.dateToISODate(dto.getKssj()));
        if (StringUtils.isNotBlank(dto.getJssj())) {
            query.le(TbWzSblswzMo::getXtCjsj, DateUtils.dateToISODate(dto.getJssj()));
        }
        query.orderByAsc(TbWzSblswzMo::getXtCjsj);
        List<TbWzSblswzMo> list = tbWzSblswzMongoService.list(query);
        return list;
    }

    public Boolean locationReceive(AppLocationReceiveDto receiveDto) {
        //经度、纬度为空不接收数据
        if (receiveDto.getJd() == null && receiveDto.getWd() == null) {
            return false;
        }
        //保存人员预警
        TbWzRywz rywz = saveRywz(receiveDto);
        TbQwXfbb bbxx = null;
        if (StringUtils.isNotBlank(receiveDto.getBbid())) {
            bbxx = tbQwXfbbMapper.queryById(receiveDto.getBbid());
        }
        saveSsjl(receiveDto, bbxx, rywz);
        saveLsjl(receiveDto, bbxx, rywz);
        if (bbxx != null) {
            saveXfwz(receiveDto, bbxx, rywz);
        }
        return true;
    }

    public TbWzRywz saveRywz(AppLocationReceiveDto receiveDto) {
        TbWzRywz rywz = tbWzRywzMapper.queryBySfzh(receiveDto.getYhSfzh());
        if (rywz == null) {
            rywz = new TbWzRywz();
        }
        rywz.setYhId(receiveDto.getYhid());
        rywz.setYhXm(receiveDto.getYhxm());
        rywz.setYhSfzh(receiveDto.getYhSfzh());
        rywz.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        List<BigDecimal[]> list = JtsUtils.decodeLineString(rywz.getZb());
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        Integer lc = DistanceUtils.getLc(rywz.getLc(), rywz.getJd(), rywz.getWd(), receiveDto.getJd(),
                receiveDto.getWd());
        Integer sc = DistanceUtils.getSc(rywz.getZhgxsj());
        BigDecimal[] decimals = new BigDecimal[2];
        decimals[0] = receiveDto.getJd();
        decimals[1] = receiveDto.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);
            }
        }
        rywz.setLc(lc);
        rywz.setSc(sc);
        rywz.setZb(JtsUtils.createLine(list));
        rywz.setZbsl(list.size());
        rywz.setJd(receiveDto.getJd());
        rywz.setWd(receiveDto.getWd());
        rywz.setZhgxsj(new Timestamp(System.currentTimeMillis()));
        rywz.setXtSjly("1");
        rywz.setJd(receiveDto.getJd());
        rywz.setWd(receiveDto.getWd());
        rywz.setSsxgajdm("510109000000");
        rywz.setSsxgajid("1");
        rywz.setSsxgaj("高新区分局");
        rywz.setSssgaj("成都市公安局");
        rywz.setSssgajdm("510100000000");
        rywz.setSssgajid("10001");
        if (StringUtils.isBlank(rywz.getId())) {
            rywz.setId(UUIDGenerator.getUUID());
            rywz.setKssj(new Date());
            rywz.setDwrq(new Date());
            rywz.setXtCjsj(new Timestamp(System.currentTimeMillis()));
            tbWzRywzMapper.insertEntity(rywz);
        } else {
            tbWzRywzMapper.updateEntity(rywz);
        }
        return rywz;
    }

    public void saveXfwz(AppLocationReceiveDto receiveDto, TbQwXfbb bbxx, TbWzRywz rywz) {
        TbWzXfwz xfwz = this.baseMapper.queryByBbid(receiveDto.getBbid());
        if (xfwz == null) {
            xfwz = new TbWzXfwz();
        }
        Integer lc = DistanceUtils.getLc(rywz.getLc(), rywz.getJd(), rywz.getWd(), receiveDto.getJd(),
                receiveDto.getWd());
        Integer sc = DistanceUtils.getSc(rywz.getZhgxsj());
        xfwz.setBbId(receiveDto.getBbid());
        xfwz.setYhId(receiveDto.getYhid());
        xfwz.setYhSfzh(receiveDto.getYhSfzh());
        xfwz.setYhXm(receiveDto.getYhxm());
        xfwz.setJzId(bbxx.getJzId());
        xfwz.setJzMc(bbxx.getJzMc());
        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] = receiveDto.getJd();
        decimals[1] = receiveDto.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.setDwrq(new Date());
        xfwz.setKssj(new Date());
        xfwz.setJd(receiveDto.getJd());
        xfwz.setWd(receiveDto.getWd());
        xfwz.setZhgxsj(new Date());
        xfwz.setXtSjly("1");
        xfwz.setSsxgajdm("510109000000");
        xfwz.setSsxgajid("1");
        xfwz.setSsxgaj("高新区分局");
        xfwz.setSssgaj("成都市公安局");
        xfwz.setSssgajdm("510100000000");
        xfwz.setSssgajid("10001");
        if (StringUtils.isBlank(xfwz.getId())) {
            xfwz.setId(UUIDGenerator.getUUID());
            xfwz.setXtCjsj(new Timestamp(System.currentTimeMillis()));
            this.baseMapper.insert(xfwz);
        } else {
            xfwz.setJssj(new Date());
            this.baseMapper.update(xfwz);
        }
    }

    public void saveSsjl(AppLocationReceiveDto receiveDto, TbQwXfbb bbxx, TbWzRywz rywz) {
        TbWzSswz sswz = new TbWzSswz();
        Integer lc = DistanceUtils.getLc(0, rywz.getJd(), rywz.getWd(), receiveDto.getJd(), receiveDto.getWd());
        Integer sc = DistanceUtils.getSc(rywz.getZhgxsj());
        BeanUtils.copyProperties(receiveDto, sswz);
        sswz.setSjly("移动设备-" + receiveDto.getYhxm() + "-" + receiveDto.getYhSfzh());
        sswz.setSjlydm("02");
        sswz.setYwid(receiveDto.getBbid());
        if (bbxx != null) {
            sswz.setJzId(bbxx.getJzId());
            sswz.setJzMc(bbxx.getJzMc());
        }
        sswz.setDwrq(new Date());
        sswz.setDwsj(new Date());
        sswz.setJd(receiveDto.getJd());
        sswz.setWd(receiveDto.getWd());
        sswz.setXfsc(sc);
        sswz.setXflc(lc);
        if (receiveDto.getJd() != null && receiveDto.getWd() != null) {
            sswz.setZb(JtsUtils.getPoint(receiveDto.getJd(), receiveDto.getWd()));
            sswz.setZbhash(GeoHashKit.encode(receiveDto.getJd(), receiveDto.getWd()));
        }
        sswz.setYhId(receiveDto.getYhid());
        sswz.setYhXm(receiveDto.getYhxm());
        sswz.setJrbh(receiveDto.getJrbh());
        sswz.setWztgz("02");//综合定位
        sswz.setXtSjly("1");
        sswz.setSsxgajdm("510109000000");
        sswz.setSsxgajid("1");
        sswz.setSsxgaj("高新区分局");
        sswz.setSssgaj("成都市公安局");
        sswz.setSssgajdm("510100000000");
        sswz.setSssgajid("10001");
        tbWzSswzMapper.insertEntity(sswz);
    }

    public void saveLsjl(AppLocationReceiveDto receiveDto, TbQwXfbb bbxx, TbWzRywz rywz) {
        TbWzLswz lswz = new TbWzLswz();
        BeanUtils.copyProperties(receiveDto, lswz);
        Integer lc = DistanceUtils.getLc(0, rywz.getJd(), rywz.getWd(), receiveDto.getJd(), receiveDto.getWd());
        Integer sc = DistanceUtils.getSc(rywz.getZhgxsj());
        lswz.setSjly("移动设备" + receiveDto.getYhxm() + "-" + receiveDto.getYhSfzh());
        lswz.setSjlydm("02");
        lswz.setYwid(receiveDto.getBbid());
        if (bbxx != null) {
            lswz.setJzId(bbxx.getJzId());
            lswz.setJzMc(bbxx.getJzMc());
        }
        lswz.setDwrq(new Date());
        lswz.setDwsj(new Date());
        lswz.setJd(receiveDto.getJd());
        lswz.setWd(receiveDto.getWd());
        lswz.setXfsc(sc);
        lswz.setXflc(lc);
        if (receiveDto.getJd() != null && receiveDto.getWd() != null) {
            lswz.setZb(JtsUtils.getPoint(receiveDto.getJd(), receiveDto.getWd()));
            lswz.setZbhash(GeoHashKit.encode(receiveDto.getJd(), receiveDto.getWd()));
        }
        lswz.setYhId(receiveDto.getYhid());
        lswz.setYhXm(receiveDto.getYhxm());
        lswz.setWztgz("02");//综合定位
        lswz.setXtSjly("1");
        lswz.setSsxgajdm("510109000000");
        lswz.setSsxgajid("1");
        lswz.setSsxgaj("高新区分局");
        lswz.setSssgaj("成都市公安局");
        lswz.setSssgajdm("510100000000");
        lswz.setSssgajid("10001");
        tbWzLswzMapper.insertEntity(lswz);
    }
}

