VehicleService.java 11 KB
package cn.com.glsx.stdinterface.modules.service;

import cn.com.glsx.devicemonitor.api.dto.response.DeviceDTO;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.devicemonitor.api.enums.GeoLocationEnum;
import cn.com.glsx.devicemonitor.api.feign.DeviceFeignService;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.common.enums.ResultEnum;
import cn.com.glsx.stdinterface.common.exception.StdInterfaceException;
import cn.com.glsx.stdinterface.dto.DevicesComposeVerifyBO;
import cn.com.glsx.stdinterface.dto.VehicleDto;
import cn.com.glsx.stdinterface.modules.converter.VehicleConverter;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.entity.VehicleDeviceOperator;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.model.*;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.com.glsx.stdinterface.modules.util.DeviceLocationUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 车辆服务
 */
@Slf4j
@Service
public class VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private VehicleDeviceMapper vehicleDeviceMapper;

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private RemoteGpsBsService remoteGpsBsService;

    @Autowired
    private DeviceFeignService deviceFeignService;

    public Vehicle getVehicleInfoBySn(String sn) {
        Vehicle vehicle = vehicleMapper.getVehicleBySn(sn);
        return vehicle;
    }

    public List<VehicleDeviceOperator> getVehicleListBySn(Collection<String> sn) {
        return vehicleMapper.listVehicleBySn(sn);
    }

    /**
     * key:sn value:VehicleDeviceOperator
     */
    public Map<String, VehicleDeviceOperator> getVehicleDeviceOperatorMap(List<String> snList) {
        List<VehicleDeviceOperator> vehicleList = getVehicleListBySn(snList);
        if (CollectionUtils.isEmpty(vehicleList)) {
            return Maps.newHashMap();
        }
        return vehicleList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(VehicleDeviceOperator::getSn, Function.identity(), (o1, o2) -> o2));
    }

    public Long getVdId(String sn) {
        VehicleDevice vehicleDevice = vehicleDeviceMapper.selectVehicleDevcieBySn(sn);
        return Optional.ofNullable(vehicleDevice)
                .map(VehicleDevice::getId)
                .orElse(null);
    }

    /**
     * key:sn value:vdId
     */
    public Map<String, Long> getSnVdIdMap(Collection<String> sn) {
        List<VehicleDevice> vehicleDevice = vehicleDeviceMapper.selectBindingDevices(sn);
        if (CollectionUtils.isEmpty(vehicleDevice)) {
            return Maps.newHashMap();
        }
        return vehicleDevice.stream()
                .collect(Collectors.toMap(VehicleDevice::getSn, VehicleDevice::getId, (o1, o2) -> o2));
    }

    public Vehicle getVehicleInfoByWorkOrderIdAndVin(Long workOrderId, String vin) {
        if (workOrderId == null || StringUtils.isBlank(vin)) {
            return null;
        }
        return vehicleMapper.selectByWorkOrderIdAndVin(workOrderId, vin);
    }

    public boolean verifyDevicesCompose(DevicesComposeVerifyBO verifyBO) {
        //获取工单id
        if (verifyBO.getWorkOrderId() == null) {
            Vehicle vehicle = vehicleMapper.selectByPrimaryKey(verifyBO.getVehicleId());
            //没有工单,无需判断设备组合,直接返回验证通过
            if (vehicle.getWorkOrderId() == null) {
                return true;
            } else {
                verifyBO.setWorkOrderId(vehicle.getWorkOrderId());
            }
        }

        //验证设备存在性
        workOrderService.checkDevicesExist(verifyBO.getDevices());

        //验证设备组合正确性
        workOrderService.checkDevicesCompose(verifyBO.getWorkOrderId(), verifyBO.getDevices());

        return true;
    }

    public List<VehicleDto> getVehicleByVinAndName(String vin, String name) {
        List<Vehicle> vehicleList = vehicleMapper.getVehicleByVinAndName(vin, name);
        return VehicleConverter.INSTANCE.syncEntity2DtoList(vehicleList);
    }

    /**
     * 根据设备号批量查询车架号信息
     *
     * @param snList 设备号集合
     * @return sn和vin的映射列表
     */
    public List<VehicleSnVinDTO> getVinBySnList(List<String> snList) {
        if (CollectionUtils.isEmpty(snList)) {
            return Lists.newArrayList();
        }
        List<VehicleSnVinDTO> list = vehicleMapper.getVinBySnList(snList);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list;
    }

    /**
     * 查询襄本设备定位信息
     *
     * @param verifyBO 襄本请求体
     * @return
     */
    public XBAddrVerifyDTO addrVerifyByXB(XBAddrVerifyBo verifyBO) {
        if (StringUtils.isEmpty(verifyBO.getSn())) {
            throw new StdInterfaceException(ResultEnum.PARAM_ERROR);
        }

        XBAddrVerifyDTO verifyDTO = vehicleMapper.getVehicleBySnAndTenantName(verifyBO.getSn(), verifyBO.getOrderSource());
        if (Objects.isNull(verifyDTO)) {
            log.warn("没有查询到该设备相关信息==" + JSONUtil.toJsonStr(verifyBO));
            throw new StdInterfaceException(ResultEnum.DEVICE_NOT_EXIST);
        }

        List<XBDeviceAddressItem> list = Lists.newArrayList();
        //查位置点
        Map<String, GpsBsData> gpsBsDataMap = remoteGpsBsService.getLastGpsBsBatchBySns(Lists.newArrayList(verifyBO.getSn()));

        XBDeviceAddressItem addressItem = new XBDeviceAddressItem();

        R<DeviceDTO> deviceDTOR = deviceFeignService.getDeviceBySn(verifyBO.getSn());
        DeviceDTO deviceDTO = deviceDTOR.getData(DeviceDTO.class);
        addressItem.setSn(verifyBO.getSn());
        addressItem.setSim(deviceDTO.getSim());
        addressItem.setDeviceType((deviceDTO.getSource() != null && deviceDTO.getSource() == Constants.DeviceSource.WIRED) ? "有线设备" : "无线设备");

        GpsBsData gpsBsData = gpsBsDataMap.get(verifyBO.getSn());
        if (gpsBsData != null) {
            String speedValue = "";
            // 获取 Gps 定位数据
            GpsLocation gpsLocation = gpsBsData.getGpsLocation();
            if (gpsLocation != null) {
                addressItem.setGpsLat(gpsLocation.getLat());
                addressItem.setGpsLng(gpsLocation.getLng());
                addressItem.setGpsPosition(gpsLocation.getAddress());
                addressItem.setGpsTime(gpsLocation.getDateTime());
                speedValue = gpsLocation.getSpeed();
            }

            // 获取基站定位数据
            GpsLocation bsLocation = gpsBsData.getBsLocation();
            if (bsLocation != null) {
                addressItem.setBsLat(bsLocation.getBlat());
                addressItem.setBsLng(bsLocation.getBlng());
                addressItem.setBsPosition(bsLocation.getAddress());
                addressItem.setBsTime(bsLocation.getDateTime());
            }

            // 如果基站时间为空,则将GPS时间填充到基站时间中
            if (StringUtils.isBlank(addressItem.getBsTime())) {
                addressItem.setBsTime(addressItem.getGpsTime());
            }

            // 设置设备位置有效状态
            addressItem.setType(DeviceLocationUtil.resolutionGpsType(addressItem.getGpsTime(), addressItem.getBsTime(), deviceDTO.getSource()));
            // 设置设备位置行驶状态
            addressItem.setStatus(DeviceLocationUtil.resolutionDeviceOnlineStatus(addressItem, deviceDTO.getSource(), speedValue));
            list.add(addressItem);
            verifyDTO.setList(list);
        }
        return verifyDTO;
    }

    /**
     * 批量查询设备GPS设备定位数据
     *
     * @param verifyBatchBo 查询gps设备参数
     * @return
     */
    public List<AddrVerifyBatchDTO> addrVerifyBatch(AddrVerifyBatchBo verifyBatchBo) {
        StopWatch sw = new StopWatch();
        sw.start("checkSnBySnListAndTenantName");
        if (CollectionUtils.isEmpty(verifyBatchBo.getSnList())) {
            throw new StdInterfaceException(ResultEnum.PARAM_ERROR);
        }

        List<String> snList = vehicleMapper.checkSnBySnListAndTenantName(verifyBatchBo.getSnList(), verifyBatchBo.getOrderSource());
        if (CollectionUtils.isEmpty(snList)) {
            log.warn("没有查询到该设备相关信息==" + JSONUtil.toJsonStr(verifyBatchBo));
            throw new StdInterfaceException(ResultEnum.DEVICE_ARR_NOT_EXIST);
        }
        sw.stop();
        log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");

        List<AddrVerifyBatchDTO> list = Lists.newArrayList();
        //查位置点
        sw.start("getLastGpsBsSimpleBatchBySns");
        Map<String, GpsBsData> gpsBsDataMap = remoteGpsBsService.getLastGpsBsSimpleBatchBySns(snList);
        sw.stop();
        log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");

        sw.start("merge");
        snList.forEach(sn -> {
            AddrVerifyBatchDTO addressItem = new AddrVerifyBatchDTO();
            GpsBsData gpsBsData = gpsBsDataMap.get(sn);
            if (gpsBsData != null) {
                addressItem.setSn(sn);
                // 获取 Gps 定位数据
                GpsLocation gpsLocation = gpsBsData.getGpsLocation();
                if (gpsLocation != null) {
                    addressItem.setLat(gpsLocation.getLat());
                    addressItem.setLng(gpsLocation.getLng());
                    addressItem.setTime(gpsLocation.getDateTime());
                    addressItem.setLocationType(GeoLocationEnum.GPS.getLocationType());
                }

                // 获取基站定位数据
                GpsLocation bsLocation = gpsBsData.getBsLocation();
                if (bsLocation != null) {
                    addressItem.setLat(bsLocation.getBlat());
                    addressItem.setLng(bsLocation.getBlng());
                    addressItem.setTime(bsLocation.getDateTime());
                    addressItem.setLocationType(GeoLocationEnum.BS.getLocationType());
                }
                list.add(addressItem);
            }
        });
        sw.stop();
        log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");
        log.info("addrVerifyBatch总耗时:" + sw.getTotalTimeSeconds() + "s");
        return list;
    }
}