package com.ozs.controller.upload;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.core.domain.entity.SysDictData;
import com.ozs.common.core.redis.RedisCache;
import com.ozs.common.utils.ApiTokenUtils;
import com.ozs.common.utils.ChineseToPingyin;
import com.ozs.common.utils.HeartbeatUtils;
import com.ozs.common.utils.HttpClientUtil;
import com.ozs.common.utils.IgnoreUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.stateSecrets.SM4Utils;
import com.ozs.config.Constant;
import com.ozs.entity.BaseCameraManagement;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.entity.BaseTerminal;
import com.ozs.entity.BaseUser;
import com.ozs.entity.BaseVehicleTerminal;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.MsgAlarmExt;
import com.ozs.entity.MsgAlarmFrequency;
import com.ozs.entity.MsgHeartbeatAlarmMessage;
import com.ozs.entity.SkynetHeartbeatLog;
import com.ozs.entity.SvcAddress;
import com.ozs.entity.TerminalHeartbeatLog;
import com.ozs.entity.TerminalIgnoreAlarm;
import com.ozs.entity.VehiclePosition;
import com.ozs.entity.vo.AlarmFilesVo;
import com.ozs.entity.vo.AlarmHeartbeatLog;
import com.ozs.entity.vo.AlarmInfoVo;
import com.ozs.entity.vo.AlarmListVo;
import com.ozs.entity.vo.CameraPlayVo;
import com.ozs.entity.vo.CameraVo;
import com.ozs.entity.vo.CamerasVo;
import com.ozs.entity.vo.Files;
import com.ozs.entity.vo.HeartbeatVo;
import com.ozs.entity.vo.ParameterVo;
import com.ozs.entity.vo.QueryAlarmVo;
import com.ozs.entity.vo.ReleaseAlarmVo;
import com.ozs.entity.vo.ReqMsgAlarmVo;
import com.ozs.entity.vo.RespAlarmList;
import com.ozs.entity.vo.RespCameraVo;
import com.ozs.entity.vo.RespGeoHazardMonitorVo;
import com.ozs.entity.vo.RespHeartbeatVo;
import com.ozs.entity.vo.SkynetHeartbeatVo;
import com.ozs.entity.vo.TerminalIgnoreVo;
import com.ozs.mapper.AlarmHeartbeatLogMapper;
import com.ozs.mapper.MsgHeartbeatAlarmMessageMapper;
import com.ozs.mapper.SkynetHeartbeatLogMapper;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
import com.ozs.service.BaseTerminalService;
import com.ozs.service.BaseUserService;
import com.ozs.service.BaseVehicleTerminalService;
import com.ozs.service.MsgAlarmExtService;
import com.ozs.service.MsgAlarmFrequencyService;
import com.ozs.service.MsgAlarmService;
import com.ozs.service.SvcAddressService;
import com.ozs.service.TerminalHeartbeatLogService;
import com.ozs.service.TerminalIgnoreAlarmService;
import com.ozs.service.VehiclePositionService;
import com.ozs.system.service.ISysDictDataService;
import com.ozs.system.service.ISysDictTypeService;
import com.ozs.utils.CameraUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * 1. 获取身份认证控制层
 *
 * @author Administrator
 */
@RestController
@Slf4j
public class GeoHazardMonitorTokenController {
    public static final String PATTERN = "^([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)$";
    public static final String PATTERNS = "^[0-9]*$";

    public static final String KEY = "58792429c08c43e9";
    @Resource
    private ApiTokenUtils apiTokenUtils;
    @Autowired
    MsgAlarmService msgAlarmService;
    @Autowired
    MsgAlarmFrequencyService msgAlarmFrequencyService;
    @Autowired
    SvcAddressService svcAddressService;
    @Autowired
    BaseCameraManagementService baseCameraManagementService;
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private MsgAlarmExtService msgAlarmExtService;
    @Autowired
    private BaseVehicleTerminalService baseVehicleTerminalService;
    @Autowired
    private VehiclePositionService vehiclePositionService;
    @Autowired
    private HeartbeatUtils heartbeatUtils;
    @Autowired
    private TerminalIgnoreAlarmService terminalIgnoreAlarmService;
    @Autowired
    private IgnoreUtils ignoreUtils;
    @Autowired
    private BaseRailwayManagementService baseRailwayManagementService;
    @Autowired
    private BaseTerminalService baseTerminalService;
    @Autowired
    private CameraUtil cameraUtil;
    @Resource
    private SkynetHeartbeatLogMapper synetHeartbeatLogMapper;
    @Resource
    private AlarmHeartbeatLogMapper alarmHeartbeatLogMapper;
    @Resource
    private MsgHeartbeatAlarmMessageMapper msgHearbeatAlarmMessageMapper;
    @Value("${base.env}")
    private String env;
    @Value("${sdk.publish}")
    private String sdkUrl;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Resource
    private TerminalHeartbeatLogService terminalHeartbeatLogService;

    /**
     * 获取web访问令牌
     *
     * @return
     */
    @PostMapping("/api/token")
    public String getWebToken(@RequestBody SvcAddress svcAddress) {
        // 生成令牌
        JSONObject jsonObject = new JSONObject();
        LambdaQueryWrapper<SvcAddress> lw = new LambdaQueryWrapper<SvcAddress>();
        if (!ObjectUtils.isEmpty(svcAddress.getClientId())) {
            lw.eq(SvcAddress::getClientId, svcAddress.getClientId());
            lw.eq(SvcAddress::getEnv, env);
        } else {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "客户端编号不能为空");
            return JSONObject.toJSONString(jsonObject);
        }
        SvcAddress serviceOne = svcAddressService.getOne(lw);
        if (!ObjectUtils.isEmpty(serviceOne)) {
            String clientSecret = SM4Utils.decryptData_ECB(serviceOne.getClientSecret(), KEY);
            String sm4 = SM4Utils.decryptData_ECB(svcAddress.getClientSecret(), clientSecret);
            if (clientSecret.equals(sm4)) {
                if (!ObjectUtils.isEmpty(svcAddress.getGrantType())) {
                    if (!svcAddress.getGrantType().toString().matches(PATTERNS)) {
                        jsonObject.put("resultCode", 0);
                        jsonObject.put("message", "失败");
                        jsonObject.put("data", "授权类型填写错误");
                        return JSONObject.toJSONString(jsonObject);
                    }
                } else {
                    jsonObject.put("resultCode", 0);
                    jsonObject.put("message", "失败");
                    jsonObject.put("data", "授权类型不能为空");
                    return JSONObject.toJSONString(jsonObject);
                }
                ArrayList<String> objects = apiTokenUtils.createGeoHazardMonitorToken(svcAddress.getGrantType().toString(), svcAddress.getClientId(), sm4);
                if (objects.size() > 0) {
                    RespGeoHazardMonitorVo respGeoHazardMonitorVo = new RespGeoHazardMonitorVo();
                    respGeoHazardMonitorVo.setAccessToken(objects.get(1));
                    Long l = Long.valueOf(objects.get(0));
                    respGeoHazardMonitorVo.setExpiresIn(l / 1000);
                    respGeoHazardMonitorVo.setTokenType("令牌类型");
                    jsonObject.put("resultCode", 1);
                    jsonObject.put("message", "ok");
                    jsonObject.put("data", respGeoHazardMonitorVo);
                    return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), sm4);
                } else {
                    jsonObject.put("resultCode", 0);
                    jsonObject.put("message", "失败");
                    jsonObject.put("data", "生成token失败");
                    return JSONObject.toJSONString(jsonObject);
                }
            } else {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "客户端密钥错误");
                return JSONObject.toJSONString(jsonObject);
            }
        } else {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "参数验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 报警信息数据传输
     */
    @PostMapping("/api/alarm")
    @Transactional(rollbackFor = Exception.class)
    public String alarm(@RequestBody ParameterVo parameterVo, HttpServletRequest request) throws Exception {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            ReqMsgAlarmVo reqMsgAlarmVo = JSON.parseObject(s, ReqMsgAlarmVo.class);
            if (ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmAttr())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "你填写的报警病害属性不符合规则!!!");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (!reqMsgAlarmVo.getAlarmMile().toString().matches(PATTERNS)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警里程位置填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (reqMsgAlarmVo.getLineDir() != 1 && reqMsgAlarmVo.getLineDir() != 2) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "行别填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<BaseRailwayManagement> queryWrapper = new LambdaQueryWrapper<BaseRailwayManagement>();
            if (!ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmRailway())) {
                queryWrapper.eq(BaseRailwayManagement::getRailwayCode, reqMsgAlarmVo.getAlarmRailway());
            }
            BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
            if (ObjectUtils.isEmpty(baseRailwayManagement)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "填写的报警线路编码不存在");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmId())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警的唯一标识不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmTime())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警时间不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (reqMsgAlarmVo.getAlarmType() < 1 || reqMsgAlarmVo.getAlarmType() > 3) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警病害类型填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (!reqMsgAlarmVo.getAlarmLevel().toString().matches(PATTERNS)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警级别填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (reqMsgAlarmVo.getAlarmTime().toString().length() == 10) {
                reqMsgAlarmVo.setAlarmTime(reqMsgAlarmVo.getAlarmTime() * 1000);
            }
            log.info("reqMsgAlarmVo:{}", reqMsgAlarmVo);

            LambdaQueryWrapper<BaseCameraManagement> wrapper = new LambdaQueryWrapper<BaseCameraManagement>();
            if (!ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmMile())) {
                wrapper.le(BaseCameraManagement::getBeginMile, reqMsgAlarmVo.getAlarmMile());
                wrapper.ge(BaseCameraManagement::getEndMile, reqMsgAlarmVo.getAlarmMile());
                wrapper.eq(BaseCameraManagement::getRailwayCode, reqMsgAlarmVo.getAlarmRailway());
                wrapper.eq(BaseCameraManagement::getEnableOrNot, 1);
            }
            List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementService.list(wrapper);
            log.info("baseCameraManagement:{}", baseCameraManagementList);
            if (baseCameraManagementList.size() <= 0) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警里程位置没有可用的相机!!!");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<MsgAlarm> lw = new LambdaQueryWrapper<MsgAlarm>();
            if (!ObjectUtils.isEmpty(reqMsgAlarmVo.getAlarmId())) {
                lw.eq(MsgAlarm::getAlarmId, reqMsgAlarmVo.getAlarmId());
            }
            MsgAlarm alarm = msgAlarmService.getOne(lw);
            BaseUser admin = baseUserService.getUserName("admin");
            MsgAlarmFrequency msgAlarmVice = new MsgAlarmFrequency();
            if (ObjectUtils.isEmpty(alarm)) {
                MsgAlarm msgAlarm = new MsgAlarm();
                BeanUtils.copyProperties(reqMsgAlarmVo, msgAlarm);
                BeanUtils.copyProperties(reqMsgAlarmVo, msgAlarmVice);
                msgAlarm.setCreateBy(admin.getUserId());
                msgAlarm.setUpdateBy(admin.getUserId());
                log.info("时间++++ long ---- " + reqMsgAlarmVo.getAlarmTime());
                log.info("时间++++" + new Date(reqMsgAlarmVo.getAlarmTime()));
                msgAlarm.setAlarmTime(new Date(reqMsgAlarmVo.getAlarmTime()));
                log.info("时间++++get" + msgAlarm.getAlarmTime());
                msgAlarm.setRailwayCode(reqMsgAlarmVo.getAlarmRailway());
                String source = split[split.length - 2];
                msgAlarm.setSource(source);
                msgAlarmService.save(msgAlarm);
                Files[] files1 = reqMsgAlarmVo.getFiles();
                if (files1.length > 0) {
                    for (int i = 0; i < files1.length; i++) {
                        if (ObjectUtils.isEmpty(files1[i].fileUrl)) {
                            jsonObject.put("resultCode", 0);
                            jsonObject.put("message", "失败");
                            jsonObject.put("data", "上传文件地址不能为空");
                            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                        }
                        if (files1[i].fileType < 1 || files1[i].fileType > 3) {
                            jsonObject.put("resultCode", 0);
                            jsonObject.put("message", "失败");
                            jsonObject.put("data", "文件类型填写错误");
                            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                        }
                        MsgAlarmExt msgAlarmExt = new MsgAlarmExt();
                        msgAlarmExt.setAlarmId(msgAlarm.getAlarmId());
                        msgAlarmExt.setAlarmAttPath(files1[i].fileUrl);
                        msgAlarmExt.setAlarmAttType(files1[i].fileType);
                        msgAlarmExtService.save(msgAlarmExt);
                    }
                }
                msgAlarmVice.setCreateBy(admin.getUserId());
                msgAlarmVice.setUpdateBy(admin.getUserId());
                msgAlarmVice.setRailwayCode(reqMsgAlarmVo.getAlarmRailway());
                msgAlarmVice.setAlarmTime(new Date(reqMsgAlarmVo.getAlarmTime()));
                msgAlarmFrequencyService.save(msgAlarmVice);

                log.info("alarmJSON ：{}", JSON.toJSONString(msgAlarm));
                log.info("->>>>>>>>>>>>>>>>>>>>>>>>>>" + msgAlarm);
                String result = HttpClientUtil.postJson(sdkUrl, JSON.toJSONString(msgAlarm));
                log.info("->>>>>>>>>>>>>>>>>>>>>>>>>>>>" + result);
                log.info("result：{}", result);

                List<String> collect = baseCameraManagementList.stream().map(BaseCameraManagement::getCameraCode).collect(Collectors.toList());
                List<SysDictData> data = dictTypeService.selectDictDataByType("alarm_play_time_value");
                CompletableFuture.runAsync(() -> cameraUtil.playbackURLs(collect, data.get(0).getDictValue()));
            } else {
                BeanUtils.copyProperties(reqMsgAlarmVo, msgAlarmVice);
                msgAlarmVice.setCreateBy(admin.getUserId());
                msgAlarmVice.setUpdateBy(admin.getUserId());
                msgAlarmVice.setAlarmTime(new Date(reqMsgAlarmVo.getAlarmTime()));
                msgAlarmVice.setRailwayCode(reqMsgAlarmVo.getAlarmRailway());
                msgAlarmFrequencyService.save(msgAlarmVice);
            }
            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            jsonObject.put("data", null);
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 报警解除接口
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/releaseAlarm")
    public String releaseAlarm(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            ReleaseAlarmVo releaseAlarmVo = JSON.parseObject(s, ReleaseAlarmVo.class);
            if (ObjectUtils.isEmpty(releaseAlarmVo.getAlarmId())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警的唯一标识不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(releaseAlarmVo.getReleaseTime())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "解除报警时间不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(releaseAlarmVo.getReleaseContent())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "解除原因不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(releaseAlarmVo.getReleaseTime())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "解除人不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<MsgAlarm> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MsgAlarm::getAlarmId, releaseAlarmVo.getAlarmId());
            MsgAlarm msgAlarm = msgAlarmService.getOne(wrapper);
            if (ObjectUtils.isEmpty(msgAlarm)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "不存在该报警的唯一标识的数据");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (releaseAlarmVo.getReleaseTime().toString().length() == 10) {
                releaseAlarmVo.setReleaseTime(releaseAlarmVo.getReleaseTime() * 1000);
            }
            msgAlarm.setReleasedTime(new Date(releaseAlarmVo.getReleaseTime()));
            msgAlarm.setReleaseContent(releaseAlarmVo.getReleaseContent());
            msgAlarm.setReleasedBy(releaseAlarmVo.getOperator());
            msgAlarm.setUpdateBy(releaseAlarmVo.getOperator());
            msgAlarm.setIsRelease(1);
            boolean count = msgAlarmService.updateById(msgAlarm);
            Map<String, Object> map = new HashMap<>();
            map.put("alarmId", msgAlarm.getAlarmId());
            if (count) {
                map.put("releaseState", 1);
                jsonObject.put("resultCode", 1);
                jsonObject.put("message", "ok");
                jsonObject.put("data", map);
            } else {
                map.put("releaseState", 2);
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", map);
            }
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 车载心跳机制交互
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/terminal/heartbeat")
    @Transactional
    public String heartbeat(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        long begin = System.currentTimeMillis();
        Map<String, Object> twoMap = new HashMap<>();
        int distance = 0;
        boolean type = true;
        RespHeartbeatVo respHeartbeatVo = new RespHeartbeatVo();
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            //  1.  解密参数
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            //  2、 心跳参数
            HeartbeatVo heartbeatVo = JSON.parseObject(s, HeartbeatVo.class);
            if (ObjectUtils.isEmpty(heartbeatVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (!heartbeatVo.getCurrentMile().toString().matches(PATTERNS)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "当前里程位置填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(heartbeatVo.getLatitude())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "当前所在纬度不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(heartbeatVo.getLongitude())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "当前所在经度不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<BaseRailwayManagement> railway = new LambdaQueryWrapper<BaseRailwayManagement>();
            if (!ObjectUtils.isEmpty(heartbeatVo.getRailwayCode())) {
                railway.eq(BaseRailwayManagement::getRailwayCode, heartbeatVo.getRailwayCode());
            }
            BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(railway);
            if (ObjectUtils.isEmpty(baseRailwayManagement)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "填写的线路编码不存在");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (heartbeatVo.getLineDir() != 1 && heartbeatVo.getLineDir() != 2) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "行别填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (!heartbeatVo.getVisualDistance().toString().matches(PATTERNS)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "司机目视距离填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (heartbeatVo.getSignalStrength() != 1 && heartbeatVo.getSignalStrength() != 2) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "信号强度填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            VehiclePosition vehiclePosition = new VehiclePosition();
            BeanUtils.copyProperties(heartbeatVo, vehiclePosition);
            LambdaQueryWrapper<BaseVehicleTerminal> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseVehicleTerminal::getTerminalCode, vehiclePosition.getTerminalCode());
            BaseVehicleTerminal baseTerminal = baseVehicleTerminalService.getOne(wrapper);
            if (ObjectUtils.isEmpty(baseTerminal)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "关联表中没有改终端编码的信息");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (heartbeatVo.getSignalStrength() == 2) {
                type = false;
            }
            vehiclePosition.setTravelMile(heartbeatVo.getCurrentMile());
            vehiclePosition.setVehicleCode(baseTerminal.getVehicleCode());
            vehiclePosition.setTerminalLatitude(heartbeatVo.getLatitude());
            vehiclePosition.setTerminalLongitude(heartbeatVo.getLongitude());
            vehiclePosition.setCreateTime(new Date());
            vehiclePosition.setCurrenTime(vehiclePosition.getCreateTime());
            //机车行驶表添加数据
            boolean save = vehiclePositionService.save(vehiclePosition);
            //修改车载终端经度纬度
            BaseTerminal baseTerminal1 = new BaseTerminal();
            baseTerminal1.setTerminalLatitude(heartbeatVo.getLatitude());
            baseTerminal1.setTerminalLongitude(heartbeatVo.getLongitude());
            baseTerminal1.setStatus(1);
            LambdaQueryWrapper<BaseTerminal> terminal = new LambdaQueryWrapper<>();
            terminal.eq(BaseTerminal::getTerminalCode, vehiclePosition.getTerminalCode());
            boolean update = baseTerminalService.update(baseTerminal1, terminal);
            //获取心跳接口相机记录表信息
            List<TerminalHeartbeatLog> heartbeatLogList = terminalHeartbeatLogService.list(new LambdaQueryWrapper<TerminalHeartbeatLog>()
                    .orderByDesc(TerminalHeartbeatLog::getCreateTime)
                    .last("limit 2"));
            List<String> codeList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(heartbeatLogList)) {
                codeList = heartbeatLogList.stream().map(TerminalHeartbeatLog::getCameraCode).collect(Collectors.toList());
            }

            //目视距离和里程位置
            if (save && update) {
                LambdaQueryWrapper<MsgAlarm> queryWrapper = new LambdaQueryWrapper<>();
                if (heartbeatVo.getLineDir() == 1) {
                    queryWrapper.ge(MsgAlarm::getAlarmMile, heartbeatVo.getCurrentMile());
                    queryWrapper.le(MsgAlarm::getAlarmMile, (heartbeatVo.getCurrentMile() + 10000));
                    queryWrapper.orderByAsc(MsgAlarm::getAlarmMile);
                } else if (heartbeatVo.getLineDir() == 2) {
                    queryWrapper.le(MsgAlarm::getAlarmMile, heartbeatVo.getCurrentMile());
                    queryWrapper.ge(MsgAlarm::getAlarmMile, (heartbeatVo.getCurrentMile() - 10000));
                    queryWrapper.orderByDesc(MsgAlarm::getAlarmMile);
                }
                queryWrapper.eq(MsgAlarm::getRailwayCode, heartbeatVo.getRailwayCode());
                queryWrapper.eq(MsgAlarm::getIsRelease, 2);
                List<MsgAlarm> alarmsList = msgAlarmService.list(queryWrapper);
                //根据终端编码 判断终端忽略报警信息记录表中的是否有忽略的报警
                if (!ObjectUtils.isEmpty(alarmsList)) {
                    // 获取当前时间
                    LocalDateTime currentDateTime = LocalDateTime.now();
                    // 获取前一天的时间
                    LocalDateTime previousDateTime = currentDateTime.minusDays(1);
                    // 格式化日期时间
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    String currentFormattedDateTime = currentDateTime.format(formatter);
                    String previousFormattedDateTime = previousDateTime.format(formatter);

                    LambdaQueryWrapper<TerminalIgnoreAlarm> queryWrapper1 = new LambdaQueryWrapper<>();
                    //报警信息表中报警标识的信息
                    List<String> collect = alarmsList.stream().map(MsgAlarm::getAlarmId).collect(Collectors.toList());
                    queryWrapper1.in(TerminalIgnoreAlarm::getAlarmId, collect);
                    queryWrapper1.eq(TerminalIgnoreAlarm::getTerminalCode, heartbeatVo.getTerminalCode());
                    queryWrapper1.ge(TerminalIgnoreAlarm::getIgnoreTime, previousFormattedDateTime);
                    queryWrapper1.le(TerminalIgnoreAlarm::getIgnoreTime, currentFormattedDateTime);
                    //终端忽略报警信息记录表
                    List<TerminalIgnoreAlarm> list2 = terminalIgnoreAlarmService.list(queryWrapper1);
                    //找出未忽略的报警信息
                    LambdaQueryWrapper<MsgAlarm> queryWrapper2 = new LambdaQueryWrapper<>();
                    if (!ObjectUtils.isEmpty(list2)) {
                        //忽略报警表中报警标识的信息
                        List<String> collect1 = list2.stream().map(TerminalIgnoreAlarm::getAlarmId).collect(Collectors.toList());
                        List<String> list = collect.stream()
                                .filter(item -> !collect1.contains(item))
                                .collect(Collectors.toList());
                        if (!ObjectUtils.isEmpty(list)) {
                            queryWrapper2.in(MsgAlarm::getAlarmId, list);
                        } else {
                            queryWrapper2.eq(MsgAlarm::getAlarmId, "1111111111111");
                        }
                    } else {
                        queryWrapper2.in(MsgAlarm::getAlarmId, collect);
                    }
                    List<MsgAlarm> list = msgAlarmService.list(queryWrapper2);
                    //有报警信息的时候
                    if (!ObjectUtils.isEmpty(list)) {
                        //获取报警里程所监控的所有相机
                        LambdaQueryWrapper<BaseCameraManagement> camera = new LambdaQueryWrapper<BaseCameraManagement>();
                        if (heartbeatVo.getLineDir() == 1) {
                            camera.ge(BaseCameraManagement::getEndMile, list.get(0).getAlarmMile());
                            camera.le(BaseCameraManagement::getBeginMile, list.get(0).getAlarmMile());
                            camera.in(BaseCameraManagement::getMonitoringDirection, 1, 3);
                            camera.orderByAsc(BaseCameraManagement::getInstallMile);
                        }
                        if (heartbeatVo.getLineDir() == 2) {
                            camera.le(BaseCameraManagement::getEndMile, list.get(0).getAlarmMile());
                            camera.ge(BaseCameraManagement::getBeginMile, list.get(0).getAlarmMile());
                            camera.in(BaseCameraManagement::getMonitoringDirection, 2, 3);
                            camera.orderByDesc(BaseCameraManagement::getInstallMile);
                        }
                        camera.eq(BaseCameraManagement::getRailwayCode, heartbeatVo.getRailwayCode());
                        camera.eq(BaseCameraManagement::getOnline, 2);
                        camera.eq(BaseCameraManagement::getEnableOrNot, 1);
                        List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementService.list(camera);
                        List<AlarmListVo> listVos = new ArrayList<>();
                        if (baseCameraManagementList.size() > 0) {
                            String playFlv = cameraUtil.heartbeatgetUrl(baseCameraManagementList.get(0).getCameraCode(), baseCameraManagementList.get(0).getChannel(), type);
                            //判断心跳相机日志表后两条数据是否包含该相机编码
                            if (!ObjectUtils.isEmpty(codeList)) {
                                if (!codeList.contains(baseCameraManagementList.get(0).getCameraCode())) {
                                    //如果不包含此相机编码
                                    for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                        cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                    }
                                }
                            }
                            //添加心跳相机日志信息
                            terminalHeartbeatLogService.add(baseCameraManagementList.get(0).getCameraCode(), baseCameraManagementList.get(0).getChannel());
                            respHeartbeatVo.setCurrentStream(playFlv);
                            respHeartbeatVo.setIsAlarm(1);
                            respHeartbeatVo.setCameraCode(baseCameraManagementList.get(0).getCameraCode());
                            twoMap.put("currentStream", playFlv);
                            twoMap.put("cameraCode", baseCameraManagementList.get(0).getCameraCode());
                            //alarmInfo对象
                            AlarmInfoVo alarmInfoVo = new AlarmInfoVo();
                            alarmInfoVo.setAlarmId(list.get(0).getAlarmId());
                            alarmInfoVo.setAlarmMile(list.get(0).getAlarmMile());
                            alarmInfoVo.setAlarmTime(list.get(0).getAlarmTime().getTime());
                            alarmInfoVo.setAlarmContent(list.get(0).getContent());
                            alarmInfoVo.setAlarmType(list.get(0).getAlarmType());
                            alarmInfoVo.setAlarmLevel(list.get(0).getAlarmLevel());
                            LambdaQueryWrapper<MsgAlarmExt> ext = new LambdaQueryWrapper<>();
                            ext.eq(MsgAlarmExt::getAlarmId, list.get(0).getAlarmId());
                            List<MsgAlarmExt> list1 = msgAlarmExtService.list(ext);
                            AlarmFilesVo[] files = new AlarmFilesVo[list1.size()];
                            if (!ObjectUtils.isEmpty(list1)) {
                                for (int i = 0; i < list1.size(); i++) {
                                    files[i] = new AlarmFilesVo();
                                    files[i].setAlarmAttType(list1.get(i).getAlarmAttType());
                                    files[i].setAlarmAttPath(list1.get(i).getAlarmAttPath());
                                }
                                //返回结果
                                alarmInfoVo.setAlarmFiles(files);
                            }
                            respHeartbeatVo.setAlarmInfo(alarmInfoVo);
                        } else {
                            jsonObject.put("resultCode", 0);
                            jsonObject.put("message", "失败");
                            jsonObject.put("data", "报警里程无可用相机");
                            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                        }
                        for (int i = 0; i < list.size(); i++) {
                            MsgHeartbeatAlarmMessage msgHearbeatAlarmMessage = new MsgHeartbeatAlarmMessage();
                            msgHearbeatAlarmMessage.setAlarmId(list.get(i).getAlarmId());
                            msgHearbeatAlarmMessage.setTerminalCode(heartbeatVo.getTerminalCode());
                            msgHearbeatAlarmMessage.setTrainNum(heartbeatVo.getTrainNum());
                            msgHearbeatAlarmMessageMapper.insert(msgHearbeatAlarmMessage);
                            //获取剩余的报警信息
                            LambdaQueryWrapper<BaseCameraManagement> ment = new LambdaQueryWrapper<BaseCameraManagement>();
                            ment.eq(BaseCameraManagement::getRailwayCode, heartbeatVo.getRailwayCode());
                            ment.eq(BaseCameraManagement::getOnline, 2);
                            if (heartbeatVo.getLineDir() == 1) {
                                ment.ge(BaseCameraManagement::getEndMile, list.get(i).getAlarmMile());
                                ment.le(BaseCameraManagement::getBeginMile, list.get(i).getAlarmMile());
                                ment.in(BaseCameraManagement::getMonitoringDirection, 1, 3);
                                ment.orderByAsc(BaseCameraManagement::getInstallMile);
                            } else if (heartbeatVo.getLineDir() == 2) {
                                ment.le(BaseCameraManagement::getEndMile, list.get(i).getAlarmMile());
                                ment.ge(BaseCameraManagement::getBeginMile, list.get(i).getAlarmMile());
                                ment.in(BaseCameraManagement::getMonitoringDirection, 2, 3);
                                ment.orderByDesc(BaseCameraManagement::getInstallMile);
                            }
                            if (i == 0) {
                                ment.last("limit 1,1000000");
                            }
                            //获取所有报警安装里程位置的相机
                            List<BaseCameraManagement> baseList = baseCameraManagementService.list(ment);
                            if (baseList.size() > 0) {
                                for (int o = 0; o < baseList.size(); o++) {
                                    //判断流媒体是否有流
                                    String playFlv = cameraUtil.heartbeatgetUrl(baseList.get(o).getCameraCode(), baseList.get(o).getChannel(), type);
                                    //判断心跳相机日志表后两条数据是否包含该相机编码
                                    if (!ObjectUtils.isEmpty(codeList)) {
                                        if (!codeList.contains(baseList.get(o).getCameraCode())) {
                                            //如果不包含此相机编码
                                            for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                                cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                            }
                                        }
                                    }
                                    //添加心跳相机日志信息
                                    terminalHeartbeatLogService.add(baseList.get(o).getCameraCode(), baseList.get(o).getChannel());
                                    AlarmListVo alarmListVo = new AlarmListVo();
                                    //alarmList对象
                                    alarmListVo.setCameraStream(playFlv);
                                    alarmListVo.setCameraCode(baseList.get(o).getCameraCode());
                                    //alarmInfo对象
                                    AlarmInfoVo alarmInfoVo1 = new AlarmInfoVo();
                                    alarmInfoVo1.setAlarmId(list.get(i).getAlarmId());
                                    alarmInfoVo1.setAlarmMile(list.get(i).getAlarmMile());
                                    alarmInfoVo1.setAlarmTime(list.get(i).getAlarmTime().getTime());
                                    alarmInfoVo1.setAlarmType(list.get(i).getAlarmType());
                                    alarmInfoVo1.setAlarmContent(list.get(i).getContent());
                                    alarmInfoVo1.setAlarmLevel(list.get(i).getAlarmLevel());
                                    LambdaQueryWrapper<MsgAlarmExt> alarm = new LambdaQueryWrapper<>();
                                    alarm.eq(MsgAlarmExt::getAlarmId, list.get(0).getAlarmId());
                                    List<MsgAlarmExt> list1 = msgAlarmExtService.list(alarm);
                                    if (list1.size() > 0) {
                                        AlarmFilesVo[] vo = new AlarmFilesVo[list1.size()];
                                        for (int i1 = 0; i1 < list1.size(); i1++) {
                                            vo[i1] = new AlarmFilesVo();
                                            vo[i1].setAlarmAttType(list1.get(i1).getAlarmAttType());
                                            vo[i1].setAlarmAttPath(list1.get(i1).getAlarmAttPath());
                                        }
                                        alarmInfoVo1.setAlarmFiles(vo);
                                    }
                                    alarmListVo.setAlarmInfo(alarmInfoVo1);
                                    listVos.add(alarmListVo);
                                }
                            }
                        }
                        AlarmListVo[] array = listVos.toArray(new AlarmListVo[]{});
                        respHeartbeatVo.setAlarmList(array);
                        //获取机车前两路视频
                        int mile = heartbeatVo.getVisualDistance() + heartbeatVo.getCurrentMile();
                        LambdaQueryWrapper<BaseCameraManagement> base = new LambdaQueryWrapper<>();
                        if (heartbeatVo.getLineDir() == 1) {
                            base.orderByAsc(BaseCameraManagement::getInstallMile);
                            base.gt(BaseCameraManagement::getInstallMile, mile);
                        }
                        if (heartbeatVo.getLineDir() == 2) {
                            base.orderByDesc(BaseCameraManagement::getInstallMile);
                            base.lt(BaseCameraManagement::getInstallMile, mile);
                        }
                        base.eq(BaseCameraManagement::getRailwayCode, heartbeatVo.getRailwayCode());
                        base.eq(BaseCameraManagement::getOnline, 2);
                        base.last("limit 2");
                        List<BaseCameraManagement> list1 = baseCameraManagementService.list(base);
                        for (BaseCameraManagement baseCameraManagement : list1) {
                            cameraUtil.heartbeatgetUrl(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel(), type);
                            //判断心跳相机日志表后两条数据是否包含该相机编码
                            if (!ObjectUtils.isEmpty(codeList)) {
                                if (!codeList.contains(baseCameraManagement.getCameraCode())) {
                                    //如果不包含此相机编码
                                    for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                        cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                    }
                                }
                            }
                            //添加心跳相机日志信息
                            terminalHeartbeatLogService.add(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
                        }
                    } else {
                        //没有报警信息的时候
                        LambdaQueryWrapper<BaseCameraManagement> base = new LambdaQueryWrapper<>();
                        if (heartbeatVo.getLineDir() == 1) {
                            distance = heartbeatVo.getVisualDistance() + heartbeatVo.getCurrentMile();
                            base.gt(BaseCameraManagement::getInstallMile, distance);
                            base.in(BaseCameraManagement::getMonitoringDirection, 1, 3);
                            base.orderByAsc(BaseCameraManagement::getInstallMile);
                        }
                        if (heartbeatVo.getLineDir() == 2) {
                            distance = heartbeatVo.getVisualDistance() - heartbeatVo.getCurrentMile();
                            base.lt(BaseCameraManagement::getInstallMile, distance);
                            base.in(BaseCameraManagement::getMonitoringDirection, 2, 3);
                            base.orderByDesc(BaseCameraManagement::getInstallMile);
                        }
                        base.eq(BaseCameraManagement::getRailwayCode, heartbeatVo.getRailwayCode());
                        base.eq(BaseCameraManagement::getOnline, 2);
                        base.last("limit 2");
                        List<BaseCameraManagement> list1 = baseCameraManagementService.list(base);
                        if (list1.size() <= 0) {
                            jsonObject.put("resultCode", 0);
                            jsonObject.put("message", "失败");
                            jsonObject.put("data", "没有监控里程的相机");
                            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                        }
                        log.info("流程1----------------->");
                        //判断流媒体是否有流
                        String playFlv = cameraUtil.heartbeatgetPlayFlv(list1.get(0).getCameraCode(), list1.get(0).getChannel(), type);
                        log.info("playFlv------------>" + playFlv);
                        //拉四路视频
                        if (StringUtils.isEmpty(playFlv)) {
                            log.info("--------------->>已进入拉四路视频");
                            for (BaseCameraManagement baseCameraManagement : list1) {
                                boolean finalType = type;
                                List<String> finalCodeList4 = codeList;
                                CompletableFuture.runAsync(() -> {
                                    cameraUtil.heartbeatgetUrl(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel(), finalType);
                                    //判断心跳相机日志表后两条数据是否包含该相机编码
                                    if (!ObjectUtils.isEmpty(finalCodeList4)) {
                                        if (!finalCodeList4.contains(baseCameraManagement.getCameraCode())) {
                                            //如果不包含此相机编码
                                            for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                                cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                            }
                                        }
                                    }
                                    //添加心跳相机日志信息
                                    terminalHeartbeatLogService.add(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
                                });
                            }
                        } else {
                            log.info("-------------->>已进入拉三路路视频");
                            list1.remove(0);
                            log.info("list1-------------->>" + list1.size());
                            for (BaseCameraManagement baseCameraManagement : list1) {
                                boolean finalType1 = type;
                                List<String> finalCodeList5 = codeList;
                                CompletableFuture.runAsync(() -> {
                                    cameraUtil.heartbeatgetUrl(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel(), finalType1);
                                    if (!ObjectUtils.isEmpty(finalCodeList5)) {
                                        //判断心跳相机日志表后两条数据是否包含该相机编码
                                        if (!finalCodeList5.contains(baseCameraManagement.getCameraCode())) {
                                            //如果不包含此相机编码
                                            for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                                cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                            }
                                        }
                                    }
                                    //添加心跳相机日志信息
                                    terminalHeartbeatLogService.add(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
                                });
                            }
                        }
                        twoMap.put("currentStream", playFlv);
                        twoMap.put("cameraCode", list1.get(0).getCameraCode());
                        respHeartbeatVo.setCurrentStream(playFlv);
                        respHeartbeatVo.setIsAlarm(2);
                        respHeartbeatVo.setCameraCode(list1.get(0).getCameraCode());
                        respHeartbeatVo.setAlarmList(null);
                        respHeartbeatVo.setAlarmInfo(null);
                    }
                } else {
                    //没有报警信息的时候
                    LambdaQueryWrapper<BaseCameraManagement> base = new LambdaQueryWrapper<>();
                    if (heartbeatVo.getLineDir() == 1) {
                        distance = heartbeatVo.getVisualDistance() + heartbeatVo.getCurrentMile();
                        base.gt(BaseCameraManagement::getInstallMile, distance);
                        base.in(BaseCameraManagement::getMonitoringDirection, 1, 3);
                        base.orderByAsc(BaseCameraManagement::getInstallMile);
                    }
                    if (heartbeatVo.getLineDir() == 2) {
                        distance = heartbeatVo.getVisualDistance() - heartbeatVo.getCurrentMile();
                        base.lt(BaseCameraManagement::getInstallMile, distance);
                        base.in(BaseCameraManagement::getMonitoringDirection, 2, 3);
                        base.orderByDesc(BaseCameraManagement::getInstallMile);
                    }
                    base.eq(BaseCameraManagement::getRailwayCode, heartbeatVo.getRailwayCode());
                    base.eq(BaseCameraManagement::getOnline, 2);
                    base.last("limit 2");
                    List<BaseCameraManagement> list1 = baseCameraManagementService.list(base);
                    if (list1.size() <= 0) {
                        jsonObject.put("resultCode", 0);
                        jsonObject.put("message", "失败");
                        jsonObject.put("data", "没有监控里程的相机");
                        return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                    }
                    log.info("流程2----------------->");
                    //判断流媒体是否有流
                    String playFlv = cameraUtil.heartbeatgetPlayFlv(list1.get(0).getCameraCode(), list1.get(0).getChannel(), type);
                    log.info("playFlv------------>" + playFlv);
                    //拉四路视频
                    if (StringUtils.isEmpty(playFlv)) {
                        log.info("--------------->>已进入拉四路视频");
                        for (BaseCameraManagement baseCameraManagement : list1) {
                            List<String> finalCodeList2 = codeList;
                            boolean finalType2 = type;
                            CompletableFuture.runAsync(() -> {
                                cameraUtil.heartbeatgetUrl(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel(), finalType2);
                                if (!ObjectUtils.isEmpty(finalCodeList2)) {
                                    //判断心跳相机日志表后两条数据是否包含该相机编码
                                    if (!finalCodeList2.contains(baseCameraManagement.getCameraCode())) {
                                        //如果不包含此相机编码
                                        for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                            cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                        }
                                    }
                                }
                                //添加心跳相机日志信息
                                terminalHeartbeatLogService.add(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
                            });
                        }
                    } else {
                        log.info("-------------->>已进入拉三路路视频");
                        list1.remove(0);
                        log.info("list1-------------->>" + list1.size());
                        for (BaseCameraManagement baseCameraManagement : list1) {
                            boolean finalType3 = type;
                            List<String> finalCodeList3 = codeList;
                            CompletableFuture.runAsync(() -> {
                                cameraUtil.heartbeatgetUrl(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel(), finalType3);
                                if (!ObjectUtils.isEmpty(finalCodeList3)) {
                                    //判断心跳相机日志表后两条数据是否包含该相机编码
                                    if (!finalCodeList3.contains(baseCameraManagement.getCameraCode())) {
                                        //如果不包含此相机编码
                                        for (TerminalHeartbeatLog terminalHeartbeatLog : heartbeatLogList) {
                                            cameraUtil.stopStream(terminalHeartbeatLog.getCameraCode(), terminalHeartbeatLog.getChannel());
                                        }
                                    }
                                }
                                //添加心跳相机日志信息
                                terminalHeartbeatLogService.add(baseCameraManagement.getCameraCode(), baseCameraManagement.getChannel());
                            });
                        }
                    }
                    twoMap.put("currentStream", playFlv);
                    twoMap.put("cameraCode", list1.get(0).getCameraCode());
                    respHeartbeatVo.setCurrentStream(playFlv);
                    respHeartbeatVo.setIsAlarm(2);
                    respHeartbeatVo.setCameraCode(list1.get(0).getCameraCode());
                    respHeartbeatVo.setAlarmList(null);
                    respHeartbeatVo.setAlarmInfo(null);
                }
                twoMap.put("visualDistance", heartbeatVo.getVisualDistance().toString());
                String string = JSON.toJSONString(twoMap);
                heartbeatUtils.createHeartbeat(Constant.HERTBEA_KEY + heartbeatVo.getTerminalCode(), string);
                jsonObject.put("resultCode", 1);
                jsonObject.put("message", "ok");
                jsonObject.put("data", respHeartbeatVo);
                long end = System.currentTimeMillis();
                System.out.println("-------------------" + (begin - end) / 1000);
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            } else {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "心跳机制交互失败");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }

    }

    /**
     * 司机忽略报警接口
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/terminal/ignore")
    public String terminalIgnore(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        Map<String, Object> map = new HashMap<>();
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            TerminalIgnoreVo terminalIgnoreVo = JSON.parseObject(s, TerminalIgnoreVo.class);
            if (ObjectUtils.isEmpty(terminalIgnoreVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(terminalIgnoreVo.getAlarmId())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警唯一UUID不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            map.put("terminalCode", terminalIgnoreVo.getTerminalCode());
            map.put("alarmId", terminalIgnoreVo.getAlarmId());
            LambdaQueryWrapper<MsgAlarm> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MsgAlarm::getAlarmId, terminalIgnoreVo.getAlarmId());
            MsgAlarm msgAlarm = msgAlarmService.getOne(wrapper);
            if (ObjectUtils.isEmpty(msgAlarm)) {
                map.put("isIgnore", 1);
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", map);
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            TerminalIgnoreAlarm terminalIgnoreAlarm = new TerminalIgnoreAlarm();
            terminalIgnoreAlarm.setAlarmId(terminalIgnoreVo.getAlarmId());
            terminalIgnoreAlarm.setTerminalCode(terminalIgnoreVo.getTerminalCode());
            terminalIgnoreAlarm.setIgnoreTime(new Date());
            terminalIgnoreAlarm.setCreateBy(terminalIgnoreVo.getTerminalCode());
            terminalIgnoreAlarm.setUpdateBy(terminalIgnoreVo.getTerminalCode());
            boolean save = terminalIgnoreAlarmService.save(terminalIgnoreAlarm);
            if (save) {
                ignoreUtils.createIgnore("HL" + terminalIgnoreAlarm.getTerminalCode(), terminalIgnoreAlarm.getAlarmId());
                map.put("isIgnore", 2);
                jsonObject.put("resultCode", 1);
                jsonObject.put("message", "ok");
                jsonObject.put("data", map);
            } else {
                map.put("isIgnore", 3);
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", map);
            }
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 获取所有的相机列表接口定义
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/query/camera")
    public String camera(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            CameraVo cameraVo = JSON.parseObject(s, CameraVo.class);
            if (ObjectUtils.isEmpty(cameraVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<BaseCameraManagement> wrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(cameraVo.getRailwayCode())) {
                wrapper.eq(BaseCameraManagement::getRailwayCode, cameraVo.getRailwayCode());
            }
            if (!ObjectUtils.isEmpty(cameraVo.getLineDir())) {
                wrapper.eq(BaseCameraManagement::getLineDir, cameraVo.getLineDir());
            }
            wrapper.eq(BaseCameraManagement::getEnableOrNot, 1);
            wrapper.orderByAsc(BaseCameraManagement::getInstallMile);
            List<BaseCameraManagement> list = baseCameraManagementService.list(wrapper);
            if (list.size() <= 0) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "没有该线路编码或行别的相机信息");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            for (BaseCameraManagement baseCameraManagement : list) {
                LambdaQueryWrapper<BaseRailwayManagement> queryWrapper = new LambdaQueryWrapper<BaseRailwayManagement>();
                if (!ObjectUtils.isEmpty(baseCameraManagement.getRailwayCode())) {
                    queryWrapper.eq(BaseRailwayManagement::getRailwayCode, baseCameraManagement.getRailwayCode());
                }
                BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
                if (ObjectUtils.isEmpty(baseRailwayManagement)) {
                    jsonObject.put("resultCode", 0);
                    jsonObject.put("message", "失败");
                    jsonObject.put("data", "没有该线路编码的线路信息");
                    return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                }
                baseCameraManagement.setRailwayName(baseRailwayManagement.getRailwayName());
            }
            list.sort(new Comparator<BaseCameraManagement>() {
                @Override
                public int compare(BaseCameraManagement o1, BaseCameraManagement o2) {
                    String name1 = ChineseToPingyin.convertHanziToPinyin(o1.getRailwayName());
                    String substring1 = name1.substring(0, 1);
                    String name2 = ChineseToPingyin.convertHanziToPinyin(o2.getRailwayName());
                    String substring2 = name2.substring(0, 1);
                    int i = substring1.compareTo(substring2);
                    if (i == 0) {
                        return -o1.getInstallMile() - o2.getInstallMile();
                    } else {
                        return i;
                    }
                }
            });
            List<RespCameraVo> cameraVos = new ArrayList<>();
            for (BaseCameraManagement baseCameraManagement : list) {
                RespCameraVo respCameraVo = new RespCameraVo();
                BeanUtils.copyProperties(baseCameraManagement, respCameraVo);
                cameraVos.add(respCameraVo);
            }

            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            jsonObject.put("data", cameraVos);
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 点播功能接口定义(直播流)
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/camera/play")
    public String cameraPlay(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            CameraPlayVo cameraPlayVo = JSON.parseObject(s, CameraPlayVo.class);
            if (ObjectUtils.isEmpty(cameraPlayVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(cameraPlayVo.getCameraCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "相机编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<BaseCameraManagement>();
            lw.eq(BaseCameraManagement::getCameraCode, cameraPlayVo.getCameraCode());
            lw.eq(BaseCameraManagement::getOnline, 2);
            BaseCameraManagement one = baseCameraManagementService.getOne(lw);
            if (ObjectUtils.isEmpty(one)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "没有该相机编码的相机或已离线");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            String playFlv = cameraUtil.heartbeatgetUrl(one.getCameraCode(), one.getChannel(), false);
            map.put("url", playFlv);
            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            jsonObject.put("data", map);
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 关闭点播功能接口
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/close/camera/play")
    public String closeCameraPlay(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            CameraPlayVo cameraPlayVo = JSON.parseObject(s, CameraPlayVo.class);
            if (ObjectUtils.isEmpty(cameraPlayVo.getCameraCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "相机编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<BaseCameraManagement>();
            lw.eq(BaseCameraManagement::getCameraCode, cameraPlayVo.getCameraCode());
            lw.eq(BaseCameraManagement::getOnline, 2);
            BaseCameraManagement one = baseCameraManagementService.getOne(lw);
            if (ObjectUtils.isEmpty(one)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "没有该相机编码的相机或已离线");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            String code = cameraUtil.stopStream(one.getCameraCode(), one.getChannel());
            log.info("---------------code------------->" + code);
            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }


    /**
     * 获取报警列表（已解除和未解除）
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/query/alarm")
    public String queryAlarm(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            CameraVo cameraVo = JSON.parseObject(s, CameraVo.class);
            if (ObjectUtils.isEmpty(cameraVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<MsgAlarm> wrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(cameraVo.getRailwayCode())) {
                wrapper.eq(MsgAlarm::getRailwayCode, cameraVo.getRailwayCode());
            }
            if (!ObjectUtils.isEmpty(cameraVo.getLineDir())) {
                wrapper.eq(MsgAlarm::getLineDir, cameraVo.getLineDir());
            }
            List<MsgAlarm> list = msgAlarmService.list(wrapper);
            if (list.size() <= 0) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "没有该报警线路或行别的报警信息");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            QueryAlarmVo queryAlarmVo = new QueryAlarmVo();
            List<RespAlarmList> listVos = new ArrayList<>();
            for (MsgAlarm msgAlarm : list) {
                RespAlarmList alarmListVo = new RespAlarmList();
                AlarmInfoVo alarmInfoVo1 = new AlarmInfoVo();
                alarmInfoVo1.setAlarmId(msgAlarm.getAlarmId());
                alarmInfoVo1.setAlarmMile(msgAlarm.getAlarmMile());
                alarmInfoVo1.setAlarmTime(msgAlarm.getAlarmTime().getTime());
                alarmInfoVo1.setAlarmType(msgAlarm.getAlarmType());
                alarmInfoVo1.setAlarmLevel(msgAlarm.getAlarmLevel());
                LambdaQueryWrapper<MsgAlarmExt> alarm = new LambdaQueryWrapper<>();
                alarm.eq(MsgAlarmExt::getAlarmId, msgAlarm.getAlarmId());
                List<MsgAlarmExt> list1 = msgAlarmExtService.list(alarm);
                if (list1.size() > 0) {
                    AlarmFilesVo[] vo = new AlarmFilesVo[list1.size()];
                    for (int i1 = 0; i1 < list1.size(); i1++) {
                        vo[i1] = new AlarmFilesVo();
                        vo[i1].setAlarmAttType(list1.get(i1).getAlarmAttType());
                        vo[i1].setAlarmAttPath(list1.get(i1).getAlarmAttPath());
                    }
                    //返回结果
                    alarmInfoVo1.setAlarmFiles(vo);
                }
                alarmListVo.setAlarmInfo(alarmInfoVo1);
                listVos.add(alarmListVo);
            }
            RespAlarmList[] array = listVos.toArray(new RespAlarmList[]{});
            queryAlarmVo.setAlarmList(array);
            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            jsonObject.put("data", queryAlarmVo);
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 获取报警回放
     *
     * @param parameterVo
     * @param request
     * @return
     */
    @PostMapping("/api/alarm/play")
    public String alarmPlay(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String urls = null;
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            TerminalIgnoreVo terminalIgnoreVo = JSON.parseObject(s, TerminalIgnoreVo.class);
            if (ObjectUtils.isEmpty(terminalIgnoreVo.getTerminalCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "车载终端编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(terminalIgnoreVo.getAlarmId())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "报警唯一UUID不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            LambdaQueryWrapper<MsgAlarm> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MsgAlarm::getAlarmId, terminalIgnoreVo.getAlarmId());
            MsgAlarm one = msgAlarmService.getOne(wrapper);
            if (ObjectUtils.isEmpty(one)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "没有该报警唯一UUID的报警数据");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            } else {
                if (ObjectUtils.isEmpty(one.getReleasedUrl())) {
                    //获取报警里程所监控的所有相机
                    LambdaQueryWrapper<BaseCameraManagement> camera = new LambdaQueryWrapper<BaseCameraManagement>();
                    if (one.getLineDir() == 1) {
                        camera.ge(BaseCameraManagement::getBeginMile, one.getAlarmMile());
                        camera.le(BaseCameraManagement::getEndMile, one.getAlarmMile());
                        camera.in(BaseCameraManagement::getMonitoringDirection, 1, 3);
                        camera.orderByAsc(BaseCameraManagement::getInstallMile);
                    } else if (one.getLineDir() == 2) {
                        camera.le(BaseCameraManagement::getBeginMile, one.getAlarmMile());
                        camera.ge(BaseCameraManagement::getEndMile, one.getAlarmMile());
                        camera.in(BaseCameraManagement::getMonitoringDirection, 2, 3);
                        camera.orderByDesc(BaseCameraManagement::getInstallMile);
                    }
                    camera.eq(BaseCameraManagement::getOnline, 2);
                    List<BaseCameraManagement> baseCameraManagementList = baseCameraManagementService.list(camera);
                    if (baseCameraManagementList.size() > 0) {
                        BaseCameraManagement baseCameraManagement = baseCameraManagementList.get(0);
                        String url = cameraUtil.streamConversion(baseCameraManagement.getCameraCode());
                        one.setReleasedUrl(url);
                        msgAlarmService.updateById(one);
                    } else {
                        jsonObject.put("resultCode", 0);
                        jsonObject.put("message", "失败");
                        jsonObject.put("data", "没有该报警里程位置的相机");
                        return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
                    }
                }
            }
            map.put("url", one.getReleasedUrl());
            jsonObject.put("resultCode", 1);
            jsonObject.put("message", "ok");
            jsonObject.put("data", map);
            return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }


    /**
     * 天网相机心跳监测接口定义
     */
    @PostMapping("/api/skynet/heartbeat")
    public String skynetHeartbeat(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            SkynetHeartbeatVo skynetHeartbeatVo = JSON.parseObject(s, SkynetHeartbeatVo.class);
            if (ObjectUtils.isEmpty(skynetHeartbeatVo.getRailwayCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "线路编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(skynetHeartbeatVo.getLineDir())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "行别不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (skynetHeartbeatVo.getLineDir() != 1 && skynetHeartbeatVo.getLineDir() != 2) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "行别填写错误");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(skynetHeartbeatVo.getInstallMile())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "安装里程位置不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (!skynetHeartbeatVo.getInstallMile().toString().matches(PATTERNS)) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "里程位置填写错误,只能填写数字");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(skynetHeartbeatVo.getCameraCode())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "相机编码不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            SkynetHeartbeatLog skynetHeartbeatLog = new SkynetHeartbeatLog();
            skynetHeartbeatLog.setCameraCode(skynetHeartbeatVo.getCameraCode());
            skynetHeartbeatLog.setInstallMile(skynetHeartbeatVo.getInstallMile());
            skynetHeartbeatLog.setRailwayCode(skynetHeartbeatVo.getRailwayCode());
            skynetHeartbeatLog.setLineDir(skynetHeartbeatVo.getLineDir());
            if (synetHeartbeatLogMapper.insert(skynetHeartbeatLog) > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("cameraCode", skynetHeartbeatVo.getCameraCode());
                jsonObject.put("resultCode", 1);
                jsonObject.put("message", "ok");
                jsonObject.put("data", map);
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            } else {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "新增失败");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    /**
     * 第三方报警系统心跳监测接口定义
     */
    @PostMapping("/api/alarm/heartbeat")
    public String alarmHeartbeat(@RequestBody ParameterVo parameterVo, HttpServletRequest request) {
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        log.info("token:{}", token);
        log.info("parameter:{}", parameterVo);
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(token)) {
            String[] split = token.split("-");
            String key = split[split.length - 1];
            String s = SM4Utils.decryptData_ECB(parameterVo.getParameter(), key);
            SvcAddress svcAddress = JSON.parseObject(s, SvcAddress.class);
            if (ObjectUtils.isEmpty(svcAddress.getGrantType())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "授权类型不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            if (ObjectUtils.isEmpty(svcAddress.getClientId())) {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "客户端编号不能为空");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
            AlarmHeartbeatLog alarmHeartbeatLog = new AlarmHeartbeatLog();
            alarmHeartbeatLog.setClientId(svcAddress.getClientId());
            alarmHeartbeatLog.setGrantType(svcAddress.getGrantType());
            if (alarmHeartbeatLogMapper.insert(alarmHeartbeatLog) > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("cameraCode", svcAddress.getClientId());
                jsonObject.put("resultCode", 1);
                jsonObject.put("message", "ok");
                jsonObject.put("data", map);
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            } else {
                jsonObject.put("resultCode", 0);
                jsonObject.put("message", "失败");
                jsonObject.put("data", "新增失败");
                return SM4Utils.encryptData_ECB(JSONObject.toJSONString(jsonObject), key);
            }
        } else {
            jsonObject.put("resultCode", 2);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "token验证失败");
            return JSONObject.toJSONString(jsonObject);
        }
    }

    @GetMapping("/test/{key}")
    public void test(@PathVariable String key) {
        String heartbeat = heartbeatUtils.getHeartbeat(key);
        Map maps2 = JSON.parseObject(heartbeat, Map.class);
        System.out.println(maps2);
    }

    @GetMapping("/testi")
    public void testi() {
        Map<String, String> map = new HashMap<>();
        map.put("age", "18");
        map.put("name", "小明");
        map.put("gender", "男");
        String string = JSON.toJSONString(map);
        heartbeatUtils.createHeartbeat("key", string);
    }

    private String objStr(HeartbeatVo heartbeatVoCH) {
        JSONObject obj = new JSONObject();
        //data
        obj.put("data", heartbeatVoCH);
        //code
        obj.put("code", 151);
        log.info("【websocket消息】 机车地址信息:" + obj.toJSONString());
        return obj.toJSONString();
    }

//    public static void main(String[] args) {
//        JSONObject res = new JSONObject();
//        res.put("grantType", "1");
//        res.put("clientId", "1005");
//        String s = SM4Utils.encryptData_ECB(JSONObject.toJSONString(res), "4370780c9a8c43e5");
//        System.out.println(s);
//    }

    @PostMapping("/encryptionAlarm")
    public String encryptionAlarm(@RequestBody ReqMsgAlarmVo vo) {
        String s1 = JSONObject.toJSONString(vo);
        return SM4Utils.encryptData_ECB(s1, "4370780c9a8c43e5");
    }
}
