package com.mosty.yjzl.service.Impl;

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.qwzx.TbQwJlDto;
import com.mosty.base.model.dto.qwzx.TbQwXfbbVo;
import com.mosty.base.model.dto.rwzx.TbRwTaskDto;
import com.mosty.base.model.dto.rwzx.TbRwTaskUserDto;
import com.mosty.base.model.dto.websocket
        .WebSocketObject;
import com.mosty.base.model.dto.yjzl.*;
import com.mosty.base.model.dto.yszx.TbYsSxtDto;
import com.mosty.base.model.entity.hczx.TbHcBpcry;
import com.mosty.base.model.entity.sjzx.TbJq;
import com.mosty.base.model.entity.trs.VwAlermYjgl;
import com.mosty.base.model.entity.trs.VwAlermYjzlUser;
import com.mosty.base.model.entity.yjzl.*;
import com.mosty.base.model.query.afjcsj.VwSyrkJzgjGxListQuery;
import com.mosty.base.model.vo.sjzx.TbJqVo;
import com.mosty.base.model.vo.trs.VwAlermYjzlVo;
import com.mosty.base.model.vo.yjzl.*;
import com.mosty.base.utils.*;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.query.yjzl.TbZlxxQuery;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.yjzl.mapper.*;
import com.mosty.yjzl.remote.*;
import com.mosty.yjzl.service.TbZlxxService;
import com.mosty.yjzl.utils.OkHttpUtils;
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.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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


@Service
@AllArgsConstructor
public class TbZlxxServiceImpl extends ServiceImpl<TbZlxxMapper, TbZlxx>
        implements TbZlxxService {

    private final TbZlxxMapper tbZlxxMapper;
    private final TbFzycMapper tbFzycMapper;
    private final TbYjxxMapper tbYjxxMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbZlxxZxjlMapper tbZlxxZxjlMapper;
    private final TbZlxxZxrMapper tbZlxxZxrMapper;
    private final TbYszxAdaptRemoteService tbYszxAdaptRemoteService;
    private final TbQwzxAdaptRemoteService tbQwzxAdaptRemoteService;
    private final TbZlxxPcryclMapper tbZlxxPcryclMapper;
    private final TbRwzxAdaptRemoteService tbRwzxAdaptRemoteService;
    private final TbZlYjSjMapper tbZlYjSjMapper;
    private final TbYjxxSjMapper tbYjxxSjMapper;
    private final TbZlYjRyMapper tbZlYjRyMapper;
    private final TbYjxxRyMapper tbYjxxRyMapper;
    private final TbZlYjClMapper tbZlYjClMapper;
    private final TbYjxxRyBqMapper tbYjxxRyBqMapper;
    private final TbYjxxClMapper tbYjxxClMapper;
    private final TbZlRgMapper tbZlRgMapper;
    private final TbSjzxAdaptRemoteService tbSjzxAdaptRemoteService;
    private final TbWebSocketAdaptRemoteService tbWebSocketAdaptRemoteService;
    private final TbFzycXfxzMapper tbFzycXfxzMapper;


    @Override
    public Map<String, Object> selectInstructCount(String startTime, String endTime) {
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);

        if (StringUtils.isEmpty(startTime)) {
            startTime = DateUtils.getSystemDateString() + " 00:00:00";
        }
        if (StringUtils.isEmpty(endTime)) {
            endTime = DateUtils.getSystemDateTimeString();
        }

        return this.tbZlxxMapper.selectInstructCount(startTime, endTime, ssbm);
    }


    @Override
    public IPage<TbZlxxVo> selectInstructList(TbZlxxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));

        if (StringUtils.isEmpty(dto.getEndTime())) {
            dto.setEndTime(DateUtils.getSystemDateTimeString());
        }
        map.put("pageIndex", (dto.getPageNum() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
        List<TbZlxx> list = tbZlxxMapper.getListPage(map);
        int count = this.tbZlxxMapper.getCount(map);
        IPage<TbZlxxVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<TbZlxxVo> rList = new ArrayList<>();
        list.forEach(item -> {
            TbZlxxVo zlxxVo = new TbZlxxVo();
            BeanUtils.copyProperties(item, zlxxVo);
            // 查询执行人
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, item.getId())
            );
            List<TbZlxxZxrVo> zxrDtoList = new ArrayList<>();
            zxrList.forEach(zxr -> {
                TbZlxxZxrVo zxrVo = new TbZlxxZxrVo();
                BeanUtils.copyProperties(zxr, zxrVo);
                zxrDtoList.add(zxrVo);
            });
            zlxxVo.setZxrVoList(zxrDtoList);
            // 查询执行记录
            List<TbZlxxZxjl> zxjlList = this.tbZlxxZxjlMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxjl>()
                            .eq(TbZlxxZxjl::getZlId, item.getId())
            );
            List<TbZlxxZxjlVo> zxjlVoList = new ArrayList<>();
            zxjlList.forEach(zxjl -> {
                TbZlxxZxjlVo vo = new TbZlxxZxjlVo();
                BeanUtils.copyProperties(zxjl, zxjlVoList);
                zxjlVoList.add(vo);
            });
            zlxxVo.setZxjlVoList(zxjlVoList);
            // 查询是否是预警信息生成的指令
            if ("01".equals(item.getZlly())) {
                TbYjxx yjxx = this.tbYjxxMapper.getInfoById(item.getYwId());
                if (yjxx != null && StringUtils.isNotBlank(yjxx.getYjGzyid())) {
                    TbYsSxtDto sxt = this.tbYszxAdaptRemoteService.getSxtInfoVo(yjxx.getYjGzyid());
                    if (sxt != null) {
                        zlxxVo.setSbbh(StringUtils.isNotBlank(sxt.getJkdbgbh()) ? sxt.getJkdbgbh() : sxt.getSbbh());
                    }
                }
            }
            rList.add(zlxxVo);
        });
        page.setTotal(count);
        page.setRecords(rList);
        return page;
    }

    @Override
    @Transactional
    public String addZl(TbZlxxInsertDto dto) {
//        if (StringUtils.isNotBlank(dto.getSsbmid())) {
//            DeptInfoVo deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
//            if (deptInfoVo == null || StringUtils.isBlank(deptInfoVo.getDeptid())) {
//                deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmid());
//            }
//            if (deptInfoVo == null) {
//                throw new BusinessException("所属部门不存在");
//            }
//            dto.setSsbmid(deptInfoVo.getDeptid());
//        }
        UserInfo user = UserInfoManager.getUser();
        TbZlxx zl = new TbZlxx();
        BeanUtils.copyProperties(dto, zl);
        zl.setZlly(zl.getZllx());
        if (StringUtils.isNotBlank(dto.getZlxflx()) && dto.getZlxflx().equals("2") && !zl.getZljsdx().equals("01")) {
            throw new BusinessException("短信指令只能下发到人员!");
        }


//        if ("2".equals(zl.getZlzxlx()) && ("02".equals(zl.getZljsdx()) || ("03").equals(zl.getZljsdx()))) {
//            throw new BusinessException("多人任务不允许发送给部门或巡组");
//        }
        if (zl.getJd() != null && zl.getWd() != null) {
            zl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
            if (StringUtils.isBlank(zl.getZlfsdd())) {
                JSONObject obj = this.tbBaseAdaptRemoteService.getAddress(String.valueOf(zl.getJd()), String.valueOf(zl.getWd()));
                if (obj != null) {
                    String address = obj.getString("address");
                    zl.setZlfsdd(address);
                }
            }
        }
        if (user != null) {
            zl.setZlfqrId(String.valueOf(user.getUserId()));
            zl.setZlfqrSfzh(user.getIdEntityCard());
            zl.setZlfqr(user.getUserName());
        } else {
            zl.setZlfqrId("-1");
            zl.setZlfqrSfzh("000000");
            zl.setZlfqr("系统");
        }
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(String.valueOf(dto.getSsbmid()));
        if (StringUtils.isBlank(dept.getDeptid()) && user != null) {
            dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(String.valueOf(user.getDeptId()));
        }
        if (dept != null && StringUtils.isNotEmpty(dept.getDeptid())) {
            zl.setSsbmdm(dept.getDeptcode());
            zl.setSsbm(dept.getDeptname());
            zl.setSsbmid(dept.getDeptid());
            zl.setSsxgaj(dept.getFxjname());
            zl.setSsxgajdm(dept.getFxjcode());
            zl.setSsxgajid(dept.getFxjid());
            zl.setSssgaj(dept.getDszname());
            zl.setSssgajdm(dept.getDszcode());
            zl.setSssgajid(dept.getDszid());
            zl.setZlfsddxzqh(StringUtils.isNotBlank(dept.getDeptcode()) ? dept.getDeptcode().substring(0, 6) : null);
        } else {
            zl.setSsbm("四川省德阳市公安局");
            zl.setSsbmdm("510600000000");
            zl.setSsbmid("44015");
            zl.setSsxgaj("四川省德阳市公安局");
            zl.setSsxgajid("44015");
            zl.setSsxgajdm("510600000000");
            zl.setSssgajid("44015");
            zl.setSssgaj("四川省德阳市公安局");
            zl.setSssgajdm("510600000000");
        }
        zl.setXtSjly("1");
        zl.setId(UUIDGenerator.getUUID());
        // 计算指令接收人数
        // 签收有效时间
        String qs = this.tbBaseAdaptRemoteService.getValue("ZLQSYXSJ");
        int qsyxsj = qs == null ? 5 : Integer.parseInt(qs);
        zl.setQsyxsj(DateUtils.getNextDate(new Date(), "m", qsyxsj));
        // 到达有效时间
        String dd = this.tbBaseAdaptRemoteService.getValue("ZLDDXCYXSJ");
        int ddxcyxsj = dd == null ? 15 : Integer.parseInt(dd);
        zl.setDdxcyxsj(DateUtils.getNextDate(new Date(), "m", ddxcyxsj));
        // 完结有效时间
        String wj = this.tbBaseAdaptRemoteService.getValue("ZLCZWJSJ");
        int zlwjyxsj = wj == null ? 120 : Integer.parseInt(wj);
        zl.setZlwjyxsj(DateUtils.getNextDate(new Date(), "m", zlwjyxsj));
        this.totalZljsrs(dto, zl);
        zl.setZlfqrq(new Date());
        zl.setZlfqsj(new Date());
        if (StringUtils.isBlank(zl.getZlzxlx())) {
            zl.setZlzxlx("1");
        }
        zl.setQszt("01");
        zl.setZlwjzt("91");
        zl.setZlzxzt("01");
        zl.setZlzxqk(DateUtils.getQueryDateString(new Date(), "02") + "," + zl.getZlfqr() + "下发了该指令！！");
        this.baseMapper.insertEntity(zl);
        // 添加指令执行人和指令执行记录
        this.addZlxxZxr(zl, dto);
        if ("04".equals(zl.getZllx())) {
            this.addSjzl(zl);
        } else if ("02".equals(zl.getZllx())) {
            this.addRyZl(zl);
        } else if ("03".equals(zl.getZllx())) {
            this.addClZl(zl);
        } else if ("06".equals(zl.getZllx())) {
            this.addRgZl(zl, dto);
        }
        return zl.getId();
    }

    // 添加人工指令
    private void addRgZl(TbZlxx zl, TbZlxxInsertDto dto) {
        TbZlRg rgzl = new TbZlRg();
        BeanUtils.copyProperties(zl, rgzl);
        rgzl.setZllx(dto.getRgzlLx());
        this.tbZlRgMapper.insertEntity(rgzl);
    }

    // 添加车辆指令子表
    private void addClZl(TbZlxx zl) {
        TbZlYjCl zlCl = new TbZlYjCl();
        BeanUtils.copyProperties(zl, zlCl);
        // 指令ID
        zlCl.setZlId(zl.getId());
        // 预警ID
        zlCl.setYjId(zl.getYwId());
        // 查询预警事件
        TbYjxxCl cl = this.tbYjxxClMapper.selectOne(
                new LambdaQueryWrapper<TbYjxxCl>()
                        .eq(TbYjxxCl::getYjId, zl.getYwId())
        );
        // 查询预警信息
        if (cl != null) {
            zlCl.setYjclId(cl.getId());
            zlCl.setYjClbq(cl.getYjClbq());
            zlCl.setYjlyBqmc(cl.getYjlyBqmc());
            zlCl.setYjClyjlx("2");
            zlCl.setYjFssj(cl.getYjFssj());
            zlCl.setYjSj(cl.getYjSj());
            zlCl.setYjJb(cl.getYjJb());
            zlCl.setZldj(cl.getYjJb());
            zlCl.setZlCphlx(cl.getYjCphlx());
            zlCl.setZlClcph(cl.getYjClcph());
            zlCl.setZlCljdcsyr(cl.getYjCljdcsyr());
            zlCl.setZlCljdcsyrhm(cl.getYjCljdcsyrhm());
            zlCl.setZlCphys(cl.getYjCphys());
            zlCl.setZlClys(cl.getYjClys());
            zlCl.setZlClpp(cl.getYjClpp());
            zlCl.setZlCllx(cl.getYjCllx());
            zlCl.setZlClnk(cl.getYjClnk());
            zlCl.setZlXsfx(cl.getYjXsfx());
            zlCl.setZlXssd(cl.getYjXssd());
            zlCl.setZlClrs(cl.getYjClrs());
            zlCl.setZlXssd(cl.getYjXssd());
            zlCl.setYjGzyid(cl.getYjGzyid());
            zlCl.setZlDzXzqh(cl.getYjDzXzqh());
            zlCl.setZlDz(cl.getYjDz());
            zlCl.setTpId(cl.getTpId());
        }
        zlCl.setZlBt(zl.getZlbt());
        zlCl.setZlNr(zl.getZlnr());
        this.tbZlYjClMapper.insertEntity(zlCl);
    }

    // 添加人员指令子表
    private void addRyZl(TbZlxx zl) {
        TbZlYjRy zlRy = new TbZlYjRy();
        BeanUtils.copyProperties(zl, zlRy);
        // 指令ID
        zlRy.setZlId(zl.getId());
        // 预警ID
        zlRy.setYjId(zl.getYwId());
        // 查询预警事件
        TbYjxxRy ry = this.tbYjxxRyMapper.selectOne(
                new LambdaQueryWrapper<TbYjxxRy>()
                        .eq(TbYjxxRy::getYjId, zl.getYwId())
        );
        if (ry != null) {
            zlRy.setRyyjId(ry.getId());
            zlRy.setYjFssj(ry.getYjFssj());
            zlRy.setYjSj(ry.getYjSj());
            zlRy.setYjJb(ry.getYjJb());
            zlRy.setZlRybq(ry.getYjRybq());
            zlRy.setZlRybqmc(ry.getYjRybqmc());
            zlRy.setZlZjlx(ry.getYjZjlx());
            zlRy.setZlZjhm(ry.getYjZjhm());
            zlRy.setZlRyxm(ry.getYjRyxm());
            zlRy.setZlRyxb(ry.getYjRyxb());
            zlRy.setZlRynl(ry.getYjRynl());
            zlRy.setZlRymz(ry.getYjRymz());
            zlRy.setZlRyjg(ry.getYjRyjg());
            zlRy.setZlRyhjdXzqh(ry.getYjRyhjdXzqh());
            zlRy.setZlRyhjd(ry.getYjRyhjd());
            zlRy.setZlRyjzd(ry.getYjRyjzd());
            zlRy.setZlRyjzdXzqh(ry.getYjRyjzdXzqh());
            zlRy.setZlRygzdw(ry.getYjRygzdw());
            zlRy.setZlRygzdwid(ry.getYjRygzdwid());
            zlRy.setZlRylxdh(ry.getYjRylxdh());
            zlRy.setZlXsd(ry.getYjXsd());
            zlRy.setZlXwms(ry.getYjXwms());
            zlRy.setLtx(ry.getLtx());
            zlRy.setLty(ry.getLty());
            zlRy.setRbx(ry.getRbx());
            zlRy.setRby(ry.getRby());
            zlRy.setYjGzyid(ry.getYjGzyid());
            zlRy.setYjDzxzqh(ry.getYjDzxzqh());
            zlRy.setYjDz(ry.getYjDz());
            zlRy.setTpId(ry.getTpId());
            zlRy.setCoverImage(ry.getCoverImage());
            zlRy.setYjlyBqmc(ry.getYjlyBqmc());
        }
        zlRy.setZlfsrq(zl.getZlfqrq());
        zlRy.setZlfssj(zl.getZlfqsj());
        zlRy.setZlBt(zl.getZlbt());
        zlRy.setZlNr(zl.getZlnr());
        this.tbZlYjRyMapper.insertEntity(zlRy);
    }

    // 添加事件指令子表
    private void addSjzl(TbZlxx zl) {
        TbZlYjSj zlSj = new TbZlYjSj();
        BeanUtils.copyProperties(zl, zlSj);
        // 指令ID
        zlSj.setZlId(zl.getId());
        // 预警ID
        zlSj.setYjId(zl.getYwId());
        // 查询预警事件
        TbYjxxSj sj = this.tbYjxxSjMapper.selectOne(
                new LambdaQueryWrapper<TbYjxxSj>()
                        .eq(TbYjxxSj::getYjId, zl.getYwId())
        );
        if (sj != null) {
            zlSj.setYjSjid(sj.getId());
            zlSj.setSjFl(sj.getSjFl());
            zlSj.setSjFlmc(sj.getSjFlmc());
            zlSj.setYjGzyid(sj.getYjGzyid());
            zlSj.setYjJb(sj.getYjJb());
            zlSj.setYjFssj(sj.getYjFssj());
            zlSj.setYjSj(sj.getYjSj());
            zlSj.setYjDzxzqh(sj.getYjDzXzqh());
            zlSj.setYjDz(sj.getYjDz());
            zlSj.setTpId(sj.getYjTp());
        }
        zlSj.setZlBt(zl.getZlbt());
        zlSj.setZlNr(zl.getZlnr());
        zlSj.setZlfsrq(zl.getZlfqrq());
        zlSj.setZlfssj(zl.getZlfqsj());
        zlSj.setCzqk(zl.getZlzxqk());
        this.tbZlYjSjMapper.insertEntity(zlSj);
    }

    // 添加指令执行人和指令执行记录
    private void addZlxxZxr(TbZlxx zl, TbZlxxInsertDto dto) {
        if (StringUtils.isNotBlank(dto.getSsbmid())) {
            DeptInfoVo deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
            if (deptInfoVo == null || StringUtils.isBlank(deptInfoVo.getDeptid())) {
                deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmid());
            }
            if (deptInfoVo == null) {
                throw new BusinessException("所属部门不存在");
            }
            dto.setSsbmid(deptInfoVo.getDeptid());
        }
        UserInfo user = UserInfoManager.getUser();
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        if (CollectionUtils.isEmpty(zxrDtoList)) {
            throw new BusinessException("请至少添加一位指令执行人");
        }
        List<TbZlxxZxr> zxrList = new ArrayList<>();
        zxrDtoList.forEach(zxrDto -> {
            // 查询
            TbZlxxZxr zxr = new TbZlxxZxr();
            BeanUtils.copyProperties(zl, zxr);
            zxr.setId(UUIDGenerator.getUUID());
            zxr.setZlId(zl.getId());
            zxr.setDqzt("01");
            zxr.setDqztsm(DateUtils.getQueryDateString(new Date(), "02") + ",接收到该指令！！");
            zxr.setDqztSj(new Date());
            zxr.setXtSjly("1");
            zxr.setZxrLx(zl.getZljsdx());
            zxr.setZxrXzid(zxrDto.getZxrXzid());
            zxr.setZxrId(zxrDto.getZxrId());
            zxr.setZxrXm(zxrDto.getZxrXm());
            zxr.setZxrSfz(zxrDto.getZxrSfz());
            zxr.setZxrDh(zxrDto.getZxrDh());
            zxr.setZxrJllx(zxrDto.getZxrJllx());
            DeptInfoVo dept = null;
            if ("01".equals(zxrDto.getZxrLx())) {
                // 查询人员的所属部门
                SysUser user1 = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(zxr.getZxrSfz());
                if (user1 == null) {
                    throw new BusinessException("用户" + zxr.getZxrXm() + "（" + zxr.getZxrSfz() + "）不存在！");
                }
                zxr.setZxrId(String.valueOf(user1.getId()));
                if (StringUtils.isBlank(zxr.getZxrDh())) {
                    zxr.setZxrDh(user1.getMobile());
                }
                dept = this.tbBaseAdaptRemoteService.getDeptInfoByUserId(String.valueOf(user1.getId()));
                zxr.setZxrLx("01");
                zl.setZljsdx("01");
            } else if ("02".equals(zxrDto.getZxrLx())) {
                dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(zxrDto.getSsbmid());
                zxr.setZxrLx("02");
                zl.setZljsdx("02");
            } else if ("03".equals(zxrDto.getZxrLx())) {
                TbQwXfbbVo bb = this.tbQwzxAdaptRemoteService.getBbInfo(zxrDto.getZxrXzid());
                if (bb != null) {
                    dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(bb.getSsbmid());
                }
                zxr.setZxrLx("03");
                zl.setZljsdx("03");
            }
            if (dept != null) {
                zxr.setSsbmdm(dept.getDeptcode());
                zxr.setSsbm(dept.getDeptname());
                zxr.setSsbmid(dept.getDeptid());
                zxr.setSsxgaj(dept.getFxjname());
                zxr.setSsxgajdm(dept.getFxjcode());
                zxr.setSsxgajid(dept.getFxjid());
                zxr.setSssgaj(dept.getDszname());
                zxr.setSssgajdm(dept.getDszcode());
                zxr.setSssgajid(dept.getDszid());
            }
            this.tbZlxxZxrMapper.insert(zxr);
            // 添加指令执行记录
            this.addZlxxZxjl(zl, "00", zxr);
            zxrList.add(zxr);
        });
        // 添加发送任务
        this.addTask(zl, zxrList, user);
    }

    // 添加指令执行人和指令执行记录
    private void addZlxxZxrOther(TbZlxx zl, TbZlxxInsertDto dto) {
        if (StringUtils.isNotBlank(dto.getSsbmid())) {
            DeptInfoVo deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
            if (deptInfoVo == null || StringUtils.isBlank(deptInfoVo.getDeptid())) {
                deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmid());
            }
            if (deptInfoVo == null) {
                throw new BusinessException("所属部门不存在");
            }
            dto.setSsbmid(deptInfoVo.getDeptid());
        }
        SysUser user = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(dto.getSfzh());
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        if (CollectionUtils.isEmpty(zxrDtoList)) {
            throw new BusinessException("请至少添加一位指令执行人");
        }
        List<TbZlxxZxr> zxrList = new ArrayList<>();
        zxrDtoList.forEach(zxrDto -> {
            // 查询
            TbZlxxZxr zxr = new TbZlxxZxr();
            BeanUtils.copyProperties(zl, zxr);
            zxr.setId(UUIDGenerator.getUUID());
            zxr.setZlId(zl.getId());
            zxr.setDqzt("01");
            zxr.setDqztsm(DateUtils.getQueryDateString(new Date(), "02") + ",接收到该指令！！");
            zxr.setDqztSj(new Date());
            zxr.setXtSjly("1");
            zxr.setZxrLx(zl.getZljsdx());
            zxr.setZxrXzid(zxrDto.getZxrXzid());
            zxr.setZxrId(zxrDto.getZxrId());
            zxr.setZxrXm(zxrDto.getZxrXm());
            zxr.setZxrSfz(zxrDto.getZxrSfz());
            zxr.setZxrDh(zxrDto.getZxrDh());
            zxr.setZxrJllx(zxrDto.getZxrJllx());
            DeptInfoVo dept = null;
            if ("01".equals(zxrDto.getZxrLx())) {
                // 查询人员的所属部门
                SysUser user1 = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(zxr.getZxrSfz());
                if (user1 == null) {
                    throw new BusinessException("用户" + zxr.getZxrXm() + "（" + zxr.getZxrSfz() + "）不存在！");
                }
                zxr.setZxrId(String.valueOf(user1.getId()));
                zxr.setZxrDh(user1.getMobile());
                dept = this.tbBaseAdaptRemoteService.getDeptInfoByUserId(String.valueOf(user1.getId()));
                zxr.setZxrLx("01");
                zl.setZljsdx("01");
            } else if ("02".equals(zxrDto.getZxrLx())) {
                dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(zxr.getSsbmid());
                zxr.setZxrLx("02");
                zl.setZljsdx("02");
            } else if ("03".equals(zxrDto.getZxrLx())) {
                TbQwXfbbVo bb = this.tbQwzxAdaptRemoteService.getBbInfo(zxrDto.getZxrXzid());
                if (bb != null) {
                    dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(bb.getSsbmid());
                }
                zxr.setZxrLx("03");
                zl.setZljsdx("03");
            }
            if (dept != null) {
                zxr.setSsbmdm(dept.getDeptcode());
                zxr.setSsbm(dept.getDeptname());
                zxr.setSsbmid(dept.getDeptid());
                zxr.setSsxgaj(dept.getFxjname());
                zxr.setSsxgajdm(dept.getFxjcode());
                zxr.setSsxgajid(dept.getFxjid());
                zxr.setSssgaj(dept.getDszname());
                zxr.setSssgajdm(dept.getDszcode());
                zxr.setSssgajid(dept.getDszid());
            }
            this.tbZlxxZxrMapper.insert(zxr);
            // 添加指令执行记录
            this.addZlxxZxjl(zl, "00", zxr);
            zxrList.add(zxr);
        });
        // 添加发送任务
        this.addTaskOther(zl, zxrList, user);
    }

    // 添加发送任务
    private void addTaskOther(TbZlxx zl, List<TbZlxxZxr> list, SysUser user) {
        TbRwTaskDto dto = new TbRwTaskDto();
        String rwmc = zl.getZlbt();
        String rwnr = zl.getZlnr();
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getDeptInfoByUserId(String.valueOf(user.getId()));
        dto.setYwid(zl.getId()).setRwmc(rwmc).setRwnr(rwnr).setRwxxlx("01")
                .setRwjsdx(zl.getZljsdx()).setSfaqwc("1").setRwlx("2").setRwxl("05").setRwjjcd("2")
                .setRwyqwcsj(DateUtils.getNextDate(new Date(), "H", +1))
                .setRwms(zl.getZlzxlx()).setSsbmdm(zl.getSsbmdm()).setSsbm(zl.getSsbm()).setSsbmid(zl.getSsbmid());
        dto.setCjrid(String.valueOf(user.getId())).setCjrxm(user.getUserName())
                .setCjrsfzh(user.getIdEntityCard()).setSsbm(dept.getDeptname()).setSsbmdm(dept.getDeptid());
        // 添加执行人
        List<TbRwTaskUserDto> userDtoList = new ArrayList<>();
        list.forEach(item -> {
            TbRwTaskUserDto userDto = new TbRwTaskUserDto();
            userDto.setRwzflx("01").setJsrid(item.getZxrId()).setJsrmc(item.getZxrXm()).setJsrsfzh(item.getZxrSfz())
                    .setJsrlxdh(item.getZxrDh()).setJsbmdm(item.getSsbmid()).setJsxzid(item.getZxrXzid()).setRwjsdx(zl.getZljsdx());
            userDtoList.add(userDto);
        });
        dto.setUserDtoList(userDtoList);
        this.tbRwzxAdaptRemoteService.addRw(dto);
    }

    // 添加发送任务
    private void addTask(TbZlxx zl, List<TbZlxxZxr> list, UserInfo user) {
        TbRwTaskDto dto = new TbRwTaskDto();
        String rwmc = zl.getZlbt();
        String rwnr = zl.getZlnr();
        dto.setYwid(zl.getId()).setRwmc(rwmc).setRwnr(rwnr).setRwxxlx("01")
                .setRwjsdx(zl.getZljsdx()).setSfaqwc("1").setRwlx("2").setRwxl("05").setRwjjcd("2")
                .setRwyqwcsj(DateUtils.getNextDate(new Date(), "H", +1))
                .setRwms(zl.getZlzxlx()).setSsbmdm(zl.getSsbmdm()).setSsbm(zl.getSsbm()).setSsbmid(zl.getSsbmid());
        if (user != null) {
            dto.setCjrid(String.valueOf(user.getUserId())).setCjrxm(user.getUserName())
                    .setCjrsfzh(user.getIdEntityCard()).setSsbm(user.getDeptName()).setSsbmdm(String.valueOf(user.getDeptId()));
        } else {
            dto.setCjrid("-1").setCjrxm("系统").setCjrsfzh("000000");
        }
        // 添加执行人
        List<TbRwTaskUserDto> userDtoList = new ArrayList<>();
        list.forEach(item -> {
            TbRwTaskUserDto userDto = new TbRwTaskUserDto();
            userDto.setRwzflx("01").setJsrid(item.getZxrId()).setJsrmc(item.getZxrXm()).setJsrsfzh(item.getZxrSfz())
                    .setJsrlxdh(item.getZxrDh()).setJsbmdm(item.getSsbmid()).setJsxzid(item.getZxrXzid()).setRwjsdx(zl.getZljsdx());
            userDtoList.add(userDto);
        });
        dto.setUserDtoList(userDtoList);
        this.tbRwzxAdaptRemoteService.addRw(dto);
    }

    /**
     * 添加指令执行记录
     *
     * @param zl     指令
     * @param zlzxzt 执行记录的状态
     */
    private void addZlxxZxjl(TbZlxx zl, String zlzxzt, TbZlxxZxr zxr) {
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZlzxddXzqhdm(zl.getZlfsddxzqh());
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt(zlzxzt);
        String str = "";
        if (zxr != null) {
            if ("01".equals(zxr.getZxrLx())) {
                str = "(人员)" + zxr.getZxrXm();
            } else if ("03".equals(zxr.getZxrLx())) {
                TbQwXfbbVo bb = this.tbQwzxAdaptRemoteService.getBbInfo(zxr.getZxrXzid());
                if (bb != null) {
                    str = "(巡组)" + (StringUtils.isNotBlank(bb.getJzMc()) ? bb.getJzMc() : (bb.getFzrXm() + "警组"));
                }
            } else if ("02".equals(zxr.getZxrLx())) {
                str = "(部门)" + zxr.getSsbm();
            }
        }
        if ("00".equals(zlzxzt)) {
            zxjl.setZxnr(zl.getZlfqr() + "于" + DateUtils.getQueryDateString(new Date(), "02") +
                    "下发了该指令！接收" + (StringUtils.isNotBlank(str) ? str : ""));
        } else if ("91".equals(zlzxzt)) {
            zxjl.setZxnr(DateUtils.getQueryDateString(new Date(), "02") + "指令过期，");
        } else if ("94".equals(zlzxzt)) {
            zxjl.setZxnr("指令有效时间到期，指令超时，指令无需再进行！！");
        }
        if (zl.getJd() != null && zl.getWd() != null) {
            zxjl.setJd(zl.getJd());
            zxjl.setWd(zl.getWd());
            zxjl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        zxjl.setSsbmdm(zl.getSsbmdm());
        zxjl.setSsbm(zl.getSsbm());
        zxjl.setSsbmid(zl.getSsbmid());
        zxjl.setSsxgaj(zl.getSsxgaj());
        zxjl.setSsxgajdm(zl.getSsxgajdm());
        zxjl.setSsxgajid(zl.getSsxgajid());
        zxjl.setSssgaj(zl.getSssgaj());
        zxjl.setSssgajdm(zl.getSssgajdm());
        zxjl.setSssgajid(zl.getSssgajid());
        zxjl.setZlzxrId(zl.getZlfqrId());
        zxjl.setZlzxrSfz(zl.getZlfqrSfzh());
        zxjl.setZlzxrXm(zl.getZlfqr());
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("1");
        zxjl.setId(UUIDGenerator.getUUID());
        if (StringUtils.isNotBlank(zxjl.getZxnr())) {
            this.tbZlxxZxjlMapper.insertEntity(zxjl);
        }
    }

    @Override
    public void updateZl(TbZlxxInsertDto dto) {
        TbZlxx zl = new TbZlxx();
        BeanUtils.copyProperties(dto, zl);
        if ("2".equals(zl.getZlzxlx()) && ("02".equals(zl.getZljsdx()) || ("03").equals(zl.getZljsdx()))) {
            throw new BusinessException("多人任务不允许发送给部门或巡组");
        }
        if (zl.getJd() != null && zl.getWd() != null) {
            zl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        // 计算指令接收人数
        this.totalZljsrs(dto, zl);
        this.baseMapper.updateEntity(zl);
        // 删除指令接收人信息
        this.tbZlxxZxjlMapper.deleteByZlid(dto.getId());
        // 删除指令执行人信息
        this.tbZlxxZxrMapper.deleteByZlid(dto.getId());
        // 重新添加指令执行人和指令执行过程
        this.addZlxxZxr(zl, dto);
    }

    @Override
    public void changeZlGqzt() {
        List<TbZlxx> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<TbZlxx>()
                        .eq(TbZlxx::getXtSjzt, "1")
                        .notIn(TbZlxx::getZlzxzt, "32", "92", "93", "94")
                        .le(TbZlxx::getQsyxsj, new Date())
                        .isNotNull(TbZlxx::getQsyxsj)
        );
        list.forEach(item -> {
            item.setZlzxzt("94");
            item.setZlwjzt("94");
            item.setZlwjsj(item.getQsyxsj());
            item.setZlwjsm("指令有效时间到期，指令超时，且有人员未完成指令！！");
            item.setZlzxqk("指令有效时间到期，指令超时，且有人员未完成指令！！" + item.getZlzxqk());
            this.baseMapper.updateEntity(item);
            // 修改指令执行人的指令状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, item.getId())
                            .eq(TbZlxxZxr::getXtSjzt, "1")
                            .notIn(TbZlxxZxr::getDqzt, "32", "92", "93", "94")
            );
            zxrList.forEach(zxr -> {
                zxr.setDqzt("94");
                zxr.setDqztSj(item.getQsyxsj());
                zxr.setDqztsm("指令有效时间到期，指令超时，指令无需再进行！！");
                this.tbZlxxZxrMapper.updateById(zxr);
            });
            // 添加指令执行记录
            this.addZlxxZxjl(item, "94", null);
        });
    }

    // 统计指令接收人数
    private void totalZljsrs(TbZlxxInsertDto dto, TbZlxx zl) {
        if ("01".equals(zl.getZljsdx())) {
            zl.setZljsrs(dto.getZxrDtoList() == null ? 0 : dto.getZxrDtoList().size());
        } else if ("02".equals(zl.getZljsdx())) {
            if (StringUtils.isEmpty(zl.getSsbmid())) {
                throw new BusinessException("请选择对应的部门！！");
            }
            int count = this.tbBaseAdaptRemoteService.getUserCount(zl.getSsbmid());
            zl.setZljsrs(count);
        } else if ("03".equals(zl.getZljsdx())) {
            List<TbZlxxZxrDtoInsertDto> list = dto.getZxrDtoList();
            if (CollectionUtils.isEmpty(list)) {
                throw new BusinessException("请选择对应的巡组！！");
            }
            TbZlxxZxrDtoInsertDto zxr = list.get(0);
            // 查询该巡组的人数
            List<TbQwJlDto> jlList = this.tbQwzxAdaptRemoteService.getJlxxByBbid(zxr.getZxrXzid());
            zl.setZljsrs(jlList.size());
        }
    }


    @Override
    @Transactional
    public void zlHandle(TbZlxxZtChangeDto dto) {
        UserInfo user = UserInfoManager.get();
        TbZlxx zl = this.tbZlxxMapper.selectById(dto.getZlId());
        if (zl == null) {
            throw new BusinessException("指令未找到！！");
        }
        TbZlxxZxr zxr = null;
        if ("01".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrId, String.valueOf(user.getUserId()))
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        } else if ("02".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
                            .eq(TbZlxxZxr::getSsbmid, dto.getSsbmid())
            );
        } else if ("03".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrXzid, dto.getZlzxrXzid())
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        }
        if (zxr == null) {
            throw new BusinessException("执行人未找到！！");
        }
        // 签收状态
        if ("01".equals(dto.getZlzxzt())) {
            this.changeQszt(dto, zl, user, zxr);
            //如果是犯罪预测指令 修改犯罪预测指令签收状态
            if (zl.getZllx().equals("09")) {
                tbFzycXfxzMapper.updateSfqs(zl.getId());
            }
        } else if ("02".equals(dto.getZlzxzt())) {
            // 到达状态
            this.changeDdzt(dto, zl, user, zxr);
        } else if ("03".equals(dto.getZlzxzt())) {
            // 处置完成
            this.changeCzzt(dto, zl, user, zxr);
        } else if ("04".equals(dto.getZlzxzt())) {
            // 完结
            this.changeWjzt(dto, zl, user, zxr);
        } else if ("08".equals(dto.getZlzxzt())) {
            // 转发
            this.changeZfzt(dto, zl, user, zxr);
        }
    }

    @Override
    public void zlHandleRygk(TbZlxxZtChangeRygkDto dto) {
        UserInfo user = UserInfoManager.get();
        TbZlxx zl = this.tbZlxxMapper.selectById(dto.getZlId());
        if (zl == null) {
            throw new BusinessException("指令未找到！！");
        }
        TbZlxxZxr zxr = null;
        if ("01".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrId, String.valueOf(user.getUserId()))
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        } else if ("02".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
                            .eq(TbZlxxZxr::getSsbmid, dto.getSsbmid())
            );
        } else if ("03".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrXzid, dto.getZlzxrXzid())
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        }
        if (zxr == null) {
            throw new BusinessException("执行人未找到！！");
        }
        // 研判
        if ("05".equals(dto.getZlzxzt()) || "06".equals(dto.getZlzxzt())) {
            this.changeYp(dto, zl, user, zxr);
        } else if ("07".equals(dto.getZlzxzt())) {
            // 反馈
            this.changeYp(dto, zl, user, zxr);
        }
    }

    private void changeYp(TbZlxxZtChangeRygkDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        String zt = "";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if ("05".equals(dto.getZlzxzt())) {
            sm += "研判了该指令";
            zt = "26";
        } else if ("06".equals(dto.getZlzxzt())) {
            sm += "再次研判了该指令";
            zt = "26";
        } else if ("07".equals(dto.getZlzxzt())) {
            sm += "反馈了该指令";
            zt = "27";
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录--研判反馈
        this.addZlxxZxjlYp(zl, zxr, zt, sm, dto, user);
        // 判断修改指令状态
        this.changeZlYpState(zl, zt, user, zxr);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 判断修改指令签收状态
    private void changeZlYpState(TbZlxx zl, String zt, UserInfo user, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setQszt(zt);
            zl.setQssj(new Date());
            zl.setZlzxzt(zt);
            String sm = "";
            if ("26".equals(zl.getZlzxzt())) {
                sm = "研判";
            } else if ("27".equals(zl.getZlzxzt())) {
                sm += "反馈";
            }
            zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令。" + zl.getZlzxqk());
            zxr.setZlzxzt(zt);
            zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令。" + zl.getZlzxqk());
            this.tbZlxxMapper.updateEntity(zl);
        }
    }


    // 添加指令执行记录
    private void addZlxxZxjlYp(TbZlxx zl, TbZlxxZxr zxr, String zt, String sm, TbZlxxZtChangeRygkDto dto, UserInfo user) {
        List<String> fjids = StringUtils.isNotBlank(dto.getFjid()) ? Arrays.asList(dto.getFjid().split(",")) : null;
        List<String> fjmcs = StringUtils.isNotBlank(dto.getFjmc()) ? Arrays.asList(dto.getFjmc().split(",")) : null;
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt(zt);
        if ("26".equals(zt)) {
            zxjl.setZxnr(sm + "，研判意见：" + dto.getZxnr());
        } else if ("27".equals(zt)) {
            zxjl.setZxnr(sm + "，处置经过描述：" + dto.getZxnr());
        }
        if (dto.getJd() != null && dto.getWd() != null) {
            zxjl.setJd(dto.getJd());
            zxjl.setWd(dto.getWd());
            zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (zxr != null) {
            zxjl.setSsbmdm(zxr.getSsbmdm());
            zxjl.setSsbm(zxr.getSsbm());
            zxjl.setSsbmid(zxr.getSsbmid());
            zxjl.setSsxgaj(zxr.getSsxgaj());
            zxjl.setSsxgajdm(zxr.getSsxgajdm());
            zxjl.setSsxgajid(zxr.getSsxgajid());
            zxjl.setSssgaj(zxr.getSssgaj());
            zxjl.setSssgajdm(zxr.getSssgajdm());
            zxjl.setSssgajid(zxr.getSssgajid());
            zxjl.setZlzxrId(zxr.getZxrId());
            zxjl.setZlzxrXzid(zxr.getZxrXzid());
            zxjl.setZlzxrDh(zxr.getZxrDh());
            zxjl.setZlzxrSfz(zxr.getZxrSfz());
            zxjl.setZlzxrXm(zxr.getZxrXm());
        }
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("2");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
        if (!CollectionUtils.isEmpty(fjids) && !CollectionUtils.isEmpty(fjmcs)) {
            for (int i = 0; i < fjids.size(); i++) {
                zxjl = new TbZlxxZxjl();
                zxjl.setId(UUIDGenerator.getUUID());
                zxjl.setZlId(zl.getId());
                zxjl.setZxsj(new Date());
                zxjl.setZlzxzt(zt);
                zxjl.setZxnr(fjmcs.get(i));
                zxjl.setFjid(fjids.get(i));
                if (dto.getJd() != null && dto.getWd() != null) {
                    zxjl.setJd(dto.getJd());
                    zxjl.setWd(dto.getWd());
                    zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
                    zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
                }
                if (zxr != null) {
                    zxjl.setSsbmdm(zxr.getSsbmdm());
                    zxjl.setSsbm(zxr.getSsbm());
                    zxjl.setSsbmid(zxr.getSsbmid());
                    zxjl.setSsxgaj(zxr.getSsxgaj());
                    zxjl.setSsxgajdm(zxr.getSsxgajdm());
                    zxjl.setSsxgajid(zxr.getSsxgajid());
                    zxjl.setSssgaj(zxr.getSssgaj());
                    zxjl.setSssgajdm(zxr.getSssgajdm());
                    zxjl.setSssgajid(zxr.getSssgajid());
                    zxjl.setZlzxrId(zxr.getZxrId());
                    zxjl.setZlzxrXzid(zxr.getZxrXzid());
                    zxjl.setZlzxrDh(zxr.getZxrDh());
                    zxjl.setZlzxrSfz(zxr.getZxrSfz());
                    zxjl.setZlzxrXm(zxr.getZxrXm());
                }
                zxjl.setZlzxdd(zl.getZlfsdd());
                zxjl.setXtSjly("2");
                zxjl.setId(UUIDGenerator.getUUID());
                this.tbZlxxZxjlMapper.insertEntity(zxjl);
            }
        }
    }

    @Override
    public TbZlxxInfoVo getInfo(String id) {
        UserInfo user = UserInfoManager.get();
        TbZlxx zlxx = this.baseMapper.selectById(id);
        TbZlxxInfoVo vo = new TbZlxxInfoVo();
        BeanUtils.copyProperties(zlxx, vo);
        if (user != null) {
            TbZlxxZxr zxr = this.baseMapper.selectZxr(id, String.valueOf(user.getDeptId()), user.getIdEntityCard());
            if (zxr != null) {
                TbZlxxZxrVo zxrVo = new TbZlxxZxrVo();
                BeanUtils.copyProperties(zxr, zxrVo);
                vo.setZxrVo(zxrVo);
            }
        }
        return vo;
    }

    @Override
    public void addZxjl(TbZlxxZxjlInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        if (user == null) {
            throw new BusinessException("登录异常，请重新登录");
        }
        TbZlxx zl = this.baseMapper.selectById(dto.getZlId());
        if (zl == null) {
            throw new BusinessException("没有找到当前指令信息！！");
        }
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        BeanUtils.copyProperties(dto, zxjl);
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt("05");
        if (zl.getJd() != null && zl.getWd() != null) {
            zxjl.setJd(zl.getJd());
            zxjl.setWd(zl.getWd());
            zxjl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        zxjl.setSsbmdm(user.getDeptCode());
        zxjl.setSsbm(user.getDeptName());
        zxjl.setSsbmid(String.valueOf(user.getDeptId()));
        zxjl.setSsxgaj(user.getFxjDeptName());
        zxjl.setSsxgajdm(user.getFxjDeptCode());
        zxjl.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        zxjl.setSssgaj(user.getDszDeptName());
        zxjl.setSssgajdm(user.getDszDeptCode());
        zxjl.setSssgajid(String.valueOf(user.getDszDeptId()));
        zxjl.setZlzxrId(String.valueOf(user.getUserId()));
        zxjl.setZlzxrSfz(user.getIdEntityCard());
        zxjl.setZlzxrXm(user.getUserName());
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("1");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    @Override
    public void addPcrycl(TbZlxxPcryclInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        TbZlxxPcrycl rc = new TbZlxxPcrycl();
        rc.setId(UUIDGenerator.getUUID());
        rc.setPclxdm(dto.getPclxdm());
        rc.setPcryclId(dto.getPcryclId());
        rc.setZlId(dto.getZlId());
        if (user != null) {
            rc.setSsbmdm(user.getDeptCode());
            rc.setSsbm(user.getDeptName());
            rc.setSsbmid(String.valueOf(user.getDeptId()));
            rc.setSsxgaj(user.getFxjDeptName());
            rc.setSsxgajdm(user.getFxjDeptCode());
            rc.setSsxgajid(String.valueOf(user.getFxjDeptId()));
            rc.setSssgaj(user.getDszDeptName());
            rc.setSssgajdm(user.getDszDeptCode());
            rc.setSssgajid(String.valueOf(user.getDszDeptId()));
        }
        rc.setXtSjly("2");
        this.tbZlxxPcryclMapper.insert(rc);
    }

    // 转发
    private void changeZfzt(TbZlxxZtChangeDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        String zt = "04";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令！";
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZfZlxxZxjl(zl, zxr, dto, user);
        // 判断修改指令完结状态
        this.changeZlZfState(zl, user, dto, zxr);
        // 添加新的指令信息
        this.sendNewZlxx(zl, dto);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
        // 查询是否是警情指令，如果是警情指令，修改警情的处置状态
        this.updateJqCzzt(zl, "2");
    }

    // 转发
    private void changeZfztOther(TbZlxxZtChangeDto dto, TbZlxx zl, SysUser user, TbZlxxZxr zxr) {
        String zt = "04";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令！";
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZfZlxxZxjlOther(zl, zxr, dto, user);
        // 判断修改指令完结状态
        this.changeZlZfStateOther(zl, user, dto, zxr);
        // 添加新的指令信息
        this.sendNewZlxx(zl, dto);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 转发指令，添加新的指令信息
    private void sendNewZlxx(TbZlxx zl, TbZlxxZtChangeDto dto) {
        TbZlxxInsertDto ins = new TbZlxxInsertDto();
        BeanUtils.copyProperties(zl, ins);
        ins.setZxrDtoList(dto.getZxrDtoList());
        this.addZl(ins);
    }

    // 到达状态
    private void changeWjzt(TbZlxxZtChangeDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        String zt = "";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if (zl.getZlwjyxsj().getTime() <= new Date().getTime()) {
            zt = "93";
            sm += "超时完结了该指令！！指令完结说明：" + dto.getZlwjsm();
        } else {
            zt = "92";
            sm += "完结了该指令！！指令完结说明：" + dto.getZlwjsm();
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjl(zl, zxr, zt, sm, dto, user);
        // 判断修改指令完结状态
        this.changeZlWjState(zl, zt, sm, user, dto.getZlwjsm(), zxr);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
        // 查询是否是警情指令，如果是警情指令，修改警情的处置状态
        this.updateJqCzzt(zl, "2");
    }

    // 到达状态
    private void changeWjztOther(TbZlxxZtChangeDto dto, TbZlxx zl, SysUser user, TbZlxxZxr zxr) {
        String zt = "";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if (zl.getZlwjyxsj().getTime() <= new Date().getTime()) {
            zt = "93";
            sm += "超时完结了该指令！！指令完结说明：" + dto.getZlwjsm();
        } else {
            zt = "92";
            sm += "完结了该指令！！指令完结说明：" + dto.getZlwjsm();
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjlOther(zl, zxr, zt, sm, dto, user);
        // 判断修改指令完结状态
        this.changeZlWjStateOther(zl, zt, sm, user, dto.getZlwjsm(), zxr);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    private void changeCzzt(TbZlxxZtChangeDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        String zt = "32";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02") + "完成处置！！";
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjl(zl, zxr, zt, sm, dto, user);
        // 判断修改指令状态
        this.changeZlDdState(zl, zt, sm, user, zxr);
        // 修改执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    private void changeCzztOther(TbZlxxZtChangeDto dto, TbZlxx zl, SysUser user, TbZlxxZxr zxr) {
        String zt = "32";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02") + "完成处置！！";
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjlOther(zl, zxr, zt, sm, dto, user);
        // 判断修改指令状态
        this.changeZlDdStateOther(zl, zt, sm, user, zxr);
        // 修改执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 到达状态
    private void changeDdzt(TbZlxxZtChangeDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        String zt = "";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if ("01".equals(zl.getZllx())) {
            zt = "11";
            sm += "到达现场，开始巡逻中..";
        } else {
            if (zl.getDdxcyxsj().getTime() <= new Date().getTime()) {
                zt = "25";
                sm += "到达现场，时间超时！！";
            } else if ("0".equals(dto.getSfsddd())) {
                // 自动
                zt = "22";
                sm += "通过位置计算，确认自动到达现场！";
            } else if ("1".equals(dto.getSfsddd())) {
                // 手动，判断两点之间的距离是否相差100米
                if (dto.getJd() != null && dto.getWd() != null && zl.getJd() != null && zl.getWd() != null) {
                    Double jl = Kit.getDistance(zl.getJd().doubleValue(), zl.getWd().doubleValue(),
                            dto.getJd().doubleValue(), dto.getWd().doubleValue());
                    if (jl < 100) {
                        zt = "24";
                        sm += "手动确认到达现场，经位置计算，确已到达，于指令地点相距" + jl + "米";
                    } else {
                        zt = "23";
                        sm += "手动确认到达现场，经位置计算，实际并未到达，于指令地点相距" + jl + "米";
                    }
                } else {
                    zt = "23";
                    sm += "手动确认到达现场，位置未知！！";
                }
            }
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjl(zl, zxr, zt, sm, dto, user);
        // 判断修改指令状态
        this.changeZlDdState(zl, zt, sm, user, zxr);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 到达状态
    private void changeDdztOther(TbZlxxZtChangeDto dto, TbZlxx zl, SysUser user, TbZlxxZxr zxr) {
        String zt = "";
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if ("01".equals(zl.getZllx())) {
            zt = "11";
            sm += "到达现场，开始巡逻中..";
        } else {
            if (zl.getDdxcyxsj().getTime() <= new Date().getTime()) {
                zt = "25";
                sm += "到达现场，时间超时！！";
            } else if ("0".equals(dto.getSfsddd())) {
                // 自动
                zt = "22";
                sm += "通过位置计算，确认自动到达现场！";
            } else if ("1".equals(dto.getSfsddd())) {
                // 手动，判断两点之间的距离是否相差100米
                if (dto.getJd() != null && dto.getWd() != null && zl.getJd() != null && zl.getWd() != null) {
                    Double jl = Kit.getDistance(zl.getJd().doubleValue(), zl.getWd().doubleValue(),
                            dto.getJd().doubleValue(), dto.getWd().doubleValue());
                    if (jl < 100) {
                        zt = "24";
                        sm += "手动确认到达现场，经位置计算，确已到达，于指令地点相距" + jl + "米";
                    } else {
                        zt = "23";
                        sm += "手动确认到达现场，经位置计算，实际并未到达，于指令地点相距" + jl + "米";
                    }
                } else {
                    zt = "23";
                    sm += "手动确认到达现场，位置未知！！";
                }
            }
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 添加执行记录
        this.addZlxxZxjlOther(zl, zxr, zt, sm, dto, user);
        // 判断修改指令状态
        this.changeZlDdStateOther(zl, zt, sm, user, zxr);
        // 修改指令执行人信息
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 签收状态
    private void changeQszt(TbZlxxZtChangeDto dto, TbZlxx zl, UserInfo user, TbZlxxZxr zxr) {
        // 是否超过签收时间
        String zt;
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if (zl.getQsyxsj().getTime() <= new Date().getTime()) {
            zt = "03";
            sm += "超时签收了该指令！";
        } else {
            zt = "02";
            sm += "签收了该指令！";
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 判断修改指令状态
        this.changeZlQsState(zl, zt, user, zxr);
        // 添加执行记录
        this.addZlxxZxjl(zl, zxr, zt, sm, dto, user);
        // 修改执行人状态
        this.tbZlxxZxrMapper.updateById(zxr);
        // 查询是否是警情指令，如果是警情指令，修改警情的处置状态
        this.updateJqCzzt(zl, "1");
    }

    // 查询是否是警情指令，如果是警情指令，修改警情的处置状态
    private void updateJqCzzt(TbZlxx zl, String czzt) {
        if ("05".equals(zl.getZllx()) && StringUtils.isNotBlank(zl.getYwId())) {
            TbJqVo jq = this.tbSjzxAdaptRemoteService.getJqInfo(zl.getYwId());
            if (jq != null) {
                this.tbSjzxAdaptRemoteService.updateJqCzzt(jq.getId(), czzt);
                // 表示警情已经完成，推送数据给前端，前端删除撒点
                if ("2".equals(czzt)) {
                    WebSocketObject obj = new WebSocketObject("02", jq.getId(), null);
                    this.tbWebSocketAdaptRemoteService.sendMessage(obj);
                }
            }
        }
    }

    // 签收状态
    private void changeQsztOther(TbZlxxZtChangeDto dto, TbZlxx zl, SysUser user, TbZlxxZxr zxr) {
        // 是否超过签收时间
        String zt;
        String sm = "于" + DateUtils.getQueryDateString(new Date(), "02");
        if (zl.getQsyxsj().getTime() <= new Date().getTime()) {
            zt = "03";
            sm += "超时签收了该指令！";
        } else {
            zt = "02";
            sm += "签收了该指令！";
        }
        zxr.setDqzt(zt);
        zxr.setDqztSj(new Date());
        zxr.setDqztsm(sm);
        // 判断修改指令状态
        this.changeZlQsStateOther(zl, zt, user, zxr);
        // 添加执行记录
        this.addZlxxZxjlOther(zl, zxr, zt, sm, dto, user);
        // 修改执行人状态
        this.tbZlxxZxrMapper.updateById(zxr);
    }

    // 判断修改指令转发状态
    private void changeZlZfState(TbZlxx zl, UserInfo user, TbZlxxZtChangeDto dto, TbZlxxZxr zxr) {
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        String str = zxrDtoList.stream().map(it -> {
            if ("01".equals(it.getZxrLx())) {
                return "(人员)" + it.getZxrXm();
            } else if ("03".equals(it.getZxrLx())) {
                return "(巡组)" + it.getZxrXzmc();
            } else if ("02".equals(it.getZxrLx())) {
                return "(部门)" + it.getSsbmmc();
            }
            return "";
        }).collect(Collectors.joining("、"));
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setZlzxzt("04");
            zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!!"
                    + zl.getZlzxqk());
            zxr.setZlzxzt("04");
            zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!!"
                    + zl.getZlzxqk());
            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他人的指令状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt("04");
                item.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!"
                        + zl.getZlzxqk());
                item.setDqzt("04");
                item.setDqztsm(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!");
                item.setDqztSj(new Date());
                this.tbZlxxZxrMapper.updateById(item);
            });
        }
    }

    // 判断修改指令转发状态
    private void changeZlZfStateOther(TbZlxx zl, SysUser user, TbZlxxZtChangeDto dto, TbZlxxZxr zxr) {
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        String str = zxrDtoList.stream().map(it -> {
            if ("01".equals(it.getZxrLx())) {
                return "(人员)" + it.getZxrXm();
            } else if ("03".equals(it.getZxrLx())) {
                return "(巡组)" + it.getZxrXzmc();
            } else if ("02".equals(it.getZxrLx())) {
                return "(部门)" + it.getSsbmmc();
            }
            return "";
        }).collect(Collectors.joining("、"));
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setZlzxzt("04");
            zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!!"
                    + zl.getZlzxqk());
            zxr.setZlzxzt("04");
            zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!!"
                    + zl.getZlzxqk());
            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他人的指令状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt("04");
                item.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!"
                        + zl.getZlzxqk());
                item.setDqzt("04");
                item.setDqztsm(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令,转发给->" + str + "!");
                item.setDqztSj(new Date());
                this.tbZlxxZxrMapper.updateById(item);
            });
        }
    }

    // 判断修改指令到达状态
    private void changeZlWjStateOther(TbZlxx zl, String zt, String sm, SysUser user, String zlwjsm, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setDdxczt(zt);
            zl.setDdxcsj(new Date());
            zl.setZlzxzt(zt);
            zl.setZlwjsm(zlwjsm);
            zl.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());

            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他人的指令状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
                item.setDqzt(zt);
                item.setDqztsm(user.getUserName() + sm);
                item.setDqztSj(new Date());
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有已完成状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z ->
                    !"92".equals(z.getDqzt()) &&
                            !"93".equals(z.getDqzt())
            ).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setZlwjzt("92");
                zl.setZlwjsj(new Date());
                zl.setZlzxzt("92");
                zl.setZlzxqk(user.getUserName() + sm + "，所有人已完结该指令！！" + zl.getZlzxqk());
                zl.setZlwjsm(zlwjsm + "--" + zl.getZlwjsm());
                zxr.setZlzxzt("92");
                zxr.setZlzxqk(user.getUserName() + sm + "，所有人已完结该指令！！" + zl.getZlzxqk());
                this.tbZlxxMapper.updateEntity(zl);
            }
        }
    }

    // 判断修改指令到达状态
    private void changeZlWjState(TbZlxx zl, String zt, String sm, UserInfo user, String zlwjsm, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setDdxczt(zt);
            zl.setDdxcsj(new Date());
            zl.setZlzxzt(zt);
            zl.setZlwjsm(zlwjsm);
            zl.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());

            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他人的指令状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
                item.setDqzt(zt);
                item.setDqztsm(user.getUserName() + sm);
                item.setDqztSj(new Date());
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有已完成状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z ->
                    !"92".equals(z.getDqzt()) &&
                            !"93".equals(z.getDqzt())
            ).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setZlwjzt("92");
                zl.setZlwjsj(new Date());
                zl.setZlzxzt("92");
                zl.setZlzxqk(user.getUserName() + sm + "，所有人已完结该指令！！" + zl.getZlzxqk());
                zl.setZlwjsm(zlwjsm + "--" + zl.getZlwjsm());
                zxr.setZlzxzt("92");
                zxr.setZlzxqk(user.getUserName() + sm + "，所有人已完结该指令！！" + zl.getZlzxqk());
                this.tbZlxxMapper.updateEntity(zl);
            }
        }
    }

    // 判断修改指令到达状态
    private void changeZlDdState(TbZlxx zl, String zt, String sm, UserInfo user, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setDdxczt(zt);
            zl.setDdxcsj(new Date());
            zl.setZlzxzt(zt);
            zl.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他指令执行人的状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
                item.setDqzt(zt);
                item.setDqztSj(new Date());
                item.setDqztsm(user.getUserName() + sm);
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有未签收和待到达现场的状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z ->
                    "01".equals(z.getDqzt()) ||
                            "02".equals(z.getDqzt()) ||
                            "03".equals(z.getDqzt()) ||
                            "21".equals(z.getDqzt()) ||
                            "22".equals(z.getDqzt()) ||
                            "23".equals(z.getDqzt()) ||
                            "24".equals(z.getDqzt()) ||
                            "31".equals(z.getDqzt())
            ).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setZlzxzt("32");
                zl.setZlzxqk(user.getUserName() + sm + "，所有人处置完成！！" + zl.getZlzxqk());
                zxr.setZlzxzt("32");
                zxr.setZlzxqk(user.getUserName() + sm + "，所有人处置完成！！" + zl.getZlzxqk());
                this.tbZlxxMapper.updateEntity(zl);
            }
        }
    }

    // 判断修改指令到达状态
    private void changeZlDdStateOther(TbZlxx zl, String zt, String sm, SysUser user, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setDdxczt(zt);
            zl.setDdxcsj(new Date());
            zl.setZlzxzt(zt);
            zl.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
            this.tbZlxxMapper.updateEntity(zl);
            // 修改其他指令执行人的状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setZlzxqk(user.getUserName() + sm + zl.getZlzxqk());
                item.setDqzt(zt);
                item.setDqztSj(new Date());
                item.setDqztsm(user.getUserName() + sm);
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有未签收和待到达现场的状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z ->
                    "01".equals(z.getDqzt()) ||
                            "02".equals(z.getDqzt()) ||
                            "03".equals(z.getDqzt()) ||
                            "21".equals(z.getDqzt()) ||
                            "22".equals(z.getDqzt()) ||
                            "23".equals(z.getDqzt()) ||
                            "24".equals(z.getDqzt()) ||
                            "31".equals(z.getDqzt())
            ).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setZlzxzt("32");
                zl.setZlzxqk(user.getUserName() + sm + "，所有人处置完成！！" + zl.getZlzxqk());
                zxr.setZlzxzt("32");
                zxr.setZlzxqk(user.getUserName() + sm + "，所有人处置完成！！" + zl.getZlzxqk());
                this.tbZlxxMapper.updateEntity(zl);
            }
        }
    }

    // 判断修改指令签收状态
    private void changeZlQsState(TbZlxx zl, String zt, UserInfo user, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setQszt(zt);
            zl.setQssj(new Date());
            zl.setZlzxzt(zt);
            String sm = "02".equals(zt) ? "签收" : "超时签收";
            zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令" + zl.getZlzxqk());
            zxr.setZlzxzt(zt);
            zxr.setDqzt(zt);
            zxr.setDqztsm(sm);
            zxr.setDqztSj(new Date());
            zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令" + zl.getZlzxqk());
            // 修改其他执行人的状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setDqzt(zt);
                item.setDqztsm(sm);
                item.setDqztSj(new Date());
                item.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                        + "了该指令" + zl.getZlzxqk());
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有未签收的状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z -> "01".equals(z.getDqzt())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setQszt("02");
                zl.setQssj(new Date());
                zl.setZlzxzt("02");
                zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
                zxr.setDqzt(zt);
                zxr.setDqztsm(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
                zxr.setDqztSj(new Date());
                zxr.setZlzxzt("02");
                zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
            }
        }
        this.tbZlxxMapper.updateEntity(zl);
    }

    // 判断修改指令签收状态
    private void changeZlQsStateOther(TbZlxx zl, String zt, SysUser user, TbZlxxZxr zxr) {
        if ("1".equals(zl.getZlzxlx())) {
            //一人完成，指令即完成
            zl.setQszt(zt);
            zl.setQssj(new Date());
            zl.setZlzxzt(zt);
            String sm = "02".equals(zt) ? "签收" : "超时签收";
            zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令" + zl.getZlzxqk());
            zxr.setZlzxzt(zt);
            zxr.setDqzt(zt);
            zxr.setDqztsm(sm);
            zxr.setDqztSj(new Date());
            zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                    + "了该指令" + zl.getZlzxqk());
            // 修改其他执行人的状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            zxrList.forEach(item -> {
                item.setZlzxzt(zt);
                item.setDqzt(zt);
                item.setDqztsm(sm);
                item.setDqztSj(new Date());
                item.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02") + sm
                        + "了该指令" + zl.getZlzxqk());
                this.tbZlxxZxrMapper.updateById(item);
            });
        } else if ("2".equals(zl.getZlzxlx())) {
            // 查询是否所有人的状态都达到该状态
            List<TbZlxxZxr> zxrList = this.tbZlxxZxrMapper.selectList(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
            // 查询是否有未签收的状态，如果没有，则改指令状态为签收
            zxrList = zxrList.stream().filter(z -> "01".equals(z.getDqzt())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(zxrList)) {
                zl.setQszt("02");
                zl.setQssj(new Date());
                zl.setZlzxzt("02");
                zl.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
                zxr.setDqzt(zt);
                zxr.setDqztsm(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
                zxr.setDqztSj(new Date());
                zxr.setZlzxzt("02");
                zxr.setZlzxqk(user.getUserName() + "于" + DateUtils.getQueryDateString(new Date(), "02")
                        + "签收了该指令，所有人都完成了指令的签收！！" + zl.getZlzxqk());
            }
        }
        this.tbZlxxMapper.updateEntity(zl);
    }

    // 添加转发指令执行记录
    private void addZfZlxxZxjl(TbZlxx zl, TbZlxxZxr zxr, TbZlxxZtChangeDto dto, UserInfo user) {
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt("11");
        String zxnr = user.getUserName();
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        String str = zxrDtoList.stream().map(it -> {
            if ("01".equals(it.getZxrLx())) {
                return "(人员)" + it.getZxrXm();
            } else if ("03".equals(it.getZxrLx())) {
                return "(巡组)" + it.getZxrXzmc();
            } else if ("02".equals(it.getZxrLx())) {
                return "(部门)" + it.getSsbmmc();
            }
            return "";
        }).collect(Collectors.joining("、"));
        zxjl.setZxnr(zxnr + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令，转发给->" + str);
        if (dto.getJd() != null && dto.getWd() != null) {
            zxjl.setJd(dto.getJd());
            zxjl.setWd(dto.getWd());
            zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (zxr != null) {
            zxjl.setSsbmdm(zxr.getSsbmdm());
            zxjl.setSsbm(zxr.getSsbm());
            zxjl.setSsbmid(zxr.getSsbmid());
            zxjl.setSsxgaj(zxr.getSsxgaj());
            zxjl.setSsxgajdm(zxr.getSsxgajdm());
            zxjl.setSsxgajid(zxr.getSsxgajid());
            zxjl.setSssgaj(zxr.getSssgaj());
            zxjl.setSssgajdm(zxr.getSssgajdm());
            zxjl.setSssgajid(zxr.getSssgajid());
            zxjl.setZlzxrId(zxr.getZxrId());
            zxjl.setZlzxrXzid(zxr.getZxrXzid());
            zxjl.setZlzxrDh(zxr.getZxrDh());
            zxjl.setZlzxrSfz(zxr.getZxrSfz());
            zxjl.setZlzxrXm(zxr.getZxrXm());
        }
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("2");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    // 添加转发指令执行记录
    private void addZfZlxxZxjlOther(TbZlxx zl, TbZlxxZxr zxr, TbZlxxZtChangeDto dto, SysUser user) {
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt("11");
        String zxnr = user.getUserName();
        List<TbZlxxZxrDtoInsertDto> zxrDtoList = dto.getZxrDtoList();
        String str = zxrDtoList.stream().map(it -> {
            if ("01".equals(it.getZxrLx())) {
                return "(人员)" + it.getZxrXm();
            } else if ("03".equals(it.getZxrLx())) {
                return "(巡组)" + it.getZxrXzmc();
            } else if ("02".equals(it.getZxrLx())) {
                return "(部门)" + it.getSsbmmc();
            }
            return "";
        }).collect(Collectors.joining("、"));
        zxjl.setZxnr(zxnr + "于" + DateUtils.getQueryDateString(new Date(), "02") + "转发了该指令，转发给->" + str);
        if (dto.getJd() != null && dto.getWd() != null) {
            zxjl.setJd(dto.getJd());
            zxjl.setWd(dto.getWd());
            zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (zxr != null) {
            zxjl.setSsbmdm(zxr.getSsbmdm());
            zxjl.setSsbm(zxr.getSsbm());
            zxjl.setSsbmid(zxr.getSsbmid());
            zxjl.setSsxgaj(zxr.getSsxgaj());
            zxjl.setSsxgajdm(zxr.getSsxgajdm());
            zxjl.setSsxgajid(zxr.getSsxgajid());
            zxjl.setSssgaj(zxr.getSssgaj());
            zxjl.setSssgajdm(zxr.getSssgajdm());
            zxjl.setSssgajid(zxr.getSssgajid());
            zxjl.setZlzxrId(zxr.getZxrId());
            zxjl.setZlzxrXzid(zxr.getZxrXzid());
            zxjl.setZlzxrDh(zxr.getZxrDh());
            zxjl.setZlzxrSfz(zxr.getZxrSfz());
            zxjl.setZlzxrXm(zxr.getZxrXm());
        }
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("2");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    // 添加指令执行记录
    private void addZlxxZxjl(TbZlxx zl, TbZlxxZxr zxr, String zt, String sm, TbZlxxZtChangeDto dto, UserInfo user) {
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt(zt);
        String zxnr = user.getUserName();
        zxjl.setZxnr(zxnr + sm);
        if (dto.getJd() != null && dto.getWd() != null) {
            zxjl.setJd(dto.getJd());
            zxjl.setWd(dto.getWd());
            zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (zxr != null) {
            zxjl.setSsbmdm(zxr.getSsbmdm());
            zxjl.setSsbm(zxr.getSsbm());
            zxjl.setSsbmid(zxr.getSsbmid());
            zxjl.setSsxgaj(zxr.getSsxgaj());
            zxjl.setSsxgajdm(zxr.getSsxgajdm());
            zxjl.setSsxgajid(zxr.getSsxgajid());
            zxjl.setSssgaj(zxr.getSssgaj());
            zxjl.setSssgajdm(zxr.getSssgajdm());
            zxjl.setSssgajid(zxr.getSssgajid());
            zxjl.setZlzxrId(zxr.getZxrId());
            zxjl.setZlzxrXzid(zxr.getZxrXzid());
            zxjl.setZlzxrDh(zxr.getZxrDh());
            zxjl.setZlzxrSfz(zxr.getZxrSfz());
            zxjl.setZlzxrXm(zxr.getZxrXm());
        }
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("2");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    // 添加指令执行记录
    private void addZlxxZxjlOther(TbZlxx zl, TbZlxxZxr zxr, String zt, String sm, TbZlxxZtChangeDto dto, SysUser user) {
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt(zt);
        String zxnr = user.getUserName();
        zxjl.setZxnr(zxnr + sm);
        if (dto.getJd() != null && dto.getWd() != null) {
            zxjl.setJd(dto.getJd());
            zxjl.setWd(dto.getWd());
            zxjl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (zxr != null) {
            zxjl.setSsbmdm(zxr.getSsbmdm());
            zxjl.setSsbm(zxr.getSsbm());
            zxjl.setSsbmid(zxr.getSsbmid());
            zxjl.setSsxgaj(zxr.getSsxgaj());
            zxjl.setSsxgajdm(zxr.getSsxgajdm());
            zxjl.setSsxgajid(zxr.getSsxgajid());
            zxjl.setSssgaj(zxr.getSssgaj());
            zxjl.setSssgajdm(zxr.getSssgajdm());
            zxjl.setSssgajid(zxr.getSssgajid());
            zxjl.setZlzxrId(zxr.getZxrId());
            zxjl.setZlzxrXzid(zxr.getZxrXzid());
            zxjl.setZlzxrDh(zxr.getZxrDh());
            zxjl.setZlzxrSfz(zxr.getZxrSfz());
            zxjl.setZlzxrXm(zxr.getZxrXm());
        }
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("2");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    private TbZlxx addTrsZlxx(VwAlermYjzlVo yj, TbZlxx zl, VwAlermYjgl gl) {
        zl = new TbZlxx();
        zl.setId(yj.getId());
        zl.setZllx("07");
        zl.setZlly("07");
        zl.setZlzxlx("1");
        zl.setZlbt("预警人员《" + gl.getGlxm() + "（" + gl.getGlsfzh() + "）》");
        zl.setZlnr(gl.getHdxq());
        zl.setZlzxqk("系统发送了当前指令！");
        zl.setZlfqrq(yj.getFbsj());
        zl.setZlfqsj(yj.getFbsj());
        zl.setYwId(yj.getZlbh());
        String zldj;
        switch (gl.getYjlyjbDm()) {
            case "10":
                zldj = "10";
                break;
            case "20":
                zldj = "30";
                break;
            case "30":
                zldj = "40";
                break;
            case "15":
                zldj = "20";
                break;
            default:
                zldj = "40";
                break;
        }
        zl.setZldj(zldj);
        zl.setZljsdx("01");
        if ("未签收".equals(yj.getStatus())) {
            zl.setQszt("01");
            zl.setZlwjzt("91");
            zl.setZlzxzt("01");
        } else if ("正常签收".equals(yj.getStatus())) {
            zl.setQszt("02");
            zl.setZlzxzt("02");
        } else if ("超时签收".equals(yj.getStatus())) {
            zl.setQszt("03");
            zl.setZlzxzt("03");
        } else if ("正常反馈".equals(yj.getStatus()) || "超时反馈".equals(yj.getStatus())) {
            zl.setZlzxzt("26");
        }
        zl.setQsyxsj(yj.getQssj());
        zl.setZlwjyxsj(yj.getCzsj());
        zl.setJd(gl.getHdjd());
        zl.setWd(gl.getHdwd());
        if (zl.getJd() != null && zl.getWd() != null) {
            zl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        zl.setZlfsddxzqh(StringUtils.isNotBlank(gl.getSqrdwdh()) ? gl.getSqrdwdh().substring(0, 6) : "");
        zl.setZlfsdd(gl.getHdfsdxz());
        zl.setZlfqrId("-1");
        zl.setZlfqr("系统");
        zl.setZlfqrSfzh("000000");
        zl.setXtSjly("1");
        zl.setFjId(gl.getFileids());
        zl.setZljsrs(yj.getUserList().size());
        if (StringUtils.isNotBlank(gl.getSqrdwdh())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(gl.getSqrdwdh());
            if (dept != null) {
                zl.setSsbmid(dept.getDeptid());
                zl.setSsbmdm(dept.getDeptcode());
                zl.setSsbm(dept.getDeptname());
                zl.setSsxgajid(dept.getFxjid());
                zl.setSsxgajdm(dept.getFxjcode());
                zl.setSsxgaj(dept.getFxjname());
                zl.setSssgajid(dept.getDszid());
                zl.setSssgajdm(dept.getDszcode());
                zl.setSssgaj(dept.getDszname());
            }
        }
        this.baseMapper.insertEntity(zl);
        return zl;
    }

    @Override
    @Transactional
    public void addTrsZl(VwAlermYjzlVo zlVo) {
        // 查询是否有当前指令
        VwAlermYjgl gl = zlVo.getVwAlermYjgl();
        TbZlxx zl = this.baseMapper.selectById(zlVo.getId());
        if (zl == null) {
            // 添加指令信息
            zl = this.addTrsZlxx(zlVo, zl, gl);
            // 添加指令签收人表
            List<VwAlermYjzlUser> userList = zlVo.getUserList();
            this.addZlzxrList(userList, zl);
            // 添加指令人员子表
            this.addZlry(zlVo, zl);
        }
    }

    @Override
    public void addTrsYj(VwAlermYjgl vo) {
        TbYjxx yjxx = this.tbYjxxMapper.selectById(vo.getId());
        if (yjxx == null) {
            yjxx = this.addTrsYjxx(vo);
            this.addYjxxRy(yjxx);
        }
    }

    @Override
    public void addZlxx(TbZlxxInsertDto dto) {
        if (StringUtils.isEmpty(dto.getSfzh())) {
            throw new BusinessException("缺失指令发送人身份证号");
        }
        if (StringUtils.isNotBlank(dto.getSsbmid())) {
            DeptInfoVo deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
            if (deptInfoVo == null || StringUtils.isBlank(deptInfoVo.getDeptid())) {
                deptInfoVo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmid());
            }
            if (deptInfoVo == null) {
                throw new BusinessException("所属部门不存在");
            }
            dto.setSsbmid(deptInfoVo.getDeptid());
        }
        SysUser user = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(dto.getSfzh());
        if (user == null) {
            throw new BusinessException("用户不存在!");
        }
        TbZlxx zl = new TbZlxx();
        BeanUtils.copyProperties(dto, zl);
        zl.setZlly(zl.getZllx());
        if ("2".equals(zl.getZlzxlx()) && ("02".equals(zl.getZljsdx()) || ("03").equals(zl.getZljsdx()))) {
            throw new BusinessException("多人任务不允许发送给部门或巡组");
        }
        if (zl.getJd() != null && zl.getWd() != null) {
            zl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        zl.setZlfqrId(String.valueOf(user.getId()));
        zl.setZlfqrSfzh(user.getIdEntityCard());
        zl.setZlfqr(user.getUserName());

        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(String.valueOf(dto.getSsbmid()));
        if (StringUtils.isBlank(dept.getDeptid())) {
            dept = this.tbBaseAdaptRemoteService.getDeptInfoByUserId(String.valueOf(user.getId()));
        }
        if (dept != null) {
            zl.setSsbmdm(dept.getDeptcode());
            zl.setSsbm(dept.getDeptname());
            zl.setSsbmid(dept.getDeptid());
            zl.setSsxgaj(dept.getFxjname());
            zl.setSsxgajdm(dept.getFxjcode());
            zl.setSsxgajid(dept.getFxjid());
            zl.setSssgaj(dept.getDszname());
            zl.setSssgajdm(dept.getDszcode());
            zl.setSssgajid(dept.getDszid());
            zl.setZlfsddxzqh(StringUtils.isNotBlank(dept.getDeptcode()) ? dept.getDeptcode().substring(0, 6) : null);
        }
        zl.setXtSjly("1");
        zl.setId(UUIDGenerator.getUUID());
        // 计算指令接收人数
        // 签收有效时间
        String qs = this.tbBaseAdaptRemoteService.getValue("ZLQSYXSJ");
        int qsyxsj = qs == null ? 5 : Integer.parseInt(qs);
        zl.setQsyxsj(DateUtils.getNextDate(new Date(), "m", qsyxsj));
        // 到达有效时间
        String dd = this.tbBaseAdaptRemoteService.getValue("ZLDDXCYXSJ");
        int ddxcyxsj = dd == null ? 15 : Integer.parseInt(dd);
        zl.setDdxcyxsj(DateUtils.getNextDate(new Date(), "m", ddxcyxsj));
        // 完结有效时间
        String wj = this.tbBaseAdaptRemoteService.getValue("ZLCZWJSJ");
        int zlwjyxsj = wj == null ? 120 : Integer.parseInt(wj);
        zl.setZlwjyxsj(DateUtils.getNextDate(new Date(), "m", zlwjyxsj));
        this.totalZljsrs(dto, zl);
        zl.setZlfqrq(new Date());
        zl.setZlfqsj(new Date());
        if (StringUtils.isBlank(zl.getZlzxlx())) {
            zl.setZlzxlx("1");
        }
        zl.setQszt("01");
        zl.setZlwjzt("91");
        zl.setZlzxzt("01");
        zl.setZlzxqk(DateUtils.getQueryDateString(new Date(), "02") + "," + zl.getZlfqr() + "下发了该指令！！");
        this.baseMapper.insertEntity(zl);
        // 添加指令执行人和指令执行记录
        this.addZlxxZxrOther(zl, dto);
        if ("04".equals(zl.getZllx())) {
            this.addSjzl(zl);
        } else if ("02".equals(zl.getZllx())) {
            this.addRyZl(zl);
        } else if ("03".equals(zl.getZllx())) {
            this.addClZl(zl);
        } else if ("06".equals(zl.getZllx())) {
            this.addRgZl(zl, dto);
        }
    }

    @Override
    public void zlHandleOther(TbZlxxZtChangeDto dto) {
        if (StringUtils.isEmpty(dto.getSfzh())) {
            throw new BusinessException("处置人身份证号必须！");
        }
        SysUser user = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(dto.getSfzh());
        if (user == null) {
            throw new BusinessException("用户未找到！");
        }
        TbZlxx zl = this.tbZlxxMapper.selectById(dto.getZlId());
        if (zl == null) {
            throw new BusinessException("指令未找到！！");
        }
        TbZlxxZxr zxr = null;
        if ("01".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrId, String.valueOf(user.getId()))
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        } else if ("02".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
                            .eq(TbZlxxZxr::getSsbmid, dto.getSsbmid())
            );
        } else if ("03".equals(dto.getZljsdx())) {
            zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZxrXzid, dto.getZlzxrXzid())
                            .eq(TbZlxxZxr::getZlId, zl.getId())
            );
        }
        if (zxr == null) {
            throw new BusinessException("执行人未找到！！");
        }
        // 签收状态
        if ("01".equals(dto.getZlzxzt())) {
            this.changeQsztOther(dto, zl, user, zxr);
        } else if ("02".equals(dto.getZlzxzt())) {
            // 到达状态
            this.changeDdztOther(dto, zl, user, zxr);
        } else if ("03".equals(dto.getZlzxzt())) {
            // 处置完成
            this.changeCzztOther(dto, zl, user, zxr);
        } else if ("04".equals(dto.getZlzxzt())) {
            // 完结
            this.changeWjztOther(dto, zl, user, zxr);
        } else if ("08".equals(dto.getZlzxzt())) {
            // 转发
            this.changeZfztOther(dto, zl, user, zxr);
        }
    }

    @Override
    public IPage<TbZlxxVo> getMySendZlList(MyZlSearchDto dto) {
        if (StringUtils.isNotBlank(dto.getKssj())) {
            dto.setKssj(dto.getKssj() + " 00:00:00");
        }
        if (StringUtils.isNotBlank(dto.getJssj())) {
            dto.setJssj(dto.getJssj() + " 23:59:59");
        }
        UserInfo user = UserInfoManager.get();
        if (user == null) return null;
        List<String> ztList = new ArrayList<>();
        List<String> lxList = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getZlzxzt())) {
            ztList = Arrays.asList(dto.getZlzxzt().split(","));
        }
        if (StringUtils.isNotBlank(dto.getZllx())) {
            lxList = Arrays.asList(dto.getZllx().split(","));
        }
        IPage<TbZlxx> page = this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbZlxx>()
                        .like(StringUtils.isNotBlank(dto.getZlnr()), TbZlxx::getZlnr, dto.getZlnr())
                        .ge(StringUtils.isNotBlank(dto.getKssj()), TbZlxx::getZlfqsj, dto.getKssj())
                        .le(StringUtils.isNotBlank(dto.getJssj()), TbZlxx::getZlfqsj, dto.getJssj())
                        .in(!CollectionUtils.isEmpty(ztList), TbZlxx::getZlzxzt, ztList)
                        .in(!CollectionUtils.isEmpty(lxList), TbZlxx::getZllx, lxList)
                        .eq(TbZlxx::getZlfqrId, user.getUserId())
                        .orderByDesc(TbZlxx::getZlfqsj)
        );
        IPage<TbZlxxVo> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbZlxxVo> resList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbZlxxVo vo = new TbZlxxVo();
                BeanUtils.copyProperties(item, vo);
                resList.add(vo);
            });
        }
        resPage.setTotal(page.getTotal());
        resPage.setRecords(resList);
        return resPage;
    }

    @Override
    public void addZxjlOther(TbZlxxZxjlInsertDto dto) {
        if (StringUtils.isEmpty(dto.getSfzh())) {
            throw new BusinessException("身份证号必须！");
        }
        SysUser user = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(dto.getSfzh());
        if (user == null) {
            throw new BusinessException("用户未找到！");
        }
        TbZlxx zl = this.baseMapper.selectById(dto.getZlId());
        if (zl == null) {
            throw new BusinessException("没有找到当前指令信息！！");
        }
        TbZlxxZxjl zxjl = new TbZlxxZxjl();
        BeanUtils.copyProperties(dto, zxjl);
        zxjl.setId(UUIDGenerator.getUUID());
        zxjl.setZlId(zl.getId());
        zxjl.setZxsj(new Date());
        zxjl.setZlzxzt("05");
        if (zl.getJd() != null && zl.getWd() != null) {
            zxjl.setJd(zl.getJd());
            zxjl.setWd(zl.getWd());
            zxjl.setZb(JtsUtils.getPoint(zl.getJd(), zl.getWd()));
            zxjl.setZbhash(GeoHashKit.encode(zl.getJd(), zl.getWd()));
        }
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getDeptInfoByUserId(String.valueOf(user.getId()));
        zxjl.setSsbm(dept.getDeptname());
        zxjl.setSsbmid(dept.getDeptid());
        zxjl.setSsbmdm(dept.getDeptcode());
        zxjl.setSsxgajdm(dept.getFxjcode());
        zxjl.setSsxgajid(dept.getFxjid());
        zxjl.setSsxgaj(dept.getFxjname());
        zxjl.setSssgaj(dept.getDszname());
        zxjl.setSssgajdm(dept.getDszcode());
        zxjl.setSssgajid(dept.getDszid());
        zxjl.setZlzxrId(String.valueOf(user.getId()));
        zxjl.setZlzxrSfz(user.getIdEntityCard());
        zxjl.setZlzxrXm(user.getUserName());
        zxjl.setZlzxdd(zl.getZlfsdd());
        zxjl.setXtSjly("1");
        zxjl.setId(UUIDGenerator.getUUID());
        this.tbZlxxZxjlMapper.insertEntity(zxjl);
    }

    @Override
    public Integer getZlCount(String ssbmdm, String time, String zlzxzt) {
        List<String> zxztList = StringUtils.isNotBlank(zlzxzt) ? Arrays.asList(zlzxzt.split(",")) : null;
        try {
//             ssbmdm = this.tbBaseAdaptRemoteService.getSsbm(ssbmdm, null);
            return this.tbZlxxZxrMapper.selectCount(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .likeRight(!StringUtils.isEmpty(ssbmdm), TbZlxxZxr::getSsbmdm, ssbmdm)
                            .in(!CollectionUtils.isEmpty(zxztList), TbZlxxZxr::getDqzt, zxztList)
                            .eq(TbZlxxZxr::getZlfqrq, time)
            );
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    private TbYjxx addTrsYjxx(VwAlermYjgl gl) {
        TbYjxx item = new TbYjxx();
        item.setId(gl.getId());
        item.setYjLx("6");
        item.setYjLylx("01");
        item.setYjFsnf(Integer.valueOf(DateUtils.getQueryDateString(gl.getHdfssj(), "13")));
        item.setYjFsyf(Integer.valueOf(DateUtils.getQueryDateString(gl.getHdfssj(), "14")));
        item.setYjFsny(Integer.valueOf(DateUtils.getQueryDateString(gl.getHdfssj(), "15")));
        Calendar ca = Calendar.getInstance();
        ca.setTime(gl.getHdfssj());
        int yjfszs = ca.get(Calendar.WEEK_OF_YEAR);
        item.setYjFszs(yjfszs);
        item.setYjFsxs(Integer.valueOf(DateUtils.getQueryDateString(gl.getHdfssj(), "16")));
        item.setYjFsrq(gl.getHdfssj());
        item.setYjFssj(gl.getHdfssj());
        item.setYjSj(gl.getHdfssj());
        // 是否发送指令
        item.setSfFszl(StringUtils.isBlank(gl.getYjzlid()) ? "0" : "1");
        String yjdj;
        switch (gl.getYjlyjbDm()) {
            case "10":
                yjdj = "10";
                break;
            case "20":
                yjdj = "30";
                break;
            case "30":
                yjdj = "40";
                break;
            case "15":
                yjdj = "20";
                break;
            default:
                yjdj = "40";
                break;
        }
        item.setYjJb(yjdj);
        item.setYjBt("预警人员《" + gl.getGlxm() + "（" + gl.getGlsfzh() + "）》");
        item.setYjNr(gl.getHdxq());
        int yjcs = this.tbYjxxMapper.countBySfzhOrCph(gl.getGlsfzh(), null);
        int yjjrcs = this.tbYjxxMapper.countCurrentBySfzhOrCph(gl.getGlsfzh(), null);
        yjcs = yjcs + 1;
        yjjrcs = yjjrcs + 1;
        item.setYjCs(yjcs);
        item.setYjJrcs(yjjrcs);
        item.setJd(gl.getHdjd());
        item.setWd(gl.getHdwd());
//        if (item.getJd() != null && item.getWd() != null) {
//            item.setZb(JtsUtils.getPoint(item.getJd(), item.getWd()));
//            item.setZbhash(GeoHashKit.encode(item.getJd(), item.getWd()));
//        }
        item.setYjDzXzqh(StringUtils.isNotBlank(gl.getSqrdwdh()) ? gl.getSqrdwdh().substring(0, 6) : null);
        item.setYjDz(gl.getHdfsdd());
        item.setYjRyxm(gl.getGlxm());
        item.setYjRysfzh(gl.getGlsfzh());
        if ("2".equals(gl.getYjlyDm())) {
            item.setYjClcph(gl.getGlqthm());
        }
        this.saveYjbq(item, gl.getRyxl());
        item.setYjTp(gl.getFileids());
        item.setYjlyid("01");
        item.setYjlyBqmc("拓尔思重点人员库");
        if (StringUtils.isNotBlank(gl.getSqrdwdh())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(gl.getSqrdwdh());
            if (dept != null) {
                item.setSsbm(dept.getDeptname());
                item.setSsbmid(dept.getDeptid());
                item.setSsbmdm(dept.getDeptcode());
                item.setSsxgajdm(dept.getFxjcode());
                item.setSsxgajid(dept.getFxjid());
                item.setSsxgaj(dept.getFxjname());
                item.setSssgaj(dept.getDszname());
                item.setSssgajdm(dept.getDszcode());
                item.setSssgajid(dept.getDszid());
            }
        }
        item.setXtSjly("1");
        this.tbYjxxMapper.insertEntity(item);
        return item;
    }

    // 添加预警信息人员
    private void addYjxxRy(TbYjxx yjxx) {
        TbYjxxRy ryxx = new TbYjxxRy();
        ryxx.setId(UUIDGenerator.getUUID());
        ryxx.setYjId(yjxx.getId());
        ryxx.setYjClid("");
        ryxx.setYjGzyid(yjxx.getYjGzyid());
        ryxx.setYjGzymc(yjxx.getYjGzymc());
        ryxx.setYjFssj(yjxx.getYjFssj());
        ryxx.setYjSj(yjxx.getYjSj());
        ryxx.setYjJb(yjxx.getYjJb());
        ryxx.setYjBt(yjxx.getYjBt());
        ryxx.setYjNr(yjxx.getYjNr());
        ryxx.setYjDzxzqh(yjxx.getYjDzXzqh());
        ryxx.setYjlyBqmc(yjxx.getYjlyBqmc());
        ryxx.setYjZjlx("1");
        ryxx.setYjZjhm(yjxx.getYjRysfzh());
        ryxx.setYjRyxm(yjxx.getYjRyxm());
        ryxx.setYjRyxb(IDCardUtil.getSex(yjxx.getYjRysfzh()));
        ryxx.setYjRynl(IDCardUtil.getAge(yjxx.getYjRysfzh()));
        ryxx.setYjRymz("");
        ryxx.setYjRyjg(yjxx.getYjDzXzqh());
        ryxx.setYjRyhjdXzqh(yjxx.getYjDzXzqh());
        ryxx.setYjRyhjd("");
        ryxx.setYjDz(yjxx.getYjDz());
        ryxx.setTpId(yjxx.getYjTp());
        ryxx.setJd(yjxx.getJd());
        ryxx.setWd(yjxx.getWd());
//        if (ryxx.getJd() != null && ryxx.getWd() != null) {
//            ryxx.setZb(yjxx.getZb());
//            ryxx.setZbhash(yjxx.getZbhash());
//        }
        ryxx.setYjRybq(yjxx.getYjbq());
        ryxx.setYjRybqmc(yjxx.getYjbqmc());
        ryxx.setSsbm(yjxx.getSsbm());
        ryxx.setSsbmid(yjxx.getSsbmid());
        ryxx.setSsxgajid(yjxx.getSsxgajid());
        ryxx.setSssgajid(yjxx.getSssgajid());
        ryxx.setSsbmdm(yjxx.getSsbmdm());
        ryxx.setSsxgaj(yjxx.getSsxgaj());
        ryxx.setSsxgajdm(yjxx.getSsxgajdm());
        ryxx.setSssgaj(yjxx.getSssgaj());
        ryxx.setSssgajdm(yjxx.getSssgajdm());
        ryxx.setXtSjly("1");
        this.tbYjxxRyMapper.insertEntity(ryxx);
        saveTbYjxxRyBq(ryxx);
    }

    // 添加人员信息标签
    private void saveTbYjxxRyBq(TbYjxxRy ryxx) {
        TbYjxxRyBq rybq = new TbYjxxRyBq();
        rybq.setId(UUIDGenerator.getUUID());
        rybq.setYjryId(ryxx.getId());
        rybq.setRybqdm(ryxx.getYjRybq());
        rybq.setRybqmc(ryxx.getYjRybqmc());
        rybq.setXtCjsj(new Timestamp(System.currentTimeMillis()));
        rybq.setXtZhgxsj(new Timestamp(System.currentTimeMillis()));
        rybq.setXtScbz("0");
        rybq.setXtSjzt("1");
        rybq.setXtSjly("1");
        this.tbYjxxRyBqMapper.insert(rybq);
    }

    private void saveYjbq(TbYjxx yjxx, String name) {
        yjxx.setYjbqmc(name);
        if (name.contains("控")) {
            yjxx.setYjbq("1");
        } else if (name.contains("在逃")) {
            yjxx.setYjbq("2");
        } else if (name.contains("毒")) {
            yjxx.setYjbq("3");
        } else if (name.contains("前科") || name.contains("盗") || name.contains("抢")) {
            yjxx.setYjbq("4");
        } else if (name.contains("精神") || name.contains("肇事") || name.contains("肇祸")) {
            yjxx.setYjbq("5");
        } else if (name.contains("涉恐")) {
            yjxx.setYjbq("6");
        } else if (name.contains("信访") || name.contains("上访")) {
            yjxx.setYjbq("7");
        } else if (name.contains("稳")) {
            yjxx.setYjbq("8");
        } else {
            yjxx.setYjbq("9");
        }
    }

    // 添加指令接收人员表
    private void addZlzxrList(List<VwAlermYjzlUser> userList, TbZlxx zl) {
        // 添加指令执行记录
        List<TbZlxxZxr> zxrList = new ArrayList<>();
        userList.forEach(user -> {
            TbZlxxZxjl jl = new TbZlxxZxjl();
            jl.setId(UUIDGenerator.getUUID());
            jl.setZlId(zl.getId());
            jl.setZxsj(user.getSigntime());
            String zlzxzt = "";
            String time = DateUtils.getQueryDateString(user.getSigntime() == null ? new Date() : user.getSigntime(), "02");
            String zlzxnr = user.getUsername() + time;
            if ("0".equals(user.getStatusCode())) {
                zlzxzt = "00";
                zlzxnr += "接收到该指令！！";
            } else if ("1".equals(user.getStatusCode())) {
                zlzxzt = "01";
                zlzxnr += "签收了该指令";
            } else if ("2".equals(user.getStatusCode())) {
                zlzxzt = "02";
                zlzxnr += "签收了该指令,超时签收！";
            } else if ("3".equals(user.getStatusCode())) {
                zlzxzt = "05";
                zlzxnr += "下发了该指令";
            } else if ("4".equals(user.getStatusCode())) {
                zlzxzt = "04";
                zlzxnr += "对该指令进行了反馈！";
            } else if ("5".equals(user.getStatusCode())) {
                zlzxzt = "04";
                zlzxnr += "对该指令进行了反馈,反馈超时！！";
            }
            jl.setZxsj(DateUtils.strToDate(time, "02"));
            jl.setZlzxzt(zlzxzt);
            jl.setZxnr(zlzxnr);
            jl.setZlzxddXzqhdm(StringUtils.isNotBlank(user.getUnitcode()) ? user.getUnitcode().substring(0, 6) : null);
            SysUser sysUser = this.tbBaseAdaptRemoteService.getUserInfoBySfzh(user.getIdcard());
            if (sysUser != null) {
                jl.setZlzxrId(String.valueOf(sysUser.getId()));
                jl.setZlzxrDh(StringUtils.isNotBlank(sysUser.getTelePhone()) ? sysUser.getTelePhone() : sysUser.getMobile());
            }
            jl.setZlzxrXm(user.getUsername());
            jl.setZlzxrSfz(user.getIdcard());
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(user.getUnitcode());
            if (dept != null) {
                jl.setSsbmdm(dept.getDeptcode());
                jl.setSsbm(dept.getDeptname());
                jl.setSsbmid(dept.getDeptid());
                jl.setSsxgaj(dept.getFxjname());
                jl.setSsxgajdm(dept.getFxjcode());
                jl.setSsxgajid(dept.getFxjid());
                jl.setSssgaj(dept.getDszname());
                jl.setSssgajdm(dept.getDszcode());
                jl.setSssgajid(dept.getDszid());
            }
            jl.setSsbmdm(user.getUnitcode());
            jl.setSsbm(user.getUnitname());
            jl.setXtSjly("1");
            this.tbZlxxZxjlMapper.insert(jl);
            // 添加指令接收人（如果存在则修改）
            TbZlxxZxr zxr = this.tbZlxxZxrMapper.selectOne(
                    new LambdaQueryWrapper<TbZlxxZxr>()
                            .eq(TbZlxxZxr::getZlId, zl.getId())
                            .eq(TbZlxxZxr::getZxrSfz, user.getIdcard())
                            .last(" limit 1")
            );
            String dqzt = "";
            String dqztSm = user.getUsername() + time;
            Date daztSj = user.getSigntime() == null ? new Date() : user.getSigntime();
            if ("0".equals(user.getStatusCode())) {
                dqzt = "01";
                dqztSm += "接收到该指令！！";
            } else if ("1".equals(user.getStatusCode())) {
                dqzt = "02";
                dqztSm += "签收了该指令";
            } else if ("2".equals(user.getStatusCode())) {
                dqzt = "03";
                dqztSm += "签收了该指令,超时签收！";
            } else if ("3".equals(user.getStatusCode())) {
                dqzt = "05";
                dqztSm += "下发了该指令";
            } else if ("4".equals(user.getStatusCode())) {
                dqzt = "27";
                dqztSm += "对该指令进行了反馈！";
            } else if ("5".equals(user.getStatusCode())) {
                dqzt = "27";
                dqztSm += "对该指令进行了反馈,反馈超时！！";
            }
            if (zxr == null) {
                zxr = new TbZlxxZxr();
                BeanUtils.copyProperties(zl, zxr);
                zxr.setId(UUIDGenerator.getUUID());
                zxr.setZlId(zl.getId());
                zxr.setZxrLx("01");
                if (sysUser != null) {
                    zxr.setZxrId(String.valueOf(sysUser.getId()));
                    zxr.setZxrDh(StringUtils.isNotBlank(sysUser.getTelePhone()) ? sysUser.getTelePhone() : sysUser.getMobile());
                }
                zxr.setZxrXm(user.getUsername());
                zxr.setZxrSfz(user.getIdcard());
                zxr.setZxrJllx("01");
                zxr.setDqztSj(daztSj);
                zxr.setDqzt(dqzt);
                zxr.setDqztsm(dqztSm);
                if (dept != null) {
                    zxr.setSsbmdm(dept.getDeptcode());
                    zxr.setSsbm(dept.getDeptname());
                    zxr.setSsbmid(dept.getDeptid());
                    zxr.setSsxgaj(dept.getFxjname());
                    zxr.setSsxgajdm(dept.getFxjcode());
                    zxr.setSsxgajid(dept.getFxjid());
                    zxr.setSssgaj(dept.getDszname());
                    zxr.setSssgajdm(dept.getDszcode());
                    zxr.setSssgajid(dept.getDszid());
                }
                zxr.setSsbmdm(user.getUnitcode());
                zxr.setSsbm(user.getUnitname());
                zxr.setXtSjly("1");
                this.tbZlxxZxrMapper.insert(zxr);
            } else {
                zxr.setDqztSj(daztSj);
                zxr.setDqzt(dqzt);
                zxr.setDqztsm(dqztSm);
                this.tbZlxxZxrMapper.updateById(zxr);
            }
            zxrList.add(zxr);
        });
        this.addTask(zl, zxrList, null);
    }

    @Override
    public IPage<TbZlYjRyVo> getSyrkZlxx(VwSyrkJzgjGxListQuery dto) {
        String tempSfz = dto.getZjhm().substring(0, 6) + "********" + dto.getZjhm().substring(14);
        IPage<TbZlYjRy> page = this.tbZlYjRyMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbZlYjRy>()
                        .eq(TbZlYjRy::getXtSjzt, "1")
                        .and(
                                it -> it.eq(TbZlYjRy::getZlZjhm, dto.getZjhm()).or()
                                        .eq(TbZlYjRy::getZlZjhm, tempSfz)
                        )
                        .orderByDesc(TbZlYjRy::getYjSj)
        );
        IPage<TbZlYjRyVo> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbZlYjRyVo> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbZlYjRyVo vo = new TbZlYjRyVo();
                BeanUtils.copyProperties(item, vo);
                list.add(vo);
            });
        }
        resPage.setTotal(page.getTotal());
        resPage.setRecords(list);
        return resPage;
    }

    @Override
    public List<Map<String, Object>> getTjOfZllx(String type) {
        String kssj = "", jssj = "";
        if ("1".equals(type)) {
            kssj = DateUtils.getQueryDateString(new Date(), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
        } else if ("2".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
        } else if ("3".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
        } else if ("4".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -90), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
        }
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);

        return this.baseMapper.getTjOfZllx(kssj, jssj, ssbm);
    }

    @Override
    public TbZlxx getLastZlxx() {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbZlxx>()
                        .eq(TbZlxx::getZllx, "07")
                        .orderByDesc(TbZlxx::getZlfqsj)
                        .last(" limit 1")
        );
    }

    @Override
    public Map<String, Object> getStatisticsByStatus() {
        UserInfo user = UserInfoManager.get();
        Map<String, Object> map = new HashMap<>();
        map.put("all", this.baseMapper.getStatisticsByStatus(user.getIdEntityCard(), String.valueOf(user.getDeptId()), "1"));
        map.put("wwc", this.baseMapper.getStatisticsByStatus(user.getIdEntityCard(), String.valueOf(user.getDeptId()), "2"));
        return map;
    }

    // 添加人员指令子表
    private void addZlry(VwAlermYjzlVo yj, TbZlxx zl) {
        VwAlermYjgl gl = yj.getVwAlermYjgl();
        TbZlYjRy ry = new TbZlYjRy();
        BeanUtils.copyProperties(zl, ry);
        ry.setId(UUIDGenerator.getUUID());
        ry.setZlId(gl.getYjzlid());
        ry.setZldj(zl.getZldj());
        ry.setZlfsrq(zl.getZlfqrq());
        ry.setZlfssj(zl.getZlfqsj());
        ry.setZlBt(zl.getZlbt());
        ry.setZlNr(zl.getZlnr());
        ry.setZlRybqmc(gl.getRyxl());
        ry.setZlRybq(this.getRybqdm(gl.getRyxl()));
        ry.setZlZjlx("1");
        ry.setZlZjhm(gl.getGlsfzh());
        ry.setZlRyxm(gl.getGlxm());
        ry.setZlRyxb(IDCardUtil.getAge(gl.getGlsfzh()) + "");
        ry.setZlRynl(IDCardUtil.getSexInteger(gl.getGlsfzh()));
        ry.setZlRyjg(gl.getGlsfzh().substring(0, 6));
        ry.setZlRyhjdXzqh(gl.getGlsfzh().substring(0, 6));
        if (zl.getJd() != null && zl.getWd() != null) {
            ry.setJd(zl.getJd());
            ry.setWd(zl.getWd());
            ry.setZb(JtsUtils.getPoint(ry.getJd(), ry.getWd()));
            ry.setZbhash(GeoHashKit.encode(ry.getJd(), ry.getWd()));
        }
        ry.setYjDz(zl.getZlfsdd());
        ry.setTpId(zl.getFjId());
        ry.setCoverImage(zl.getZltp());
        this.tbZlYjRyMapper.insertEntity(ry);
    }

    private String getRybqdm(String name) {
        String bqdm;
        if (name.contains("控")) {
            bqdm = "1";
        } else if (name.contains("在逃")) {
            bqdm = "2";
        } else if (name.contains("毒")) {
            bqdm = "3";
        } else if (name.contains("前科") || name.contains("盗") || name.contains("抢")) {
            bqdm = "4";
        } else if (name.contains("精神") || name.contains("肇事") || name.contains("肇祸")) {
            bqdm = "5";
        } else if (name.contains("涉恐")) {
            bqdm = "6";
        } else if (name.contains("信访") || name.contains("上访")) {
            bqdm = "7";
        } else if (name.contains("稳")) {
            bqdm = "8";
        } else {
            bqdm = "9";
        }
        return bqdm;
    }

    @Override
    public Map<String, Integer> yjzlTj() {
        Map<String, Integer> zlMap = new HashMap();
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);
        //统计预警指令
        //进行中
        Integer jx = this.baseMapper.selectCount(new LambdaQueryWrapper<TbZlxx>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbZlxx::getSsbmdm, ssbm)
                .ge(TbZlxx::getZlfqrq, DateUtils.getSystemDateString())
                .notIn(TbZlxx::getZlzxzt, "01,32,91,92,93,94")
        );
        zlMap.put("jx", jx);
        //完成
        Integer wc = this.baseMapper.selectCount(new LambdaQueryWrapper<TbZlxx>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbZlxx::getSsbmdm, ssbm)
                .ge(TbZlxx::getZlfqrq, DateUtils.getSystemDateString())
                .in(TbZlxx::getZlzxzt, "32,91,92,93,94")
        );
        zlMap.put("wc", wc);
        //犯罪预测
        Integer fzyc = this.tbFzycMapper.selectCount(new LambdaQueryWrapper<TbFzyc>()
                .likeRight(StringUtils.isNotBlank(ssbm), TbFzyc::getSsbmdm, ssbm)
                .ge(TbFzyc::getRealDate, DateUtils.getShortCompactString(new Date()))
        );
        zlMap.put("fzyc", fzyc);
        return zlMap;
    }

    @Override
    public void andJqZl(String ssbmid, String jqid, String bbids, String zlxflx) {
        if (StringUtils.isEmpty(jqid) || StringUtils.isEmpty(bbids)) throw new BusinessException("警情id和报备id不能为空！");
        TbJqVo jq = this.tbSjzxAdaptRemoteService.getJqInfo(jqid);
        if (ObjectUtils.isNotEmpty(jq)) {
            List<String> list = Arrays.asList(bbids.split(","));
            TbZlxxInsertDto dto = new TbZlxxInsertDto();
            dto.setYwId(jqid);
            dto.setZllx("05");
            dto.setZlly("05");
            if (list.size() > 1) {
                dto.setZlzxlx("2");
            } else {
                dto.setZlzxlx("1");
            }
            dto.setZlbt(jq.getBjlxmc());
            dto.setZlnr(jq.getBjnr());
            switch (jq.getJqjb()) {
                case "01":
                    dto.setZldj("10");
                    break;
                case "02":
                    dto.setZldj("20");
                    break;
                case "03":
                    dto.setZldj("30");
                    break;
                default:
                    dto.setZldj("40");
                    break;
            }
            dto.setZljsdx("03");
            dto.setJd(jq.getJd());
            dto.setWd(jq.getWd());
            dto.setZlfsddxzqh(jq.getSfdzxzqh());
            dto.setZlfsdd(jq.getSfdz());
            List<TbZlxxZxrDtoInsertDto> insertDtoList = new ArrayList<>();
            for (String bbid : list) {
                TbZlxxZxrDtoInsertDto zxrDtoInsertDto = new TbZlxxZxrDtoInsertDto();
                zxrDtoInsertDto.setZxrLx("03");
                zxrDtoInsertDto.setZxrXzid(bbid);
                insertDtoList.add(zxrDtoInsertDto);
            }
            dto.setZxrDtoList(insertDtoList);
            dto.setSsbmid(ssbmid);
            dto.setZlxflx(zlxflx);
            this.addZl(dto);
        }


    }

    @Override
    public void andYjZl(String yjid, String bbids) {
        if (StringUtils.isEmpty(yjid) || StringUtils.isEmpty(bbids)) throw new BusinessException("预警id和报备id不能为空！");
        TbYjxx yjxx = this.tbYjxxMapper.selectById(yjid);
        if (ObjectUtils.isNotEmpty(yjxx)) {
            List<String> list = Arrays.asList(bbids.split(","));
            TbZlxxInsertDto dto = new TbZlxxInsertDto();
            dto.setYwId(yjid);

            if (StringUtils.isNotBlank(yjxx.getYjLx()) && yjxx.getYjLx().equals("2")) {
                dto.setZllx("03"); //车辆预警
                dto.setZlly("03");
            } else {
                dto.setZllx("02"); //车辆预警
                dto.setZlly("02");
            }
            if (list.size() > 1) {
                dto.setZlzxlx("2");
            } else {
                dto.setZlzxlx("1");
            }
            dto.setZlbt(yjxx.getYjBt());
            dto.setZlnr(yjxx.getYjNr());
            dto.setZldj(yjxx.getYjJb());
            dto.setZljsdx("03");
            dto.setJd(yjxx.getJd());
            dto.setWd(yjxx.getWd());
            dto.setZlfsddxzqh(yjxx.getYjDzXzqh());
            dto.setZlfsdd(yjxx.getYjDz());
            dto.setZltp(yjxx.getYjTp());
            List<TbZlxxZxrDtoInsertDto> insertDtoList = new ArrayList<>();
            for (String bbid : list) {
                TbZlxxZxrDtoInsertDto zxrDtoInsertDto = new TbZlxxZxrDtoInsertDto();
                zxrDtoInsertDto.setZxrLx("03");
                zxrDtoInsertDto.setZxrXzid(bbid);
                insertDtoList.add(zxrDtoInsertDto);
            }
            dto.setZxrDtoList(insertDtoList);
            this.addZl(dto);
        }

    }


}
