DeviceActiveService.java 3.42 KB
package cn.com.glsx.admin.modules.service;

import cn.com.glsx.admin.common.enums.TypeConverseEunm;
import cn.com.glsx.admin.modules.mapper.VehicleMapper;
import cn.com.glsx.admin.modules.model.export.DeviceActiveExprot;
import cn.com.glsx.admin.modules.model.param.DeviceActiveSearch;
import cn.com.glsx.admin.modules.multithread.AsyncTasks;
import cn.com.glsx.admin.modules.multithread.ListSplitIterator;
import cn.com.glsx.admin.modules.remote.RemoteGpsBsService;
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.util.List;
import java.util.concurrent.CountDownLatch;

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

    @Autowired
    private RemoteGpsBsService remoteGpsBsService;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private AsyncTasks asyncTasks;

    /**
     * 查询设备激活列表
     *
     * @param deviceActiveSearch 查询实体
     * @return
     */
    public List<DeviceActiveExprot> findDeviceActiveList(DeviceActiveSearch deviceActiveSearch) {
        List<DeviceActiveExprot> deviceActiveList = vehicleMapper.getBaseDeviceActiveListWithAuth(deviceActiveSearch);
        getDeviceActiveData(deviceActiveList);
        return deviceActiveList;
    }

    /**
     * 导出设备激活列表
     *
     * @param deviceActiveSearch 导出实体
     * @return
     */
    public List<DeviceActiveExprot> exportDeviceActiveList(DeviceActiveSearch deviceActiveSearch) {
        List<DeviceActiveExprot> deviceActiveList = vehicleMapper.getBaseDeviceActiveListWithAuth(deviceActiveSearch);
        getDeviceActiveData(deviceActiveList);
        // 数字类型转换为字符串类型
        deviceActiveList.forEach(deviceActiveExprot -> {
            deviceActiveExprot.setDeviceTypeStr(TypeConverseEunm.DeviceTypeEnum.getStrByDeviceType(deviceActiveExprot.getDeviceType()));
            deviceActiveExprot.setSourceStr(TypeConverseEunm.SourceEnum.getStrBySourceType(deviceActiveExprot.getSource()));
            deviceActiveExprot.setIsOnLineStr(TypeConverseEunm.OnLineEnum.getStrByonLineType(deviceActiveExprot.getIsOnLine()));
        });
        return deviceActiveList;
    }

    /**
     * 多线程查询设备总里程和总行驶次数
     *
     * @param dataList 数据集合大小
     */
    public void getDeviceActiveData(List<DeviceActiveExprot> dataList) {
        log.info("###get device active data start###");
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        StopWatch sw = new StopWatch();
        sw.start("getDeviceActiveData");
        ListSplitIterator<DeviceActiveExprot> listSplitIterator = new ListSplitIterator<>(dataList, 500);
        CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size());
        while (listSplitIterator.hasNext()) {
            List<DeviceActiveExprot> deviceActiveList = listSplitIterator.next();
            asyncTasks.getDeviceActiveData(deviceActiveList, countDownLatch, remoteGpsBsService);
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sw.stop();
        log.info("###get device active data end" + sw.getLastTaskTimeMillis() + "ms###");
    }
}