package com.ningdatech.carapi.vehicle;

import java.io.*;
import java.nio.file.Files;
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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.archives.enums.AuditStateEnum;
import com.ningdatech.carapi.car.equipment.model.NdVehicleEquipmentChannels;
import com.ningdatech.carapi.car.equipment.model.NdVehicleEquipmentInfo;
import com.ningdatech.carapi.car.equipment.service.INdVehicleEquipmentChannelsService;
import com.ningdatech.carapi.car.equipment.service.INdVehicleEquipmentInfoService;
import com.ningdatech.carapi.car.vehicle.manage.VehicleManage;
import com.ningdatech.carapi.car.vehicle.model.NdVehicleBaseInfo;
import com.ningdatech.carapi.car.vehicle.service.IVehicleBaseInfoService;
import com.ningdatech.carapi.common.constant.VehicleTypeEnum;
import com.ningdatech.carapi.sys.entity.dto.CompanyDTO;
import com.ningdatech.carapi.sys.helper.CompaniesCacheHelper;

import cn.hutool.core.collection.CollUtil;

/**
 * @Classname OpenApiTest
 * @Description
 * @Date 2022/11/3 10:47
 * @Created by PoffyZhang
 */
public class CompanyVehicleTest extends AppTests {

    @Autowired
    private CompaniesCacheHelper companiesCacheHelper;

    @Autowired
    private VehicleManage vehicleManage;

    @Autowired
    private INdVehicleEquipmentInfoService equipmentInfoService;
    @Autowired
    private INdVehicleEquipmentChannelsService equipmentChannelsService;

    @Autowired
    private IVehicleBaseInfoService vehicleBaseInfoService;

    @Test
    public void test() throws FileNotFoundException {
        File f = new File("C:\\Users\\PoffyZhang\\Desktop\\海盐秦山混凝土有限公司.xls");
        Workbook wb = readExcel(new FileInputStream(f),f.getName());
        Assert.assertNotNull(wb);
        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);

                String carPlate = row.getCell(0).toString();
                String companyName = row.getCell(1).toString();
                String equipmentNo = row.getCell(2).toString();

                System.out.println(carPlate + "," + companyName + "," + equipmentNo);

                CompanyDTO company = companiesCacheHelper.getByName(companyName);
                if(Objects.isNull(company)){
                    continue;
                }
                NdVehicleBaseInfo vehicle = vehicleManage.queryByCarPlateAndCompanyId(carPlate,company.getId());
                //如果已经有该车辆
                if(Objects.isNull(vehicle)){
                    NdVehicleBaseInfo vehicleBaseInfo = new NdVehicleBaseInfo();
                    vehicleBaseInfo.setCompanyId(company.getId());
                    vehicleBaseInfo.setAuditState(AuditStateEnum.AUDIT_SUCCESS.getCode());
                    vehicleBaseInfo.setRegionId(company.getRegionId());
                    vehicleBaseInfo.setRegionName(company.getRegionName());
                    vehicleBaseInfo.setCarPlate(carPlate);
                    vehicleBaseInfo.setAuditMind("审核通过");
                    vehicleBaseInfo.setAuditBy(1L);
                    vehicleBaseInfo.setAuditTime(LocalDateTime.now());
                    vehicleBaseInfo.setStatus(1);
                    vehicleBaseInfoService.save(vehicleBaseInfo);
                    vehicle = vehicleBaseInfo;
                }

                //1.先删除该车辆下的设备
                equipmentInfoService.remove(Wrappers.lambdaQuery(NdVehicleEquipmentInfo.class)
                        .eq(NdVehicleEquipmentInfo::getVehicleId,vehicle.getId()));
                //2.添加车辆设备
                NdVehicleEquipmentInfo saveEntity = new NdVehicleEquipmentInfo();
                saveEntity.setCarPlate(carPlate);
                saveEntity.setRegionId(company.getRegionId());
                saveEntity.setEquipmentNo(equipmentNo);
                saveEntity.setEquipmentFunction("摄像头");
                saveEntity.setVehicleId(vehicle.getId());
                equipmentInfoService.save(saveEntity);
            }
        }
    }

    @Test
    public void test2() throws FileNotFoundException {
        File f = new File("C:\\Users\\PoffyZhang\\Desktop\\联调车辆信息新（网泽）(1)(1).xlsx");
        Workbook wb = readExcel(new FileInputStream(f),f.getName());
        Assert.assertNotNull(wb);
        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                String carPlate = row.getCell(0).toString();
                String companyName = row.getCell(1).toString();

                if(StringUtils.isBlank(carPlate)){
                    System.out.println("空车牌号行 跳过");
                    continue;
                }

                List<Cell>  equipmentNos = Lists.newArrayList();
                equipmentNos.add(row.getCell(2));
                equipmentNos.add(row.getCell(3));
                equipmentNos.add(row.getCell(4));
                equipmentNos.add(row.getCell(5));
                equipmentNos.add(row.getCell(6));
                equipmentNos.add(row.getCell(7));
                equipmentNos.add(row.getCell(8));
                equipmentNos.add(row.getCell(9));

                List<NdVehicleBaseInfo> vehicles = vehicleBaseInfoService.list(Wrappers.lambdaQuery(NdVehicleBaseInfo.class)
                        .eq(NdVehicleBaseInfo::getCarPlate,carPlate)
                        .eq(NdVehicleBaseInfo::getAuditState,AuditStateEnum.AUDIT_SUCCESS.getCode()));

                if(CollUtil.isEmpty(vehicles)){
                    NdVehicleBaseInfo vehicle = new NdVehicleBaseInfo();
                    CompanyDTO company = companiesCacheHelper.getByName(companyName);
                    if(Objects.isNull(company)){
                        System.out.println("没有这个公司 " + companyName);
                        continue;
                    }
                    vehicle.setCompanyId(company.getId());
                    vehicle.setAuditState(AuditStateEnum.AUDIT_SUCCESS.getCode());
                    vehicle.setRegionId(company.getRegionId());
                    vehicle.setRegionName(company.getRegionName());
                    vehicle.setCarPlate(carPlate);
                    vehicle.setAuditMind("审核通过");
                    vehicle.setAuditBy(1L);
                    vehicle.setAuditTime(LocalDateTime.now());
                    vehicle.setStatus(1);
                    if(vehicleBaseInfoService.save(vehicle)){
                        vehicles.add(vehicle);
                    }
                }
                //1.先删除该车辆下的设备
                equipmentInfoService.remove(Wrappers.lambdaQuery(NdVehicleEquipmentInfo.class)
                        .eq(NdVehicleEquipmentInfo::getVehicleId,vehicles.get(0).getId()));

                int j = 1;
                for(Cell equipmentNoCell : equipmentNos){
                    if(Objects.isNull(equipmentNoCell)){
                        System.out.println("空行跳过");
                        continue;
                    }
                    String equipmentNo = equipmentNoCell.toString();
                    System.out.println(carPlate + "," + companyName + "," + equipmentNo);

                    try{
                        NdVehicleBaseInfo vehicle = vehicles.get(0);

                        CompanyDTO company = companiesCacheHelper.getById(vehicle.getCompanyId());
                        if(Objects.isNull(company)){
                            System.out.println("没有此公司 !" + vehicle.getCompanyId());
                            continue;
                        }

                        //2.添加车辆设备
                        NdVehicleEquipmentInfo saveEntity = new NdVehicleEquipmentInfo();
                        saveEntity.setCarPlate(carPlate);
                        saveEntity.setRegionId(company.getRegionId());
                        saveEntity.setEquipmentNo(equipmentNo);
                        saveEntity.setName("摄像头" + j);
                        saveEntity.setVehicleId(vehicle.getId());
                        if(equipmentInfoService.save(saveEntity)){
                            System.out.println("设备添加成功 :" + JSON.toJSONString(saveEntity));
                        }
                        j++;
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Test
    public void importLvDao() throws FileNotFoundException {
        File f = new File("C:\\Users\\PoffyZhang\\Desktop\\绿岛上报的设备通道.xlsx");
        Workbook wb = readExcel(new FileInputStream(f),f.getName());
        Assert.assertNotNull(wb);
        List<String> hasNoCarPlate = Lists.newArrayList();
        Map<String,Integer> equipmentCountMap = Maps.newConcurrentMap();

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                String carPlate = row.getCell(0).toString();
                String companyName = row.getCell(1).toString();

                if(StringUtils.isBlank(carPlate)){
                    System.out.println("空车牌号行 跳过");
                    continue;
                }

                String equipmentNo = row.getCell(2).toString();

                List<NdVehicleBaseInfo> vehicles = vehicleBaseInfoService.list(Wrappers.lambdaQuery(NdVehicleBaseInfo.class)
                        .eq(NdVehicleBaseInfo::getCarPlate,carPlate)
                        .eq(NdVehicleBaseInfo::getAuditState,AuditStateEnum.AUDIT_SUCCESS.getCode()));

                if(CollUtil.isEmpty(vehicles)){
                    hasNoCarPlate.add(carPlate);
                    continue;
//                    NdVehicleBaseInfo vehicle = new NdVehicleBaseInfo();
//                    CompanyDTO company = companiesCacheHelper.getByName(companyName);
//                    if(Objects.isNull(company)){
//                        System.out.println("没有这个公司 " + companyName);
//                        continue;
//                    }
//                    vehicle.setCompanyId(company.getId());
//                    vehicle.setAuditState(AuditStateEnum.AUDIT_SUCESS.getCode());
//                    vehicle.setRegionId(company.getRegionId());
//                    vehicle.setRegionName(company.getRegionName());
//                    vehicle.setCarPlate(carPlate);
//                    vehicle.setAudiMind("审核通过");
//                    vehicle.setAuditBy(1L);
//                    vehicle.setAuditTime(LocalDateTime.now());
//                    vehicle.setStatus(1);
//                    if(vehicleBaseInfoService.save(vehicle)){
//                        vehicles.add(vehicle);
//                    }
                }
//                //1.先删除该车辆下的设备
//                equipmentInfoService.remove(Wrappers.lambdaQuery(NdVehicleEquipmentInfo.class)
//                        .eq(NdVehicleEquipmentInfo::getVehicleId,vehicles.get(0).getId()));

                if(Objects.isNull(equipmentNo)){
                    System.out.println("空行跳过");
                    continue;
                }
                System.out.println(carPlate + "," + companyName + "," + equipmentNo);

                try{
                    NdVehicleBaseInfo vehicle = vehicles.get(0);

                    CompanyDTO company = companiesCacheHelper.getById(vehicle.getCompanyId());
                    if(Objects.isNull(company)){
                        System.out.println("没有此公司 !" + vehicle.getCompanyId());
                        continue;
                    }

                    if(equipmentCountMap.containsKey(carPlate)){
                        equipmentCountMap.put(carPlate,equipmentCountMap.get(carPlate) + 1);
                    }else{
                        equipmentCountMap.put(carPlate,1);
                    }

                    //2.添加车辆设备
                    NdVehicleEquipmentInfo old = equipmentInfoService.getOne(Wrappers.lambdaQuery(NdVehicleEquipmentInfo.class)
                            .eq(NdVehicleEquipmentInfo::getEquipmentNo,equipmentNo).last("limit 1"));
                    NdVehicleEquipmentInfo saveEntity = new NdVehicleEquipmentInfo();
                    if(Objects.nonNull(old)){
                        saveEntity = old;
                    }
                    saveEntity.setCarPlate(carPlate);
                    saveEntity.setRegionId(company.getRegionId());
                    saveEntity.setEquipmentNo(equipmentNo);
                    saveEntity.setName("摄像头" + equipmentCountMap.get(carPlate));
                    saveEntity.setVehicleId(vehicle.getId());
                    if(equipmentInfoService.saveOrUpdate(saveEntity)){
                        System.out.println("设备添加成功 :" + JSON.toJSONString(saveEntity));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }


            if(!hasNoCarPlate.isEmpty()){
                System.out.println("系统不存在的车辆号 " + hasNoCarPlate.size() + "个");
                for(String carPlate : hasNoCarPlate){
                    System.out.print(carPlate + ",");
                }
            }
        }
    }

    @Test
    public void test4() throws IOException {
        File file = new File("C:\\Users\\PoffyZhang\\Desktop\\宁达文档\\联调车辆信息新（网泽）(1)(1).xlsx");
        InputStream is = Files.newInputStream(file.toPath());
        Workbook wb = readExcel(is,file.getName());

        Assert.assertNotNull(wb);

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                String carPlate = row.getCell(0).toString();
                String companyName = row.getCell(1).toString();
                System.out.println(carPlate + "," + companyName);
            }
        }
    }

    @Test
    public void test5() throws IOException {
        File file = new File("C:\\Users\\CMM\\Desktop\\数据导入-0314\\散办车辆信息汇总.xlsx");
        InputStream is = Files.newInputStream(file.toPath());
        Workbook wb = readExcel(is,file.getName());

        Assert.assertNotNull(wb);

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                String carPlate = row.getCell(0).toString();
                String equipmentNo = row.getCell(2).toString();
                System.out.println(carPlate + "," + equipmentNo);

                NdVehicleEquipmentInfo equipmentInfo = new NdVehicleEquipmentInfo();
                NdVehicleEquipmentInfo vehicleEquipmentInfo = equipmentInfoService.getOne(Wrappers.lambdaQuery(NdVehicleEquipmentInfo.class)
                        .eq(NdVehicleEquipmentInfo::getEquipmentNo, equipmentNo));
                if (Objects.nonNull(vehicleEquipmentInfo)){
                    System.out.println("设备号已存在!");
                    continue;
                }
                equipmentInfo.setCarPlate(carPlate);
                equipmentInfo.setEquipmentNo(equipmentNo);
                equipmentInfoService.save(equipmentInfo);
            }
        }
    }

    @Test
    public void importLvDaoExist() throws FileNotFoundException {
        File f = new File("C:\\Users\\PoffyZhang\\Desktop\\绿岛上报的设备通道.xlsx");
        Workbook wb = readExcel(new FileInputStream(f),f.getName());

        Set<String> hasNoCarPlate = Sets.newHashSet();

        Assert.assertNotNull(wb);

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                String carPlate = row.getCell(0).toString();
                String companyName = row.getCell(1).toString();

                if(StringUtils.isBlank(carPlate)){
                    System.out.println("空车牌号行 跳过");
                    continue;
                }

                String equipmentNo = row.getCell(2).toString();

                List<NdVehicleBaseInfo> vehicles = vehicleBaseInfoService.list(Wrappers.lambdaQuery(NdVehicleBaseInfo.class)
                        .eq(NdVehicleBaseInfo::getCarPlate,carPlate)
                        .eq(NdVehicleBaseInfo::getAuditState,AuditStateEnum.AUDIT_SUCCESS.getCode()));

                if(CollUtil.isEmpty(vehicles)){
                    hasNoCarPlate.add(carPlate);
                    continue;
                }
            }


            if(!hasNoCarPlate.isEmpty()){
                System.out.println("系统不存在的车辆号 " + hasNoCarPlate.size() + "个");
                for(String carPlate : hasNoCarPlate){
                    System.out.print(carPlate + ",");
                }
            }
        }
    }

    //这个下面是单独函数
    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 testTTTT(){
        List<String> carsOnline = Arrays.asList("1","2");
        Boolean update = vehicleBaseInfoService.update(Wrappers.lambdaUpdate(NdVehicleBaseInfo.class)
                .set(NdVehicleBaseInfo::getStatus, 0)
                .notIn(NdVehicleBaseInfo::getCarPlate, carsOnline));
        System.out.println(update);
    }


    // 导入设备编号
    @Test
    public void importEquipmentNo() throws FileNotFoundException {
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中导设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\吴霞设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\绿岛设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\马良设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\星软设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中交设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\网泽设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\道为尔设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\金华丽驰设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\嘉兴信达设备信息.xlsx");
        //File f = new File("D:\\work\\水泥车视频设备数据\\视频设备添加-星软-11.29.xlsx");
        File f = new File("D:\\work\\水泥车视频设备数据\\视频设备添加-星软-12.12.xlsx");

        Workbook wb = readExcel(new FileInputStream(f),f.getName());

        Set<String> hasNoCarPlate = Sets.newHashSet();

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);

            // 获取车辆基本信息
            Map<String, NdVehicleBaseInfo> baseInfoMap = vehicleBaseInfoService.list().stream()
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 获取车辆设备信息
            Map<String, List<NdVehicleEquipmentInfo>> equipmentMap = equipmentInfoService.list().stream()
                    .collect(Collectors.groupingBy(NdVehicleEquipmentInfo::getCarPlate));

            // 获取车辆通道信息
            Map<Long, NdVehicleEquipmentChannels> channelsMap = equipmentChannelsService.list().stream()
                    .collect(Collectors.toMap(NdVehicleEquipmentChannels::getEquipmentId, 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 equipmentNo = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;

                // 车辆类型
                //String vehicleType = Objects.nonNull(row.getCell(2)) ? row.getCell(2).toString() : StringUtils.EMPTY;
                // 车辆颜色
                //String vehicleColor = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;

                // SIM卡号
                //String simCardNo = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
                // 终端号
                //String endPoint = Objects.nonNull(row.getCell(5)) ? row.getCell(5).toString() : StringUtils.EMPTY;
                // 设备功能
                String equipmentFunction = Objects.nonNull(row.getCell(6)) ? row.getCell(6).toString() : StringUtils.EMPTY;
                // 设备名称
                String name = Objects.nonNull(row.getCell(7)) ? row.getCell(7).toString() : StringUtils.EMPTY;

                // 设备通道名称
                String channelName = Objects.nonNull(row.getCell(8)) ? row.getCell(8).toString() : StringUtils.EMPTY;
                // 平台ID
                String platformId = Objects.nonNull(row.getCell(9)) ? row.getCell(9).toString() : StringUtils.EMPTY;

                NdVehicleBaseInfo baseInfo = baseInfoMap.get(carPlate);
                if (Objects.isNull(baseInfo)){
                    // 车牌号不在系统中
                    hasNoCarPlate.add(carPlate);
                    continue;
                }
                // 更新车辆类型和车辆颜色
                //Integer type = VehicleTypeEnum.getCodeByDesc(vehicleType);
                //baseInfo.setVehicleType(type);
                //baseInfo.setPlateColor(vehicleColor);
                //baseInfo.setUpdateOn(LocalDateTime.now());
                //vehicleBaseInfoService.updateById(baseInfo);

                // 获取当前车牌号的设备信息
                NdVehicleEquipmentInfo equipmentInfo = new NdVehicleEquipmentInfo();
                List<NdVehicleEquipmentInfo> equipmentInfos = equipmentMap.get(carPlate);
                if (CollUtil.isNotEmpty(equipmentInfos)) {
                    // 当前车牌号已存在设备信息
                    Map<String, NdVehicleEquipmentInfo> equipmentInfoMap = equipmentInfos.stream()
                            .collect(Collectors.toMap(NdVehicleEquipmentInfo::getEquipmentNo, v -> v));
                    if (equipmentInfoMap.containsKey(equipmentNo)) {
                        // 已导入该设备信息
                        NdVehicleEquipmentInfo vehicleEquipmentInfo = equipmentInfoMap.get(equipmentNo);
                        equipmentInfo.setId(vehicleEquipmentInfo.getId());
                    }
                }

                // 保存设备信息
                //equipmentInfo.setSimCardNo(simCardNo);
                //equipmentInfo.setEndPoint(endPoint);
                equipmentInfo.setEquipmentNo(equipmentNo);
                equipmentInfo.setEquipmentFunction(equipmentFunction);
                equipmentInfo.setVehicleId(baseInfo.getId());
                equipmentInfo.setCarPlate(carPlate);
                equipmentInfo.setName(name);
                equipmentInfo.setPlatformId(platformId);
                equipmentInfo.setUpdateOn(LocalDateTime.now());
                equipmentInfoService.saveOrUpdate(equipmentInfo);

                // 保存通道名称
                NdVehicleEquipmentChannels equipmentChannel = new NdVehicleEquipmentChannels();
                Long equipmentId = equipmentInfo.getId();
                // 通道信息存在
                if (channelsMap.containsKey(equipmentId)){
                    NdVehicleEquipmentChannels channels = channelsMap.get(equipmentId);
                    equipmentChannel.setId(channels.getId());
                }
                equipmentChannel.setEquipmentId(equipmentId);
                equipmentChannel.setName(channelName);
                equipmentChannel.setUpdateOn(LocalDateTime.now());
                equipmentChannelsService.saveOrUpdate(equipmentChannel);
            }

            if(!hasNoCarPlate.isEmpty()){
                System.out.println("系统不存在的车辆号 " + hasNoCarPlate.size() + "个");
                for(String carPlate : hasNoCarPlate){
                    System.out.print(carPlate + ",");
                }
            }
        }
    }

    // 更新车辆类型
    @Test
    public void importEquipmentNo2() throws FileNotFoundException {
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中导设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\吴霞设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\绿岛设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\马良设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\星软设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中交设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\网泽设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\道为尔设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\金华丽驰设备信息.xlsx");
        File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\嘉兴信达设备信息.xlsx");

        Workbook wb = readExcel(new FileInputStream(f),f.getName());

        Set<String> hasNoCarPlate = Sets.newHashSet();

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);

            // 获取车辆基本信息
            Map<String, NdVehicleBaseInfo> baseInfoMap = vehicleBaseInfoService.list().stream()
                    .collect(Collectors.toMap(NdVehicleBaseInfo::getCarPlate, v -> v));

            // 获取车辆设备信息
            Map<String, List<NdVehicleEquipmentInfo>> equipmentMap = equipmentInfoService.list().stream()
                    .collect(Collectors.groupingBy(NdVehicleEquipmentInfo::getCarPlate));

            //获取最大列数
            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 equipmentNo = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;

                // 车辆类型
                String vehicleType = Objects.nonNull(row.getCell(2)) ? row.getCell(2).toString() : StringUtils.EMPTY;
                // 车辆颜色
                String vehicleColor = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;

                // SIM卡号
                String simCardNo = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
                // 终端号
                String endPoint = Objects.nonNull(row.getCell(5)) ? row.getCell(5).toString() : StringUtils.EMPTY;
                // 设备功能
                String equipmentFunction = Objects.nonNull(row.getCell(6)) ? row.getCell(6).toString() : StringUtils.EMPTY;
                // 设备名称
                String name = Objects.nonNull(row.getCell(7)) ? row.getCell(7).toString() : StringUtils.EMPTY;

                // 设备通道名称
                String channelName = Objects.nonNull(row.getCell(8)) ? row.getCell(8).toString() : StringUtils.EMPTY;
                // 平台ID
                String platformId = Objects.nonNull(row.getCell(9)) ? row.getCell(9).toString() : StringUtils.EMPTY;

                NdVehicleBaseInfo baseInfo = baseInfoMap.get(carPlate);
                if (Objects.isNull(baseInfo)){
                    // 车牌号不在系统中
                    hasNoCarPlate.add(carPlate);
                    continue;
                }
                // 更新车辆类型和车辆颜色
                Integer type = VehicleTypeEnum.getCodeByDesc(vehicleType);
                baseInfo.setVehicleType(type);
                baseInfo.setPlateColor(vehicleColor);
                baseInfo.setUpdateOn(LocalDateTime.now());
                vehicleBaseInfoService.updateById(baseInfo);

                // 获取当前车牌号的设备信息
                NdVehicleEquipmentInfo equipmentInfo = new NdVehicleEquipmentInfo();
                List<NdVehicleEquipmentInfo> equipmentInfos = equipmentMap.get(carPlate);
                if (CollUtil.isNotEmpty(equipmentInfos)) {
                    // 当前车牌号已存在设备信息
                    Map<String, NdVehicleEquipmentInfo> equipmentInfoMap = equipmentInfos.stream()
                            .collect(Collectors.toMap(NdVehicleEquipmentInfo::getEquipmentNo, v -> v));
                    if (equipmentInfoMap.containsKey(equipmentNo)) {
                        // 已导入该设备信息
                        NdVehicleEquipmentInfo vehicleEquipmentInfo = equipmentInfoMap.get(equipmentNo);
                        equipmentInfo.setId(vehicleEquipmentInfo.getId());
                    }
                }

                // 保存设备信息
                equipmentInfo.setSimCardNo(simCardNo);
                equipmentInfo.setEndPoint(endPoint);
                equipmentInfo.setEquipmentNo(equipmentNo);
                equipmentInfo.setEquipmentFunction(equipmentFunction);
                equipmentInfo.setVehicleId(baseInfo.getId());
                equipmentInfo.setCarPlate(carPlate);
                equipmentInfo.setName(name);
                equipmentInfo.setPlatformId(platformId);
                equipmentInfo.setUpdateOn(LocalDateTime.now());
                equipmentInfoService.saveOrUpdate(equipmentInfo);
            }

            if(!hasNoCarPlate.isEmpty()){
                System.out.println("系统不存在的车辆号 " + hasNoCarPlate.size() + "个");
                for(String carPlate : hasNoCarPlate){
                    System.out.print(carPlate + ",");
                }
            }
        }
    }


    // 导入平台ID
    @Test
    public void importPlatId() throws FileNotFoundException {
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中导设备信息.xlsx");
        File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\吴霞设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\绿岛设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\马良设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\星软设备信息.xlsx");
        //File f = new File("C:\\Users\\CMM\\Desktop\\水泥车数据初始化\\设备信息\\中交设备信息.xlsx");

        Workbook wb = readExcel(new FileInputStream(f),f.getName());

        Set<String> hasNoCarPlate = Sets.newHashSet();

        Row row;
        if(wb != null){
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);

            // 获取车辆设备信息
            Map<String, NdVehicleEquipmentInfo> equipmentMap = equipmentInfoService.list().stream()
                    .filter(e -> "video".equals(e.getEquipmentFunction()))
                    .collect(Collectors.toMap(e -> e.getCarPlate() + e.getEquipmentNo(), e -> e));

            //获取最大列数
            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 equipmentNo = Objects.nonNull(row.getCell(1)) ? row.getCell(1).toString() : StringUtils.EMPTY;

                // 车辆类型
                String vehicleType = Objects.nonNull(row.getCell(2)) ? row.getCell(2).toString() : StringUtils.EMPTY;
                // 车辆颜色
                String vehicleColor = Objects.nonNull(row.getCell(3)) ? row.getCell(3).toString() : StringUtils.EMPTY;

                // SIM卡号
                String simCardNo = Objects.nonNull(row.getCell(4)) ? row.getCell(4).toString() : StringUtils.EMPTY;
                // 终端号
                String endPoint = Objects.nonNull(row.getCell(5)) ? row.getCell(5).toString() : StringUtils.EMPTY;
                // 设备功能
                String equipmentFunction = Objects.nonNull(row.getCell(6)) ? row.getCell(6).toString() : StringUtils.EMPTY;
                // 设备名称
                String name = Objects.nonNull(row.getCell(7)) ? row.getCell(7).toString() : StringUtils.EMPTY;

                // 设备通道名称
                String channelName = Objects.nonNull(row.getCell(8)) ? row.getCell(8).toString() : StringUtils.EMPTY;

                // 平台ID
                String platformId = Objects.nonNull(row.getCell(9)) ? row.getCell(9).toString() : StringUtils.EMPTY;

                // 获取当前车牌号的设备信息
                NdVehicleEquipmentInfo info = equipmentMap.get(carPlate + equipmentNo);
                if (Objects.isNull(info)){
                    // 不在设备表中,跳过
                    continue;
                }
                info.setPlatformId(platformId);
                equipmentInfoService.updateById(info);


                //// 保存通道名称
                //NdVehicleEquipmentChannels equipmentChannel = new NdVehicleEquipmentChannels();
                //Long equipmentId = info.getId();
                //equipmentChannel.setEquipmentId(equipmentId);
                //equipmentChannel.setName(channelName);
                //equipmentChannel.setUpdateOn(LocalDateTime.now());
                //equipmentChannelsService.save(equipmentChannel);
            }
        }
    }
}
