package com.ningdatech.carapi.car;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.NdDateUtils;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.archives.enums.AuditStateEnum;
import com.ningdatech.carapi.car.equipment.model.NdVehicleEquipmentInfo;
import com.ningdatech.carapi.car.equipment.service.INdVehicleEquipmentInfoService;
import com.ningdatech.carapi.car.vehicle.model.NdVehicleBaseInfo;
import com.ningdatech.carapi.car.vehicle.model.dto.VehicleArchivesPageQuery;
import com.ningdatech.carapi.car.vehicle.service.IVehicleBaseInfoService;
import com.ningdatech.carapi.common.constant.DeletedEnum;
import com.ningdatech.carapi.scheduler.service.IVehicleOnlineStatusDayService;
import com.ningdatech.carapi.scheduler.service.IVehicleOnlineStatusHourService;
import com.ningdatech.carapi.sys.entity.Company;
import com.ningdatech.carapi.sys.entity.Operator;
import com.ningdatech.carapi.sys.entity.Region;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.sys.service.CompanyService;
import com.ningdatech.carapi.sys.service.IRegionService;
import com.ningdatech.carapi.sys.service.OperatorService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Classname InsureOutdangerTest
 * @Description
 * @Date 2022/12/22 10:47
 * @Created by PoffyZhang
 */
@Slf4j
public class VehicleBaseInfoTest extends AppTests {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private IVehicleBaseInfoService vehicleBaseInfoService;
    @Autowired
    private OperatorService operatorService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RegionsCacheHelper regionCacheHelper;
    @Autowired
    private INdVehicleEquipmentInfoService vehicleEquipmentInfoService;
    @Autowired
    private IVehicleOnlineStatusHourService vehicleOnlineStatusHourService;
    @Autowired
    private IVehicleOnlineStatusDayService vehicleOnlineStatusDayService;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Test
    public void test() throws FileNotFoundException {
        File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\车辆.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> companySet = new HashSet<>();
            HashSet<String> operatorSet = new HashSet<>();
            HashSet<String> countySet = new HashSet<>();

            // 根据企业名查询企业信息
            Map<String, Company> companyNameMap = companyService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            companySet.add(v.getName()))
                    .collect(Collectors.toMap(Company::getName, v -> v));

            // 根据运营商名称查询运营商信息
            Map<String, Operator> operatorMap = operatorService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            operatorSet.add(v.getName()))
                    .collect(Collectors.toMap(Operator::getName, v -> v));

            // 根据区县名称获取区域ID
            Map<String, Region> regionMap = regionService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            !"市本级".equals(v.getName()) &&
                            countySet.add(v.getName()))
                    .collect(Collectors.toMap(Region::getName, v -> v));

            //获取最大列数
            for (int i = 1; i < rownum; i++) {

                NdVehicleBaseInfo vehicleBaseInfo = new NdVehicleBaseInfo();

                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 企业名称
                String companyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                Company companyInfo = companyNameMap.get(companyName);
                // 区县
                String countyName = Objects.nonNull(row.getCell(2)) ? row.getCell(2).toString() : StringUtils.EMPTY;
                Region region = regionMap.get(countyName);
                Long regionId = null;
                if (Objects.nonNull(region)) {
                    regionId = region.getId();
                }
                String displayName = regionCacheHelper.getDisplayName(regionId);
                // 运营商
                String operatorName = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;
                Operator operator = operatorMap.get(operatorName);
                Long operatorId = null;
                if (Objects.nonNull(operator)) {
                    operatorId = operator.getId();
                }

                Long companyId = companyInfo.getId();
                if (Objects.nonNull(companyInfo)) {
                    // 获取最新一个公司信息
                    companyId = companyInfo.getId();
                } else if (!StrPool.EMPTY.equals(companyName)) {
                    // 没有该公司信息,新增企业信息
                    Company company = new Company();
                    company.setRegionId(regionId);
                    company.setRegionName(displayName);
                    company.setOperatorId(operatorId);
                    company.setCompanyType(1);
                    company.setName(companyName);
                    company.setSortName(companyName);
                    company.setCompanyStatus(0);
                    companyService.save(company);
                    companyId = company.getId();
                    // 更新map
                    companyNameMap.put(companyName,company);
                }
                vehicleBaseInfo.setCarPlate(carPlate);
                vehicleBaseInfo.setOperatorId(operatorId);
                vehicleBaseInfo.setRegionId(regionId);
                vehicleBaseInfo.setRegionName(displayName);
                vehicleBaseInfo.setCompanyId(companyId);
                vehicleBaseInfo.setAuditState(2);
                vehicleBaseInfo.setUpdateOn(LocalDateTime.now());
                vehicleBaseInfo.setCreateOn(LocalDateTime.now());
                vehicleBaseInfoService.save(vehicleBaseInfo);
                System.out.println("准备更新 " + i + "条:" + JSON.toJSONString(vehicleBaseInfo));
            }
        }
    }

    @Test
    public void testRegion(){
        RegionDTO regionDTO = regionCacheHelper.getByRegionId(6L);
        String regionName = regionDTO.getRegionName();
        System.out.println(regionName);
    }

    //public fileout void main(String[] args) {
    //    Date date = new Date();
    //    System.out.println(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
    //}

    //这个下面是单独函数
    public static Workbook readExcel(InputStream is, String extString) {
        Workbook wb = null;
        extString = extString.substring(extString.lastIndexOf("."));
        try {
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                return wb = new XSSFWorkbook(is);
            } else {
                return wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }


    @Test
    public void test2() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\运营商车辆.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> companySet = new HashSet<>();
            HashSet<String> operatorSet = new HashSet<>();
            HashSet<String> countySet = new HashSet<>();

            int count = 0;
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 根据企业名查询企业信息
            Map<String, Company> companyNameMap = companyService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            companySet.add(v.getName()))
                    .collect(Collectors.toMap(Company::getName, v -> v));

            //获取最大列数
            List<NdVehicleBaseInfo> list = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {

                NdVehicleBaseInfo vehicleBaseInfo = new NdVehicleBaseInfo();

                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 企业名称
                String companyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                // 企业信息不存在
                if (!companyNameMap.containsKey(companyName)){
                   log.info("该车牌号：" + carPlate + "没有公司信息");
                    Company com = new Company();
                    com.setName(companyName);
                    com.setSortName(companyName);
                    com.setCompanyStatus(0);
                    companyService.save(com);
                    // 更新map
                    companyNameMap.put(companyName,com);
                }
                if (!vehicleBaseInfoMap.containsKey(carPlate)) {
                    Company company = companyNameMap.get(companyName);
                    vehicleBaseInfo.setCarPlate(carPlate);
                    vehicleBaseInfo.setCompanyId(company.getId());
                    vehicleBaseInfo.setOperatorId(company.getOperatorId());
                    vehicleBaseInfo.setRegionId(company.getRegionId());
                    vehicleBaseInfo.setRegionName(company.getRegionName());
                    vehicleBaseInfo.setAuditState(2);
                    list.add(vehicleBaseInfo);
                    count++;
                }
            }
            System.out.println("新增的车辆数为：" + count);
            HashSet<String> set = new HashSet<>();
            List<NdVehicleBaseInfo> infos =
                list.stream().filter(c -> set.add(c.getCarPlate())).collect(Collectors.toList());
            System.out.println("去重后的总数为：" + infos.size());
            vehicleBaseInfoService.saveOrUpdateBatch(infos);
            System.out.println("更新完成");
            
        }

    }

    @Test
    public void test3() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\运营商车辆.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            HashSet<String> companySet = new HashSet<>();

            // 根据企业名查询企业信息
            Map<String, Company> companyNameMap = companyService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            companySet.add(v.getName()))
                    .collect(Collectors.toMap(Company::getName, v -> v));

            //获取最大列数
            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);

                // 企业名称
                String companyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                // 企业信息不存在
                if (!companyNameMap.containsKey(companyName)){
                    Company com = new Company();
                    com.setName(companyName);
                    com.setSortName(companyName);
                    com.setCompanyStatus(0);
                    companyService.save(com);
                }

            }
            System.out.println("更新完成");

        }

    }



    @Test
    public void test4() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\运营商车辆.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> companySet = new HashSet<>();

            int count = 0;
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 根据企业名查询企业信息
            Map<String, Company> companyNameMap = companyService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            companySet.add(v.getName()))
                    .collect(Collectors.toMap(Company::getName, v -> v));

            //获取最大列数
            List<NdVehicleBaseInfo> list = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 企业名称
                String companyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                if (vehicleBaseInfoMap.containsKey(carPlate)) {
                    NdVehicleBaseInfo vehicleBaseInfo = vehicleBaseInfoMap.get(carPlate);
                    Company company = companyNameMap.get(companyName);
                    vehicleBaseInfo.setCompanyId(company.getId());
                    list.add(vehicleBaseInfo);
                    count++;
                }
            }
            System.out.println("新增的车辆数为：" + count);
            HashSet<String> set = new HashSet<>();
            List<NdVehicleBaseInfo> infos =
                    list.stream().filter(c -> set.add(c.getCarPlate())).collect(Collectors.toList());
            System.out.println("去重后的总数为：" + infos.size());
            vehicleBaseInfoService.saveOrUpdateBatch(infos);
            System.out.println("更新完成");

        }

    }


    @Test
    public void test5() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\缺区域2.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> countySet = new HashSet<>();

            int count = 0;
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 根据区县名称获取区域ID
            Map<String, Region> regionMap = regionService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            !"市本级".equals(v.getName()) &&
                            countySet.add(v.getName()))
                    .collect(Collectors.toMap(Region::getName, v -> v));

            //获取最大列数
            List<NdVehicleBaseInfo> list = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {

                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 区县
                String countyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                Region region = regionMap.get(countyName);
                Long regionId = null;
                if (Objects.nonNull(region)) {
                    regionId = region.getId();
                }
                String displayName = regionCacheHelper.getDisplayName(regionId);
                if (carPlates.contains(carPlate)) {
                    NdVehicleBaseInfo baseInfo = vehicleBaseInfoMap.get(carPlate);
                    baseInfo.setRegionId(regionId);
                    baseInfo.setRegionName(displayName);
                    count++;
                    list.add(baseInfo);
                }
            }
            log.info("更新的车辆数量为：" + count);
            vehicleBaseInfoService.saveOrUpdateBatch(list);
            System.out.println("更新完成");
        }
    }

    @Test
    public void test6() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\车牌缺运营商.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> companySet = new HashSet<>();
            HashSet<String> operatorSet = new HashSet<>();
            HashSet<String> countySet = new HashSet<>();

            int count = 0;
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 根据运营商名称查询运营商信息
            Map<String, Operator> operatorMap = operatorService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            operatorSet.add(v.getName()))
                    .collect(Collectors.toMap(Operator::getName, v -> v));

            //获取最大列数
            List<NdVehicleBaseInfo> list = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {

                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 运营商
                String operatorName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                Operator operator = operatorMap.get(operatorName);
                Long operatorId = null;
                if (Objects.nonNull(operator)) {
                    operatorId = operator.getId();
                }

                if (carPlates.contains(carPlate)) {
                    NdVehicleBaseInfo baseInfo = vehicleBaseInfoMap.get(carPlate);
                    baseInfo.setOperatorId(operatorId);
                    count++;
                    list.add(baseInfo);
                }
            }
            log.info("更新的车辆数为：" + count);
            vehicleBaseInfoService.saveOrUpdateBatch(list);
            System.out.println("更新完成");
        }
    }

    @Test
    public void test7() throws FileNotFoundException {
        File f = new File("D:\\work\\file\\0510\\缺企业.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            HashSet<String> carPlates = new HashSet<>();
            HashSet<String> companySet = new HashSet<>();

            int count = 0;
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 根据企业名查询企业信息
            Map<String, Company> companyNameMap = companyService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getName()) &&
                            companySet.add(v.getName()))
                    .collect(Collectors.toMap(Company::getName, v -> v));

            //获取最大列数
            List<NdVehicleBaseInfo> list = new ArrayList<>();
            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 企业名称
                String companyName = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                Company companyInfo = companyNameMap.get(companyName);
                Long companyId = null;
                if (Objects.nonNull(companyInfo)) {
                    // 获取最新一个公司信息
                    companyId = companyInfo.getId();
                }
                if (carPlates.contains(carPlate)) {
                    NdVehicleBaseInfo baseInfo = vehicleBaseInfoMap.get(carPlate);
                    baseInfo.setCompanyId(companyId);
                    count++;
                    list.add(baseInfo);
                }
            }
            log.info("更新的车辆数为：" + count);
            vehicleBaseInfoService.saveOrUpdateBatch(list);
            System.out.println("更新完成");
        }
    }

    // 更新车辆设备表中车辆ID
    @Test
    public void test8(){
        Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

        List<NdVehicleEquipmentInfo> equipmentInfos = vehicleEquipmentInfoService.list();

        for (NdVehicleEquipmentInfo equipmentInfo : equipmentInfos) {
            String carPlate = equipmentInfo.getCarPlate();
            NdVehicleBaseInfo baseInfo = vehicleBaseInfoMap.get(carPlate);
            if (Objects.nonNull(baseInfo)) {
                equipmentInfo.setVehicleId(baseInfo.getId());
                if (Objects.nonNull(baseInfo.getVehicleType())){
                    equipmentInfo.setVehicleType(Long.valueOf(baseInfo.getVehicleType()));
                }
                vehicleEquipmentInfoService.updateById(equipmentInfo);
            }
        }
    }

    // 更新车辆区域信息
    @Test
    public void test9(){
        List<NdVehicleBaseInfo> baseInfos = vehicleBaseInfoService.list().stream()
                .filter(v -> Objects.isNull(v.getRegionId()))
                .collect(Collectors.toList());
        Map<Long, Company> companyMap = companyService.list().stream().collect(Collectors.toMap(Company::getId, c -> c));
        for (NdVehicleBaseInfo baseInfo : baseInfos) {
            Long companyId = baseInfo.getCompanyId();
            Company company = companyMap.get(companyId);
            Long regionId = company.getRegionId();
            String regionName = company.getRegionName();
            baseInfo.setRegionId(regionId);
            baseInfo.setRegionName(regionName);
            vehicleBaseInfoService.updateById(baseInfo);
        }
    }

    @Test
    public void test10() throws FileNotFoundException {
        File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\车辆类型数据.xlsx");
        Workbook wb = readExcel(new FileInputStream(f), f.getName());
        Row row = null;
        if (wb != null) {
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            HashSet<String> carPlates = new HashSet<>();
            // 根据车牌号查询车辆信息
            Map<String, NdVehicleBaseInfo> vehicleBaseInfoMap = vehicleBaseInfoService.list().stream()
                    .filter(v -> !StrPool.EMPTY.equals(v.getCarPlate()) &&
                            carPlates.add(v.getCarPlate()))
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            //获取最大列数
            for (int i = 1; i < rownum; i++) {
                row = sheet.getRow(i);

                // 车牌号
                String carPlate = Objects.nonNull(row.getCell(0)) ? row.getCell(0).toString() : StringUtils.EMPTY;
                // 车辆类型
                String vehicleType = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;
                // 审核时间
                LocalDateTime dateTime = LocalDateTime.now();
                Cell cell = row.getCell(2);
                if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
                    Date dateValue = cell.getDateCellValue();
                    // 处理日期值
                    dateTime = NdDateUtils.date2LocalDateTime(dateValue);
                    if (vehicleBaseInfoMap.containsKey(carPlate)) {
                        NdVehicleBaseInfo baseInfo = vehicleBaseInfoMap.get(carPlate);
                        baseInfo.setAuditTime(dateTime);
                        vehicleBaseInfoService.updateById(baseInfo);
                    }
                }

            }
        }
    }


    private Long lastIndex = 626L;
    private Integer lastCount = 626;
    @Test
    public void test11(){
        readLastIndex();
        System.out.println(lastIndex);
        System.out.println(lastCount);
    }
    private void readLastIndex() {
        File file = new File("C:\\Users\\CMM\\Desktop\\demo\\vehicleLicenseIndex.txt");
        try {
            List<String> dataList = FileUtil.readLines(file, StandardCharsets.UTF_8);
            if (CollUtil.isNotEmpty(dataList) && dataList.size() == 2){
                lastIndex = Long.valueOf(dataList.get(0));
                lastCount = Integer.valueOf(dataList.get(1));
            }
        } catch (Exception e) {
            log.info("读取下标和车牌号失败!");
        }
    }

    @Test
    public void test12(){

        LambdaQueryWrapper<NdVehicleBaseInfo> query =
                Wrappers.lambdaQuery(NdVehicleBaseInfo.class).ge(NdVehicleBaseInfo::getId, 1000)
                        .lt(NdVehicleBaseInfo::getId, 1000 + 2900 - 1401 + 1)
                        .select(NdVehicleBaseInfo::getCarPlate,NdVehicleBaseInfo::getId).isNotNull(NdVehicleBaseInfo::getCarPlate);
        List<NdVehicleBaseInfo> list = vehicleBaseInfoService.list(query);
        if (CollUtil.isNotEmpty(list)) {
            List<String> carPlateSet = list.stream()
                    .sorted(Comparator.comparing(NdVehicleBaseInfo::getId))
                    .map(NdVehicleBaseInfo::getCarPlate)
                    .collect(Collectors.toList());
            String[] carPlates = carPlateSet.stream().toArray(String[]::new);
            System.out.println(carPlates[1499]);
            System.out.println(carPlates[0]);
        }
    }


    @Test
    public void test13(){
        Map<Long, String> infoMap =
                vehicleBaseInfoService.list(Wrappers.lambdaQuery(NdVehicleBaseInfo.class)
                                .select(NdVehicleBaseInfo::getId,NdVehicleBaseInfo::getCarPlate))
                        .stream().collect(Collectors.toMap(NdVehicleBaseInfo::getId, NdVehicleBaseInfo::getCarPlate));
        OptionalLong maxKey = infoMap.keySet().stream().mapToLong(Long::longValue).max();
        Long maxIndex = null;
        if (maxKey.isPresent()){
            maxIndex = maxKey.getAsLong();
        }
        // 校验下标对应的车牌号是否一致
        String carNo;
        carNo = infoMap.get(lastIndex);
        while (Objects.isNull(carNo)) {
            log.info("车辆id{}不存在,请检查该车辆信息是否已经被删除!", lastIndex);
            // 查找下一个车辆信息,直到车辆信息存在
            lastIndex++;
            if (Objects.nonNull(maxIndex) && lastIndex.compareTo(maxIndex) > 0){
                log.info("系统中没有待查询的车牌号");
                return;
            }
            carNo = infoMap.get(lastIndex);
        }

        // 从车辆信息表中从上次执行中断的下标获取一定数量的车牌号
        int cnt = 2950 - lastCount;
        LambdaQueryWrapper<NdVehicleBaseInfo> query =
                Wrappers.lambdaQuery(NdVehicleBaseInfo.class)
                        .ge(NdVehicleBaseInfo::getId, lastIndex)
                        .last("limit " + cnt)
                        .select(NdVehicleBaseInfo::getCarPlate, NdVehicleBaseInfo::getId)
                        .isNotNull(NdVehicleBaseInfo::getCarPlate);
        List<NdVehicleBaseInfo> list = vehicleBaseInfoService.list(query);
        log.info("此次任务车辆数量为:" + list.size());

        // 任务执行完毕后保存最终的数据下标
        TreeMap<Long, String> sortedMap = new TreeMap<>(infoMap);
        Map.Entry<Long, String> higherEntry = sortedMap.higherEntry(lastIndex);
        if (Objects.nonNull(higherEntry)){
            lastIndex = higherEntry.getKey();
        }
        log.info("下个下标为:" + lastIndex);
    }


    @Test
    public void test14(){
        VehicleArchivesPageQuery vehicleArchivesPageQuery = new VehicleArchivesPageQuery();
        LambdaQueryWrapper<NdVehicleBaseInfo> otherWrapper = Wrappers.lambdaQuery(NdVehicleBaseInfo.class);
        otherWrapper.eq(NdVehicleBaseInfo::getDeleted, DeletedEnum.NO_DELETED.getCode())
                .eq(Objects.nonNull(vehicleArchivesPageQuery.getCompanyId()),NdVehicleBaseInfo::getCompanyId,vehicleArchivesPageQuery.getCompanyId())
                .like(StringUtils.isNotBlank(vehicleArchivesPageQuery.getCarPlate()), NdVehicleBaseInfo::getCarPlate, vehicleArchivesPageQuery.getCarPlate())
                .like(StringUtils.isNotBlank(vehicleArchivesPageQuery.getUseCompanyName()),NdVehicleBaseInfo::getUseCompanyName,vehicleArchivesPageQuery.getUseCompanyName())
                .eq(Objects.nonNull(vehicleArchivesPageQuery.getVehicleType()), NdVehicleBaseInfo::getVehicleType, vehicleArchivesPageQuery.getVehicleType())
                .eq(Objects.nonNull(vehicleArchivesPageQuery.getVehicleStatus()),NdVehicleBaseInfo::getVehicleStatus,vehicleArchivesPageQuery.getVehicleStatus())
                .eq(Objects.nonNull(vehicleArchivesPageQuery.getRegionId()),NdVehicleBaseInfo::getRegionId,vehicleArchivesPageQuery.getRegionId())
                .eq(NdVehicleBaseInfo::getAuditState, AuditStateEnum.AUDIT_SUCCESS.getCode())
                .orderBy(Boolean.TRUE,Boolean.FALSE,NdVehicleBaseInfo::getUpdateOn);
        List<NdVehicleBaseInfo> baseInfos = vehicleBaseInfoService.list(otherWrapper).stream().filter(v -> Objects.isNull(v.getCompanyId())).collect(Collectors.toList());
        log.info("数量为{}",baseInfos.size());
    }
}
