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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.DateUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.cs.platform.power.model.Organization;
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.LmsTmsZoneMapper;
import com.prolog.rdc.tms.dao.mapper.TmsApprovalBusinessDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsApprovalBusinessLogMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarriersMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarriersTempMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseContainerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDeliverymanMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDeviceMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDriverMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseFeeMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBasePaperMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseVehicleMapper;
import com.prolog.rdc.tms.dao.mapper.TmsCarrierPaperLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsDepartmentBaseLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLineCarrierLinkMapper;
import com.prolog.rdc.tms.dao.mapper.TmsSettlementTypeModelMapper;
import com.prolog.rdc.tms.model.dto.ExpensePopDTO;
import com.prolog.rdc.tms.model.dto.SettlementTypeDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StandardTemplateDetailsListDTO;
import com.prolog.rdc.tms.model.dto.basicData.BaseFeeDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierApprovalInfoDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierApprovalLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierApprovalNodeDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierByIdPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierImportPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarriersByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.LineByCarriersDTO;
import com.prolog.rdc.tms.model.dto.basicData.UserCarrierPopupDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.ContainerDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeviceDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.PaperDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePagingQueryDTO;
import com.prolog.rdc.tms.model.entity.LmsTmsZoneModel;
import com.prolog.rdc.tms.model.entity.line.TmsApprovalBusDetailModel;
import com.prolog.rdc.tms.model.entity.line.TmsApprovalBusLogModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarriersModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarriersTempModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseContainerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDeliverymanModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDeviceModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDriverModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseFeeModel;
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.TmsCarrierPaperLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsDepartmentBaseLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsLineCarrierLinkModel;
import com.prolog.rdc.tms.model.entity.line.TmsSettlementTypeModel;
import com.prolog.rdc.tms.model.vo.AccountSaveInfoDetailVO;
import com.prolog.rdc.tms.model.vo.AccountSaveInfoVO;
import com.prolog.rdc.tms.model.vo.ApprovalBusinessNodeVO;
import com.prolog.rdc.tms.model.vo.FeeSaveInfoDetailVO;
import com.prolog.rdc.tms.model.vo.FeeSaveInfoVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.ApprovalOpenVO;
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.basicData.CarrierAddVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalInfoVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalInvalidVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalRetractVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalSubmitVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierByIdPageVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierCopyVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierModifySysFlagVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierPageVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierRelationInfoVO;
import com.prolog.rdc.tms.model.vo.basicData.CarriersByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.SettlementTypeVO;
import com.prolog.rdc.tms.model.vo.basicData.UserCarrierPopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.CarrierPaperLinkVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.CarrierPaperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CarriersImportStdVO;
import com.prolog.rdc.tms.rmi.feign.UserFeign;
import com.prolog.rdc.tms.service.ApprovalBusinessService;
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.basicData.CarriersService;
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.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;


/**
 *
 *
 * 基础数据-承运商管理实现层
 *
 * @className :CarriersServiceImpl
 * @author :xyf
 * @creationTime :2022/03/31
 * @description :
 *
 * @modifier :xyf
 * @modifyTime :2022/03/31
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class CarriersServiceImpl implements CarriersService {

	@Autowired
	private TmsBaseCarriersMapper tmsBaseCarriersMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private TmsLineCarrierLinkMapper tmsCarrierMapper;

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@Autowired
	private ApprovalBusinessService approvalBusinessService;

	@Autowired
	private TmsApprovalBusinessLogMapper tmsApprovalBusinessLogMapper;

	@Autowired
	private TmsApprovalBusinessDetailMapper tmsApprovalBusinessDetailMapper;

	@Autowired
	private TmsBaseCarriersTempMapper tmsBaseCarriersTempMapper;

	@Autowired
	private UserFeign userFeign;

	@Autowired
	private LmsTmsZoneMapper lmsTmsZoneMapper;

	@Autowired
	private AmqpTemplate amqpTemplate;

	@Autowired
	private TmsBaseFeeMapper tmsBaseFeeMapper;
	@Autowired
	private TmsSettlementTypeModelMapper settlementTypeModelMapper;

	@Autowired
	private TmsCarrierPaperLinkMapper tmsCarrierPaperLinkMapper;

	@Autowired
	private TmsBasePaperMapper tmsBasePaperMapper;

	@Autowired
	private TmsBaseDriverMapper tmsBaseDriverMapper;

	@Autowired
	private TmsBaseDeliverymanMapper tmsBaseDeliverymanMapper;

	@Autowired
	private TmsBaseVehicleMapper tmsBaseVehicleMapper;

	@Autowired
	private TmsBaseContainerMapper tmsBaseContainerMapper;

	@Autowired
	private TmsBaseDeviceMapper tmsBaseDeviceMapper;

	@Autowired
	private TmsDepartmentBaseLinkMapper tmsDepartmentBaseLinkMapper;

	/**
	 * 承运商分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<CarrierPageDTO> pageCarrierData(CarrierPageVO carrierPageVO) {
		ToolKits.isParamNull(carrierPageVO, "承运商分页查询请求不为空");
		ToolKits.isParamNull(carrierPageVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(carrierPageVO.getPageSize(), "页数不为空");

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

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

		// 合作性质状态
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.COOPERATIVE_NATURE, null, null, null);

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

		// 银行
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.BANK, null, null, null);

		List<String> params = Lists.newArrayList(DataDictionaryConstant.CARRIER_SYS_FLAG // 承运商系统标志
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 start
		carrierPageVO.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			// 判断是否为部门权限
			carrierPageVO.setIsDept("Y");
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				carrierPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				carrierPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				carrierPageVO.setCarrierIds(user.getCarriers().stream().toArray(Long[]::new));
				carrierPageVO.setIsData("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			Long[] carrierIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			carrierPageVO.setCarrierIds(carrierIds);
			carrierPageVO.setIsData("Y");
		} else {
			// 其他账号
			throw new RuntimeException("当前账号没有权限");
		}
		PageUtils.startPage(carrierPageVO.getPageNum(), carrierPageVO.getPageSize());
		List<CarrierPageDTO> tmsBaseCarriersModels = tmsBaseCarriersMapper.pageCarrierData(carrierPageVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 end

		for (CarrierPageDTO tmsBaseCarriersModel : tmsBaseCarriersModels) {
			// 响应对象
			CarrierPageDTO carrierPageDTO = new CarrierPageDTO();
			BeanUtils.copyProperties(tmsBaseCarriersModel, carrierPageDTO);
			carrierPageDTO.setCooperativeNatureName(PublicMethods.change(restMessage1, String.valueOf(carrierPageDTO.getCooperativeNature())));
			carrierPageDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(carrierPageDTO.getStatus())));
			carrierPageDTO.setBankName(PublicMethods.change(restMessage3, String.valueOf(carrierPageDTO.getBank())));

			carrierPageDTO.setCarrierSysFlagName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SYS_FLAG, String.valueOf(carrierPageDTO.getCarrierSysFlag())));
			carrierPageDTOs.add(carrierPageDTO);
		}
		// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 start
		Page<CarrierPageDTO> page = PageUtils.getPage(carrierPageDTOs);
		if (tmsBaseCarriersModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<CarrierPageDTO> p0 = (com.github.pagehelper.Page<CarrierPageDTO>) tmsBaseCarriersModels;
			// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 end
			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<CarrierPopupDTO> popupCarrierData(CarrierPopupVO carrierPopupVO) {
		ToolKits.isParamNull(carrierPopupVO, "承运商弹窗查询请求不为空");
		ToolKits.isParamNull(carrierPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(carrierPopupVO.getPageSize(), "页数不为空");

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

		// 合作性质状态
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.COOPERATIVE_NATURE, null, null, null);

		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);
		carrierPopupVO.setEnterpriseId(user.getEnterpriseId());
		carrierPopupVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			carrierPopupVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				carrierPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				carrierPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				carrierPopupVO.setCarrierIds(user.getCarriers().stream().toArray(Long[]::new));
				carrierPopupVO.setIsData("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			Long[] carrierIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			carrierPopupVO.setCarrierIds(carrierIds);
			carrierPopupVO.setIsData("Y");
		} else {
			// 其他账号
			throw new RuntimeException("当前账号没有权限");
		}
		PageUtils.startPage(carrierPopupVO.getPageNum(), carrierPopupVO.getPageSize());
		List<CarrierPopupDTO> tmsBaseCarriersModels = tmsBaseCarriersMapper.popupCarrierData(carrierPopupVO);

		for (CarrierPopupDTO carrierPopupDTO : tmsBaseCarriersModels) {
			// 响应对象
			carrierPopupDTO.setCooperativeNatureName(PublicMethods.change(restMessage1, String.valueOf(carrierPopupDTO.getCooperativeNature())));
			carrierPopupDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(carrierPopupDTO.getStatus())));
			carrierPopupDTO.setCarrierSysFlag(carrierPopupDTO.getCarrierSysFlag());
		}
		Page<CarrierPopupDTO> page = PageUtils.getPage(tmsBaseCarriersModels);
		return page;
	}

	/**
	 * 用户承运商弹框
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<UserCarrierPopupDTO> popupUserCarrierData(UserCarrierPopupVO userCarrierPopupVO) {
		ToolKits.isParamNull(userCarrierPopupVO, "承运商弹窗查询请求不为空");
		ToolKits.isParamNull(userCarrierPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(userCarrierPopupVO.getPageSize(), "页数不为空");

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

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

		// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 start
		userCarrierPopupVO.setEnterpriseId(user.getEnterpriseId());
		userCarrierPopupVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			userCarrierPopupVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				userCarrierPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				userCarrierPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				userCarrierPopupVO.setCarrierIds(user.getCarriers().stream().toArray(Long[]::new));
				userCarrierPopupVO.setIsData("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			Long[] carrierIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			userCarrierPopupVO.setCarrierIds(carrierIds);
			userCarrierPopupVO.setIsData("Y");
		} else {
			// 其他账号
			throw new RuntimeException("当前账号没有权限");
		}
		PageUtils.startPage(userCarrierPopupVO.getPageNum(), userCarrierPopupVO.getPageSize());
		List<UserCarrierPopupDTO> tmsBaseCarriersModels = tmsBaseCarriersMapper.popupUserCarrierData(userCarrierPopupVO);

		for (UserCarrierPopupDTO tmsBaseCarriersModel : tmsBaseCarriersModels) {
			// 响应对象
			UserCarrierPopupDTO userCarrierPopupDTO = new UserCarrierPopupDTO();
			userCarrierPopupDTO.setId(tmsBaseCarriersModel.getId());
			userCarrierPopupDTO.setCarrierId(tmsBaseCarriersModel.getId());
			userCarrierPopupDTO.setCarrierCode(tmsBaseCarriersModel.getCarrierCode());
			userCarrierPopupDTO.setCarrierName(tmsBaseCarriersModel.getCarrierName());
			userCarrierPopupDTOs.add(userCarrierPopupDTO);
		}

		Page<UserCarrierPopupDTO> page = PageUtils.getPage(userCarrierPopupDTOs);
		if (tmsBaseCarriersModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<UserCarrierPopupDTO> p0 = (com.github.pagehelper.Page<UserCarrierPopupDTO>) tmsBaseCarriersModels;
			// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 end
			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;
	}

	/**
	 * 承运商查看
	 */
	@Override
	public CarrierCheckDTO getCarrierData(Long id) throws Exception {
		ToolKits.isParamNull(id, "承运商ID不为空");

		List<String> params = Lists.newArrayList(DataDictionaryConstant.COOPERATIVE_NATURE,
				DataDictionaryConstant.CARRIER_SYS_FLAG,// 承运商系统标志
				DataDictionaryConstant.CARRIERS_TYPE
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		// 根据ID查询承运商对象
		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(id, TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID【" + id + "】不存在");

		CarrierCheckDTO carrierCheckDTO = new CarrierCheckDTO();
		BeanUtils.copyProperties(tmsBaseCarriersModel, carrierCheckDTO);

		carrierCheckDTO.setCarrierSysFlagName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SYS_FLAG, String.valueOf(carrierCheckDTO.getCarrierSysFlag())));
		carrierCheckDTO.setCooperativeNatureName(ConvertorUtil.getName(dicData, DataDictionaryConstant.COOPERATIVE_NATURE, String.valueOf(carrierCheckDTO.getCooperativeNature())));
		carrierCheckDTO.setCarriersTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIERS_TYPE, String.valueOf(carrierCheckDTO.getCarriersType())));

		return carrierCheckDTO;
	}

	/**
	 * 承运商新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addCarrierData(CarrierAddVO carrierAddVO) {
		ToolKits.isParamNull(carrierAddVO, "承运商新增请求不为空");
		ToolKits.isParamNull(carrierAddVO.getCode(), "承运商编码不为空");
		ToolKits.isParamNull(carrierAddVO.getName(), "承运商名称不为空");
		ToolKits.isParamNull(carrierAddVO.getDepartmentId(), "所属部门ID不能为空");
		ToolKits.isParamNull(carrierAddVO.getDepartmentCode(), "所属部门编号不能为空");
		ToolKits.isParamNull(carrierAddVO.getDepartmentName(), "所属部门名称不能为空");
		ToolKits.isParamNull(carrierAddVO.getCarriersType(), "公司类型不能为空");

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

		// 判断承运商编码是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "code", carrierAddVO.getCode(), "enterpriseId", user.getEnterpriseId(), tmsBaseCarriersMapper, "承运商编码已存在");
		// 判断承运商名称是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "name", carrierAddVO.getName(), "enterpriseId", user.getEnterpriseId(), tmsBaseCarriersMapper, "承运商名称已存在");

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CARRIER_ID, "L");
		ToolKits.isParamNull(id, "承运商ID生成失败");

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

		TmsBaseCarriersModel tmsBaseCarriersModel = new TmsBaseCarriersModel();
		BeanUtils.copyProperties(carrierAddVO, tmsBaseCarriersModel);
		tmsBaseCarriersModel.setId(Long.valueOf(id));
		tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_CREATE);
		tmsBaseCarriersModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseCarriersModel.setDepartmentId(carrierAddVO.getDepartmentId());
		tmsBaseCarriersModel.setDepartmentCode(carrierAddVO.getDepartmentCode());
		tmsBaseCarriersModel.setDepartmentName(carrierAddVO.getDepartmentName());
		tmsBaseCarriersModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseCarriersModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseCarriersModel.setCreatetime(date);
		tmsBaseCarriersModel.setCreateperson(user.getNickname());
		tmsBaseCarriersModel.setUpdatetime(date);
		tmsBaseCarriersModel.setUpdateperson(user.getNickname());
		tmsBaseCarriersModel.setCarrierSysFlag(carrierAddVO.getCarrierSysFlag());
		tmsBaseCarriersMapper.save(tmsBaseCarriersModel);

		if (CollectionUtil.isNotEmpty(carrierAddVO.getCarrierPaperVOList())) {
			carrierAddVO.getCarrierPaperVOList().forEach(vo -> {
				CarrierPaperLinkVO carrierPaperLinkVO = BeanUtil.copyProperties(vo, CarrierPaperLinkVO.class);
				carrierPaperLinkVO.setCarrierId(tmsBaseCarriersModel.getId());
				carrierPaperLinkVO.setCarrierCode(tmsBaseCarriersModel.getCode());
				carrierPaperLinkVO.setCarrierName(tmsBaseCarriersModel.getName());
				addCarrierPaper(carrierPaperLinkVO);
			});

		}
	}

	/**
	 * 承运商复制
	 */
	@Override
	public void copyCarrierData(CarrierCopyVO carrierCopyVO) {
		ToolKits.isParamNull(carrierCopyVO, "承运商复制请求不为空");
		ToolKits.isParamNull(carrierCopyVO.getCode(), "承运商编码不为空");
		ToolKits.isParamNull(carrierCopyVO.getName(), "承运商名称不为空");
		ToolKits.isParamNull(carrierCopyVO.getDepartmentId(), "所属部门ID不能为空");
		ToolKits.isParamNull(carrierCopyVO.getDepartmentCode(), "所属部门编号不能为空");
		ToolKits.isParamNull(carrierCopyVO.getDepartmentName(), "所属部门名称不能为空");
		ToolKits.isParamNull(carrierCopyVO.getCarriersType(), "公司类型不能为空");

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

		// 判断承运商编码是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "code", carrierCopyVO.getCode(), "enterpriseId", user.getEnterpriseId(), tmsBaseCarriersMapper, "承运商编码已存在");
		// 判断承运商名称是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "name", carrierCopyVO.getName(), "enterpriseId", user.getEnterpriseId(), tmsBaseCarriersMapper, "承运商名称已存在");

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CARRIER_ID, "L");
		ToolKits.isParamNull(id, "承运商ID生成失败");

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

		TmsBaseCarriersModel tmsBaseCarriersModel = new TmsBaseCarriersModel();
		BeanUtils.copyProperties(carrierCopyVO, tmsBaseCarriersModel);
		tmsBaseCarriersModel.setId(Long.valueOf(id));
		tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_CREATE);
		tmsBaseCarriersModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseCarriersModel.setDepartmentId(carrierCopyVO.getDepartmentId());
		tmsBaseCarriersModel.setDepartmentCode(carrierCopyVO.getDepartmentCode());
		tmsBaseCarriersModel.setDepartmentName(carrierCopyVO.getDepartmentName());
		tmsBaseCarriersModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseCarriersModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseCarriersModel.setCreatetime(date);
		tmsBaseCarriersModel.setCreateperson(user.getNickname());
		tmsBaseCarriersModel.setUpdatetime(date);
		tmsBaseCarriersModel.setUpdateperson(user.getNickname());
		tmsBaseCarriersModel.setCarrierSysFlag(carrierCopyVO.getCarrierSysFlag());
		tmsBaseCarriersMapper.save(tmsBaseCarriersModel);
	}

	/**
	 * 承运商修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyCarrierData(CarrierModifyVO carrierModifyVO) {
		ToolKits.isParamNull(carrierModifyVO, "承运商修改请求不为空");
		ToolKits.isParamNull(carrierModifyVO.getId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierModifyVO.getDepartmentId(), "所属部门ID不能为空");
		ToolKits.isParamNull(carrierModifyVO.getDepartmentCode(), "所属部门编号不能为空");
		ToolKits.isParamNull(carrierModifyVO.getDepartmentName(), "所属部门名称不能为空");
		ToolKits.isParamNull(carrierModifyVO.getCarriersType(), "公司类型不能为空");

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

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new RuntimeException("当前用户没有操作权限");
		}

		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierModifyVO.getId(), TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID【" + carrierModifyVO.getId() + "】不存在");

		Assert.isTrue(!(tmsBaseCarriersModel.getStatus() == StatusConstant.STATUS_DISABLE), "承运商已停用不能修改");

		Assert.isTrue(!(tmsBaseCarriersModel.getStatus() == StatusConstant.STATUS_UNDER_REVIEW), "承运商正在审核中不能修改");

		BeanUtils.copyProperties(carrierModifyVO, tmsBaseCarriersModel);
		// TODO 组织和部门默认 后期替换权限
		tmsBaseCarriersModel.setOrganizationId(user.getOrganizationId());
		tmsBaseCarriersModel.setOrganizationCode(String.valueOf(user.getOrganizationId()));
		tmsBaseCarriersModel.setOrganizationName(user.getOrgnaizationName());
		tmsBaseCarriersModel.setDepartmentId(carrierModifyVO.getDepartmentId());
		tmsBaseCarriersModel.setDepartmentCode(carrierModifyVO.getDepartmentCode());
		tmsBaseCarriersModel.setDepartmentName(carrierModifyVO.getDepartmentName());
		tmsBaseCarriersModel.setUpdatetime(new Date());
		tmsBaseCarriersModel.setUpdateperson(user.getNickname());
		tmsBaseCarriersModel.setCarrierSysFlag(carrierModifyVO.getCarrierSysFlag());
		tmsBaseCarriersMapper.update(tmsBaseCarriersModel);

		if (CollectionUtil.isNotEmpty(carrierModifyVO.getCarrierPaperVOList())) {
			Date date = new Date();
			carrierModifyVO.getCarrierPaperVOList().forEach(vo -> {
				ToolKits.isParamNull(vo.getId(), "证件ID不为空");
				TmsBasePaperModel dbTmsBasePaperModel = tmsBasePaperMapper.findById(vo.getId(), TmsBasePaperModel.class);
				if (ObjectUtil.isNotNull(dbTmsBasePaperModel)) {
					dbTmsBasePaperModel.setIssuingAuthority(vo.getIssuingAuthority());
					dbTmsBasePaperModel.setIssuingDate(vo.getIssuingDate());
					dbTmsBasePaperModel.setStartDate(vo.getStartDate());
					dbTmsBasePaperModel.setEndDate(vo.getEndDate());
					dbTmsBasePaperModel.setPicture(vo.getPicture());
					dbTmsBasePaperModel.setMemo(vo.getMemo());
					dbTmsBasePaperModel.setUpdateperson(user.getNickname());
					dbTmsBasePaperModel.setUpdatetime(date);
					tmsBasePaperMapper.update(dbTmsBasePaperModel);
				} else {
					CarrierPaperLinkVO carrierPaperLinkVO = BeanUtil.copyProperties(vo, CarrierPaperLinkVO.class);
					carrierPaperLinkVO.setCarrierId(tmsBaseCarriersModel.getId());
					carrierPaperLinkVO.setCarrierCode(tmsBaseCarriersModel.getCode());
					carrierPaperLinkVO.setCarrierName(tmsBaseCarriersModel.getName());
					addCarrierPaper(carrierPaperLinkVO);
				}
			});

		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyCarrierSysFlagData(CarrierModifySysFlagVO carrierModifyVO) {
		ToolKits.isParamNull(carrierModifyVO, "承运商修改请求不为空");
		ToolKits.isParamNull(carrierModifyVO.getCarrierId(), "承运商ID不为空");

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

		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierModifyVO.getCarrierId(), TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID【" + carrierModifyVO.getCarrierId() + "】不存在");

		if (StatusConstant.STATUS_DISABLE.equals(tmsBaseCarriersModel.getStatus())) {
			throw new PrologException("承运商已停用不能修改");
		}
		if (StatusConstant.STATUS_UNDER_REVIEW.equals(tmsBaseCarriersModel.getStatus())) {
			throw new PrologException("承运商正在审核中不能修改");
		}

		tmsBaseCarriersModel.setUpdatetime(new Date());
		tmsBaseCarriersModel.setUpdateperson(user.getNickname());
		tmsBaseCarriersModel.setCarrierSysFlag(DataDictionaryConstant.CARRIER_SYS_FLAG_INNER);
		tmsBaseCarriersMapper.update(tmsBaseCarriersModel);
	}

	/**
	 * 承运商停用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void disableCarrierData(List<String> ids) {
		ToolKits.isParamNull(ids, "承运商停用请求不为空");

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

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

		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByIds(TmsBaseCarriersModel.class, ids.toArray(), null);
		if (ToolKits.isNotEmpty(tmsBaseCarriersModels)) {
			List<String> tmsBaseCarriersModelList = tmsBaseCarriersModels.stream().filter(o -> StatusConstant.STATUS_UNDER_REVIEW.equals(o.getStatus())).map(TmsBaseCarriersModel::getName).collect(Collectors.toList());
			if (ToolKits.isNotEmpty(tmsBaseCarriersModelList)) {
				throw new RuntimeException("承运商[" + tmsBaseCarriersModelList.stream().collect(Collectors.joining(",")) + "]在审核中不可启用");
			}
		}
		for (TmsBaseCarriersModel tmsBaseCarriersModel : tmsBaseCarriersModels) {
			tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_DISABLE);
			tmsBaseCarriersModel.setUpdatetime(date);
			tmsBaseCarriersModel.setUpdateperson(user.getNickname());
			tmsBaseCarriersMapper.update(tmsBaseCarriersModel);
		}
	}

	/**
	 * 承运商启用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void enableCarrierData(List<String> ids) {
		ToolKits.isParamNull(ids, "承运商启用请求不为空");

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

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

		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByIds(TmsBaseCarriersModel.class, ids.toArray(), null);

		ToolKits.isParamNull(tmsBaseCarriersModels, "承运商ID[" + ids.stream().collect(Collectors.joining(",")) + "]错误,查询为空!");

		List<TmsBaseCarriersModel> models = tmsBaseCarriersModels.stream().filter(o -> DataDictionaryConstant.STATUS_UNDER_REVIEW.equals(o.getStatus()) || DataDictionaryConstant.STATUS_ENABLE.equals(o.getStatus())).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(models)) {
			throw new PrologException("存在审核中或已启用状态承运商");
		}

		for (TmsBaseCarriersModel tmsBaseCarriersModel : tmsBaseCarriersModels) {

			// 封装判断所需数据
			ApprovalOpenVO approvalOpenVO = new ApprovalOpenVO();
			approvalOpenVO.setApprovalFlowType(DataDictionaryConstant.APPROVAL_FLOW_TYPE_CARRIER);
			approvalOpenVO.setApprovalObjectId(String.valueOf(tmsBaseCarriersModel.getId()));
			approvalOpenVO.setApprovalObjectCode(tmsBaseCarriersModel.getCode());
			approvalOpenVO.setApprovalObjectName(tmsBaseCarriersModel.getName());
			approvalOpenVO.setInitiatedUserId(user.getUserid());
			approvalOpenVO.setInitiatedUserName(user.getNickname());

			// 判断是否有承运商审核流
			if (approvalBusinessService.approvalOpen(approvalOpenVO)) {
				// 状态改为审核中
				tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_UNDER_REVIEW);
				tmsBaseCarriersModel.setUpdatetime(date);
				tmsBaseCarriersModel.setUpdateperson(user.getNickname());
				tmsBaseCarriersMapper.update(tmsBaseCarriersModel);

				// 生成临时表数据
				TmsBaseCarriersTempModel tmsBaseCarriersTempModel = new TmsBaseCarriersTempModel();
				BeanUtils.copyProperties(tmsBaseCarriersModel, tmsBaseCarriersTempModel);
				tmsBaseCarriersTempMapper.save(tmsBaseCarriersTempModel);
			} else {
				// 没有审核流直接修改为启用状态
				tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_ENABLE);
				tmsBaseCarriersModel.setUpdatetime(date);
				tmsBaseCarriersModel.setUpdateperson(user.getNickname());
				tmsBaseCarriersMapper.update(tmsBaseCarriersModel);
			}
		}

	}

	/**
	 * 承运商物理删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteCarrierData(List<CarrierDeleteVO> carrierDeleteVOs) {
		ToolKits.isParamNull(carrierDeleteVOs, "承运商删除请求不为空");

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

		List<Long> ids = carrierDeleteVOs.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).distinct().map(CarrierDeleteVO::getId).collect(Collectors.toList());
		ToolKits.isParamNull(ids, "承运商ID不能为空");

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

		// 删除承运商
		tmsBaseCarriersMapper.deleteByIds(ids.toArray(), TmsBaseCarriersModel.class);

		// 删除线路承运方
		Criteria criteria = Criteria.forClass(TmsLineCarrierLinkModel.class);
		Restriction r1 = Restrictions.in("carrierId", ids.toArray());
		criteria.setRestriction(r1);
		tmsCarrierMapper.deleteByCriteria(criteria);

		// 删除承运商证件
		criteria = Criteria.forClass(TmsCarrierPaperLinkModel.class);
		r1 = Restrictions.in("carrierId", ids.toArray());
		criteria.setRestriction(r1);
		List<TmsCarrierPaperLinkModel> tmsCarrierPaperLinkModelList = tmsCarrierPaperLinkMapper.findByCriteria(criteria);
		if (CollectionUtil.isNotEmpty(tmsCarrierPaperLinkModelList)) {
			tmsCarrierPaperLinkMapper.deleteByIds(tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getId).collect(Collectors.toList()).toArray(), TmsCarrierPaperLinkModel.class);
			tmsBasePaperMapper.deleteByIds(tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getPaperId).collect(Collectors.toList()).toArray(), TmsBasePaperModel.class);
		}

	}

	/**
	 * 承运商根据ID分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<CarrierByIdPageDTO> PageCarrierByIdData(CarrierByIdPageVO carrierByIdPageVO) {
		ToolKits.isParamNull(carrierByIdPageVO, "承运商查询请求不为空");
		ToolKits.isParamNull(carrierByIdPageVO.getIds(), "承运商ID集不为空");
		ToolKits.isParamNull(carrierByIdPageVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(carrierByIdPageVO.getPageSize(), "页数不为空");

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

		// 合作性质状态
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.COOPERATIVE_NATURE, null, null, null);

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

		List<String> params = Lists.newArrayList(DataDictionaryConstant.CARRIER_SYS_FLAG // 承运商系统标志
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

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

		Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
		Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r2 = ToolKits.isEmpty(carrierByIdPageVO.getName()) ? null : Restrictions.like("name", "%" + carrierByIdPageVO.getName() + "%");
		Restriction r3 = Restrictions.in("id", carrierByIdPageVO.getIds().toArray());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(carrierByIdPageVO.getPageNum(), carrierByIdPageVO.getPageSize());
		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);

		for (TmsBaseCarriersModel tmsBaseCarriersModel : tmsBaseCarriersModels) {
			CarrierByIdPageDTO carrierByIdPageDTO = new CarrierByIdPageDTO();
			BeanUtils.copyProperties(tmsBaseCarriersModel, carrierByIdPageDTO);
			carrierByIdPageDTO.setCooperativeNatureName(PublicMethods.change(restMessage1, String.valueOf(carrierByIdPageDTO.getCooperativeNature())));
			carrierByIdPageDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(carrierByIdPageDTO.getStatus())));

			carrierByIdPageDTO.setCarrierSysFlagName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SYS_FLAG, String.valueOf(carrierByIdPageDTO.getCarrierSysFlag())));
			carrierByIdPageDTOs.add(carrierByIdPageDTO);
		}

		Page<CarrierByIdPageDTO> page = PageUtils.getPage(carrierByIdPageDTOs);
		if (tmsBaseCarriersModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseCarriersModel> p0 = (com.github.pagehelper.Page<TmsBaseCarriersModel>) tmsBaseCarriersModels;
			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 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(TmsBaseCarriersModel.class);
		Restriction r1 = Restrictions.eq("importBatchNumber", batchno);
		criteria.setRestriction(r1);
		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(tmsBaseCarriersModels)) {
			throw new RuntimeException("批次号[" + batchno + "]错误,查询为空!");
		}
		List<Long> ids = tmsBaseCarriersModels.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).distinct().map(TmsBaseCarriersModel::getId).collect(Collectors.toList());

		for (TmsBaseCarriersModel vo : tmsBaseCarriersModels) {
			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 + "]使用,不能删除!");
			}
		}

		// 删除承运商
		tmsBaseCarriersMapper.deleteByIds(ids.toArray(), TmsBaseCarriersModel.class);

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

		// 删除承运商证件
		criteria = Criteria.forClass(TmsCarrierPaperLinkModel.class);
		r1 = Restrictions.in("carrierId", ids.toArray());
		criteria.setRestriction(r1);
		List<TmsCarrierPaperLinkModel> tmsCarrierPaperLinkModelList = tmsCarrierPaperLinkMapper.findByCriteria(criteria);
		if (CollectionUtil.isNotEmpty(tmsCarrierPaperLinkModelList)) {
			tmsCarrierPaperLinkMapper.deleteByIds(tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getId).collect(Collectors.toList()).toArray(), TmsCarrierPaperLinkModel.class);
			tmsBasePaperMapper.deleteByIds(tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getPaperId).collect(Collectors.toList()).toArray(), TmsBasePaperModel.class);
		}
	}

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

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

		// 合作性质状态
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.COOPERATIVE_NATURE, null, null, null);

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

		List<String> params = Lists.newArrayList(DataDictionaryConstant.CARRIER_SYS_FLAG // 承运商系统标志
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.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" }));
		PageUtils.startPage(importPageVO.getPageNum(), importPageVO.getPageSize());
		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);

		for (TmsBaseCarriersModel tmsBaseCarriersModel : tmsBaseCarriersModels) {
			// 响应对象
			CarrierImportPageDTO carrierImportPageDTO = new CarrierImportPageDTO();
			BeanUtils.copyProperties(tmsBaseCarriersModel, carrierImportPageDTO);
			carrierImportPageDTO.setCooperativeNatureName(PublicMethods.change(restMessage1, String.valueOf(carrierImportPageDTO.getCooperativeNature())));
			carrierImportPageDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(carrierImportPageDTO.getStatus())));
			carrierImportPageDTO.setCarrierSysFlagName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SYS_FLAG, String.valueOf(carrierImportPageDTO.getCarrierSysFlag())));
			carrierImportPageDTOs.add(carrierImportPageDTO);
		}

		Page<CarrierImportPageDTO> page = PageUtils.getPage(carrierImportPageDTOs);
		if (tmsBaseCarriersModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseCarriersModel> p0 = (com.github.pagehelper.Page<TmsBaseCarriersModel>) tmsBaseCarriersModels;
			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);
	}

	/**
	 * 承运商审核信息查询
	 *
	 * @param carrierApprovalInfoVO
	 * @return
	 */
	@Override
	public CarrierApprovalInfoDTO getApprovalInfoData(CarrierApprovalInfoVO carrierApprovalInfoVO) {
		ToolKits.isParamNull(carrierApprovalInfoVO.getCarriersId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierApprovalInfoVO.getApprovalBusinessId(), "审批业务ID不为空");

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

		// 审批历史 结果
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.NODE_APPROVAL_RESULT, null, null, null);

		// 审批节点 状态
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.APPROVAL_BUSINESS_NODE_STATUS, null, null, null);

		// 定义返回参数
		CarrierApprovalInfoDTO carrierApprovalInfoDTO = new CarrierApprovalInfoDTO();

		// 查询临时表数据
		TmsBaseCarriersTempModel tmsBaseCarriersTempModel = tmsBaseCarriersTempMapper.findById(carrierApprovalInfoVO.getCarriersId(), TmsBaseCarriersTempModel.class);

		// 判断是否存在临时表数据
		if (ToolKits.isEmpty(tmsBaseCarriersTempModel)) {
			// 没有临时数据查询正式表
			TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierApprovalInfoVO.getCarriersId(), TmsBaseCarriersModel.class);
			ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID【" + carrierApprovalInfoVO.getCarriersId() + "】不存在");

			BeanUtils.copyProperties(tmsBaseCarriersModel, carrierApprovalInfoDTO);
		} else {
			BeanUtils.copyProperties(tmsBaseCarriersTempModel, carrierApprovalInfoDTO);
		}

		// 承运商审核历史
		List<CarrierApprovalLogDTO> carrierApprovalLogDTOS = Lists.newArrayList();
		// 承运商审核节点
		List<CarrierApprovalNodeDTO> carrierApprovalNodeDTOS = Lists.newArrayList();

		// 查询审核历史
		Criteria criteria = Criteria.forClass(TmsApprovalBusLogModel.class);
		Restriction r1 = Restrictions.eq("approvalBusinessId", carrierApprovalInfoVO.getApprovalBusinessId());
		criteria.setRestriction(r1);
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsApprovalBusLogModel> tmsApprovalBusLogModels = tmsApprovalBusinessLogMapper.findByCriteria(criteria);

		// 封装审核历史
		for (TmsApprovalBusLogModel tmsApprovalBusLogModel : tmsApprovalBusLogModels) {
			CarrierApprovalLogDTO carrierApprovalLogDTO = new CarrierApprovalLogDTO();
			BeanUtils.copyProperties(tmsApprovalBusLogModel, carrierApprovalLogDTO);
			carrierApprovalLogDTO.setResultName(PublicMethods.change(restMessage1, String.valueOf(tmsApprovalBusLogModel.getResult())));
			carrierApprovalLogDTOS.add(carrierApprovalLogDTO);
		}

		// 查询审核节点
		criteria = Criteria.forClass(TmsApprovalBusDetailModel.class);
		r1 = Restrictions.eq("approvalBusinessId", carrierApprovalInfoVO.getApprovalBusinessId());
		criteria.setRestriction(r1);
		criteria.setOrder(Order.newInstance().asc(new String[] { "approvalNodeOrder" }));

		List<TmsApprovalBusDetailModel> tmsApprovalBusDetailModels = tmsApprovalBusinessDetailMapper.findByCriteria(criteria);

		// 封装审核节点
		for (TmsApprovalBusDetailModel tmsApprovalBusDetailModel : tmsApprovalBusDetailModels) {
			CarrierApprovalNodeDTO carrierApprovalNodeDTO = new CarrierApprovalNodeDTO();
			BeanUtils.copyProperties(tmsApprovalBusDetailModel, carrierApprovalNodeDTO);
			carrierApprovalNodeDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(tmsApprovalBusDetailModel.getStatus())));
			carrierApprovalNodeDTOS.add(carrierApprovalNodeDTO);
		}

		criteria = Criteria.forClass(TmsCarrierPaperLinkModel.class);
		r1 = Restrictions.eq(TmsCarrierPaperLinkModel::getCarrierId, carrierApprovalInfoVO.getCarriersId());
		Restriction r2 = Restrictions.eq(TmsCarrierPaperLinkModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsCarrierPaperLinkModel> tmsCarrierPaperLinkModelList = tmsCarrierPaperLinkMapper.findByCriteria(criteria);
		List<PaperDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsCarrierPaperLinkModelList)) {
			List<TmsBasePaperModel> tmsBasePaperModelList = tmsBasePaperMapper.findByIds(TmsBasePaperModel.class, tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getPaperId).toArray(), null);
			// 证件类型
			List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.PAPER_TYPE, null, null, null);
			// 证件状态
			List<Map<String, Object>> restMessage4 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);
			dtoList = tmsBasePaperModelList.stream().map(tmsBasePaperModel -> {
				PaperDTO paperDTO = new PaperDTO();
				BeanUtils.copyProperties(tmsBasePaperModel, paperDTO);
				paperDTO.setTypeName(PublicMethods.change(restMessage3, String.valueOf(paperDTO.getType())));
				paperDTO.setStatusName(PublicMethods.change(restMessage4, String.valueOf(paperDTO.getStatus())));
				return paperDTO;
			}).collect(Collectors.toList());
		}

		carrierApprovalInfoDTO.setCarrierApprovalLogDTOList(carrierApprovalLogDTOS);
		carrierApprovalInfoDTO.setCarrierApprovalNodeDTOList(carrierApprovalNodeDTOS);
		carrierApprovalInfoDTO.setPaperDTOList(dtoList);

		return carrierApprovalInfoDTO;
	}

	/**
	 * 承运商审核作废
	 *
	 * @param carrierApprovalInvalidVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void carrierApprovalInvalidData(CarrierApprovalInvalidVO carrierApprovalInvalidVO) {
		ToolKits.isParamNull(carrierApprovalInvalidVO, "承运商审批撤回作废不为空");
		ToolKits.isParamNull(carrierApprovalInvalidVO.getCarriersId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierApprovalInvalidVO.getApprovalBusinessId(), "审批业务ID不为空");

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

		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierApprovalInvalidVO.getCarriersId(), TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID[" + carrierApprovalInvalidVO.getCarriersId() + "]有误,查询为空!");

		// 审核业务作废
		approvalBusinessService.approvalInvalidData(carrierApprovalInvalidVO.getApprovalBusinessId());

		// 承运商状态回退
		tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_CREATE);
		tmsBaseCarriersMapper.update(tmsBaseCarriersModel);

		// 删除临时表数据
		tmsBaseCarriersTempMapper.deleteById(carrierApprovalInvalidVO.getCarriersId(), TmsBaseCarriersTempModel.class);
	}

	/**
	 * 承运商审核撤回
	 *
	 * @param carrierApprovalRetractVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void carrierApprovalRetractData(CarrierApprovalRetractVO carrierApprovalRetractVO) {
		ToolKits.isParamNull(carrierApprovalRetractVO, "承运商审批撤回请求不为空");
		ToolKits.isParamNull(carrierApprovalRetractVO.getCarriersId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierApprovalRetractVO.getApprovalBusinessId(), "审批业务ID不为空");

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

		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierApprovalRetractVO.getCarriersId(), TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID[" + carrierApprovalRetractVO.getCarriersId() + "]有误,查询为空!");

		// 审核业务撤回
		approvalBusinessService.approvalRetractData(carrierApprovalRetractVO.getApprovalBusinessId());
	}

	/**
	 * 承运商审核提交
	 *
	 * @param carrierApprovalSubmitVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void carrierApprovalSubmitData(CarrierApprovalSubmitVO carrierApprovalSubmitVO) {
		ToolKits.isParamNull(carrierApprovalSubmitVO, "承运商审批提交请求不为空");
		ToolKits.isParamNull(carrierApprovalSubmitVO.getCarriersId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierApprovalSubmitVO.getApprovalBusinessId(), "审批业务ID不为空");
		// TODO 拦截 后期放开
		/*
		 * ToolKits.isParamNull(carrierApprovalSubmitVO.getDepartmentId(),"所属部门ID不能为空");
		 * ToolKits.isParamNull(carrierApprovalSubmitVO.getDepartmentCode(),"所属部门编号不能为空"
		 * );
		 * ToolKits.isParamNull(carrierApprovalSubmitVO.getDepartmentName(),"所属部门名称不能为空"
		 * );
		 */

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

		// 查询临时表数据
		TmsBaseCarriersTempModel tmsBaseCarriersTempModel = tmsBaseCarriersTempMapper.findById(carrierApprovalSubmitVO.getCarriersId(), TmsBaseCarriersTempModel.class);
		ToolKits.isParamNull(tmsBaseCarriersTempModel, "承运商ID[" + carrierApprovalSubmitVO.getCarriersId() + "]有误,为找到临时表数据");

		// 审批业务提交
		approvalBusinessService.approvalSubmitData(carrierApprovalSubmitVO.getApprovalBusinessId());

		// 修改临时表数据
		BeanUtils.copyProperties(carrierApprovalSubmitVO, tmsBaseCarriersTempModel);
		tmsBaseCarriersTempMapper.update(tmsBaseCarriersTempModel);

	}

	/**
	 * 承运商审核
	 *
	 * @param carrierApprovalVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void carrierApprovalData(CarrierApprovalVO carrierApprovalVO) {
		ToolKits.isParamNull(carrierApprovalVO, "承运商审核请求不为空");
		ToolKits.isParamNull(carrierApprovalVO.getCarriersId(), "承运商ID不为空");
		ToolKits.isParamNull(carrierApprovalVO.getApprovalBusinessId(), "审批业务ID不为空");
		ToolKits.isParamNull(carrierApprovalVO.getApprovalObjectName(), "审批对象名称不为空");
		ToolKits.isParamNull(carrierApprovalVO.getApprovalId(), "审批流程ID不为空");
		ToolKits.isParamNull(carrierApprovalVO.getNextApproverNodeId(), "下一审批节点ID不为空");
		ToolKits.isParamNull(carrierApprovalVO.getNextApprovalOrder(), "下一节点顺序不为空");
		ToolKits.isParamNull(carrierApprovalVO.getResult(), "审批结果不为空");
		ToolKits.isParamNull(carrierApprovalVO.getNodeApproverType(), "节点审批者类型不为空");
		ToolKits.isParamNull(carrierApprovalVO.getNodeApprovalType(), "审批方式不为空");

		if (carrierApprovalVO.getResult() != DataDictionaryConstant.NODE_APPROVAL_RESULT_PASS) {
			ToolKits.isParamNull(carrierApprovalVO.getApprovalOpinion(), "驳回意见不为空");
		}

		if (DataDictionaryConstant.NODE_APPROVER_TYPE_ROLE == carrierApprovalVO.getNodeApproverType()) {
			ToolKits.isParamNull(carrierApprovalVO.getNextApproverRoleId(), "下一审批角色ID不为空");
			ToolKits.isParamNull(carrierApprovalVO.getNextApproverRoleName(), "下一审批角色名称不为空");
		} else {
			ToolKits.isParamNull(carrierApprovalVO.getNextApproverUserId(), "下一审批用户ID不为空");
			ToolKits.isParamNull(carrierApprovalVO.getNextApproverUserName(), "下一审批用户名称不为空");
		}

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

		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersMapper.findById(carrierApprovalVO.getCarriersId(), TmsBaseCarriersModel.class);
		ToolKits.isParamNull(tmsBaseCarriersModel, "承运商ID[" + carrierApprovalVO.getCarriersId() + "]有误,查询为空!");

		// 当前时间
		Date date = DateUtils.nowDate();

		// 封装审核所需数据
		ApprovalBusinessNodeVO approvalBusinessNodeVO = new ApprovalBusinessNodeVO();
		approvalBusinessNodeVO.setApprovalObjectName(carrierApprovalVO.getApprovalObjectName());
		approvalBusinessNodeVO.setApprovalBusinessId(carrierApprovalVO.getApprovalBusinessId());
		approvalBusinessNodeVO.setApprovalId(carrierApprovalVO.getApprovalId());
		approvalBusinessNodeVO.setNextApprovalNodeId(carrierApprovalVO.getNextApproverNodeId());
		approvalBusinessNodeVO.setNextApprovalOrder(carrierApprovalVO.getNextApprovalOrder());
		approvalBusinessNodeVO.setNextApproverRoleId(carrierApprovalVO.getNextApproverRoleId());
		approvalBusinessNodeVO.setNextApproverRoleName(carrierApprovalVO.getNextApproverRoleName());
		approvalBusinessNodeVO.setNextApproverUserId(carrierApprovalVO.getNextApproverUserId());
		approvalBusinessNodeVO.setNextApproverUserName(carrierApprovalVO.getNextApproverUserName());
		approvalBusinessNodeVO.setApproverPersonId(user.getUserid());
		approvalBusinessNodeVO.setApproverPersonName(user.getNickname());
		approvalBusinessNodeVO.setApprovalOpinion(ToolKits.isEmpty(carrierApprovalVO.getApprovalOpinion()) ? "" : carrierApprovalVO.getApprovalOpinion());
		approvalBusinessNodeVO.setApprovalTime(date);
		approvalBusinessNodeVO.setResult(carrierApprovalVO.getResult());

		// TODO 当前用户角色和角色ID只有一个 后期权限完善后补全
		approvalBusinessNodeVO.setApproverPersonRoleId(user.getRoles().stream().map(l -> l.toString()).collect(Collectors.joining(",")));
		approvalBusinessNodeVO.setApproverPersonRoleName("");

		approvalBusinessNodeVO.setNodeApproverType(carrierApprovalVO.getNodeApproverType());
		approvalBusinessNodeVO.setNodeApprovalType(carrierApprovalVO.getNodeApprovalType());

		Boolean isFinish = approvalBusinessService.nodeApproval(approvalBusinessNodeVO);

		// 判断是否已结束流程
		if (isFinish) {
			// 查询临时表数据
			TmsBaseCarriersTempModel tmsBaseCarriersTempModel = tmsBaseCarriersTempMapper.findById(carrierApprovalVO.getCarriersId(), TmsBaseCarriersTempModel.class);
			// 判断临时表是否为空
			if (ToolKits.isNotEmpty(tmsBaseCarriersTempModel)) {
				// 修改正式数据
				BeanUtils.copyProperties(tmsBaseCarriersTempModel, tmsBaseCarriersModel);
				tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_ENABLE);
				tmsBaseCarriersModel.setUpdatetime(date);
				tmsBaseCarriersMapper.update(tmsBaseCarriersModel);

				tmsBaseCarriersTempMapper.deleteById(tmsBaseCarriersTempModel.getId(), TmsBaseCarriersTempModel.class);
			} else {
				tmsBaseCarriersModel.setStatus(StatusConstant.STATUS_ENABLE);
				tmsBaseCarriersModel.setUpdatetime(date);
				tmsBaseCarriersMapper.update(tmsBaseCarriersModel);
			}
		}

	}

	@Override
	public void feeSave(FeeSaveInfoVO feeSaveInfoVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();

		String code = feeSaveInfoVO.getCode();
		Criteria criteria = Criteria.forClass(TmsBaseFeeModel.class);
		Restriction r1 = Restrictions.eq("code", code);
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		if (ObjectUtil.isEmpty(feeSaveInfoVO.getList())) {
			tmsBaseFeeMapper.deleteByCriteria(criteria);
		}
		if (ObjectUtil.isNotEmpty(feeSaveInfoVO.getList())) {
			tmsBaseFeeMapper.deleteByCriteria(criteria);
			this.saveFee(feeSaveInfoVO, user);
		}
	}

	private void saveFee(FeeSaveInfoVO feeSaveInfoVO, UserEntity user) {
		List<FeeSaveInfoDetailVO> list = feeSaveInfoVO.getList();
		List<TmsBaseFeeModel> models = Lists.newArrayList();
		for (FeeSaveInfoDetailVO feeSaveInfoDetailVO : list) {
			TmsBaseFeeModel bmsBaseFeeModel = new TmsBaseFeeModel();
			bmsBaseFeeModel.setExpenseCode(feeSaveInfoDetailVO.getExpenseCode());
			bmsBaseFeeModel.setExpenseName(feeSaveInfoDetailVO.getExpenseName());
			bmsBaseFeeModel.setExpenseAccountCode(feeSaveInfoDetailVO.getExpenseAccountCode());
			bmsBaseFeeModel.setExpenseAccountName(feeSaveInfoDetailVO.getExpenseAccountName());
			bmsBaseFeeModel.setCode(feeSaveInfoVO.getCode());
			bmsBaseFeeModel.setCreateperson(user.getNickname());
			bmsBaseFeeModel.setCreatetime(DateUtil.date());
			bmsBaseFeeModel.setUpdateperson(user.getNickname());
			bmsBaseFeeModel.setUpdatetime(DateUtil.date());
			bmsBaseFeeModel.setEnterpriseId(user.getEnterpriseId());
			bmsBaseFeeModel.setEnterpriseName(user.getEnterpriseName());
			bmsBaseFeeModel.setDataType(feeSaveInfoDetailVO.getDataType());

			models.add(bmsBaseFeeModel);
		}
		tmsBaseFeeMapper.saveBatch(models);
	}

	@Override
	public void accountSave(AccountSaveInfoVO accountSaveInfoVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();

		String code = accountSaveInfoVO.getCode();
		Criteria criteria = Criteria.forClass(TmsSettlementTypeModel.class);
		Restriction r1 = Restrictions.eq("code", code);
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		if (ObjectUtil.isEmpty(accountSaveInfoVO.getList())) {
			settlementTypeModelMapper.deleteByCriteria(criteria);
		}
		if (ObjectUtil.isNotEmpty(accountSaveInfoVO.getList())) {
			settlementTypeModelMapper.deleteByCriteria(criteria);
			this.saveSettlementType(accountSaveInfoVO, user);
		}
	}

	@Override
	public List<SettlementTypeDTO> getSettlementByCode(SettlementTypeVO settlementTypeVO) {
		List<SettlementTypeDTO> list = Lists.newArrayList();
		Criteria criteria = Criteria.forClass(TmsSettlementTypeModel.class);
		Restriction r1 = ObjectUtil.isEmpty(settlementTypeVO.getAccountWayCode()) ? null : Restrictions.eq("accountWayCode", settlementTypeVO.getAccountWayCode());
		Restriction r2 = ObjectUtil.isEmpty(settlementTypeVO.getCode()) ? null : Restrictions.eq("code", settlementTypeVO.getCode());
		Restriction r3 = Restrictions.eq("enterpriseId", settlementTypeVO.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsSettlementTypeModel> mapper = settlementTypeModelMapper.findByCriteria(criteria);
		for (TmsSettlementTypeModel tmsSettlementTypeModel : mapper) {
			SettlementTypeDTO dto = new SettlementTypeDTO();
			BeanUtils.copyProperties(tmsSettlementTypeModel, dto);
			list.add(dto);
		}
		return list;
	}

	@SuppressWarnings("resource")
	@Override
	public Page<BaseFeeDTO> findBaseFeePage(ExpensePopDTO dto) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		List<BaseFeeDTO> list = Lists.newArrayList();
		Criteria criteria = Criteria.forClass(TmsBaseFeeModel.class);
		Restriction r1 = ObjectUtil.isEmpty(dto.getExpenseCode()) ? null : Restrictions.like("expenseCode", "%" + dto.getExpenseCode() + "%");
		Restriction r2 = ObjectUtil.isEmpty(dto.getExpenseName()) ? null : Restrictions.like(TmsBaseFeeModel::getExpenseName, "%" + dto.getExpenseName() + "%");
		Restriction r3 = ObjectUtil.isEmpty(dto.getCarrierCode()) ? null : Restrictions.eq("code", dto.getCarrierCode());
		Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsBaseFeeModel> mapper = tmsBaseFeeMapper.findByCriteria(criteria);
		for (TmsBaseFeeModel tmsSettlementTypeModel : mapper) {
			BaseFeeDTO baseFeeDTO = new BaseFeeDTO();
			BeanUtils.copyProperties(tmsSettlementTypeModel, baseFeeDTO);
			if (tmsSettlementTypeModel.getExpenseType() == 1) {
				baseFeeDTO.setExpenseTypeName("用户");
			} else {
				baseFeeDTO.setExpenseTypeName("系统");
			}
			list.add(baseFeeDTO);
		}

		Page<BaseFeeDTO> page = PageUtils.getPage(list);
		if (mapper instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseFeeModel> p0 = (com.github.pagehelper.Page<TmsBaseFeeModel>) mapper;
			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;
	}

	@Override
	public List<BaseFeeDTO> feeCode(SettlementTypeVO settlementTypeVO) {
		List<BaseFeeDTO> list = Lists.newArrayList();
		Criteria criteria = Criteria.forClass(TmsBaseFeeModel.class);
		Restriction r1 = ObjectUtil.isEmpty(settlementTypeVO.getExpenseCode()) ? null : Restrictions.eq("expenseCode", settlementTypeVO.getExpenseCode());
		Restriction r2 = ObjectUtil.isEmpty(settlementTypeVO.getCode()) ? null : Restrictions.eq("code", settlementTypeVO.getCode());
		Restriction r3 = Restrictions.eq("enterpriseId", settlementTypeVO.getEnterpriseId());
		Restriction r4 = ObjectUtil.isEmpty(settlementTypeVO.getDataType()) ? null : Restrictions.eq(TmsBaseFeeModel::getDataType, settlementTypeVO.getDataType());
		criteria.setRestriction(Restrictions.and(r1, r2, r3,r4));
		List<TmsBaseFeeModel> mapper = tmsBaseFeeMapper.findByCriteria(criteria);
		for (TmsBaseFeeModel tmsSettlementTypeModel : mapper) {
			BaseFeeDTO dto = new BaseFeeDTO();
			BeanUtils.copyProperties(tmsSettlementTypeModel, dto);
			list.add(dto);
		}
		return list;
	}

	private void saveSettlementType(AccountSaveInfoVO vo, UserEntity user) {
		List<AccountSaveInfoDetailVO> list = vo.getList();
		List<TmsSettlementTypeModel> models = Lists.newArrayList();
		for (AccountSaveInfoDetailVO accountSaveInfoDetailVO : list) {
			TmsSettlementTypeModel settlementTypeModel = new TmsSettlementTypeModel();
			settlementTypeModel.setAccountWayCode(accountSaveInfoDetailVO.getAccountWayCode());
			settlementTypeModel.setAccountWayName(accountSaveInfoDetailVO.getAccountWayName());
			settlementTypeModel.setAccountWay(accountSaveInfoDetailVO.getAccountType());
			settlementTypeModel.setCode(vo.getCode());
			settlementTypeModel.setCreateperson(user.getNickname());
			settlementTypeModel.setCreatetime(DateUtil.date());
			settlementTypeModel.setEnterpriseId(user.getEnterpriseId());
			settlementTypeModel.setEnterpriseName(user.getEnterpriseName());
			settlementTypeModel.setUpdateperson(user.getNickname());
			settlementTypeModel.setUpdatetime(DateUtil.date());
			models.add(settlementTypeModel);
		}
		settlementTypeModelMapper.saveBatch(models);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchImportCarriersData(MultipartFile file, String importBatchNumber) throws Exception {
		// 人员权限
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 导入文件判断
		FileUtils.multipartFileJudge(file);
		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.CARRIER_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<CarriersImportStdVO> importVOClass = CarriersImportStdVO.class;
			List<CarriersImportStdVO> 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.CARRIER_IMPORT_LOG, logAddVO);
			e.printStackTrace();
			throw new ParameterException(e.getMessage());
		}
	}

	// 承运商异步保存导入失败日志
	@RabbitListener(queues = RabbitMQConstant.CARRIER_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<CarriersImportStdVO> importVOClass = CarriersImportStdVO.class;
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.CARRIER_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<CarriersImportStdVO> 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 (CarriersImportStdVO 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 + "行导入数据,部门名称不能为空");
			}
			i++;
		}
		// 验证导入文件中承运商编号是否重复
		Map<String, Long> codeMap = importVOs.stream().collect(Collectors.groupingBy(CarriersImportStdVO::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(CarriersImportStdVO::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 (CarriersImportStdVO vo : importVOs) {
			if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
				// ToolKits.isParamNull(vo.getOrganizationName(), "所属组织名称不能为空");
				ToolKits.isParamNull(vo.getDepartmentName(), "所属部门名称不能为空");
			}
			if (ToolKits.isNotEmpty(vo.getPhoneNumber()) && !Validator.isMobile(vo.getPhoneNumber())) {
				// throw new ParameterException(String.format("%s手机号不符合规范",
				// vo.getPhoneNumber()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,手机号格式不符合规范!");
			}
			if (ToolKits.isNotEmpty(vo.getEmail()) && !Validator.isEmail(vo.getEmail())) {
				// throw new ParameterException(String.format("%邮箱不符合规范", vo.getEmail()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,邮箱格式不符合规范!");
			}
			if (ToolKits.isNotEmpty(vo.getPostCode()) && !Validator.isZipCode(vo.getPostCode())) {
				// throw new ParameterException(String.format("%邮编不符合规范", vo.getPostCode()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,邮编格式不符合规范!");
			}

			if (ToolKits.isNotEmpty(vo.getLandlineNumber())) {
				Matcher matcher = null;
				Pattern p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
				Pattern p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
				if (vo.getLandlineNumber().length() > 9) {
					matcher = p1.matcher(vo.getLandlineNumber());
					if (!matcher.matches()) {
						throw new RuntimeException("座机号错误!");
					}
				} else {
					matcher = p2.matcher(vo.getLandlineNumber());
					if (!matcher.matches()) {
						throw new RuntimeException("座机号错误!");
					}
				}
			}

			TmsBaseCarriersModel model = new TmsBaseCarriersModel();
			BeanUtils.copyProperties(vo, model);

			List<String> valueList = Lists.newArrayList("1", "2");
			if (ToolKits.isNotEmpty(vo.getCooperativeNature())) {
				if (!valueList.contains(vo.getCooperativeNature())) {
					throw new ParameterException("第" + (i + 1) + "行导入数据,合作性质不符合规范!");
				}
				model.setCooperativeNature(Integer.parseInt(vo.getCooperativeNature()));
			}

			// 判断省市区
			if (ToolKits.isNotEmpty(vo.getProvinceName())) {
				List<LmsTmsZoneModel> lmsTmsZoneModels = lmsTmsZoneMapper.getZoneListByZname(vo.getProvinceName(), "2", null);
				if (ToolKits.isEmpty(lmsTmsZoneModels)) {
					throw new RuntimeException("省名称[" + vo.getProvinceName() + "]错误");
				}
				model.setProvince(lmsTmsZoneModels.get(0).getZoneid());
			}
			if (ToolKits.isNotEmpty(vo.getCityName())) {
				List<LmsTmsZoneModel> lmsTmsZoneModels = lmsTmsZoneMapper.getZoneListByZname(vo.getCityName(), "3", vo.getProvinceName());
				if (ToolKits.isEmpty(lmsTmsZoneModels)) {
					throw new RuntimeException("市名称[" + vo.getCityName() + "]错误");
				}
				model.setCity(lmsTmsZoneModels.get(0).getZoneid());
			}
			if (ToolKits.isNotEmpty(vo.getDistrictName())) {
				List<LmsTmsZoneModel> lmsTmsZoneModels = lmsTmsZoneMapper.getZoneListByZname(vo.getDistrictName(), "4", vo.getCityName());
				if (ToolKits.isEmpty(lmsTmsZoneModels)) {
					throw new RuntimeException("区名称[" + vo.getDistrictName() + "]错误");
				}
				model.setDistrict(lmsTmsZoneModels.get(0).getZoneid());
			}

			// 判断承运商编码是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseCarriersMapper, "承运商编码[" + vo.getCode() + "]已存在");
			PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "code", vo.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseCarriersMapper, "承运商编码[" + vo.getCode() + "]已停用,请直接启用!");
			// 判断承运商名称是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "name", vo.getName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseCarriersMapper, "承运商名称[" + vo.getName() + "]已存在");
			PublicMethods.isThereAnyinformation(TmsBaseCarriersModel.class, "name", vo.getName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseCarriersMapper, "承运商名称[" + vo.getName() + "]已停用,请直接启用!");

			// 承运商ID
			String carrierId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CARRIER_ID, "L");
			ToolKits.isParamNull(carrierId, "承运商ID生成失败");
			// 承运商对象
			model.setId(Long.valueOf(carrierId));
			model.setStatus(StatusConstant.STATUS_ENABLE);
			model.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			model.setImportBatchNumber(importBatchNumber);
			model.setEnterpriseId(user.getEnterpriseId());
			model.setEnterpriseName(user.getEnterpriseName());
			model.setCreateperson(user.getNickname());
			model.setCreatetime(date);
			model.setUpdateperson(user.getNickname());
			model.setUpdatetime(date);

			if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
				// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 start
				String[] departments = vo.getDepartmentName().split(",");
				List<String> deptIds = Lists.newArrayList();
				List<String> deptCodes = Lists.newArrayList();
				for (String department : departments) {
					// 调用获取部门信息API
					RestMessage<List<Organization>> restMessage = userFeign.search(null, department, 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(department + "不存在,请先维护部门!");
						}
						// 部门信息赋值
						deptIds.add(organization.getId());
						deptCodes.add(organization.getCode());
					} else {
						throw new RuntimeException(department + "不存在,请先维护部门!");
					}
				}
				model.setDepartmentId(deptIds.stream().collect(Collectors.joining(",")));
				model.setDepartmentCode(deptCodes.stream().collect(Collectors.joining(",")));
				// STD-DEVELOP20220823 lhb JC-HZ-001货主支持多部门 20220830 end
				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("顶级部门不存在,请先维护部门!");
				}
			}
//			valueList = Lists.newArrayList("0", "1");
//			if (ToolKits.isNotEmpty(vo.getPickUpPicture())) {
//				if (!valueList.contains(vo.getPickUpPicture())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,提货图片(是/否)不符合规范!");
//				}
//				model.setPickUpPicture(Integer.parseInt(vo.getPickUpPicture()));
//			}
//			if (ToolKits.isNotEmpty(vo.getPickUpSign())) {
//				if (!valueList.contains(vo.getPickUpSign())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,提货签到(是/否)不符合规范!");
//				}
//				model.setPickUpSign(Integer.parseInt(vo.getPickUpSign()));
//			}
//			if (ToolKits.isNotEmpty(vo.getAutoPickUpSign())) {
//				if (!valueList.contains(vo.getAutoPickUpSign())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,自动提货签到(是/否)不符合规范!");
//				}
//				model.setAutoPickUpSign(Integer.parseInt(vo.getAutoPickUpSign()));
//			}
//			if (ToolKits.isNotEmpty(vo.getDeliverySign())) {
//				if (!valueList.contains(vo.getDeliverySign())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,送达签到(是/否)不符合规范!");
//				}
//				model.setDeliverySign(Integer.parseInt(vo.getDeliverySign()));
//			}
//			if (ToolKits.isNotEmpty(vo.getDeliveryPicture())) {
//				if (!valueList.contains(vo.getDeliveryPicture())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,送达图片(是/否)不符合规范!");
//				}
//				model.setDeliveryPicture(Integer.parseInt(vo.getDeliveryPicture()));
//			}
//			if (ToolKits.isNotEmpty(vo.getAutoDeliverySign())) {
//				if (!valueList.contains(vo.getAutoDeliverySign())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,自动送达签到(是/否)不符合规范!");
//				}
//				model.setAutoDeliverySign(Integer.parseInt(vo.getAutoDeliverySign()));
//			}
//			if (ToolKits.isNotEmpty(vo.getReturnOrderFlag())) {
//				if (!valueList.contains(vo.getReturnOrderFlag())) {
//					throw new ParameterException("第" + (i + 1) + "行导入数据,是否要求回单(是/否)不符合规范!");
//				}
//				model.setReturnOrderFlag(Integer.parseInt(vo.getReturnOrderFlag()));
//			}
			model.setCarrierSysFlag(DataDictionaryConstant.CARRIER_SYS_FLAG_OUTSIDE);
			tmsBaseCarriersMapper.save(model);
			i++;
		}
		return i;
	}

	/**
	 * 承运商司机分页查询
	 *
	 * @param vo
	 * @return
	 */
	@Override
	public Page<DriverPagingQueryDTO> pageCarrierDriver(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsBaseDriverModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseDriverModel::getAffiliationId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsBaseDriverModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseDriverModel> tmsBaseDriverModelList = tmsBaseDriverMapper.findByCriteria(criteria);
		List<DriverPagingQueryDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsBaseDriverModelList)) {
			// 司机类型
			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);
			dtoList = tmsBaseDriverModelList.stream().map(tmsBaseDriverModel -> {
				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())));
				return driverPagingQueryDTO;
			}).collect(Collectors.toList());
		}
		Page<DriverPagingQueryDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseDriverModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseDriverModel> p0 = (com.github.pagehelper.Page<TmsBaseDriverModel>) tmsBaseDriverModelList;
			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<DeliverymanPagingQueryDTO> pageCarrierDeliveryman(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsBaseDeliverymanModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseDeliverymanModel::getAffiliationId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsBaseDeliverymanModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseDeliverymanModel> tmsBaseDeliverymanModelList = tmsBaseDeliverymanMapper.findByCriteria(criteria);
		List<DeliverymanPagingQueryDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsBaseDeliverymanModelList)) {
			// 送货员类型
			List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.DELIVERYMAN_TYPE, null, null, null);
			// 送货员状态
			List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);
			dtoList = tmsBaseDeliverymanModelList.stream().map(tmsBaseDeliverymanModel -> {
				DeliverymanPagingQueryDTO deliverymanPagingQueryDTO = new DeliverymanPagingQueryDTO();
				BeanUtils.copyProperties(tmsBaseDeliverymanModel, deliverymanPagingQueryDTO);
				deliverymanPagingQueryDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(deliverymanPagingQueryDTO.getType())));
				deliverymanPagingQueryDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(deliverymanPagingQueryDTO.getStatus())));
				return deliverymanPagingQueryDTO;
			}).collect(Collectors.toList());
		}
		Page<DeliverymanPagingQueryDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseDeliverymanModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseDeliverymanModel> p0 = (com.github.pagehelper.Page<TmsBaseDeliverymanModel>) tmsBaseDeliverymanModelList;
			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<VehiclePagingQueryDTO> pageCarrierVehicle(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsBaseVehicleModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseVehicleModel::getAffiliationId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsBaseVehicleModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseVehicleModel> tmsBaseVehicleModelList = tmsBaseVehicleMapper.findByCriteria(criteria);
		List<VehiclePagingQueryDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsBaseVehicleModelList)) {
			// 车辆类型
			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);
			dtoList = tmsBaseVehicleModelList.stream().map(tmsBaseVehicleModel -> {
				VehiclePagingQueryDTO vehiclePagingQueryDTO = new VehiclePagingQueryDTO();
				BeanUtils.copyProperties(tmsBaseVehicleModel, vehiclePagingQueryDTO);
				vehiclePagingQueryDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(vehiclePagingQueryDTO.getType())));
				vehiclePagingQueryDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(vehiclePagingQueryDTO.getStatus())));
				vehiclePagingQueryDTO.setBrandName(PublicMethods.change(restMessage3, String.valueOf(vehiclePagingQueryDTO.getBrand())));
				vehiclePagingQueryDTO.setVehicleTypeCategoryName(PublicMethods.change(restMessage4, String.valueOf(vehiclePagingQueryDTO.getVehicleTypeCategory())));
				vehiclePagingQueryDTO.setMileageManagementName(PublicMethods.change(restMessage6, String.valueOf(vehiclePagingQueryDTO.getMileageManagement())));
				return vehiclePagingQueryDTO;
			}).collect(Collectors.toList());
		}
		Page<VehiclePagingQueryDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseVehicleModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseVehicleModel> p0 = (com.github.pagehelper.Page<TmsBaseVehicleModel>) tmsBaseVehicleModelList;
			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> pageCarrierContainer(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsBaseContainerModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseContainerModel::getAffiliationId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsBaseContainerModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseContainerModel> tmsBaseContainerModelList = tmsBaseContainerMapper.findByCriteria(criteria);
		List<ContainerDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsBaseContainerModelList)) {
			List<Long> baseId = tmsBaseContainerModelList.stream().map(TmsBaseContainerModel::getId).collect(Collectors.toList());
			criteria = Criteria.forClass(TmsDepartmentBaseLinkModel.class);
			r1 = Restrictions.in("baseId", baseId.toArray());
			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);
			dtoList = tmsBaseContainerModelList.stream().map(tmsBaseContainerModel -> {
				ContainerDTO containerDTO = new ContainerDTO();
				BeanUtils.copyProperties(tmsBaseContainerModel, containerDTO);
				containerDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(containerDTO.getType())));
				containerDTO.setStatusName(PublicMethods.change(restMessage3, String.valueOf(containerDTO.getStatus())));
				containerDTO.setBelongToName(PublicMethods.change(restMessage4, String.valueOf(containerDTO.getBelongTo())));
				List<TmsDepartmentBaseLinkModel> linkList = baseMap.get(containerDTO.getId().toString());
				if (CollectionUtil.isNotEmpty(linkList)) {
					containerDTO.setDepartmentId(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentId).collect(Collectors.joining(",")));
					containerDTO.setDepartmentCode(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentCode).collect(Collectors.joining(",")));
					containerDTO.setDepartmentName(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentName).collect(Collectors.joining(",")));
				}
				return containerDTO;
			}).collect(Collectors.toList());
		}
		Page<ContainerDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseContainerModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseContainerModel> p0 = (com.github.pagehelper.Page<TmsBaseContainerModel>) tmsBaseContainerModelList;
			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<DeviceDTO> pageCarrierDevice(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsBaseDeviceModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseDeviceModel::getAffiliationId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsBaseDeviceModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseDeviceModel> tmsBaseDeviceModelList = tmsBaseDeviceMapper.findByCriteria(criteria);
		List<DeviceDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsBaseDeviceModelList)) {
			List<Long> baseId = tmsBaseDeviceModelList.stream().map(TmsBaseDeviceModel::getId).collect(Collectors.toList());
			criteria = Criteria.forClass(TmsDepartmentBaseLinkModel.class);
			r1 = Restrictions.in("baseId", baseId.toArray());
			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);
			dtoList = tmsBaseDeviceModelList.stream().map(tmsBaseDeviceModel -> {
				DeviceDTO deviceDTO = new DeviceDTO();
				BeanUtils.copyProperties(tmsBaseDeviceModel, deviceDTO);
				deviceDTO.setCompanyName(PublicMethods.change(restMessage2, String.valueOf(deviceDTO.getCompany())));
				deviceDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(deviceDTO.getType())));
				deviceDTO.setStatusName(PublicMethods.change(restMessage3, String.valueOf(deviceDTO.getStatus())));
				deviceDTO.setBelongToName(PublicMethods.change(restMessage4, String.valueOf(deviceDTO.getBelongTo())));
				List<TmsDepartmentBaseLinkModel> linkList = baseMap.get(deviceDTO.getId().toString());
				if (CollectionUtil.isNotEmpty(linkList)) {
					deviceDTO.setDepartmentId(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentId).collect(Collectors.joining(",")));
					deviceDTO.setDepartmentCode(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentCode).collect(Collectors.joining(",")));
					deviceDTO.setDepartmentName(linkList.stream().map(TmsDepartmentBaseLinkModel::getDepartmentName).collect(Collectors.joining(",")));
				}
				return deviceDTO;
			}).collect(Collectors.toList());
		}
		Page<DeviceDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseDeviceModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseDeviceModel> p0 = (com.github.pagehelper.Page<TmsBaseDeviceModel>) tmsBaseDeviceModelList;
			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<PaperDTO> pageCarrierPaper(CarrierRelationInfoVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Criteria criteria = Criteria.forClass(TmsCarrierPaperLinkModel.class);
		Restriction r1 = Restrictions.eq(TmsCarrierPaperLinkModel::getCarrierId, vo.getCarrierId());
		Restriction r2 = Restrictions.eq(TmsCarrierPaperLinkModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsCarrierPaperLinkModel> tmsCarrierPaperLinkModelList = tmsCarrierPaperLinkMapper.findByCriteria(criteria);
		List<PaperDTO> dtoList = Lists.newArrayList();
		if (CollectionUtil.isNotEmpty(tmsCarrierPaperLinkModelList)) {
			List<TmsBasePaperModel> tmsBasePaperModelList = tmsBasePaperMapper.findByIds(TmsBasePaperModel.class, tmsCarrierPaperLinkModelList.stream().map(TmsCarrierPaperLinkModel::getPaperId).toArray(), null);
			// 证件类型
			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);
			dtoList = tmsBasePaperModelList.stream().map(tmsBasePaperModel -> {
				PaperDTO paperDTO = new PaperDTO();
				BeanUtils.copyProperties(tmsBasePaperModel, paperDTO);
				paperDTO.setTypeName(PublicMethods.change(restMessage1, String.valueOf(paperDTO.getType())));
				paperDTO.setStatusName(PublicMethods.change(restMessage2, String.valueOf(paperDTO.getStatus())));
				return paperDTO;
			}).collect(Collectors.toList());
		}
		Page<PaperDTO> page = PageUtils.getPage(dtoList);
		if (tmsCarrierPaperLinkModelList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsCarrierPaperLinkModel> p0 = (com.github.pagehelper.Page<TmsCarrierPaperLinkModel>) tmsCarrierPaperLinkModelList;
			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
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addCarrierPaper(CarrierPaperLinkVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		Date date = new Date();
		ToolKits.isParamNull(vo.getId(), "证件ID不为空");
		ToolKits.isParamNull(vo.getCode(), "证件编码不为空");
		ToolKits.isParamNull(vo.getType(), "证件类型不为空");

		// 承运商证件保存
		TmsBasePaperModel tmsBasePaperModel = new TmsBasePaperModel();
		BeanUtils.copyProperties(vo, tmsBasePaperModel);
		tmsBasePaperModel.setPaperObjectType(DataDictionaryConstant.PAPER_OBJECT_CARRIER);
		tmsBasePaperModel.setPaperObjectId(vo.getCarrierId());
		tmsBasePaperModel.setPaperObjectCode(vo.getCarrierCode());
		tmsBasePaperModel.setPaperObjectName(vo.getCarrierName());
		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);

		// 承运商证件关联保存
		TmsCarrierPaperLinkModel tmsCarrierPaperLinkModel = new TmsCarrierPaperLinkModel();
		tmsCarrierPaperLinkModel.setId(ToolKits.getUUID());
		tmsCarrierPaperLinkModel.setCarrierId(vo.getCarrierId());
		tmsCarrierPaperLinkModel.setCarrierCode(vo.getCarrierCode());
		tmsCarrierPaperLinkModel.setCarrierName(vo.getCarrierName());
		tmsCarrierPaperLinkModel.setPaperId(tmsBasePaperModel.getId());
		tmsCarrierPaperLinkModel.setPaperCode(tmsBasePaperModel.getCode());
		tmsCarrierPaperLinkModel.setPaperObjectType(tmsBasePaperModel.getPaperObjectType());
		tmsCarrierPaperLinkModel.setEnterpriseId(user.getEnterpriseId());
		tmsCarrierPaperLinkModel.setEnterpriseName(user.getEnterpriseName());
		tmsCarrierPaperLinkModel.setCreateperson(user.getNickname());
		tmsCarrierPaperLinkModel.setCreatetime(date);
		tmsCarrierPaperLinkModel.setUpdateperson(user.getNickname());
		tmsCarrierPaperLinkModel.setUpdatetime(date);
		tmsCarrierPaperLinkMapper.save(tmsCarrierPaperLinkModel);
	}

	/**
	 * 承运商证件资料修改
	 *
	 * @param vo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCarrierPaper(CarrierPaperLinkVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		TmsBasePaperModel dbTmsBasePaperModel = tmsBasePaperMapper.findById(vo.getId(), TmsBasePaperModel.class);
		dbTmsBasePaperModel.setIssuingAuthority(vo.getIssuingAuthority());
		dbTmsBasePaperModel.setIssuingDate(vo.getIssuingDate());
		dbTmsBasePaperModel.setStartDate(vo.getStartDate());
		dbTmsBasePaperModel.setEndDate(vo.getEndDate());
		dbTmsBasePaperModel.setPicture(vo.getPicture());
		dbTmsBasePaperModel.setMemo(vo.getMemo());
		dbTmsBasePaperModel.setUpdateperson(user.getNickname());
		dbTmsBasePaperModel.setUpdatetime(new Date());
		tmsBasePaperMapper.update(dbTmsBasePaperModel);
	}

	/**
	 * 承运商证件资料是否启用
	 *
	 * @param vo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void whetherEnableCarrierPaper(CarrierPaperWhetherEnableVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		vo.getPaperIdList().forEach(paperId -> {
			TmsBasePaperModel tmsBasePaperModel = tmsBasePaperMapper.findById(paperId, TmsBasePaperModel.class);
			ToolKits.isParamNull(tmsBasePaperModel, "证件ID【" + paperId + "】不存在");
			if (tmsBasePaperModel.getStatus().equals(vo.getStatus())) {
				if (StatusConstant.STATUS_ENABLE.equals(vo.getStatus())) {
					throw new BizException("承运商证件" + tmsBasePaperModel.getCode() + "已启用");
				} else {
					throw new BizException("承运商证件" + tmsBasePaperModel.getCode() + "已停用");
				}
			}
			tmsBasePaperModel.setStatus(vo.getStatus());
			tmsBasePaperModel.setUpdateperson(user.getNickname());
			tmsBasePaperModel.setUpdatetime(new Date());
			tmsBasePaperMapper.update(tmsBasePaperModel);
		});
	}

	/**
	 * 承运商证件资料删除
	 *
	 * @param paperIdList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteCarrierPaper(List<Long> paperIdList) {
		tmsBasePaperMapper.deleteByIds(paperIdList.toArray(), TmsBasePaperModel.class);
		Criteria criteria = Criteria.forClass(TmsCarrierPaperLinkModel.class);
		Restriction r1 = Restrictions.in("paperId", paperIdList.toArray());
		criteria.setRestriction(Restrictions.and(r1));
		tmsCarrierPaperLinkMapper.deleteByCriteria(criteria);
	}

	/**
	 * 根据承运商编码名称查询承运商资料
	 */
	@Override
	public CarriersByEncodingNameDTO getCarriersByEncodingNameData(CarriersByEncodingNameVO carriersByEncodingNameVO) {
		ToolKits.isParamNull(carriersByEncodingNameVO, "请求参数不为空");
		ToolKits.isParamNull(carriersByEncodingNameVO.getCode(), "承运商编码不为空");
		ToolKits.isParamNull(carriersByEncodingNameVO.getName(), "承运商名称不为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseCarriersModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseCarriersModel::getCode, carriersByEncodingNameVO.getCode());
		Restriction r2 = Restrictions.eq(TmsBaseCarriersModel::getName, carriersByEncodingNameVO.getName());
		Restriction r3 = Restrictions.eq(TmsBaseCarriersModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsBaseCarriersModel> tmsBaseCarriersModels = tmsBaseCarriersMapper.findByCriteria(criteria);
		Assert.isTrue(tmsBaseCarriersModels.size() > 0, "不存在编码为【" + carriersByEncodingNameVO.getCode() + "】,名称为【" + carriersByEncodingNameVO.getName() + "】的承运商");
		Assert.isTrue(tmsBaseCarriersModels.size() == 1, "编码为【" + carriersByEncodingNameVO.getCode() + "】,名称为【" + carriersByEncodingNameVO.getName() + "】的承运商资料重复");

		// 承运商资料对象
		TmsBaseCarriersModel tmsBaseCarriersModel = tmsBaseCarriersModels.get(0);
		Assert.isTrue(tmsBaseCarriersModel.getStatus() == StatusConstant.STATUS_ENABLE, "承运商【" + carriersByEncodingNameVO.getName() + "】未启用");

		// 响应对象
		CarriersByEncodingNameDTO carriersByEncodingNameDTO = new CarriersByEncodingNameDTO();
		BeanUtils.copyProperties(tmsBaseCarriersModel, carriersByEncodingNameDTO);

		return carriersByEncodingNameDTO;
	}

}
