AsyncTasks.java 7.23 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.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.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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

@Slf4j
@Service
public class AsyncTasks {

    /**
     * 异步填充设备激活经纬度和激活地址信息
     *
     * @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关闭资源");
        }
    }
}