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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.prolog.rdc.tms.util.*;
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.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.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.TmsBaseCargoOwnerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarriersMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCustomerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDriverMapper;
import com.prolog.rdc.tms.dao.mapper.TmsCustomerConsigneeMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLineConsigneeLinkMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.SysRefdatadDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StandardTemplateDetailsListDTO;
import com.prolog.rdc.tms.model.dto.basicData.CargoOwnerPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerCarrierDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneeAndShipperPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneeByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneeImportPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneePopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerImportPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.ImportMatchingDataDTO;
import com.prolog.rdc.tms.model.dto.basicData.LineByCustomerDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerPopDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerReceiverDTO;
import com.prolog.rdc.tms.model.dto.basicData.UserCustomerPopupDTO;
import com.prolog.rdc.tms.model.entity.LmsTmsZoneModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCargoOwnerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarriersModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCustomerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDriverModel;
import com.prolog.rdc.tms.model.entity.line.TmsCustomerConsignee;
import com.prolog.rdc.tms.model.entity.line.TmsLineConsigneeLinkModel;
import com.prolog.rdc.tms.model.vo.IntfCustomerConsigneeSaveVO;
import com.prolog.rdc.tms.model.vo.IntfCustomerSaveVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadQueryVO;
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.CargoOwnerPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ConsigneeAddressVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerCarrierVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneeAndShipperPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneeByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneePopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerReceiverSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerReceiverUpdateVO;
import com.prolog.rdc.tms.model.vo.basicData.ImportMatchingDataVO;
import com.prolog.rdc.tms.model.vo.basicData.QueryCustomerAndReceiverVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerReceiverQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerUpdateVO;
import com.prolog.rdc.tms.model.vo.basicData.UserCustomerPopupVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CustomerImportStdVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CustomerReceiverImportStdVO;
import com.prolog.rdc.tms.rmi.feign.UserFeign;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicConfigure.ImportLogService;
import com.prolog.rdc.tms.service.basicConfigure.SysImportTemplethService;
import com.prolog.rdc.tms.service.basicData.CustomerService;
import com.prolog.wms.base.util.ExcelUtils;
import com.prolog.wms.base.util.JsonMapper;

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

import javax.annotation.Resource;

/**
 * 基础数据-客户管理实现层
 *
 * @author :xiaoyf
 * @version :1.0.0
 * @className :TmsBaseCustomerServiceImpl
 * @creationTime :2022/02/25
 * @description :
 */
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private TmsBaseDriverMapper tmsBaseDriverMapper;

	@Autowired
	private TmsBaseCarriersMapper tmsBaseCarriersMapper;

	@Autowired
	private TmsBaseCustomerMapper tmsBaseCustomerMapper;

	@Autowired
	private TmsCustomerConsigneeMapper tmsCustomerConsigneeMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private TmsLineConsigneeLinkMapper tmsArriverMapper;

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@Autowired
	private TmsBaseCargoOwnerMapper tmsBaseCargoOwnerMapper;

	@Autowired
	private LmsTmsZoneMapper lmsTmsZoneMapper;

	@Autowired
	private UserFeign userTmsResource;

	@Autowired
	private AmqpTemplate amqpTemplate;

    @Resource
    private AMapGeocodingUtil aMapGeocodingUtil;

	// 客户分页查询
	@SuppressWarnings("resource")
	@Override
	public Page<TmsBaseCustomerDTO> queryCustomer(TmsBaseCustomerQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		if (vo.getPageNum() == 0) {
			throw new BizException("当前页不能为0");
		}
		if (vo.getPageSize() == 0) {
			throw new BizException("页行数不能为0");
		}

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		vo.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			// 判断是否为部门权限
			vo.setIsDept("Y");
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				vo.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				vo.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
				vo.setIsCustom("Y");
				vo.setIsCargoOwner("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCargoOwnerIds(ownerIds);
			vo.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setIdArrs(ids);
			vo.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseCustomerDTO> list = tmsBaseCustomerMapper.queryCustomer(vo);
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end

		// 翻译字典
		List<String> params = Lists.newArrayList(DataDictionaryConstant.COOPERATIVE_NATURE, DataDictionaryConstant.MAINDATA_STATUS, DataDictionaryConstant.PICK_UP_SIGN_TIME, DataDictionaryConstant.DELIVERY_SIGN_TIME, DataDictionaryConstant.SIGN_TIME_UNIT, DataDictionaryConstant.EARLY_WARNING_STATUS, StatusConstant.WHETHER, DataDictionaryConstant.BANK);
		Map<String, Object> maps = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		List<TmsBaseCustomerDTO> dtos = new ArrayList<TmsBaseCustomerDTO>();
		for (TmsBaseCustomerDTO model : list) {
			TmsBaseCustomerDTO dto = new TmsBaseCustomerDTO();
			BeanUtils.copyProperties(model, dto);

			dto.setCooperativeNatureName(ConvertorUtil.getName(maps, DataDictionaryConstant.COOPERATIVE_NATURE, String.valueOf(dto.getCooperativeNature())));
			dto.setStatusName(ConvertorUtil.getName(maps, DataDictionaryConstant.MAINDATA_STATUS, String.valueOf(dto.getStatus())));
			dto.setPickUpSignTimeName(ConvertorUtil.getName(maps, DataDictionaryConstant.PICK_UP_SIGN_TIME, String.valueOf(dto.getPickUpSignTime())));
			dto.setPickUpSignTimeUnitName(ConvertorUtil.getName(maps, DataDictionaryConstant.SIGN_TIME_UNIT, String.valueOf(dto.getPickUpSignTimeUnit())));
			dto.setDeliverySignTimeName(ConvertorUtil.getName(maps, DataDictionaryConstant.DELIVERY_SIGN_TIME, String.valueOf(dto.getDeliverySignTime())));
			dto.setDeliverySignTimeUnitName(ConvertorUtil.getName(maps, DataDictionaryConstant.SIGN_TIME_UNIT, String.valueOf(dto.getDeliverySignTimeUnit())));
			dto.setPickUpSignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getPickUpSign())));
			dto.setAutoPickUpSignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getAutoPickUpSign())));
			dto.setPickUpPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getPickUpPicture())));
			dto.setDeliverySignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getDeliverySign())));
			dto.setAutoDeliverySignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getAutoDeliverySign())));
			dto.setDeliveryPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getDeliveryPicture())));
			dto.setReturnOrderPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getReturnOrderPicture())));
			dto.setReturnOrderFlagName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getReturnOrderFlag())));
			dto.setBankName(ConvertorUtil.getName(maps, DataDictionaryConstant.BANK, ToolKits.isEmpty(dto.getBank()) ? null : String.valueOf(dto.getBank())));

			dtos.add(dto);
		}

		Page<TmsBaseCustomerDTO> page = PageUtils.getPage(dtos);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseCustomerDTO> p0 = (com.github.pagehelper.Page<TmsBaseCustomerDTO>) list;
			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<TmsBaseCustomerDTO> queryTmsBaseCustomerPopup(TmsBaseCustomerQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		if (vo.getPageNum() == 0) {
			throw new BizException("当前页不能为0");
		}
		if (vo.getPageSize() == 0) {
			throw new BizException("页行数不能为0");
		}

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			// 判断是否为部门权限
			vo.setIsDept("Y");
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				vo.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				vo.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
				vo.setIsCustom("Y");
				vo.setIsCargoOwner("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCargoOwnerIds(ownerIds);
			vo.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setIdArrs(ids);
			vo.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseCustomerDTO> list = tmsBaseCustomerMapper.queryCustomer(vo);

		SysRefdatadQueryVO sysrefdatadqueryvo = new SysRefdatadQueryVO();
		String[] objectcodes = { DataDictionaryConstant.COOPERATIVE_NATURE, // 合作性质
				DataDictionaryConstant.MAINDATA_STATUS, // 启用、停用
				DataDictionaryConstant.PICK_UP_SIGN_TIME, // 提货签到时间点
				DataDictionaryConstant.DELIVERY_SIGN_TIME, // 送达签到时间点
				DataDictionaryConstant.SIGN_TIME_UNIT, // 签到时间单位
				DataDictionaryConstant.EARLY_WARNING_STATUS };
		sysrefdatadqueryvo.setObjectcodes(objectcodes);
		List<SysRefdatadDTO> sysrefdataddtos = new ArrayList<SysRefdatadDTO>();
		try {
			sysrefdataddtos = sysRefdatadService.getSysRefdatadData(sysrefdatadqueryvo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("获取数据字典失败");
		}

		for (TmsBaseCustomerDTO customerDTO : list) {
			for (SysRefdatadDTO sysrefdataddto : sysrefdataddtos) {
				if (DataDictionaryConstant.PICK_UP_SIGN_TIME.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(customerDTO.getPickUpSignTime()))) {
					customerDTO.setPickUpSignTimeName(sysrefdataddto.getName());
				}
				if (DataDictionaryConstant.SIGN_TIME_UNIT.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(customerDTO.getPickUpSignTimeUnit()))) {
					customerDTO.setPickUpSignTimeUnitName(sysrefdataddto.getName());
				}
				if (DataDictionaryConstant.DELIVERY_SIGN_TIME.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(customerDTO.getDeliverySignTime()))) {
					customerDTO.setDeliverySignTimeName(sysrefdataddto.getName());
				}
				if (DataDictionaryConstant.SIGN_TIME_UNIT.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(customerDTO.getDeliverySignTimeUnit()))) {
					customerDTO.setDeliverySignTimeUnitName(sysrefdataddto.getName());
				}
			}
		}
		Page<TmsBaseCustomerDTO> page = PageUtils.getPage(list);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseCustomerDTO> p0 = (com.github.pagehelper.Page<TmsBaseCustomerDTO>) list;
			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;
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
	}

	/**
	 * 客户收发货方弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<CustomerConsigneeAndShipperPopupDTO> popupCustomerConsigneeAndShipperData(CustomerConsigneeAndShipperPopupVO customerConsigneeAndShipperPopupVO) {
		ToolKits.isParamNull(customerConsigneeAndShipperPopupVO, "客户弹窗请求不为空");
		ToolKits.isParamNull(customerConsigneeAndShipperPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(customerConsigneeAndShipperPopupVO.getPageSize(), "页数不为空");
		ToolKits.isParamNull(customerConsigneeAndShipperPopupVO.getCargoOwnerId(), "货主ID不为空");

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

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

		// 查询客户信息
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		customerConsigneeAndShipperPopupVO.setEnterpriseId(user.getEnterpriseId());
		customerConsigneeAndShipperPopupVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			// 判断是否为部门权限
			customerConsigneeAndShipperPopupVO.setIsDept("Y");
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				customerConsigneeAndShipperPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				customerConsigneeAndShipperPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				customerConsigneeAndShipperPopupVO.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				customerConsigneeAndShipperPopupVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
				customerConsigneeAndShipperPopupVO.setIsCustom("Y");
				customerConsigneeAndShipperPopupVO.setIsCargoOwner("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerConsigneeAndShipperPopupVO.setCargoOwnerIds(ownerIds);
			customerConsigneeAndShipperPopupVO.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerConsigneeAndShipperPopupVO.setIdArrs(ids);
			customerConsigneeAndShipperPopupVO.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		List<TmsBaseCustomerDTO> tmsBaseCustomerModels = tmsBaseCustomerMapper.popupCustomerConsigneeAndShipperData(customerConsigneeAndShipperPopupVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
		if (ToolKits.isNotEmpty(tmsBaseCustomerModels)) {
			// 获取客户ID
			Long[] customerIds = (tmsBaseCustomerModels.stream().map(TmsBaseCustomerDTO::getId).distinct().collect(Collectors.toList())).stream().toArray(Long[]::new);

			// 翻译字典
			List<String> params = Lists.newArrayList(DataDictionaryConstant.COOPERATIVE_NATURE);
			Map<String, Object> maps = ConvertorUtil.listDropDownData(sysRefdatadService, params);

			// 查询客户收货方表
			Criteria criteria1 = Criteria.forClass(TmsCustomerConsignee.class);
			Restriction r11 = Restrictions.in("customerId", customerIds);
			Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
			criteria1.setRestriction(Restrictions.and(r11, r12));
			criteria1.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
			PageUtils.startPage(customerConsigneeAndShipperPopupVO.getPageNum(), customerConsigneeAndShipperPopupVO.getPageSize());
			List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria1);
			for (TmsCustomerConsignee tmsCustomerConsignee : tmsCustomerConsignees) {
				// 筛选客户对象
				TmsBaseCustomerDTO tmsBaseCustomerModel = tmsBaseCustomerModels.stream().filter(s -> s.getId().longValue() == tmsCustomerConsignee.getCustomerId().longValue()).collect(Collectors.toList()).get(0);

				// 响应对象
				CustomerConsigneeAndShipperPopupDTO customerConsigneeAndShipperPopupDTO = new CustomerConsigneeAndShipperPopupDTO();
				customerConsigneeAndShipperPopupDTO.setShipperUnitId(tmsBaseCustomerModel.getId());
				customerConsigneeAndShipperPopupDTO.setShipperUnitCode(tmsBaseCustomerModel.getCode());
				customerConsigneeAndShipperPopupDTO.setShipperUnitName(tmsBaseCustomerModel.getName());
				customerConsigneeAndShipperPopupDTO.setShipperId(tmsCustomerConsignee.getId());
				customerConsigneeAndShipperPopupDTO.setShipperCode(tmsCustomerConsignee.getCode());
				customerConsigneeAndShipperPopupDTO.setShipperName(tmsCustomerConsignee.getName());
				customerConsigneeAndShipperPopupDTO.setShipperContact(tmsCustomerConsignee.getContact());
				customerConsigneeAndShipperPopupDTO.setShipperPhoneNumber(tmsCustomerConsignee.getPhoneNumber());
				customerConsigneeAndShipperPopupDTO.setShipperLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
				customerConsigneeAndShipperPopupDTO.setShipperEmail(tmsCustomerConsignee.getEmail());
				customerConsigneeAndShipperPopupDTO.setShipperRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
				customerConsigneeAndShipperPopupDTO.setShipperProvince(tmsCustomerConsignee.getProvince());
				customerConsigneeAndShipperPopupDTO.setShipperProvinceName(tmsCustomerConsignee.getProvinceName());
				customerConsigneeAndShipperPopupDTO.setShipperCity(tmsCustomerConsignee.getCity());
				customerConsigneeAndShipperPopupDTO.setShipperCityName(tmsCustomerConsignee.getCityName());
				customerConsigneeAndShipperPopupDTO.setShipperDistrict(tmsCustomerConsignee.getDistrict());
				customerConsigneeAndShipperPopupDTO.setShipperDistrictName(tmsCustomerConsignee.getDistrictName());
				customerConsigneeAndShipperPopupDTO.setShipperAddress(tmsCustomerConsignee.getAddress());
				customerConsigneeAndShipperPopupDTO.setShipperLongitude(tmsCustomerConsignee.getLongitude());
				customerConsigneeAndShipperPopupDTO.setShipperLatitude(tmsCustomerConsignee.getLatitude());
				customerConsigneeAndShipperPopupDTO.setShipperMemo(tmsCustomerConsignee.getMemo());
				customerConsigneeAndShipperPopupDTO.setShipperPickUpSign(tmsBaseCustomerModel.getPickUpSign());
				customerConsigneeAndShipperPopupDTO.setShipperAutoPickUpSign(tmsBaseCustomerModel.getAutoPickUpSign());
				customerConsigneeAndShipperPopupDTO.setShipperPickUpPicture(tmsBaseCustomerModel.getPickUpPicture());
				customerConsigneeAndShipperPopupDTO.setShipperPickUpRail(tmsBaseCustomerModel.getDefaultPickUpRail());
				customerConsigneeAndShipperPopupDTO.setDeliveryPattern(tmsCustomerConsignee.getDeliveryPattern());

				customerConsigneeAndShipperPopupDTO.setCooperativeNature(tmsBaseCustomerModel.getCooperativeNature());
				customerConsigneeAndShipperPopupDTO.setCooperativeNatureName(ConvertorUtil.getName(maps, DataDictionaryConstant.COOPERATIVE_NATURE, String.valueOf(tmsBaseCustomerModel.getCooperativeNature())));

				customerConsigneeAndShipperPopupDTO.setConsigneeUnitId(tmsBaseCustomerModel.getId());
				customerConsigneeAndShipperPopupDTO.setConsigneeUnitCode(tmsBaseCustomerModel.getCode());
				customerConsigneeAndShipperPopupDTO.setConsigneeUnitName(tmsBaseCustomerModel.getName());
				customerConsigneeAndShipperPopupDTO.setConsigneeId(tmsCustomerConsignee.getId());
				customerConsigneeAndShipperPopupDTO.setConsigneeCode(tmsCustomerConsignee.getCode());
				customerConsigneeAndShipperPopupDTO.setConsigneeName(tmsCustomerConsignee.getName());
				customerConsigneeAndShipperPopupDTO.setConsigneeContact(tmsCustomerConsignee.getContact());
				customerConsigneeAndShipperPopupDTO.setConsigneePhoneNumber(tmsCustomerConsignee.getPhoneNumber());
				customerConsigneeAndShipperPopupDTO.setConsigneeLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
				customerConsigneeAndShipperPopupDTO.setConsigneeEmail(tmsCustomerConsignee.getEmail());
				customerConsigneeAndShipperPopupDTO.setConsigneeRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
				customerConsigneeAndShipperPopupDTO.setConsigneeProvince(tmsCustomerConsignee.getProvince());
				customerConsigneeAndShipperPopupDTO.setConsigneeProvinceName(tmsCustomerConsignee.getProvinceName());
				customerConsigneeAndShipperPopupDTO.setConsigneeCity(tmsCustomerConsignee.getCity());
				customerConsigneeAndShipperPopupDTO.setConsigneeCityName(tmsCustomerConsignee.getCityName());
				customerConsigneeAndShipperPopupDTO.setConsigneeDistrict(tmsCustomerConsignee.getDistrict());
				customerConsigneeAndShipperPopupDTO.setConsigneeDistrictName(tmsCustomerConsignee.getDistrictName());
				customerConsigneeAndShipperPopupDTO.setConsigneeAddress(tmsCustomerConsignee.getAddress());
				customerConsigneeAndShipperPopupDTO.setConsigneeLongitude(tmsCustomerConsignee.getLongitude());
				customerConsigneeAndShipperPopupDTO.setConsigneeLatitude(tmsCustomerConsignee.getLatitude());
				customerConsigneeAndShipperPopupDTO.setConsigneeMemo(tmsCustomerConsignee.getMemo());
				customerConsigneeAndShipperPopupDTO.setConsigneeDeliverySign(tmsBaseCustomerModel.getDeliverySign());
				customerConsigneeAndShipperPopupDTO.setConsigneeAutoDeliverySign(tmsBaseCustomerModel.getAutoDeliverySign());
				customerConsigneeAndShipperPopupDTO.setConsigneeDeliveryPicture(tmsBaseCustomerModel.getDeliveryPicture());
				customerConsigneeAndShipperPopupDTO.setConsigneeDeliveryRail(tmsBaseCustomerModel.getDefaultDeliveryRail());
				customerConsigneeAndShipperPopupDTO.setReturnOrderFlag(tmsBaseCustomerModel.getReturnOrderFlag());
				customerConsigneeAndShipperPopupDTO.setReturnOrderDeadline(tmsBaseCustomerModel.getReturnOrderDeadline());
				customerConsigneeAndShipperPopupDTO.setConsigneeReturnOrderPicture(tmsBaseCustomerModel.getReturnOrderPicture());

				customerConsigneeAndShipperPopupDTO.setPickUpSignTime(tmsBaseCustomerModel.getPickUpSignTime());
				customerConsigneeAndShipperPopupDTO.setPickUpSignTimeDura(tmsBaseCustomerModel.getPickUpSignTimeDura());
				customerConsigneeAndShipperPopupDTO.setPickUpSignTimeUnit(tmsBaseCustomerModel.getPickUpSignTimeUnit());
				customerConsigneeAndShipperPopupDTO.setDeliverySignTime(tmsBaseCustomerModel.getDeliverySignTime());
				customerConsigneeAndShipperPopupDTO.setDeliverySignTimeDura(tmsBaseCustomerModel.getDeliverySignTimeDura());
				customerConsigneeAndShipperPopupDTO.setDeliverySignTimeUnit(tmsBaseCustomerModel.getDeliverySignTimeUnit());

				customerConsigneeAndShipperPopupDTO.setStationId(tmsCustomerConsignee.getStationId());
				customerConsigneeAndShipperPopupDTO.setStationCode(tmsCustomerConsignee.getStationCode());
				customerConsigneeAndShipperPopupDTO.setStationName(tmsCustomerConsignee.getStationName());
				customerConsigneeAndShipperPopupDTO.setStationAreas(tmsCustomerConsignee.getStationAreas());
				customerConsigneeAndShipperPopupDTO.setStationDeliveryOrder(tmsCustomerConsignee.getStationDeliveryOrder());

				customerConsigneeAndShipperPopupDTOs.add(customerConsigneeAndShipperPopupDTO);
			}

			Page<CustomerConsigneeAndShipperPopupDTO> page = PageUtils.getPage(customerConsigneeAndShipperPopupDTOs);
			if (tmsCustomerConsignees instanceof com.github.pagehelper.Page) {
				com.github.pagehelper.Page<TmsCustomerConsignee> p0 = (com.github.pagehelper.Page<TmsCustomerConsignee>) tmsCustomerConsignees;
				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;
		}

		return null;
	}

	/**
	 * 用户客户弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<UserCustomerPopupDTO> popupUserCustomerData(UserCustomerPopupVO userCustomerPopupVO) {
		ToolKits.isParamNull(userCustomerPopupVO, "客户资料弹窗请求不为空");
		ToolKits.isParamNull(userCustomerPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(userCustomerPopupVO.getPageSize(), "页数不为空");

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

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

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		userCustomerPopupVO.setEnterpriseId(user.getEnterpriseId());
		userCustomerPopupVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			userCustomerPopupVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				userCustomerPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				userCustomerPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				userCustomerPopupVO.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				userCustomerPopupVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
				userCustomerPopupVO.setIsCustom("Y");
				userCustomerPopupVO.setIsCargoOwner("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			userCustomerPopupVO.setCargoOwnerIds(ownerIds);
			userCustomerPopupVO.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			userCustomerPopupVO.setIdArrs(ids);
			userCustomerPopupVO.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		PageUtils.startPage(userCustomerPopupVO.getPageNum(), userCustomerPopupVO.getPageSize());
		List<UserCustomerPopupDTO> tmsBaseCustomerModels = tmsBaseCustomerMapper.popupUserCustomerData(userCustomerPopupVO);
		for (UserCustomerPopupDTO tmsBaseCustomerModel : tmsBaseCustomerModels) {
			// 响应对象
			UserCustomerPopupDTO userCustomerPopupDTO = new UserCustomerPopupDTO();
			userCustomerPopupDTO.setId(tmsBaseCustomerModel.getId());
			userCustomerPopupDTO.setCustomerId(tmsBaseCustomerModel.getId());
			userCustomerPopupDTO.setCustomerCode(tmsBaseCustomerModel.getCode());
			userCustomerPopupDTO.setCustomerName(tmsBaseCustomerModel.getName());
			userCustomerPopupDTOs.add(userCustomerPopupDTO);
		}

		Page<UserCustomerPopupDTO> page = PageUtils.getPage(userCustomerPopupDTOs);
		if (tmsBaseCustomerModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<UserCustomerPopupDTO> p0 = (com.github.pagehelper.Page<UserCustomerPopupDTO>) tmsBaseCustomerModels;
			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;
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
	}

	/**
	 * 客户-收货方查询
	 *
	 * @param vo
	 * @return
	 */
	@Override
	public Page<TmsBaseCustomerPopDTO> queryCustomerAndReceiver(QueryCustomerAndReceiverVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		if (vo.getPageNum() == 0) {
			throw new BizException("当前页不能为0");
		}
		if (vo.getPageSize() == 0) {
			throw new BizException("页行数不能为0");
		}

		vo.setUserType(user.getUserType());
		vo.setIsDept(user.getOwnedAllDataAuthFlag());
		vo.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? new String[] {} : user.getDepts().stream().toArray(String[]::new));
			} else {
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? new Long[] {} : user.getConsignors().stream().toArray(Long[]::new));
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? new Long[] {} : user.getCustomers().stream().toArray(Long[]::new));
				vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? new String[] {} : user.getDepts().stream().toArray(String[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			vo.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			vo.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseCustomerPopDTO> list = tmsBaseCustomerMapper.findCustomerAndReceiver(vo);
		for (TmsBaseCustomerPopDTO dto : list) {
			// 查询所有已绑定的收货方
			Criteria criteriaLineConsignee = Criteria.forClass(TmsLineConsigneeLinkModel.class);
			Restriction link = Restrictions.eq("customerId", dto.getId());
			criteriaLineConsignee.setRestriction(link);
			List<TmsLineConsigneeLinkModel> tmsLineConsigneeLinkModels = tmsArriverMapper.findByCriteria(criteriaLineConsignee);

			// 排除查询收货方
			Criteria criteriaReceiver = Criteria.forClass(TmsCustomerConsignee.class);
			Restriction receiver1 = Restrictions.eq("customerId", dto.getId());
			Restriction receiver2 = ToolKits.isEmpty(tmsLineConsigneeLinkModels) ? null : Restrictions.notIn("id", tmsLineConsigneeLinkModels.stream().filter(o -> ToolKits.isNotEmpty(o.getConsigneeId())).map(TmsLineConsigneeLinkModel::getConsigneeId).collect(Collectors.toList()).toArray());
			criteriaReceiver.setRestriction(Restrictions.and(receiver1, receiver2));

			List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteriaReceiver);

			List<TmsBaseCustomerReceiverDTO> dtoList = Lists.newArrayList();

			for (TmsCustomerConsignee tmsCustomerConsignee : tmsCustomerConsignees) {
				TmsBaseCustomerReceiverDTO tmsBaseCustomerReceiverDTO = new TmsBaseCustomerReceiverDTO();
				BeanUtils.copyProperties(tmsCustomerConsignee, tmsBaseCustomerReceiverDTO);
				dtoList.add(tmsBaseCustomerReceiverDTO);
			}
			dto.setTmsBaseCustomerReceiverDTOs(dtoList);
		}
		return PageUtils.getPage(list);
	}

	// 客户查看
	@Override
	public TmsBaseCustomerDTO viewCustomer(Long id) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限!");
		}

		// 翻译字典
		List<String> params = Lists.newArrayList(DataDictionaryConstant.COOPERATIVE_NATURE, DataDictionaryConstant.MAINDATA_STATUS, DataDictionaryConstant.PICK_UP_SIGN_TIME, DataDictionaryConstant.DELIVERY_SIGN_TIME, DataDictionaryConstant.SIGN_TIME_UNIT, DataDictionaryConstant.EARLY_WARNING_STATUS, StatusConstant.WHETHER, DataDictionaryConstant.BANK);
		Map<String, Object> maps = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		TmsBaseCustomerModel model = tmsBaseCustomerMapper.findById(id, TmsBaseCustomerModel.class);
		TmsBaseCustomerDTO dto = new TmsBaseCustomerDTO();
		BeanUtils.copyProperties(model, dto);
		dto.setCooperativeNatureName(ConvertorUtil.getName(maps, DataDictionaryConstant.COOPERATIVE_NATURE, String.valueOf(dto.getCooperativeNature())));
		dto.setStatusName(ConvertorUtil.getName(maps, DataDictionaryConstant.MAINDATA_STATUS, String.valueOf(dto.getStatus())));
		dto.setPickUpSignTimeName(ConvertorUtil.getName(maps, DataDictionaryConstant.PICK_UP_SIGN_TIME, String.valueOf(dto.getPickUpSignTime())));
		dto.setPickUpSignTimeUnitName(ConvertorUtil.getName(maps, DataDictionaryConstant.SIGN_TIME_UNIT, String.valueOf(dto.getPickUpSignTimeUnit())));
		dto.setDeliverySignTimeName(ConvertorUtil.getName(maps, DataDictionaryConstant.DELIVERY_SIGN_TIME, String.valueOf(dto.getDeliverySignTime())));
		dto.setDeliverySignTimeUnitName(ConvertorUtil.getName(maps, DataDictionaryConstant.SIGN_TIME_UNIT, String.valueOf(dto.getDeliverySignTimeUnit())));
		dto.setPickUpSignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getPickUpSign())));
		dto.setAutoPickUpSignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getAutoPickUpSign())));
		dto.setPickUpPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getPickUpPicture())));
		dto.setDeliverySignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getDeliverySign())));
		dto.setAutoDeliverySignName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getAutoDeliverySign())));
		dto.setDeliveryPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getDeliveryPicture())));
		dto.setReturnOrderPictureName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getReturnOrderPicture())));
		dto.setReturnOrderFlagName(ConvertorUtil.getName(maps, StatusConstant.WHETHER, String.valueOf(dto.getReturnOrderFlag())));
		dto.setBankName(ConvertorUtil.getName(maps, DataDictionaryConstant.BANK, ToolKits.isEmpty(dto.getBank()) ? null : String.valueOf(dto.getBank())));

		return dto;
	}

	/**
	 * 客户资料新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveCustomer(TmsBaseCustomerSaveVO vo) throws Exception {
		ToolKits.isParamNull(vo, "客户资料新增请求不为空");
		ToolKits.isParamNull(vo.getCode(), "客户编码不为空");
		ToolKits.isParamNull(vo.getName(), "客户名称不为空");
		ToolKits.isParamNull(vo.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(vo.getCargoOwnerCode(), "货主编号不为空");
		ToolKits.isParamNull(vo.getCargoOwnerName(), "货主名称不为空");
		ToolKits.isParamNull(vo.getCooperativeNature(), "合作性质不为空");

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

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

		Map<String, Object> map = Maps.newHashMap();
		map.put("code", vo.getCode());
		map.put("enterpriseId", user.getEnterpriseId());
		map.put("status", StatusConstant.STATUS_ENABLE);

		Map<String, Object> map2 = Maps.newHashMap();
		map2.put("name", vo.getName());
		map2.put("enterpriseId", user.getEnterpriseId());
		map2.put("status", StatusConstant.STATUS_ENABLE);

		// 判断客户编码 商品名称 是否已存在
		PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map, TmsBaseCustomerModel.class, "客户编码[" + vo.getCode() + "]已存在");
		PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map2, TmsBaseCustomerModel.class, "客户名称[" + vo.getName() + "]已存在");

		// 判断客户编码 商品名称 是否已停用
		map.put("status", StatusConstant.STATUS_DISABLE);
		map2.put("status", StatusConstant.STATUS_DISABLE);
		PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map, TmsBaseCustomerModel.class, "客户编码[" + vo.getCode() + "]已停用,请直接启用");
		PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map2, TmsBaseCustomerModel.class, "客户名称[" + vo.getName() + "]已停用,请直接启用");

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
		if (ToolKits.isEmpty(id)) {
			throw new BizException("客户id获取失败");
		}

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

		TmsBaseCustomerModel model = new TmsBaseCustomerModel();
		BeanUtils.copyProperties(vo, model);
		model.setId(Long.valueOf(id));

		model.setPickUpSign(ToolKits.isEmpty(vo.getPickUpSign()) ? StatusConstant.WHETHER_NO : vo.getPickUpSign());
		model.setAutoPickUpSign(ToolKits.isEmpty(vo.getAutoPickUpSign()) ? StatusConstant.WHETHER_NO : vo.getAutoPickUpSign());
		model.setPickUpPicture(ToolKits.isEmpty(vo.getPickUpPicture()) ? StatusConstant.WHETHER_NO : vo.getPickUpPicture());
		model.setDeliverySign(ToolKits.isEmpty(vo.getDeliverySign()) ? StatusConstant.WHETHER_NO : vo.getDeliverySign());
		model.setAutoDeliverySign(ToolKits.isEmpty(vo.getAutoDeliverySign()) ? StatusConstant.WHETHER_NO : vo.getAutoDeliverySign());
		model.setDeliveryPicture(ToolKits.isEmpty(vo.getDeliveryPicture()) ? StatusConstant.WHETHER_NO : vo.getDeliveryPicture());
		model.setReturnOrderPicture(ToolKits.isEmpty(vo.getReturnOrderPicture()) ? StatusConstant.WHETHER_NO : vo.getReturnOrderPicture());
		model.setReturnOrderFlag(ToolKits.isEmpty(vo.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : vo.getReturnOrderFlag());

		model.setStatus(StatusConstant.STATUS_ENABLE);
		model.setSource(DataDictionaryConstant.SOURCE_ADD);
		model.setCreatetime(date);
		model.setCreateperson(user.getNickname());
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		tmsBaseCustomerMapper.save(model);

		//新增客户默认收货方
		saveDefaultCustomerReceiver(model);
	}

	// 客户复制
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void copyCustomer(TmsBaseCustomerSaveVO vo) throws Exception {
		ToolKits.isParamNull(vo, "客户资料复制请求不为空");
		ToolKits.isParamNull(vo.getCode(), "客户编码不为空");
		ToolKits.isParamNull(vo.getName(), "客户名称不为空");
		ToolKits.isParamNull(vo.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(vo.getCargoOwnerCode(), "货主编号不为空");
		ToolKits.isParamNull(vo.getCargoOwnerName(), "货主名称不为空");
		ToolKits.isParamNull(vo.getCooperativeNature(), "合作性质不为空");

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

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

		// 校验是否已存在编码
		Criteria criteria = Criteria.forClass(TmsBaseCustomerModel.class);
		Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r2 = Restrictions.eq("code", vo.getCode());
		Restriction r3 = Restrictions.eq("cargoOwnerId", vo.getCargoOwnerId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsBaseCustomerModel> list = tmsBaseCustomerMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(list)) {
			if (StatusConstant.STATUS_DISABLE == list.get(0).getStatus()) {
				throw new BizException("编码为【" + vo.getCode() + "】的客户已停用,请直接启用");
			} else {
				throw new BizException("系统已存在编码为【" + vo.getCode() + "】的客户");
			}
		}

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
		if (ToolKits.isEmpty(id)) {
			throw new BizException("客户id获取失败");
		}

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

		TmsBaseCustomerModel model = new TmsBaseCustomerModel();
		BeanUtils.copyProperties(vo, model);
		model.setId(Long.valueOf(id));

		model.setPickUpSign(ToolKits.isEmpty(vo.getPickUpSign()) ? StatusConstant.WHETHER_NO : vo.getPickUpSign());
		model.setAutoPickUpSign(ToolKits.isEmpty(vo.getAutoPickUpSign()) ? StatusConstant.WHETHER_NO : vo.getAutoPickUpSign());
		model.setPickUpPicture(ToolKits.isEmpty(vo.getPickUpPicture()) ? StatusConstant.WHETHER_NO : vo.getPickUpPicture());
		model.setDeliverySign(ToolKits.isEmpty(vo.getDeliverySign()) ? StatusConstant.WHETHER_NO : vo.getDeliverySign());
		model.setAutoDeliverySign(ToolKits.isEmpty(vo.getAutoDeliverySign()) ? StatusConstant.WHETHER_NO : vo.getAutoDeliverySign());
		model.setDeliveryPicture(ToolKits.isEmpty(vo.getDeliveryPicture()) ? StatusConstant.WHETHER_NO : vo.getDeliveryPicture());
		model.setReturnOrderPicture(ToolKits.isEmpty(vo.getReturnOrderPicture()) ? StatusConstant.WHETHER_NO : vo.getReturnOrderPicture());
		model.setReturnOrderFlag(ToolKits.isEmpty(vo.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : vo.getReturnOrderFlag());

		model.setStatus(StatusConstant.STATUS_ENABLE);
		model.setSource(DataDictionaryConstant.SOURCE_ADD);
		model.setCreatetime(date);
		model.setCreateperson(user.getNickname());
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		tmsBaseCustomerMapper.save(model);

		//新增客户默认收货方
		saveDefaultCustomerReceiver(model);
	}

	/**
	 * 客户资料修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCustomer(TmsBaseCustomerUpdateVO vo) throws Exception {
		ToolKits.isParamNull(vo, "客户资料复制请求不为空");
		ToolKits.isParamNull(vo.getId(), "客户ID不为空");
		ToolKits.isParamNull(vo.getCode(), "客户编码不为空");
		ToolKits.isParamNull(vo.getName(), "客户名称不为空");
		ToolKits.isParamNull(vo.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(vo.getCargoOwnerCode(), "货主编号不为空");
		ToolKits.isParamNull(vo.getCargoOwnerName(), "货主名称不为空");
		ToolKits.isParamNull(vo.getCooperativeNature(), "合作性质不为空");

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

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

		TmsBaseCustomerModel model = tmsBaseCustomerMapper.findById(vo.getId(), TmsBaseCustomerModel.class);
		if (ToolKits.isNotEmpty(model)) {
			// 如果对客户编码进行了修改操作,则需要判断修改后的客户编码在系统中是否已存在(当前用户企业ID下的客户中)
			if (!Objects.equals(model.getCode(), vo.getCode())) {
				// 校验是否已存在编码
				Criteria criteria = Criteria.forClass(TmsBaseCustomerModel.class);
				Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				Restriction r2 = Restrictions.eq("code", vo.getCode());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsBaseCustomerModel> list = tmsBaseCustomerMapper.findByCriteria(criteria);
				if (ToolKits.isNotEmpty(list)) {
					throw new BizException("系统已存在编码为【" + vo.getCode() + "】的客户");
				}
			}

			BeanUtils.copyProperties(vo, model);
			model.setUpdatetime(new Date());
			model.setUpdateperson(user.getNickname());
			tmsBaseCustomerMapper.update(model);

			//修改收货方
			Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
			Restriction r1 = Restrictions.eq("customerId", model.getId());
			criteria.setRestriction(Restrictions.and(r1));

			Map<String, Object> map = Maps.newHashMap();
			map.put("updatetime", DateUtil.date());
			map.put("updateperson", user.getNickname());
			map.put("name",model.getName());
			map.put("shortName",model.getShortName());
			map.put("carrierType",DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER);
			map.put("carrierId",null);
			map.put("carrierCode",null);
			map.put("carrierName",null);
			map.put("contact",model.getContact());
			map.put("phoneNumber",model.getPhoneNumber());
			map.put("relationPhoneNumber",null);
			map.put("province",model.getProvince());
			map.put("provinceName",model.getProvinceName());
			map.put("city",model.getCity());
			map.put("cityName",model.getCityName());
			map.put("district",model.getDistrict());
			map.put("districtName",model.getDistrictName());
			map.put("address",model.getAddress());
			map.put("email",model.getEmail());
			map.put("longitude",model.getLongitude());
			map.put("latitude",model.getLatitude());
			map.put("memo",null);
			map.put("deliveryPattern",null);
			map.put("stationId",null);
			map.put("stationCode",null);
			map.put("stationName",null);
			map.put("stationDeliveryOrder",null);
			map.put("stationAreas",null);
			map.put("status",StatusConstant.STATUS_ENABLE);
			tmsCustomerConsigneeMapper.updateMapByCriteria(map, criteria);
		}
	}

	// 客户删除(停用)
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteCustomer(List<String> ids) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		List<TmsBaseCustomerModel> list = tmsBaseCustomerMapper.findByIds(TmsBaseCustomerModel.class, ids.toArray(), null);
		for (TmsBaseCustomerModel model : list) {
			model.setStatus(StatusConstant.STATUS_DISABLE);
			model.setUpdatetime(new Date());
			model.setUpdateperson(user.getNickname());
			tmsBaseCustomerMapper.update(model);
		}

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.in("customerId", ids.stream().map(s -> Long.parseLong(s)).collect(Collectors.toList()));
		criteria.setRestriction(Restrictions.and(r1));

		Map<String, Object> map = Maps.newHashMap();
		map.put("status", StatusConstant.STATUS_DISABLE);
		map.put("updatetime", DateUtil.date());
		map.put("updateperson", user.getNickname());
		tmsCustomerConsigneeMapper.updateMapByCriteria(map, criteria);
	}

	// 客户启用
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void activeCustomer(List<String> ids) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		List<TmsBaseCustomerModel> list = tmsBaseCustomerMapper.findByIds(TmsBaseCustomerModel.class, ids.toArray(), null);
		for (TmsBaseCustomerModel model : list) {
			model.setStatus(StatusConstant.STATUS_ENABLE);
			model.setUpdatetime(new Date());
			model.setUpdateperson(user.getNickname());
			tmsBaseCustomerMapper.update(model);
		}
	}

	/**
	 * 客户物理删除
	 *
	 * @param list
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void neverDelete(List<TmsBaseCustomerDeleteVO> list) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限！");
		ToolKits.isParamNull(list, "参数不能为空");

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

		// 校验是否在正常的线路中
		for (TmsBaseCustomerDeleteVO vo : list) {
			List<LineByCustomerDTO> dtoList = tmsArriverMapper.findLineByCustomer(vo.getId(), null);
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCustomerDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("客户[" + dtoList.get(0).getCustomerName() + "]正在被线路[" + str + "]使用,不能删除!");
			}
		}
		// 删除客户表
		tmsBaseCustomerMapper.deleteByIds(ids.toArray(), TmsBaseCustomerModel.class);

		// 删除对应的收货方
		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.in("customerId", ids.toArray());
		criteria.setRestriction(r1);
		tmsCustomerConsigneeMapper.deleteByCriteria(criteria);

		// 删除线路-收货方
		criteria = Criteria.forClass(TmsLineConsigneeLinkModel.class);
		r1 = Restrictions.in("customerId", ids.toArray());
		criteria.setRestriction(r1);
		tmsArriverMapper.deleteByCriteria(criteria);
	}

	/**
	 * 收货方物理删除
	 *
	 * @param list
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void neverDeleteCustomerReceiver(List<TmsBaseCustomerDeleteVO> list) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限！");
		ToolKits.isParamNull(list, "参数不能为空");

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

		// 校验是否在正常的线路中
		for (TmsBaseCustomerDeleteVO vo : list) {
			List<LineByCustomerDTO> dtoList = tmsArriverMapper.findLineByCustomer(null, vo.getId());
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCustomerDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("收货方[" + dtoList.get(0).getArriverName() + "]正在被线路[" + str + "]使用,不能删除!");
			}
		}

		// 删除收货方
		tmsCustomerConsigneeMapper.deleteByIds(ids.toArray(), TmsCustomerConsignee.class);

		// 删除线路-收货方
		Criteria criteria = Criteria.forClass(TmsLineConsigneeLinkModel.class);
		Restriction r1 = Restrictions.in("customerId", ids.toArray());
		criteria.setRestriction(r1);
		tmsArriverMapper.deleteByCriteria(criteria);
	}

	// 客户收货方查询
	@Override
	public List<TmsBaseCustomerReceiverDTO> queryCustomerReceiver(Long customerId) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		ToolKits.isParamNull(customerId, "客户ID不能为空");

		SysRefdatadQueryVO sysrefdatadqueryvo = new SysRefdatadQueryVO();
		String[] objectcodes = { DataDictionaryConstant.MAINDATA_STATUS, DataDictionaryConstant.LINE_CARRIER_TYPE, DataDictionaryConstant.DELIVERY_PATTERN// 启用、停用
		};
		sysrefdatadqueryvo.setObjectcodes(objectcodes);
		List<SysRefdatadDTO> sysrefdataddtos = new ArrayList<SysRefdatadDTO>();
		try {
			sysrefdataddtos = sysRefdatadService.getSysRefdatadData(sysrefdatadqueryvo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException("获取数据字典失败");
		}

		TmsBaseCustomerReceiverQueryVO vo = new TmsBaseCustomerReceiverQueryVO();
		vo.setCustomerId(customerId);
		List<TmsBaseCustomerReceiverDTO> list = tmsCustomerConsigneeMapper.queryTmsBaseCustomerReceiver(vo, null);
		for (TmsBaseCustomerReceiverDTO dto : list) {
			for (SysRefdatadDTO sysrefdataddto : sysrefdataddtos) {
				if (DataDictionaryConstant.MAINDATA_STATUS.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(dto.getStatus()))) {
					dto.setStatusName(sysrefdataddto.getName());
				}
				if (DataDictionaryConstant.LINE_CARRIER_TYPE.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(dto.getCarrierType()))) {
					dto.setCarrierTypeName(sysrefdataddto.getName());
				}
				if (DataDictionaryConstant.DELIVERY_PATTERN.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(dto.getDeliveryPattern()))) {
					dto.setDeliveryPatternName(sysrefdataddto.getName());
				}
			}
		}
		return list;
	}

	// 客户收货方查看
	@Override
	public TmsBaseCustomerReceiverDTO viewCustomerReceiver(Long id) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}

		SysRefdatadQueryVO sysrefdatadqueryvo = new SysRefdatadQueryVO();
		String[] objectcodes = { DataDictionaryConstant.MAINDATA_STATUS,// 启用、停用
		};
		sysrefdatadqueryvo.setObjectcodes(objectcodes);
		List<SysRefdatadDTO> sysrefdataddtos = new ArrayList<SysRefdatadDTO>();
		try {
			sysrefdataddtos = sysRefdatadService.getSysRefdatadData(sysrefdatadqueryvo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException("获取数据字典失败");
		}

		TmsBaseCustomerReceiverQueryVO vo = new TmsBaseCustomerReceiverQueryVO();
		vo.setId(id);
		List<TmsBaseCustomerReceiverDTO> list = tmsCustomerConsigneeMapper.queryTmsBaseCustomerReceiver(vo, null);
		for (TmsBaseCustomerReceiverDTO dto : list) {
			for (SysRefdatadDTO sysrefdataddto : sysrefdataddtos) {
				if (DataDictionaryConstant.MAINDATA_STATUS.equals(sysrefdataddto.getObjectcode()) && sysrefdataddto.getCode().equals(String.valueOf(dto.getStatus()))) {
					dto.setStatusName(sysrefdataddto.getName());
				}
			}
		}
		return list.get(0);
	}

	/**
	 * 客户收货方新增
	 */
	@Override
	public void saveCustomerReceiver(CustomerReceiverSaveVO customerReceiverSaveVO) {
		ToolKits.isParamNull(customerReceiverSaveVO, "客户收货方新增请求不为空");
		ToolKits.isParamNull(customerReceiverSaveVO.getCode(), "客户收货方编码不为空");
		ToolKits.isParamNull(customerReceiverSaveVO.getName(), "客户收货方名称不为空");
		ToolKits.isParamNull(customerReceiverSaveVO.getCustomerId(), "客户ID不为空");
		ToolKits.isParamNull(customerReceiverSaveVO.getCustomerCode(), "客户编码不为空");
		ToolKits.isParamNull(customerReceiverSaveVO.getCustomerName(), "客户名称不为空");

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

		// 校验是否已存在编码
		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r2 = Restrictions.eq("code", customerReceiverSaveVO.getCode());
		Restriction r3 = Restrictions.eq("customerId", customerReceiverSaveVO.getCustomerId());// 同一客户下不同收货方编码不可重复,不同客户下收货方编码可重复
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsCustomerConsignee> list = tmsCustomerConsigneeMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(list)) {
			throw new BizException("当前客户下已存在编码为【" + customerReceiverSaveVO.getCode() + "】的收货方");
		}

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
		if (ToolKits.isEmpty(id)) {
			throw new BizException("收货方id获取失败");
		}

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

		TmsCustomerConsignee model = new TmsCustomerConsignee();
		BeanUtils.copyProperties(customerReceiverSaveVO, model);
		model.setId(Long.valueOf(id));
		model.setCreatetime(new Date());
		model.setCreateperson(user.getNickname());
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		model.setStatus(StatusConstant.STATUS_ENABLE);
		model.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsCustomerConsigneeMapper.save(model);
	}

	/**
	 * 客户收货方修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCustomerReceiver(CustomerReceiverUpdateVO customerReceiverUpdateVO) {
		ToolKits.isParamNull(customerReceiverUpdateVO, "客户收货方修改请求不为空");
		ToolKits.isParamNull(customerReceiverUpdateVO.getId(), "收货方ID不为空");
		ToolKits.isParamNull(customerReceiverUpdateVO.getCustomerId(), "客户ID不为空");
		ToolKits.isParamNull(customerReceiverUpdateVO.getCustomerCode(), "客户编码不为空");
		ToolKits.isParamNull(customerReceiverUpdateVO.getCustomerName(), "客户名称不为空");

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

		// 根据ID查询客户收货方
		TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsigneeMapper.findById(customerReceiverUpdateVO.getId(), TmsCustomerConsignee.class);
		ToolKits.isParamNull(tmsCustomerConsignee, "客户收货方资料不存在");

		BeanUtils.copyProperties(customerReceiverUpdateVO, tmsCustomerConsignee);
		tmsCustomerConsignee.setUpdatetime(new Date());
		tmsCustomerConsignee.setUpdateperson(user.getNickname());
		tmsCustomerConsigneeMapper.update(tmsCustomerConsignee);
	}

	// 收货方删除(停用)
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteCustomerReceiver(List<String> ids) throws Exception {
		ToolKits.isParamNull(ids, "请求不为空");

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

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.in("id", ids.stream().map(s -> Long.parseLong(s)).collect(Collectors.toList()));
		criteria.setRestriction(Restrictions.and(r1));

		Map<String, Object> map = Maps.newHashMap();
		map.put("status", StatusConstant.STATUS_DISABLE);
		map.put("updatetime", DateUtil.date());
		map.put("updateperson", user.getNickname());
		tmsCustomerConsigneeMapper.updateMapByCriteria(map, criteria);
	}

	// 收货方启用
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void activeCustomerReceiver(List<String> ids) throws Exception {
		ToolKits.isParamNull(ids, "请求不为空");

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

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.in("id", ids.stream().map(s -> Long.parseLong(s)).collect(Collectors.toList()));
		criteria.setRestriction(Restrictions.and(r1));

		Map<String, Object> map = Maps.newHashMap();
		map.put("status", StatusConstant.STATUS_ENABLE);
		map.put("updatetime", DateUtil.date());
		map.put("updateperson", user.getNickname());
		tmsCustomerConsigneeMapper.updateMapByCriteria(map, criteria);
	}

	// 客户地址解析专用查询
	@SuppressWarnings("resource")
	@Override
	public Page<TmsBaseCustomerDTO> queryCustomerForAddress(TmsBaseCustomerQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		if (vo.getPageNum() == 0) {
			throw new BizException("当前页不能为0");
		}
		if (vo.getPageSize() == 0) {
			throw new BizException("页行数不能为0");
		}
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		vo.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			vo.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				vo.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				vo.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				vo.setIsCustom("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCargoOwnerIds(ownerIds);
			vo.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setIdArrs(ids);
			vo.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseCustomerDTO> list = tmsBaseCustomerMapper.queryCustomerForAddress(vo);
		List<TmsBaseCustomerDTO> dtos = new ArrayList<TmsBaseCustomerDTO>();
		for (TmsBaseCustomerDTO model : list) {
			TmsBaseCustomerDTO dto = new TmsBaseCustomerDTO();
			BeanUtils.copyProperties(model, dto);
			dtos.add(dto);
		}

		Page<TmsBaseCustomerDTO> page = PageUtils.getPage(dtos);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseCustomerDTO> p0 = (com.github.pagehelper.Page<TmsBaseCustomerDTO>) list;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
		return page;
	}

	// 收货方地址解析专用查询
	@Override
	public Page<TmsBaseCustomerReceiverDTO> queryCustomerReceiverForAddress(TmsBaseCustomerReceiverQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		if (ToolKits.isEmpty(user)) {
			throw new BizException("当前人员没有操作权限！");
		}
		if (vo.getPageNum() == 0) {
			throw new BizException("当前页不能为0");
		}
		if (vo.getPageSize() == 0) {
			throw new BizException("页行数不能为0");
		}
		ToolKits.isParamNull(vo.getCustomerId(), "客户ID不能为空");
		if (vo.getIds().size() == 0) {
			throw new BizException("请勾选收货方!");
		}
		vo.setCustomerId(vo.getCustomerId());
		String[] param = null;
		if (vo.getIds().size() > 0) {
			param = vo.getIds().toArray(new String[vo.getIds().size()]);
		}
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsBaseCustomerReceiverDTO> list = tmsCustomerConsigneeMapper.queryTmsBaseCustomerReceiver(vo, param);
		return PageUtils.getPage(list);
	}

	/**
	 * 客户导入撤销
	 *
	 * @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(TmsBaseCustomerModel.class);
		Restriction r1 = Restrictions.eq("importBatchNumber", batchno);
		criteria.setRestriction(r1);
		List<TmsBaseCustomerModel> tmsBaseCustomerModels = tmsBaseCustomerMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(tmsBaseCustomerModels)) {
			throw new RuntimeException("批次号[" + batchno + "]错误,查询为空!");
		}
		List<Long> ids = tmsBaseCustomerModels.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).distinct().map(TmsBaseCustomerModel::getId).collect(Collectors.toList());

		// 校验是否在正常的线路中
		for (TmsBaseCustomerModel vo : tmsBaseCustomerModels) {
			List<LineByCustomerDTO> dtoList = tmsArriverMapper.findLineByCustomer(vo.getId(), null);
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCustomerDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("客户[" + dtoList.get(0).getCustomerName() + "]正在被线路[" + str + "]使用,不能删除!");
			}
		}
		// 删除客户表
		tmsBaseCustomerMapper.deleteByIds(ids.toArray(), TmsBaseCustomerModel.class);

		// 删除对应的收货方
		criteria = Criteria.forClass(TmsCustomerConsignee.class);
		r1 = Restrictions.in("customerId", ids.toArray());
		criteria.setRestriction(r1);
		tmsCustomerConsigneeMapper.deleteByCriteria(criteria);

		// 删除线路-收货方
		criteria = Criteria.forClass(TmsLineConsigneeLinkModel.class);
		r1 = Restrictions.in("customerId", ids.toArray());
		criteria.setRestriction(r1);
		tmsArriverMapper.deleteByCriteria(criteria);
	}

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

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

		List<Long> ids = tmsBaseCustomerReceiverModels.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).distinct().map(TmsCustomerConsignee::getId).collect(Collectors.toList());

		// 校验是否在正常的线路中
		for (TmsCustomerConsignee vo : tmsBaseCustomerReceiverModels) {
			List<LineByCustomerDTO> dtoList = tmsArriverMapper.findLineByCustomer(null, vo.getId());
			if (ToolKits.isNotEmpty(dtoList)) {
				String str = dtoList.stream().map(LineByCustomerDTO::getLineName).distinct().collect(Collectors.joining(","));
				throw new RuntimeException("收货方[" + dtoList.get(0).getArriverName() + "]正在被线路[" + str + "]使用,不能删除!");
			}
		}

		// 删除收货方
		tmsCustomerConsigneeMapper.deleteByIds(ids.toArray(), TmsCustomerConsignee.class);

		// 删除线路-收货方
		criteria = Criteria.forClass(TmsLineConsigneeLinkModel.class);
		r1 = Restrictions.in(TmsLineConsigneeLinkModel::getConsigneeId, ids.toArray());
		criteria.setRestriction(r1);
		tmsArriverMapper.deleteByCriteria(criteria);
	}

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

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

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		importPageVO.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			importPageVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				importPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				importPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				importPageVO.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				importPageVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
				importPageVO.setIsCustom("Y");
				importPageVO.setIsCargoOwner("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			importPageVO.setCargoOwnerIds(ownerIds);
			importPageVO.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			importPageVO.setIdArrs(ids);
			importPageVO.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end

		// 翻译字典
		List<String> params = Lists.newArrayList(DataDictionaryConstant.COOPERATIVE_NATURE, DataDictionaryConstant.MAINDATA_STATUS);
		Map<String, Object> maps = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		PageUtils.startPage(importPageVO.getPageNum(), importPageVO.getPageSize());
		List<CustomerImportPageDTO> tmsBaseCustomerModels = tmsBaseCustomerMapper.pageCustomerByImportBatchNumberData(importPageVO);
		for (CustomerImportPageDTO tmsBaseCustomerModel : tmsBaseCustomerModels) {
			// 响应对象
			CustomerImportPageDTO dto = new CustomerImportPageDTO();
			BeanUtils.copyProperties(tmsBaseCustomerModel, dto);
			dto.setCooperativeNatureName(ConvertorUtil.getName(maps, DataDictionaryConstant.COOPERATIVE_NATURE, String.valueOf(dto.getCooperativeNature())));
			dto.setStatusName(ConvertorUtil.getName(maps, DataDictionaryConstant.MAINDATA_STATUS, String.valueOf(dto.getStatus())));
			customerImportPageDTOs.add(dto);
		}

		Page<CustomerImportPageDTO> page = PageUtils.getPage(customerImportPageDTOs);
		if (tmsBaseCustomerModels instanceof com.github.pagehelper.Page) {
			// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
			com.github.pagehelper.Page<CustomerImportPageDTO> p0 = (com.github.pagehelper.Page<CustomerImportPageDTO>) tmsBaseCustomerModels;
			// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 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 RestMessage.newInstance(true, "查询成功", page);
	}

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

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

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.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<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria);
		for (TmsCustomerConsignee tmsCustomerConsignee : tmsCustomerConsignees) {
			// 响应对象
			CustomerConsigneeImportPageDTO customerConsigneeImportPageDTO = new CustomerConsigneeImportPageDTO();
			BeanUtils.copyProperties(tmsCustomerConsignee, customerConsigneeImportPageDTO);
			customerConsigneeImportPageDTOs.add(customerConsigneeImportPageDTO);
		}

		Page<CustomerConsigneeImportPageDTO> page = PageUtils.getPage(customerConsigneeImportPageDTOs);
		if (tmsCustomerConsignees instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsCustomerConsignee> p0 = (com.github.pagehelper.Page<TmsCustomerConsignee>) tmsCustomerConsignees;
			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);
	}

	/**
	 * 收货方地址解析
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void parseCustomerConsigneeAddress(Long id, BigDecimal longitude, BigDecimal latitude, String address) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		TmsCustomerConsignee tmsBaseCustomerReceiver = tmsCustomerConsigneeMapper.findById(id, TmsCustomerConsignee.class);

		ToolKits.isParamNull(tmsBaseCustomerReceiver, "客户收货方不存在");

		tmsBaseCustomerReceiver.setLongitude(longitude);
		tmsBaseCustomerReceiver.setLatitude(latitude);
		tmsBaseCustomerReceiver.setAddress(ToolKits.isEmpty(address) ? null : address);
		tmsBaseCustomerReceiver.setUpdateperson(user.getNickname());
		tmsBaseCustomerReceiver.setUpdatetime(DateUtil.date());
		tmsCustomerConsigneeMapper.update(tmsBaseCustomerReceiver);
	}

	/**
	 * 客户收货方弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<CustomerConsigneePopupDTO> popupCustomerConsigneeData(CustomerConsigneePopupVO customerConsigneePopupVO) {
		ToolKits.isParamNull(customerConsigneePopupVO, "客户收货方弹窗请求不为空");
		ToolKits.isParamNull(customerConsigneePopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(customerConsigneePopupVO.getPageSize(), "页数不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r2 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r3 = ToolKits.isEmpty(customerConsigneePopupVO.getCustomerId()) ? null : Restrictions.eq("customerId", customerConsigneePopupVO.getCustomerId());
		Restriction r4 = ToolKits.isEmpty(customerConsigneePopupVO.getCode()) ? null : Restrictions.like("code", "%" + customerConsigneePopupVO.getCode() + "%");
		Restriction r5 = ToolKits.isEmpty(customerConsigneePopupVO.getName()) ? null : Restrictions.like("name", "%" + customerConsigneePopupVO.getName() + "%");

		// 权限控制
		Restriction r6 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_N)) {
				r6 = Restrictions.in("customerId", user.getCustomers().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			r6 = null;
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			r6 = Restrictions.eq("customerId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(customerConsigneePopupVO.getPageNum(), customerConsigneePopupVO.getPageSize());
		List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria);
		for (TmsCustomerConsignee tmsCustomerConsignee : tmsCustomerConsignees) {
			// 响应对象
			CustomerConsigneePopupDTO customerConsigneePopupDTO = new CustomerConsigneePopupDTO();
			BeanUtils.copyProperties(tmsCustomerConsignee, customerConsigneePopupDTO);

			customerConsigneePopupDTOs.add(customerConsigneePopupDTO);
		}

		Page<CustomerConsigneePopupDTO> page = PageUtils.getPage(customerConsigneePopupDTOs);
		if (tmsCustomerConsignees instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsCustomerConsignee> p0 = (com.github.pagehelper.Page<TmsCustomerConsignee>) tmsCustomerConsignees;
			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 file
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchImportCustomerData(MultipartFile file, String importBatchNumber) throws Exception {
		// 人员权限
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 导入文件判断
		FileUtils.multipartFileJudge(file);
		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.CUSTOMER_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<CustomerImportStdVO> importVOClass = CustomerImportStdVO.class;
			List<CustomerImportStdVO> 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.CUSTOMER_IMPORT_LOG, logAddVO);
			e.printStackTrace();
			throw new ParameterException(e.getMessage());
		}
	}

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

	/**
	 * 客户导入过程(新)
	 *
	 * @param file
	 * @param importBatchNumber
	 * @return
	 * @throws Exception
	 */
	public int batchInsertProcess(MultipartFile file, String importBatchNumber, UserEntity user) {
		Date date = new Date();
		Properties properties = new Properties();
		// 待转换的对象
		Class<CustomerImportStdVO> importVOClass = CustomerImportStdVO.class;
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.CUSTOMER_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<CustomerImportStdVO> 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 (CustomerImportStdVO importVO : importVOs) {
			if (ToolKits.isEmpty(importVO.getCode())) {
				throw new PrologException("第" + i + "行导入数据,客户编码不能为空");
			}
			if (ToolKits.isEmpty(importVO.getName())) {
				throw new PrologException("第" + i + "行导入数据,客户名称不能为空");
			}
			if (ToolKits.isEmpty(importVO.getDepartmentName())) {
				throw new PrologException("第" + i + "行导入数据,部门名称不能为空");
			}
			if (ToolKits.isEmpty(importVO.getCooperativeNature())) {
				throw new PrologException("第" + i + "行导入数据,合作性质为必选项");
			}
			if (ToolKits.isEmpty(importVO.getCargoOwnerName())) {
				throw new PrologException("第" + i + "行导入数据,所属货主名称不能为空");
			}
			i++;
		}
		// 验证导入文件中客户编号是否重复
		Map<String, Long> codeMap = importVOs.stream().collect(Collectors.groupingBy(CustomerImportStdVO::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(CustomerImportStdVO::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;
		// 客户对象
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		for (CustomerImportStdVO importVO : importVOs) {
			TmsBaseCustomerModel tmsBaseCustomerModel = new TmsBaseCustomerModel();
			BeanUtils.copyProperties(importVO, tmsBaseCustomerModel);
			List<Organization> data1 = Lists.newArrayList();
			// 货主校验
			CargoOwnerPagingQueryVO cargoOwnerPagingQueryVO = new CargoOwnerPagingQueryVO();
			cargoOwnerPagingQueryVO.setCargoOwnerName(importVO.getCargoOwnerName());
			cargoOwnerPagingQueryVO.setEnterpriseId(user.getEnterpriseId());
			if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
				// 自有账号
				// 所属部门名称判定是否存在
				String[] departments = importVO.getDepartmentName().split(",");
				List<String> deptIds = Lists.newArrayList();
				List<String> deptCodes = Lists.newArrayList();
				for (String dept : departments) {
					RestMessage<List<Organization>> departMents = userTmsResource.search(null, dept, null, String.valueOf(user.getEnterpriseId()));
					if (!departMents.isSuccess()) {
						// throw new RuntimeException("部门【" + importVO.getDepartmentName() + "】不存在!");
						throw new RuntimeException("用户部门信息查询失败");
					} else {
						data1 = departMents.getData();
						if (data1.size() == 0) {
							throw new RuntimeException("部门【" + dept + "】不存在!");
						}
						if (!user.getDepts().contains(data1.get(0).getId())) {
							throw new RuntimeException("部门【" + dept + "】不存在，请先维护部门!");
						}
						// 部门信息赋值
						Organization organization = data1.get(0);
						deptIds.add(organization.getId());
						deptCodes.add(organization.getCode());
					}
				}
				tmsBaseCustomerModel.setDepartmentName(importVO.getDepartmentName());
				tmsBaseCustomerModel.setDepartmentId(deptIds.stream().collect(Collectors.joining(",")));
				tmsBaseCustomerModel.setDepartmentCode(deptCodes.stream().collect(Collectors.joining(",")));

				// 判断是否为部门权限
				if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
					// 部门权限
					cargoOwnerPagingQueryVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
					cargoOwnerPagingQueryVO.setIsDept("Y");
				} else {
					// 非部门权限，顶级部门取得
					cargoOwnerPagingQueryVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
					cargoOwnerPagingQueryVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
					cargoOwnerPagingQueryVO.setIsDept("N");
				}
			} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
				// 获取当前账号的顶级部门
				RestMessage<List<Organization>> restMessage3 = userTmsResource.search("0", null, null, String.valueOf(user.getEnterpriseId()));
				if (!restMessage3.isSuccess()) {
					throw new RuntimeException("获取部门信息失败!");
				}
				data1 = restMessage3.getData();
				ToolKits.isParamNull(data1, "顶级部门不存在,请先维护部门!");
				String[] depts = { data1.get(0).getId() };
				Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
				cargoOwnerPagingQueryVO.setDepartmentIds(depts);
				cargoOwnerPagingQueryVO.setCargoOwnerIds(ownerIds);
			}
			List<CargoOwnerPagingQueryDTO> tmsBaseCargoOwnerModels = tmsBaseCargoOwnerMapper.pageCargoOwnerData(cargoOwnerPagingQueryVO);
			ToolKits.isParamNull(tmsBaseCargoOwnerModels, "货主【" + importVO.getCargoOwnerName() + "】不存在!");

			CargoOwnerPagingQueryDTO tmsBaseCargoOwnerModel = tmsBaseCargoOwnerModels.get(0);
			// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
			Assert.isTrue(tmsBaseCargoOwnerModel.getStatus() != StatusConstant.STATUS_DISABLE, "货主【" + importVO.getCargoOwnerName() + "】已被停用");

			if (ToolKits.isNotEmpty(importVO.getPhoneNumber()) && !Validator.isMobile(importVO.getPhoneNumber())) {
				throw new ParameterException(String.format("%s手机号不符合规范", importVO.getPhoneNumber()));
			}
			if (ToolKits.isNotEmpty(importVO.getEmail()) && !Validator.isEmail(importVO.getEmail())) {
				throw new ParameterException(String.format("%邮箱不符合规范", importVO.getEmail()));
			}
			/*
			 * if (ToolKits.isNotEmpty(importVO.getPostCode()) &&
			 * !Validator.isZipCode(importVO.getPostCode())) { throw new
			 * ParameterException(String.format("%邮编不符合规范", importVO.getPostCode())); }
			 */
			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 (ToolKits.isNotEmpty(importVO.getLandlineNumber()) && importVO.getLandlineNumber().length() > 9) {
				matcher = p1.matcher(importVO.getLandlineNumber());
				if (!matcher.matches()) {
					throw new RuntimeException("座机号错误!");
				}
			} else {
				if (ToolKits.isNotEmpty(importVO.getLandlineNumber())) {
					matcher = p2.matcher(importVO.getLandlineNumber());
					if (!matcher.matches()) {
						throw new RuntimeException("座机号错误!");
					}
				}
			}
			Map<String, Object> map = Maps.newHashMap();
			map.put("code", importVO.getCode());
			map.put("enterpriseId", user.getEnterpriseId());
			map.put("cargoOwnerId", tmsBaseCargoOwnerModel.getCargoOwnerId());
			map.put("status", StatusConstant.STATUS_ENABLE);

			Map<String, Object> map2 = Maps.newHashMap();
			map2.put("name", importVO.getName());
			map2.put("enterpriseId", user.getEnterpriseId());
			map2.put("cargoOwnerId", tmsBaseCargoOwnerModel.getCargoOwnerId());
			map2.put("status", StatusConstant.STATUS_ENABLE);

			// 判断客户编码 客户名称 是否已存在
			PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map, TmsBaseCustomerModel.class, "客户编码[" + importVO.getCode() + "]已存在");
			PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map2, TmsBaseCustomerModel.class, "客户名称[" + importVO.getName() + "]已存在");

			// 判断客户编码 客户名称 是否已停用
			map.put("status", StatusConstant.STATUS_DISABLE);
			map2.put("status", StatusConstant.STATUS_DISABLE);
			PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map, TmsBaseCustomerModel.class, "客户编码[" + importVO.getCode() + "]已停用,请直接启用");
			PublicMethods.doesItExistByMap(tmsBaseCustomerMapper, map2, TmsBaseCustomerModel.class, "客户名称[" + importVO.getName() + "]已停用,请直接启用");

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

			// 客户ID
			String customerId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
			ToolKits.isParamNull(customerId, "客户ID生成失败");

			// 客户资料对象
			tmsBaseCustomerModel.setId(Long.valueOf(customerId));
			tmsBaseCustomerModel.setStatus(StatusConstant.STATUS_ENABLE);
			tmsBaseCustomerModel.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			tmsBaseCustomerModel.setImportBatchNumber(importBatchNumber);
			tmsBaseCustomerModel.setEnterpriseId(user.getEnterpriseId());
			tmsBaseCustomerModel.setEnterpriseName(user.getEnterpriseName());
			tmsBaseCustomerModel.setCreateperson(user.getNickname());
			tmsBaseCustomerModel.setCreatetime(date);
			tmsBaseCustomerModel.setUpdateperson(user.getNickname());
			tmsBaseCustomerModel.setUpdatetime(date);
			if (ToolKits.isNotEmpty(importVO.getCooperativeNature())) {
				tmsBaseCustomerModel.setCooperativeNature(Integer.parseInt(importVO.getCooperativeNature()));
			}
			tmsBaseCustomerModel.setCargoOwnerId(tmsBaseCargoOwnerModel.getCargoOwnerId());
			tmsBaseCustomerModel.setCargoOwnerCode(tmsBaseCargoOwnerModel.getCargoOwnerCode());
			tmsBaseCustomerModel.setCargoOwnerName(tmsBaseCargoOwnerModel.getCargoOwnerName());

			tmsBaseCustomerModel.setPickUpSign(ToolKits.isEmpty(importVO.getPickUpSign()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getPickUpSign()));
			tmsBaseCustomerModel.setAutoPickUpSign(ToolKits.isEmpty(importVO.getAutoPickUpSign()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getAutoPickUpSign()));
			tmsBaseCustomerModel.setPickUpPicture(ToolKits.isEmpty(importVO.getPickUpPicture()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getPickUpPicture()));
			tmsBaseCustomerModel.setDeliverySign(ToolKits.isEmpty(importVO.getDeliverySign()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getDeliverySign()));
			tmsBaseCustomerModel.setAutoDeliverySign(ToolKits.isEmpty(importVO.getAutoDeliverySign()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getAutoDeliverySign()));
			tmsBaseCustomerModel.setDeliveryPicture(ToolKits.isEmpty(importVO.getDeliveryPicture()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getDeliveryPicture()));
			tmsBaseCustomerModel.setReturnOrderFlag(ToolKits.isEmpty(importVO.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : Integer.parseInt(importVO.getReturnOrderFlag()));

			if (ToolKits.isNotEmpty(importVO.getPickUpSignTime())) {
				tmsBaseCustomerModel.setPickUpSignTime(Integer.valueOf(importVO.getPickUpSignTime()));
			}
			if (ToolKits.isNotEmpty(importVO.getPickUpSignTimeUnit())) {
				tmsBaseCustomerModel.setPickUpSignTimeUnit(Integer.valueOf(importVO.getPickUpSignTimeUnit()));
			}
			if (ToolKits.isNotEmpty(importVO.getDeliverySignTime())) {
				tmsBaseCustomerModel.setDeliverySignTime(Integer.valueOf(importVO.getDeliverySignTime()));
			}
			if (ToolKits.isNotEmpty(importVO.getDeliverySignTimeUnit())) {
				tmsBaseCustomerModel.setDeliverySignTimeUnit(Integer.valueOf(importVO.getDeliverySignTimeUnit()));
			}

			tmsBaseCustomerMapper.save(tmsBaseCustomerModel);
			i++;
		}
		return i;
	}

	/**
	 * 客户收货方批量导入(新)
	 *
	 * @param file
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchImportCustomerReceiverData(MultipartFile file, String importBatchNumber) throws Exception {
		// 人员权限
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 导入文件判断
		FileUtils.multipartFileJudge(file);
		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.CUSTOMER_CONSIGNEE_IMPORT);
		logAddVO.setImportBatchNumber(importBatchNumber);
		// 导入统计
		int count = 0;
		try {
			// 导入执行
			count = this.batchImportCustomerReceiverData(file, importBatchNumber, user);
			logAddVO.setStatus(DataDictionaryConstant.IMPORT_STATUS_YES);
			logAddVO.setSuccessNum(count);
			logAddVO.setFailNum(0);
			importLogService.addImportLogData(logAddVO);
		} catch (Exception e) {
			Class<CustomerReceiverImportStdVO> importVOClass = CustomerReceiverImportStdVO.class;
			List<CustomerReceiverImportStdVO> 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.CUSTOMER_RECEIVER_IMPORT_LOG, logAddVO);
			e.printStackTrace();
			throw new ParameterException(e.getMessage());
		}
	}

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

	/**
	 * 客户收货方导入过程(新)
	 *
	 * @param file
	 * @param importBatchNumber
	 * @return
	 * @throws Exception
	 */
	public int batchImportCustomerReceiverData(MultipartFile file, String importBatchNumber, UserEntity user) {
		// 当前时间
		Date date = new Date();
		Properties properties = new Properties();
		// 待转换的对象
		Class<CustomerReceiverImportStdVO> importVOClass = CustomerReceiverImportStdVO.class;
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.CUSTOMER_CONSIGNEE_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<CustomerReceiverImportStdVO> 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 (CustomerReceiverImportStdVO importVO : importVOs) {
			if (ToolKits.isEmpty(importVO.getCustomerName())) {
				throw new PrologException("第" + i + "行导入数据,客户名称不能为空");
			}
			if (ToolKits.isEmpty(importVO.getCode())) {
				throw new PrologException("第" + i + "行导入数据,收货方编码不能为空");
			}
			if (ToolKits.isEmpty(importVO.getName())) {
				throw new PrologException("第" + i + "行导入数据,收货方名称不能为空");
			}
			i++;
		}
		// 验证导入文件中客户收货放编号是否重复
		Map<String, Long> codeMap = importVOs.stream().collect(Collectors.groupingBy(CustomerReceiverImportStdVO::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(CustomerReceiverImportStdVO::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 (CustomerReceiverImportStdVO importVO : importVOs) {
			if (ToolKits.isNotEmpty(importVO.getPhoneNumber()) && !Validator.isMobile(importVO.getPhoneNumber())) {
				throw new ParameterException(String.format("%s手机号不符合规范", importVO.getPhoneNumber()));
			}
			if (ToolKits.isNotEmpty(importVO.getEmail()) && !Validator.isEmail(importVO.getEmail())) {
				// throw new ParameterException(String.format("%邮箱不符合规范", importVO.getEmail()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,邮箱格式不符合规范!");
			}
			if (ToolKits.isNotEmpty(importVO.getRelationPhoneNumber()) && !Validator.isMobile(importVO.getRelationPhoneNumber())) {
				// throw new ParameterException(String.format("%关联手机不符合规范",
				// importVO.getRelationPhoneNumber()));
				throw new ParameterException("第" + (i + 1) + "行导入数据,关联手机号格式不符合规范!");
			}
			if (ToolKits.isNotEmpty(importVO.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 (importVO.getLandlineNumber().length() > 9) {
					matcher = p1.matcher(importVO.getLandlineNumber());
					if (!matcher.matches()) {
						throw new RuntimeException("座机号错误!");
					}
				} else {
					matcher = p2.matcher(importVO.getLandlineNumber());
					if (!matcher.matches()) {
						throw new RuntimeException("座机号错误!");
					}
				}
			}

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

			if (ToolKits.isNotEmpty(importVO.getCarrierType())) {
				if (ToolKits.isEmpty(importVO.getCarrierCode()) || ToolKits.isEmpty(importVO.getCarrierName())) {
					throw new PrologException("第" + (i + 1) + "行导入数据,承运方编码、承运方名称不能为空!");
				}
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("enterpriseId", user.getEnterpriseId());
				queryMap.put("code", importVO.getCarrierCode());
				queryMap.put("name", importVO.getCarrierName());
				if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_CARRIER, Integer.parseInt(importVO.getCarrierType()))) {
					// 校验承运商是否存在
					List<TmsBaseCarriersModel> tmsBaseCarriers = tmsBaseCarriersMapper.findByMap(queryMap, TmsBaseCarriersModel.class);
					if (ToolKits.isEmpty(tmsBaseCarriers) || tmsBaseCarriers.size() == 0) {
						throw new PrologException("第" + (i + 1) + "行导入数据,承运商编码或承运商名称在系统中不存在!");
					}
					Assert.isTrue(tmsBaseCarriers.get(0).getStatus() != StatusConstant.STATUS_DISABLE, "承运商[" + importVO.getCarrierName() + "]已被停用");
					model.setCarrierId(tmsBaseCarriers.get(0).getId());
				} else if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER, Integer.parseInt(importVO.getCarrierType()))) {
					// 校验司机是否存在
					List<TmsBaseDriverModel> tmsBaseDrivers = tmsBaseDriverMapper.findByMap(queryMap, TmsBaseDriverModel.class);
					if (ToolKits.isEmpty(tmsBaseDrivers) || tmsBaseDrivers.size() == 0) {
						throw new PrologException("第" + (i + 1) + "行导入数据,司机编码或司机名称在系统中不存在!");
					}
					Assert.isTrue(tmsBaseDrivers.get(0).getStatus() != StatusConstant.STATUS_DISABLE, "司机[" + importVO.getCarrierName() + "]已被停用");
					model.setCarrierId(tmsBaseDrivers.get(0).getId());
				} else {
					throw new PrologException("第" + (i + 1) + "行导入数据,默认承运方类型错误");
				}
			}

			if (ToolKits.isEmpty(importVO.getCarrierType())) {
				if (ToolKits.isNotEmpty(importVO.getCarrierCode()) || ToolKits.isNotEmpty(importVO.getCarrierName())) {
					throw new PrologException("第" + (i + 1) + "行导入数据,请选择默认承运方类型!");
				}
			}

			/*
			 * if(ToolKits.isNotEmpty(importVO.getDeliveryPattern())){
			 * if(ObjectUtil.notEqual(importVO.getDeliveryPattern(),DataDictionaryConstant.
			 * DELIVERY_PATTERN_DELIVERY)
			 * &&ObjectUtil.notEqual(importVO.getDeliveryPattern(),DataDictionaryConstant.
			 * DELIVERY_PATTERN_PICKUP)){ throw new
			 * PrologException("第"+(i+1)+"行导入数据,配送方式错误!"); } }
			 */

			Criteria criteria = Criteria.forClass(TmsBaseCustomerModel.class);
			Restriction r1 = Restrictions.eq("name", importVO.getCustomerName());
			Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
			criteria.setRestriction(Restrictions.and(r1, r2));
			List<TmsBaseCustomerModel> tmsBaseCustomerModels = tmsBaseCustomerMapper.findByCriteria(criteria);
			ToolKits.isParamNull(tmsBaseCustomerModels, "未找到对应的客户[" + importVO.getCustomerName() + "]");
			TmsBaseCustomerModel tmsBaseCustomerModel = tmsBaseCustomerModels.get(0);
			Assert.isTrue(tmsBaseCustomerModel.getStatus() != StatusConstant.STATUS_DISABLE, "客户[" + importVO.getCustomerName() + "]已被停用");
			// 客户ID
			model.setCustomerId(tmsBaseCustomerModel.getId());

			// 判断收货方编码是否已存在
			PublicMethods.isThereAnyinformation(TmsCustomerConsignee.class, "code", importVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsCustomerConsigneeMapper, "收货方编码[" + importVO.getCode() + "]已存在");
			PublicMethods.isThereAnyinformation(TmsCustomerConsignee.class, "code", importVO.getCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsCustomerConsigneeMapper, "收货方编码[" + importVO.getCode() + "]已停用,请直接启用!");
			// 判断收货方名称是否已存在
			PublicMethods.isThereAnyinformation(TmsCustomerConsignee.class, "name", importVO.getName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsCustomerConsigneeMapper, "收货方名称[" + importVO.getName() + "]已存在");
			PublicMethods.isThereAnyinformation(TmsCustomerConsignee.class, "name", importVO.getName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsCustomerConsigneeMapper, "收货方名称[" + importVO.getName() + "]已停用,请直接启用!");

			// 客户收货方ID
			String customerId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
			ToolKits.isParamNull(customerId, "客户收货方ID生成失败");
			model.setId(Long.valueOf(customerId));
			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);
			model.setCustomerCode(tmsBaseCustomerModel.getCode());
			if (ToolKits.isNotEmpty(importVO.getCarrierType())) {
				model.setCarrierType(Integer.parseInt(importVO.getCarrierType()));
			}
			if (ToolKits.isNotEmpty(importVO.getDeliveryPattern())) {
				model.setDeliveryPattern(Integer.parseInt(importVO.getDeliveryPattern()));
			}
			tmsCustomerConsigneeMapper.save(model);
			i++;
		}
		return i;
	}

	/**
	 * 根据客户编码名称查询客户资料
	 */
	@Override
	public CustomerByEncodingNameDTO getCustomerByEncodingNameData(CustomerByEncodingNameVO customerByEncodingNameVO) {
		ToolKits.isParamNull(customerByEncodingNameVO, "根据客户编码名称查询客户资料请求不为空");
		ToolKits.isParamNull(customerByEncodingNameVO.getCode(), "客户编码不为空");
		ToolKits.isParamNull(customerByEncodingNameVO.getName(), "客户名称不为空");

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

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		customerByEncodingNameVO.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			customerByEncodingNameVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				customerByEncodingNameVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				customerByEncodingNameVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				customerByEncodingNameVO.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				customerByEncodingNameVO.setIsCustom("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerByEncodingNameVO.setCargoOwnerIds(ownerIds);
			customerByEncodingNameVO.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerByEncodingNameVO.setIdArrs(ids);
			customerByEncodingNameVO.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		List<TmsBaseCustomerDTO> tmsBaseCustomerModels = tmsBaseCustomerMapper.getCustomerByEncodingNameData(customerByEncodingNameVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
		Assert.isTrue(tmsBaseCustomerModels.size() > 0, "不存在编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户");
		Assert.isTrue(tmsBaseCustomerModels.size() == 1, "编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户资料重复");

		// 客户资料对象
		TmsBaseCustomerDTO tmsBaseCustomerModel = tmsBaseCustomerModels.get(0);
		Assert.isTrue(tmsBaseCustomerModel.getStatus() == StatusConstant.STATUS_ENABLE, "客户【" + customerByEncodingNameVO.getName() + "】未启用");

		// 根据客户查询客户收货方
		Criteria criteria1 = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r11 = Restrictions.eq("customerId", tmsBaseCustomerModel.getId());
		Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria1.setRestriction(Restrictions.and(r11, r12));
		List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria1);
		Assert.isTrue(tmsCustomerConsignees.size() > 0, "编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户未维护客户收货方");
		Assert.isTrue(tmsCustomerConsignees.size() == 1, "请指定编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户收货方");

		// 客户收货方资料对象
		TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsignees.get(0);
		Assert.isTrue(tmsCustomerConsignee.getStatus() == StatusConstant.STATUS_ENABLE, "客户收货方【" + tmsCustomerConsignee.getName() + "】未启用");

		// 响应对象
		CustomerConsigneeByEncodingNameDTO customerConsigneeByEncodingNameDTO = new CustomerConsigneeByEncodingNameDTO();
		BeanUtils.copyProperties(tmsCustomerConsignee, customerConsigneeByEncodingNameDTO);

		// 响应对象
		CustomerByEncodingNameDTO customerByEncodingNameDTO = new CustomerByEncodingNameDTO();
		BeanUtils.copyProperties(tmsBaseCustomerModel, customerByEncodingNameDTO);
		customerByEncodingNameDTO.setConsignee(customerConsigneeByEncodingNameDTO);

		return customerByEncodingNameDTO;
	}

	/**
	 * 根据客户收货方编码名称查询客户收货方资料
	 */
	@Override
	public CustomerConsigneeByEncodingNameDTO getCustomerConsigneeByEncodingNameData(CustomerConsigneeByEncodingNameVO customerConsigneeByEncodingNameVO) {
		ToolKits.isParamNull(customerConsigneeByEncodingNameVO, "根据客户收货方编码名称查询客户收货方资料请求不为空");
		ToolKits.isParamNull(customerConsigneeByEncodingNameVO.getCode(), "客户收货方编码不为空");
		ToolKits.isParamNull(customerConsigneeByEncodingNameVO.getName(), "客户收货方名称不为空");
		ToolKits.isParamNull(customerConsigneeByEncodingNameVO.getCustomerId(), "客户ID为空");

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

		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.eq("code", customerConsigneeByEncodingNameVO.getCode());
		Restriction r2 = Restrictions.eq("name", customerConsigneeByEncodingNameVO.getName());
		Restriction r3 = Restrictions.eq("customerId", customerConsigneeByEncodingNameVO.getCustomerId());
		Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria);
		Assert.isTrue(tmsCustomerConsignees.size() > 0, "不存在编码为【" + customerConsigneeByEncodingNameVO.getCode() + "】,名称为【" + customerConsigneeByEncodingNameVO.getName() + "】的客户收货方");
		Assert.isTrue(tmsCustomerConsignees.size() == 1, "编码为【" + customerConsigneeByEncodingNameVO.getCode() + "】,名称为【" + customerConsigneeByEncodingNameVO.getName() + "】的客户收货方资料重复");

		// 客户收货方资料对象
		TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsignees.get(0);
		Assert.isTrue(tmsCustomerConsignee.getStatus() == StatusConstant.STATUS_ENABLE, "客户收货方【" + customerConsigneeByEncodingNameVO.getName() + "】未启用");

		// 响应对象
		CustomerConsigneeByEncodingNameDTO customerConsigneeByEncodingNameDTO = new CustomerConsigneeByEncodingNameDTO();
		BeanUtils.copyProperties(tmsCustomerConsignee, customerConsigneeByEncodingNameDTO);

		return customerConsigneeByEncodingNameDTO;
	}

	/**
	 * 客户匹配唯一承运方
	 *
	 * @param carrierVO
	 */
	public CustomerCarrierDTO customerCarrier(CustomerCarrierVO carrierVO) {
		ToolKits.isParamNull(carrierVO, "客户匹配唯一承运方请求不为空");
		ToolKits.isParamNull(carrierVO.getConsigneeId(), "客户收货方ID不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		CustomerCarrierDTO carrierDTO = new CustomerCarrierDTO();
		TmsCustomerConsignee customerConsignee = tmsCustomerConsigneeMapper.findById(carrierVO.getConsigneeId(), TmsCustomerConsignee.class);
		if (ToolKits.isNotEmpty(customerConsignee)) {
			BeanUtils.copyProperties(customerConsignee, carrierDTO);
		} else {
			return null;
		}
		return carrierDTO;
	}

	/**
	 * 客户接口转TMS
	 */
	@Override
	public List<IntfErrorLogDTO> saveCustomerInterfaceData(List<IntfCustomerSaveVO> intfCustomerSaveVOs) {
		// 响应集合
		List<IntfErrorLogDTO> intfErrorLogDTOs = Lists.newArrayList();

		Date date = DateUtil.date();

		// 循环处理
		for (IntfCustomerSaveVO intfCustomerSaveVO : intfCustomerSaveVOs) {
		    //api    https://lbs.amap.com/api/webservice/guide/api/georegeo
            BigDecimal longitude = null;
            BigDecimal latitude = null;
            if(ObjectUtil.isNotEmpty(intfCustomerSaveVO.getAddress()) && ObjectUtil.isEmpty(intfCustomerSaveVO.getLongitude()) && ObjectUtil.isEmpty(intfCustomerSaveVO.getLatitude())){
                log.info("高德入参:{}", intfCustomerSaveVO.getAddress());
                JSONObject jsonObject = aMapGeocodingUtil.amapGeocodingInfo(intfCustomerSaveVO.getAddress());
                log.info("高德响应:{}", jsonObject.toJSONString());
                JSONArray geocodes = jsonObject.getJSONArray("geocodes");
                if (ObjectUtil.isNotEmpty(geocodes)) {
                    JSONObject geocode = geocodes.getJSONObject(0);
                    String longitudeAndLatitude = geocode.getString("location");
                    if(ObjectUtil.isNotEmpty(longitudeAndLatitude)){
                        String[] str = longitudeAndLatitude.split(",");
                        longitude = new BigDecimal(str[0]);
                        latitude = new BigDecimal(str[1]);
                    }
                }
            }

			// 响应对象
			IntfErrorLogDTO intfErrorLogDTO = new IntfErrorLogDTO();

			try {
				// 货主校验
				Criteria criteria1 = Criteria.forClass(TmsBaseCargoOwnerModel.class);
				Restriction r11 = Restrictions.eq(TmsBaseCargoOwnerModel::getCargoOwnerCode, intfCustomerSaveVO.getCargoOwnerCode());
				Restriction r12 = Restrictions.eq(TmsBaseCargoOwnerModel::getCargoOwnerName, intfCustomerSaveVO.getCargoOwnerName());
				Restriction r13 = Restrictions.eq(TmsBaseCargoOwnerModel::getEnterpriseId, intfCustomerSaveVO.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r12, r13));
				List<TmsBaseCargoOwnerModel> tmsBaseCargoOwnerModels = tmsBaseCargoOwnerMapper.findByCriteria(criteria1);
				Assert.isTrue(tmsBaseCargoOwnerModels.size() > 0, "货主【" + intfCustomerSaveVO.getCargoOwnerName() + "】不存在");
				Assert.isTrue(tmsBaseCargoOwnerModels.size() <= 1, "货主【" + intfCustomerSaveVO.getCargoOwnerName() + "】资料重复");

				// 货主对象
				TmsBaseCargoOwnerModel baseCargoOwnerModel = tmsBaseCargoOwnerModels.get(0);
				Assert.isTrue(baseCargoOwnerModel.getStatus().intValue() == StatusConstant.STATUS_ENABLE.intValue(), "货主【" + intfCustomerSaveVO.getCargoOwnerName() + "】未启用");

				// 校验客户
				Criteria criteria = Criteria.forClass(TmsBaseCustomerModel.class);
				Restriction r1 = Restrictions.eq(TmsBaseCustomerModel::getCode, intfCustomerSaveVO.getCode());
				Restriction r2 = Restrictions.eq(TmsBaseCustomerModel::getCargoOwnerId, baseCargoOwnerModel.getCargoOwnerId());
				Restriction r3 = Restrictions.eq(TmsBaseCustomerModel::getEnterpriseId, intfCustomerSaveVO.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2, r3));
				List<TmsBaseCustomerModel> tmsBaseCustomerModels = tmsBaseCustomerMapper.findByCriteria(criteria);
				if (tmsBaseCustomerModels.size() > 0) {
					Assert.isTrue(tmsBaseCustomerModels.size() <= 1, "客户编码[" + intfCustomerSaveVO.getCode() + "]维护重复");

					// 客户资料对象
					TmsBaseCustomerModel tmsBaseCustomerModel = tmsBaseCustomerModels.get(0);
					tmsBaseCustomerModel.setName(intfCustomerSaveVO.getName());
					tmsBaseCustomerModel.setShortName(intfCustomerSaveVO.getShortName());
					tmsBaseCustomerModel.setCargoOwnerId(baseCargoOwnerModel.getCargoOwnerId());
					tmsBaseCustomerModel.setCargoOwnerCode(baseCargoOwnerModel.getCargoOwnerCode());
					tmsBaseCustomerModel.setCargoOwnerName(baseCargoOwnerModel.getCargoOwnerName());
					tmsBaseCustomerModel.setContact(intfCustomerSaveVO.getContact());
					tmsBaseCustomerModel.setPhoneNumber(intfCustomerSaveVO.getPhoneNumber());
					tmsBaseCustomerModel.setCooperativeNature(intfCustomerSaveVO.getCooperativeNature());
					tmsBaseCustomerModel.setAddress(intfCustomerSaveVO.getAddress());
					tmsBaseCustomerModel.setPickUpSign(ToolKits.isEmpty(intfCustomerSaveVO.getPickUpSign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getPickUpSign());
					tmsBaseCustomerModel.setAutoPickUpSign(ToolKits.isEmpty(intfCustomerSaveVO.getAutoPickUpSign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getAutoPickUpSign());
					tmsBaseCustomerModel.setPickUpPicture(ToolKits.isEmpty(intfCustomerSaveVO.getPickUpPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getPickUpPicture());
					tmsBaseCustomerModel.setDeliverySign(ToolKits.isEmpty(intfCustomerSaveVO.getDeliverySign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getDeliverySign());
					tmsBaseCustomerModel.setAutoDeliverySign(ToolKits.isEmpty(intfCustomerSaveVO.getAutoDeliverySign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getAutoDeliverySign());
					tmsBaseCustomerModel.setDeliveryPicture(ToolKits.isEmpty(intfCustomerSaveVO.getDeliveryPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getDeliveryPicture());
					tmsBaseCustomerModel.setReturnOrderFlag(ToolKits.isEmpty(intfCustomerSaveVO.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getReturnOrderFlag());
					tmsBaseCustomerModel.setReturnOrderPicture(ToolKits.isEmpty(intfCustomerSaveVO.getReturnOrderPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getReturnOrderPicture());
					tmsBaseCustomerModel.setStatus(intfCustomerSaveVO.getStatus());
					tmsBaseCustomerModel.setUpdatetime(date);
					if((ToolKits.isNotEmpty(intfCustomerSaveVO.getAddress()))
					        && ToolKits.isEmpty(intfCustomerSaveVO.getLongitude())
							&& ToolKits.isEmpty(intfCustomerSaveVO.getLatitude())){
                        tmsBaseCustomerModel.setLongitude(longitude);
                        tmsBaseCustomerModel.setLatitude(latitude);
                    }else {
                        tmsBaseCustomerModel.setLongitude(intfCustomerSaveVO.getLongitude());
                        tmsBaseCustomerModel.setLatitude(intfCustomerSaveVO.getLatitude());
                    }
					tmsBaseCustomerModel.setProvince(intfCustomerSaveVO.getProvince());
					tmsBaseCustomerModel.setProvinceName(intfCustomerSaveVO.getProvinceName());
					tmsBaseCustomerModel.setCity(intfCustomerSaveVO.getCity());
					tmsBaseCustomerModel.setCityName(intfCustomerSaveVO.getCityName());
					tmsBaseCustomerModel.setDistrict(intfCustomerSaveVO.getDistrict());
					tmsBaseCustomerModel.setDistrictName(intfCustomerSaveVO.getDistrictName());
					tmsBaseCustomerMapper.update(tmsBaseCustomerModel);

					// 更新客户收货方
					Map<String, Object> map = Maps.newHashMap();
					map.put("customerId", tmsBaseCustomerModel.getId());
					map.put("code", tmsBaseCustomerModel.getCode());
					map.put("enterpriseId", tmsBaseCustomerModel.getEnterpriseId());
					List<TmsCustomerConsignee> customerConsignees = tmsCustomerConsigneeMapper.findByMap(map, TmsCustomerConsignee.class);
					if (customerConsignees.size() > 0) {
						Assert.isTrue(customerConsignees.size() <= 1, "客户收货方编码[" + intfCustomerSaveVO.getCode() + "]维护重复");

						TmsCustomerConsignee tmsCustomerConsignee = customerConsignees.get(0);
						tmsCustomerConsignee.setName(tmsBaseCustomerModel.getName());
						tmsCustomerConsignee.setShortName(tmsBaseCustomerModel.getShortName());
						tmsCustomerConsignee.setCustomerName(tmsBaseCustomerModel.getName());
						tmsCustomerConsignee.setContact(tmsBaseCustomerModel.getContact());
						tmsCustomerConsignee.setPhoneNumber(tmsBaseCustomerModel.getPhoneNumber());
						tmsCustomerConsignee.setLandlineNumber(tmsBaseCustomerModel.getLandlineNumber());
						tmsCustomerConsignee.setEmail(tmsBaseCustomerModel.getEmail());
						tmsCustomerConsignee.setRelationPhoneNumber(null);
						tmsCustomerConsignee.setProvince(tmsBaseCustomerModel.getProvince());
						tmsCustomerConsignee.setProvinceName(tmsBaseCustomerModel.getProvinceName());
						tmsCustomerConsignee.setCity(tmsBaseCustomerModel.getCity());
						tmsCustomerConsignee.setCityName(tmsBaseCustomerModel.getCityName());
						tmsCustomerConsignee.setDistrict(tmsBaseCustomerModel.getDistrict());
						tmsCustomerConsignee.setDistrictName(tmsBaseCustomerModel.getDistrictName());
						tmsCustomerConsignee.setAddress(tmsBaseCustomerModel.getAddress());
						tmsCustomerConsignee.setLongitude(tmsBaseCustomerModel.getLongitude());
						tmsCustomerConsignee.setLatitude(tmsBaseCustomerModel.getLatitude());
						tmsCustomerConsignee.setStatus(tmsBaseCustomerModel.getStatus());
						tmsCustomerConsignee.setMemo(tmsBaseCustomerModel.getMemo());
						tmsCustomerConsignee.setExtStr1(intfCustomerSaveVO.getExtStr1());
						tmsCustomerConsignee.setExtStr2(intfCustomerSaveVO.getExtStr2());
						tmsCustomerConsignee.setExtStr3(intfCustomerSaveVO.getExtStr3());
						tmsCustomerConsignee.setExtStr4(intfCustomerSaveVO.getExtStr4());
						tmsCustomerConsignee.setExtStr5(intfCustomerSaveVO.getExtStr5());
						tmsCustomerConsignee.setExtDate1(intfCustomerSaveVO.getExtDate1());
						tmsCustomerConsignee.setExtDate2(intfCustomerSaveVO.getExtDate2());
						tmsCustomerConsignee.setExtDate3(intfCustomerSaveVO.getExtDate3());
						tmsCustomerConsignee.setExtDate4(intfCustomerSaveVO.getExtDate4());
						tmsCustomerConsignee.setExtDate5(intfCustomerSaveVO.getExtDate5());
						tmsCustomerConsignee.setExtNum1(intfCustomerSaveVO.getExtNum1());
						tmsCustomerConsignee.setExtNum2(intfCustomerSaveVO.getExtNum2());
						tmsCustomerConsignee.setExtNum3(intfCustomerSaveVO.getExtNum3());
						tmsCustomerConsignee.setExtNum4(intfCustomerSaveVO.getExtNum4());
						tmsCustomerConsignee.setExtNum5(intfCustomerSaveVO.getExtNum5());
						tmsCustomerConsignee.setUpdatetime(date);
						tmsCustomerConsigneeMapper.update(tmsCustomerConsignee);
					} else {
						// 不存在新增
						String codeengineId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
						ToolKits.isParamNull(codeengineId, "客户收货方ID生成失败");

						TmsCustomerConsignee tmsCustomerConsignee = new TmsCustomerConsignee();
						tmsCustomerConsignee.setId(Long.valueOf(codeengineId));
						tmsCustomerConsignee.setCode(tmsBaseCustomerModel.getCode());
						tmsCustomerConsignee.setName(tmsBaseCustomerModel.getName());
						tmsCustomerConsignee.setShortName(tmsBaseCustomerModel.getShortName());
						tmsCustomerConsignee.setCustomerId(tmsBaseCustomerModel.getId());
						tmsCustomerConsignee.setCustomerCode(tmsBaseCustomerModel.getCode());
						tmsCustomerConsignee.setCustomerName(tmsBaseCustomerModel.getName());
						tmsCustomerConsignee.setContact(tmsBaseCustomerModel.getContact());
						tmsCustomerConsignee.setPhoneNumber(tmsBaseCustomerModel.getPhoneNumber());
						tmsCustomerConsignee.setLandlineNumber(tmsBaseCustomerModel.getLandlineNumber());
						tmsCustomerConsignee.setEmail(tmsBaseCustomerModel.getEmail());
						tmsCustomerConsignee.setRelationPhoneNumber(null);
						tmsCustomerConsignee.setProvince(tmsBaseCustomerModel.getProvince());
						tmsCustomerConsignee.setProvinceName(tmsBaseCustomerModel.getProvinceName());
						tmsCustomerConsignee.setCity(tmsBaseCustomerModel.getCity());
						tmsCustomerConsignee.setCityName(tmsBaseCustomerModel.getCityName());
						tmsCustomerConsignee.setDistrict(tmsBaseCustomerModel.getDistrict());
						tmsCustomerConsignee.setDistrictName(tmsBaseCustomerModel.getDistrictName());
						tmsCustomerConsignee.setAddress(tmsBaseCustomerModel.getAddress());
						tmsCustomerConsignee.setLongitude(tmsBaseCustomerModel.getLongitude());
						tmsCustomerConsignee.setLatitude(tmsBaseCustomerModel.getLatitude());
						tmsCustomerConsignee.setStatus(tmsBaseCustomerModel.getStatus());
						tmsCustomerConsignee.setMemo(tmsBaseCustomerModel.getMemo());
						tmsCustomerConsignee.setExtStr1(intfCustomerSaveVO.getExtStr1());
						tmsCustomerConsignee.setExtStr2(intfCustomerSaveVO.getExtStr2());
						tmsCustomerConsignee.setExtStr3(intfCustomerSaveVO.getExtStr3());
						tmsCustomerConsignee.setExtStr4(intfCustomerSaveVO.getExtStr4());
						tmsCustomerConsignee.setExtStr5(intfCustomerSaveVO.getExtStr5());
						tmsCustomerConsignee.setExtDate1(intfCustomerSaveVO.getExtDate1());
						tmsCustomerConsignee.setExtDate2(intfCustomerSaveVO.getExtDate2());
						tmsCustomerConsignee.setExtDate3(intfCustomerSaveVO.getExtDate3());
						tmsCustomerConsignee.setExtDate4(intfCustomerSaveVO.getExtDate4());
						tmsCustomerConsignee.setExtDate5(intfCustomerSaveVO.getExtDate5());
						tmsCustomerConsignee.setExtNum1(intfCustomerSaveVO.getExtNum1());
						tmsCustomerConsignee.setExtNum2(intfCustomerSaveVO.getExtNum2());
						tmsCustomerConsignee.setExtNum3(intfCustomerSaveVO.getExtNum3());
						tmsCustomerConsignee.setExtNum4(intfCustomerSaveVO.getExtNum4());
						tmsCustomerConsignee.setExtNum5(intfCustomerSaveVO.getExtNum5());
						tmsCustomerConsignee.setEnterpriseId(intfCustomerSaveVO.getEnterpriseId());
						tmsCustomerConsignee.setEnterpriseName(intfCustomerSaveVO.getEnterpriseName());
						tmsCustomerConsignee.setSource(DataDictionaryConstant.SOURCE_INTERFACE);
						tmsCustomerConsignee.setCreatetime(date);
						tmsCustomerConsignee.setCreateperson(intfCustomerSaveVO.getCreateperson());
						tmsCustomerConsignee.setUpdateperson(intfCustomerSaveVO.getUpdateperson());
						tmsCustomerConsignee.setUpdatetime(date);
						tmsCustomerConsigneeMapper.save(tmsCustomerConsignee);
					}

				} else {
					String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
					Assert.isTrue(ToolKits.isNotEmpty(id), "客户ID生成为空");

					TmsBaseCustomerModel tmsBaseCustomerModel = new TmsBaseCustomerModel();
					BeanUtils.copyProperties(intfCustomerSaveVO, tmsBaseCustomerModel);
					tmsBaseCustomerModel.setId(Long.valueOf(id));
					tmsBaseCustomerModel.setCargoOwnerId(baseCargoOwnerModel.getCargoOwnerId());
					tmsBaseCustomerModel.setPickUpSign(ToolKits.isEmpty(intfCustomerSaveVO.getPickUpSign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getPickUpSign());
					tmsBaseCustomerModel.setAutoPickUpSign(ToolKits.isEmpty(intfCustomerSaveVO.getAutoPickUpSign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getAutoPickUpSign());
					tmsBaseCustomerModel.setPickUpPicture(ToolKits.isEmpty(intfCustomerSaveVO.getPickUpPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getPickUpPicture());
					tmsBaseCustomerModel.setDeliverySign(ToolKits.isEmpty(intfCustomerSaveVO.getDeliverySign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getDeliverySign());
					tmsBaseCustomerModel.setAutoDeliverySign(ToolKits.isEmpty(intfCustomerSaveVO.getAutoDeliverySign()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getAutoDeliverySign());
					tmsBaseCustomerModel.setDeliveryPicture(ToolKits.isEmpty(intfCustomerSaveVO.getDeliveryPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getDeliveryPicture());
					tmsBaseCustomerModel.setReturnOrderFlag(ToolKits.isEmpty(intfCustomerSaveVO.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getReturnOrderFlag());
					tmsBaseCustomerModel.setReturnOrderPicture(ToolKits.isEmpty(intfCustomerSaveVO.getReturnOrderPicture()) ? StatusConstant.WHETHER_NO : intfCustomerSaveVO.getReturnOrderPicture());
					tmsBaseCustomerModel.setStatus(StatusConstant.STATUS_ENABLE);
					tmsBaseCustomerModel.setSource(DataDictionaryConstant.SOURCE_INTERFACE);
					tmsBaseCustomerModel.setCreatetime(date);
					tmsBaseCustomerModel.setUpdatetime(date);
					if((ToolKits.isNotEmpty(intfCustomerSaveVO.getAddress()))
							&& ToolKits.isEmpty(intfCustomerSaveVO.getLongitude())
							&& ToolKits.isEmpty(intfCustomerSaveVO.getLatitude())){
						tmsBaseCustomerModel.setLongitude(longitude);
						tmsBaseCustomerModel.setLatitude(latitude);
					}else {
						tmsBaseCustomerModel.setLongitude(intfCustomerSaveVO.getLongitude());
						tmsBaseCustomerModel.setLatitude(intfCustomerSaveVO.getLatitude());
					}
					tmsBaseCustomerMapper.save(tmsBaseCustomerModel);

					// 维护客户收货方
					String codeengineId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
					ToolKits.isParamNull(codeengineId, "客户收货方ID生成失败");

					TmsCustomerConsignee tmsCustomerConsignee = new TmsCustomerConsignee();
					tmsCustomerConsignee.setId(Long.valueOf(codeengineId));
					tmsCustomerConsignee.setCode(tmsBaseCustomerModel.getCode());
					tmsCustomerConsignee.setName(tmsBaseCustomerModel.getName());
					tmsCustomerConsignee.setShortName(tmsBaseCustomerModel.getShortName());
					tmsCustomerConsignee.setCustomerId(tmsBaseCustomerModel.getId());
					tmsCustomerConsignee.setCustomerCode(tmsBaseCustomerModel.getCode());
					tmsCustomerConsignee.setCustomerName(tmsBaseCustomerModel.getName());
					tmsCustomerConsignee.setContact(tmsBaseCustomerModel.getContact());
					tmsCustomerConsignee.setPhoneNumber(tmsBaseCustomerModel.getPhoneNumber());
					tmsCustomerConsignee.setLandlineNumber(tmsBaseCustomerModel.getLandlineNumber());
					tmsCustomerConsignee.setEmail(tmsBaseCustomerModel.getEmail());
					tmsCustomerConsignee.setRelationPhoneNumber(null);
					tmsCustomerConsignee.setProvince(tmsBaseCustomerModel.getProvince());
					tmsCustomerConsignee.setProvinceName(tmsBaseCustomerModel.getProvinceName());
					tmsCustomerConsignee.setCity(tmsBaseCustomerModel.getCity());
					tmsCustomerConsignee.setCityName(tmsBaseCustomerModel.getCityName());
					tmsCustomerConsignee.setDistrict(tmsBaseCustomerModel.getDistrict());
					tmsCustomerConsignee.setDistrictName(tmsBaseCustomerModel.getDistrictName());
					tmsCustomerConsignee.setAddress(tmsBaseCustomerModel.getAddress());
					tmsCustomerConsignee.setLongitude(tmsBaseCustomerModel.getLongitude());
					tmsCustomerConsignee.setLatitude(tmsBaseCustomerModel.getLatitude());
					tmsCustomerConsignee.setStatus(tmsBaseCustomerModel.getStatus());
					tmsCustomerConsignee.setMemo(tmsBaseCustomerModel.getMemo());
					tmsCustomerConsignee.setExtStr1(intfCustomerSaveVO.getExtStr1());
					tmsCustomerConsignee.setExtStr2(intfCustomerSaveVO.getExtStr2());
					tmsCustomerConsignee.setExtStr3(intfCustomerSaveVO.getExtStr3());
					tmsCustomerConsignee.setExtStr4(intfCustomerSaveVO.getExtStr4());
					tmsCustomerConsignee.setExtStr5(intfCustomerSaveVO.getExtStr5());
					tmsCustomerConsignee.setExtDate1(intfCustomerSaveVO.getExtDate1());
					tmsCustomerConsignee.setExtDate2(intfCustomerSaveVO.getExtDate2());
					tmsCustomerConsignee.setExtDate3(intfCustomerSaveVO.getExtDate3());
					tmsCustomerConsignee.setExtDate4(intfCustomerSaveVO.getExtDate4());
					tmsCustomerConsignee.setExtDate5(intfCustomerSaveVO.getExtDate5());
					tmsCustomerConsignee.setExtNum1(intfCustomerSaveVO.getExtNum1());
					tmsCustomerConsignee.setExtNum2(intfCustomerSaveVO.getExtNum2());
					tmsCustomerConsignee.setExtNum3(intfCustomerSaveVO.getExtNum3());
					tmsCustomerConsignee.setExtNum4(intfCustomerSaveVO.getExtNum4());
					tmsCustomerConsignee.setExtNum5(intfCustomerSaveVO.getExtNum5());
					tmsCustomerConsignee.setEnterpriseId(intfCustomerSaveVO.getEnterpriseId());
					tmsCustomerConsignee.setEnterpriseName(intfCustomerSaveVO.getEnterpriseName());
					tmsCustomerConsignee.setSource(DataDictionaryConstant.SOURCE_INTERFACE);
					tmsCustomerConsignee.setCreatetime(date);
					tmsCustomerConsignee.setCreateperson(intfCustomerSaveVO.getCreateperson());
					tmsCustomerConsignee.setUpdateperson(intfCustomerSaveVO.getUpdateperson());
					tmsCustomerConsignee.setUpdatetime(date);
					tmsCustomerConsigneeMapper.save(tmsCustomerConsignee);
				}

				// 成功信息
				intfErrorLogDTO.setErrorInfo(null);
				intfErrorLogDTO.setDataId(intfCustomerSaveVO.getId());
				intfErrorLogDTO.setStatus(true);

				intfErrorLogDTOs.add(intfErrorLogDTO);
			} catch (Exception e) {
				e.printStackTrace();

				log.info("客户资料接口处理失败:{}", e);

				// 错误信息
				intfErrorLogDTO.setErrorInfo(e.getMessage());
				intfErrorLogDTO.setDataId(intfCustomerSaveVO.getId());
				intfErrorLogDTO.setStatus(false);

				intfErrorLogDTOs.add(intfErrorLogDTO);
			}
		}

		// 修改接口数据状态
		return intfErrorLogDTOs;
	}

	/**
	 * 客户收货方接口转TMS
	 */
	@Override
	public List<IntfErrorLogDTO> saveCustomerConsigneeInterfaceData(List<IntfCustomerConsigneeSaveVO> vos) {
		List<IntfErrorLogDTO> intfErrorLogDTOs = Lists.newArrayList();

		Date date = DateUtil.date();

		// 循环处理
		for (IntfCustomerConsigneeSaveVO vo : vos) {
			// 响应对象
			IntfErrorLogDTO intfErrorLogDTO = new IntfErrorLogDTO();
			try {
				// 货主校验
				Criteria criteria1 = Criteria.forClass(TmsBaseCargoOwnerModel.class);
				Restriction r11 = Restrictions.eq(TmsBaseCargoOwnerModel::getCargoOwnerCode, vo.getCargoOwnerCode());
				Restriction r12 = Restrictions.eq(TmsBaseCargoOwnerModel::getCargoOwnerName, vo.getCargoOwnerName());
				Restriction r13 = Restrictions.eq(TmsBaseCargoOwnerModel::getEnterpriseId, vo.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r12, r13));
				List<TmsBaseCargoOwnerModel> tmsBaseCargoOwnerModels = tmsBaseCargoOwnerMapper.findByCriteria(criteria1);
				Assert.isTrue(tmsBaseCargoOwnerModels.size() > 0, "货主【" + vo.getCargoOwnerName() + "】不存在");
				Assert.isTrue(tmsBaseCargoOwnerModels.size() <= 1, "货主【" + vo.getCargoOwnerName() + "】资料重复");

				// 货主对象
				TmsBaseCargoOwnerModel baseCargoOwnerModel = tmsBaseCargoOwnerModels.get(0);
				Assert.isTrue(baseCargoOwnerModel.getStatus().intValue() == StatusConstant.STATUS_ENABLE.intValue(), "货主【" + vo.getCargoOwnerName() + "】已停用");

				// 查询客户是否存在
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("code", vo.getCustomerCode());
				queryMap.put("enterpriseId", vo.getEnterpriseId());
				queryMap.put("cargoOwnerId", baseCargoOwnerModel.getCargoOwnerId());
				List<TmsBaseCustomerModel> customers = tmsBaseCustomerMapper.findByMap(queryMap, TmsBaseCustomerModel.class);
				Assert.isTrue(customers.size() > 0, "客户【" + vo.getCustomerName() + "】不存在");
				Assert.isTrue(customers.size() <= 1, "客户【" + vo.getCustomerName() + "】资料重复");

				// 客户对象
				TmsBaseCustomerModel baseCustomerModel = customers.get(0);
				Assert.isTrue(baseCustomerModel.getStatus().intValue() == StatusConstant.STATUS_ENABLE.intValue(), "客户【" + vo.getCustomerName() + "】已停用");

				// 查询客户收货方是否存在
				Map<String, Object> queryMap2 = Maps.newHashMap();
				queryMap2.put("customerId", baseCustomerModel.getId());
				queryMap2.put("code", vo.getCode());
				queryMap2.put("enterpriseId", vo.getEnterpriseId());
				List<TmsCustomerConsignee> customerConsignees = tmsCustomerConsigneeMapper.findByMap(queryMap2, TmsCustomerConsignee.class);

				TmsCustomerConsignee tmsCustomerConsignee = null;
				// 客户收货方不存在,执行新增
				if (ObjectUtil.isEmpty(customerConsignees) || customerConsignees.size() == 0) {
					tmsCustomerConsignee = new TmsCustomerConsignee();
					BeanUtils.copyProperties(vo, tmsCustomerConsignee);
					// 校验省市区
					if (ToolKits.isNotEmpty(vo.getProvinceName())) {
						List<LmsTmsZoneModel> lmsTmsZoneModels = lmsTmsZoneMapper.getZoneListByZname(vo.getProvinceName(), "2", null);
						if (ToolKits.isEmpty(lmsTmsZoneModels)) {
							throw new RuntimeException("省名称[" + vo.getProvinceName() + "]错误");
						}
						tmsCustomerConsignee.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() + "]错误");
						}
						tmsCustomerConsignee.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() + "]错误");
						}
						tmsCustomerConsignee.setDistrict(lmsTmsZoneModels.get(0).getZoneid());
					}

					// 客户收货方ID
					String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
					ToolKits.isParamNull(id, "客户收货方ID生成失败");
					tmsCustomerConsignee.setId(Long.valueOf(id));
					tmsCustomerConsignee.setCustomerId(baseCustomerModel.getId());
					tmsCustomerConsignee.setEnterpriseId(vo.getEnterpriseId());
					tmsCustomerConsignee.setEnterpriseName(vo.getEnterpriseName());
					tmsCustomerConsignee.setSource(DataDictionaryConstant.SOURCE_INTERFACE);
					tmsCustomerConsignee.setCreatetime(date);
					tmsCustomerConsignee.setUpdatetime(date);
					tmsCustomerConsigneeMapper.save(tmsCustomerConsignee);
				} else {// 客户收货方存在,执行更新
					tmsCustomerConsignee = customerConsignees.get(0);
					BeanUtils.copyProperties(vo, tmsCustomerConsignee);

					// 校验省市区
					if (ToolKits.isNotEmpty(vo.getProvinceName())) {
						List<LmsTmsZoneModel> lmsTmsZoneModels = lmsTmsZoneMapper.getZoneListByZname(vo.getProvinceName(), "2", null);
						if (ToolKits.isEmpty(lmsTmsZoneModels)) {
							throw new RuntimeException("省名称[" + vo.getProvinceName() + "]错误");
						}
						tmsCustomerConsignee.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() + "]错误");
						}
						tmsCustomerConsignee.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() + "]错误");
						}
						tmsCustomerConsignee.setDistrict(lmsTmsZoneModels.get(0).getZoneid());
					}

					tmsCustomerConsignee.setCustomerId(baseCustomerModel.getId());
					tmsCustomerConsignee.setUpdatetime(date);
					tmsCustomerConsigneeMapper.update(tmsCustomerConsignee);
				}

				// 成功信息
				intfErrorLogDTO.setErrorInfo(null);
				intfErrorLogDTO.setDataId(vo.getId());
				intfErrorLogDTO.setStatus(true);
				intfErrorLogDTOs.add(intfErrorLogDTO);
			} catch (Exception e) {
				e.printStackTrace();

				log.info("客户收货方资料接口处理失败:{}", e);

				// 错误信息
				intfErrorLogDTO.setErrorInfo(e.getMessage());
				intfErrorLogDTO.setDataId(vo.getId());
				intfErrorLogDTO.setStatus(false);
				intfErrorLogDTOs.add(intfErrorLogDTO);
			}
		}

		return intfErrorLogDTOs;
	}

	/**
	 * 运单导入时匹配客户资料
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ImportMatchingDataDTO importMatchingData(ImportMatchingDataVO importMatchingDataVO) {
		ToolKits.isParamNull(importMatchingDataVO, "请求参数不为空");
		ToolKits.isParamNull(importMatchingDataVO.getCargoOwnerId(), "货主ID不为空");

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

		// 查询客户
		Criteria criteria = Criteria.forClass(TmsBaseCustomerModel.class);
		Restriction r1 = ToolKits.isEmpty(importMatchingDataVO.getUnitCode()) ? null : Restrictions.eq(TmsBaseCustomerModel::getCode, importMatchingDataVO.getUnitCode());
		Restriction r2 = Restrictions.eq(TmsBaseCustomerModel::getName, importMatchingDataVO.getUnitName());
		Restriction r3 = Restrictions.eq(TmsBaseCustomerModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r4 = Restrictions.eq(TmsBaseCustomerModel::getCargoOwnerId, importMatchingDataVO.getCargoOwnerId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsBaseCustomerModel> baseCustomerModels = tmsBaseCustomerMapper.findByCriteria(criteria);
		if (baseCustomerModels.size() > 0) {
			Assert.isTrue(baseCustomerModels.size() <= 1, "客户资料【" + importMatchingDataVO.getUnitName() + "】维护重复");

			// 客户对象
			TmsBaseCustomerModel baseCustomerModel = baseCustomerModels.get(0);
			Assert.isTrue(baseCustomerModel.getCargoOwnerId().intValue() == importMatchingDataVO.getCargoOwnerId().intValue(), "客户资料【" + importMatchingDataVO.getUnitName() + "】不属于当前货主");

			// 查询客户收货方表
			Criteria criteria1 = Criteria.forClass(TmsCustomerConsignee.class);
			Restriction r11 = Restrictions.eq(TmsCustomerConsignee::getCustomerId, baseCustomerModel.getId());
			Restriction r12 = ToolKits.isEmpty(importMatchingDataVO.getCode()) ? null : Restrictions.eq(TmsCustomerConsignee::getCode, importMatchingDataVO.getCode());
			Restriction r13 = Restrictions.eq(TmsCustomerConsignee::getName, importMatchingDataVO.getName());
			Restriction r14 = Restrictions.eq(TmsCustomerConsignee::getEnterpriseId, user.getEnterpriseId());
			criteria1.setRestriction(Restrictions.and(r11, r12, r13, r14));
			List<TmsCustomerConsignee> customerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria1);

			// 客户收货方
			TmsCustomerConsignee tmsCustomerConsignee = null;
			if (customerConsignees.size() > 0) {
				Assert.isTrue(customerConsignees.size() <= 1, "客户关联资料【" + importMatchingDataVO.getName() + "】维护重复");

				tmsCustomerConsignee = customerConsignees.get(0);
			} else {
				Assert.isTrue(importMatchingDataVO.isNoSave(), "客户收货方资料【" + importMatchingDataVO.getName() + "】未维护");

				// 不存在新增
				String codeengineId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
				ToolKits.isParamNull(codeengineId, "客户收货方ID生成失败");

				tmsCustomerConsignee = new TmsCustomerConsignee();
				tmsCustomerConsignee.setId(Long.valueOf(codeengineId));
				if (ToolKits.isNotEmpty(importMatchingDataVO.getShipperName())) {
					// 客户收货方
					tmsCustomerConsignee.setCode(ToolKits.isEmpty(importMatchingDataVO.getShipperCode()) ? "KH-" + codeengineId : importMatchingDataVO.getShipperCode());
					tmsCustomerConsignee.setName(importMatchingDataVO.getShipperName());
					tmsCustomerConsignee.setShortName(importMatchingDataVO.getShipperName());
					tmsCustomerConsignee.setContact(importMatchingDataVO.getShipperContact());
					tmsCustomerConsignee.setPhoneNumber(importMatchingDataVO.getShipperPhoneNumber());
					tmsCustomerConsignee.setLandlineNumber(importMatchingDataVO.getShipperLandlineNumber());
					tmsCustomerConsignee.setEmail(importMatchingDataVO.getShipperEmail());
					tmsCustomerConsignee.setRelationPhoneNumber(null);
					tmsCustomerConsignee.setProvince(importMatchingDataVO.getShipperProvince());
					tmsCustomerConsignee.setProvinceName(importMatchingDataVO.getShipperProvinceName());
					tmsCustomerConsignee.setCity(importMatchingDataVO.getShipperCity());
					tmsCustomerConsignee.setCityName(importMatchingDataVO.getShipperCityName());
					tmsCustomerConsignee.setDistrict(importMatchingDataVO.getShipperDistrict());
					tmsCustomerConsignee.setDistrictName(importMatchingDataVO.getShipperDistrictName());
					tmsCustomerConsignee.setAddress(importMatchingDataVO.getShipperAddress());
					tmsCustomerConsignee.setLongitude(importMatchingDataVO.getShipperLongitude());
					tmsCustomerConsignee.setLatitude(importMatchingDataVO.getShipperLatitude());
					tmsCustomerConsignee.setMemo(importMatchingDataVO.getShipperMemo());
				} else {
					// 客户收货方
					tmsCustomerConsignee.setCode(ToolKits.isEmpty(importMatchingDataVO.getConsigneeCode()) ? "KH-" + codeengineId : importMatchingDataVO.getConsigneeCode());
					tmsCustomerConsignee.setName(importMatchingDataVO.getConsigneeName());
					tmsCustomerConsignee.setShortName(importMatchingDataVO.getConsigneeName());
					tmsCustomerConsignee.setContact(importMatchingDataVO.getConsigneeContact());
					tmsCustomerConsignee.setPhoneNumber(importMatchingDataVO.getConsigneePhoneNumber());
					tmsCustomerConsignee.setLandlineNumber(importMatchingDataVO.getConsigneeLandlineNumber());
					tmsCustomerConsignee.setEmail(importMatchingDataVO.getConsigneeEmail());
					tmsCustomerConsignee.setRelationPhoneNumber(null);
					tmsCustomerConsignee.setProvince(importMatchingDataVO.getConsigneeProvince());
					tmsCustomerConsignee.setProvinceName(importMatchingDataVO.getConsigneeProvinceName());
					tmsCustomerConsignee.setCity(importMatchingDataVO.getConsigneeCity());
					tmsCustomerConsignee.setCityName(importMatchingDataVO.getConsigneeCityName());
					tmsCustomerConsignee.setDistrict(importMatchingDataVO.getConsigneeDistrict());
					tmsCustomerConsignee.setDistrictName(importMatchingDataVO.getConsigneeDistrictName());
					tmsCustomerConsignee.setAddress(importMatchingDataVO.getConsigneeAddress());
					tmsCustomerConsignee.setLongitude(importMatchingDataVO.getConsigneeLongitude());
					tmsCustomerConsignee.setLatitude(importMatchingDataVO.getConsigneeLatitude());
					tmsCustomerConsignee.setMemo(importMatchingDataVO.getConsigneeMemo());
				}

				tmsCustomerConsignee.setCustomerId(baseCustomerModel.getId());
				tmsCustomerConsignee.setCustomerCode(baseCustomerModel.getCode());
				tmsCustomerConsignee.setCustomerName(baseCustomerModel.getName());
				tmsCustomerConsignee.setStatus(StatusConstant.STATUS_ENABLE);
				tmsCustomerConsignee.setEnterpriseId(user.getEnterpriseId());
				tmsCustomerConsignee.setEnterpriseName(user.getEnterpriseName());
				tmsCustomerConsignee.setSource(DataDictionaryConstant.SOURCE_EXCEL);
				tmsCustomerConsignee.setCreatetime(DateUtil.date());
				tmsCustomerConsignee.setCreateperson(user.getNickname());
				tmsCustomerConsignee.setUpdateperson(user.getNickname());
				tmsCustomerConsignee.setUpdatetime(DateUtil.date());
				tmsCustomerConsigneeMapper.save(tmsCustomerConsignee);

			}

			ImportMatchingDataDTO importMatchingDataDTO = new ImportMatchingDataDTO();
			importMatchingDataDTO.setShipperUnitId(baseCustomerModel.getId());
			importMatchingDataDTO.setShipperUnitCode(baseCustomerModel.getCode());
			importMatchingDataDTO.setShipperUnitName(baseCustomerModel.getName());
			importMatchingDataDTO.setShipperId(tmsCustomerConsignee.getId());
			importMatchingDataDTO.setShipperCode(tmsCustomerConsignee.getCode());
			importMatchingDataDTO.setShipperName(tmsCustomerConsignee.getName());
			importMatchingDataDTO.setShipperContact(tmsCustomerConsignee.getContact());
			importMatchingDataDTO.setShipperPhoneNumber(tmsCustomerConsignee.getPhoneNumber());
			importMatchingDataDTO.setShipperLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
			importMatchingDataDTO.setShipperEmail(tmsCustomerConsignee.getEmail());
			importMatchingDataDTO.setShipperRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
			importMatchingDataDTO.setShipperProvince(tmsCustomerConsignee.getProvince());
			importMatchingDataDTO.setShipperProvinceName(tmsCustomerConsignee.getProvinceName());
			importMatchingDataDTO.setShipperCity(tmsCustomerConsignee.getCity());
			importMatchingDataDTO.setShipperCityName(tmsCustomerConsignee.getCityName());
			importMatchingDataDTO.setShipperDistrict(tmsCustomerConsignee.getDistrict());
			importMatchingDataDTO.setShipperDistrictName(tmsCustomerConsignee.getDistrictName());
			importMatchingDataDTO.setShipperAddress(tmsCustomerConsignee.getAddress());
			importMatchingDataDTO.setShipperLongitude(tmsCustomerConsignee.getLongitude());
			importMatchingDataDTO.setShipperLatitude(tmsCustomerConsignee.getLatitude());
			importMatchingDataDTO.setShipperMemo(tmsCustomerConsignee.getMemo());
			importMatchingDataDTO.setShipperPickUpSign(baseCustomerModel.getPickUpSign());
			importMatchingDataDTO.setShipperAutoPickUpSign(baseCustomerModel.getAutoPickUpSign());
			importMatchingDataDTO.setShipperPickUpPicture(baseCustomerModel.getPickUpPicture());
			importMatchingDataDTO.setShipperPickUpRail(baseCustomerModel.getDefaultPickUpRail());

			importMatchingDataDTO.setConsigneeUnitId(baseCustomerModel.getId());
			importMatchingDataDTO.setConsigneeUnitCode(baseCustomerModel.getCode());
			importMatchingDataDTO.setConsigneeUnitName(baseCustomerModel.getName());
			importMatchingDataDTO.setConsigneeId(tmsCustomerConsignee.getId());
			importMatchingDataDTO.setConsigneeCode(tmsCustomerConsignee.getCode());
			importMatchingDataDTO.setConsigneeName(tmsCustomerConsignee.getName());
			importMatchingDataDTO.setConsigneeContact(tmsCustomerConsignee.getContact());
			importMatchingDataDTO.setConsigneePhoneNumber(tmsCustomerConsignee.getPhoneNumber());
			importMatchingDataDTO.setConsigneeLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
			importMatchingDataDTO.setConsigneeEmail(tmsCustomerConsignee.getEmail());
			importMatchingDataDTO.setConsigneeRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
			importMatchingDataDTO.setConsigneeProvince(tmsCustomerConsignee.getProvince());
			importMatchingDataDTO.setConsigneeProvinceName(tmsCustomerConsignee.getProvinceName());
			importMatchingDataDTO.setConsigneeCity(tmsCustomerConsignee.getCity());
			importMatchingDataDTO.setConsigneeCityName(tmsCustomerConsignee.getCityName());
			importMatchingDataDTO.setConsigneeDistrict(tmsCustomerConsignee.getDistrict());
			importMatchingDataDTO.setConsigneeDistrictName(tmsCustomerConsignee.getDistrictName());
			importMatchingDataDTO.setConsigneeAddress(tmsCustomerConsignee.getAddress());
			importMatchingDataDTO.setConsigneeLongitude(tmsCustomerConsignee.getLongitude());
			importMatchingDataDTO.setConsigneeLatitude(tmsCustomerConsignee.getLatitude());
			importMatchingDataDTO.setConsigneeMemo(tmsCustomerConsignee.getMemo());
			importMatchingDataDTO.setConsigneeDeliverySign(baseCustomerModel.getDeliverySign());
			importMatchingDataDTO.setConsigneeAutoDeliverySign(baseCustomerModel.getAutoDeliverySign());
			importMatchingDataDTO.setConsigneeDeliveryPicture(baseCustomerModel.getDeliveryPicture());
			importMatchingDataDTO.setConsigneeDeliveryRail(baseCustomerModel.getDefaultDeliveryRail());
			importMatchingDataDTO.setReturnOrderFlag(baseCustomerModel.getReturnOrderFlag());
			importMatchingDataDTO.setReturnOrderDeadline(baseCustomerModel.getReturnOrderDeadline());
			importMatchingDataDTO.setConsigneeReturnOrderPicture(baseCustomerModel.getReturnOrderPicture());

			importMatchingDataDTO.setShipperSignTime(baseCustomerModel.getPickUpSignTime());
			importMatchingDataDTO.setShipperSignTimeDura(baseCustomerModel.getPickUpSignTimeDura());
			importMatchingDataDTO.setShipperSignTimeUnit(baseCustomerModel.getPickUpSignTimeUnit());

			importMatchingDataDTO.setConsigneeSignTime(baseCustomerModel.getDeliverySignTime());
			importMatchingDataDTO.setConsigneeSignTimeDura(baseCustomerModel.getDeliverySignTimeDura());
			importMatchingDataDTO.setConsigneeSignTimeUnit(baseCustomerModel.getDeliverySignTimeUnit());

			importMatchingDataDTO.setStationId(tmsCustomerConsignee.getStationId());
			importMatchingDataDTO.setStationCode(tmsCustomerConsignee.getStationCode());
			importMatchingDataDTO.setStationName(tmsCustomerConsignee.getStationName());
			importMatchingDataDTO.setStationAreas(tmsCustomerConsignee.getStationAreas());
			importMatchingDataDTO.setStationDeliveryOrder(tmsCustomerConsignee.getStationDeliveryOrder());

			return importMatchingDataDTO;
		} else {
			Assert.isTrue(importMatchingDataVO.isNoSave(), "客户资料【" + importMatchingDataVO.getUnitName() + "】未维护");

			String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
			if (ToolKits.isEmpty(id)) {
				throw new BizException("客户id获取失败");
			}

			TmsBaseCustomerModel baseCustomerModel = new TmsBaseCustomerModel();
			baseCustomerModel.setId(Long.valueOf(id));
			if (ToolKits.isNotEmpty(importMatchingDataVO.getShipperUnitName())) {
				baseCustomerModel.setCode(ToolKits.isEmpty(importMatchingDataVO.getShipperUnitCode()) ? "KH-" + id : importMatchingDataVO.getShipperUnitCode());
				baseCustomerModel.setName(importMatchingDataVO.getShipperUnitName());
				baseCustomerModel.setShortName(importMatchingDataVO.getShipperUnitName());
				baseCustomerModel.setCargoOwnerId(importMatchingDataVO.getCargoOwnerId());
				baseCustomerModel.setCargoOwnerCode(importMatchingDataVO.getCargoOwnerCode());
				baseCustomerModel.setCargoOwnerName(importMatchingDataVO.getCargoOwnerName());
				baseCustomerModel.setProvince(importMatchingDataVO.getShipperProvince());
				baseCustomerModel.setProvinceName(importMatchingDataVO.getShipperProvinceName());
				baseCustomerModel.setCity(importMatchingDataVO.getShipperCity());
				baseCustomerModel.setCityName(importMatchingDataVO.getShipperCityName());
				baseCustomerModel.setDistrict(importMatchingDataVO.getShipperDistrict());
				baseCustomerModel.setDistrictName(importMatchingDataVO.getShipperDistrictName());
				baseCustomerModel.setAddress(importMatchingDataVO.getShipperAddress());
				baseCustomerModel.setContact(importMatchingDataVO.getShipperContact());
				baseCustomerModel.setPhoneNumber(importMatchingDataVO.getShipperPhoneNumber());
				baseCustomerModel.setLandlineNumber(importMatchingDataVO.getShipperLandlineNumber());
				baseCustomerModel.setEmail(importMatchingDataVO.getShipperEmail());
				baseCustomerModel.setPostCode(null);
				baseCustomerModel.setLongitude(importMatchingDataVO.getShipperLongitude());
				baseCustomerModel.setLatitude(importMatchingDataVO.getShipperLatitude());
				baseCustomerModel.setCooperativeNature(DataDictionaryConstant.COOPERATIVE_NATURE_LS);
				baseCustomerModel.setMemo(importMatchingDataVO.getShipperMemo());

				baseCustomerModel.setPickUpSign(ToolKits.isEmpty(importMatchingDataVO.getShipperPickUpSign()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getShipperPickUpSign());
				baseCustomerModel.setAutoPickUpSign(ToolKits.isEmpty(importMatchingDataVO.getShipperAutoPickUpSign()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getShipperAutoPickUpSign());
				baseCustomerModel.setPickUpPicture(ToolKits.isEmpty(importMatchingDataVO.getShipperPickUpPicture()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getShipperPickUpPicture());
				baseCustomerModel.setDefaultPickUpRail(importMatchingDataVO.getShipperPickUpRail());
				baseCustomerModel.setPickUpSignTime(importMatchingDataVO.getShipperSignTime());
				baseCustomerModel.setPickUpSignTimeDura(importMatchingDataVO.getShipperSignTimeDura());
				baseCustomerModel.setPickUpSignTimeUnit(importMatchingDataVO.getShipperSignTimeUnit());

				baseCustomerModel.setDeliverySign(StatusConstant.WHETHER_NO);
				baseCustomerModel.setAutoDeliverySign(StatusConstant.WHETHER_NO);
				baseCustomerModel.setDeliveryPicture(StatusConstant.WHETHER_NO);
				baseCustomerModel.setReturnOrderPicture(StatusConstant.WHETHER_NO);
				baseCustomerModel.setDefaultDeliveryRail(null);
				baseCustomerModel.setReturnOrderFlag(StatusConstant.WHETHER_NO);
				baseCustomerModel.setReturnOrderDeadline(null);
				baseCustomerModel.setDeliverySignTime(null);
				baseCustomerModel.setDeliverySignTimeDura(null);
				baseCustomerModel.setDeliverySignTimeUnit(null);
			} else {
				baseCustomerModel.setCode(ToolKits.isEmpty(importMatchingDataVO.getConsigneeUnitCode()) ? "KH-" + id : importMatchingDataVO.getConsigneeUnitCode());
				baseCustomerModel.setName(importMatchingDataVO.getConsigneeUnitName());
				baseCustomerModel.setShortName(importMatchingDataVO.getConsigneeUnitName());
				baseCustomerModel.setCargoOwnerId(importMatchingDataVO.getCargoOwnerId());
				baseCustomerModel.setCargoOwnerCode(importMatchingDataVO.getCargoOwnerCode());
				baseCustomerModel.setCargoOwnerName(importMatchingDataVO.getCargoOwnerName());
				baseCustomerModel.setProvince(importMatchingDataVO.getConsigneeProvince());
				baseCustomerModel.setProvinceName(importMatchingDataVO.getConsigneeProvinceName());
				baseCustomerModel.setCity(importMatchingDataVO.getConsigneeCity());
				baseCustomerModel.setCityName(importMatchingDataVO.getConsigneeCityName());
				baseCustomerModel.setDistrict(importMatchingDataVO.getConsigneeDistrict());
				baseCustomerModel.setDistrictName(importMatchingDataVO.getConsigneeDistrictName());
				baseCustomerModel.setAddress(importMatchingDataVO.getConsigneeAddress());
				baseCustomerModel.setContact(importMatchingDataVO.getConsigneeContact());
				baseCustomerModel.setPhoneNumber(importMatchingDataVO.getConsigneePhoneNumber());
				baseCustomerModel.setLandlineNumber(importMatchingDataVO.getConsigneeLandlineNumber());
				baseCustomerModel.setEmail(importMatchingDataVO.getConsigneeEmail());
				baseCustomerModel.setPostCode(null);
				baseCustomerModel.setLongitude(importMatchingDataVO.getConsigneeLongitude());
				baseCustomerModel.setLatitude(importMatchingDataVO.getConsigneeLatitude());
				baseCustomerModel.setCooperativeNature(DataDictionaryConstant.COOPERATIVE_NATURE_LS);
				baseCustomerModel.setMemo(importMatchingDataVO.getConsigneeMemo());

				baseCustomerModel.setPickUpSign(StatusConstant.WHETHER_NO);
				baseCustomerModel.setAutoPickUpSign(StatusConstant.WHETHER_NO);
				baseCustomerModel.setPickUpPicture(StatusConstant.WHETHER_NO);
				baseCustomerModel.setDefaultPickUpRail(null);
				baseCustomerModel.setPickUpSignTime(null);
				baseCustomerModel.setPickUpSignTimeDura(null);
				baseCustomerModel.setPickUpSignTimeUnit(null);

				baseCustomerModel.setDeliverySign(ToolKits.isEmpty(importMatchingDataVO.getConsigneeDeliverySign()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getConsigneeDeliverySign());
				baseCustomerModel.setAutoDeliverySign(ToolKits.isEmpty(importMatchingDataVO.getConsigneeAutoDeliverySign()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getConsigneeAutoDeliverySign());
				baseCustomerModel.setDeliveryPicture(ToolKits.isEmpty(importMatchingDataVO.getConsigneeDeliveryPicture()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getConsigneeDeliveryPicture());
				baseCustomerModel.setReturnOrderPicture(ToolKits.isEmpty(importMatchingDataVO.getConsigneeReturnOrderPicture()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getConsigneeReturnOrderPicture());
				baseCustomerModel.setDefaultDeliveryRail(importMatchingDataVO.getConsigneeDeliveryRail());
				baseCustomerModel.setReturnOrderFlag(ToolKits.isEmpty(importMatchingDataVO.getReturnOrderFlag()) ? StatusConstant.WHETHER_NO : importMatchingDataVO.getReturnOrderFlag());
				baseCustomerModel.setReturnOrderDeadline(importMatchingDataVO.getReturnOrderDeadline());
				baseCustomerModel.setDeliverySignTime(importMatchingDataVO.getConsigneeSignTime());
				baseCustomerModel.setDeliverySignTimeDura(importMatchingDataVO.getConsigneeSignTimeDura());
				baseCustomerModel.setDeliverySignTimeUnit(importMatchingDataVO.getConsigneeSignTimeUnit());
			}

			baseCustomerModel.setStatus(StatusConstant.STATUS_ENABLE);
			baseCustomerModel.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			baseCustomerModel.setDepartmentId(importMatchingDataVO.getDepartmentId());
			baseCustomerModel.setDepartmentCode(importMatchingDataVO.getDepartmentCode());
			baseCustomerModel.setDepartmentName(importMatchingDataVO.getDepartmentName());
			baseCustomerModel.setEnterpriseId(user.getEnterpriseId());
			baseCustomerModel.setEnterpriseName(user.getEnterpriseName());
			baseCustomerModel.setCreatetime(DateUtil.date());
			baseCustomerModel.setCreateperson(user.getNickname());
			baseCustomerModel.setUpdatetime(DateUtil.date());
			baseCustomerModel.setUpdateperson(user.getNickname());
			tmsBaseCustomerMapper.save(baseCustomerModel);

			// 维护客户收货方
			String codeengineId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
			ToolKits.isParamNull(codeengineId, "客户收货方ID生成失败");

			// 客户收货方
			TmsCustomerConsignee tmsCustomerConsignee = new TmsCustomerConsignee();
			tmsCustomerConsignee.setId(Long.valueOf(codeengineId));
			tmsCustomerConsignee.setCode(ToolKits.isEmpty(importMatchingDataVO.getCode()) ? "KH-" + codeengineId : importMatchingDataVO.getCode());
			tmsCustomerConsignee.setName(importMatchingDataVO.getName());
			tmsCustomerConsignee.setShortName(importMatchingDataVO.getName());
			tmsCustomerConsignee.setCustomerId(baseCustomerModel.getId());
			tmsCustomerConsignee.setCustomerCode(baseCustomerModel.getCode());
			tmsCustomerConsignee.setCustomerName(baseCustomerModel.getName());
			tmsCustomerConsignee.setContact(baseCustomerModel.getContact());
			tmsCustomerConsignee.setPhoneNumber(baseCustomerModel.getPhoneNumber());
			tmsCustomerConsignee.setLandlineNumber(baseCustomerModel.getLandlineNumber());
			tmsCustomerConsignee.setEmail(baseCustomerModel.getEmail());
			tmsCustomerConsignee.setRelationPhoneNumber(null);
			tmsCustomerConsignee.setProvince(baseCustomerModel.getProvince());
			tmsCustomerConsignee.setProvinceName(baseCustomerModel.getProvinceName());
			tmsCustomerConsignee.setCity(baseCustomerModel.getCity());
			tmsCustomerConsignee.setCityName(baseCustomerModel.getCityName());
			tmsCustomerConsignee.setDistrict(baseCustomerModel.getDistrict());
			tmsCustomerConsignee.setDistrictName(baseCustomerModel.getDistrictName());
			tmsCustomerConsignee.setAddress(baseCustomerModel.getAddress());
			tmsCustomerConsignee.setLongitude(baseCustomerModel.getLongitude());
			tmsCustomerConsignee.setLatitude(baseCustomerModel.getLatitude());
			tmsCustomerConsignee.setStatus(baseCustomerModel.getStatus());
			tmsCustomerConsignee.setMemo(baseCustomerModel.getMemo());
			tmsCustomerConsignee.setEnterpriseId(baseCustomerModel.getEnterpriseId());
			tmsCustomerConsignee.setEnterpriseName(baseCustomerModel.getEnterpriseName());
			tmsCustomerConsignee.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			tmsCustomerConsignee.setCreatetime(DateUtil.date());
			tmsCustomerConsignee.setCreateperson(user.getNickname());
			tmsCustomerConsignee.setUpdateperson(user.getNickname());
			tmsCustomerConsignee.setUpdatetime(DateUtil.date());
			tmsCustomerConsigneeMapper.save(tmsCustomerConsignee);

			// 响应
			ImportMatchingDataDTO importMatchingDataDTO = new ImportMatchingDataDTO();
			importMatchingDataDTO.setShipperUnitId(baseCustomerModel.getId());
			importMatchingDataDTO.setShipperUnitCode(baseCustomerModel.getCode());
			importMatchingDataDTO.setShipperUnitName(baseCustomerModel.getName());
			importMatchingDataDTO.setShipperId(tmsCustomerConsignee.getId());
			importMatchingDataDTO.setShipperCode(tmsCustomerConsignee.getCode());
			importMatchingDataDTO.setShipperName(tmsCustomerConsignee.getName());
			importMatchingDataDTO.setShipperContact(tmsCustomerConsignee.getContact());
			importMatchingDataDTO.setShipperPhoneNumber(tmsCustomerConsignee.getPhoneNumber());
			importMatchingDataDTO.setShipperLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
			importMatchingDataDTO.setShipperEmail(tmsCustomerConsignee.getEmail());
			importMatchingDataDTO.setShipperRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
			importMatchingDataDTO.setShipperProvince(tmsCustomerConsignee.getProvince());
			importMatchingDataDTO.setShipperProvinceName(tmsCustomerConsignee.getProvinceName());
			importMatchingDataDTO.setShipperCity(tmsCustomerConsignee.getCity());
			importMatchingDataDTO.setShipperCityName(tmsCustomerConsignee.getCityName());
			importMatchingDataDTO.setShipperDistrict(tmsCustomerConsignee.getDistrict());
			importMatchingDataDTO.setShipperDistrictName(tmsCustomerConsignee.getDistrictName());
			importMatchingDataDTO.setShipperAddress(tmsCustomerConsignee.getAddress());
			importMatchingDataDTO.setShipperLongitude(tmsCustomerConsignee.getLongitude());
			importMatchingDataDTO.setShipperLatitude(tmsCustomerConsignee.getLatitude());
			importMatchingDataDTO.setShipperMemo(tmsCustomerConsignee.getMemo());
			importMatchingDataDTO.setShipperPickUpSign(baseCustomerModel.getPickUpSign());
			importMatchingDataDTO.setShipperAutoPickUpSign(baseCustomerModel.getAutoPickUpSign());
			importMatchingDataDTO.setShipperPickUpPicture(baseCustomerModel.getPickUpPicture());
			importMatchingDataDTO.setShipperPickUpRail(baseCustomerModel.getDefaultPickUpRail());

			importMatchingDataDTO.setConsigneeUnitId(baseCustomerModel.getId());
			importMatchingDataDTO.setConsigneeUnitCode(baseCustomerModel.getCode());
			importMatchingDataDTO.setConsigneeUnitName(baseCustomerModel.getName());
			importMatchingDataDTO.setConsigneeId(tmsCustomerConsignee.getId());
			importMatchingDataDTO.setConsigneeCode(tmsCustomerConsignee.getCode());
			importMatchingDataDTO.setConsigneeName(tmsCustomerConsignee.getName());
			importMatchingDataDTO.setConsigneeContact(tmsCustomerConsignee.getContact());
			importMatchingDataDTO.setConsigneePhoneNumber(tmsCustomerConsignee.getPhoneNumber());
			importMatchingDataDTO.setConsigneeLandlineNumber(tmsCustomerConsignee.getLandlineNumber());
			importMatchingDataDTO.setConsigneeEmail(tmsCustomerConsignee.getEmail());
			importMatchingDataDTO.setConsigneeRelatedPhoneNumber(tmsCustomerConsignee.getRelationPhoneNumber());
			importMatchingDataDTO.setConsigneeProvince(tmsCustomerConsignee.getProvince());
			importMatchingDataDTO.setConsigneeProvinceName(tmsCustomerConsignee.getProvinceName());
			importMatchingDataDTO.setConsigneeCity(tmsCustomerConsignee.getCity());
			importMatchingDataDTO.setConsigneeCityName(tmsCustomerConsignee.getCityName());
			importMatchingDataDTO.setConsigneeDistrict(tmsCustomerConsignee.getDistrict());
			importMatchingDataDTO.setConsigneeDistrictName(tmsCustomerConsignee.getDistrictName());
			importMatchingDataDTO.setConsigneeAddress(tmsCustomerConsignee.getAddress());
			importMatchingDataDTO.setConsigneeLongitude(tmsCustomerConsignee.getLongitude());
			importMatchingDataDTO.setConsigneeLatitude(tmsCustomerConsignee.getLatitude());
			importMatchingDataDTO.setConsigneeMemo(tmsCustomerConsignee.getMemo());
			importMatchingDataDTO.setConsigneeDeliverySign(baseCustomerModel.getDeliverySign());
			importMatchingDataDTO.setConsigneeAutoDeliverySign(baseCustomerModel.getAutoDeliverySign());
			importMatchingDataDTO.setConsigneeDeliveryPicture(baseCustomerModel.getDeliveryPicture());
			importMatchingDataDTO.setConsigneeDeliveryRail(baseCustomerModel.getDefaultDeliveryRail());
			importMatchingDataDTO.setReturnOrderFlag(baseCustomerModel.getReturnOrderFlag());
			importMatchingDataDTO.setReturnOrderDeadline(baseCustomerModel.getReturnOrderDeadline());
			importMatchingDataDTO.setConsigneeReturnOrderPicture(baseCustomerModel.getReturnOrderPicture());

			importMatchingDataDTO.setShipperSignTime(baseCustomerModel.getPickUpSignTime());
			importMatchingDataDTO.setShipperSignTimeDura(baseCustomerModel.getPickUpSignTimeDura());
			importMatchingDataDTO.setShipperSignTimeUnit(baseCustomerModel.getPickUpSignTimeUnit());

			importMatchingDataDTO.setConsigneeSignTime(baseCustomerModel.getDeliverySignTime());
			importMatchingDataDTO.setConsigneeSignTimeDura(baseCustomerModel.getDeliverySignTimeDura());
			importMatchingDataDTO.setConsigneeSignTimeUnit(baseCustomerModel.getDeliverySignTimeUnit());

			importMatchingDataDTO.setStationId(tmsCustomerConsignee.getStationId());
			importMatchingDataDTO.setStationCode(tmsCustomerConsignee.getStationCode());
			importMatchingDataDTO.setStationName(tmsCustomerConsignee.getStationName());
			importMatchingDataDTO.setStationAreas(tmsCustomerConsignee.getStationAreas());
			importMatchingDataDTO.setStationDeliveryOrder(tmsCustomerConsignee.getStationDeliveryOrder());
			return importMatchingDataDTO;
		}
	}

	/**
	 * 临时客户收货方保存
	 *
	 * @param consigneeAddressVO
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void tempConsigneeSave(ConsigneeAddressVO consigneeAddressVO) {
		log.info("临时客户收货方保存:" + JSON.toJSONString(consigneeAddressVO));
		ToolKits.isParamNull(consigneeAddressVO, "临时客户收货方保存请求不为空");
		ToolKits.isParamNull(consigneeAddressVO.getName(), "客户收货方编码不为空");
		ToolKits.isParamNull(consigneeAddressVO.getAddress(), "地址不为空");
		ToolKits.isParamNull(consigneeAddressVO.getProvinceName(), "省不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCityName(), "市不为空");
		ToolKits.isParamNull(consigneeAddressVO.getDistrictName(), "区不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerCode(), "货主编码不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerName(), "货主名称不为空");

		if (ToolKits.isEmpty(consigneeAddressVO.getPhoneNumber()) && ToolKits.isEmpty(consigneeAddressVO.getLandlineNumber())) {
			throw new PrologException("手机号和座机号至少输入一个");
		}
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		// 校验是否已存在临时客户
		consigneeAddressVO.setEnterpriseId(user.getEnterpriseId());
		consigneeAddressVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
		consigneeAddressVO.setCustomName(consigneeAddressVO.getCargoOwnerName() + "_临时客户");
		List<TmsBaseCustomerDTO> customerDTOS = tmsBaseCustomerMapper.getTempCustomer(consigneeAddressVO);
		String customId = null;
		if (ToolKits.isEmpty(customerDTOS)) {
			// 新加临时客户
			List<Organization> organizations = Lists.newArrayList();
			List<String> deptIds = Lists.newArrayList();
			List<String> deptCodes = Lists.newArrayList();
			List<String> deptNames = Lists.newArrayList();
			RestMessage<List<Organization>> departMents = userTmsResource.selectByUserId(String.valueOf(user.getUserid()));
			if (!departMents.isSuccess()) {
				throw new RuntimeException("用户部门信息查询失败");
			} else {
				organizations = departMents.getData();
				if (organizations.size() == 0) {
					throw new RuntimeException("用户所属部门不存在!");
				}
				organizations.stream().forEach(organization -> {
					deptIds.add(organization.getId());
					deptCodes.add(organization.getCode());
					deptNames.add(organization.getName());
				});
			}
			customId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_ID, "L");
			if (ToolKits.isEmpty(customId)) {
				throw new BizException("客户id获取失败");
			}
			TmsBaseCustomerModel model = new TmsBaseCustomerModel();
			model.setId(Long.valueOf(customId));
			model.setCode(customId);
			model.setName(consigneeAddressVO.getCargoOwnerName() + "_临时客户");
			model.setCargoOwnerId(consigneeAddressVO.getCargoOwnerId());
			model.setCargoOwnerCode(consigneeAddressVO.getCargoOwnerCode());
			model.setCargoOwnerName(consigneeAddressVO.getCargoOwnerName());
			model.setStatus(StatusConstant.STATUS_ENABLE);
			model.setSource(DataDictionaryConstant.SOURCE_ADD);
			model.setCooperativeNature(2);
			model.setCreatetime(date);
			model.setCreateperson(user.getNickname());
			model.setUpdateperson(user.getNickname());
			model.setUpdatetime(date);
			model.setEnterpriseId(user.getEnterpriseId());
			model.setEnterpriseName(user.getEnterpriseName());
			model.setDepartmentId(deptIds.stream().collect(Collectors.joining(",")));
			model.setDepartmentCode(deptCodes.stream().collect(Collectors.joining(",")));
			model.setDepartmentName(deptNames.stream().collect(Collectors.joining(",")));
			tmsBaseCustomerMapper.save(model);

			log.info("临时客户新增:" + JSON.toJSONString(model));
		}

		// 校验是否已存在编码
		Criteria criteria = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r1 = Restrictions.eq(TmsCustomerConsignee::getEnterpriseId, user.getEnterpriseId());
		Restriction r2 = Restrictions.eq(TmsCustomerConsignee::getCustomerName, consigneeAddressVO.getCargoOwnerName() + "_临时客户");
		Restriction r3 = Restrictions.eq(TmsCustomerConsignee::getName, consigneeAddressVO.getName());
		Restriction r4 = Restrictions.eq(TmsCustomerConsignee::getProvinceName, consigneeAddressVO.getProvinceName());
		Restriction r5 = Restrictions.eq(TmsCustomerConsignee::getCityName, consigneeAddressVO.getCityName());
		Restriction r6 = Restrictions.eq(TmsCustomerConsignee::getDistrictName, consigneeAddressVO.getDistrictName());
		Restriction r7 = Restrictions.eq(TmsCustomerConsignee::getAddress, consigneeAddressVO.getAddress());

		Restriction r8 = ToolKits.isEmpty(consigneeAddressVO.getPhoneNumber()) ? null : Restrictions.eq(TmsCustomerConsignee::getPhoneNumber, consigneeAddressVO.getPhoneNumber());
		Restriction r9 = ToolKits.isEmpty(consigneeAddressVO.getLandlineNumber()) ? null : Restrictions.eq(TmsCustomerConsignee::getLandlineNumber, consigneeAddressVO.getLandlineNumber());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r9));
		List<TmsCustomerConsignee> list = tmsCustomerConsigneeMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(list)) {
			String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_CUSTOMER_RECEIVER_ID, "L");
			if (ToolKits.isEmpty(id)) {
				throw new BizException("收货方id获取失败");
			}

			TmsCustomerConsignee model = new TmsCustomerConsignee();
			model.setId(Long.valueOf(id));
			model.setCode(id);
			if (ToolKits.isEmpty(customerDTOS)) {
				model.setCustomerId(Long.valueOf(customId));
				model.setCustomerCode(customId);
				model.setCustomerName(consigneeAddressVO.getCargoOwnerName() + "_临时客户");
			} else {
				model.setCustomerId(customerDTOS.get(0).getId());
				model.setCustomerCode(customerDTOS.get(0).getCode());
				model.setCustomerName(customerDTOS.get(0).getName());
			}
			model.setName(consigneeAddressVO.getName());
			model.setAddress(consigneeAddressVO.getAddress());
			model.setPhoneNumber(consigneeAddressVO.getPhoneNumber());
			model.setLandlineNumber(consigneeAddressVO.getLandlineNumber());
			model.setProvince(consigneeAddressVO.getProvince());
			model.setProvinceName(consigneeAddressVO.getProvinceName());
			model.setCity(consigneeAddressVO.getCity());
			model.setCityName(consigneeAddressVO.getCityName());
			model.setDistrict(consigneeAddressVO.getDistrict());
			model.setDistrictName(consigneeAddressVO.getDistrictName());
			model.setLatitude(consigneeAddressVO.getLatitude());
			model.setLongitude(consigneeAddressVO.getLongitude());
			model.setCreatetime(new Date());
			model.setCreateperson(user.getNickname());
			model.setUpdateperson(user.getNickname());
			model.setUpdatetime(date);
			model.setEnterpriseId(user.getEnterpriseId());
			model.setEnterpriseName(user.getEnterpriseName());
			model.setStatus(StatusConstant.STATUS_ENABLE);
			model.setSource(DataDictionaryConstant.SOURCE_ADD);
			tmsCustomerConsigneeMapper.save(model);

			log.info("临时客户收货方新增:" + JSON.toJSONString(model));
		}
	}

	/**
	 * 临时客户收货方更新
	 *
	 * @param consigneeAddressVO
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void tempConsigneeUpdate(ConsigneeAddressVO consigneeAddressVO) {
		log.info("临时客户收货方更新:" + JSON.toJSONString(consigneeAddressVO));
		ToolKits.isParamNull(consigneeAddressVO, "临时客户收货方保存请求不为空");
		ToolKits.isParamNull(consigneeAddressVO.getConsigneeId(), "临时客户收货方ID不为空");
		ToolKits.isParamNull(consigneeAddressVO.getName(), "客户收货方编码不为空");
		ToolKits.isParamNull(consigneeAddressVO.getAddress(), "地址不为空");
		ToolKits.isParamNull(consigneeAddressVO.getProvinceName(), "省不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCityName(), "市不为空");
		ToolKits.isParamNull(consigneeAddressVO.getDistrictName(), "区不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerCode(), "货主编码不为空");
		ToolKits.isParamNull(consigneeAddressVO.getCargoOwnerName(), "货主名称不为空");

		if (ToolKits.isEmpty(consigneeAddressVO.getPhoneNumber()) && ToolKits.isEmpty(consigneeAddressVO.getLandlineNumber())) {
			throw new PrologException("手机号和座机号至少输入一个");
		}
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		TmsCustomerConsignee consignee = tmsCustomerConsigneeMapper.findById(consigneeAddressVO.getConsigneeId(), TmsCustomerConsignee.class);
		if (ToolKits.isEmpty(consignee)) {
			throw new PrologException("客户收货方不存在");
		}
		consignee.setName(consigneeAddressVO.getName());
		consignee.setAddress(consigneeAddressVO.getAddress());
		consignee.setPhoneNumber(consigneeAddressVO.getPhoneNumber());
		consignee.setLandlineNumber(consigneeAddressVO.getLandlineNumber());
		consignee.setProvince(consigneeAddressVO.getProvince());
		consignee.setProvinceName(consigneeAddressVO.getProvinceName());
		consignee.setCity(consigneeAddressVO.getCity());
		consignee.setCityName(consigneeAddressVO.getCityName());
		consignee.setDistrict(consigneeAddressVO.getDistrict());
		consignee.setDistrictName(consigneeAddressVO.getDistrictName());
		consignee.setUpdateperson(user.getNickname());
		consignee.setUpdatetime(date);
		tmsCustomerConsigneeMapper.update(consignee);

	}

	/**
	 * 临时客户收货方删除
	 *
	 * @param consigneeIds
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void tempConsigneeDelete(List<Long> consigneeIds) {
		log.info("临时客户收货方删除:" + JSON.toJSONString(consigneeIds));
		ToolKits.isParamNull(consigneeIds, "临时客户收货方删除请求不为空");

		tmsCustomerConsigneeMapper.deleteByIds(consigneeIds.stream().toArray(Long[]::new), TmsCustomerConsignee.class);
	}

	/**
	 * 更新收货方经纬度
	 * 
	 * @param idList
	 * @param latitude
	 * @param longitude
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateConsigneeLatitudeAndLongitude(List<Long> idList, BigDecimal latitude, BigDecimal longitude) {
		List<TmsCustomerConsignee> tmsCustomerConsigneeList = tmsCustomerConsigneeMapper.findByIds(TmsCustomerConsignee.class, idList.toArray(), null);
		if (CollectionUtil.isNotEmpty(tmsCustomerConsigneeList)) {
			UserEntity user = PlgFxUsrUtils.getUserEntity();
			tmsCustomerConsigneeList.forEach(tmsCustomerConsignee -> {
				tmsCustomerConsignee.setUpdateperson(user.getNickname());
				tmsCustomerConsignee.setUpdatetime(new Date());
				tmsCustomerConsignee.setLongitude(longitude);
				tmsCustomerConsignee.setLatitude(latitude);
				tmsCustomerConsigneeMapper.update(tmsCustomerConsignee);
			});
		}
	}

	/**
	 * 根据客户编码名称查询客户资料
	 */
	@Override
	public CustomerByEncodingNameDTO getCustomerByNameData(CustomerByEncodingNameVO customerByEncodingNameVO) {
		ToolKits.isParamNull(customerByEncodingNameVO, "根据客户名称查询客户资料请求不为空");
		ToolKits.isParamNull(customerByEncodingNameVO.getName(), "客户名称不为空");

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

		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 start
		customerByEncodingNameVO.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			customerByEncodingNameVO.setIsDept("Y");
			// 自有账号
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				customerByEncodingNameVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				// 非部门权限，顶级部门取得
				customerByEncodingNameVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				customerByEncodingNameVO.setIdArrs(user.getCustomers().stream().toArray(Long[]::new));
				customerByEncodingNameVO.setIsCustom("Y");
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主账号
			// 显示所属货主为当前登录账号单位id的客户
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerByEncodingNameVO.setCargoOwnerIds(ownerIds);
			customerByEncodingNameVO.setIsCargoOwner("Y");
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户账号
			// 显示客户id为当前登录账号单位id 的客户
			Long[] ids = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			customerByEncodingNameVO.setIdArrs(ids);
			customerByEncodingNameVO.setIsCustom("Y");
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		List<TmsBaseCustomerDTO> tmsBaseCustomerModels = tmsBaseCustomerMapper.getCustomerByEncodingNameData(customerByEncodingNameVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001 客户支持多部门 20220906 end
		Assert.isTrue(tmsBaseCustomerModels.size() > 0, "名称为【" + customerByEncodingNameVO.getName() + "】的客户");
		Assert.isTrue(tmsBaseCustomerModels.size() == 1, "名称为【" + customerByEncodingNameVO.getName() + "】的客户资料重复");

		// 客户资料对象
		TmsBaseCustomerDTO tmsBaseCustomerModel = tmsBaseCustomerModels.get(0);
		Assert.isTrue(tmsBaseCustomerModel.getStatus() == StatusConstant.STATUS_ENABLE, "客户【" + customerByEncodingNameVO.getName() + "】未启用");

		// 根据客户查询客户收货方
		Criteria criteria1 = Criteria.forClass(TmsCustomerConsignee.class);
		Restriction r11 = Restrictions.eq("customerId", tmsBaseCustomerModel.getId());
		Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria1.setRestriction(Restrictions.and(r11, r12));
		List<TmsCustomerConsignee> tmsCustomerConsignees = tmsCustomerConsigneeMapper.findByCriteria(criteria1);
		Assert.isTrue(tmsCustomerConsignees.size() > 0, "编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户未维护客户收货方");
		Assert.isTrue(tmsCustomerConsignees.size() == 1, "请指定编码为【" + customerByEncodingNameVO.getCode() + "】,名称为【" + customerByEncodingNameVO.getName() + "】的客户收货方");

		// 客户收货方资料对象
		TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsignees.get(0);
		Assert.isTrue(tmsCustomerConsignee.getStatus() == StatusConstant.STATUS_ENABLE, "客户收货方【" + tmsCustomerConsignee.getName() + "】未启用");

		// 响应对象
		CustomerConsigneeByEncodingNameDTO customerConsigneeByEncodingNameDTO = new CustomerConsigneeByEncodingNameDTO();
		BeanUtils.copyProperties(tmsCustomerConsignee, customerConsigneeByEncodingNameDTO);

		// 响应对象
		CustomerByEncodingNameDTO customerByEncodingNameDTO = new CustomerByEncodingNameDTO();
		BeanUtils.copyProperties(tmsBaseCustomerModel, customerByEncodingNameDTO);
		customerByEncodingNameDTO.setConsignee(customerConsigneeByEncodingNameDTO);

		return customerByEncodingNameDTO;
	}

	/**
	 * 添加客户默认收货方
	 * @param model
	 */
	private void saveDefaultCustomerReceiver(TmsBaseCustomerModel model) {
		CustomerReceiverSaveVO customerReceiverSaveVO = new CustomerReceiverSaveVO();
		StringBuffer customerReceiverCode = new StringBuffer();
		BeanUtils.copyProperties(model, customerReceiverSaveVO);

		customerReceiverCode.append(model.getCode()).append("001");
		customerReceiverSaveVO.setCode(customerReceiverCode.toString());
		customerReceiverSaveVO.setCarrierType(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER);
		customerReceiverSaveVO.setCarrierId(null);
		customerReceiverSaveVO.setCarrierCode(null);
		customerReceiverSaveVO.setCarrierName(null);
		customerReceiverSaveVO.setCarrierPhoneNumber(null);
		customerReceiverSaveVO.setRelationPhoneNumber(null);
		customerReceiverSaveVO.setMemo(null);
		customerReceiverSaveVO.setStationId(null);
		customerReceiverSaveVO.setStationCode(null);
		customerReceiverSaveVO.setStationName(null);
		customerReceiverSaveVO.setStationAreas(null);
		customerReceiverSaveVO.setCustomerId(model.getId());
		customerReceiverSaveVO.setCustomerCode(model.getCode());
		customerReceiverSaveVO.setCustomerName(model.getName());
		this.saveCustomerReceiver(customerReceiverSaveVO);
	}
}
