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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
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.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.constant.*;
import com.prolog.rdc.tms.dao.mapper.*;
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.*;
import com.prolog.rdc.tms.model.entity.line.*;
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.*;
import com.prolog.rdc.tms.model.vo.xlsimport.DriverImportStdVO;
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.DriverService;
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 lombok.extern.slf4j.Slf4j;
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 java.util.*;
import java.util.stream.Collectors;

/**
 * 车队管理-司机管理实现层
 *
 * @author :fanzw
 * @version :1.0.0
 * @className :DriverServiceImpl
 * @creationTime :2022/03/15
 * @description :
 * @modifier :fanzw
 * @modifyTime :2022/03/15
 * @reviseRemarks:
 */
@Slf4j
@Service
public class DriverServiceImpl implements DriverService {

	@Autowired
	private TmsBaseDriverMapper tmsBaseDriverMapper;

	@Autowired
	private TmsDriverVehicleLinkMapper tmsDriverVehicleLinkMapper;

	@Autowired
	private TmsDriverPaperLinkMapper tmsDriverPaperLinkMapper;

	@Autowired
	private TmsBasePaperMapper tmsBasePaperMapper;

	@Autowired
	private TmsLineCarrierLinkMapper tmsLineCarrierLinkMapper;

	@Autowired
	private TmsBaseTranslineMapper tmsBaseTranslineMapper;

	@Autowired
	private TmsBaseVehicleMapper tmsBaseVehicleMapper;

	@Autowired
	private TmsBaseVehicleTypeMapper tmsBaseVehicleTypeMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private TmsLineCarrierLinkMapper tmsCarrierMapper;

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@Autowired
	private TmsBaseCarriersMapper tmsBaseCarriersMapper;

	@Autowired
	private UserFeign userTmsResource;

	@Autowired
	private VehicleService vehicleService;

	@Autowired
	private VehicleTypeService vehicleTypeService;

	@Autowired
	private AmqpTemplate amqpTemplate;

	/**
	 * 司机资料分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DriverPagingQueryDTO> pageDriverData(DriverPagingQueryVO driverPagingQueryVO) {
		ToolKits.isParamNull(driverPagingQueryVO, "司机资料查询请求不为空");
		ToolKits.isParamNull(driverPagingQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(driverPagingQueryVO.getPageSize(), "页数不为空");

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

		// 响应对象集合
		List<DriverPagingQueryDTO> driverPagingQueryDTOs = new ArrayList<DriverPagingQueryDTO>();

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

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

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = ToolKits.isEmpty(driverPagingQueryVO.getCode()) ? null : Restrictions.like("code", "%" + driverPagingQueryVO.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(driverPagingQueryVO.getName()) ? null : Restrictions.like("name", "%" + driverPagingQueryVO.getName() + "%");
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// 权限控制
		Restriction r4 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r4 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);
		for (TmsBaseDriverModel tmsBaseDriverModel : tmsBaseDriverModels) {
			// 响应对象
			DriverPagingQueryDTO driverPagingQueryDTO = new DriverPagingQueryDTO();
			BeanUtils.copyProperties(tmsBaseDriverModel, driverPagingQueryDTO);
			driverPagingQueryDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(driverPagingQueryDTO.getType())));
			driverPagingQueryDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(driverPagingQueryDTO.getStatus())));
			driverPagingQueryDTOs.add(driverPagingQueryDTO);
		}

		Page<DriverPagingQueryDTO> page = PageUtils.getPage(driverPagingQueryDTOs);
		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<DriverPopupDTO> popupDriverData(DriverPopupVO driverPopupVO) {
		ToolKits.isParamNull(driverPopupVO, "司机资料查询请求不为空");
		ToolKits.isParamNull(driverPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(driverPopupVO.getPageSize(), "页数不为空");

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

		// 响应对象集合
		List<DriverPopupDTO> driverPopupDTOs = new ArrayList<DriverPopupDTO>();

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

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = ToolKits.isEmpty(driverPopupVO.getCode()) ? null : Restrictions.like("code", "%" + driverPopupVO.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(driverPopupVO.getName()) ? null : Restrictions.like("name", "%" + driverPopupVO.getName() + "%");
		Restriction r7 = ToolKits.isEmpty(driverPopupVO.getCarriersId()) ? null : Restrictions.eq("affiliationId", driverPopupVO.getCarriersId());
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r4 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		// 权限控制
		Restriction r5 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r5 = ToolKits.isEmpty(user.getDepts()) ? null : Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r5 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			r5 = Restrictions.in(TmsBaseDriverModel::getDepartmentId, user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		Restriction r6 = null;
		Boolean hasVehicleTypeDrivers = Boolean.FALSE;
		if (ToolKits.isNotEmpty(driverPopupVO.getVehicleTypeId())) {
			VehiclePopupVO vehicleTypeVO = new VehiclePopupVO();
			vehicleTypeVO.setVehicleTypeId(driverPopupVO.getVehicleTypeId());
			List<VehicleDriverPagingQueryDTO> DriverDTOS = tmsBaseVehicleMapper.queryDriverByVehicleType(vehicleTypeVO);
			if (ToolKits.isNotEmpty(DriverDTOS)) {
				List<Long> driverIds = DriverDTOS.stream().map(VehicleDriverPagingQueryDTO::getDriverId).distinct().collect(Collectors.toList());
				r6 = Restrictions.in(TmsBaseDriverModel::getId, driverIds.stream().toArray());
				hasVehicleTypeDrivers = Boolean.TRUE;
			}
		}

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5,r6,r7));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		// 分页
		PageUtils.startPage(driverPopupVO.getPageNum(), driverPopupVO.getPageSize());
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);
		for (TmsBaseDriverModel tmsBaseDriverModel : tmsBaseDriverModels) {
			// 响应对象
			DriverPopupDTO driverPopupDTO = new DriverPopupDTO();
			BeanUtils.copyProperties(tmsBaseDriverModel, driverPopupDTO);

			if (Boolean.TRUE.equals(hasVehicleTypeDrivers)) {
				driverPopupDTO.setHasVehicleTypeDriver("1");
			} else {
				driverPopupDTO.setHasVehicleTypeDriver("0");
			}
			// 查询司机对应的车辆信息
			List<TmsBaseVehicleModel> tmsBaseVehicleModels = vehicleService.listVehicleByDriverIdData(tmsBaseDriverModel.getId(), driverPopupVO.getVehicleTypeId());
			if (tmsBaseVehicleModels.size() == 1) {
				driverPopupDTO.setVehicleId(tmsBaseVehicleModels.get(0).getId());
				driverPopupDTO.setVehicleCode(tmsBaseVehicleModels.get(0).getCode());
				driverPopupDTO.setVehiclePlateNumber(tmsBaseVehicleModels.get(0).getPlateNumber());
				driverPopupDTO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
				driverPopupDTO.setVehicleTypeCode(tmsBaseVehicleModels.get(0).getVehicleTypeCode());
				driverPopupDTO.setVehicleTypeName(tmsBaseVehicleModels.get(0).getVehicleTypeName());
				// STD-DEVELOP20220823 lhb 司机默认带出唯一车辆 20220913 start
				driverPopupDTO.setVehType(tmsBaseVehicleModels.get(0).getType());
				driverPopupDTO.setVehTypeName(PublicMethods.change(restMessage1, String.valueOf(tmsBaseVehicleModels.get(0).getType())));

				driverPopupDTO.setVehicleAscription(driverPopupDTO.getVehType());
				driverPopupDTO.setVehicleAscriptionName(driverPopupDTO.getVehTypeName());

				if (ToolKits.isNotEmpty(tmsBaseVehicleModels.get(0).getVehicleTypeId())) {
					VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
					vehicleTypeCheckVO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
					VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
					driverPopupDTO.setVolume(vehicleTypeCheckDTO.getVolume());
					driverPopupDTO.setWeight(vehicleTypeCheckDTO.getWeight());
					driverPopupDTO.setVehicleTypeCategory(vehicleTypeCheckDTO.getType());
					driverPopupDTO.setVehicleTypeCategoryName(vehicleTypeCheckDTO.getTypeName());
				}
				// STD-DEVELOP20220823 lhb 司机默认带出唯一车辆 20220913 end

				// 查询司机证件关联数据
				Criteria criteria1 = Criteria.forClass(TmsDriverPaperLinkModel.class);
				Restriction r11 = Restrictions.eq(TmsDriverPaperLinkModel::getDriverId, tmsBaseDriverModel.getId());
				Restriction r12 = Restrictions.eq(TmsDriverPaperLinkModel::getPaperObjectType, Integer.valueOf(1));
				criteria1.setRestriction(Restrictions.and(r11, r12));
				List<TmsDriverPaperLinkModel> tmsDriverPaperLinkModels = tmsDriverPaperLinkMapper.findByCriteria(criteria1);
				if (ToolKits.isNotEmpty(tmsDriverPaperLinkModels)) {
					driverPopupDTO.setPaperCode(tmsDriverPaperLinkModels.get(0).getPaperCode());
				}
			}

			driverPopupDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(driverPopupDTO.getType())));
			driverPopupDTOs.add(driverPopupDTO);
		}

		Page<DriverPopupDTO> page = PageUtils.getPage(driverPopupDTOs);
		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;
	}

	public DriverPopupDTO getDriverForCarriersAutoDis(DriverPopupVO driverPopupVO) {
		ToolKits.isParamNull(driverPopupVO, "司机资料查询请求不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = ToolKits.isEmpty(driverPopupVO.getCarriersId()) ? null : Restrictions.eq("affiliationId", driverPopupVO.getCarriersId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r3 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		// 权限控制
		Restriction r4 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r4 = ToolKits.isEmpty(user.getDepts()) ? null : Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r4 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			r4 = Restrictions.in(TmsBaseDriverModel::getDepartmentId, user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);

		if (ToolKits.isEmpty(tmsBaseDriverModels)) {
			return null;
		}
		// 响应对象
		DriverPopupDTO driverPopupDTO = new DriverPopupDTO();
		BeanUtil.copyProperties(tmsBaseDriverModels.get(0), driverPopupDTO);

		// 查询司机车辆关联数据
		Criteria criteriaDriverVehicleLink = Criteria.forClass(TmsDriverVehicleLinkModel.class);
		Restriction r11 = Restrictions.eq("driverId", driverPopupDTO.getId());
		criteriaDriverVehicleLink.setRestriction(Restrictions.and(r11));
		List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = tmsDriverVehicleLinkMapper.findByCriteria(criteriaDriverVehicleLink);
		// 获取车辆ID
		List<Long> vehicleIds = Lists.newArrayList();
		if (ToolKits.isNotEmpty(tmsDriverVehicleLinkModels)) {
			vehicleIds.addAll(tmsDriverVehicleLinkModels.stream().map(TmsDriverVehicleLinkModel::getVehicleId).distinct().collect(Collectors.toList()));
		}
		Criteria criteriaBaseVehicle = Criteria.forClass(TmsBaseVehicleModel.class);
		r1 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		r3 = ToolKits.isEmpty(vehicleIds) ? null:Restrictions.in("id", vehicleIds.stream().toArray());
		r4 = ToolKits.isEmpty(driverPopupVO.getCarriersId()) ? null:Restrictions.eq(TmsBaseVehicleModel::getAffiliationId, driverPopupVO.getCarriersId());
		criteriaBaseVehicle.setRestriction(Restrictions.and(r1, r2, r3,r4));
		List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteriaBaseVehicle);
		if (ToolKits.isEmpty(tmsBaseVehicleModels)) {
			return null;
		}
		// 查询司机对应的车辆信息
		driverPopupDTO.setVehicleId(tmsBaseVehicleModels.get(0).getId());
		driverPopupDTO.setVehicleCode(tmsBaseVehicleModels.get(0).getCode());
		driverPopupDTO.setVehiclePlateNumber(tmsBaseVehicleModels.get(0).getPlateNumber());
		driverPopupDTO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
		driverPopupDTO.setVehicleTypeCode(tmsBaseVehicleModels.get(0).getVehicleTypeCode());
		driverPopupDTO.setVehicleTypeName(tmsBaseVehicleModels.get(0).getVehicleTypeName());
		driverPopupDTO.setVehType(tmsBaseVehicleModels.get(0).getType());
		driverPopupDTO.setVehTypeName(PublicMethods.change(restMessage1, String.valueOf(tmsBaseVehicleModels.get(0).getType())));

		driverPopupDTO.setVehicleAscription(driverPopupDTO.getVehType());
		driverPopupDTO.setVehicleAscriptionName(driverPopupDTO.getVehTypeName());

		if (ToolKits.isNotEmpty(tmsBaseVehicleModels.get(0).getVehicleTypeId())) {
			VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
			vehicleTypeCheckVO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
			VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
			if (ToolKits.isNotEmpty(vehicleTypeCheckDTO)) {
				driverPopupDTO.setVolume(vehicleTypeCheckDTO.getVolume());
				driverPopupDTO.setWeight(vehicleTypeCheckDTO.getWeight());
				driverPopupDTO.setVehicleTypeCategory(vehicleTypeCheckDTO.getType());
				driverPopupDTO.setVehicleTypeCategoryName(vehicleTypeCheckDTO.getTypeName());
			}
		}
		driverPopupDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(driverPopupDTO.getType())));
		return driverPopupDTO;
	}

	/**
	 * 司机资料查看
	 */
	@Override
	public DriverCheckDTO getDriverData(DriverCheckVO driverCheckVO) {
		ToolKits.isParamNull(driverCheckVO, "司机资料查看请求不为空");
		ToolKits.isParamNull(driverCheckVO.getId(), "司机ID不为空");


		// 司机状态
		List<Map<String, Object>> restMessage = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = Restrictions.eq("id", driverCheckVO.getId());
		criteria.setRestriction(Restrictions.and(r1));
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);

		Assert.isTrue(tmsBaseDriverModels.size() == 1, "司机ID【" + driverCheckVO.getId() + "】存在重复数据，查询失败");

		// 司机资料对象
		TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverModels.get(0);

		// 响应对象
		DriverCheckDTO driverCheckDTO = new DriverCheckDTO();
		BeanUtils.copyProperties(tmsBaseDriverModel, driverCheckDTO);
		driverCheckDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(driverCheckDTO.getStatus())));

		return driverCheckDTO;
	}

	/**
	 * 司机资料是否启用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void whetherEnableDriverData(DriverWhetherEnableVO driverWhetherEnableVO) {
		ToolKits.isParamNull(driverWhetherEnableVO, "司机资料是否启用请求不为空");
		ToolKits.isParamNull(driverWhetherEnableVO.getDriverIds(), "司机ID集不为空");
		ToolKits.isParamNull(driverWhetherEnableVO.getStatus(), "状态不为空");

		Assert.isTrue(driverWhetherEnableVO.getStatus() == StatusConstant.STATUS_ENABLE || driverWhetherEnableVO.getStatus() == StatusConstant.STATUS_DISABLE, "司机状态【" + driverWhetherEnableVO.getStatus() + "】错误");

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

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

		for (DriverByIdVO driverByIdVO : driverWhetherEnableVO.getDriverIds()) {
			ToolKits.isParamNull(driverByIdVO.getId(), "司机ID不为空");

			// 根据ID查询司机对象
			TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverMapper.findById(driverByIdVO.getId(), TmsBaseDriverModel.class);
			ToolKits.isParamNull(tmsBaseDriverModel, "司机ID【" + driverByIdVO.getId() + "】不存在");

			tmsBaseDriverModel.setStatus(driverWhetherEnableVO.getStatus());
			tmsBaseDriverModel.setUpdateperson(user.getNickname());
			tmsBaseDriverModel.setUpdatetime(date);
			tmsBaseDriverMapper.update(tmsBaseDriverModel);
		}
	}

	/**
	 * 司机资料新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addDriverData(DriverAddVO driverAddVO) {
		ToolKits.isParamNull(driverAddVO, "司机资料新增请求不为空");
		ToolKits.isParamNull(driverAddVO.getCode(), "司机编码不为空");
		ToolKits.isParamNull(driverAddVO.getName(), "司机名称不为空");
		ToolKits.isParamNull(driverAddVO.getType(), "司机类型不为空");
		ToolKits.isParamNull(driverAddVO.getPhoneNumber(), "手机号不为空");

		ToolKits.isParamNull(driverAddVO.getDepartmentId(), "所属部门ID不能为空");
		ToolKits.isParamNull(driverAddVO.getDepartmentCode(), "所属部门编号不能为空");
		ToolKits.isParamNull(driverAddVO.getDepartmentName(), "所属部门名称不能为空");

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

		// 判断司机编码是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseDriverModel.class, "code", driverAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), tmsBaseDriverMapper, "司机编码已存在");

		// 司机ID
		String driverId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.DRIVER_ID, "L");
		ToolKits.isParamNull(driverId, "司机ID生成失败");
		TmsBaseDriverModel tmsBaseDriverModel = new TmsBaseDriverModel();
		BeanUtils.copyProperties(driverAddVO, tmsBaseDriverModel);
		// 判断隶属机构
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			if (driverAddVO.getType() == DataDictionaryConstant.DRIVER_TYPE_OWN) {
				if (ToolKits.isNotEmpty(driverAddVO.getAffiliationCode()) || ToolKits.isNotEmpty(driverAddVO.getAffiliationName())) {
					throw new BizException("司机归属为自有，隶属机构为空!");
				}
			} else {
				ToolKits.isParamNull(driverAddVO.getAffiliationId(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverAddVO.getAffiliationCode(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverAddVO.getAffiliationName(), "隶属机构名称不为空");

				Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
				Restriction r1 = Restrictions.eq("id", driverAddVO.getAffiliationId());
				Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
				ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + driverAddVO.getAffiliationName() + "]");
				TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
				Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + driverAddVO.getAffiliationName() + "】已被停用");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			if (driverAddVO.getType() == DataDictionaryConstant.DRIVER_TYPE_OWN) {
				// 自有司机
				tmsBaseDriverModel.setAffiliationId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
				tmsBaseDriverModel.setAffiliationCode(user.getLoginUserCompanyInfo().getCode());
				tmsBaseDriverModel.setAffiliationName(user.getLoginUserCompanyInfo().getName());
			} else {
				ToolKits.isParamNull(driverAddVO.getAffiliationId(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverAddVO.getAffiliationCode(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverAddVO.getAffiliationName(), "隶属机构名称不为空");

				Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
				Restriction r1 = Restrictions.eq("id", driverAddVO.getAffiliationId());
				Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
				ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + driverAddVO.getAffiliationName() + "]");
				TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
				Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + driverAddVO.getAffiliationName() + "】已被停用");
			}
		}

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

		// 司机资料对象保存
		tmsBaseDriverModel.setId(Long.valueOf(driverId));
		tmsBaseDriverModel.setStatus(StatusConstant.STATUS_ENABLE);
		tmsBaseDriverModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		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);

		// 司机车辆关联不为空新增
		if (ToolKits.isNotEmpty(driverAddVO.getDriverVehicleLinks())) {
			for (DriverVehicleLinkAddVO driverVehicleLink : driverAddVO.getDriverVehicleLinks()) {
				ToolKits.isParamNull(driverVehicleLink.getVehicleId(), "车辆ID不为空");
				ToolKits.isParamNull(driverVehicleLink.getVehicleCode(), "车辆编码不为空");
				ToolKits.isParamNull(driverVehicleLink.getVehiclePlateNumber(), "车牌号不为空");

				// 司机车辆关联保存
				TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
				tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
				tmsDriverVehicleLinkModel.setDriverId(tmsBaseDriverModel.getId());
				tmsDriverVehicleLinkModel.setDriverCode(tmsBaseDriverModel.getCode());
				tmsDriverVehicleLinkModel.setDriverName(tmsBaseDriverModel.getName());
				tmsDriverVehicleLinkModel.setVehicleId(driverVehicleLink.getVehicleId());
				tmsDriverVehicleLinkModel.setVehicleCode(driverVehicleLink.getVehicleCode());
				tmsDriverVehicleLinkModel.setVehicleName(driverVehicleLink.getVehicleName());
				tmsDriverVehicleLinkModel.setVehiclePlateNumber(driverVehicleLink.getVehiclePlateNumber());
				tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
				tmsDriverVehicleLinkModel.setCreatetime(date);
				tmsDriverVehicleLinkModel.setUpdateperson(user.getNickname());
				tmsDriverVehicleLinkModel.setUpdatetime(date);
				tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);
			}
		}

		// 司机证件关联不为空新增
		if (ToolKits.isNotEmpty(driverAddVO.getDriverPaperLinks())) {
			for (DriverPaperLinkAddVO driverPaperLink : driverAddVO.getDriverPaperLinks()) {
				ToolKits.isParamNull(driverPaperLink.getId(), "证件ID不为空");
				ToolKits.isParamNull(driverPaperLink.getCode(), "证件编码不为空");
				ToolKits.isParamNull(driverPaperLink.getType(), "证件类型不为空");

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

				// 司机证件保存
				TmsBasePaperModel tmsBasePaperModel = new TmsBasePaperModel();
				BeanUtils.copyProperties(driverPaperLink, tmsBasePaperModel);
				tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_DRIVER);
				tmsBasePaperModel.setPaperObjectId(tmsBaseDriverModel.getId());
				tmsBasePaperModel.setPaperObjectCode(tmsBaseDriverModel.getCode());
				tmsBasePaperModel.setPaperObjectName(tmsBaseDriverModel.getName());
				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);

				// 司机证件关联保存
				TmsDriverPaperLinkModel tmsDriverPaperLinkModel = new TmsDriverPaperLinkModel();
				tmsDriverPaperLinkModel.setLineid(ToolKits.getUUID());
				tmsDriverPaperLinkModel.setDriverId(tmsBaseDriverModel.getId());
				tmsDriverPaperLinkModel.setDriverCode(tmsBaseDriverModel.getCode());
				tmsDriverPaperLinkModel.setDriverName(tmsBaseDriverModel.getName());
				tmsDriverPaperLinkModel.setPaperId(tmsBasePaperModel.getId());
				tmsDriverPaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
				tmsDriverPaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
				tmsDriverPaperLinkModel.setCreateperson(user.getNickname());
				tmsDriverPaperLinkModel.setCreatetime(date);
				tmsDriverPaperLinkModel.setUpdateperson(user.getNickname());
				tmsDriverPaperLinkModel.setUpdatetime(date);
				tmsDriverPaperLinkMapper.save(tmsDriverPaperLinkModel);
			}
		}

		// 注册司机账号
		User adduser = new User();
		adduser.setCompanyId(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationId()) ? null : tmsBaseDriverModel.getAffiliationId().toString());
		adduser.setCompanyCode(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationCode()) ? null : tmsBaseDriverModel.getAffiliationCode());
		adduser.setCompanyName(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationName()) ? null : tmsBaseDriverModel.getAffiliationName());
		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> restMessage = userTmsResource.addTmsUser(adduser);
		if (!restMessage.isSuccess()) {
			throw new ParameterException("司机账号注册失败:" + restMessage.getMessage());
		}
	}

	/**
	 * 司机资料修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyDriverData(DriverModifyVO driverModifyVO) {
		ToolKits.isParamNull(driverModifyVO, "司机资料修改请求不为空");
		ToolKits.isParamNull(driverModifyVO.getId(), "司机ID不为空");
		ToolKits.isParamNull(driverModifyVO.getType(), "司机类型不为空");
		ToolKits.isParamNull(driverModifyVO.getPhoneNumber(), "手机号不为空");

		ToolKits.isParamNull(driverModifyVO.getDepartmentId(), "所属部门ID不能为空");
		ToolKits.isParamNull(driverModifyVO.getDepartmentCode(), "所属部门编号不能为空");
		ToolKits.isParamNull(driverModifyVO.getDepartmentName(), "所属部门名称不能为空");

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

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

		// 根据ID查询司机信息
		TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverMapper.findById(driverModifyVO.getId(), TmsBaseDriverModel.class);
		ToolKits.isParamNull(tmsBaseDriverModel, "司机ID【" + driverModifyVO.getId() + "】不存在");

		Assert.isTrue(tmsBaseDriverModel.getStatus() == StatusConstant.STATUS_ENABLE, "司机已停用不能修改");

		// 判断隶属机构
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			if (driverModifyVO.getType() == DataDictionaryConstant.DRIVER_TYPE_OWN) {
				if (ToolKits.isNotEmpty(driverModifyVO.getAffiliationCode()) || ToolKits.isNotEmpty(driverModifyVO.getAffiliationName())) {
					throw new BizException("司机归属为自有，隶属机构为空!");
				}
			} else {
				ToolKits.isParamNull(driverModifyVO.getAffiliationId(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverModifyVO.getAffiliationCode(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverModifyVO.getAffiliationName(), "隶属机构名称不为空");

				Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
				Restriction r1 = Restrictions.eq("id", driverModifyVO.getAffiliationId());
				Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
				ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + driverModifyVO.getAffiliationName() + "]");
				TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
				Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + driverModifyVO.getAffiliationName() + "】已被停用");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			if (driverModifyVO.getType() == DataDictionaryConstant.DRIVER_TYPE_OWN) {
				// 自有司机
				tmsBaseDriverModel.setAffiliationId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
				tmsBaseDriverModel.setAffiliationCode(user.getLoginUserCompanyInfo().getCode());
				tmsBaseDriverModel.setAffiliationName(user.getLoginUserCompanyInfo().getName());
			} else {
				ToolKits.isParamNull(driverModifyVO.getAffiliationId(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverModifyVO.getAffiliationCode(), "隶属机构编码不为空");
				ToolKits.isParamNull(driverModifyVO.getAffiliationName(), "隶属机构名称不为空");

				Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
				Restriction r1 = Restrictions.eq("id", driverModifyVO.getAffiliationId());
				Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
				ToolKits.isParamNull(tmsBaseCarriersModels, "未找到对应的隶属机构[" + driverModifyVO.getAffiliationName() + "]");
				TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
				Assert.isTrue(tmsBaseCarriersModel.getStatus() != StatusConstant.STATUS_DISABLE, "隶属机构【" + driverModifyVO.getAffiliationName() + "】已被停用");
			}
		}
		// 司机资料对象修改
		tmsBaseDriverModel.setType(driverModifyVO.getType());
		tmsBaseDriverModel.setAffiliationId(driverModifyVO.getAffiliationId());
		tmsBaseDriverModel.setAffiliationCode(driverModifyVO.getAffiliationCode());
		tmsBaseDriverModel.setAffiliationName(driverModifyVO.getAffiliationName());
		tmsBaseDriverModel.setAffiliationType(driverModifyVO.getAffiliationType());
		if (ObjectUtil.notEqual(tmsBaseDriverModel.getDepartmentId(), driverModifyVO.getDepartmentId())) {
			tmsBaseDriverModel.setDepartmentId(driverModifyVO.getDepartmentId());
			tmsBaseDriverModel.setDepartmentCode(driverModifyVO.getDepartmentCode());
			tmsBaseDriverModel.setDepartmentName(driverModifyVO.getDepartmentName());
		}
		tmsBaseDriverModel.setPhoneNumber(driverModifyVO.getPhoneNumber());
		tmsBaseDriverModel.setPassword(driverModifyVO.getPassword());
		tmsBaseDriverModel.setMemo(driverModifyVO.getMemo());
		tmsBaseDriverModel.setMaxTravelTime(driverModifyVO.getMaxTravelTime());
		tmsBaseDriverModel.setMaxMileage(driverModifyVO.getMaxMileage());
		tmsBaseDriverModel.setMaxDeliveryCustomer(driverModifyVO.getMaxDeliveryCustomer());
		tmsBaseDriverModel.setExtStr1(driverModifyVO.getExtStr1());
		tmsBaseDriverModel.setExtStr2(driverModifyVO.getExtStr2());
		tmsBaseDriverModel.setExtStr3(driverModifyVO.getExtStr3());
		tmsBaseDriverModel.setExtStr4(driverModifyVO.getExtStr4());
		tmsBaseDriverModel.setExtStr5(driverModifyVO.getExtStr5());
		tmsBaseDriverModel.setExtNum1(driverModifyVO.getExtNum1());
		tmsBaseDriverModel.setExtNum2(driverModifyVO.getExtNum2());
		tmsBaseDriverModel.setExtNum3(driverModifyVO.getExtNum3());
		tmsBaseDriverModel.setExtNum4(driverModifyVO.getExtNum4());
		tmsBaseDriverModel.setExtNum5(driverModifyVO.getExtNum5());
		tmsBaseDriverModel.setExtDate1(driverModifyVO.getExtDate1());
		tmsBaseDriverModel.setExtDate2(driverModifyVO.getExtDate2());
		tmsBaseDriverModel.setExtDate3(driverModifyVO.getExtDate3());
		tmsBaseDriverModel.setExtDate4(driverModifyVO.getExtDate4());
		tmsBaseDriverModel.setExtDate5(driverModifyVO.getExtDate5());
		tmsBaseDriverModel.setUpdateperson(user.getNickname());
		tmsBaseDriverModel.setUpdatetime(date);

		tmsBaseDriverMapper.update(tmsBaseDriverModel);

		// 删除司机车辆关联
		Criteria criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
		Restriction r1 = Restrictions.eq("driverId", driverModifyVO.getId());
		criteria.setRestriction(Restrictions.and(r1));
		tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);

		// 司机车辆关联修改不为空新增
		if (ToolKits.isNotEmpty(driverModifyVO.getDriverVehicleLinks())) {
			for (DriverVehicleLinkModifyVO driverVehicleLinkModifyVO : driverModifyVO.getDriverVehicleLinks()) {
				ToolKits.isParamNull(driverVehicleLinkModifyVO.getVehicleId(), "车辆ID不为空");
				ToolKits.isParamNull(driverVehicleLinkModifyVO.getVehicleCode(), "车辆编码不为空");
				ToolKits.isParamNull(driverVehicleLinkModifyVO.getVehiclePlateNumber(), "车牌号不为空");

				// 司机车辆关联保存
				TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
				tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
				tmsDriverVehicleLinkModel.setDriverId(tmsBaseDriverModel.getId());
				tmsDriverVehicleLinkModel.setDriverCode(tmsBaseDriverModel.getCode());
				tmsDriverVehicleLinkModel.setDriverName(tmsBaseDriverModel.getName());
				tmsDriverVehicleLinkModel.setVehicleId(driverVehicleLinkModifyVO.getVehicleId());
				tmsDriverVehicleLinkModel.setVehicleCode(driverVehicleLinkModifyVO.getVehicleCode());
				tmsDriverVehicleLinkModel.setVehicleName(driverVehicleLinkModifyVO.getVehicleName());
				tmsDriverVehicleLinkModel.setVehiclePlateNumber(driverVehicleLinkModifyVO.getVehiclePlateNumber());
				tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
				tmsDriverVehicleLinkModel.setCreatetime(date);
				tmsDriverVehicleLinkModel.setUpdateperson(user.getNickname());
				tmsDriverVehicleLinkModel.setUpdatetime(date);
				tmsDriverVehicleLinkMapper.save(tmsDriverVehicleLinkModel);
			}
		}

		// 司机证件关联不为空新增或修改
		if (ToolKits.isNotEmpty(driverModifyVO.getDriverPaperLinks())) {
			for (DriverPaperLinkModifyVO driverPaperLinkModifyVO : driverModifyVO.getDriverPaperLinks()) {
				ToolKits.isParamNull(driverPaperLinkModifyVO.getId(), "证件ID不为空");

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

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

					// 司机证件保存
					tmsBasePaperModel = new TmsBasePaperModel();
					BeanUtils.copyProperties(driverPaperLinkModifyVO, tmsBasePaperModel);
					tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_DRIVER);
					tmsBasePaperModel.setPaperObjectId(tmsBaseDriverModel.getId());
					tmsBasePaperModel.setPaperObjectCode(tmsBaseDriverModel.getCode());
					tmsBasePaperModel.setPaperObjectName(tmsBaseDriverModel.getName());
					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);

					// 司机证件关联保存
					TmsDriverPaperLinkModel tmsDriverPaperLinkModel = new TmsDriverPaperLinkModel();
					tmsDriverPaperLinkModel.setLineid(ToolKits.getUUID());
					tmsDriverPaperLinkModel.setDriverId(tmsBaseDriverModel.getId());
					tmsDriverPaperLinkModel.setDriverCode(tmsBaseDriverModel.getCode());
					tmsDriverPaperLinkModel.setDriverName(tmsBaseDriverModel.getName());
					tmsDriverPaperLinkModel.setPaperId(tmsBasePaperModel.getId());
					tmsDriverPaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
					tmsDriverPaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
					tmsDriverPaperLinkModel.setCreateperson(user.getNickname());
					tmsDriverPaperLinkModel.setCreatetime(date);
					tmsDriverPaperLinkModel.setUpdateperson(user.getNickname());
					tmsDriverPaperLinkModel.setUpdatetime(date);
					tmsDriverPaperLinkMapper.save(tmsDriverPaperLinkModel);
				} else {
					Assert.isTrue(tmsBasePaperModel.getStatus() == StatusConstant.STATUS_ENABLE, "证件已停用不能修改");

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

		// 同步司机用户信息
		User adduser = new User();
		adduser.setCompanyId(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationId()) ? null : tmsBaseDriverModel.getAffiliationId().toString());
		adduser.setCompanyCode(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationCode()) ? null : tmsBaseDriverModel.getAffiliationCode());
		adduser.setCompanyName(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationName()) ? null : tmsBaseDriverModel.getAffiliationName());
		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.setUsername(tmsBaseDriverModel.getPhoneNumber());

		RestMessage<Boolean> restMessage = userTmsResource.updateEnterpriseUser2(adduser);
		if (!restMessage.isSuccess()) {
			throw new ParameterException("司机用户信息修改失败:" + restMessage.getMessage());
		}
	}

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

		List<Long> ids = list.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).distinct().map(DriverModifyDeleteVO::getId).collect(Collectors.toList());
		ToolKits.isParamNull(ids, "司机ID不能为空");

		for (DriverModifyDeleteVO vo : list) {
			List<LineByCarriersDTO> dtoList = tmsCarrierMapper.findLineByCarriers(vo.getId());
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCarriersDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("司机[" + dtoList.get(0).getCarrierName() + "]正在被线路[" + str + "]使用,不能删除!");
			}

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

			// 删除线路承运方
			criteria = Criteria.forClass(TmsLineCarrierLinkModel.class);
			r1 = Restrictions.eq("carrierId", vo.getId());
			criteria.setRestriction(r1);
			tmsCarrierMapper.deleteByCriteria(criteria);

			// 删除司机车辆link表
			criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
			r1 = Restrictions.eq("driverId", vo.getId());
			criteria.setRestriction(r1);
			tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);

			// 删除司机
			tmsBaseDriverMapper.deleteById(vo.getId(), TmsBaseDriverModel.class);
		}

		// 删除司机用户
		String idStrs = ids.stream().map(Objects::toString).collect(Collectors.joining(","));
		RestMessage<Boolean> restMessage = userTmsResource.delEnterpriseUser(idStrs);
		if (restMessage.isSuccess()) {
			if (!restMessage.getData()) {
				throw new RuntimeException("删除司机用户失败");
			}
		} else {
			throw new RuntimeException("删除司机用户失败：" + restMessage.getMessage());
		}
	}

	/**
	 * 司机常跑线路查看
	 */
	@Override
	public List<DriverLineCheckDTO> listDriverLineData(DriverLineCheckVO driverLineCheckVO) {
		ToolKits.isParamNull(driverLineCheckVO, "司机常跑线路查看请求不为空");
		ToolKits.isParamNull(driverLineCheckVO.getId(), "司机ID为空");

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

		// 响应对象集合
		List<DriverLineCheckDTO> driverLineCheckDTOs = new ArrayList<DriverLineCheckDTO>();

		// 查询线路承运方关联关系为司机的数据
		Criteria criteria1 = Criteria.forClass(TmsLineCarrierLinkModel.class);
		Restriction r11 = Restrictions.eq("type", DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER);
		Restriction r12 = Restrictions.eq("carrierId", driverLineCheckVO.getId());
		criteria1.setRestriction(Restrictions.and(r11, r12));
		List<TmsLineCarrierLinkModel> tmsLineCarrierLinkModels = tmsLineCarrierLinkMapper.findByCriteria(criteria1);

		// 获取线路ID
		Long[] lineIds = (tmsLineCarrierLinkModels.stream().map(TmsLineCarrierLinkModel::getLineId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);

		// 查询对应的线路
		Criteria criteria2 = Criteria.forClass(TmsBaseLineModel.class);
		Restriction r21 = Restrictions.in("lineId", lineIds);
		Restriction r22 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r23 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria2.setRestriction(Restrictions.and(r21, r22, r23));
		criteria2.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseLineModel> tmsBaseTranslineModels = tmsBaseTranslineMapper.findByCriteria(criteria2);
		for (TmsBaseLineModel tmsBaseTranslineModel : tmsBaseTranslineModels) {
			// 响应对象
			DriverLineCheckDTO driverLineCheckDTO = new DriverLineCheckDTO();
			BeanUtils.copyProperties(tmsBaseTranslineModel, driverLineCheckDTO);
			driverLineCheckDTOs.add(driverLineCheckDTO);
		}

		return driverLineCheckDTOs;
	}

	/**
	 * 未绑/已绑车辆查询
	 */
	@Override
	public List<DriverVehicleLinkDTO> listDriverVehicleData(DriverVehicleLinkVO driverVehicleLinkVO) {
		ToolKits.isParamNull(driverVehicleLinkVO, "未绑/已绑车辆查询请求不为空");
		ToolKits.isParamNull(driverVehicleLinkVO.getId(), "司机ID为空");

		if (ToolKits.isNotEmpty(driverVehicleLinkVO.getWhether())) {
			Assert.isTrue(driverVehicleLinkVO.getWhether() == StatusConstant.WHETHER_NO || driverVehicleLinkVO.getWhether() == StatusConstant.WHETHER_YES, "是否绑定状态【" + driverVehicleLinkVO.getWhether() + "】错误");
		}

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

		// 响应对象集合
		List<DriverVehicleLinkDTO> driverVehicleLinkDTOs = new ArrayList<DriverVehicleLinkDTO>();

		// 查询司机车辆关联数据
		Criteria criteria1 = Criteria.forClass(TmsDriverVehicleLinkModel.class);
		Restriction r11 = Restrictions.eq("driverId", driverVehicleLinkVO.getId());
		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 criteria2 = Criteria.forClass(TmsBaseVehicleModel.class);
		Restriction r21 = null;
		if (ToolKits.isNotEmpty(driverVehicleLinkVO.getWhether())) {
			r21 = driverVehicleLinkVO.getWhether() == StatusConstant.WHETHER_NO ? Restrictions.notIn("id", vehicleIds) : Restrictions.in("id", vehicleIds);
		}
		// 权限控制
		Restriction r24 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r24 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r24 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		Restriction r22 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r23 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria2.setRestriction(Restrictions.and(r21, r22, r23, r24));
		criteria2.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria2);
		List<String> params = Lists.newArrayList(
				DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, // 车型类别
				DataDictionaryConstant.VEHICLE_TYPE // 车辆归属
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);
		for (TmsBaseVehicleModel tmsBaseVehicleModel : tmsBaseVehicleModels) {
			// 响应对象
			DriverVehicleLinkDTO driverVehicleLinkDTO = new DriverVehicleLinkDTO();
			// 车辆数据赋值
			driverVehicleLinkDTO.setVehicleId(tmsBaseVehicleModel.getId());
			driverVehicleLinkDTO.setVehicleCode(tmsBaseVehicleModel.getCode());
			driverVehicleLinkDTO.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
			driverVehicleLinkDTO.setVehicleType(tmsBaseVehicleModel.getType());
			driverVehicleLinkDTO.setAffiliationId(tmsBaseVehicleModel.getAffiliationId());
			driverVehicleLinkDTO.setAffiliationCode(tmsBaseVehicleModel.getAffiliationCode());
			driverVehicleLinkDTO.setAffiliationName(tmsBaseVehicleModel.getAffiliationName());

			driverVehicleLinkDTO.setVehicleAscriptionName(ConvertorUtil.getName(dicData, DataDictionaryConstant.VEHICLE_TYPE, String.valueOf(tmsBaseVehicleModel.getType())));
			driverVehicleLinkDTO.setVehicleAscription(tmsBaseVehicleModel.getType());

			if (ToolKits.isNotEmpty(tmsBaseVehicleModel.getVehicleTypeId())) {
				// 查询车辆对应的车型
				Criteria criteria3 = Criteria.forClass(TmsBaseVehicleTypeModel.class);
				Restriction r31 = Restrictions.eq("id", tmsBaseVehicleModel.getVehicleTypeId());
				Restriction r32 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
				criteria3.setRestriction(Restrictions.and(r31, r32));
				List<TmsBaseVehicleTypeModel> tmsBaseVehicleTypeModels = tmsBaseVehicleTypeMapper.findByCriteria(criteria3);
				Assert.isTrue(tmsBaseVehicleTypeModels.size() == 1, "车辆对应的车型数据不存在或重复");

				// 车型数据赋值
				driverVehicleLinkDTO.setBrand(tmsBaseVehicleTypeModels.get(0).getBrand());
				driverVehicleLinkDTO.setVehicleTypeCategory(tmsBaseVehicleTypeModels.get(0).getType());
				driverVehicleLinkDTO.setLength(tmsBaseVehicleTypeModels.get(0).getLength());
				driverVehicleLinkDTO.setWidth(tmsBaseVehicleTypeModels.get(0).getWidth());
				driverVehicleLinkDTO.setHeight(tmsBaseVehicleTypeModels.get(0).getHeight());
				driverVehicleLinkDTO.setWeight(tmsBaseVehicleTypeModels.get(0).getWeight());
				driverVehicleLinkDTO.setVolume(tmsBaseVehicleTypeModels.get(0).getVolume());
				driverVehicleLinkDTO.setVehicleTypeName(tmsBaseVehicleTypeModels.get(0).getName());
				driverVehicleLinkDTO.setVehicleTypeId(tmsBaseVehicleTypeModels.get(0).getId());
				driverVehicleLinkDTO.setVehicleTypeCode(tmsBaseVehicleTypeModels.get(0).getCode());
				driverVehicleLinkDTO.setVehicleTypeCategoryName(ConvertorUtil.getName(dicData, DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, String.valueOf(tmsBaseVehicleTypeModels.get(0).getType())));

			}

			driverVehicleLinkDTOs.add(driverVehicleLinkDTO);
		}

		return driverVehicleLinkDTOs;
	}

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

		for (DriverVehicleDeleteVO driverVehicleDeleteVO : driverVehicleDeleteVOs) {
			ToolKits.isParamNull(driverVehicleDeleteVO.getDriverId(), "司机ID不为空");
			ToolKits.isParamNull(driverVehicleDeleteVO.getVehicleId(), "车辆ID不为空");

			Criteria criteria = Criteria.forClass(TmsDriverVehicleLinkModel.class);
			Restriction r1 = Restrictions.eq("driverId", driverVehicleDeleteVO.getDriverId());
			Restriction r2 = Restrictions.eq("vehicleId", driverVehicleDeleteVO.getVehicleId());
			criteria.setRestriction(Restrictions.and(r1, r2));
			tmsDriverVehicleLinkMapper.deleteByCriteria(criteria);
		}
	}

	/**
	 * 司机证件关联查看
	 */
	@Override
	public List<DriverPaperLinkCheckDTO> listDriverPaperLinkCheckData(DriverPaperLinkCheckVO driverPaperLinkCheckVO) {
		ToolKits.isParamNull(driverPaperLinkCheckVO, "司机证件资料查看请求不为空");
		ToolKits.isParamNull(driverPaperLinkCheckVO.getId(), "司机ID为空");

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

		// 响应对象集合
		List<DriverPaperLinkCheckDTO> driverPaperLinkCheckDTOs = new ArrayList<DriverPaperLinkCheckDTO>();

		// 查询司机证件关联数据
		Criteria criteria1 = Criteria.forClass(TmsDriverPaperLinkModel.class);
		Restriction r11 = Restrictions.eq("driverId", driverPaperLinkCheckVO.getId());
		criteria1.setRestriction(Restrictions.and(r11));
		List<TmsDriverPaperLinkModel> tmsDriverPaperLinkModels = tmsDriverPaperLinkMapper.findByCriteria(criteria1);

		// 获取证件ID
		Long[] paperIds = (tmsDriverPaperLinkModels.stream().map(TmsDriverPaperLinkModel::getPaperId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);

		// 查询对应的证件
		Criteria criteria2 = Criteria.forClass(TmsBasePaperModel.class);
		Restriction r21 = Restrictions.in("id", paperIds);
		Restriction r23 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria2.setRestriction(Restrictions.and(r21, r23));
		criteria2.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBasePaperModel> tmsBasePaperModels = tmsBasePaperMapper.findByCriteria(criteria2);
		for (TmsBasePaperModel tmsBasePaperModel : tmsBasePaperModels) {
			// 响应对象
			DriverPaperLinkCheckDTO driverPaperLinkCheckDTO = new DriverPaperLinkCheckDTO();
			BeanUtils.copyProperties(tmsBasePaperModel, driverPaperLinkCheckDTO);
			driverPaperLinkCheckDTOs.add(driverPaperLinkCheckDTO);
		}

		return driverPaperLinkCheckDTOs;
	}

	/**
	 * 司机证件资料查看
	 */
	@Override
	public DriverPaperCheckDTO getDriverPaperCheckData(DriverPaperCheckVO driverPaperCheckVO) {
		ToolKits.isParamNull(driverPaperCheckVO, "司机证件资料查看请求不为空");
		ToolKits.isParamNull(driverPaperCheckVO.getId(), "证件ID不为空");

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

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

		Assert.isTrue(tmsBasePaperModels.size() == 1, "司机ID【" + driverPaperCheckVO.getId() + "】存在重复数据，查询失败");

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

		// 响应对象
		DriverPaperCheckDTO driverPaperCheckDTO = new DriverPaperCheckDTO();
		BeanUtils.copyProperties(tmsBasePaperModel, driverPaperCheckDTO);

		return driverPaperCheckDTO;
	}

	/**
	 * 司机证件资料新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addDriverPaperData(DriverPaperAddVO driverPaperAddVO) {
		ToolKits.isParamNull(driverPaperAddVO, "司机证件资料新增请求不为空");
		ToolKits.isParamNull(driverPaperAddVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(driverPaperAddVO.getDriverCode(), "司机编码不为空");
		ToolKits.isParamNull(driverPaperAddVO.getDriverName(), "司机名称不为空");
		ToolKits.isParamNull(driverPaperAddVO.getId(), "证件ID不为空");
		ToolKits.isParamNull(driverPaperAddVO.getCode(), "证件编码不为空");
		ToolKits.isParamNull(driverPaperAddVO.getType(), "证件类型不为空");

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

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

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

		// 司机证件保存
		TmsBasePaperModel tmsBasePaperModel = new TmsBasePaperModel();
		BeanUtils.copyProperties(driverPaperAddVO, tmsBasePaperModel);
		tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_DRIVER);
		tmsBasePaperModel.setPaperObjectId(driverPaperAddVO.getDriverId());
		tmsBasePaperModel.setPaperObjectCode(driverPaperAddVO.getDriverCode());
		tmsBasePaperModel.setPaperObjectName(driverPaperAddVO.getDriverName());
		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);

		// 司机证件关联保存
		TmsDriverPaperLinkModel tmsDriverPaperLinkModel = new TmsDriverPaperLinkModel();
		tmsDriverPaperLinkModel.setLineid(ToolKits.getUUID());
		tmsDriverPaperLinkModel.setDriverId(driverPaperAddVO.getDriverId());
		tmsDriverPaperLinkModel.setDriverCode(driverPaperAddVO.getDriverCode());
		tmsDriverPaperLinkModel.setDriverName(driverPaperAddVO.getDriverName());
		tmsDriverPaperLinkModel.setPaperId(tmsBasePaperModel.getId());
		tmsDriverPaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
		tmsDriverPaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
		tmsDriverPaperLinkModel.setCreateperson(user.getNickname());
		tmsDriverPaperLinkModel.setCreatetime(date);
		tmsDriverPaperLinkModel.setUpdateperson(user.getNickname());
		tmsDriverPaperLinkModel.setUpdatetime(date);
		tmsDriverPaperLinkMapper.save(tmsDriverPaperLinkModel);
	}

	/**
	 * 司机证件资料修改
	 */
	@Override
	public void modifyDriverPaperData(DriverPaperModifyVO driverPaperModifyVO) {
		ToolKits.isParamNull(driverPaperModifyVO, "司机证件资料修改请求不为空");
		ToolKits.isParamNull(driverPaperModifyVO.getId(), "司机证件ID不为空");

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

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

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

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

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

	/**
	 * 司机证件资料是否启用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void whetherEnableDriverPaperData(DriverPaperWhetherEnableVO driverPaperWhetherEnableVO) {
		ToolKits.isParamNull(driverPaperWhetherEnableVO, "司机证件资料是否启用请求不为空");
		ToolKits.isParamNull(driverPaperWhetherEnableVO.getDriverPaperIds(), "司机证件ID集不为空");
		ToolKits.isParamNull(driverPaperWhetherEnableVO.getStatus(), "状态不为空");

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

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

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

		for (DriverPaperByIdVO driverPaperByIdVO : driverPaperWhetherEnableVO.getDriverPaperIds()) {
			ToolKits.isParamNull(driverPaperByIdVO.getId(), "司机证件ID不为空");

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

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

	/**
	 * 司机证件资料删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteDriverPaperData(List<DriverPaperDeleteVO> driverPaperDeleteVOs) {
		ToolKits.isParamNull(driverPaperDeleteVOs, "司机证件资料删除请求不为空");

		for (DriverPaperDeleteVO driverPaperDeleteVO : driverPaperDeleteVOs) {
			ToolKits.isParamNull(driverPaperDeleteVO.getDriverId(), "司机ID不为空");
			ToolKits.isParamNull(driverPaperDeleteVO.getPaperId(), "证件ID不为空");

			// 证件资料删除
			long count = tmsBasePaperMapper.deleteById(driverPaperDeleteVO.getPaperId(), TmsBasePaperModel.class);
			if (count == 1) {
				// 司机证件关联删除
				Criteria criteria = Criteria.forClass(TmsDriverPaperLinkModel.class);
				Restriction r1 = Restrictions.eq("driverId", driverPaperDeleteVO.getDriverId());
				Restriction r2 = Restrictions.eq("paperId", driverPaperDeleteVO.getPaperId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				tmsDriverPaperLinkMapper.deleteByCriteria(criteria);
			} else {
				throw new ParameterException("证件资料删除失败");
			}
		}
	}

	/**
	 * 司机导入撤销
	 *
	 * @param batchno
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void rollbackData(String batchno) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(batchno, "导入批次号不能为空");

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

		for (TmsBaseDriverModel model : tmsBaseDriverModels) {
			List<LineByCarriersDTO> dtoList = tmsCarrierMapper.findLineByCarriers(model.getId());
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCarriersDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("司机[" + dtoList.get(0).getCarrierName() + "]正在被线路[" + str + "]使用,不能删除!");
			}

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

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

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

			// 删除司机
			tmsBaseDriverMapper.deleteById(model.getId(), TmsBaseDriverModel.class);
		}
	}

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

		// 响应对象集合
		List<DriverImportPageDTO> driverImportPageDTOs = new ArrayList<DriverImportPageDTO>();

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

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

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = Restrictions.eq("importBatchNumber", importPageVO.getImportBatchNumber());
		Restriction r2 = Restrictions.eq("source", DataDictionaryConstant.SOURCE_EXCEL);
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// 权限控制
		Restriction r4 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r4 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);
		for (TmsBaseDriverModel tmsBaseDriverModel : tmsBaseDriverModels) {
			// 响应对象
			DriverImportPageDTO driverImportPageDTO = new DriverImportPageDTO();
			BeanUtils.copyProperties(tmsBaseDriverModel, driverImportPageDTO);
			driverImportPageDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(driverImportPageDTO.getType())));
			driverImportPageDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(driverImportPageDTO.getStatus())));
			driverImportPageDTOs.add(driverImportPageDTO);
		}

		Page<DriverImportPageDTO> page = PageUtils.getPage(driverImportPageDTOs);
		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 RestMessage.newInstance(true, "查询成功", page);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchImportDriverData(MultipartFile file, String importBatchNumber) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		// 导入文件判断
		FileUtils.multipartFileJudge(file);
		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.DRIVER_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<DriverImportStdVO> importVOClass = DriverImportStdVO.class;
			List<DriverImportStdVO> 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.DRIVER_IMPORT_LOG, logAddVO);
			e.printStackTrace();
			throw new ParameterException(e.getMessage());
		}
	}

	// 司机异步保存导入失败日志
	@RabbitListener(queues = RabbitMQConstant.DRIVER_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<DriverImportStdVO> importVOClass = DriverImportStdVO.class;
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.DRIVER_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<DriverImportStdVO> 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 (DriverImportStdVO importVO : importVOs) {
			if (ToolKits.isEmpty(importVO.getCode())) {
				throw new PrologException("第" + i + "行导入数据,司机编码不能为空");
			}
			if (ToolKits.isEmpty(importVO.getName())) {
				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.getPhoneNumber())) {
				throw new PrologException("第" + i + "行导入数据,手机号不能为空");
			}
			if (ToolKits.isEmpty(importVO.getPassword())) {
				throw new PrologException("第" + i + "行导入数据,密码不能为空");
			}
			i++;
		}
		// 验证导入文件中司机编号是否重复
		Map<String, Long> codeMap = importVOs.stream().collect(Collectors.groupingBy(DriverImportStdVO::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(DriverImportStdVO::getName, 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 (DriverImportStdVO vo : importVOs) {
			if (ToolKits.isNotEmpty(vo.getPhoneNumber()) && !Validator.isMobile(vo.getPhoneNumber())) {
				// throw new ParameterException(String.format("%s手机号不符合规范",
				// vo.getPhoneNumber()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,手机号格式不符合规范!");
			}
			// 司机对象
			TmsBaseDriverModel tmsBaseDriverModel = new TmsBaseDriverModel();
			BeanUtils.copyProperties(vo, tmsBaseDriverModel);

			// 部门验证
			List<Organization> data1 = Lists.newArrayList();
			tmsBaseDriverModel.setType(Integer.parseInt(vo.getType()));

			// 判断司机编码是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseDriverModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseDriverMapper, "司机编码[" + vo.getCode() + "]已存在");
			PublicMethods.isThereAnyinformation(TmsBaseDriverModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseDriverMapper, "司机编码[" + vo.getCode() + "]已停用,请直接启用!");
			// 判断隶属机构
			if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
				// 自有账号
				// 所属部门名称判定是否存在
				RestMessage<List<Organization>> departMents = userTmsResource.search(null, vo.getDepartmentName(), null, String.valueOf(user.getEnterpriseId()));
				if (!departMents.isSuccess()) {
					throw new RuntimeException("部门【" + vo.getDepartmentName() + "】不存在!");
				} else {
					data1 = departMents.getData();
					if (ToolKits.isEmpty(data1)) {
						throw new RuntimeException("部门【" + vo.getDepartmentName() + "】不存在，请先维护部门!!");
					}
					if (!(user.getDepts().contains(data1.get(0).getId()))) {
						throw new RuntimeException("部门【" + vo.getDepartmentName() + "】不存在，请先维护部门!");
					}
				}
				if (DataDictionaryConstant.DRIVER_TYPE_ZY.equals(vo.getType())) {
					if (ToolKits.isNotEmpty(vo.getAffiliationCode()) || ToolKits.isNotEmpty(vo.getAffiliationName())) {
						// throw new BizException("自有账号导入,司机归属为自有，隶属机构需为空!");
						tmsBaseDriverModel.setAffiliationCode(null);
						tmsBaseDriverModel.setAffiliationName(null);
					}
				} else {
					ToolKits.isParamNull(vo.getAffiliationCode(), "隶属机构编码不为空");
					ToolKits.isParamNull(vo.getAffiliationName(), "隶属机构名称不为空");

					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;
					Restriction r5 = null;

					// 判断是否为部门权限
					if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
						// 部门权限
						r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
					} else {
						r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
						r5 = Restrictions.in("id", user.getCarriers().stream().toArray(Long[]::new));
					}

					criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5));
					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() + "】已被停用");

					tmsBaseDriverModel.setAffiliationId(tmsBaseCarriersModel.getId());
					if (DataDictionaryConstant.DRIVER_TYPE_GK.equals(vo.getType())) {
						tmsBaseDriverModel.setAffiliationType(DataDictionaryConstant.DRIVER_TYPE_ANCHORED);
					} else {
						tmsBaseDriverModel.setAffiliationType(DataDictionaryConstant.DRIVER_TYPE_TEMPORARY);
					}
				}
			} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
				// 非自有账号 顶级部门取得
				RestMessage<List<Organization>> departMents2 = userTmsResource.search("0", null, null, String.valueOf(user.getEnterpriseId()));
				if (!departMents2.isSuccess()) {
					throw new RuntimeException("供应商的顶级部门不存在!");
				} else {
					data1 = departMents2.getData();
				}
				if (DataDictionaryConstant.DRIVER_TYPE_ZY.equals(vo.getType())) {
					// 自有司机
					tmsBaseDriverModel.setAffiliationId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
					tmsBaseDriverModel.setAffiliationCode(user.getLoginUserCompanyInfo().getCode());
					tmsBaseDriverModel.setAffiliationName(user.getLoginUserCompanyInfo().getName());
				} else {
					ToolKits.isParamNull(vo.getAffiliationCode(), "隶属机构编码不为空");
					ToolKits.isParamNull(vo.getAffiliationName(), "隶属机构名称不为空");

					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;
					r4 = Restrictions.eq("id", Long.valueOf(user.getLoginUserCompanyInfo().getId()));

					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() + "】已被停用");

					tmsBaseDriverModel.setAffiliationId(tmsBaseCarriersModel.getId());
					if (DataDictionaryConstant.DRIVER_TYPE_GK.equals(vo.getType())) {
						tmsBaseDriverModel.setAffiliationType(DataDictionaryConstant.DRIVER_TYPE_ANCHORED);
					} else {
						tmsBaseDriverModel.setAffiliationType(DataDictionaryConstant.DRIVER_TYPE_TEMPORARY);
					}
				}
			}

			// 司机ID
			String driverId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.DRIVER_ID, "L");
			ToolKits.isParamNull(driverId, "司机ID生成失败");

			List<TmsDriverVehicleLinkModel> tmsDriverVehicleLinkModels = Lists.newArrayList();
			// 判断车牌号
			if (ToolKits.isNotEmpty(vo.getPlateNumber())) {
				String[] vehiclePlateNumbers = vo.getPlateNumber().split(",");
				for (String vehiclePlateNumber : vehiclePlateNumbers) {
					Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
					Restriction r1 = Restrictions.eq("plateNumber", vehiclePlateNumber);
					criteria.setRestriction(r1);
					List<TmsBaseVehicleModel> tmsBaseVehicleModels = tmsBaseVehicleMapper.findByCriteria(criteria);
					ToolKits.isParamNull(tmsBaseVehicleModels, "未找到对应的车牌号[" + vo.getAffiliationName() + "]");
					TmsBaseVehicleModel tmsBaseVehicleModel = tmsBaseVehicleModels.get(0);
					Assert.isTrue(tmsBaseVehicleModel.getStatus() != StatusConstant.STATUS_DISABLE, "车牌号【" + vo.getAffiliationName() + "】已被停用");

					TmsDriverVehicleLinkModel tmsDriverVehicleLinkModel = new TmsDriverVehicleLinkModel();
					tmsDriverVehicleLinkModel.setLineid(ToolKits.getUUID());
					tmsDriverVehicleLinkModel.setDriverId(Long.valueOf(driverId));
					tmsDriverVehicleLinkModel.setDriverCode(vo.getCode());
					tmsDriverVehicleLinkModel.setDriverName(vo.getName());
					tmsDriverVehicleLinkModel.setVehicleId(tmsBaseVehicleModel.getId());
					tmsDriverVehicleLinkModel.setVehicleCode(tmsBaseVehicleModel.getCode());
					tmsDriverVehicleLinkModel.setVehicleName("");
					tmsDriverVehicleLinkModel.setVehiclePlateNumber(tmsBaseVehicleModel.getPlateNumber());
					tmsDriverVehicleLinkModel.setCreateperson(user.getNickname());
					tmsDriverVehicleLinkModel.setCreatetime(date);

					tmsDriverVehicleLinkModels.add(tmsDriverVehicleLinkModel);
				}
			}

			// 司机资料赋值
			tmsBaseDriverModel.setId(Long.valueOf(driverId));
			tmsBaseDriverModel.setStatus(StatusConstant.STATUS_ENABLE);
			tmsBaseDriverModel.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			tmsBaseDriverModel.setImportBatchNumber(importBatchNumber);
			tmsBaseDriverModel.setCreatetime(new Date());
			tmsBaseDriverModel.setCreateperson(user.getNickname());
			tmsBaseDriverModel.setUpdateperson(user.getNickname());
			tmsBaseDriverModel.setUpdatetime(new Date());
			tmsBaseDriverModel.setEnterpriseId(user.getEnterpriseId());
			tmsBaseDriverModel.setEnterpriseName(user.getEnterpriseName());
			// 部门
			tmsBaseDriverModel.setDepartmentId(data1.get(0).getId());
			tmsBaseDriverModel.setDepartmentCode(data1.get(0).getCode());
			tmsBaseDriverModel.setDepartmentName(data1.get(0).getName());
			tmsBaseDriverMapper.save(tmsBaseDriverModel);
			// 司机-车辆link表新增
			if (ToolKits.isNotEmpty(tmsDriverVehicleLinkModels)) {
				tmsDriverVehicleLinkMapper.saveBatch(tmsDriverVehicleLinkModels);
			}
			i++;

			// 注册司机账号
			User adduser = new User();
			adduser.setCompanyId(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationId()) ? null : tmsBaseDriverModel.getAffiliationId().toString());
			adduser.setCompanyCode(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationCode()) ? null : tmsBaseDriverModel.getAffiliationCode());
			adduser.setCompanyName(ToolKits.isEmpty(tmsBaseDriverModel.getAffiliationName()) ? null : tmsBaseDriverModel.getAffiliationName());
			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> restMessage = userTmsResource.addTmsUser(adduser);
			if (!restMessage.isSuccess()) {
				throw new ParameterException("司机账号注册失败:" + restMessage.getMessage());
			}
		}
		return i;
	}

	/**
	 * 司机资料弹窗
	 *
	 * @param driverPopupVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DriverPopupDTO> popupDriverSelfRegData(DriverPopupVO driverPopupVO) {
		ToolKits.isParamNull(driverPopupVO, "司机资料查询请求不为空");
		ToolKits.isParamNull(driverPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(driverPopupVO.getPageSize(), "页数不为空");

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

		// 响应对象集合
		List<DriverPopupDTO> driverPopupDTOs = new ArrayList<DriverPopupDTO>();

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

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

		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = ToolKits.isEmpty(driverPopupVO.getCode()) ? null : Restrictions.like("code", "%" + driverPopupVO.getCode() + "%");
		Restriction r2 = ToolKits.isEmpty(driverPopupVO.getName()) ? null : Restrictions.like("name", "%" + driverPopupVO.getName() + "%");
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r4 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r6 = Restrictions.eq(TmsBaseDriverModel::getCreateperson, user.getNickname());
		// 权限控制
		Restriction r5 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			r5 = ToolKits.isEmpty(user.getDepts()) ? null : Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			// 显示司机的隶属机构id为当前登录账号单位id 的司机 或 司机的所属组织id为当前登录账号单位id 的司机
			r5 = Restrictions.eq("affiliationId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseDriverModel> tmsBaseDriverModels = tmsBaseDriverMapper.findByCriteria(criteria);
		for (TmsBaseDriverModel tmsBaseDriverModel : tmsBaseDriverModels) {
			// 响应对象
			DriverPopupDTO driverPopupDTO = new DriverPopupDTO();
			BeanUtils.copyProperties(tmsBaseDriverModel, driverPopupDTO);

			// 查询司机对应的车辆信息
			List<TmsBaseVehicleModel> tmsBaseVehicleModels = vehicleService.listVehicleByDriverIdDataSelfReg(tmsBaseDriverModel.getId());
			if (tmsBaseVehicleModels.size() == 1) {
				driverPopupDTO.setVehicleId(tmsBaseVehicleModels.get(0).getId());
				driverPopupDTO.setVehicleCode(tmsBaseVehicleModels.get(0).getCode());
				driverPopupDTO.setVehiclePlateNumber(tmsBaseVehicleModels.get(0).getPlateNumber());
				driverPopupDTO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
				driverPopupDTO.setVehicleTypeCode(tmsBaseVehicleModels.get(0).getVehicleTypeCode());
				driverPopupDTO.setVehicleTypeName(tmsBaseVehicleModels.get(0).getVehicleTypeName());
				// STD-DEVELOP20220823 lhb 司机默认带出唯一车辆 20220913 start
				driverPopupDTO.setVehType(tmsBaseVehicleModels.get(0).getType());
				driverPopupDTO.setVehTypeName(PublicMethods.change(restMessage1, String.valueOf(tmsBaseVehicleModels.get(0).getType())));

				if (ToolKits.isNotEmpty(tmsBaseVehicleModels.get(0).getVehicleTypeId())) {
					VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
					vehicleTypeCheckVO.setVehicleTypeId(tmsBaseVehicleModels.get(0).getVehicleTypeId());
					VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
					driverPopupDTO.setVolume(vehicleTypeCheckDTO.getVolume());
					driverPopupDTO.setWeight(vehicleTypeCheckDTO.getWeight());
				}
				// STD-DEVELOP20220823 lhb 司机默认带出唯一车辆 20220913 end
			}

			driverPopupDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(driverPopupDTO.getType())));
			driverPopupDTOs.add(driverPopupDTO);
		}

		Page<DriverPopupDTO> page = PageUtils.getPage(driverPopupDTOs);
		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;
	}

}
