package com.mosty.yjzl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.yjzl.TbFzycDto;
import com.mosty.base.model.dto.yjzl.TbFzycXljlDto;
import com.mosty.base.model.entity.wzzx.TbWzSbsswz;
import com.mosty.base.model.entity.wzzx.TbWzXfwz;
import com.mosty.base.model.entity.yjzl.TbFzyc;
import com.mosty.base.model.entity.yjzl.TbFzycXljl;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.wzzx.TbWzSblswzVo;
import com.mosty.base.model.vo.yjzl.TbFzJlVo;
import com.mosty.base.model.vo.yjzl.TbFzycXljlVo;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.Kit;
import com.mosty.base.utils.PositionUtil;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.yjzl.mapper.TbFzycXfxzMapper;
import com.mosty.yjzl.mapper.TbFzycXljlMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbFzycService;
import com.mosty.yjzl.service.TbFzycXljlService;
import lombok.AllArgsConstructor;
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 sun.rmi.runtime.Log;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@AllArgsConstructor
public class TbFzycXljlServiceImpl extends ServiceImpl<TbFzycXljlMapper, TbFzycXljl>
        implements TbFzycXljlService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbFzycService tbFzycService;
    private final TbFzycXfxzMapper tbFzycXfxzMapper;

    @Override
    public int insert(TbFzycXljlVo xljlVo) {
        TbFzycXljl tbFzycXljl = new TbFzycXljl();
        BeanUtils.copyProperties(xljlVo, tbFzycXljl);
        tbFzycXljl.setId(UUIDGenerator.getUUID());
        if (StringUtils.isNotEmpty(xljlVo.getSsbmid())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xljlVo.getSsbmdm());
            if (dept != null) {
                tbFzycXljl.setSsbm(dept.getDeptname());
                tbFzycXljl.setSsbmdm(dept.getDeptcode());
                tbFzycXljl.setSsxgaj(dept.getFxjname());
                tbFzycXljl.setSsxgajid(String.valueOf(dept.getFxjid()));
                tbFzycXljl.setSsxgajdm(dept.getFxjcode());
                tbFzycXljl.setSssgaj(dept.getDszname());
                tbFzycXljl.setSssgajid(String.valueOf(dept.getDszid()));
                tbFzycXljl.setSssgajdm(dept.getDszcode());
            }
        }
        return this.baseMapper.insertEntity(tbFzycXljl);
    }

    public int saveFzjl(TbFzycXljlVo xljlVo) {
        //判断该巡组是否巡逻过该犯罪预警预测方格
        TbFzycXljl fzycXlj = this.baseMapper.selectByFzBb(xljlVo);
        if (null == fzycXlj) {//该巡组没有巡逻过该方格，直接保存
            //修改犯罪预测状态 已巡逻
            tbFzycService.updateBySfxl(xljlVo.getFzyjyczlId());
            xljlVo.setXlkssj(new Date());
            List<BigDecimal[]> list = new ArrayList<>();
            BigDecimal[] decimals = new BigDecimal[2];
            decimals[0] = xljlVo.getDqjd();
            decimals[1] = xljlVo.getDqwd();
            list.add(decimals);
            list.add(decimals);
            xljlVo.setZb(JtsUtils.createLine(list));
            xljlVo.setXljssj(new Date());
            this.insert(xljlVo);
        } else {
            List<BigDecimal[]> zbList = JtsUtils.decodeLineString(fzycXlj.getZb());
            fzycXlj.setZbList(zbList);
            fzycXlj.setXzmc(xljlVo.getXzmc());
            //该巡组巡逻过该方格，进入下一个判断
//            if (fzycXlj.getXljssj() == null) {
//
//                //该方格只被该巡组巡逻过一次，直接更新，不用判断 (有问题 第一次点位 然后跑出去了 再回来 巡逻时长要乱)
//                fzycXlj.setZxJl(xljlVo.getZxJl());
//
//                int l =(int) (fzycXlj.getXljssj().getTime() - fzycXlj.getXlkssj().getTime())/1000;
//                fzycXlj.setXfsc(l);
//                int distance =(int) PositionUtil.getDistance(fzycXlj.getDqjd().doubleValue(), fzycXlj.getDqwd().doubleValue(),
//                        xljlVo.getDqjd().doubleValue(), xljlVo.getDqwd().doubleValue());
//                fzycXlj.setXflc(distance + fzycXlj.getXflc());
//
//                fzycXlj.setDqjd(xljlVo.getDqjd());
//                fzycXlj.setDqwd(xljlVo.getDqwd());
//
//                List<BigDecimal[]> list = fzycXlj.getZbList();
//                if (CollectionUtils.isEmpty(list)) {
//                    list = new ArrayList<>();
//                }
//                BigDecimal[] decimals = new BigDecimal[2];
//                decimals[0] = xljlVo.getDqjd();
//                decimals[1] = xljlVo.getDqwd();
//                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);
//                    }
//                }
//                fzycXlj.setZb(JtsUtils.createLine(list));
//
//                this.baseMapper.updateEntity(fzycXlj);
//            } else {
            if (new Date().getTime() - fzycXlj.getXljssj().getTime() > 300000) {
                //该巡组上次进入方格的结束时间距离当前时间已经超过五分钟分钟，所以保存一条新的数据
                xljlVo.setXlkssj(new Date());
                List<BigDecimal[]> list = new ArrayList<>();
                BigDecimal[] decimals = new BigDecimal[2];
                decimals[0] = xljlVo.getDqjd();
                decimals[1] = xljlVo.getDqwd();
                list.add(decimals);
                list.add(decimals);
                xljlVo.setZb(JtsUtils.createLine(list));
                xljlVo.setXljssj(new Date());
                this.insert(xljlVo);
            } else {
                //该巡组上次进入方格的结束时间距离当前时间没有超过五分钟分钟，所以更新本条数据
                fzycXlj.setZxJl(xljlVo.getZxJl());
                fzycXlj.setXljssj(new Date());
                int l = (int) (fzycXlj.getXljssj().getTime() - fzycXlj.getXlkssj().getTime()) / 1000;
                fzycXlj.setXfsc(l);
                int distance = (int) PositionUtil.getDistance(fzycXlj.getDqjd().doubleValue(), fzycXlj.getDqwd().doubleValue(),
                        xljlVo.getDqjd().doubleValue(), xljlVo.getDqwd().doubleValue());
                fzycXlj.setXflc(distance + fzycXlj.getXflc());
                fzycXlj.setDqjd(xljlVo.getDqjd());
                fzycXlj.setDqwd(xljlVo.getDqwd());
                List<BigDecimal[]> list = fzycXlj.getZbList();
                if (CollectionUtils.isEmpty(list)) {
                    list = new ArrayList<>();
                }
                BigDecimal[] decimals = new BigDecimal[2];
                decimals[0] = xljlVo.getDqjd();
                decimals[1] = xljlVo.getDqwd();
                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);
                    }
                }
                fzycXlj.setZb(JtsUtils.createLine(list));
                this.baseMapper.updateEntity(fzycXlj);
//                }
            }
        }

        return 1;
    }

    @Override
    public int updateById(TbFzycXljlVo xljlVo) {
        TbFzycXljl tbFzycXljl = new TbFzycXljl();
        BeanUtils.copyProperties(xljlVo, tbFzycXljl);
        if (StringUtils.isNotEmpty(xljlVo.getSsbmid())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(xljlVo.getSsbmid());
            if (dept != null) {
                tbFzycXljl.setSsbm(dept.getDeptname());
                tbFzycXljl.setSsbmdm(dept.getDeptcode());
                tbFzycXljl.setSsxgaj(dept.getFxjname());
                tbFzycXljl.setSsxgajid(String.valueOf(dept.getFxjid()));
                tbFzycXljl.setSsxgajdm(dept.getFxjcode());
                tbFzycXljl.setSssgaj(dept.getDszname());
                tbFzycXljl.setSssgajid(String.valueOf(dept.getDszid()));
                tbFzycXljl.setSssgajdm(dept.getDszcode());
            }
        }
        return this.baseMapper.updateEntity(tbFzycXljl);
    }

    @Override
    public TbFzycXljl queryById(String id) {
        return this.baseMapper.selectById(id);
    }

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

    @Override
    public List<TbFzycXljlDto> qfzxl(String fzyjyczlId) {
        List<TbFzycXljl> qfzxl = this.baseMapper.qfzxl(fzyjyczlId);
        if (!CollectionUtils.isEmpty(qfzxl)) {
            List<TbFzycXljlDto> list = new ArrayList<>();
            for (TbFzycXljl xljl : qfzxl) {
                TbFzycXljlDto dto = new TbFzycXljlDto();
                BeanUtils.copyProperties(xljl, dto);
                List<BigDecimal[]> zbList = JtsUtils.decodeLineString(xljl.getZb());
                dto.setZbList(zbList);
                //查询有没指令
                String qs = tbFzycXfxzMapper.selectQs(xljl.getXlbbId(), xljl.getFzyjyczlId());
                if (StringUtils.isEmpty(qs)) {
                    dto.setSfqsmc("无");
                } else {
                    if (qs.equals("1")) {
                        dto.setSfqsmc("签收");
                    } else {
                        dto.setSfqsmc("未签收");
                    }
                }
                dto.setSfqs(qs);
                //查询核查人员数
                dto.setPcry(this.baseMapper.pcrys(xljl.getXlbbId()));
                //查询核查车数
                dto.setPccl(this.baseMapper.pccls(xljl.getXlbbId()));
                list.add(dto);
            }
            return list;
        }
        return null;
    }

    @Override
    public int addBySbwz(TbFzJlVo fzJlVo) {
        if (fzJlVo != null && StringUtils.isNotBlank(fzJlVo.getSsbmdm())) {
            TbFzycDto tbFzycDto = new TbFzycDto();
            tbFzycDto.setSsbmdm(fzJlVo.getSsbmdm());
            List<TbFzyc> tbFzycs = tbFzycService.queryList(tbFzycDto);
            if (!CollectionUtils.isEmpty(tbFzycs)) {
                for (TbFzyc fzyc : tbFzycs) {
                    //计算中心点位
                    BigDecimal X = fzyc.getZxX();
                    BigDecimal Y = fzyc.getZxY();
                    //计算两个位置距离
                    double distance = Kit.getDistance(X.doubleValue(), Y.doubleValue(),
                            fzJlVo.getJd().doubleValue(), fzJlVo.getWd().doubleValue());
                    //巡逻到犯罪预测区域，提示已巡逻 200米
                    String gridType = fzyc.getGridType();
                    Long aLong = ((Long.valueOf(gridType)) / 2) + 100;
                    if (distance <= aLong) {
                        //保存数据
                        TbFzycXljlVo vo = new TbFzycXljlVo();
                        vo.setXlbbId(fzJlVo.getBbid());
                        vo.setFzyjyczlId(fzyc.getId());
                        vo.setXlkssj(new Date());
                        vo.setZxJl(new BigDecimal(distance));
                        vo.setSsbm(fzJlVo.getSsbm());
                        vo.setSsbmdm(fzJlVo.getSsbmdm());
                        vo.setSsbmid(fzJlVo.getSsbmid());
                        vo.setSsxgaj(fzJlVo.getSsxgaj());
                        vo.setSsxgajdm(fzJlVo.getSsxgajdm());
                        vo.setSsxgajid(fzJlVo.getSsxgajid());
                        vo.setSssgaj(fzJlVo.getSssgaj());
                        vo.setSssgajdm(fzJlVo.getSssgajdm());
                        vo.setSssgajid(fzJlVo.getSssgaj());
                        vo.setDqjd(fzJlVo.getJd());
                        vo.setDqwd(fzJlVo.getWd());
                        vo.setXzmc(fzJlVo.getXzmc());
                        this.saveFzjl(vo);
                    }

                }
            }

        }
        return 0;
    }
}
