package com.mosty.hczx.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.hczx.BpcclSaveDto;
import com.mosty.base.model.dto.hczx.TbHcBpcclInsertDto;
import com.mosty.base.model.entity.hczx.*;
import com.mosty.base.model.query.hczx.HcTjQuery;
import com.mosty.base.model.query.hczx.TbHcBpcclQuery;
import com.mosty.base.model.vo.hczx.*;
import com.mosty.base.utils.*;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.base.model.query.hczx.TbHcTodayBpcclQuery;
import com.mosty.hczx.mapper.*;
import com.mosty.hczx.remote.TbBaseAdaptRemoteService;
import com.mosty.hczx.service.TbHcBpcclService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class TbHcBpcclServiceImpl extends ServiceImpl<TbHcBpcclMapper, TbHcBpccl>
        implements TbHcBpcclService {

    private final TbHcTpMapper tbHcTpMapper;
    private final TbHcBpcclWpMapper tbHcBpcclWpMapper;
    private final TbHcBpcclBqMapper tbHcBpcclBqMapper;
    private final TbHcBpcryMapper tbHcBpcryMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;


    @Override
    public Integer selectCarCount(String ssbmdm, String isChild) {
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(ssbmdm, isChild);
        return this.baseMapper.selectCarCount(ssbm);
    }

    @Override
    public IPage<TbHcBpcclVo> selectCarList(TbHcTodayBpcclQuery dto) {
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));

        IPage<TbHcBpcclVo> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
//        UserInfo userInfo = UserInfoManager.get();
//        map.put("useSql", PermissionsUtil.createSql("a", userInfo));
        int count = this.baseMapper.getCount(map);
        List<TbHcBpccl> list = this.baseMapper.getList(map);
        page.setTotal(count);
        List<TbHcBpcclVo> voList = new ArrayList<>();
        list.forEach(item -> {
            // 查询车辆图片
            TbHcBpcclVo vo = new TbHcBpcclVo();
            BeanUtils.copyProperties(item, vo);
            List<TbHcTp> tpList = this.tbHcTpMapper.selectList(
                    new LambdaQueryWrapper<TbHcTp>()
                            .eq(TbHcTp::getLyid, item.getId())
                            .eq(TbHcTp::getLybm, "tb_hc_bpccl")
            );

            if (!CollectionUtils.isEmpty(tpList)) {
                List<TbHcTpVo> collect = tpList.stream().map(v -> {
                    TbHcTpVo tbHcTpVo = new TbHcTpVo();
                    BeanUtils.copyProperties(v, tbHcTpVo);
                    return tbHcTpVo;
                }).collect(Collectors.toList());
                vo.setTpList(collect);
            }


            // 查询车辆物品
            List<TbHcBpcclWp> wpList = this.tbHcBpcclWpMapper.selectList(
                    new LambdaQueryWrapper<TbHcBpcclWp>()
                            .eq(TbHcBpcclWp::getPcywid, item.getId())
            );
            List<TbHcBpcclWpVo> wpVoList = new ArrayList<>();
            wpList.forEach(wp -> {
                TbHcBpcclWpVo wpVo = new TbHcBpcclWpVo();
                BeanUtils.copyProperties(wp, wpVo);
                // 查询物品图片
                List<TbHcTp> wpTpList = this.tbHcTpMapper.selectList(
                        new LambdaQueryWrapper<TbHcTp>()
                                .eq(TbHcTp::getLyid, wp.getId())
                                .eq(TbHcTp::getLybm, "tb_hc_bpccl_wp")
                );
                wpVo.setWpTpIdList(wpTpList.stream().map(TbHcTp::getFjid).collect(Collectors.toList()));
                wpVoList.add(wpVo);
            });
            vo.setWpVoList(wpVoList);
            voList.add(vo);
        });
        page.setRecords(voList);
        return page;
    }

    @Override
    public TbHcBpcclVo selectCl(TbHcBpcclQuery dto) {
        UserInfo user = UserInfoManager.get();

        TbHcBpccl item = new TbHcBpccl();
        BeanUtils.copyProperties(dto, item);
        if (StringUtils.isNotBlank(dto.getHpzl()) && StringUtils.isNotBlank(dto.getHphm())) {
            if (dto.getJd() == null || dto.getWd() == null) {
                // 默认为县公安局经纬度
                dto.setJd(Constant.JD).setWd(Constant.WD);
                item.setJd(Constant.JD).setWd(Constant.WD);
            }
            ClpcVo vo = this.getClByHp(dto.getHpzl(), dto.getHphm(), user, item);
            if (vo != null && StringUtils.isNotBlank(vo.getHphm())) {
                item.setId(UUIDGenerator.getUUID()).setClsbdh(vo.getClsbdh())
                        .setJdcsyr(vo.getJdcsyr()).setJdcsyrsfzh(vo.getSfzh())
                        .setPcrq(new Date()).setPcsj(new Date())
                        .setPcmjId(String.valueOf(user.getUserId())).setPcmjJh(user.getInDustRialId())
                        .setPcmjSfzh(user.getIdEntityCard()).setPcmjXm(user.getUserName())
                        .setSsbmdm(user.getDeptCode())
                        .setSsbm(user.getDeptName())
                        .setSsbmid(String.valueOf(user.getDeptId()))
                        .setSsxgaj(user.getFxjDeptName())
                        .setSsxgajid(String.valueOf(user.getFxjDeptId()))
                        .setSsxgajdm(user.getFxjDeptCode())
                        .setSssgaj(user.getDszDeptName())
                        .setSssgajid(String.valueOf(user.getDszDeptId()))
                        .setSssgajdm(user.getDszDeptCode()).setXtSjly("2");
                item.setZdch(dto.getZdch());
                item.setZdxh(dto.getZdxh());
                item.setZdsim(dto.getZdsim());
                item.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()))
                        .setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
                StringBuilder rybqdm = new StringBuilder();
                StringBuilder rybq = new StringBuilder();
                // 查询是否盗抢车辆
                DqclVo dqclVo = this.getDqcl(dto.getHpzl(), dto.getHphm(), user);
                if (dqclVo != null) {
                    rybqdm.append("盗抢车辆").append(",");
                    rybq.append("DQCL").append(",");
                    // 保存车辆标签信息
                    TbHcBpcclBq bq = new TbHcBpcclBq();
                    bq.setPcywid(item.getId());
                    bq.setBqmc("盗抢车辆");
                    bq.setBqbh("DQCL");
                    bq.setXtSjly("1");
                    bq.setSsbmdm(String.valueOf(user.getDeptId()));
                    bq.setSsbm(user.getDeptName());
                    bq.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    bq.setSsxgaj(user.getFxjDeptName());
                    bq.setSssgajdm(String.valueOf(user.getDszDeptId()));
                    bq.setSssgaj(user.getDszDeptName());
                    this.tbHcBpcclBqMapper.insert(bq);
                }
                // 查询是否临控车辆
                BkclVo bkclVo = this.getBkcl(dto.getHpzl(), dto.getHphm(), user, item);
                if (bkclVo != null) {
                    rybqdm.append("临控车辆").append(",");
                    rybq.append("LKCL").append(",");
                    // 保存车辆标签信息
                    TbHcBpcclBq bq = new TbHcBpcclBq();
                    bq.setPcywid(item.getId());
                    bq.setBqmc("临控车辆");
                    bq.setBqbh("LKCL");
                    bq.setXtSjly("1");
                    bq.setSsbmdm(String.valueOf(user.getDeptId()));
                    bq.setSsbm(user.getDeptName());
                    bq.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    bq.setSsxgaj(user.getFxjDeptName());
                    bq.setSssgajdm(String.valueOf(user.getDszDeptId()));
                    bq.setSssgaj(user.getDszDeptName());
                    this.tbHcBpcclBqMapper.insert(bq);
                }
                item.setBqmc(rybq.toString().length() > 0 ? rybq.toString().substring(0, rybq.toString().length() - 1) : "");
                item.setBqxxsj(rybqdm.toString().length() > 0 ? rybqdm.toString().substring(0, rybqdm.toString().length() - 1) : "");
                item.setPcclJg("9");
                item.setPcclJgmc("盘查");
                this.baseMapper.insertEntity(item);
                // 保存信息到巡逻盘查库中
//                this.saveClToXlpc(item, user);

                TbHcBpcclVo clvo = new TbHcBpcclVo();
                BeanUtils.copyProperties(item, clvo);
                return clvo;
            }
        }
        return null;
    }

    @Override
    public TbHcBpcclVo selectNwCl(TbHcBpcclQuery dto) {
        UserInfo user = UserInfoManager.get();

        TbHcBpccl item = new TbHcBpccl();
        BeanUtils.copyProperties(dto, item);
        if (StringUtils.isNotBlank(dto.getHpzl()) && StringUtils.isNotBlank(dto.getHphm())) {
            if (dto.getJd() == null || dto.getWd() == null) {
                // 默认为县公安局经纬度
                dto.setJd(Constant.JD).setWd(Constant.WD);
                item.setJd(Constant.JD).setWd(Constant.WD);
            }

            //获取车辆重点标签状态（从内网中查询）
            Integer zdbqNum = this.getClZdbqZtFromNw(dto, user);
            if(zdbqNum != null && zdbqNum != 0){
                item.setId(UUIDGenerator.getUUID())
//                        .setClsbdh(vo.getClsbdh())
//                        .setJdcsyr(vo.getJdcsyr()).setJdcsyrsfzh(vo.getSfzh())
                        .setPcrq(new Date()).setPcsj(new Date())
                        .setPcmjId(String.valueOf(user.getUserId())).setPcmjJh(user.getInDustRialId())
                        .setPcmjSfzh(user.getIdEntityCard()).setPcmjXm(user.getUserName())
                        .setSsbmdm(user.getDeptCode())
                        .setSsbm(user.getDeptName())
                        .setSsbmid(String.valueOf(user.getDeptId()))
                        .setSsxgaj(user.getFxjDeptName())
                        .setSsxgajid(String.valueOf(user.getFxjDeptId()))
                        .setSsxgajdm(user.getFxjDeptCode())
                        .setSssgaj(user.getDszDeptName())
                        .setSssgajid(String.valueOf(user.getDszDeptId()))
                        .setSssgajdm(user.getDszDeptCode()).setXtSjly("2");
                item.setZdch(dto.getZdch());
                item.setZdxh(dto.getZdxh());
                item.setZdsim(dto.getZdsim());
                item.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()))
                        .setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
                StringBuilder rybqdm = new StringBuilder();
                StringBuilder rybq = new StringBuilder();
                TbHcBpcclVo clvo = new TbHcBpcclVo();

                if (zdbqNum == 2) {
                    //判断是否是被盗抢车辆
                    rybqdm.append("盗抢车辆").append(",");
                    rybq.append("DQCL").append(",");
                    // 保存车辆标签信息
                    TbHcBpcclBq bq = new TbHcBpcclBq();
                    bq.setPcywid(item.getId());
                    bq.setBqmc("盗抢车辆");
                    bq.setBqbh("DQCL");
                    bq.setXtSjly("1");
                    bq.setSsbmdm(String.valueOf(user.getDeptId()));
                    bq.setSsbm(user.getDeptName());
                    bq.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    bq.setSsxgaj(user.getFxjDeptName());
                    bq.setSssgajdm(String.valueOf(user.getDszDeptId()));
                    bq.setSssgaj(user.getDszDeptName());
                    this.tbHcBpcclBqMapper.insert(bq);
                    clvo.setPcbqsm("该车辆为被盗抢车辆，请使用移动警务终端查看详情");
                }else if (zdbqNum == 3) {
                    //判断是否临控车辆
                    rybqdm.append("临控车辆").append(",");
                    rybq.append("LKCL").append(",");
                    // 保存车辆标签信息
                    TbHcBpcclBq bq = new TbHcBpcclBq();
                    bq.setPcywid(item.getId());
                    bq.setBqmc("临控车辆");
                    bq.setBqbh("LKCL");
                    bq.setXtSjly("1");
                    bq.setSsbmdm(String.valueOf(user.getDeptId()));
                    bq.setSsbm(user.getDeptName());
                    bq.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    bq.setSsxgaj(user.getFxjDeptName());
                    bq.setSssgajdm(String.valueOf(user.getDszDeptId()));
                    bq.setSssgaj(user.getDszDeptName());
                    this.tbHcBpcclBqMapper.insert(bq);
                    clvo.setPcbqsm("该车辆为临控车辆，请使用移动警务终端查看详情");
                }else{
                    clvo.setPcbqsm("正常车辆");
                }

                item.setBqmc(rybq.toString().length() > 0 ? rybq.toString().substring(0, rybq.toString().length() - 1) : "");
                item.setBqxxsj(rybqdm.toString().length() > 0 ? rybqdm.toString().substring(0, rybqdm.toString().length() - 1) : "");
                item.setPcclJg("9");
                item.setPcclJgmc("盘查");
                this.baseMapper.insertEntity(item);

                BeanUtils.copyProperties(item, clvo);
                return clvo;
            }
        }
        return null;
    }

    @Override
    public void saveBpccl(TbHcBpcclInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        if (user == null) {
            throw new BusinessException("请重新登录");
        }
        if (StringUtils.isBlank(dto.getId())) {
            throw new BusinessException("盘查Id不存在");
        }
        TbHcBpccl cl = this.baseMapper.selectById(dto.getId());
        if (cl == null) {
            throw new BusinessException("盘查对象不存在");
        }
        cl.setPcclJg(dto.getPcclJg());
        cl.setPcclJgmc(dto.getPcclJgmc());
        cl.setPcclYjdwdm(dto.getPcclYjdwdm());
        cl.setPcclYjdw(dto.getPcclYjdw());
        cl.setPcclYjyy(dto.getPcclYjyy());
        // 查询当月数量
        cl.setSsdwdycs(0);
        this.baseMapper.updateEntity(cl);
        // 保存图片信息
        List<String> tpIdList = dto.getTpIdList();
        this.saveTp(tpIdList, "tb_hc_bpccl", cl.getId());
        // 保存物品信息
        List<TbHcBpcclWpVo> clWpList = dto.getClWpList();
        this.saveClTp(clWpList, cl);
        // 保存同乘人员信息
        List<String> rypcidList = dto.getRypcidList();
        this.saveTcry(rypcidList, cl);
        // 保存信息到巡逻盘查库中
//        this.saveClToXlpc(cl, user);
    }

    // 保存同乘人员信息
    private void saveTcry(List<String> rypcidList, TbHcBpccl cl) {
        if (!CollectionUtils.isEmpty(rypcidList)) {
            rypcidList.forEach(item -> {
                TbHcBpcry ry = this.tbHcBpcryMapper.selectById(item);
                if (ry != null) {
                    ry.setPcclId(cl.getId());
                    this.tbHcBpcryMapper.updateById(ry);
                }
            });
        }
    }

    @Override
    public List<TbHcBpcclVo> getClOf2h() {
        UserInfo user = UserInfoManager.get();
        if (user != null) {
            String sfzh = user.getIdEntityCard();
            // 两小时之前
            String time = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "H", -1), "02");
            List<TbHcBpccl> list = this.baseMapper.getClOf2h(sfzh, time);
            List<TbHcBpcclVo> voList = new ArrayList<>();
            list.forEach(item -> {
                TbHcBpcclVo vo = new TbHcBpcclVo();
                BeanUtils.copyProperties(item, vo);
                voList.add(vo);
            });
            return voList;
        }
        return new ArrayList<>();
    }


    @Override
    public Map<String, Integer> getClpcCount(TbHcBpcclQuery dto) {
        if (StringUtils.isBlank(dto.getJssj()) && StringUtils.isBlank(dto.getKssj())) {
            String systemDateString = DateUtils.getSystemDateString();
            dto.setKssj(systemDateString);
            dto.setJssj(systemDateString);
        }
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        Map<String, Object> map = new HashMap<>();
        Map<String, Integer> clMap = new HashMap<>();
        dto.setPcclJg("");
        map.put("dto", dto);
        int All = this.baseMapper.getCountCl(map);
        clMap.put("All", All);
        dto.setPcclJg("1");
        map.put("dto", dto);
        int fx = this.baseMapper.getCountCl(map);
        clMap.put("fx", fx);
        dto.setPcclJg("2");
        map.put("dto", dto);
        int yj = this.baseMapper.getCountCl(map);
        clMap.put("yj", yj);
        dto.setPcclJg("9");
        map.put("dto", dto);
        int pc = this.baseMapper.getCountCl(map);
        clMap.put("pc", pc);
        return clMap;
    }


    @Override
    public IPage<TbHcBpcclVo> getClpcList(TbHcBpcclQuery dto) {
        if (StringUtils.isBlank(dto.getJssj()) && StringUtils.isBlank(dto.getKssj())) {
            String systemDateString = DateUtils.getSystemDateString();
            dto.setKssj(systemDateString);
            dto.setJssj(systemDateString);
        }
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        IPage<TbHcBpcclVo> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
//        UserInfo userInfo = UserInfoManager.get();
//        map.put("useSql", PermissionsUtil.createSql("a", userInfo));
        int count = this.baseMapper.getCountCl(map);
        List<TbHcBpccl> list = this.baseMapper.getListCl(map);
        List<TbHcBpcclVo> rList = new ArrayList<>();
        list.forEach(item -> {
            // 查询盘查车辆图片
            TbHcBpcclVo vo = new TbHcBpcclVo();
            BeanUtils.copyProperties(item, vo);
            // 查询人员照片信息
            List<TbHcTp> tpList = this.tbHcTpMapper.selectList(
                    new LambdaQueryWrapper<TbHcTp>()
                            .eq(TbHcTp::getLybm, "tb_hc_bpccl")
                            .eq(TbHcTp::getLyid, item.getId())
            );

            if (!CollectionUtils.isEmpty(tpList)) {
                List<TbHcTpVo> collect = tpList.stream().map(v -> {
                    TbHcTpVo tbHcTpVo = new TbHcTpVo();
                    BeanUtils.copyProperties(v, tbHcTpVo);
                    return tbHcTpVo;
                }).collect(Collectors.toList());
                vo.setTpList(collect);
            }

            // 查询物品信息
            List<TbHcBpcclWp> wpList = this.tbHcBpcclWpMapper.selectList(
                    new LambdaQueryWrapper<TbHcBpcclWp>()
                            .eq(TbHcBpcclWp::getPcywid, item.getId())
            );
            // 车辆物品
            List<TbHcBpcclWpVo> wpVoList = new ArrayList<>();
            wpList.forEach(wp -> {
                TbHcBpcclWpVo wpVo = new TbHcBpcclWpVo();
                BeanUtils.copyProperties(wp, wpVo);
                // 获取物品图片
                List<TbHcTp> wpTpList = this.tbHcTpMapper.selectList(
                        new LambdaQueryWrapper<TbHcTp>()
                                .eq(TbHcTp::getLybm, "tb_hc_bpccl_wp")
                                .eq(TbHcTp::getLyid, wp.getId())
                );
                List<String> wpTpIdList = wpTpList.stream().map(TbHcTp::getId).collect(Collectors.toList());
                wpVo.setWpTpIdList(wpTpIdList);
                wpVoList.add(wpVo);
            });
            vo.setWpVoList(wpVoList);
            // 同乘人员
            List<TbHcBpcry> ryList = this.tbHcBpcryMapper.selectList(
                    new LambdaQueryWrapper<TbHcBpcry>()
                            .eq(TbHcBpcry::getPcclId, item.getId())
                            .eq(TbHcBpcry::getXtSjzt, "1")
            );
            List<TbHcBpcryVo> ryVoList = new ArrayList<>();
            ryList.forEach(ry -> {
                TbHcBpcryVo ryVo = new TbHcBpcryVo();
                BeanUtils.copyProperties(ry, ryVo);
                ryVoList.add(ryVo);
            });
            vo.setRyList(ryVoList);
            rList.add(vo);
        });
        page.setTotal(count);
        page.setRecords(rList);
        return page;
    }

    // 保存人员物品列表信息
    private void saveClTp(List<TbHcBpcclWpVo> ryWpList, TbHcBpccl cl) {
        if (!CollectionUtils.isEmpty(ryWpList)) {
            ryWpList.forEach(wpVo -> {
                TbHcBpcclWp wp = new TbHcBpcclWp();
                BeanUtils.copyProperties(wpVo, wp);
                wp.setId(com.mosty.common.util.UUIDGenerator.getUUID());
                wp.setPcywid(cl.getId());
                wp.setXtSjly("2");
                wp.setSsbmdm(cl.getSsbmdm());
                wp.setSsbm(cl.getSsbm());
                wp.setSsbmid(cl.getSsbmid());
                wp.setSsxgajdm(cl.getSsxgajdm());
                wp.setSsxgaj(cl.getSsxgaj());
                wp.setSsxgajid(cl.getSsxgajid());
                wp.setSssgajdm(cl.getSssgajdm());
                wp.setSssgaj(cl.getSssgaj());
                wp.setSssgajid(cl.getSssgajid());
                this.tbHcBpcclWpMapper.insert(wp);
                // 保存物品图片
                List<String> wpTpIdList = wpVo.getWpTpIdList();
                this.saveTp(wpTpIdList, "tb_hc_bpccl_wp", wp.getId());
            });
        }
    }

    // 保存图片信息
    private void saveTp(List<String> tpIdList, String lybm, String lyid) {
        if (!CollectionUtils.isEmpty(tpIdList)) {
            tpIdList.forEach(item -> {
                TbHcTp tp = new TbHcTp();
                tp.setId(com.mosty.common.util.UUIDGenerator.getUUID());
                tp.setLybm(lybm);
                tp.setLyid(lyid);
                tp.setXtSjly("2");
                tp.setFjid(item);
                this.tbHcTpMapper.insert(tp);
            });
        }
    }

    // 保存车辆信息到巡逻盘查中
    private void saveClToXlpc(TbHcBpccl item, UserInfo user) {
        BpcclSaveDto dto = new BpcclSaveDto();
        BeanUtils.copyProperties(item, dto);
        dto.setPcsj(DateUtils.getQueryDateString(item.getPcsj(), "02"));
        dto.setPcdjd(item.getJd().doubleValue());
        dto.setPcdwd(item.getWd().doubleValue());
        dto.setIndustrialid(item.getPcmjJh());
        dto.setUserid(item.getPcmjSfzh());
        dto.setUsername(item.getPcmjXm());
        dto.setSsdwdm(user.getDeptCode());
        dto.setSsdwmc(user.getDeptName());
        dto.setSsfsxdm(user.getFxjDeptCode());
        dto.setSsfsxmc(user.getFxjDeptName());
        dto.setSsdszdm(user.getDszDeptCode());
        dto.setSsdszmc(user.getDszDeptName());
        dto.setPccljg(item.getPcclJg());
        dto.setPccljgmc(item.getPcclJgmc());
        dto.setGlxtdm("73"); //德阳巡防
        try {
            Map map = Kit.beanToMap(dto);
            HttpUtils.executePost(Constant.XLPC_URL + "kkpcPczqBpcclxxb/save", map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 获取盗抢车辆信息
    private DqclVo getDqcl(String hpzl, String hphm, UserInfo user) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("hpzl", hpzl);
            params.put("hphm", hphm);
            params.put("userid", user.getIdEntityCard());
            params.put("username", user.getUserName());
            params.put("ssdwdm", user.getDeptCode());
            params.put("ssdwmc", user.getDeptName());
            params.put("ssfsxdm", user.getFxjDeptCode());
            params.put("ssfsxmc", user.getFxjDeptName());
            params.put("ssdszdm", user.getDszDeptCode());
            params.put("ssdszmc", user.getDszDeptName());
            params.put("industrialid", user.getInDustRialId());
            params.put("mobile", "");
            String result = HttpUtils.executePost(Constant.XLPC_URL + "requestservice/zhk_Bdqjdcxx/queryDetail", params);
            if (com.mosty.common.base.util.StringUtils.isNotBlank(result)) {
                JSONObject json = JSONObject.parseObject(result);
                String code = json.getString("code");
                if ("0".equals(code)) {
                    String dataStr = json.getString("data");
                    return JSONObject.parseObject(dataStr, DqclVo.class);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取布控车辆信息
    private BkclVo getBkcl(String hpzl, String hphm, UserInfo user, TbHcBpccl item) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("hpzl", hpzl);
            params.put("hphm", hphm);
            params.put("kkid", "");
            params.put("kkmc", "");
            params.put("userid", user.getIdEntityCard());
            params.put("username", user.getUserName());
            params.put("ssdwdm", user.getDeptCode());
            params.put("ssdwmc", user.getDeptName());
            params.put("ssfsxdm", user.getFxjDeptCode());
            params.put("ssfsxmc", user.getFxjDeptName());
            params.put("ssdszdm", user.getDszDeptCode());
            params.put("ssdszmc", user.getDszDeptName());
            params.put("industrialid", user.getInDustRialId());
            params.put("mobile", "");
            params.put("zdxh", item.getZdxh());
            params.put("zdch", item.getZdch());
            params.put("zdsim", item.getZdsim());
            params.put("pcdjd", item.getJd().stripTrailingZeros().toPlainString());
            params.put("pcdwd", item.getWd().stripTrailingZeros().toPlainString());
            String result = HttpUtils.executePost(Constant.XLPC_URL + "lkClxx/queryDetail", params);
            if (com.mosty.common.base.util.StringUtils.isNotBlank(result)) {
                JSONObject json = JSONObject.parseObject(result);
                String code = json.getString("code");
                if ("0".equals(code)) {
                    String dataStr = json.getString("data");
                    return JSONObject.parseObject(dataStr, BkclVo.class);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询车辆详情信息
    private ClpcVo getClByHp(String hpzl, String hphm, UserInfo user, TbHcBpccl item) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("hpzl", hpzl);
            params.put("hphm", hphm);
            params.put("userid", user.getIdEntityCard());
            params.put("username", user.getUserName());
            params.put("ssdwdm", user.getDeptCode());
            params.put("ssdwmc", user.getDeptName());
            params.put("ssfsxdm", user.getFxjDeptCode());
            params.put("ssfsxmc", user.getFxjDeptName());
            params.put("ssdszdm", user.getDszDeptCode());
            params.put("ssdszmc", user.getDszDeptName());
            params.put("industrialid", user.getInDustRialId());
            params.put("mobile", "");
            params.put("zdxh", item.getZdxh());
            params.put("zdch", item.getZdch());
            params.put("zdsim", item.getZdsim());
            params.put("pcdjd", item.getJd().stripTrailingZeros().toPlainString());
            params.put("pcdwd", item.getWd().stripTrailingZeros().toPlainString());
            String result = HttpUtils.executePost(Constant.XLPC_URL + "requestservice/jdc/queryDetail", params);
            if (com.mosty.common.base.util.StringUtils.isNotBlank(result)) {
                JSONObject json = JSONObject.parseObject(result);
                String code = json.getString("code");
                String dataStr = json.getString("data");
                if ("0".equals(code)) {
                    JSONObject resJson = JSONObject.parseObject(dataStr);
                    return JSON.toJavaObject(resJson, ClpcVo.class);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取车辆重点标签状态（从内网中查询）
     * @param dto 车辆盘查DTO
     * @param user 用户信息
     * @return 车辆重点标签状态
     */
    private Integer getClZdbqZtFromNw(TbHcBpcclQuery dto, UserInfo user){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("hpzl", dto.getHpzl());
        paramMap.put("hphm", dto.getHphm());
        paramMap.put("userName", user.getUserName());
        paramMap.put("inDustRialId", user.getInDustRialId());
        paramMap.put("idEntityCard", user.getIdEntityCard());
        paramMap.put("phone", user.getPhone());
        paramMap.put("deptName", user.getDeptName());
        paramMap.put("deptCode", user.getDeptCode());
        paramMap.put("fxjDeptName", user.getFxjDeptName());
        paramMap.put("fxjDeptCode", user.getFxjDeptCode());
        paramMap.put("dszDeptName", user.getDszDeptName());
        paramMap.put("dszDeptCode", user.getDszDeptCode());
        paramMap.put("zdxh", dto.getZdxh());
        paramMap.put("zdch", dto.getZdch());
        paramMap.put("zdsim", dto.getZdsim());
        paramMap.put("jd", dto.getJd());
        paramMap.put("wd", dto.getWd());

        String sync = OkHttpUtils.builder().url(Constant.NW_URL)
                .addHeader("Content-Type", "application/json;charset=utf-8")
                .addParam("ywlx", "ZY")
                .addParam("url", Constant.NW_CLZDBQ_URL)
                .addParam("token", "46296c279b51420cb64bd5cb6d8ec3d8")
                .addParam("sbid", "ZY")
                .addParam("sbzid", "ZY")
                .addParam("data", paramMap)
                .post(true)
                .sync();
        if (com.mosty.common.base.util.StringUtils.isNotBlank(sync)) {
            JSONObject json = JSONObject.parseObject(sync);
            String errno = json.getString("errno");
            String dataStr = json.getString("data");
            if ("0".equals(errno)) {
                JSONObject dataJson = JSONObject.parseObject(dataStr);
                return Integer.parseInt(dataJson.getString("code"));
            }
        }
        return -1;
    }

    @Override
    public Map<String, Integer> bpcclTj() {
        Map<String, Integer> ryMap = new HashMap();
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);
        Integer integer = this.baseMapper.selectCount(new LambdaQueryWrapper<TbHcBpccl>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbHcBpccl::getSsbmdm, ssbm)
                .ge(TbHcBpccl::getPcsj, DateUtils.getSystemDateString())
        );
        ryMap.put("all", integer);
        Integer dq = this.baseMapper.selectCount(new LambdaQueryWrapper<TbHcBpccl>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbHcBpccl::getSsbmdm, ssbm)
                .ge(TbHcBpccl::getPcsj, DateUtils.getSystemDateString())
                .and(v -> v.like(TbHcBpccl::getBqmc, "DQ").or().like(TbHcBpccl::getBqmc, "盗抢"))
        );
        ryMap.put("dq", dq);
        Integer lk = this.baseMapper.selectCount(new LambdaQueryWrapper<TbHcBpccl>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbHcBpccl::getSsbmdm, ssbm)
                .ge(TbHcBpccl::getPcsj, DateUtils.getSystemDateString())
                .and(v -> v.like(TbHcBpccl::getBqmc, "LK").or().like(TbHcBpccl::getBqmc, "临控"))
        );
        ryMap.put("lk", lk);
        return ryMap;
    }

    @Override
    public int getSjpcc(String ssbmdm, String time) {
        return this.baseMapper.selectCount(
                new LambdaQueryWrapper<TbHcBpccl>()
                        .likeRight(!StringUtils.isEmpty(ssbmdm), TbHcBpccl::getSsbmdm, ssbmdm)
                        .eq(TbHcBpccl::getPcrq, time)
        );

    }

    @Override
    public Object getPctj(HcTjQuery query) {
        Map map = new HashMap();
        IPage<HcTjQuery> page = PageUtils.buildPage(query.getPageSize(), query.getPageCurrent());
        query.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(query.getSsbmdm(), null));
        IPage<Map<String, Object>> bmdmList = this.baseMapper.getPctj(page, query);
        map.put("pageList", bmdmList);
        //统计人员数
        map.put("clsl", this.baseMapper.getPctjCl(query));
        map.put("rysl", this.baseMapper.getPctjRy(query));
        return map;
    }

    @Override
    public int getPcWptj(String ssbmdm, String time) {
        return this.baseMapper.getPcWptj(ssbmdm, time);
    }

}
