DeviceActiveService.java 9.07 KB
package cn.com.glsx.stdinterface.modules.service;

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 com.glsx.plat.common.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * 设备激活 service
 *
 * @author yangll
 */
@Slf4j
@Service
public class DeviceActiveService {

    @Autowired
    private RemoteGpsBsService remoteGpsBsService;

    @Autowired
    private VehicleDeviceMapper vehicleDeviceMapper;

    /**
     * 保存设备激活信息 feign 调用
     *
     * @param snList 设备号集合
     */
    public void saveActiveInfo(List<String> snList) {
        if (CollectionUtil.isEmpty(snList)) {
            log.info("saveActiveInfo snList is null");
            return;
        }
        saveActiveInfoByFeign(snList);
    }

    /**
     * 保存设备激活信息 by 定时任务调用
     *
     * @param snList 设备号集合
     */
    public void saveActiveInfoByTask(List<String> snList) {
        if (CollectionUtil.isEmpty(snList)) {
            log.info("saveActiveInfoByTask snList is null");
            return;
        }
        saveActiveInfoSyncByTask(snList);
    }

    /**
     * 多线程补充设备激活经纬度数据和激活地址 by 定时任务调用
     *
     * @param dataList 设备号集合大小
     */
    public void saveActiveInfoSyncByTask(List<String> dataList) {
        log.info("###save active info by task data start###");
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        StopWatch sw = new StopWatch();
        sw.start("saveActiveInfoSyncByTask");
        for (String sn : dataList) {
            saveActiveInfoByTask(sn);
        }
        sw.stop();
        log.info("###save active info by task data end" + sw.getLastTaskTimeMillis() + "ms###");
    }

    /**
     * 获取7天内没有激活数据的设备号
     *
     * @return 设备号集合
     */
    public List<String> getUnActiveInfoSnList() {
        // 获取七天前的日期
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -7);
        String dayTimeFormat = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        log.info("开始数据统计的天数===" + dayTimeFormat);
        return vehicleDeviceMapper.getUnActiveInfoSnList(dayTimeFormat);
    }

    /**
     * 手动更新全部设备激活定位数据
     */
    public void saveActiveInfoFlushAll() {
        List<String> allUnActiveInfoSnList = vehicleDeviceMapper.getAllUnActiveInfoSnList();
        log.info("allUnActiveInfoSnList size===" + allUnActiveInfoSnList.size());
        saveActiveInfoByTask(allUnActiveInfoSnList);
    }

    public void saveActiveInfoByFeign(List<String> snList) {
        if (CollectionUtil.isEmpty(snList)) {
            log.info("snList is null");
            return;
        }
        try {
            log.info("saveActiveInfoByFeign 开始同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
            List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
            if (CollectionUtil.isEmpty(lastGpsBsList)) {
                log.info("saveActiveInfoByFeign lastGpsBsList is null");
                return;
            }
            lastGpsBsList.forEach(gpsBsData -> {
                log.info("Feign 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("saveActiveInfoByFeign异常信息===" + e.getMessage(), e);
        } finally {
            log.info(Thread.currentThread().getName() + "关闭资源");
        }
    }

    /**
     * 通过sn同步设备激活信息
     *
     * @param sn 设备号
     */
    public void saveActiveInfoByTask(String sn) {
        if (StringUtils.isEmpty(sn)) {
            log.info("sn is null");
            return;
        }
        try {
            log.info("saveActiveInfoByTask 开始同步激活信息,sn===" + sn);
            List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(Lists.newArrayList(sn));
            if (CollectionUtil.isEmpty(lastGpsBsList)) {
                log.info("saveActiveInfoByTask lastGpsBsList is null");
                return;
            }
            lastGpsBsList.forEach(gpsBsData -> {
                log.info("Task gpsBsData===" + JSONUtil.toJsonStr(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 {
            log.info(Thread.currentThread().getName() + "关闭资源");
        }
    }
}