AsyncTasks.java 9.75 KB
package cn.com.glsx.stdinterface.modules.multithread;

import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.devicemonitor.api.feign.GpsBsFeignService;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.dto.sxj.OfflineAlarmInfo;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AsyncTasks {

    @Autowired
    private GpsBsFeignService gpsBsFeignService;

    /**
     * 异步填充设备激活经纬度和激活地址信息
     *
     * @param snList
     * @param countDownLatch
     * @param remoteGpsBsService
     */
    @Async
    public void saveActiveInfo(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
        if (CollectionUtils.isEmpty(snList)) {
            log.info("countDownLatch查询数据为空关闭资源");
            countDownLatch.countDown();
            return;
        }
        try {
            log.info("saveActiveInfo 开始分批同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
            List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
            if (CollectionUtil.isEmpty(lastGpsBsList)) {
                log.info("saveActiveInfo lastGpsBsList is null");
                return;
            }
            lastGpsBsList.forEach(gpsBsData -> {
                log.info("gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
                VehicleDevice vehicleDevice = new VehicleDevice();
                vehicleDevice.setSn(gpsBsData.getSn());
                GpsLocation gpsLocation = gpsBsData.getGpsLocation();
                // 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
                if (Objects.nonNull(gpsLocation)) {
                    vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
                    vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
                    vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
                    vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
                    vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
                    // 保存激活信息
                    log.info("active device info gps:" + JSONUtil.toJsonStr(vehicleDevice));
                    vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
                } else {
                    GpsLocation bsLocation = gpsBsData.getBsLocation();
                    if (Objects.nonNull(bsLocation)) {
                        vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
                        vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
                        vehicleDevice.setActiveLocationLng(bsLocation.getLng());
                        vehicleDevice.setActiveLocationLat(bsLocation.getLat());
                        vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
                        // 保存激活信息
                        log.info("active device info bs:" + JSONUtil.toJsonStr(vehicleDevice));
                        vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
                    }
                }
            });
        } catch (Exception e) {
            log.info("saveActiveInfo异常信息===" + e.getMessage(), e);
        } finally {
            countDownLatch.countDown();
            log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
        }
    }

    /**
     * 异步填充设备激活经纬度和激活地址信息
     *
     * @param snList
     * @param countDownLatch
     * @param remoteGpsBsService
     */
    @Async
    public void saveActiveInfoByTask(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
        if (CollectionUtils.isEmpty(snList)) {
            log.info("countDownLatch查询数据为空关闭资源");
            countDownLatch.countDown();
            return;
        }
        try {
            log.info("saveActiveInfoByTask 开始分批同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
            List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(snList);
            if (CollectionUtil.isEmpty(lastGpsBsList)) {
                log.info("saveActiveInfoByTask lastGpsBsList is null");
                return;
            }
            lastGpsBsList.forEach(gpsBsData -> {
                VehicleDevice vehicleDevice = new VehicleDevice();
                vehicleDevice.setSn(gpsBsData.getSn());
                GpsLocation gpsLocation = gpsBsData.getGpsLocation();
                // 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
                if (ObjectUtil.isNotNull(gpsLocation)) {
                    vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
                    vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
                    vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
                    vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
                    vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
                    // 保存激活信息
                    log.info("active device info by task gps:" + JSONUtil.toJsonStr(vehicleDevice));
                    vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
                } else {
                    GpsLocation bsLocation = gpsBsData.getBsLocation();
                    if (ObjectUtil.isNotNull(bsLocation)) {
                        vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
                        vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
                        vehicleDevice.setActiveLocationLng(bsLocation.getLng());
                        vehicleDevice.setActiveLocationLat(bsLocation.getLat());
                        vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
                        // 保存激活信息
                        log.info("active device info by task bs:" + JSONUtil.toJsonStr(vehicleDevice));
                        vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
                    }
                }
            });
        } catch (Exception e) {
            log.info("saveActiveInfoByTask异常信息===" + e.getMessage(), e);
        } finally {
            countDownLatch.countDown();
            log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
        }
    }

    /**
     * 异步解析填充离线设备经纬度信息
     *
     * @param offlineExportList  离线数据列表
     * @param countDownLatch     线程计数器
     */
    @Async
    public void parseOfficeGpsAndBs(List<OfflineAlarmInfo> offlineExportList, CountDownLatch countDownLatch) {
        if (CollectionUtils.isEmpty(offlineExportList)) {
            log.info("countDownLatch查询数据为空关闭资源");
            countDownLatch.countDown();
            return;
        }
        try {
            List<String> snList = offlineExportList.stream().map(OfflineAlarmInfo::getImei).collect(Collectors.toList());
            Map<String, GpsBsData> gpsBsDataMap = getLastGpsBsBatchBySns(snList);
            offlineExportList.forEach(offlineExport -> {
                GpsBsData gpsBsData = gpsBsDataMap.get(offlineExport.getImei());
                if (gpsBsData != null) {
                    // 封装最后GPS数据
                    GpsLocation gpsLocation = gpsBsData.getGpsLocation();
                    if (ObjectUtil.isNotNull(gpsLocation)) {
                        offlineExport.setLng(gpsLocation.getLng());
                        offlineExport.setLat(gpsLocation.getLat());
                    }
                }
            });
        } catch (Exception e) {
            log.info("parseOfficeGpsAndBs异常信息===" + e.getMessage());
        } finally {
            countDownLatch.countDown();
            log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
        }
    }

    public Map<String, GpsBsData> getLastGpsBsBatchBySns(List<String> snList) {
        R<List<GpsBsData>> lastGpsBsBatch = gpsBsFeignService.getLastGpsBsBatch(snList);
        List<GpsBsData> gpsBsDataList = lastGpsBsBatch.getDataList(new TypeToken<List<GpsBsData>>() {
        }.getType());

        Map<String, GpsBsData> gpsBsDataMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(gpsBsDataList)) {
            gpsBsDataMap = gpsBsDataList.stream().collect(Collectors.toMap(GpsBsData::getSn, Function.identity(), (o, n) -> n));
        }
        return gpsBsDataMap;
    }
}