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.websocket.WebSocketObject;
import com.mosty.base.model.dto.wzzx.LocationInfo;
import com.mosty.base.model.dto.wzzx.XfbbUpdateDto;
import com.mosty.base.model.entity.qwzx.TbQwXfbb;
import com.mosty.base.model.entity.wzzx.TbWzLswz;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSbsswz;
import com.mosty.base.model.vo.yjzl.TbFzJlVo;
import com.mosty.base.utils.DistanceUtils;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.wzzx.mapper.TbQwXfbbMapper;
import com.mosty.wzzx.mapper.TbWzLswzMapper;
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 lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbQwXfbbServiceImpl extends ServiceImpl<TbQwXfbbMapper, TbQwXfbb> implements TbQwXfbbService {

    @Resource
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    @Resource
    private TbQwzxAdaptRemoteService tbQwzxAdaptRemoteService;
    @Resource
    private TbWebSocketAdaptRemoteService tbWebSocketAdaptRemoteService;
    @Resource
    private TbYjzlAdaptRemoteService tbYjzlAdaptRemoteService;
    @Resource
    private TbWzLswzMapper tbWzLswzMapper;

    public Integer updateXfbb(TbQwXfbb xfbb, LocationInfo locationInfo, TbWzRywz oldRywz, Integer lc) {
        if (StringUtils.isBlank(locationInfo.getBbid())) {
            return 0;
        }
        XfbbUpdateDto dto = new XfbbUpdateDto();
        BigDecimal jd = BigDecimal.valueOf(locationInfo.getLng());
        BigDecimal wd = BigDecimal.valueOf(locationInfo.getLat());
        dto.setId(locationInfo.getBbid());
        dto.setJd(jd);
        dto.setWd(wd);
        dto.setZb(JtsUtils.getPoint(jd, wd));
        dto.setZbhash(GeoHashKit.encode(jd, wd));
        //计算位置
        dto.setDqwz(locationInfo.getAddress());
        dto.setZbsj(new Date());
        //计算巡防里程
//        Integer lc = DistanceUtils.getLc(xfbb.getXflc(), xfbb.getJd(),  xfbb.getWd(), jd, wd);
        dto.setXflc(lc);
        dto.setXfzt("0");
//        dto.setXfsc(oldRywz.getSc());
        this.baseMapper.updateXfbb(dto);
        // 推送位置信息给前端
//        TbQwXfbb xfbb = this.tbQwzxAdaptRemoteService.getBbxxInfo(locationInfo.getBbid());

        if (xfbb != null) {
            xfbb.setZb(null);
            xfbb.setXfzt("0");
            if (StringUtils.isNotBlank(xfbb.getFzrSfzh())) {
                SysUser user1 = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(xfbb.getFzrSfzh());
                xfbb.setLx(user1.getType());
            }
            WebSocketObject obj = new WebSocketObject("04", xfbb, null);
            this.tbWebSocketAdaptRemoteService.sendBbMessage(obj);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                TbFzJlVo vo = new TbFzJlVo();
                BeanUtils.copyProperties(xfbb, vo);
                String xzmc = xfbb.getJzMc();
                if (StringUtils.isEmpty(xzmc)) {
                    xzmc = xfbb.getFzrXm() + "警组";
                }
                vo.setBbid(xfbb.getId());
                vo.setXzmc(xzmc);
                vo.setJd(jd);
                vo.setWd(wd);
                tbYjzlAdaptRemoteService.addBySbwz(vo);
                System.out.println("犯罪线程结束----->"+(System.currentTimeMillis() - startTime));
            }
        }).start();
        return 1;
    }



    @Override
    public void updateXfbbUdp(TbQwXfbb bb, TbWzSbsswz sbsswz, Integer lc) {
        XfbbUpdateDto dto = new XfbbUpdateDto();
        BigDecimal jd = sbsswz.getJd();
        BigDecimal wd = sbsswz.getWd();
        dto.setId(bb.getId());
        dto.setJd(jd);
        dto.setWd(wd);
        dto.setZb(JtsUtils.getPoint(jd, wd));
        dto.setZbhash(GeoHashKit.encode(jd, wd));
        dto.setZbsj(new Date());
        dto.setXflc(lc);
        dto.setXfzt("0");
        // 保存犯罪数据
        bb.setZb(null);
        TbFzJlVo vo = new TbFzJlVo();
        BeanUtils.copyProperties(vo, bb);
        String xzmc = bb.getJzMc();
        if (StringUtils.isEmpty(xzmc)) {
            xzmc = bb.getFzrXm() + "警组";
        }
        vo.setBbid(bb.getId());
        vo.setXzmc(xzmc);
        vo.setJd(jd);
        vo.setWd(wd);
        this.tbYjzlAdaptRemoteService.addBySbwz(vo);
        this.baseMapper.updateXfbb(dto);
        TbWzLswz lswz = new TbWzLswz();
        lswz.setId(UUIDGenerator.getUUID());
        lswz.setSjly("UDP推送：" + sbsswz.getSbmc());
        lswz.setSjlydm("01");
        lswz.setYwid(bb.getId());
        lswz.setYhId(bb.getFzrId());
        lswz.setYhXm(bb.getFzrXm());
        lswz.setYhSfzh(bb.getFzrSfzh());
        lswz.setJzId(bb.getJzId());
        lswz.setJzMc(bb.getJzMc());
        lswz.setDwrq(new Date());
        lswz.setDwsj(new Date());
        lswz.setJd(jd);
        lswz.setWd(wd);
        lswz.setZb(JtsUtils.getPoint(jd, wd));
        lswz.setZbhash(GeoHashKit.encode(jd, wd));
        lswz.setWztgz("01");
        lswz.setSsbm(bb.getSsbm());
        lswz.setSsbmdm(bb.getSsbmdm());
        lswz.setSsbmid(bb.getSsbmid());
        lswz.setSsxgaj(bb.getSsxgaj());
        lswz.setSsxgajid(bb.getSsxgajid());
        lswz.setSsxgajdm(bb.getSsxgajdm());
        lswz.setSssgajid(bb.getSssgajid());
        lswz.setSssgaj(bb.getSssgaj());
        lswz.setSssgajdm(bb.getSssgajdm());
        lswz.setDwbz("UDP");
        lswz.setDzxz(sbsswz.getDzxz());
        this.tbWzLswzMapper.insertEntity(lswz);
        // 推送位置信息给前端
        TbQwXfbb xfbb = this.tbQwzxAdaptRemoteService.getBbxxInfo(bb.getId());
        if (xfbb != null) {
            xfbb.setZb(null);
            xfbb.setXfzt("0");
            if (StringUtils.isNotBlank(xfbb.getFzrSfzh())) {
                SysUser user1 = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(xfbb.getFzrSfzh());
                xfbb.setLx(user1.getType());
            }
            WebSocketObject obj = new WebSocketObject("04", xfbb, null);
            this.tbWebSocketAdaptRemoteService.sendBbMessage(obj);
        }

    }

    @Override
    public int getsfbb(String id) {
        return this.baseMapper.getsfbb(id);
    }


    public Integer updateById(String id, LocationInfo locationInfo) {
        TbQwXfbb xfbb = this.baseMapper.queryById(id);
        if (xfbb == null) {
            return 0;
        }
        XfbbUpdateDto dto = new XfbbUpdateDto();
        BigDecimal jd = BigDecimal.valueOf(locationInfo.getLng());
        BigDecimal wd = BigDecimal.valueOf(locationInfo.getLat());
        Integer lc = DistanceUtils.getLc(xfbb.getXflc(), xfbb.getJd(), xfbb.getWd(), jd, wd);
        Integer sc = DistanceUtils.getSc(xfbb.getBbkssj());
        dto.setId(xfbb.getId());
        dto.setJd(jd);
        dto.setWd(wd);
        dto.setZb(JtsUtils.getPoint(jd, wd));
        dto.setZbhash(GeoHashKit.encode(jd, wd));
        dto.setZbsj(new Date());
        dto.setXflc(lc);
//        dto.setXfsc(sc);
        return this.baseMapper.updateXfbb(dto);
    }

}
