package com.prolog.rdc.tms.service.fleetManagement.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.cs.platform.power.model.Organization;
import com.prolog.cs.platform.power.model.enterprise.Role;
import com.prolog.cs.platform.power.model.enterprise.User;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.config.enums.BelongToEnum;
import com.prolog.rdc.tms.config.enums.DepartmentBaseLinkBaseTypeEnum;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.RabbitMQConstant;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarriersMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseContainerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDeviceMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDriverMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBasePaperMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseVehicleMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseVehicleTypeMapper;
import com.prolog.rdc.tms.dao.mapper.TmsDepartmentBaseLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsDriverVehicleLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLineCarrierLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsVehicleContainerLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsVehicleDeviceLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsVehiclePaperLinkMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.StandardTemplateDetailsListDTO;
import com.prolog.rdc.tms.model.dto.basicData.LineByCarriersDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.ContainerDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeviceDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverAutomaticDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleAndDriverDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleAutomaticDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleDeviceLinkViewDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleDriverPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleImportPageDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleLinePagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePaperCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePaperPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePopupDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleTypeCheckDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarriersModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseContainerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDriverModel;
import com.prolog.rdc.tms.model.entity.line.TmsBasePaperModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseVehicleModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseVehicleTypeModel;
import com.prolog.rdc.tms.model.entity.line.TmsDepartmentBaseLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsDriverVehicleLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsLineCarrierLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsVehicleContainerLink;
import com.prolog.rdc.tms.model.entity.line.TmsVehicleDeviceLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsVehiclePaperLinkModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.ImportLogAddVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.ImportPageVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleAndVehicleVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleByIdVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleContainerLinkVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDeviceDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDeviceLinkAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDeviceLinkModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDriverDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDriverLinkAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDriverLinkModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleDriverPagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleLinePagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperByIdVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperLinkAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperLinkModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperPagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePaperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.VehicleImportStdVO;
import com.prolog.rdc.tms.rmi.feign.UserFeign;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicConfigure.ImportLogService;
import com.prolog.rdc.tms.service.basicConfigure.SysImportTemplethService;
import com.prolog.rdc.tms.service.fleetManagement.DeviceService;
import com.prolog.rdc.tms.service.fleetManagement.VehicleService;
import com.prolog.rdc.tms.service.fleetManagement.VehicleTypeService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import com.prolog.rdc.tms.util.FileUtils;
import com.prolog.rdc.tms.util.ImportUtils;
import com.prolog.rdc.tms.util.PublicMethods;
import com.prolog.wms.base.util.ExcelUtils;
import com.prolog.wms.base.util.JsonMapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 车队管理-车辆管理实现层
 *
 * @author Eton Woo
 * @date 2022/03/15
 */
@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private SysRefdatadService sysRefdatadService;

    @Autowired
    private TmsBaseVehicleTypeMapper tmsBaseVehicleTypeMapper;

    @Autowired
    private TmsBaseVehicleMapper tmsBaseVehicleMapper;

    @Autowired
    private SysCodeengineService sysCodeengineService;

    @Autowired
    private TmsDriverVehicleLinkMapper tmsDriverVehicleLinkMapper;

    @Autowired
    private TmsBaseDriverMapper tmsBaseDriverMapper;

    @Autowired
    private TmsBasePaperMapper tmsBasePaperMapper;

    @Autowired
    private TmsVehiclePaperLinkMapper tmsVehiclePaperLinkMapper;

    @Autowired
    private TmsLineCarrierLinkMapper tmsCarrierMapper;

    @Autowired
    private ImportLogService importLogService;

    @Autowired
    private SysImportTemplethService sysImportTemplethService;

    @Autowired
    private TmsBaseCarriersMapper tmsBaseCarriersMapper;

    @Autowired
    private TmsBaseContainerMapper tmsBaseContainerMapper;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private VehicleTypeService vehicleTypeService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private TmsLineCarrierLinkMapper tmsLineCarrierLinkMapper;

    @Autowired
    private TmsVehicleDeviceLinkMapper tmsVehicleDeviceLinkMapper;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private TmsVehicleContainerLinkMapper tmsVehicleContainerLinkMapper;

    @Autowired
    private TmsDepartmentBaseLinkMapper tmsDepartmentBaseLinkMapper;

    @Autowired
    private TmsBaseDeviceMapper tmsBaseDeviceMapper;

    /**
     * 车辆资料分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehiclePagingQueryDTO> pageVehicleData(VehiclePagingQueryVO vehiclePagingQueryVO) {
        ToolKits.isParamNull(vehiclePagingQueryVO, "车辆资料查询请求不为空");
        ToolKits.isParamNull(vehiclePagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            // 所属部门
            vehiclePagingQueryVO.setDepts(user.getDepts());
        } else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            vehiclePagingQueryVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
        } else {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 车辆类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE, null, null, null);

        // 车辆状态
        List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

        // 车型品牌
        List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE_BRAND, null, null, null);

        // 车型类别
        List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, null, null, null);

        // 里程管理
        List<Map<String, Object>> restMessage6 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MILEAGE_MANAGEMENT, null, null, null);

        // 分页
        PageUtils.startPage(vehiclePagingQueryVO.getPageNum(), vehiclePagingQueryVO.getPageSize());

        // 相应对象集合
        List<VehiclePagingQueryDTO> vehiclePagingQueryDTs = tmsBaseVehicleMapper.queryPageVehicleData(vehiclePagingQueryVO);

        for (VehiclePagingQueryDTO item : vehiclePagingQueryDTs) {
            // 响应对象
            item.setTypeName(PublicMethods.change(restMessage1, String.valueOf(item.getType())));
            item.setStatusName(PublicMethods.change(restMessage2, String.valueOf(item.getStatus())));
            item.setBrandName(PublicMethods.change(restMessage3, String.valueOf(item.getBrand())));
            item.setVehicleTypeCategoryName(PublicMethods.change(restMessage4, String.valueOf(item.getVehicleTypeCategory())));
            item.setMileageManagementName(PublicMethods.change(restMessage6, String.valueOf(item.getMileageManagement())));
        }

        Page<VehiclePagingQueryDTO> page = PageUtils.getPage(vehiclePagingQueryDTs);
        if (vehiclePagingQueryDTs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehiclePagingQueryDTO> p0 = (com.github.pagehelper.Page<VehiclePagingQueryDTO>) vehiclePagingQueryDTs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-已绑司机资料分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehicleDriverPagingQueryDTO> pageVehicleDriverData(VehicleDriverPagingQueryVO vehicleDriverPagingQueryVO) {
        ToolKits.isParamNull(vehicleDriverPagingQueryVO, "车辆-司机查询请求不为空");
        ToolKits.isParamNull(vehicleDriverPagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehicleDriverPagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            // 所属部门
            vehicleDriverPagingQueryVO.setDepts(user.getDepts());
        } else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {

            vehicleDriverPagingQueryVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
        } else {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        vehicleDriverPagingQueryVO.setEnterpriseId(user.getEnterpriseId());

        // 司机类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DRIVER_TYPE, null, null, null);

        // 分页
        PageUtils.startPage(vehicleDriverPagingQueryVO.getPageNum(), vehicleDriverPagingQueryVO.getPageSize());

        // 相应对象集合
        List<VehicleDriverPagingQueryDTO> vehicleDriverPagingQueryDTOs = tmsBaseVehicleMapper.queryPageVehicleDriverData(vehicleDriverPagingQueryVO);

        for (VehicleDriverPagingQueryDTO item : vehicleDriverPagingQueryDTOs) {
            // 响应对象
            item.setDriverTypeName(PublicMethods.change(restMessage1, String.valueOf(item.getDriverType())));
        }

        Page<VehicleDriverPagingQueryDTO> page = PageUtils.getPage(vehicleDriverPagingQueryDTOs);
        if (vehicleDriverPagingQueryDTOs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehicleDriverPagingQueryDTO> p0 = (com.github.pagehelper.Page<VehicleDriverPagingQueryDTO>) vehicleDriverPagingQueryDTOs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-未绑司机资料分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehicleDriverPagingQueryDTO> pageVehicleNotDriverData(VehicleDriverPagingQueryVO vehicleDriverPagingQueryVO) {
        ToolKits.isParamNull(vehicleDriverPagingQueryVO, "车辆-司机查询请求不为空");
        ToolKits.isParamNull(vehicleDriverPagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehicleDriverPagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 司机类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DRIVER_TYPE, null, null, null);

        // 分页
        PageUtils.startPage(vehicleDriverPagingQueryVO.getPageNum(), vehicleDriverPagingQueryVO.getPageSize());

        // 相应对象集合
        List<VehicleDriverPagingQueryDTO> vehicleDriverPagingQueryDTOs = new ArrayList<VehicleDriverPagingQueryDTO>();

        String[] driverIds = new String[]{};
        if (ToolKits.isNotEmpty(vehicleDriverPagingQueryVO.getVehicleId())) {
            // 查询司机车辆关联数据
            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r11 = Restrictions.eq("vehicleId", vehicleDriverPagingQueryVO.getVehicleId());
            criteria1.setRestriction(Restrictions.and(r11));
            List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

            // 获取司机ID
            driverIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getDriverId).distinct().collect(Collectors.toList())).stream().toArray(String[]::new);
        }

        // 查询对应司机
        Criteria criteria2 = Criteria.forClass(TmsBaseDriverModel.class);
        // Restriction r21 = ToolKits.isEmpty(driverIds) ? null :
        // Restrictions.notIn("id", driverIds);
        Restriction r22 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
        Restriction r23 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        Restriction r1 = null;
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            r1 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
        } else {
            // todo 后期替换单位ID
            r1 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));

        }
        criteria2.setRestriction(Restrictions.and(r22, r23, r1));
        criteria2.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria2);

        for (VehicleDriverPagingQueryDTO item : vehicleDriverPagingQueryDTOs) {
            // 响应对象
            item.setDriverTypeName(PublicMethods.change(restMessage1, String.valueOf(item.getDriverType())));
        }

        for (TmsBaseDriverModel item : tmsBaseDriverModels) {
            // 响应对象
            VehicleDriverPagingQueryDTO vehicleDriverPagingQueryDTO = new VehicleDriverPagingQueryDTO();
            BeanUtils.copyProperties(item, vehicleDriverPagingQueryDTO);
            vehicleDriverPagingQueryDTO.setDriverId(item.getId());
            vehicleDriverPagingQueryDTO.setDriverCode(item.getCode());
            vehicleDriverPagingQueryDTO.setDriverMemo(item.getMemo());
            vehicleDriverPagingQueryDTO.setDriverName(item.getName());
            vehicleDriverPagingQueryDTO.setDriverType(item.getType());
            vehicleDriverPagingQueryDTO.setDriverTypeName(PublicMethods.change(restMessage1, String.valueOf(item.getType())));
            vehicleDriverPagingQueryDTOs.add(vehicleDriverPagingQueryDTO);
        }

        Page<VehicleDriverPagingQueryDTO> page = PageUtils.getPage(vehicleDriverPagingQueryDTOs);
        if (tmsBaseDriverModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsBaseDriverModel> p0 = (com.github.pagehelper.Page<TmsBaseDriverModel>) tmsBaseDriverModels;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-线路资料分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehicleLinePagingQueryDTO> pageVehicleLineData(VehicleLinePagingQueryVO vehicleLinePagingQueryVO) {
        ToolKits.isParamNull(vehicleLinePagingQueryVO, "车辆-线路查询请求不为空");
        ToolKits.isParamNull(vehicleLinePagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehicleLinePagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 分页
        PageUtils.startPage(vehicleLinePagingQueryVO.getPageNum(), vehicleLinePagingQueryVO.getPageSize());

        // 相应对象集合
        List<VehicleLinePagingQueryDTO> vehicleLinePagingQueryDTOs = tmsBaseVehicleMapper.queryPageVehicleLineData(vehicleLinePagingQueryVO);

        Page<VehicleLinePagingQueryDTO> page = PageUtils.getPage(vehicleLinePagingQueryDTOs);
        if (vehicleLinePagingQueryDTOs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehicleLinePagingQueryDTO> p0 = (com.github.pagehelper.Page<VehicleLinePagingQueryDTO>) vehicleLinePagingQueryDTOs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-证件资料分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehiclePaperPagingQueryDTO> pageVehiclePaperData(VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
        ToolKits.isParamNull(vehiclePaperPagingQueryVO, "车辆-证件查询请求不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 证件类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.PAPER_TYPE, null, null, null);

        // 证件状态
        List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

        // 分页
        PageUtils.startPage(vehiclePaperPagingQueryVO.getPageNum(), vehiclePaperPagingQueryVO.getPageSize());

        // 相应对象集合
        List<VehiclePaperPagingQueryDTO> vehiclePaperPagingQueryDTOs = tmsBaseVehicleMapper.queryPageVehiclePaperData(vehiclePaperPagingQueryVO);

        for (VehiclePaperPagingQueryDTO item : vehiclePaperPagingQueryDTOs) {
            // 响应对象
            item.setTypeName(PublicMethods.change(restMessage1, String.valueOf(item.getType())));
            item.setStatusName(PublicMethods.change(restMessage2, String.valueOf(item.getStatus())));
        }

        Page<VehiclePaperPagingQueryDTO> page = PageUtils.getPage(vehiclePaperPagingQueryDTOs);
        if (vehiclePaperPagingQueryDTOs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehiclePaperPagingQueryDTO> p0 = (com.github.pagehelper.Page<VehiclePaperPagingQueryDTO>) vehiclePaperPagingQueryDTOs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-设备资料分页查询
     *
     * @param vehiclePaperPagingQueryVO
     * @return
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehicleDeviceLinkViewDTO> pageVehicleDeviceData(VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
        ToolKits.isParamNull(vehiclePaperPagingQueryVO, "车辆-设备查询请求不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 设备类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_TYPE, null, null, null);
        // 设备生产厂商
        List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_COMPANY, null, null, null);
        // 归属
        List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(BelongToEnum.BELONG_TO_TYPE, null, null, null);

        // 分页
        PageUtils.startPage(vehiclePaperPagingQueryVO.getPageNum(), vehiclePaperPagingQueryVO.getPageSize());
        // 相应对象集合
        List<VehicleDeviceLinkViewDTO> vehiclePaperPagingDeviceDTOs = tmsBaseVehicleMapper.pageVehicleDeviceData(vehiclePaperPagingQueryVO);

        for (VehicleDeviceLinkViewDTO deviceLinkViewDTO : vehiclePaperPagingDeviceDTOs) {
            deviceLinkViewDTO.setCompanyName(PublicMethods.change(restMessage2, String.valueOf(deviceLinkViewDTO.getCompany())));
            deviceLinkViewDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(deviceLinkViewDTO.getType())));
            deviceLinkViewDTO.setBelongToName(PublicMethods.change(restMessage3, String.valueOf(deviceLinkViewDTO.getBelongTo())));
        }
        Page<VehicleDeviceLinkViewDTO> page = PageUtils.getPage(vehiclePaperPagingDeviceDTOs);
        if (vehiclePaperPagingDeviceDTOs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehicleDeviceLinkViewDTO> p0 = (com.github.pagehelper.Page<VehicleDeviceLinkViewDTO>) vehiclePaperPagingDeviceDTOs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆-容器资料弹窗查询
     *
     * @param vo
     * @return
     */
    @Override
    public Page<ContainerDTO> pageVehicleContainerData(VehiclePaperPagingQueryVO vo) {
        ToolKits.isParamNull(vo.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vo.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vo.getPageSize(), "页数不为空");
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<ContainerDTO> containerDTOList = tmsBaseVehicleMapper.queryVehicleContainer(vo);
        if (CollectionUtil.isNotEmpty(containerDTOList)) {
            List<Long> baseId = containerDTOList.stream().map(ContainerDTO::getId).collect(Collectors.toList());
            Criteria criteria = Criteria.forClass(TmsDepartmentBaseLinkModel.class);
            Restriction r1 = Restrictions.in("baseId", baseId.toArray());
            Restriction r2 = Restrictions.eq("baseType", DepartmentBaseLinkBaseTypeEnum.CONTAINER.getCode());
            criteria.setRestriction(Restrictions.and(r1, r2));
            List<TmsDepartmentBaseLinkModel> tmsDepartmentBaseLinkModelList = tmsDepartmentBaseLinkMapper.findByCriteria(criteria);
            Map<String, List<TmsDepartmentBaseLinkModel>> baseMap = tmsDepartmentBaseLinkModelList.stream().collect(Collectors.groupingBy(TmsDepartmentBaseLinkModel::getBaseId));
            // 容器类型
            List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.CONTAINER_TYPE, null, null, null);
            // 设备状态
            List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);
            // 归属
            List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(BelongToEnum.BELONG_TO_TYPE, null, null, null);
            containerDTOList.forEach(dto -> {
                dto.setTypeName(PublicMethods.change(restMessage1, String.valueOf(dto.getType())));
                dto.setStatusName(PublicMethods.change(restMessage3, String.valueOf(dto.getStatus())));
                dto.setBelongToName(PublicMethods.change(restMessage4, String.valueOf(dto.getBelongTo())));
                List<TmsDepartmentBaseLinkModel> linkList = baseMap.get(dto.getId().toString());
                if (CollectionUtil.isNotEmpty(linkList)) {
                    dto.setDepartmentId(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentId).collect(Collectors.joining(",")));
                    dto.setDepartmentCode(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentCode).collect(Collectors.joining(",")));
                    dto.setDepartmentName(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentName).collect(Collectors.joining(",")));
                }
            });
        }
        return PageUtils.getPage(containerDTOList);
    }

    /**
     * 车辆-设备资料弹窗查询
     *
     * @param vehiclePaperPagingQueryVO
     * @return
     */
    @Override
    public Page<VehicleDeviceLinkViewDTO> pageVehicleDevicePopupData(VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
        ToolKits.isParamNull(vehiclePaperPagingQueryVO, "车辆-设备查询请求不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePaperPagingQueryVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 设备类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_TYPE, null, null, null);
        // 设备生产厂商
        List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_COMPANY, null, null, null);
        // 归属
        List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(BelongToEnum.BELONG_TO_TYPE, null, null, null);

        // 分页
        PageUtils.startPage(vehiclePaperPagingQueryVO.getPageNum(), vehiclePaperPagingQueryVO.getPageSize());
        // 相应对象集合
        List<VehicleDeviceLinkViewDTO> vehiclePaperPagingDeviceDTOs = tmsBaseVehicleMapper.pageVehicleDeviceData(vehiclePaperPagingQueryVO);
        for (VehicleDeviceLinkViewDTO deviceLinkViewDTO : vehiclePaperPagingDeviceDTOs) {
            deviceLinkViewDTO.setCompanyName(PublicMethods.change(restMessage2, String.valueOf(deviceLinkViewDTO.getCompany())));
            deviceLinkViewDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(deviceLinkViewDTO.getType())));
            deviceLinkViewDTO.setBelongToName(PublicMethods.change(restMessage3, String.valueOf(deviceLinkViewDTO.getBelongTo())));
        }
        Page<VehicleDeviceLinkViewDTO> page = PageUtils.getPage(vehiclePaperPagingDeviceDTOs);
        if (vehiclePaperPagingDeviceDTOs instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<VehicleDeviceLinkViewDTO> p0 = (com.github.pagehelper.Page<VehicleDeviceLinkViewDTO>) vehiclePaperPagingDeviceDTOs;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 设备资料弹窗查询
     *
     * @param vo
     * @return
     */
    @Override
    public Page<VehicleDeviceLinkViewDTO> pageDevicePopupData(VehiclePaperPagingQueryVO vo) {
        ToolKits.isParamNull(vo, "车辆-设备查询请求不为空");
        ToolKits.isParamNull(vo.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vo.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vo.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (StrUtil.isNotBlank(vo.getVehiclePlateNumber())) {
            VehiclePaperPagingQueryVO vehicleVO = new VehiclePaperPagingQueryVO();
            vehicleVO.setVehiclePlateNumber(vo.getVehiclePlateNumber());
            List<VehicleDeviceLinkViewDTO> vehicleDeviceLinkViewDTOList =
                    tmsBaseVehicleMapper.pageVehicleDeviceData(vehicleVO);
            if (CollectionUtil.isNotEmpty(vehicleDeviceLinkViewDTOList)) {
                vo.setDeviceIdList(vehicleDeviceLinkViewDTOList.stream().map(VehicleDeviceLinkViewDTO::getDeviceId).collect(Collectors.toList()));
            }
        }
        vo.setStatus(DataDictionaryConstant.STATUS_ENABLE);


        vo.setEnterpriseId(user.getEnterpriseId());
        // 自有账号
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            vo.setDepartmentIdList(user.getDepts());
        }
        // 承运商账号
        else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            vo.setAffiliationId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
        }
        // 司机账号
        else if (BussinessConstant.USER_TYPE_SJ.equals(user.getUserType())) {
            TmsBaseDriverModel driverModel = tmsBaseDriverMapper.findById(user.getUserid(), TmsBaseDriverModel.class);
            if (ObjectUtil.isNull(driverModel)) {
                throw new BizException("司机不存在！");
            }
            if (DataDictionaryConstant.DRIVER_TYPE_OWN.equals(driverModel.getType())) {
                vo.setDepartmentIdList(user.getDepts());
            } else {
                vo.setAffiliationId(driverModel.getAffiliationId());
            }
        }
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<VehicleDeviceLinkViewDTO> dtos = tmsBaseDeviceMapper.findDevicesByCondition(vo);
        if (CollectionUtil.isNotEmpty(dtos)) {
            List<Long> baseId = dtos.stream().map(VehicleDeviceLinkViewDTO::getDeviceId).collect(Collectors.toList());
            Criteria criteria = Criteria.forClass(TmsDepartmentBaseLinkModel.class);
            Restriction r1 = Restrictions.in("baseId", baseId.toArray());
            Restriction r2 = Restrictions.eq("baseType", DepartmentBaseLinkBaseTypeEnum.DEVICE.getCode());
            criteria.setRestriction(Restrictions.and(r1, r2));
            List<TmsDepartmentBaseLinkModel> tmsDepartmentBaseLinkModelList = tmsDepartmentBaseLinkMapper.findByCriteria(criteria);
            Map<String, List<TmsDepartmentBaseLinkModel>> baseMap =
                    tmsDepartmentBaseLinkModelList.stream().collect(Collectors.groupingBy(TmsDepartmentBaseLinkModel::getBaseId));
            // 设备类型
            List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_TYPE, null, null, null);
            // 设备生产厂商
            List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_COMPANY, null, null, null);
            // 设备状态
            List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);
            // 归属
            List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(BelongToEnum.BELONG_TO_TYPE, null, null, null);
            dtos.forEach(dto -> {
                dto.setCompanyName(PublicMethods.change(restMessage2, String.valueOf(dto.getCompany())));
                dto.setTypeName(PublicMethods.change(restMessage1, String.valueOf(dto.getType())));
                dto.setStatusName(PublicMethods.change(restMessage3, String.valueOf(dto.getStatus())));
                dto.setBelongToName(PublicMethods.change(restMessage4, String.valueOf(dto.getBelongTo())));
                List<TmsDepartmentBaseLinkModel> linkList = baseMap.get(dto.getDeviceId().toString());
                if (CollectionUtil.isNotEmpty(linkList)) {
                    dto.setDepartmentId(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentId).collect(Collectors.joining(",")));
                    dto.setDepartmentCode(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentCode).collect(Collectors.joining(",")));
                    dto.setDepartmentName(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentName).collect(Collectors.joining(",")));
                }
                dto.setId(String.valueOf(dto.getDeviceId()));
            });
        }
        Page<VehicleDeviceLinkViewDTO> page = PageUtils.getPage(dtos);
        return page;
    }

    /*
     * /** 车辆资料查看
     */
    @Override
    public VehicleCheckDTO getVehicleData(VehicleCheckVO vehicleCheckVO) {
        ToolKits.isParamNull(vehicleCheckVO, "车辆资料查看请求不为空");
        ToolKits.isParamNull(vehicleCheckVO.getVehicleId(), "车辆ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 里程管理
        List<Map<String, Object>> restMessage6 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MILEAGE_MANAGEMENT, null, null, null);

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = Restrictions.eq("id", vehicleCheckVO.getVehicleId());
        Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);

        if (tmsBaseVehicleModels.size() == 0) {
            throw new ParameterException("车辆ID【" + vehicleCheckVO.getVehicleId() + "】数据不存在，查询失败");
        } else if (tmsBaseVehicleModels.size() > 1) {
            throw new ParameterException("车辆ID【" + vehicleCheckVO.getVehicleId() + "】存在重复数据，查询失败");
        }

        // 车辆资料对象
        TmsBaseVehicleModel tmsBaseVehicleModel = tmsBaseVehicleModels.get(0);

        // 车型资料对象
        TmsBaseVehicleTypeModel tmsBaseVehicleTypeModel = new TmsBaseVehicleTypeModel();
        if (ToolKits.isNotEmpty(tmsBaseVehicleModel.getVehicleTypeId())) {
            Criteria typeCriteria = Criteria.forClass(TmsBaseVehicleTypeModel.class);
            Restriction typeR1 = Restrictions.eq("id", tmsBaseVehicleModel.getVehicleTypeId());
            Restriction typeR2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
            typeCriteria.setRestriction(Restrictions.and(typeR1, typeR2));
            List<TmsBaseVehicleTypeModel> tmsBaseVehicleTypeModels = tmsBaseVehicleTypeMapper.findByCriteria(typeCriteria);

            if (tmsBaseVehicleTypeModels.size() == 0) {
                throw new ParameterException("车型ID【" + tmsBaseVehicleModel.getVehicleTypeId() + "】数据不存在，查询失败");
            } else if (tmsBaseVehicleTypeModels.size() > 1) {
                throw new ParameterException("车型ID【" + tmsBaseVehicleModel.getVehicleTypeId() + "】存在重复数据，查询失败");
            }

            tmsBaseVehicleTypeModel = tmsBaseVehicleTypeModels.get(0);
        }
        // 响应对象
        VehicleCheckDTO vehicleCheckDTO = new VehicleCheckDTO();
        BeanUtils.copyProperties(tmsBaseVehicleModel, vehicleCheckDTO);

        if (ToolKits.isNotEmpty(tmsBaseVehicleTypeModel)) {
            vehicleCheckDTO.setBrand(tmsBaseVehicleTypeModel.getBrand());
            vehicleCheckDTO.setTypeCategory(tmsBaseVehicleTypeModel.getType());
            vehicleCheckDTO.setLength(tmsBaseVehicleTypeModel.getLength());
            vehicleCheckDTO.setWidth(tmsBaseVehicleTypeModel.getWidth());
            vehicleCheckDTO.setHeight(tmsBaseVehicleTypeModel.getHeight());
            vehicleCheckDTO.setMaxMileage(tmsBaseVehicleTypeModel.getMaxMileage());
            vehicleCheckDTO.setWeight(tmsBaseVehicleTypeModel.getWeight());
            vehicleCheckDTO.setVolume(tmsBaseVehicleTypeModel.getVolume());
            vehicleCheckDTO.setMileageManagementName(PublicMethods.change(restMessage6, String.valueOf(vehicleCheckDTO.getMileageManagement())));
        }

        return vehicleCheckDTO;
    }

    /**
     * 车型资料是否启用
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void whetherEnableVehicleData(VehicleWhetherEnableVO vehicleWhetherEnableVO) throws Exception {
        ToolKits.isParamNull(vehicleWhetherEnableVO, "车型资料是否启用请求不为空");
        ToolKits.isParamNull(vehicleWhetherEnableVO.getVehicleByIds(), "车辆ID集不为空");
        ToolKits.isParamNull(vehicleWhetherEnableVO.getStatus(), "状态不为空");

        Assert.isTrue(vehicleWhetherEnableVO.getStatus() == StatusConstant.STATUS_ENABLE || vehicleWhetherEnableVO.getStatus() == StatusConstant.STATUS_DISABLE, "车辆状态【" + vehicleWhetherEnableVO.getStatus() + "】错误");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 当前日期
        Date date = DateUtil.date();

        for (VehicleByIdVO vehicleByIdVO : vehicleWhetherEnableVO.getVehicleByIds()) {
            ToolKits.isParamNull(vehicleByIdVO.getVehicleId(), "车辆ID不为空");

            // 根据ID查询车辆
            TmsBaseVehicleModel tmsBaseVehicleModel = tmsBaseVehicleMapper.findById(vehicleByIdVO.getVehicleId(), TmsBaseVehicleModel.class);
            ToolKits.isParamNull(tmsBaseVehicleModel, "车辆ID【" + vehicleByIdVO.getVehicleId() + "】不存在");

            tmsBaseVehicleModel.setStatus(vehicleWhetherEnableVO.getStatus());
            tmsBaseVehicleModel.setUpdateperson(user.getNickname());
            tmsBaseVehicleModel.setUpdatetime(date);
            tmsBaseVehicleMapper.update(tmsBaseVehicleModel);
        }
    }

    /**
     * 车辆资料新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addVehicleData(VehicleAddVO vehicleAddVO) throws Exception {
        ToolKits.isParamNull(vehicleAddVO, "车辆资料新增请求不为空");
        ToolKits.isParamNull(vehicleAddVO.getCode(), "车辆编码不为空");
        ToolKits.isParamNull(vehicleAddVO.getPlateNumber(), "车牌号不为空");
        ToolKits.isParamNull(vehicleAddVO.getType(), "车辆类型不为空");
        ToolKits.isParamNull(vehicleAddVO.getVehicleTypeId(), "车型ID不为空");
        ToolKits.isParamNull(vehicleAddVO.getVehicleTypeCode(), "车型编码不为空");
        ToolKits.isParamNull(vehicleAddVO.getVehicleTypeName(), "车型编码不为空");
        ToolKits.isParamNull(vehicleAddVO.getDepartmentId(), "所属部门ID不能为空");
        ToolKits.isParamNull(vehicleAddVO.getDepartmentCode(), "所属部门编号不能为空");
        ToolKits.isParamNull(vehicleAddVO.getDepartmentName(), "所属部门名称不能为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 判断车辆编码是否已存在
        PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "code", vehicleAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseVehicleMapper, "车辆编码已存在");
        PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "code", vehicleAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseVehicleMapper, "车辆编码已存在，请直接启用");

        // 判断车牌号是否已存在
        PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "plateNumber", vehicleAddVO.getPlateNumber(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseVehicleMapper, "车牌号已存在");
        PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "plateNumber", vehicleAddVO.getPlateNumber(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseVehicleMapper, "车牌号已存在，请直接启用");

        // 车辆ID
        String vehicleId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_VEHICLE_ID, "L");
        ToolKits.isParamNull(vehicleId, "车辆ID生成失败");

        // 当前日期
        Date date = DateUtil.date();

        // 车辆资料对象保存
        TmsBaseVehicleModel tmsBaseVehicleModel = new TmsBaseVehicleModel();
        BeanUtils.copyProperties(vehicleAddVO, tmsBaseVehicleModel);
        tmsBaseVehicleModel.setId(Long.valueOf(vehicleId));
        tmsBaseVehicleModel.setStatus(StatusConstant.STATUS_ENABLE);
        tmsBaseVehicleModel.setSource(DataDictionaryConstant.SOURCE_ADD);
        tmsBaseVehicleModel.setEnterpriseId(user.getEnterpriseId());
        tmsBaseVehicleModel.setEnterpriseName(user.getEnterpriseName());
        tmsBaseVehicleModel.setCreateperson(user.getNickname());
        tmsBaseVehicleModel.setCreatetime(date);
        tmsBaseVehicleMapper.save(tmsBaseVehicleModel);

        // 车辆司机关联不为空新增
        if (ToolKits.isNotEmpty(vehicleAddVO.getVehicleDriverLinks())) {
            for (VehicleDriverLinkAddVO vehicleDriverLink : vehicleAddVO.getVehicleDriverLinks()) {
                ToolKits.isParamNull(vehicleDriverLink.getDriverId(), "司机ID不为空");
                ToolKits.isParamNull(vehicleDriverLink.getDriverCode(), "司机编码不为空");
                ToolKits.isParamNull(vehicleDriverLink.getDriverName(), "司机名称不为空");

                // 司机车辆关联保存
                TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
                tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
                tmsDriverVehicleLinkModel.setDriverId(vehicleDriverLink.getDriverId());
                tmsDriverVehicleLinkModel.setDriverCode(vehicleDriverLink.getDriverCode());
                tmsDriverVehicleLinkModel.setDriverName(vehicleDriverLink.getDriverName());
                tmsDriverVehicleLinkModel.setVehicleId(Long.valueOf(vehicleId));
                tmsDriverVehicleLinkModel.setVehicleCode(vehicleAddVO.getCode());
                tmsDriverVehicleLinkModel.setVehiclePlateNumber(vehicleAddVO.getPlateNumber());
                tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
                tmsDriverVehicleLinkModel.setCreatetime(date);
                tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);
            }
        }

        // 车辆证件关联不为空新增
        if (ToolKits.isNotEmpty(vehicleAddVO.getVehiclePaperLinks())) {
            for (VehiclePaperLinkAddVO vehiclePaperLink : vehicleAddVO.getVehiclePaperLinks()) {
                ToolKits.isParamNull(vehiclePaperLink.getId(), "证件ID不为空");
                ToolKits.isParamNull(vehiclePaperLink.getCode(), "证件编码不为空");
                ToolKits.isParamNull(vehiclePaperLink.getType(), "证件类型不为空");

                // 车辆证件保存
                TmsBasePaperModel tmsBasePaperModel = new TmsBasePaperModel();
                BeanUtils.copyProperties(vehiclePaperLink, tmsBasePaperModel);
                tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_VEHICLE);
                tmsBasePaperModel.setPaperObjectId(tmsBaseVehicleModel.getId());
                tmsBasePaperModel.setPaperObjectCode(tmsBaseVehicleModel.getCode());
                tmsBasePaperModel.setPaperObjectName(tmsBaseVehicleModel.getPlateNumber());
                tmsBasePaperModel.setStatus(StatusConstant.STATUS_ENABLE);
                tmsBasePaperModel.setEnterpriseId(user.getEnterpriseId());
                tmsBasePaperModel.setEnterpriseName(user.getEnterpriseName());
                tmsBasePaperModel.setCreateperson(user.getNickname());
                tmsBasePaperModel.setCreatetime(date);
                tmsBasePaperModel.setUpdateperson(user.getNickname());
                tmsBasePaperModel.setUpdatetime(date);
                tmsBasePaperMapper.save(tmsBasePaperModel);

                // 车辆证件关联保存
                TmsVehiclePaperLinkModel tmsVehiclePaperLinkModel = new TmsVehiclePaperLinkModel();
                tmsVehiclePaperLinkModel.setLineid(ToolKits.getUUID());
                tmsVehiclePaperLinkModel.setVehicleId(Long.valueOf(vehicleId));
                tmsVehiclePaperLinkModel.setVehicleCode(vehicleAddVO.getCode());
                tmsVehiclePaperLinkModel.setVehiclePlateNumber(vehicleAddVO.getPlateNumber());
                tmsVehiclePaperLinkModel.setPaperId(tmsBasePaperModel.getId());
                tmsVehiclePaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
                tmsVehiclePaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
                tmsVehiclePaperLinkModel.setCreateperson(user.getNickname());
                tmsVehiclePaperLinkModel.setCreatetime(date);
                tmsVehiclePaperLinkMapper.save(tmsVehiclePaperLinkModel);
            }
        }

        // 车辆设备关联不为空新增
        if (ToolKits.isNotEmpty(vehicleAddVO.getVehicleDeviceLinks())) {
            for (VehicleDeviceLinkAddVO deviceLinkAddVO : vehicleAddVO.getVehicleDeviceLinks()) {
                ToolKits.isParamNull(deviceLinkAddVO.getDeviceId(), "设备ID不为空");
                ToolKits.isParamNull(deviceLinkAddVO.getDeviceCode(), "设备编码不为空");
                ToolKits.isParamNull(deviceLinkAddVO.getDeviceName(), "设备名称不为空");

                // 车辆设备关联保存
                TmsVehicleDeviceLinkModel vehicleDeviceLinkModel = new TmsVehicleDeviceLinkModel();
                vehicleDeviceLinkModel.setId(ToolKits.getUUID());
                vehicleDeviceLinkModel.setDeviceId(deviceLinkAddVO.getDeviceId());
                vehicleDeviceLinkModel.setDeviceCode(deviceLinkAddVO.getDeviceCode());
                vehicleDeviceLinkModel.setDeviceName(deviceLinkAddVO.getDeviceName());

                vehicleDeviceLinkModel.setVehicleId(Long.valueOf(vehicleId));
                vehicleDeviceLinkModel.setVehicleCode(vehicleAddVO.getCode());
                vehicleDeviceLinkModel.setVehiclePlateNumber(vehicleAddVO.getPlateNumber());
                vehicleDeviceLinkModel.setCreateperson(user.getNickname());
                vehicleDeviceLinkModel.setCreatetime(date);
                tmsVehicleDeviceLinkMapper.save(vehicleDeviceLinkModel);
            }
        }
        // 车辆容器关联不为空新增
        if (ToolKits.isNotEmpty(vehicleAddVO.getVehicleContainerLinks())) {
            addVehicleContainerLink(vehicleAddVO.getVehicleContainerLinks(), Long.valueOf(vehicleId), vehicleAddVO.getCode(), vehicleAddVO.getPlateNumber());
        }
    }

    /**
     * 车辆资料修改
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyVehicleData(VehicleModifyVO vehicleModifyVO) throws Exception {
        ToolKits.isParamNull(vehicleModifyVO, "车辆资料修改请求不为空");
        ToolKits.isParamNull(vehicleModifyVO.getId(), "车辆ID不为空");
        ToolKits.isParamNull(vehicleModifyVO.getCode(), "车辆编码不为空");
        ToolKits.isParamNull(vehicleModifyVO.getPlateNumber(), "车牌号不为空");
        ToolKits.isParamNull(vehicleModifyVO.getType(), "车辆类型不为空");
        ToolKits.isParamNull(vehicleModifyVO.getVehicleTypeId(), "车型ID不为空");
        ToolKits.isParamNull(vehicleModifyVO.getVehicleTypeCode(), "车型编码不为空");
        ToolKits.isParamNull(vehicleModifyVO.getVehicleTypeName(), "车型编码不为空");
        ToolKits.isParamNull(vehicleModifyVO.getDepartmentId(), "所属部门ID不能为空");
        ToolKits.isParamNull(vehicleModifyVO.getDepartmentCode(), "所属部门编号不能为空");
        ToolKits.isParamNull(vehicleModifyVO.getDepartmentName(), "所属部门名称不能为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 当前日期
        Date date = DateUtil.date();

        // 根据ID查询车辆信息
        TmsBaseVehicleModel tmsBaseVehicleModel = tmsBaseVehicleMapper.findById(vehicleModifyVO.getId(), TmsBaseVehicleModel.class);
        ToolKits.isParamNull(tmsBaseVehicleModel, "车辆ID【" + vehicleModifyVO.getId() + "】不存在");

        Assert.isTrue(tmsBaseVehicleModel.getStatus() == StatusConstant.STATUS_ENABLE, "车辆已停用不能修改");

        if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            if (!tmsBaseVehicleModel.getDepartmentId().equals(vehicleModifyVO.getDepartmentId()) || !tmsBaseVehicleModel.getDepartmentCode().equals(vehicleModifyVO.getDepartmentCode()) || !tmsBaseVehicleModel.getDepartmentName().equals(vehicleModifyVO.getDepartmentName())) {
                ToolKits.isParamNull(user, "当前人员所属部门不可修改");
            }
            if (tmsBaseVehicleModel.getType() == DataDictionaryConstant.VEHICLE_TYPE_OWN) {
                if (!tmsBaseVehicleModel.getAffiliationId().equals(vehicleModifyVO.getAffiliationId()) || !tmsBaseVehicleModel.getAffiliationCode().equals(vehicleModifyVO.getAffiliationCode()) || !tmsBaseVehicleModel.getAffiliationName().equals(vehicleModifyVO.getAffiliationName())) {
                    ToolKits.isParamNull(user, "当前人员隶属机构不可修改");
                }
            } else {
                // todo 非自有账号若归属为自有，隶属机构为当前登录账号的单位名称
            }
        }

        // 车辆资料对象修改
        BeanUtils.copyProperties(vehicleModifyVO, tmsBaseVehicleModel);
        tmsBaseVehicleModel.setUpdateperson(user.getNickname());
        tmsBaseVehicleModel.setUpdatetime(date);
        tmsBaseVehicleMapper.update(tmsBaseVehicleModel);

        // 司机车辆关联修改不为空新增
        if (ToolKits.isNotEmpty(vehicleModifyVO.getVehicleDriverLinks())) {
            // 删除司机车辆关联
            Criteria criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r1 = Restrictions.eq("vehicleId", vehicleModifyVO.getId());
            criteria.setRestriction(Restrictions.and(r1));
            tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);

            for (VehicleDriverLinkModifyVO vehicleDriverLinkModifyVO : vehicleModifyVO.getVehicleDriverLinks()) {
                ToolKits.isParamNull(vehicleDriverLinkModifyVO.getDriverId(), "司机ID不为空");
                ToolKits.isParamNull(vehicleDriverLinkModifyVO.getDriverCode(), "司机编码不为空");
                ToolKits.isParamNull(vehicleDriverLinkModifyVO.getDriverName(), "司机名称不为空");

                // 司机车辆关联保存
                TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
                tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
                tmsDriverVehicleLinkModel.setDriverId(vehicleDriverLinkModifyVO.getDriverId());
                tmsDriverVehicleLinkModel.setDriverCode(vehicleDriverLinkModifyVO.getDriverCode());
                tmsDriverVehicleLinkModel.setDriverName(vehicleDriverLinkModifyVO.getDriverName());
                tmsDriverVehicleLinkModel.setVehicleId(tmsBaseVehicleModel.getId());
                tmsDriverVehicleLinkModel.setVehicleCode(tmsBaseVehicleModel.getCode());
                tmsDriverVehicleLinkModel.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
                tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
                tmsDriverVehicleLinkModel.setCreatetime(date);
                tmsDriverVehicleLinkModel.setUpdateperson(user.getNickname());
                tmsDriverVehicleLinkModel.setUpdatetime(date);
                tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);
            }
        }

        // 车辆证件关联不为空新增或修改
        if (ToolKits.isNotEmpty(vehicleModifyVO.getVehiclePaperLinks())) {
            for (VehiclePaperLinkModifyVO vehiclePaperLinkModifyVO : vehicleModifyVO.getVehiclePaperLinks()) {
                ToolKits.isParamNull(vehiclePaperLinkModifyVO.getId(), "证件ID不为空");

                // 根据ID查询证件对象
                TmsBasePaperModel tmsBasePaperModel = tmsBasePaperMapper.findById(vehiclePaperLinkModifyVO.getId(), TmsBasePaperModel.class);
                // 证件对象为空新增，否则修改
                if (ToolKits.isEmpty(tmsBasePaperModel)) {
                    ToolKits.isParamNull(vehiclePaperLinkModifyVO.getCode(), "证件编码不为空");
                    ToolKits.isParamNull(vehiclePaperLinkModifyVO.getType(), "证件类型不为空");

                    // 判断车辆证件编码是否已存在
                    PublicMethods.isThereAnyinformation(TmsBasePaperModel.class, "code", vehiclePaperLinkModifyVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBasePaperMapper, "车辆证件编码已存在");
                    PublicMethods.isThereAnyinformation(TmsBasePaperModel.class, "code", vehiclePaperLinkModifyVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBasePaperMapper, "车辆证件编码已存在，请直接启用");

                    // 车辆证件保存
                    tmsBasePaperModel = new TmsBasePaperModel();
                    BeanUtils.copyProperties(vehiclePaperLinkModifyVO, tmsBasePaperModel);
                    tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_VEHICLE);
                    tmsBasePaperModel.setPaperObjectId(tmsBaseVehicleModel.getId());
                    tmsBasePaperModel.setPaperObjectCode(tmsBaseVehicleModel.getCode());
                    tmsBasePaperModel.setPaperObjectName(tmsBaseVehicleModel.getPlateNumber());
                    tmsBasePaperModel.setStatus(StatusConstant.STATUS_ENABLE);
                    tmsBasePaperModel.setEnterpriseId(user.getEnterpriseId());
                    tmsBasePaperModel.setEnterpriseName(user.getEnterpriseName());
                    tmsBasePaperModel.setCreateperson(user.getNickname());
                    tmsBasePaperModel.setCreatetime(date);
                    tmsBasePaperModel.setUpdateperson(user.getNickname());
                    tmsBasePaperModel.setUpdatetime(date);
                    tmsBasePaperMapper.save(tmsBasePaperModel);

                    // 车辆证件关联保存
                    TmsVehiclePaperLinkModel tmsVehiclePaperLinkModel = new TmsVehiclePaperLinkModel();
                    tmsVehiclePaperLinkModel.setLineid(ToolKits.getUUID());
                    tmsVehiclePaperLinkModel.setVehicleId(tmsBaseVehicleModel.getId());
                    tmsVehiclePaperLinkModel.setVehicleCode(tmsBaseVehicleModel.getCode());
                    tmsVehiclePaperLinkModel.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
                    tmsVehiclePaperLinkModel.setPaperId(tmsBasePaperModel.getId());
                    tmsVehiclePaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
                    tmsVehiclePaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
                    tmsVehiclePaperLinkModel.setCreateperson(user.getNickname());
                    tmsVehiclePaperLinkModel.setCreatetime(date);
                    tmsVehiclePaperLinkMapper.save(tmsVehiclePaperLinkModel);
                } else {
                    Assert.isTrue(tmsBasePaperModel.getStatus() == StatusConstant.STATUS_ENABLE, "证件已停用不能修改");

                    tmsBasePaperModel.setIssuingAuthority(vehiclePaperLinkModifyVO.getIssuingAuthority());
                    tmsBasePaperModel.setIssuingDate(vehiclePaperLinkModifyVO.getIssuingDate());
                    tmsBasePaperModel.setStartDate(vehiclePaperLinkModifyVO.getStartDate());
                    tmsBasePaperModel.setEndDate(vehiclePaperLinkModifyVO.getEndDate());
                    tmsBasePaperModel.setPicture(vehiclePaperLinkModifyVO.getPicture());
                    tmsBasePaperModel.setMemo(vehiclePaperLinkModifyVO.getMemo());
                    tmsBasePaperModel.setUpdateperson(user.getNickname());
                    tmsBasePaperModel.setUpdatetime(date);
                    tmsBasePaperMapper.update(tmsBasePaperModel);
                }
            }
        }

        // 车辆设备关联修改不为空新增
        if (ToolKits.isNotEmpty(vehicleModifyVO.getVehicleDeviceLinks())) {
            // 删除车辆设备关联
            Criteria criteria = Criteria.forClass(TmsVehicleDeviceLinkModel.class);
            Restriction r1 = Restrictions.eq(TmsVehicleDeviceLinkModel::getVehicleId, vehicleModifyVO.getId());
            criteria.setRestriction(Restrictions.and(r1));
            tmsVehicleDeviceLinkMapper.deleteByCriteria(criteria);
            for (VehicleDeviceLinkModifyVO deviceLinkModifyVO : vehicleModifyVO.getVehicleDeviceLinks()) {
                ToolKits.isParamNull(deviceLinkModifyVO.getDeviceId(), "设备ID不为空");
                ToolKits.isParamNull(deviceLinkModifyVO.getDeviceCode(), "设备编码不为空");
                ToolKits.isParamNull(deviceLinkModifyVO.getDeviceName(), "设备名称不为空");

                // 车辆设备关联保存
                TmsVehicleDeviceLinkModel vehicleDeviceLinkModel = new TmsVehicleDeviceLinkModel();
                vehicleDeviceLinkModel.setId(ToolKits.getUUID());
                vehicleDeviceLinkModel.setDeviceId(deviceLinkModifyVO.getDeviceId());
                vehicleDeviceLinkModel.setDeviceCode(deviceLinkModifyVO.getDeviceCode());
                vehicleDeviceLinkModel.setDeviceName(deviceLinkModifyVO.getDeviceName());

                vehicleDeviceLinkModel.setVehicleId(Long.valueOf(vehicleModifyVO.getId()));
                vehicleDeviceLinkModel.setVehicleCode(vehicleModifyVO.getCode());
                vehicleDeviceLinkModel.setVehiclePlateNumber(vehicleModifyVO.getPlateNumber());
                vehicleDeviceLinkModel.setCreateperson(user.getNickname());
                vehicleDeviceLinkModel.setCreatetime(date);
                tmsVehicleDeviceLinkMapper.save(vehicleDeviceLinkModel);
            }
        }

        // 车辆容器关联修改不为空新增
        if (ToolKits.isNotEmpty(vehicleModifyVO.getVehicleContainerLinks())) {
            Criteria criteria = Criteria.forClass(TmsVehicleContainerLink.class);
            Restriction r1 = Restrictions.eq(TmsVehicleContainerLink::getVehicleId, vehicleModifyVO.getId());
            criteria.setRestriction(Restrictions.and(r1));
            tmsVehicleContainerLinkMapper.deleteByCriteria(criteria);
            addVehicleContainerLink(vehicleModifyVO.getVehicleContainerLinks(), Long.valueOf(vehicleModifyVO.getId()), vehicleModifyVO.getCode(), vehicleModifyVO.getPlateNumber());
        }
    }

    /**
     * 车辆资料删除
     *
     * @param list
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleData(List<VehicleDeleteVO> list) throws Exception {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        for (VehicleDeleteVO vo : list) {
            ToolKits.isParamNull(vo.getId(), "车辆ID不能为空");

            TmsBaseVehicleModel model = tmsBaseVehicleMapper.findById(vo.getId(), TmsBaseVehicleModel.class);
            ToolKits.isParamNull(model, "车辆ID[" + vo.getId() + "]有误,查询为空!");

            List<LineByCarriersDTO> dtoList = tmsCarrierMapper.findLineByVehicleSql(model.getPlateNumber());
            if (ToolKits.isNotEmpty(dtoList)) {
                String str = dtoList.stream().map(LineByCarriersDTO::getLineName).distinct().collect(Collectors.joining(","));
                throw new RuntimeException("车辆[" + dtoList.get(0).getPhoneNumber() + "]正在被线路[" + str + "]使用,不能删除!");
            }

            // 车辆绑定司机删除
            Criteria criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r1 = Restrictions.eq("vehicleId", model.getId());
            criteria.setRestriction(r1);
            tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);

            // 车辆绑定设备删除
            criteria = Criteria.forClass(TmsVehicleDeviceLinkModel.class);
            r1 = Restrictions.eq("vehicleId", model.getId());
            criteria.setRestriction(r1);
            tmsVehicleDeviceLinkMapper.deleteByCriteria(criteria);

            criteria = Criteria.forClass(TmsVehicleContainerLink.class);
            r1 = Restrictions.eq("vehicleId", model.getId());
            criteria.setRestriction(r1);
            tmsVehicleContainerLinkMapper.deleteByCriteria(criteria);

            // 关联删除
            criteria = Criteria.forClass(TmsVehiclePaperLinkModel.class);
            criteria.setRestriction(r1);
            List<TmsVehiclePaperLinkModel> tmsVehiclePaperLinkModels = tmsVehiclePaperLinkMapper.findByCriteria(criteria);
            if (ToolKits.isNotEmpty(tmsVehiclePaperLinkModels)) {
                List<Long> paperIds = tmsVehiclePaperLinkModels.stream().filter(o -> ToolKits.isNotEmpty(o.getPaperId())).distinct().map(TmsVehiclePaperLinkModel::getPaperId).collect(Collectors.toList());
                List<String> lineIds = tmsVehiclePaperLinkModels.stream().filter(o -> ToolKits.isNotEmpty(o.getLineid())).distinct().map(TmsVehiclePaperLinkModel::getLineid).collect(Collectors.toList());
                // 删除证件
                tmsBasePaperMapper.deleteByIds(paperIds.toArray(), TmsBasePaperModel.class);
                // 车辆绑定证件删除
                tmsVehiclePaperLinkMapper.deleteByIds(lineIds.toArray(), TmsVehiclePaperLinkModel.class);
            }

            // 删除对应线路承运方
            criteria = Criteria.forClass(TmsLineCarrierLinkModel.class);
            r1 = Restrictions.eq(TmsLineCarrierLinkModel::getVehiclePlateNumber, model.getPlateNumber());
            criteria.setRestriction(r1);
            tmsLineCarrierLinkMapper.deleteByCriteria(criteria);

            // 删除车辆
            tmsBaseVehicleMapper.deleteById(model.getId(), TmsBaseVehicleModel.class);
        }
    }

    /**
     * 车辆绑定司机删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleDriverData(List<VehicleDriverDeleteVO> vehicleDriverDeleteVOs) throws Exception {
        ToolKits.isParamNull(vehicleDriverDeleteVOs, "车辆绑定司机删除请求不为空");

        for (VehicleDriverDeleteVO vehicleDriverDeleteVO : vehicleDriverDeleteVOs) {
            ToolKits.isParamNull(vehicleDriverDeleteVO.getLineid(), "主键ID不为空");

            tmsDriverVehicleLinkMapper.deleteById(vehicleDriverDeleteVO.getLineid(), TmsDriverVehicleLinkModel.class);
        }
    }

    /**
     * 车辆绑定设备删除
     *
     * @param vehicleDeviceDeleteVOs
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleDeviceData(List<VehicleDeviceDeleteVO> vehicleDeviceDeleteVOs) throws Exception {
        ToolKits.isParamNull(vehicleDeviceDeleteVOs, "车辆绑定设备删除请求不为空");

        for (VehicleDeviceDeleteVO vehicleDeviceDeleteVO : vehicleDeviceDeleteVOs) {
            ToolKits.isParamNull(vehicleDeviceDeleteVO.getId(), "车辆设备表主键ID不为空");

            tmsVehicleDeviceLinkMapper.deleteById(vehicleDeviceDeleteVO.getId(), TmsVehicleDeviceLinkModel.class);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleContainerData(List<String> idList) {
        idList.forEach(id -> tmsVehicleContainerLinkMapper.deleteById(id, TmsVehicleContainerLink.class));
    }

    /**
     * 车辆-证件资料新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addVehiclePaperData(VehiclePaperAddVO vehiclePaperAddVO) throws Exception {
        ToolKits.isParamNull(vehiclePaperAddVO, "车辆证件资料新增请求不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getVehicleId(), "车辆ID不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getVehicleCode(), "车辆编码不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getPlateNumber(), "车牌号不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getId(), "证件ID不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getCode(), "证件编码不为空");
        ToolKits.isParamNull(vehiclePaperAddVO.getType(), "证件类型不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 判断车辆证件编码是否已存在
        PublicMethods.isThereAnyinformation(TmsBasePaperModel.class, "code", vehiclePaperAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBasePaperMapper, "车辆证件编码已存在");
        PublicMethods.isThereAnyinformation(TmsBasePaperModel.class, "code", vehiclePaperAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBasePaperMapper, "车辆证件编码已存在，请直接启用");

        // 当前日期
        Date date = DateUtil.date();

        // 车辆证件保存
        TmsBasePaperModel tmsBasePaperModel = new TmsBasePaperModel();
        BeanUtils.copyProperties(vehiclePaperAddVO, tmsBasePaperModel);
        tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_VEHICLE);
        tmsBasePaperModel.setPaperObjectId(vehiclePaperAddVO.getVehicleId());
        tmsBasePaperModel.setPaperObjectCode(vehiclePaperAddVO.getVehicleCode());
        tmsBasePaperModel.setPaperObjectName(vehiclePaperAddVO.getPlateNumber());
        tmsBasePaperModel.setStatus(StatusConstant.STATUS_ENABLE);
        tmsBasePaperModel.setEnterpriseId(user.getEnterpriseId());
        tmsBasePaperModel.setEnterpriseName(user.getEnterpriseName());
        tmsBasePaperModel.setCreateperson(user.getNickname());
        tmsBasePaperModel.setCreatetime(date);
        tmsBasePaperMapper.save(tmsBasePaperModel);

        // 车辆证件关联保存
        TmsVehiclePaperLinkModel tmsVehiclePaperLinkModel = new TmsVehiclePaperLinkModel();
        tmsVehiclePaperLinkModel.setLineid(ToolKits.getUUID());
        tmsVehiclePaperLinkModel.setVehicleId(vehiclePaperAddVO.getVehicleId());
        tmsVehiclePaperLinkModel.setVehicleCode(vehiclePaperAddVO.getVehicleCode());
        tmsVehiclePaperLinkModel.setVehiclePlateNumber(vehiclePaperAddVO.getPlateNumber());
        tmsVehiclePaperLinkModel.setPaperId(tmsBasePaperModel.getId());
        tmsVehiclePaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
        tmsVehiclePaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
        tmsVehiclePaperLinkModel.setCreateperson(user.getNickname());
        tmsVehiclePaperLinkModel.setCreatetime(date);
        tmsVehiclePaperLinkMapper.save(tmsVehiclePaperLinkModel);
    }

    /**
     * 车辆证件资料查看
     */
    @Override
    public VehiclePaperCheckDTO getVehiclePaperCheckData(VehiclePaperCheckVO vehiclePaperCheckVO) {
        ToolKits.isParamNull(vehiclePaperCheckVO, "车辆证件资料查看请求不为空");
        ToolKits.isParamNull(vehiclePaperCheckVO.getId(), "证件ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        Criteria criteria = Criteria.forClass(TmsBasePaperModel.class);
        Restriction r1 = Restrictions.eq("id", vehiclePaperCheckVO.getId());
        Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2));
        List<TmsBasePaperModel> tmsBasePaperModels = tmsBasePaperMapper.findByCriteria(criteria);

        if (tmsBasePaperModels.size() == 0) {
            throw new ParameterException("证件ID【" + vehiclePaperCheckVO.getId() + "】数据不存在，查询失败");
        } else if (tmsBasePaperModels.size() > 1) {
            throw new ParameterException("证件ID【" + vehiclePaperCheckVO.getId() + "】存在重复数据，查询失败");
        }

        // 车辆证件资料对象
        TmsBasePaperModel tmsBasePaperModel = tmsBasePaperModels.get(0);

        // 响应对象
        VehiclePaperCheckDTO vehiclePaperCheckDTO = new VehiclePaperCheckDTO();
        BeanUtils.copyProperties(tmsBasePaperModel, vehiclePaperCheckDTO);

        return vehiclePaperCheckDTO;
    }

    /**
     * 车辆证件资料修改
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyVehiclePaperData(VehiclePaperModifyVO vehiclePaperModifyVO) throws Exception {
        ToolKits.isParamNull(vehiclePaperModifyVO, "车辆证件资料修改请求不为空");
        ToolKits.isParamNull(vehiclePaperModifyVO.getId(), "车辆证件ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 当前日期
        Date date = DateUtil.date();

        // 根据ID查询司机证件信息
        TmsBasePaperModel tmsBasePaperModel = tmsBasePaperMapper.findById(vehiclePaperModifyVO.getId(), TmsBasePaperModel.class);
        ToolKits.isParamNull(tmsBasePaperModel, "证件ID【" + vehiclePaperModifyVO.getId() + "】不存在");

        Assert.isTrue(tmsBasePaperModel.getStatus() == StatusConstant.STATUS_ENABLE, "证件已停用不能修改");

        tmsBasePaperModel.setIssuingAuthority(vehiclePaperModifyVO.getIssuingAuthority());
        tmsBasePaperModel.setIssuingDate(vehiclePaperModifyVO.getIssuingDate());
        tmsBasePaperModel.setStartDate(vehiclePaperModifyVO.getStartDate());
        tmsBasePaperModel.setEndDate(vehiclePaperModifyVO.getEndDate());
        tmsBasePaperModel.setPicture(vehiclePaperModifyVO.getPicture());
        tmsBasePaperModel.setMemo(vehiclePaperModifyVO.getMemo());
        tmsBasePaperModel.setUpdateperson(user.getNickname());
        tmsBasePaperModel.setUpdatetime(date);
        tmsBasePaperMapper.update(tmsBasePaperModel);
    }

    /**
     * 车辆证件资料是否启用
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void whetherEnableVehiclePaperData(VehiclePaperWhetherEnableVO vehiclePaperWhetherEnableVO) throws Exception {
        ToolKits.isParamNull(vehiclePaperWhetherEnableVO, "车辆证件资料是否启用请求不为空");
        ToolKits.isParamNull(vehiclePaperWhetherEnableVO.getVehiclePaperIds(), "车辆证件ID集不为空");
        ToolKits.isParamNull(vehiclePaperWhetherEnableVO.getStatus(), "状态不为空");

        Assert.isTrue(vehiclePaperWhetherEnableVO.getStatus() == StatusConstant.STATUS_ENABLE || vehiclePaperWhetherEnableVO.getStatus() == StatusConstant.STATUS_DISABLE, "证件状态【" + vehiclePaperWhetherEnableVO.getStatus() + "】错误");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 当前日期
        Date date = DateUtil.date();

        for (VehiclePaperByIdVO vehiclePaperByIdVO : vehiclePaperWhetherEnableVO.getVehiclePaperIds()) {
            ToolKits.isParamNull(vehiclePaperByIdVO.getId(), "车辆证件ID不为空");

            // 根据ID查询证件对象
            TmsBasePaperModel tmsBasePaperModel = tmsBasePaperMapper.findById(vehiclePaperByIdVO.getId(), TmsBasePaperModel.class);
            ToolKits.isParamNull(tmsBasePaperModel, "车辆证件ID【" + vehiclePaperByIdVO.getId() + "】不存在");

            tmsBasePaperModel.setStatus(vehiclePaperWhetherEnableVO.getStatus());
            tmsBasePaperModel.setUpdateperson(user.getNickname());
            tmsBasePaperModel.setUpdatetime(date);
            tmsBasePaperMapper.update(tmsBasePaperModel);
        }
    }

    /**
     * 车辆证件资料删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehiclePaperData(List<VehiclePaperDeleteVO> vehiclePaperDeleteVOs) {
        ToolKits.isParamNull(vehiclePaperDeleteVOs, "车辆证件资料删除请求不为空");

        for (VehiclePaperDeleteVO vehiclePaperDeleteVO : vehiclePaperDeleteVOs) {
            ToolKits.isParamNull(vehiclePaperDeleteVO.getVehicleId(), "车辆ID不为空");
            ToolKits.isParamNull(vehiclePaperDeleteVO.getPaperId(), "证件ID不为空");

            // 证件资料删除
            long count = tmsBasePaperMapper.deleteById(vehiclePaperDeleteVO.getPaperId(), TmsBasePaperModel.class);
            if (count == 1) {
                // 车辆证件关联删除
                Criteria criteria = Criteria.forClass(TmsVehiclePaperLinkModel.class);
                Restriction r1 = Restrictions.eq("vehicleId", vehiclePaperDeleteVO.getVehicleId());
                Restriction r2 = Restrictions.eq("paperId", vehiclePaperDeleteVO.getPaperId());
                criteria.setRestriction(Restrictions.and(r1, r2));
                tmsVehiclePaperLinkMapper.deleteByCriteria(criteria);
            } else {
                throw new ParameterException("证件资料删除失败");
            }
        }
    }

    /**
     * 车辆资料弹窗
     */
    @SuppressWarnings("resource")
    @Override
    public Page<VehiclePopupDTO> popupVehicleData(VehiclePopupVO vehiclePopupVO) {
        ToolKits.isParamNull(vehiclePopupVO, "车型资料查询请求不为空");
        ToolKits.isParamNull(vehiclePopupVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePopupVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS) && !user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 相应对象集合
        List<VehiclePopupDTO> vehiclePopupDTOs = new ArrayList<VehiclePopupDTO>();

        // 车辆类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE, null, null, null);

        // 车辆ID
        Long[] vehicleIds = null;

        // 当司机ID不为空时
        if (ToolKits.isNotEmpty(vehiclePopupVO.getDriverId())) {
            // 查询司机车辆关联数据
            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r11 = Restrictions.eq("driverId", vehiclePopupVO.getDriverId());
            criteria1.setRestriction(Restrictions.and(r11));
            List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

            // 获取车辆ID
            vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);
        }

        // 当司机ID集合不为空时
        if (ToolKits.isNotEmpty(vehiclePopupVO.getDriverIds())) {
            // 查询司机车辆关联数据
            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r11 = Restrictions.in("driverId", vehiclePopupVO.getDriverIds().stream().toArray(Long[]::new));
            criteria1.setRestriction(Restrictions.and(r11));
            List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

            // 获取车辆ID
            vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);
        }

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = ToolKits.isEmpty(vehiclePopupVO.getCode()) ? null : Restrictions.like("code", "%" + vehiclePopupVO.getCode() + "%");
        Restriction r2 = ToolKits.isEmpty(vehiclePopupVO.getPlateNumber()) ? null : Restrictions.like("plateNumber", "%" + vehiclePopupVO.getPlateNumber() + "%");
        Restriction r3 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
        Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        Restriction r5 = null;

		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) || user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
            r5 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
        } else {
            r5 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));

        }

        Restriction r6 = ToolKits.isEmpty(vehiclePopupVO.getDriverId()) ? null : Restrictions.in("id", vehicleIds);
        Restriction r7 = ToolKits.isEmpty(vehiclePopupVO.getDriverIds()) ? null : Restrictions.in("id", vehicleIds);
        Restriction r8 = ToolKits.isEmpty(vehiclePopupVO.getVehicleTypeId()) ? null : Restrictions.eq(TmsBaseVehicleModel::getVehicleTypeId, vehiclePopupVO.getVehicleTypeId());
        Restriction r9 = ToolKits.isEmpty(vehiclePopupVO.getCarriersId()) ? null : Restrictions.eq(TmsBaseVehicleModel::getAffiliationId, vehiclePopupVO.getCarriersId());
        criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7,r8,r9));
        criteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        // 分页
        PageUtils.startPage(vehiclePopupVO.getPageNum(), vehiclePopupVO.getPageSize());
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);

        for (TmsBaseVehicleModel item : tmsBaseVehicleModels) {
            // 响应对象
            VehiclePopupDTO vehiclePopupDTO = new VehiclePopupDTO();
            BeanUtils.copyProperties(item, vehiclePopupDTO);
            vehiclePopupDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(vehiclePopupDTO.getType())));
            vehiclePopupDTO.setVehicleAscription(vehiclePopupDTO.getType());
            vehiclePopupDTO.setVehicleAscriptionName(vehiclePopupDTO.getTypeName());
            // 根据车辆车型ID查询车辆体积和载重
            if (ToolKits.isNotEmpty(item.getVehicleTypeId())) {
                VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
                vehicleTypeCheckVO.setVehicleTypeId(item.getVehicleTypeId());
                VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
                vehiclePopupDTO.setWeight(vehicleTypeCheckDTO.getWeight());
                vehiclePopupDTO.setVolume(vehicleTypeCheckDTO.getVolume());
                vehiclePopupDTO.setTypeCategory(vehicleTypeCheckDTO.getType());
                vehiclePopupDTO.setTypeCategoryName(vehicleTypeCheckDTO.getTypeName());
                //vehiclePopupDTO.setTypeName(vehicleTypeCheckDTO.getName());
                vehiclePopupDTO.setVehicleTypeId(vehicleTypeCheckDTO.getId());
                vehiclePopupDTO.setVehicleTypeCode(vehicleTypeCheckDTO.getCode());
                vehiclePopupDTO.setVehicleTypeName(vehicleTypeCheckDTO.getName());
                vehiclePopupDTO.setLength(vehicleTypeCheckDTO.getLength());
                vehiclePopupDTO.setWidth(vehicleTypeCheckDTO.getWidth());
                vehiclePopupDTO.setHeight(vehicleTypeCheckDTO.getHeight());
                vehiclePopupDTO.setVehicleSpec(NumberUtil.null2Zero(vehiclePopupDTO.getLength()).toString() + "*" + NumberUtil.null2Zero(vehiclePopupDTO.getWidth()).toString() + "*" + NumberUtil.null2Zero(vehiclePopupDTO.getHeight()).toString());
            }
            // 唯一设备信息
            Criteria criteriaDevice = Criteria.forClass(TmsVehicleDeviceLinkModel.class);
            r1 = Restrictions.eq(TmsVehicleDeviceLinkModel::getVehicleId, item.getId());
            criteriaDevice.setRestriction(Restrictions.and(r1));
            List<TmsVehicleDeviceLinkModel> deviceLinkModels = tmsVehicleDeviceLinkMapper.findByCriteria(criteriaDevice);
            if (ToolKits.isNotEmpty(deviceLinkModels) && deviceLinkModels.size() == 1) {
                vehiclePopupDTO.setDeviceId(deviceLinkModels.get(0).getDeviceId());
                vehiclePopupDTO.setDeviceCode(deviceLinkModels.get(0).getDeviceCode());
                vehiclePopupDTO.setDeviceName(deviceLinkModels.get(0).getDeviceName());
            }
            // 唯一容器信息
            Criteria criteriaContainer = Criteria.forClass(TmsVehicleContainerLink.class);
            r1 = Restrictions.eq(TmsVehicleContainerLink::getVehicleId, item.getId());
            criteriaContainer.setRestriction(Restrictions.and(r1));
            List<TmsVehicleContainerLink> containerLinkModels = tmsVehicleContainerLinkMapper.findByCriteria(criteriaContainer);
            if (ToolKits.isNotEmpty(containerLinkModels) && containerLinkModels.size() == 1) {
                vehiclePopupDTO.setContainerId(containerLinkModels.get(0).getContainerId());
                vehiclePopupDTO.setContainerCode(containerLinkModels.get(0).getContainerCode());
                vehiclePopupDTO.setContainerName(containerLinkModels.get(0).getContainerName());
            }
            vehiclePopupDTOs.add(vehiclePopupDTO);

            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            r1 = Restrictions.eq(TmsDriverVehicleLinkModel::getVehicleId, item.getId());
            criteria1.setRestriction(Restrictions.and(r1));
            List<TmsDriverVehicleLinkModel> driverVehicleLinkList = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);
            if (CollectionUtil.isNotEmpty(driverVehicleLinkList) && driverVehicleLinkList.size() == 1) {
                TmsBaseDriverModel driver = tmsBaseDriverMapper.findById(driverVehicleLinkList.get(0).getDriverId(), TmsBaseDriverModel.class);
                vehiclePopupDTO.setDriverId(driver.getId());
                vehiclePopupDTO.setDriverCode(driver.getCode());
                vehiclePopupDTO.setDriverName(driver.getName());
                vehiclePopupDTO.setDriverPhoneNumber(driver.getPhoneNumber());
                vehiclePopupDTO.setDriverMaxMileage(driver.getMaxMileage());
                vehiclePopupDTO.setDriverMaxDeliveryCustomer(driver.getMaxDeliveryCustomer());
                vehiclePopupDTO.setDriverMaxTravelTime(driver.getMaxTravelTime());
            }
        }

        Page<VehiclePopupDTO> page = PageUtils.getPage(vehiclePopupDTOs);
        if (tmsBaseVehicleModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsBaseVehicleModel> p0 = (com.github.pagehelper.Page<TmsBaseVehicleModel>) tmsBaseVehicleModels;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆资料
     */
    @SuppressWarnings("resource")
    @Override
    public List<VehiclePopupDTO> vehicleData(VehiclePopupVO vehiclePopupVO) {
        ToolKits.isParamNull(vehiclePopupVO, "车型资料查询请求不为空");
        List<VehiclePopupDTO> vehiclePopupDTOs = tmsBaseVehicleMapper.queryVehicleAndVehicleType(vehiclePopupVO);
        List<String> params = Lists.newArrayList(DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, // 车型类别
                DataDictionaryConstant.VEHICLE_TYPE //车辆类型

        );
        Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);
        for (VehiclePopupDTO vehiclePopupDTO : vehiclePopupDTOs) {
            // 车型资料对象
            vehiclePopupDTO.setTypeCategory(vehiclePopupDTO.getType());
            vehiclePopupDTO.setTypeCategoryName(ConvertorUtil.getName(dicData, DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, String.valueOf(vehiclePopupDTO.getType())));
            vehiclePopupDTO.setVehicleAscription(vehiclePopupDTO.getVehicleAscription());
            vehiclePopupDTO.setVehicleAscriptionName(ConvertorUtil.getName(dicData, DataDictionaryConstant.VEHICLE_TYPE, String.valueOf(vehiclePopupDTO.getVehicleAscription())));
            if (ToolKits.isNotEmpty(vehiclePopupVO.getConfigParam())) {
                // 设备
                if (vehiclePopupVO.getConfigParam().contains("1")) {
                    // 唯一设备信息
                    Criteria criteriaDevice = Criteria.forClass(TmsVehicleDeviceLinkModel.class);
                    Restriction r1 = Restrictions.eq(TmsVehicleDeviceLinkModel::getVehicleId, vehiclePopupDTO.getId());
                    criteriaDevice.setRestriction(Restrictions.and(r1));
                    List<TmsVehicleDeviceLinkModel> deviceLinkModels = tmsVehicleDeviceLinkMapper.findByCriteria(criteriaDevice);
                    if (ToolKits.isNotEmpty(deviceLinkModels) && deviceLinkModels.size() == 1) {
                        vehiclePopupDTO.setDeviceId(deviceLinkModels.get(0).getDeviceId());
                        vehiclePopupDTO.setDeviceCode(deviceLinkModels.get(0).getDeviceCode());
                        vehiclePopupDTO.setDeviceName(deviceLinkModels.get(0).getDeviceName());
                    }
                }
                if (vehiclePopupVO.getConfigParam().contains("2") || vehiclePopupVO.getConfigParam().contains("3")) {
                    // 唯一容器信息
                    Criteria criteriaContainer = Criteria.forClass(TmsVehicleContainerLink.class);
                    Restriction r1 = Restrictions.eq(TmsVehicleContainerLink::getVehicleId, vehiclePopupDTO.getId());
                    criteriaContainer.setRestriction(Restrictions.and(r1));
                    List<TmsVehicleContainerLink> containerLinkModels = tmsVehicleContainerLinkMapper.findByCriteria(criteriaContainer);
                    if (ToolKits.isNotEmpty(containerLinkModels) && containerLinkModels.size() == 1) {
                        vehiclePopupDTO.setContainerId(containerLinkModels.get(0).getContainerId());
                        vehiclePopupDTO.setContainerCode(containerLinkModels.get(0).getContainerCode());
                        vehiclePopupDTO.setContainerName(containerLinkModels.get(0).getContainerName());
                    }
                }
            }
        }
        return vehiclePopupDTOs;
    }

    /**
     * 车辆导入撤销
     *
     * @param batchno
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rollbackData(String batchno) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = Restrictions.eq("importBatchNumber", batchno);
        criteria.setRestriction(r1);
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);
        if (ToolKits.isEmpty(tmsBaseVehicleModels)) {
            throw new RuntimeException("批次号[" + batchno + "]错误,查询为空!");
        }
        for (TmsBaseVehicleModel model : tmsBaseVehicleModels) {

            List<LineByCarriersDTO> dtoList = tmsCarrierMapper.findLineByVehicleSql(model.getPlateNumber());
            if (ToolKits.isNotEmpty(dtoList)) {
                String str = dtoList.stream().map(LineByCarriersDTO::getLineName).distinct().collect(Collectors.joining(","));
                throw new RuntimeException("车辆[" + dtoList.get(0).getPhoneNumber() + "]正在被线路[" + str + "]使用,不能删除!");
            }

            // 车辆绑定司机删除
            criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            r1 = Restrictions.eq("vehicleId", model.getId());
            criteria.setRestriction(r1);
            tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);

            // 关联删除
            criteria = Criteria.forClass(TmsVehiclePaperLinkModel.class);
            criteria.setRestriction(r1);
            List<TmsVehiclePaperLinkModel> tmsVehiclePaperLinkModels = tmsVehiclePaperLinkMapper.findByCriteria(criteria);
            if (ToolKits.isNotEmpty(tmsVehiclePaperLinkModels)) {
                List<Long> paperIds = tmsVehiclePaperLinkModels.stream().filter(o -> ToolKits.isNotEmpty(o.getPaperId())).distinct().map(TmsVehiclePaperLinkModel::getPaperId).collect(Collectors.toList());
                List<String> lineIds = tmsVehiclePaperLinkModels.stream().filter(o -> ToolKits.isNotEmpty(o.getLineid())).distinct().map(TmsVehiclePaperLinkModel::getLineid).collect(Collectors.toList());
                if (ToolKits.isNotEmpty(paperIds)) {
                    // 删除证件
                    tmsBasePaperMapper.deleteByIds(paperIds.toArray(), TmsBasePaperModel.class);
                }
                if (ToolKits.isNotEmpty(lineIds)) {
                    // 车辆绑定证件删除
                    tmsVehiclePaperLinkMapper.deleteByIds(lineIds.toArray(), TmsVehiclePaperLinkModel.class);
                }
            }

            // 删除对应线路承运方
            criteria = Criteria.forClass(TmsLineCarrierLinkModel.class);
            r1 = Restrictions.eq("vehicleNo", model.getPlateNumber());
            criteria.setRestriction(r1);
            tmsCarrierMapper.deleteByCriteria(criteria);

            // 删除车辆
            tmsBaseVehicleMapper.deleteById(model.getId(), TmsBaseVehicleModel.class);
        }
    }

    /**
     * 车辆导入分页查询
     */
    @SuppressWarnings("resource")
    @Override
    public RestMessage<Object> pageVehicleByImportBatchNumberData(ImportPageVO importPageVO) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 相应对象集合
        List<VehicleImportPageDTO> vehicleImportPageDTOs = new ArrayList<VehicleImportPageDTO>();

        // 车辆类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE, null, null, null);

        // 车辆状态
        List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

        // 车型品牌
        List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE_BRAND, null, null, null);

        // 车型类别
        List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, null, null, null);

        // GPS设备生产厂商
        List<Map<String, Object>> restMessage5 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DEVICE_COMPANY, null, null, null);

        // 分页
        PageUtils.startPage(importPageVO.getPageNum(), importPageVO.getPageSize());

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = Restrictions.eq("importBatchNumber", importPageVO.getImportBatchNumber());
        Restriction r2 = Restrictions.eq("source", DataDictionaryConstant.SOURCE_EXCEL);
        Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2, r3));
        criteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);
        for (TmsBaseVehicleModel tmsBaseVehicleModel : tmsBaseVehicleModels) {
            // 响应对象
            VehicleImportPageDTO vehicleImportPageDTO = new VehicleImportPageDTO();
            BeanUtils.copyProperties(tmsBaseVehicleModel, vehicleImportPageDTO);
            vehicleImportPageDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(vehicleImportPageDTO.getType())));
            vehicleImportPageDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(vehicleImportPageDTO.getStatus())));
            vehicleImportPageDTO.setBrandName(PublicMethods.change(restMessage3, String.valueOf(vehicleImportPageDTO.getBrand())));
            vehicleImportPageDTO.setVehicleTypeCategoryName(PublicMethods.change(restMessage4, String.valueOf(vehicleImportPageDTO.getVehicleTypeCategory())));
            vehicleImportPageDTO.setDeviceCompanyName(PublicMethods.change(restMessage5, String.valueOf(vehicleImportPageDTO.getDeviceCompany())));

            vehicleImportPageDTOs.add(vehicleImportPageDTO);
        }

        Page<VehicleImportPageDTO> page = PageUtils.getPage(vehicleImportPageDTOs);
        if (tmsBaseVehicleModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsBaseVehicleModel> p0 = (com.github.pagehelper.Page<TmsBaseVehicleModel>) tmsBaseVehicleModels;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return RestMessage.newInstance(true, "查询成功", page);
    }

    // 车辆导入(新)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchImportVehicleData(MultipartFile file, String importBatchNumber) throws Exception {
        // 人员权限
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");
        // 导入文件判断
        FileUtils.multipartFileJudge(file);
        // 导入日志对象
        ImportLogAddVO logAddVO = new ImportLogAddVO();
        logAddVO.setImportObjNo(DataDictionaryConstant.VEHICLE_IMPORT);
        logAddVO.setImportBatchNumber(importBatchNumber);
        // 导入统计
        int count = 0;
        try {
            // 导入执行
            count = this.batchInsertProcess(file, importBatchNumber, user);
            logAddVO.setStatus(DataDictionaryConstant.IMPORT_STATUS_YES);
            logAddVO.setSuccessNum(count);
            logAddVO.setFailNum(0);
            importLogService.addImportLogData(logAddVO);
        } catch (Exception e) {
            Class<VehicleImportStdVO> importVOClass = VehicleImportStdVO.class;
            List<VehicleImportStdVO> importVOs = ExcelUtils.read(file, importVOClass);

            logAddVO.setStatus(DataDictionaryConstant.IMPORT_STATUS_NO);
            logAddVO.setSuccessNum(0);
            logAddVO.setFailNum(importVOs.size());
            logAddVO.setFailReason(e.getMessage());
            // 为了避免导入失败时导入日志的保存事务也被回滚,所以采用消息队列异步保存导入失败的日志
            amqpTemplate.convertAndSend(RabbitMQConstant.VEHICLE_IMPORT_LOG, logAddVO);
            e.printStackTrace();
            throw new ParameterException(e.getMessage());
        }
    }

    // 异步保存导入失败日志
    @RabbitListener(queues = RabbitMQConstant.VEHICLE_IMPORT_LOG)
    @Transactional(rollbackFor = Exception.class)
    public void saveFailImportLog(ImportLogAddVO logAddVO) {
        try {
            importLogService.addImportLogData(logAddVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 车辆导入过程(新)
     *
     * @param file
     * @param
     * @param
     * @return
     * @throws Exception
     */
    public int batchInsertProcess(MultipartFile file, String importBatchNumber, UserEntity user) {
        Date date = new Date();
        Properties properties = new Properties();
        // 待转换的对象
        Class<VehicleImportStdVO> importVOClass = VehicleImportStdVO.class;
        // 标准模板查询
        List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.VEHICLE_IMPORT);
        for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
            // 属性字段和注解属性字段值 键值对
            properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
        }
        // @ExcelProperty注解属性修改
        ImportUtils.setHeaderValue(properties, importVOClass);
        // EXCEL转对象
        List<VehicleImportStdVO> importVOs = null;
        try {
            importVOs = ExcelUtils.read(file, importVOClass);
        } catch (Exception e) {
            e.printStackTrace();

            throw new ParameterException("EXCEL转对象失败:" + e.getMessage());
        }

        ToolKits.isParamNull(importVOs, "EXCEL文件内容为空,请重新导入");
        log.info("送货员导入详情:{}", JsonMapper.toJson(importVOs));

        int i = 1;
        for (VehicleImportStdVO importVO : importVOs) {
            if (ToolKits.isEmpty(importVO.getCode())) {
                throw new PrologException("第" + i + "行导入数据,车辆编码不能为空");
            }
            if (ToolKits.isEmpty(importVO.getPlateNumber())) {
                throw new PrologException("第" + i + "行导入数据,车牌号不能为空");
            }
            if (ToolKits.isEmpty(importVO.getDepartmentName())) {
                throw new PrologException("第" + i + "行导入数据,部门名称不能为空");
            }
            if (ToolKits.isEmpty(importVO.getType())) {
                throw new PrologException("第" + i + "行导入数据,归属为必选项");
            }
            if (ToolKits.isEmpty(importVO.getVehicleTypeCode())) {
                throw new PrologException("第" + i + "行导入数据,车型编码不能为空");
            }
            if (ToolKits.isEmpty(importVO.getVehicleTypeName())) {
                throw new PrologException("第" + i + "行导入数据,车型名称不能为空");
            }
            i++;
        }
        // 验证导入文件中车辆编号是否重复
        Map<String, Long> codeMap = importVOs.stream().collect(Collectors.groupingBy(VehicleImportStdVO::getCode, Collectors.counting()));
        for (Map.Entry<String, Long> longEntry : codeMap.entrySet()) {
            if (longEntry.getValue() > 1) {
                throw new ParameterException(String.format("导入文件中，车型编号[%s]重复", longEntry.getKey()));
            }
        }

        // 验证导入文件中车牌号是否重复
        Map<String, Long> nameMap = importVOs.stream().collect(Collectors.groupingBy(VehicleImportStdVO::getPlateNumber, Collectors.counting()));
        for (Map.Entry<String, Long> longEntry : nameMap.entrySet()) {
            if (longEntry.getValue() > 1) {
                throw new ParameterException(String.format("导入文件中，车牌号[%s]重复", longEntry.getKey()));
            }
        }

        i = 0;
        for (VehicleImportStdVO vo : importVOs) {
            // ToolKits.isParamNull(vo.getOrganizationName(), "所属组织名称不能为空");
            // ToolKits.isParamNull(vo.getDepartmentName(), "所属部门名称不能为空");
            ToolKits.isParamNull(vo.getVehicleTypeCode(), "车型编码不能为空");
            ToolKits.isParamNull(vo.getVehicleTypeName(), "车型名称不能为空");
            if (ToolKits.isNotEmpty(vo.getAffiliationCode()) || ToolKits.isNotEmpty(vo.getAffiliationName())) {
                ToolKits.isParamNull(vo.getAffiliationCode(), "隶属机构编码不为空");
                ToolKits.isParamNull(vo.getAffiliationName(), "隶属机构名称不为空");
            }
            if (ToolKits.isNotEmpty(vo.getDriverCode()) || ToolKits.isNotEmpty(vo.getDriverName())) {
                ToolKits.isParamNull(vo.getDriverCode(), "司机编号不为空");
                ToolKits.isParamNull(vo.getDriverName(), "司机名称不为空");
            }

            TmsBaseVehicleModel model = new TmsBaseVehicleModel();
            BeanUtils.copyProperties(vo, model);
            model.setType(Integer.parseInt(vo.getType()));

            // 判断车辆编码是否已存在
            PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseVehicleMapper, "车辆编码[" + vo.getCode() + "]已存在");
            PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseVehicleMapper, "车辆编码[" + vo.getCode() + "]已停用,请直接启用!");
            // 判断车牌号是否已存在
            PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "plateNumber", vo.getPlateNumber(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseVehicleMapper, "车牌号[" + vo.getPlateNumber() + "]已存在");
            PublicMethods.isThereAnyinformation(TmsBaseVehicleModel.class, "plateNumber", vo.getPlateNumber(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseVehicleMapper, "车牌号[" + vo.getPlateNumber() + "]已停用,请直接启用!");
            // 判断所属部门
            if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
                // 调用获取部门信息API
                RestMessage<List<Organization>> restMessage = userFeign.search(null, vo.getDepartmentName(), null, String.valueOf(user.getEnterpriseId()));
                if (!restMessage.isSuccess()) {
                    throw new RuntimeException("获取部门信息API错误!");
                }
                List<Organization> data = restMessage.getData();
                // 校验部门是否存在
                if (ToolKits.isNotEmpty(data)) {
                    Organization organization = data.get(0);
                    // 判断是否在登录账号的所属部门或下级部门内
                    if (!user.getDepts().contains(organization.getId())) {
                        throw new RuntimeException(vo.getDepartmentName() + "不存在,请先维护部门!");
                    }
                    // 部门信息赋值
                    model.setDepartmentId(organization.getId());
                    model.setDepartmentCode(organization.getCode());
                } else {
                    throw new RuntimeException(vo.getDepartmentName() + "不存在,请先维护部门!");
                }
                model.setDepartmentName(vo.getDepartmentName());
            } else {

                // 获取当前账号的顶级部门
                // 调用获取部门信息API
                RestMessage<List<Organization>> restMessage = userFeign.search("0", null, null, String.valueOf(user.getEnterpriseId()));
                if (!restMessage.isSuccess()) {
                    throw new RuntimeException("获取部门信息API错误!");
                }
                List<Organization> data = restMessage.getData();
                // 校验部门是否存在
                if (ToolKits.isNotEmpty(data)) {
                    if (data.size() > 1) {
                        throw new RuntimeException("登录账号顶级部门重复!");
                    }
                    Organization organization = data.get(0);
                    // 部门信息赋值
                    model.setDepartmentId(organization.getId());
                    model.setDepartmentCode(organization.getCode());
                    model.setDepartmentName(organization.getName());
                } else {
                    throw new RuntimeException("顶级部门不存在,请先维护部门!");
                }
            }

            // 判断隶属机构
            if (ToolKits.isNotEmpty(vo.getAffiliationCode())) {
                /*
                 * if (DataDictionaryConstant.VEHICLE_TYPE_ZY.equals(vo.getType())) { Criteria
                 * criteria = Criteria.forClass(TmsBaseCargoOwnerModel.class); Restriction r1 =
                 * Restrictions.eq("cargoOwnerCode", vo.getAffiliationCode()); Restriction r2 =
                 * Restrictions.eq("cargoOwnerName", vo.getAffiliationName());
                 * criteria.setRestriction(Restrictions.and(r1, r2));
                 * List<TmsBaseCargoOwnerModel> tmsBaseCargoOwnerModels =
                 * tmsBaseCargoOwnerMapper.findByCriteria(criteria);
                 * ToolKits.isParamNull(tmsBaseCargoOwnerModels, "未找到对应的隶属机构[" +
                 * vo.getAffiliationName() + "]"); TmsBaseCargoOwnerModel tmsBaseCargoOwnerModel
                 * = tmsBaseCargoOwnerModels.get(0);
                 * Assert.isTrue(tmsBaseCargoOwnerModel.getStatus() !=
                 * StatusConstant.STATUS_DISABLE, "隶属机构【" + vo.getAffiliationName() + "】已被停用");
                 *
                 * model.setAffiliationId(tmsBaseCargoOwnerModel.getCargoOwnerId()); } else {
                 * Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
                 * Restriction r1 = Restrictions.eq("code", vo.getAffiliationCode());
                 * Restriction r2 = Restrictions.eq("name", vo.getAffiliationName());
                 * criteria.setRestriction(Restrictions.and(r1, r2)); List<TmsBaseCarriersModel>
                 * tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
                 * ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" +
                 * vo.getAffiliationName() + "]"); TmsBaseCarriersModel tmsBaseCarriersModel =
                 * tmsBaseCarriersModels.get(0); Assert.isTrue(tmsBaseCarriersModel.getStatus()
                 * != StatusConstant.STATUS_DISABLE, "隶属机构【" + vo.getAffiliationName() +
                 * "】已被停用");
                 *
                 * model.setAffiliationId(tmsBaseCarriersModel.getId()); }
                 */
                if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
                    if (!DataDictionaryConstant.VEHICLE_TYPE_ZY.equals(vo.getType())) {
                        Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
                        Restriction r1 = Restrictions.eq("code", vo.getAffiliationCode());
                        Restriction r2 = Restrictions.eq("name", vo.getAffiliationName());
                        Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
                        Restriction r4 = null;
                        // 判断是否为部门权限
                        if (ToolKits.isNotEmpty(user.getDepts())) {
                            r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
                        } else {
                            Restriction r5 = Restrictions.eq("departmentId", user.getDepts().stream().toArray(String[]::new));
                            Restriction r6 = Restrictions.in("id", user.getCarriers().stream().toArray(Long[]::new));
                            r4 = Restrictions.and(r5, r6);
                        }
                        criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
                        List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
                        ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + vo.getAffiliationName() + "]");
                        TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
                        Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + vo.getAffiliationName() + "】已被停用");

                        model.setAffiliationId(tmsBaseCarriersModel.getId());
                    } else {
                        model.setAffiliationId(null);
                        model.setAffiliationCode(null);
                        model.setAffiliationName(null);
                    }
                } else {
                    if (!DataDictionaryConstant.VEHICLE_TYPE_ZY.equals(vo.getType())) {
                        Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
                        Restriction r1 = Restrictions.eq("organizationId", vo.getAffiliationCode());
                        Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
                        criteria.setRestriction(Restrictions.and(r1, r2));
                        List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
                        ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + vo.getAffiliationName() + "]");
                        TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
                        Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + vo.getAffiliationName() + "】已被停用");

                        model.setAffiliationId(tmsBaseCarriersModel.getId());
                        model.setAffiliationCode(tmsBaseCarriersModel.getCode());
                        model.setAffiliationName(tmsBaseCarriersModel.getName());
                    } else {
                        // todo 非自有账号若归属为自有，隶属机构为当前登录账号的单位名称
                    }
                }
            }
            String vehicleId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_VEHICLE_ID, "L");
            ToolKits.isParamNull(vehicleId, "车辆ID生成失败");
            // 判断设备号
            if (ToolKits.isNotEmpty(vo.getDeviceCode())) {
                if (ToolKits.isEmpty(vo.getDeviceName())) {
                    throw new PrologException("存在设备编码时，设备名称不能为空");
                }
                String[] codes = vo.getDeviceCode().split(",");
                String[] names = vo.getDeviceName().split(",");
                if (codes.length != names.length) {
                    throw new PrologException("设备编码和设备名称不一致");
                }
                int idx = 0;
                for (String code : codes) {
                    DeviceVO deviceVO = new DeviceVO();
                    deviceVO.setCode(code);
                    deviceVO.setName(names[idx]);
                    deviceVO.setPageNum(1);
                    deviceVO.setPageSize(99);
                    Page<DeviceDTO> deviceDTOPage = deviceService.deviceQuery(deviceVO);
                    if (ToolKits.isEmpty(deviceDTOPage) || ToolKits.isEmpty(deviceDTOPage.getList())) {
                        throw new PrologException("设备号[" + names[idx] + "]未找到");
                    }
                    DeviceDTO tmsBaseDeviceModel = deviceDTOPage.getList().get(0);
                    // 车辆设备关联保存
                    TmsVehicleDeviceLinkModel vehicleDeviceLinkModel = new TmsVehicleDeviceLinkModel();
                    vehicleDeviceLinkModel.setId(ToolKits.getUUID());
                    vehicleDeviceLinkModel.setDeviceId(tmsBaseDeviceModel.getId());
                    vehicleDeviceLinkModel.setDeviceCode(tmsBaseDeviceModel.getCode());
                    vehicleDeviceLinkModel.setDeviceName(tmsBaseDeviceModel.getName());

                    vehicleDeviceLinkModel.setVehicleId(Long.valueOf(vehicleId));
                    vehicleDeviceLinkModel.setVehicleCode(vo.getCode());
                    vehicleDeviceLinkModel.setVehiclePlateNumber(vo.getPlateNumber());
                    vehicleDeviceLinkModel.setCreateperson(user.getNickname());
                    vehicleDeviceLinkModel.setCreatetime(date);
                    tmsVehicleDeviceLinkMapper.save(vehicleDeviceLinkModel);
                    idx++;
                }
            }
            // 判断容器
            if (ToolKits.isNotEmpty(vo.getContainerCode())) {
                if (ToolKits.isEmpty(vo.getContainerName())) {
                    throw new PrologException("存在容器编码时，容器名称不能为空");
                }
                String[] codes = vo.getContainerCode().split(",");
                String[] names = vo.getContainerName().split(",");
                if (codes.length != names.length) {
                    throw new PrologException("容器编码和容器名称不一致");
                }
                List<VehicleContainerLinkVO> vehicleContainerLinks = Lists.newArrayList();
                for (int i1 = 0; i1 < codes.length; i1++) {
                    Criteria criteria = Criteria.forClass(TmsBaseContainerModel.class);
                    Restriction r1 = Restrictions.eq("code", codes[i1]);
                    Restriction r2 = Restrictions.eq("name", names[i1]);
                    criteria.setRestriction(Restrictions.and(r1, r2));
                    List<TmsBaseContainerModel> tmsBaseContainerModelList = tmsBaseContainerMapper.findByCriteria(criteria);
                    if (CollectionUtil.isEmpty(tmsBaseContainerModelList)) {
                        throw new PrologException("容器号[" + names[i1] + "]未找到");
                    }
                    vehicleContainerLinks.addAll(tmsBaseContainerModelList.stream().map(tmsBaseContainerModel -> {
                        VehicleContainerLinkVO linkVO = new VehicleContainerLinkVO();
                        linkVO.setContainerId(tmsBaseContainerModel.getId().toString());
                        linkVO.setContainerCode(tmsBaseContainerModel.getCode());
                        linkVO.setContainerName(tmsBaseContainerModel.getName());
                        return linkVO;
                    }).collect(Collectors.toList()));
                }
                addVehicleContainerLink(vehicleContainerLinks, Long.valueOf(vehicleId), vo.getCode(), vo.getPlateNumber());
            }
            /// 判断司机
            if (ToolKits.isNotEmpty(vo.getDriverCode())) {
                Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
                Restriction r1 = Restrictions.eq("code", vo.getDriverCode());
                Restriction r2 = Restrictions.eq("name", vo.getDriverName());
                criteria.setRestriction(Restrictions.and(r1, r2));
                List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);
                ToolKits.isParamNull(tmsBaseDriverModels, "司机编号[" + vo.getDriverCode() + "]司机名称[" + vo.getDriverName() + "]未找到");
                TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverModels.get(0);

                // 车辆司机表新增
                TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
                tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
                tmsDriverVehicleLinkModel.setDriverId(tmsBaseDriverModel.getId());
                tmsDriverVehicleLinkModel.setDriverCode(tmsBaseDriverModel.getCode());
                tmsDriverVehicleLinkModel.setDriverName(tmsBaseDriverModel.getName());
                tmsDriverVehicleLinkModel.setVehicleId(Long.valueOf(vehicleId));
                tmsDriverVehicleLinkModel.setVehicleCode(vo.getCode());
                tmsDriverVehicleLinkModel.setVehicleName("");
                tmsDriverVehicleLinkModel.setVehiclePlateNumber(vo.getPlateNumber());
                tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
                tmsDriverVehicleLinkModel.setCreatetime(date);
                tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);

            }

            // 判断车型
            Criteria criteria = Criteria.forClass(TmsBaseVehicleTypeModel.class);
            Restriction r1 = Restrictions.eq("code", vo.getVehicleTypeCode());
            Restriction r2 = Restrictions.eq("name", vo.getVehicleTypeName());
            criteria.setRestriction(Restrictions.and(r1, r2));
            List<TmsBaseVehicleTypeModel> tmsBaseVehicleTypeModels = tmsBaseVehicleTypeMapper.findByCriteria(criteria);
            ToolKits.isParamNull(tmsBaseVehicleTypeModels, "车型编码[" + vo.getVehicleTypeCode() + "]车型名称[" + vo.getVehicleTypeName() + "]不存在");
            TmsBaseVehicleTypeModel tmsBaseVehicleTypeModel = tmsBaseVehicleTypeModels.get(0);
            model.setVehicleTypeId(tmsBaseVehicleTypeModel.getId());
            model.setVehicleTypeCode(tmsBaseVehicleTypeModel.getCode());
            model.setVehicleTypeName(tmsBaseVehicleTypeModel.getName());

            // 车辆资料赋值
            model.setId(Long.valueOf(vehicleId));
            model.setStatus(StatusConstant.STATUS_ENABLE);
            model.setSource(DataDictionaryConstant.SOURCE_EXCEL);
            model.setImportBatchNumber(importBatchNumber);
            model.setCreatetime(new Date());
            model.setCreateperson(user.getNickname());
            model.setEnterpriseId(user.getEnterpriseId());
            model.setEnterpriseName(user.getEnterpriseName());
            tmsBaseVehicleMapper.save(model);
            i++;
        }
        return i;
    }

    /**
     * 根据司机ID查询车辆信息
     */
    @Override
    public List<TmsBaseVehicleModel> listVehicleByDriverIdData(Long driverId,Long...vehicleTypeId) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 查询司机车辆关联数据
        Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
        Restriction r11 = Restrictions.eq("driverId", driverId);
        criteria1.setRestriction(Restrictions.and(r11));
        List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

        // 获取车辆ID
        Long[] vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
        Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        Restriction r3 = Restrictions.in("id", vehicleIds);
        Restriction r4 = ToolKits.isEmpty(vehicleTypeId) ? null:Restrictions.eq(TmsBaseVehicleModel::getVehicleTypeId, vehicleTypeId[0]);
        criteria.setRestriction(Restrictions.and(r1, r2, r3,r4));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);

        return tmsBaseVehicleModels;
    }

    /**
     * 司机车辆查询或新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public VehicleAndDriverDTO vehicleAutomaticData(VehicleAndVehicleVO vehicleAndVehicleVO) {
        ToolKits.isParamNull(vehicleAndVehicleVO, "请求参数不为空");
        ToolKits.isParamNull(vehicleAndVehicleVO.getDriverName(), "司机姓名不为空");
        ToolKits.isParamNull(vehicleAndVehicleVO.getPhoneNumber(), "手机号不为空");
        //ToolKits.isParamNull(vehicleAndVehicleVO.getVehiclePlateNumber(), "车牌号不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");
        ToolKits.isParamNull(user.getDepts(), "当前人员没有部门权限");

        // 查询用户的关联部门信息
        RestMessage<List<Organization>> restMessage = userFeign.selectByUserId(String.valueOf(user.getUserid()));
        Assert.isTrue(restMessage.isSuccess(), "查询用户部门信息失败:" + restMessage.getMessage());
        Assert.isTrue(ToolKits.isNotEmpty(restMessage.getData()), "用户【" + user.getNickname() + "】未绑定部门");
        // 部门对象
        Organization organization = restMessage.getData().get(0);

        // 当前时间
        Date date = new Date();

        // 响应对象
        VehicleAndDriverDTO vehicleAndDriverDTO = new VehicleAndDriverDTO();

        // 判断司机是否存在
        Criteria criteri1 = Criteria.forClass(TmsBaseDriverModel.class);
        Restriction r11 = Restrictions.eq(TmsBaseDriverModel::getPhoneNumber, vehicleAndVehicleVO.getPhoneNumber());
        Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        criteri1.setRestriction(Restrictions.and(r11, r12));
        List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteri1);
        if (ToolKits.isEmpty(tmsBaseDriverModels)) {
            // 司机ID
            String driverId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.DRIVER_ID, "L");
            ToolKits.isParamNull(driverId, "司机ID生成失败");

            TmsBaseDriverModel tmsBaseDriverModel = new TmsBaseDriverModel();
            tmsBaseDriverModel.setId(Long.valueOf(driverId));
            tmsBaseDriverModel.setCode(driverId);
            tmsBaseDriverModel.setName(vehicleAndVehicleVO.getDriverName());
            if (ToolKits.isNotEmpty(vehicleAndVehicleVO.getVehicleAscription())) {
                tmsBaseDriverModel.setType(vehicleAndVehicleVO.getVehicleAscription());
            } else {
                tmsBaseDriverModel.setType(DataDictionaryConstant.DRIVER_TYPE_TEMPORARY);
            }
            tmsBaseDriverModel.setPhoneNumber(vehicleAndVehicleVO.getPhoneNumber());
            tmsBaseDriverModel.setPassword(BussinessConstant.PASSWORD);
            tmsBaseDriverModel.setStatus(DataDictionaryConstant.STATUS_ENABLE);
            tmsBaseDriverModel.setSource(DataDictionaryConstant.SOURCE_ADD);
            // 承运商登录默认隶属机构为承运商
            if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
                tmsBaseDriverModel.setAffiliationId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
                tmsBaseDriverModel.setAffiliationCode(user.getLoginUserCompanyInfo().getCode());
                tmsBaseDriverModel.setAffiliationName(user.getLoginUserCompanyInfo().getName());
                tmsBaseDriverModel.setAffiliationType(DataDictionaryConstant.AFFILIATION_TYPE_CYS);
            }
            tmsBaseDriverModel.setDepartmentId(organization.getId());
            tmsBaseDriverModel.setDepartmentCode(organization.getCode());
            tmsBaseDriverModel.setDepartmentName(organization.getName());
            tmsBaseDriverModel.setEnterpriseId(user.getEnterpriseId());
            tmsBaseDriverModel.setEnterpriseName(user.getEnterpriseName());
            tmsBaseDriverModel.setCreateperson(user.getNickname());
            tmsBaseDriverModel.setCreatetime(date);
            tmsBaseDriverModel.setUpdateperson(user.getNickname());
            tmsBaseDriverModel.setUpdatetime(date);
            tmsBaseDriverMapper.save(tmsBaseDriverModel);

            // 校验司机信息是否已注册
            RestMessage<UserDTO> restMessage2 = userFeign.getPlatformUserByUserName(tmsBaseDriverModel.getPhoneNumber(), null);
            if (restMessage2.isSuccess()) {
                if (ToolKits.isEmpty(restMessage2.getData())) {
                    // 注册司机账号
                    User adduser = new User();
                    adduser.setCompanyId(null);
                    adduser.setCompanyCode(null);
                    adduser.setCompanyName(null);
                    adduser.setCreateDate(date);
                    adduser.setDisable(false);
                    adduser.setEmailAddr(null);
                    adduser.setEmployeeNum(null);
                    adduser.setExpireDate(null);
                    adduser.setEnterpriseId(user.getEnterpriseId().toString());
                    adduser.setId(tmsBaseDriverModel.getId().toString());
                    adduser.setNickname(tmsBaseDriverModel.getName());
                    adduser.setPassword(ToolKits.isEmpty(tmsBaseDriverModel.getPassword()) ? BussinessConstant.PASSWORD : tmsBaseDriverModel.getPassword());
                    adduser.setPersonType(BussinessConstant.PERSON_TYPE_PT);
                    adduser.setPhoneNumber(tmsBaseDriverModel.getPhoneNumber());
                    adduser.setRoleId(BussinessConstant.ROLEID_DRIVER_ID);
                    adduser.setServiceId(null);
                    adduser.setUserCategory(BussinessConstant.USER_TYPE_SJ);
                    adduser.setUsername(tmsBaseDriverModel.getPhoneNumber());
                    adduser.setUserState("0");
                    // 角色信息
                    List<Role> roles = new ArrayList<Role>();
                    Role role = new Role();
                    role.setAllAuth(BussinessConstant.ALLAUTH_NO);
                    role.setEnterpriseId(user.getEnterpriseId().toString());
                    role.setId(BussinessConstant.ROLEID_DRIVER_ID);
                    role.setName(BussinessConstant.ROLEID_DRIVER_NAME);
                    role.setNum(BussinessConstant.ROLEID_DRIVER_ID);
                    roles.add(role);
                    adduser.setRoles(roles);

                    RestMessage<String> restMessage1 = userFeign.addTmsUser(adduser);
                    if (!restMessage1.isSuccess()) {
                        throw new ParameterException("司机账号注册失败:" + restMessage1.getMessage());
                    }
                }
            } else {
                throw new ParameterException("校验司机信息是否存在失败:" + restMessage2.getMessage());
            }

            // 司机信息响应
            DriverAutomaticDTO driver = new DriverAutomaticDTO();
            driver.setDriverId(tmsBaseDriverModel.getId());
            driver.setDriverCode(tmsBaseDriverModel.getCode());
            driver.setDriverName(tmsBaseDriverModel.getName());
            driver.setDriverPhoneNumber(tmsBaseDriverModel.getPhoneNumber());
            driver.setAffiliationId(tmsBaseDriverModel.getAffiliationId());
            driver.setAffiliationCode(tmsBaseDriverModel.getAffiliationCode());
            driver.setAffiliationName(tmsBaseDriverModel.getAffiliationName());
            vehicleAndDriverDTO.setDriver(driver);

        } else {
            // 已存在的司机信息
            TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverModels.get(0);

            // 司机信息响应
            DriverAutomaticDTO driver = new DriverAutomaticDTO();
            driver.setDriverId(tmsBaseDriverModel.getId());
            driver.setDriverCode(tmsBaseDriverModel.getCode());
            driver.setDriverName(tmsBaseDriverModel.getName());
            driver.setDriverPhoneNumber(tmsBaseDriverModel.getPhoneNumber());
            driver.setAffiliationId(tmsBaseDriverModel.getAffiliationId());
            driver.setAffiliationCode(tmsBaseDriverModel.getAffiliationCode());
            driver.setAffiliationName(tmsBaseDriverModel.getAffiliationName());
            vehicleAndDriverDTO.setDriver(driver);
        }
        if (ToolKits.isEmpty(vehicleAndVehicleVO.getVehiclePlateNumber())) {
            return vehicleAndDriverDTO;
        }
        // 判断车辆是否存在
        Criteria criteri2 = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r21 = Restrictions.eq(TmsBaseVehicleModel::getPlateNumber, vehicleAndVehicleVO.getVehiclePlateNumber());
        Restriction r22 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        criteri2.setRestriction(Restrictions.and(r21, r22));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteri2);
        if (ToolKits.isEmpty(tmsBaseVehicleModels)) {
            // 车辆ID
            String vehicleId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_VEHICLE_ID, "L");
            ToolKits.isParamNull(vehicleId, "车辆ID生成失败");

            TmsBaseVehicleModel tmsBaseVehicleModel = new TmsBaseVehicleModel();
            tmsBaseVehicleModel.setId(Long.valueOf(vehicleId));
            tmsBaseVehicleModel.setCode(vehicleId);
            tmsBaseVehicleModel.setPlateNumber(vehicleAndVehicleVO.getVehiclePlateNumber());
            tmsBaseVehicleModel.setType(ObjectUtil.isNotEmpty(vehicleAndVehicleVO.getVehicleAscription()) ? vehicleAndVehicleVO.getVehicleAscription():DataDictionaryConstant.VEHICLE_TYPE_TEMPORARY);
            tmsBaseVehicleModel.setStatus(DataDictionaryConstant.STATUS_ENABLE);
            tmsBaseVehicleModel.setSource(DataDictionaryConstant.SOURCE_ADD);
            tmsBaseVehicleModel.setDepartmentId(organization.getId());
            tmsBaseVehicleModel.setDepartmentCode(organization.getCode());
            tmsBaseVehicleModel.setDepartmentName(organization.getName());
            tmsBaseVehicleModel.setEnterpriseId(user.getEnterpriseId());
            tmsBaseVehicleModel.setEnterpriseName(user.getEnterpriseName());
            tmsBaseVehicleModel.setCreateperson(user.getNickname());
            tmsBaseVehicleModel.setCreatetime(date);
            tmsBaseVehicleModel.setUpdateperson(user.getNickname());
            tmsBaseVehicleModel.setUpdatetime(date);
            tmsBaseVehicleMapper.save(tmsBaseVehicleModel);

            // 车辆信息响应
            VehicleAutomaticDTO vehicle = new VehicleAutomaticDTO();
            vehicle.setVehicleId(tmsBaseVehicleModel.getId());
            vehicle.setVehicleCode(tmsBaseVehicleModel.getCode());
            vehicle.setVehicleName(null);
            vehicle.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
            vehicleAndDriverDTO.setVehicle(vehicle);
        } else {
            // 已存在的车辆信息
            TmsBaseVehicleModel tmsBaseVehicleModel = tmsBaseVehicleModels.get(0);

            // 车辆信息响应
            VehicleAutomaticDTO vehicle = new VehicleAutomaticDTO();
            vehicle.setVehicleId(tmsBaseVehicleModel.getId());
            vehicle.setVehicleCode(tmsBaseVehicleModel.getCode());
            vehicle.setVehicleName(null);
            vehicle.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
            vehicleAndDriverDTO.setVehicle(vehicle);
        }

        // 判断车辆司机关联关系是否存在
        Criteria criteri3 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
        Restriction r31 = Restrictions.eq(TmsDriverVehicleLinkModel::getDriverId, vehicleAndDriverDTO.getDriver().getDriverId());
        Restriction r32 = Restrictions.eq(TmsDriverVehicleLinkModel::getVehicleId, vehicleAndDriverDTO.getVehicle().getVehicleId());
        criteri3.setRestriction(Restrictions.and(r31, r32));
        List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteri3);
        if (ToolKits.isEmpty(tmsDriverVehicleLinkModels)) {
            // 司机车辆关联保存
            TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
            tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
            tmsDriverVehicleLinkModel.setDriverId(vehicleAndDriverDTO.getDriver().getDriverId());
            tmsDriverVehicleLinkModel.setDriverCode(vehicleAndDriverDTO.getDriver().getDriverCode());
            tmsDriverVehicleLinkModel.setDriverName(vehicleAndDriverDTO.getDriver().getDriverName());
            tmsDriverVehicleLinkModel.setVehicleId(vehicleAndDriverDTO.getVehicle().getVehicleId());
            tmsDriverVehicleLinkModel.setVehicleCode(vehicleAndDriverDTO.getVehicle().getVehicleCode());
            tmsDriverVehicleLinkModel.setVehicleName(vehicleAndDriverDTO.getVehicle().getVehicleName());
            tmsDriverVehicleLinkModel.setVehiclePlateNumber(vehicleAndDriverDTO.getVehicle().getVehiclePlateNumber());
            tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
            tmsDriverVehicleLinkModel.setCreatetime(date);
            tmsDriverVehicleLinkModel.setUpdateperson(user.getNickname());
            tmsDriverVehicleLinkModel.setUpdatetime(date);
            tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);
        }

        return vehicleAndDriverDTO;
    }

    /**
     * 根据司机ID查询车辆信息
     */
    @Override
    public List<TmsBaseVehicleModel> listVehicleByDriverIdDataSelfReg(Long driverId) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 查询司机车辆关联数据
        Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
        Restriction r11 = Restrictions.eq("driverId", driverId);
        criteria1.setRestriction(Restrictions.and(r11));
        List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

        // 获取车辆ID
        Long[] vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
        Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        Restriction r3 = Restrictions.in("id", vehicleIds);
        Restriction r4 = Restrictions.eq(TmsBaseVehicleModel::getCreateperson, user.getNickname());
        criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);

        return tmsBaseVehicleModels;
    }

    /**
     * 车辆资料弹窗
     *
     * @param vehiclePopupVO
     * @return
     */
    @Override
    public Page<VehiclePopupDTO> popupVehicleSelfRegData(VehiclePopupVO vehiclePopupVO) {
        ToolKits.isParamNull(vehiclePopupVO, "车型资料查询请求不为空");
        ToolKits.isParamNull(vehiclePopupVO.getPageNum(), "页码不为空");
        ToolKits.isParamNull(vehiclePopupVO.getPageSize(), "页数不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
            ToolKits.isParamNull(user, "当前人员没有操作权限");
        }

        // 相应对象集合
        List<VehiclePopupDTO> vehiclePopupDTOs = new ArrayList<VehiclePopupDTO>();

        // 车辆类型
        List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.VEHICLE_TYPE, null, null, null);

        // 车辆ID
        Long[] vehicleIds = null;

        // 当司机ID不为空时
        if (ToolKits.isNotEmpty(vehiclePopupVO.getDriverId())) {
            // 查询司机车辆关联数据
            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r11 = Restrictions.eq("driverId", vehiclePopupVO.getDriverId());
            criteria1.setRestriction(Restrictions.and(r11));
            List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

            // 获取车辆ID
            vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);
        }

        // 当司机ID集合不为空时
        if (ToolKits.isNotEmpty(vehiclePopupVO.getDriverIds())) {
            // 查询司机车辆关联数据
            Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
            Restriction r11 = Restrictions.in("driverId", vehiclePopupVO.getDriverIds().stream().toArray(Long[]::new));
            criteria1.setRestriction(Restrictions.and(r11));
            List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteria1);

            // 获取车辆ID
            vehicleIds = (tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);
        }

        // 分页
        PageUtils.startPage(vehiclePopupVO.getPageNum(), vehiclePopupVO.getPageSize());

        Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
        Restriction r1 = ToolKits.isEmpty(vehiclePopupVO.getCode()) ? null : Restrictions.like("code", "%" + vehiclePopupVO.getCode() + "%");
        Restriction r2 = ToolKits.isEmpty(vehiclePopupVO.getPlateNumber()) ? null : Restrictions.like("plateNumber", "%" + vehiclePopupVO.getPlateNumber() + "%");
        Restriction r3 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
        Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
        Restriction r5 = null;

        Restriction r6 = ToolKits.isEmpty(vehiclePopupVO.getDriverId()) ? null : Restrictions.in("id", vehicleIds);
        Restriction r7 = ToolKits.isEmpty(vehiclePopupVO.getDriverIds()) ? null : Restrictions.in("id", vehicleIds);
        Restriction r8 = Restrictions.eq(TmsBaseVehicleModel::getCreateperson, user.getNickname());
        criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8));
        criteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);

        for (TmsBaseVehicleModel item : tmsBaseVehicleModels) {
            // 响应对象
            VehiclePopupDTO vehiclePopupDTO = new VehiclePopupDTO();
            BeanUtils.copyProperties(item, vehiclePopupDTO);
            vehiclePopupDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(vehiclePopupDTO.getType())));

            // 根据车辆车型ID查询车辆体积和载重
            if (ToolKits.isNotEmpty(item.getVehicleTypeId())) {
                VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
                vehicleTypeCheckVO.setVehicleTypeId(item.getVehicleTypeId());
                VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
                vehiclePopupDTO.setWeight(vehicleTypeCheckDTO.getWeight());
                vehiclePopupDTO.setVolume(vehicleTypeCheckDTO.getVolume());
            }

            vehiclePopupDTOs.add(vehiclePopupDTO);
        }

        Page<VehiclePopupDTO> page = PageUtils.getPage(vehiclePopupDTOs);
        if (tmsBaseVehicleModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsBaseVehicleModel> p0 = (com.github.pagehelper.Page<TmsBaseVehicleModel>) tmsBaseVehicleModels;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }

        return page;
    }

    /**
     * 车辆容器关联新增
     *
     * @param vehicleContainerLinks
     * @param vehicleId
     * @param vehicleCode
     * @param vehiclePlateNumber
     */
    private void addVehicleContainerLink(List<VehicleContainerLinkVO> vehicleContainerLinks, Long vehicleId, String vehicleCode, String vehiclePlateNumber) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        vehicleContainerLinks.forEach(vehicleContainerLinkVO -> {
            ToolKits.isParamNull(vehicleContainerLinkVO.getContainerId(), "容器ID不为空");
            ToolKits.isParamNull(vehicleContainerLinkVO.getContainerCode(), "容器编码不为空");
            ToolKits.isParamNull(vehicleContainerLinkVO.getContainerName(), "容器名称不为空");

            TmsVehicleContainerLink vehicleContainerLinkModel = new TmsVehicleContainerLink();
            vehicleContainerLinkModel.setId(ToolKits.getUUID());
            vehicleContainerLinkModel.setContainerId(vehicleContainerLinkVO.getContainerId());
            vehicleContainerLinkModel.setContainerCode(vehicleContainerLinkVO.getContainerCode());
            vehicleContainerLinkModel.setContainerName(vehicleContainerLinkVO.getContainerName());

            vehicleContainerLinkModel.setVehicleId(vehicleId);
            vehicleContainerLinkModel.setVehicleCode(vehicleCode);
            vehicleContainerLinkModel.setVehiclePlateNumber(vehiclePlateNumber);
            vehicleContainerLinkModel.setCreateperson(user.getNickname());
            vehicleContainerLinkModel.setCreatetime(new Date());
            vehicleContainerLinkModel.setUpdateperson(user.getNickname());
            vehicleContainerLinkModel.setUpdatetime(new Date());
            vehicleContainerLinkModel.setEnterpriseId(user.getEnterpriseId());
            vehicleContainerLinkModel.setEnterpriseName(user.getEnterpriseName());
            tmsVehicleContainerLinkMapper.save(vehicleContainerLinkModel);
        });
    }

}
