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

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

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 com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.BizException;
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.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsContainerBillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsContainerBillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsContainerBillLogMapper;
import com.prolog.rdc.tms.model.dto.ContoinerBillConsigneeCountDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillConsigneeDetailsListDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillConsigneePageDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillContainerCountDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillContainerPageDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillLogQueryDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillReturnCountDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillReturnDetailsListDTO;
import com.prolog.rdc.tms.model.dto.ContoinerBillReturnPageDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.entity.TmsContainerBillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsContainerBillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsContainerBillLogModel;
import com.prolog.rdc.tms.model.vo.ContoinerBillConsigneeDetailsListVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillConsigneePageVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillContainerAbnormalVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillContainerPageVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillLogQueryVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnCancelVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnDetailsListVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnDetailsSaveVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnModifyVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnPageVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnReceiptedDetailsVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnReceiptedVO;
import com.prolog.rdc.tms.model.vo.ContoinerBillReturnSaveVO;
import com.prolog.rdc.tms.proxy.MainDataProxy;
import com.prolog.rdc.tms.service.ContainerBillService;
import com.prolog.rdc.tms.util.ConvertorUtil;

import cn.hutool.core.date.DateUtil;

/**
 * 订单管理实现层
 *
 * @author :zhangsj
 * @version :1.0.0
 * @className :ShipbillServiceImpl
 * @creationTime :2022/03/31
 * @description :
 * @modifier :zhangsj
 * @modifyTime :2022/03/31
 * @reviseRemarks:
 */
@Service
public class ContainerBillServiceImpl implements ContainerBillService {

	@Autowired
	private TmsContainerBillDetailMapper tmsContainerBillDetailMapper;

	@Autowired
	private TmsContainerBillHeaderMapper tmsContainerBillHeaderMapper;

	@Autowired
	private TmsContainerBillLogMapper tmsContainerBillLogMapper;

	@Autowired
	private ConvertorUtil convertorUtil;

	@Autowired
	private MainDataProxy mainDataProxy;


	/**
	 * 容器维度查询
	 *
	 * @param contoinerBillContainerPageVO
	 * @return
	 */
	@Override
	public Page<ContoinerBillContainerPageDTO> pageContoinerBillContainerData(ContoinerBillContainerPageVO contoinerBillContainerPageVO) {
		ToolKits.isParamNull(contoinerBillContainerPageVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillContainerPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(contoinerBillContainerPageVO.getPageSize(), "每页条数不为空");

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

		contoinerBillContainerPageVO.setUserType(user.getUserType());
		contoinerBillContainerPageVO.setEnterpriseId(user.getEnterpriseId());

		// 翻译字典
		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.CONTAINER_TYPE, DataDictionaryConstant.CONTAINER_BILL_STATUS, DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS
		});

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillContainerPageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillContainerPageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		// 查询
		PageUtils.startPage(contoinerBillContainerPageVO.getPageNum(), contoinerBillContainerPageVO.getPageSize());
		List<ContoinerBillContainerPageDTO> contoinerBillContainerPageDTOs = tmsContainerBillDetailMapper.contoinerBillContainerSql(contoinerBillContainerPageVO);
		for (ContoinerBillContainerPageDTO contoinerBillContainerPageDTO : contoinerBillContainerPageDTOs) {
			// 类型翻译
			contoinerBillContainerPageDTO.setContainerTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_TYPE, String.valueOf(contoinerBillContainerPageDTO.getContainerType())));
			contoinerBillContainerPageDTO.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_BILL_STATUS, String.valueOf(contoinerBillContainerPageDTO.getStatus())));
			contoinerBillContainerPageDTO.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS, String.valueOf(contoinerBillContainerPageDTO.getAbnormalStatus())));
		}

		return PageUtils.getPage(contoinerBillContainerPageDTOs);
	}

	/**
	 * 容器维度统计查询
	 */
	@Override
	public ContoinerBillContainerCountDTO countContoinerBillContainerData(ContoinerBillContainerPageVO contoinerBillContainerPageVO) {
		ToolKits.isParamNull(contoinerBillContainerPageVO, "请求不为空！");

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

		contoinerBillContainerPageVO.setUserType(user.getUserType());
		contoinerBillContainerPageVO.setEnterpriseId(user.getEnterpriseId());

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillContainerPageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillContainerPageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillContainerPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		List<ContoinerBillContainerPageDTO> contoinerBills = tmsContainerBillDetailMapper.contoinerBillContainerSql(contoinerBillContainerPageVO);

		// 在途托盘
		List<ContoinerBillContainerPageDTO> onwayContainers1 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_ZT.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()).collect(Collectors.toList());
		// 在途周转箱
		List<ContoinerBillContainerPageDTO> onwayContainers2 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_ZT.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_2.intValue()).collect(Collectors.toList());

		// 在店托盘
		List<ContoinerBillContainerPageDTO> instoreContainers1 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()).collect(Collectors.toList());
		// 在店周转箱
		List<ContoinerBillContainerPageDTO> instoreContainers2 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_2.intValue()).collect(Collectors.toList());

		// 回途托盘
		List<ContoinerBillContainerPageDTO> backContainers1 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_HT.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()).collect(Collectors.toList());
		// 回途周转箱
		List<ContoinerBillContainerPageDTO> backContainers2 = contoinerBills.stream().filter(s -> s.getStatus().intValue() == DataDictionaryConstant.CONTAINER_BILL_STATUS_HT.intValue() && s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_2.intValue()).collect(Collectors.toList());

		// 返回对象
		ContoinerBillContainerCountDTO containerCount = new ContoinerBillContainerCountDTO();
		containerCount.setOnwayPalletsNumbers(new BigDecimal(onwayContainers1.size()));
		containerCount.setOnwayTotesNumbers(new BigDecimal(onwayContainers2.size()));
		containerCount.setOnwayContainersNumbers(containerCount.getOnwayPalletsNumbers().add(containerCount.getOnwayTotesNumbers()));

		containerCount.setInstorePalletsNumbers(new BigDecimal(instoreContainers1.size()));
		containerCount.setInstoreTotesNumbers(new BigDecimal(instoreContainers2.size()));
		containerCount.setInstoreContainersNumbers(containerCount.getInstorePalletsNumbers().add(containerCount.getInstoreTotesNumbers()));

		containerCount.setBackPalletsNumbers(new BigDecimal(backContainers1.size()));
		containerCount.setBackTotesNumbers(new BigDecimal(backContainers2.size()));
		containerCount.setBackContainersNumbers(containerCount.getBackPalletsNumbers().add(containerCount.getBackTotesNumbers()));

		return containerCount;
	}

	/**
	 * 容器业务日志查询
	 */
	@Override
	public List<ContoinerBillLogQueryDTO> listContoinerBillLogData(ContoinerBillLogQueryVO contoinerBillLogQueryVO) {
		ToolKits.isParamNull(contoinerBillLogQueryVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillLogQueryVO.getContainerId(), "容器ID不为空");

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

		Criteria criteria = Criteria.forClass(TmsContainerBillLogModel.class);
		Restriction r1 = Restrictions.eq(TmsContainerBillLogModel::getContainerId, contoinerBillLogQueryVO.getContainerId());
		Restriction r2 = Restrictions.eq(TmsContainerBillLogModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc("createtime"));
		List<TmsContainerBillLogModel> tmsContainerBillLogModels = tmsContainerBillLogMapper.findByCriteria(criteria);

		List<ContoinerBillLogQueryDTO> contoinerBillLogQueryDTOs = Lists.newArrayList();
		for (TmsContainerBillLogModel tmsContainerBillLogModel : tmsContainerBillLogModels) {
			ContoinerBillLogQueryDTO contoinerBillLogQueryDTO = new ContoinerBillLogQueryDTO();
			BeanUtils.copyProperties(tmsContainerBillLogModel, contoinerBillLogQueryDTO);

			contoinerBillLogQueryDTOs.add(contoinerBillLogQueryDTO);
		}


		return contoinerBillLogQueryDTOs;
	}

	/**
	 * 容器异常处理
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void abnormalContoinerBillContainerData(ContoinerBillContainerAbnormalVO contoinerBillContainerAbnormalVO) {
		ToolKits.isParamNull(contoinerBillContainerAbnormalVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillContainerAbnormalVO.getAbnormalStatus(), "异常状态不为空");

		if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_DS.intValue() == contoinerBillContainerAbnormalVO.getAbnormalStatus().intValue()) {
			ToolKits.isParamNull(contoinerBillContainerAbnormalVO.getAbnormalMemo(), "处理异常时，异常备注不为空");
		}

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

		// 容器管理单明细对象
		TmsContainerBillDetailModel tmsContainerBillDetailModel = tmsContainerBillDetailMapper.findById(contoinerBillContainerAbnormalVO.getId(), TmsContainerBillDetailModel.class);
		Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillDetailModel), "对应的容器明细单不存在");

		// 正常处理
		if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC.intValue() == contoinerBillContainerAbnormalVO.getAbnormalStatus().intValue()) {
			tmsContainerBillDetailModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC);
			tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
			tmsContainerBillDetailModel.setUpdatetime(DateUtil.date());
			tmsContainerBillDetailMapper.update(tmsContainerBillDetailModel);

			// 判断对应的主单是否还存在异常的容器
			Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
			Restriction r1 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillNo, tmsContainerBillDetailModel.getContainerBillNo());
			Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, tmsContainerBillDetailModel.getContainerBillType());
			Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r4 = Restrictions.ne(TmsContainerBillDetailModel::getAbnormalStatus, DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC);
			criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
			List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);
			if (tmsContainerBillDetailModels.size() <= 0) {
				TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(tmsContainerBillDetailModel.getContainerBillNo(), TmsContainerBillHeaderModel.class);
				Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillHeaderModel), "容器业务单号【" + tmsContainerBillDetailModel.getContainerBillNo() + "】不存在");

				// 更新主单的异常状态
				tmsContainerBillHeaderModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_BILL_ABNORMAL_STATUS_ZC);
				tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
				tmsContainerBillHeaderModel.setUpdatetime(DateUtil.date());
				tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);
			}

			// 记录容器日志
			TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
			tmsContainerBillLogModel.setId(ToolKits.getUUID());
			tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
			tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
			tmsContainerBillLogModel.setOperateDate(DateUtil.date());
			tmsContainerBillLogModel.setOperateEvent("异常处理为正常");
			tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
			tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
			tmsContainerBillLogModel.setCreateperson(user.getNickname());
			tmsContainerBillLogModel.setCreatetime(DateUtil.date());
			tmsContainerBillLogModel.setUpdateperson(user.getNickname());
			tmsContainerBillLogModel.setUpdatetime(DateUtil.date());
			tmsContainerBillLogMapper.save(tmsContainerBillLogModel);
		} else {
			// 异常处理
			tmsContainerBillDetailModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_DS);
			tmsContainerBillDetailModel.setAbnormalMemo("丢失：" + contoinerBillContainerAbnormalVO.getAbnormalMemo());
			tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
			tmsContainerBillDetailModel.setUpdatetime(DateUtil.date());
			tmsContainerBillDetailMapper.update(tmsContainerBillDetailModel);

			// 更新主单的异常状态
			TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(tmsContainerBillDetailModel.getContainerBillNo(), TmsContainerBillHeaderModel.class);
			Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillHeaderModel), "容器业务单号【" + tmsContainerBillDetailModel.getContainerBillNo() + "】不存在");

			tmsContainerBillHeaderModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_BILL_ABNORMAL_STATUS_YC);
			tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
			tmsContainerBillHeaderModel.setUpdatetime(DateUtil.date());
			tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);

			// 记录容器日志
			TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
			tmsContainerBillLogModel.setId(ToolKits.getUUID());
			tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
			tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
			tmsContainerBillLogModel.setOperateDate(DateUtil.date());
			tmsContainerBillLogModel.setOperateEvent("异常处理为：" + (ToolKits.isEmpty(tmsContainerBillDetailModel.getAbnormalMemo()) ? "" : tmsContainerBillDetailModel.getAbnormalMemo()));
			tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
			tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
			tmsContainerBillLogModel.setCreateperson(user.getNickname());
			tmsContainerBillLogModel.setCreatetime(DateUtil.date());
			tmsContainerBillLogModel.setUpdateperson(user.getNickname());
			tmsContainerBillLogModel.setUpdatetime(DateUtil.date());
			tmsContainerBillLogMapper.save(tmsContainerBillLogModel);
		}

	}

	/**
	 * 门店在途查询
	 */
	@Override
	public Page<ContoinerBillConsigneePageDTO> pageContoinerBillConsigneeData(ContoinerBillConsigneePageVO contoinerBillConsigneePageVO) {
		ToolKits.isParamNull(contoinerBillConsigneePageVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillConsigneePageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(contoinerBillConsigneePageVO.getPageSize(), "每页条数不为空");

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

		contoinerBillConsigneePageVO.setUserType(user.getUserType());
		contoinerBillConsigneePageVO.setEnterpriseId(user.getEnterpriseId());

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillConsigneePageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillConsigneePageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		// 查询
		PageUtils.startPage(contoinerBillConsigneePageVO.getPageNum(), contoinerBillConsigneePageVO.getPageSize());
		List<ContoinerBillConsigneePageDTO> contoinerBillConsigneePageDTOs = tmsContainerBillHeaderMapper.contoinerBillConsigneeSql(contoinerBillConsigneePageVO);
		for (ContoinerBillConsigneePageDTO contoinerBillConsigneePageDTO : contoinerBillConsigneePageDTOs) {
			contoinerBillConsigneePageDTO.setId(ToolKits.getUUID());

			List<String> containerBillNos = Lists.newArrayList();
			String[] str = contoinerBillConsigneePageDTO.getContainerBillNos().split(",");
			for (int i = 0; i < str.length; i++) {
				containerBillNos.add(str[i]);
			}

			// 查询容器随车单明细
			Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
			Restriction r1 = Restrictions.in(TmsContainerBillDetailModel::getContainerBillNo, containerBillNos.stream().toArray(String[]::new));
			Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_SCD);
			Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r4 = Restrictions.eq(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
			criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
			List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);

			// 托盘容器对象
			List<TmsContainerBillDetailModel> palletsNumbers = tmsContainerBillDetailModels.stream().filter(s -> s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()).collect(Collectors.toList());
			// 周转箱容器对象
			List<TmsContainerBillDetailModel> totesNumbers = tmsContainerBillDetailModels.stream().filter(s -> s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_2.intValue()).collect(Collectors.toList());

			contoinerBillConsigneePageDTO.setPalletsNumber(new BigDecimal(palletsNumbers.size()));
			contoinerBillConsigneePageDTO.setTotesNumber(new BigDecimal(totesNumbers.size()));
			contoinerBillConsigneePageDTO.setContainersNumber(contoinerBillConsigneePageDTO.getPalletsNumber().add(contoinerBillConsigneePageDTO.getTotesNumber()));
		}

		return PageUtils.getPage(contoinerBillConsigneePageDTOs);
	}

	/**
	 * 门店在途统计查询
	 */
	@Override
	public ContoinerBillConsigneeCountDTO countContoinerBillConsigneeData(ContoinerBillConsigneePageVO contoinerBillConsigneePageVO) {
		ToolKits.isParamNull(contoinerBillConsigneePageVO, "请求不为空！");

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

		contoinerBillConsigneePageVO.setUserType(user.getUserType());
		contoinerBillConsigneePageVO.setEnterpriseId(user.getEnterpriseId());

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillConsigneePageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillConsigneePageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillConsigneePageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		// 查询
		List<ContoinerBillConsigneePageDTO> contoinerBillConsigneePageDTOs = tmsContainerBillHeaderMapper.contoinerBillConsigneeSql(contoinerBillConsigneePageVO);

		// 容器随车单号
		List<String> billNoList = contoinerBillConsigneePageDTOs.stream().map(ContoinerBillConsigneePageDTO::getContainerBillNos).distinct().collect(Collectors.toList());

		List<String> containerBillNos = Lists.newArrayList();
		for (String billNo : billNoList) {
			String[] str = billNo.split(",");
			for (int i = 0; i < str.length; i++) {
				containerBillNos.add(str[i]);
			}
		}

		// 查询容器随车单明细
		Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
		Restriction r1 = Restrictions.in(TmsContainerBillDetailModel::getContainerBillNo, containerBillNos.stream().toArray(String[]::new));
		Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_SCD);
		Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r4 = Restrictions.eq(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);

		// 托盘容器对象
		List<TmsContainerBillDetailModel> palletsNumbers = tmsContainerBillDetailModels.stream().filter(s -> s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()).collect(Collectors.toList());
		// 周转箱容器对象
		List<TmsContainerBillDetailModel> totesNumbers = tmsContainerBillDetailModels.stream().filter(s -> s.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_2.intValue()).collect(Collectors.toList());

		ContoinerBillConsigneeCountDTO contoinerBillConsigneeCountDTO = new ContoinerBillConsigneeCountDTO();
		contoinerBillConsigneeCountDTO.setPalletsNumbers(new BigDecimal(palletsNumbers.size()));
		contoinerBillConsigneeCountDTO.setTotesNumbers(new BigDecimal(totesNumbers.size()));
		contoinerBillConsigneeCountDTO.setContainersNumbers(contoinerBillConsigneeCountDTO.getPalletsNumbers().add(contoinerBillConsigneeCountDTO.getTotesNumbers()));

		return contoinerBillConsigneeCountDTO;
	}

	/**
	 * 门店在途容器明细查询
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ContoinerBillConsigneeDetailsListDTO> listContoinerBillConsigneeDetailsData(ContoinerBillConsigneeDetailsListVO contoinerBillConsigneeDetailsListVO) {
		ToolKits.isParamNull(contoinerBillConsigneeDetailsListVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillConsigneeDetailsListVO.getContainerBillNos(), "关联单号不为空");

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

		String[] containerBillNos = contoinerBillConsigneeDetailsListVO.getContainerBillNos().split(",");

		// 翻译字典
		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.CONTAINER_TYPE });

		Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
		Restriction r1 = Restrictions.in(TmsContainerBillDetailModel::getContainerBillNo, containerBillNos);
		Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_SCD);
		Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r4 = Restrictions.eq(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
		Restriction r5 = ToolKits.isEmpty(contoinerBillConsigneeDetailsListVO.getContainerCode()) ? null : Restrictions.like(TmsContainerBillDetailModel::getContainerCode, "%" + contoinerBillConsigneeDetailsListVO.getContainerCode() + "%");
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5));
		criteria.setOrder(Order.newInstance().desc(TmsContainerBillDetailModel::getContainerCode));
		List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);

		List<ContoinerBillConsigneeDetailsListDTO> contoinerBillConsigneeDetailsListDTOs = Lists.newArrayList();
		for (TmsContainerBillDetailModel tmsContainerBillDetailModel : tmsContainerBillDetailModels) {
			ContoinerBillConsigneeDetailsListDTO contoinerBillConsigneeDetailsListDTO = new ContoinerBillConsigneeDetailsListDTO();
			BeanUtils.copyProperties(tmsContainerBillDetailModel, contoinerBillConsigneeDetailsListDTO);
			// 容器类型翻译
			contoinerBillConsigneeDetailsListDTO.setContainerTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_TYPE, String.valueOf(contoinerBillConsigneeDetailsListDTO.getContainerType())));

			contoinerBillConsigneeDetailsListDTOs.add(contoinerBillConsigneeDetailsListDTO);
		}

		return contoinerBillConsigneeDetailsListDTOs;
	}

	/**
	 * 归还容器
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveContoinerBillReturnData(ContoinerBillReturnSaveVO contoinerBillReturnSaveVO) {
		ToolKits.isParamNull(contoinerBillReturnSaveVO, "请求不为空！");
//		ToolKits.isParamNull(contoinerBillReturnSaveVO.getDriverId(), "司机ID不为空");
//		ToolKits.isParamNull(contoinerBillReturnSaveVO.getDriverCode(), "司机编码不为空");
		ToolKits.isParamNull(contoinerBillReturnSaveVO.getDriverName(), "司机名称不为空");
		ToolKits.isParamNull(contoinerBillReturnSaveVO.getDriverPhoneNumber(), "司机手机不为空");
//		ToolKits.isParamNull(contoinerBillReturnSaveVO.getVehicleId(), "车辆ID不为空");
		ToolKits.isParamNull(contoinerBillReturnSaveVO.getVehiclePlateNumber(), "车牌号不为空");
		ToolKits.isParamNull(contoinerBillReturnSaveVO.getContainerBillNos(), "关联单号不为空");
		ToolKits.isParamNull(contoinerBillReturnSaveVO.getDetails(), "容器明细不为空");

		String[] containerBillNos = contoinerBillReturnSaveVO.getContainerBillNos().split(",");

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

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

		// 查询容器随车单
		Criteria criteria = Criteria.forClass(TmsContainerBillHeaderModel.class);
		Restriction r1 = Restrictions.in(TmsContainerBillHeaderModel::getContainerBillNo, containerBillNos);
		Restriction r2 = Restrictions.eq(TmsContainerBillHeaderModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_SCD);
		Restriction r3 = Restrictions.eq(TmsContainerBillHeaderModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsContainerBillHeaderModel> tmsContainerBillHeaderModels = tmsContainerBillHeaderMapper.findByCriteria(criteria);
		Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillHeaderModels), "所选的容器随车单不存在");
		
		// 校验发货方是否同一个
		List<Long> shipperIds = tmsContainerBillHeaderModels.stream().map(TmsContainerBillHeaderModel::getShipperId).distinct().collect(Collectors.toList());
		Assert.isTrue(shipperIds.size() == 1, "请选择同一发货方");

		// 校验收货方是否同一个
		List<Long> consigneeIds = tmsContainerBillHeaderModels.stream().map(TmsContainerBillHeaderModel::getConsigneeId).distinct().collect(Collectors.toList());
		Assert.isTrue(consigneeIds.size() == 1, "请选择同一收货方");

		// 生成容器归还单单号
		String containerBillNo = mainDataProxy.getCodeNumberByCode(SeriaNnumberConstant.CONTAINER_BILL_NO_GH);

		// 生成容器归还单
		TmsContainerBillHeaderModel tmsContainerBillHeaderModel = new TmsContainerBillHeaderModel();
		tmsContainerBillHeaderModel.setContainerBillNo(containerBillNo);
		tmsContainerBillHeaderModel.setContainerBillType(DataDictionaryConstant.CONTAINER_BILL_TYPE_GHD);
		tmsContainerBillHeaderModel.setContainerBillDate(date);
		tmsContainerBillHeaderModel.setShipperType(tmsContainerBillHeaderModels.get(0).getConsigneeType());
		tmsContainerBillHeaderModel.setShipperUnitId(tmsContainerBillHeaderModels.get(0).getConsigneeUnitId());
		tmsContainerBillHeaderModel.setShipperUnitCode(tmsContainerBillHeaderModels.get(0).getConsigneeUnitCode());
		tmsContainerBillHeaderModel.setShipperUnitName(tmsContainerBillHeaderModels.get(0).getConsigneeUnitName());
		tmsContainerBillHeaderModel.setShipperId(tmsContainerBillHeaderModels.get(0).getConsigneeId());
		tmsContainerBillHeaderModel.setShipperCode(tmsContainerBillHeaderModels.get(0).getConsigneeCode());
		tmsContainerBillHeaderModel.setShipperName(tmsContainerBillHeaderModels.get(0).getConsigneeName());
		tmsContainerBillHeaderModel.setShipperContact(tmsContainerBillHeaderModels.get(0).getConsigneeContact());
		tmsContainerBillHeaderModel.setShipperPhoneNumber(tmsContainerBillHeaderModels.get(0).getConsigneePhoneNumber());
		tmsContainerBillHeaderModel.setShipperAddress(tmsContainerBillHeaderModels.get(0).getConsigneeAddress());
		tmsContainerBillHeaderModel.setConsigneeType(tmsContainerBillHeaderModels.get(0).getShipperType());
		tmsContainerBillHeaderModel.setConsigneeUnitId(tmsContainerBillHeaderModels.get(0).getShipperUnitId());
		tmsContainerBillHeaderModel.setConsigneeUnitCode(tmsContainerBillHeaderModels.get(0).getShipperUnitCode());
		tmsContainerBillHeaderModel.setConsigneeUnitName(tmsContainerBillHeaderModels.get(0).getShipperUnitName());
		tmsContainerBillHeaderModel.setConsigneeId(tmsContainerBillHeaderModels.get(0).getShipperId());
		tmsContainerBillHeaderModel.setConsigneeCode(tmsContainerBillHeaderModels.get(0).getShipperCode());
		tmsContainerBillHeaderModel.setConsigneeName(tmsContainerBillHeaderModels.get(0).getShipperName());
		tmsContainerBillHeaderModel.setConsigneeContact(tmsContainerBillHeaderModels.get(0).getShipperContact());
		tmsContainerBillHeaderModel.setConsigneePhoneNumber(tmsContainerBillHeaderModels.get(0).getShipperPhoneNumber());
		tmsContainerBillHeaderModel.setConsigneeAddress(tmsContainerBillHeaderModels.get(0).getShipperAddress());
		tmsContainerBillHeaderModel.setDriverId(contoinerBillReturnSaveVO.getDriverId());
		tmsContainerBillHeaderModel.setDriverCode(contoinerBillReturnSaveVO.getDriverCode());
		tmsContainerBillHeaderModel.setDriverName(contoinerBillReturnSaveVO.getDriverName());
		tmsContainerBillHeaderModel.setDriverPhoneNumber(contoinerBillReturnSaveVO.getDriverPhoneNumber());
		tmsContainerBillHeaderModel.setVehicleId(contoinerBillReturnSaveVO.getVehicleId());
		tmsContainerBillHeaderModel.setVehiclePlateNumber(contoinerBillReturnSaveVO.getVehiclePlateNumber());
		tmsContainerBillHeaderModel.setDeliveryTime(null);
		tmsContainerBillHeaderModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_BILL_ABNORMAL_STATUS_ZC);
		tmsContainerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_GH);
		tmsContainerBillHeaderModel.setDepartmentId(tmsContainerBillHeaderModels.get(0).getDepartmentId());
		tmsContainerBillHeaderModel.setDepartmentCode(tmsContainerBillHeaderModels.get(0).getDepartmentCode());
		tmsContainerBillHeaderModel.setDepartmentName(tmsContainerBillHeaderModels.get(0).getDepartmentName());
		tmsContainerBillHeaderModel.setEnterpriseId(user.getEnterpriseId());
		tmsContainerBillHeaderModel.setEnterpriseName(user.getEnterpriseName());
		tmsContainerBillHeaderModel.setCreatetime(date);
		tmsContainerBillHeaderModel.setCreateperson(user.getNickname());
		tmsContainerBillHeaderModel.setUpdatetime(date);
		tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());

		// 托盘数量
		BigDecimal palletsNumber = BigDecimal.ZERO;
		// 周转箱数量
		BigDecimal totesNumber = BigDecimal.ZERO;

		// 生成容器明细
		List<ContoinerBillReturnDetailsSaveVO> contoinerBillReturnDetailsSaveVOs = contoinerBillReturnSaveVO.getDetails();
		for (ContoinerBillReturnDetailsSaveVO contoinerBillReturnDetailsSaveVO : contoinerBillReturnDetailsSaveVOs) {
			ToolKits.isParamNull(contoinerBillReturnDetailsSaveVO.getContainerBillNo(), "容器随车单号不为空");
			ToolKits.isParamNull(contoinerBillReturnDetailsSaveVO.getId(), "容器随车明细行ID不为空");

			TmsContainerBillDetailModel tmsContainerBillDetailModel = new TmsContainerBillDetailModel();
			BeanUtils.copyProperties(contoinerBillReturnDetailsSaveVO, tmsContainerBillDetailModel);

			tmsContainerBillDetailModel.setId(ToolKits.getUUID());
			tmsContainerBillDetailModel.setContainerBillNo(containerBillNo);
			tmsContainerBillDetailModel.setContainerBillType(DataDictionaryConstant.CONTAINER_BILL_TYPE_GHD);
			tmsContainerBillDetailModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC);
			tmsContainerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_GH);
			tmsContainerBillDetailModel.setEnterpriseId(user.getEnterpriseId());
			tmsContainerBillDetailModel.setEnterpriseName(user.getEnterpriseName());
			tmsContainerBillDetailModel.setCreatetime(date);
			tmsContainerBillDetailModel.setCreateperson(user.getNickname());
			tmsContainerBillDetailModel.setUpdatetime(date);
			tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
			tmsContainerBillDetailModel.setRelevancyBillNo(contoinerBillReturnDetailsSaveVO.getContainerBillNo());
			tmsContainerBillDetailModel.setRelevancyId(contoinerBillReturnDetailsSaveVO.getId());
			tmsContainerBillDetailMapper.save(tmsContainerBillDetailModel);
			
			if (tmsContainerBillDetailModel.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()) {
				palletsNumber = palletsNumber.add(new BigDecimal("1"));
			} else {
				totesNumber = totesNumber.add(new BigDecimal("1"));
			}
			
			// 已生成归还单明细的随车明细更新为归还
			TmsContainerBillDetailModel containerBillDetailModel = tmsContainerBillDetailMapper.findById(contoinerBillReturnDetailsSaveVO.getId(), TmsContainerBillDetailModel.class);
			Assert.isTrue(ToolKits.isNotEmpty(containerBillDetailModel), "随车单明细ID【" + contoinerBillReturnDetailsSaveVO.getId() + "】不存在");

			// 随车单明细更新状态为归还
			containerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_GH);
			containerBillDetailModel.setUpdatetime(date);
			containerBillDetailModel.setUpdateperson(user.getNickname());
			tmsContainerBillDetailMapper.update(containerBillDetailModel);

			// 记录容器日志
			TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
			tmsContainerBillLogModel.setId(ToolKits.getUUID());
			tmsContainerBillLogModel.setContainerBillNo(containerBillNo);
			tmsContainerBillLogModel.setContainerId(contoinerBillReturnDetailsSaveVO.getContainerId());
			tmsContainerBillLogModel.setOperateDate(date);
			tmsContainerBillLogModel.setOperateEvent("生成容器归还单");
			tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
			tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
			tmsContainerBillLogModel.setCreateperson(user.getNickname());
			tmsContainerBillLogModel.setCreatetime(date);
			tmsContainerBillLogModel.setUpdateperson(user.getNickname());
			tmsContainerBillLogModel.setUpdatetime(date);
			tmsContainerBillLogMapper.save(tmsContainerBillLogModel);
		}

		tmsContainerBillHeaderModel.setContainersNumber(palletsNumber.add(totesNumber));
		tmsContainerBillHeaderModel.setPalletsNumber(palletsNumber);
		tmsContainerBillHeaderModel.setTotesNumber(totesNumber);
		tmsContainerBillHeaderMapper.save(tmsContainerBillHeaderModel);

		// 判断随车单明细是否都已生成归还单
		for (TmsContainerBillHeaderModel containerBillHeaderModel : tmsContainerBillHeaderModels) {
			Criteria criteria2 = Criteria.forClass(TmsContainerBillDetailModel.class);
			Restriction r21 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillNo, containerBillHeaderModel.getContainerBillNo());
			Restriction r22 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_SCD);
			Restriction r23 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r24 = Restrictions.eq(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
			criteria2.setRestriction(Restrictions.and(r21, r22, r23, r24));
			List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria2);
			if (tmsContainerBillDetailModels.size() <= 0) {
				containerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_GH);
				containerBillHeaderModel.setUpdateperson(user.getNickname());
				containerBillHeaderModel.setUpdatetime(date);
				tmsContainerBillHeaderMapper.update(containerBillHeaderModel);
			}
		}
	}

	/**
	 * 容器归还单查询
	 */
	@Override
	public Page<ContoinerBillReturnPageDTO> pageContoinerBillReturnData(ContoinerBillReturnPageVO contoinerBillReturnPageVO) {
		ToolKits.isParamNull(contoinerBillReturnPageVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillReturnPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(contoinerBillReturnPageVO.getPageSize(), "每页条数不为空");

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

		contoinerBillReturnPageVO.setUserType(user.getUserType());
		contoinerBillReturnPageVO.setEnterpriseId(user.getEnterpriseId());

		if (ToolKits.isNotEmpty(contoinerBillReturnPageVO.getShipperName())) {
			contoinerBillReturnPageVO.setShipperNames(contoinerBillReturnPageVO.getShipperName().split(","));
		}

		// 翻译字典
		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.CONTAINER_BILL_STATUS, DataDictionaryConstant.CONTAINER_BILL_ABNORMAL_STATUS });

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillReturnPageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillReturnPageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		// 查询
		PageUtils.startPage(contoinerBillReturnPageVO.getPageNum(), contoinerBillReturnPageVO.getPageSize());
		List<ContoinerBillReturnPageDTO> contoinerBillReturnPageDTOs = tmsContainerBillHeaderMapper.contoinerBillReturnSql(contoinerBillReturnPageVO);
		for (ContoinerBillReturnPageDTO contoinerBillReturnPageDTO : contoinerBillReturnPageDTOs) {
			// 类型翻译
			contoinerBillReturnPageDTO.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_BILL_ABNORMAL_STATUS, String.valueOf(contoinerBillReturnPageDTO.getAbnormalStatus())));
			contoinerBillReturnPageDTO.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_BILL_STATUS, String.valueOf(contoinerBillReturnPageDTO.getStatus())));
		}

		return PageUtils.getPage(contoinerBillReturnPageDTOs);
	}

	/**
	 * 容器归还单统计查询
	 */
	@Override
	public ContoinerBillReturnCountDTO countContoinerBillReturnData(ContoinerBillReturnPageVO contoinerBillReturnPageVO) {
		ToolKits.isParamNull(contoinerBillReturnPageVO, "请求不为空！");

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

		contoinerBillReturnPageVO.setUserType(user.getUserType());
		contoinerBillReturnPageVO.setEnterpriseId(user.getEnterpriseId());

		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			} else {
				contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
				contoinerBillReturnPageVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_KH)) {
			// 客户ID
			contoinerBillReturnPageVO.setCustomer(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			contoinerBillReturnPageVO.setDepts(user.getDepts().stream().toArray(String[]::new));
		} else {
			throw new BizException("当前人员没有操作权限！");
		}

		List<ContoinerBillReturnPageDTO> contoinerBillReturnPageDTOs = tmsContainerBillHeaderMapper.contoinerBillReturnSql(contoinerBillReturnPageVO);

		// 响应对象
		ContoinerBillReturnCountDTO contoinerBillReturnCountDTO = new ContoinerBillReturnCountDTO();
		contoinerBillReturnCountDTO.setContainersNumbers(contoinerBillReturnPageDTOs.stream().map(ContoinerBillReturnPageDTO::getContainersNumber).reduce(BigDecimal.ZERO, BigDecimal::add));
		contoinerBillReturnCountDTO.setPalletsNumbers(contoinerBillReturnPageDTOs.stream().map(ContoinerBillReturnPageDTO::getPalletsNumber).reduce(BigDecimal.ZERO, BigDecimal::add));
		contoinerBillReturnCountDTO.setTotesNumbers(contoinerBillReturnPageDTOs.stream().map(ContoinerBillReturnPageDTO::getTotesNumber).reduce(BigDecimal.ZERO, BigDecimal::add));

		return contoinerBillReturnCountDTO;
	}

	/**
	 * 容器归还单明细查询
	 */
	@Override
	public List<ContoinerBillReturnDetailsListDTO> listContoinerBillReturnDetailsData(ContoinerBillReturnDetailsListVO contoinerBillReturnDetailsListVO) {
		ToolKits.isParamNull(contoinerBillReturnDetailsListVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillReturnDetailsListVO.getContainerBillNo(), "容器归还单号不为空");

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

		// 翻译字典
		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.CONTAINER_TYPE });

		Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
		Restriction r1 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillNo, contoinerBillReturnDetailsListVO.getContainerBillNo());
		Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_GHD);
		Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r4 = Restrictions.ge(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_GH);
		Restriction r5 = Restrictions.le(TmsContainerBillDetailModel::getStatus, DataDictionaryConstant.CONTAINER_BILL_STATUS_YSD);
		Restriction r6 = ToolKits.isEmpty(contoinerBillReturnDetailsListVO.getContainerCode()) ? null : Restrictions.like(TmsContainerBillDetailModel::getContainerCode, "%" + contoinerBillReturnDetailsListVO.getContainerCode() + "%");
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6));
		List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);

		List<ContoinerBillReturnDetailsListDTO> contoinerBillReturnDetailsListDTOs = Lists.newArrayList();
		for (TmsContainerBillDetailModel tmsContainerBillDetailModel : tmsContainerBillDetailModels) {
			ContoinerBillReturnDetailsListDTO contoinerBillReturnDetailsListDTO = new ContoinerBillReturnDetailsListDTO();
			BeanUtils.copyProperties(tmsContainerBillDetailModel, contoinerBillReturnDetailsListDTO);
			// 容器类型翻译
			contoinerBillReturnDetailsListDTO.setContainerTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONTAINER_TYPE, String.valueOf(contoinerBillReturnDetailsListDTO.getContainerType())));

			contoinerBillReturnDetailsListDTOs.add(contoinerBillReturnDetailsListDTO);
		}

		return contoinerBillReturnDetailsListDTOs;
	}

	/**
	 * 容器归还单取消
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancelContoinerBillReturnData(List<ContoinerBillReturnCancelVO> contoinerBillReturnCancelVOs) {
		ToolKits.isParamNull(contoinerBillReturnCancelVOs, "请求不为空！");

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

		Assert.isTrue(user.getUserType().equals(BussinessConstant.USER_TYPE_KH), "当前用户没有取消权限");

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

		// 存放容器随车单号
		Set<String> sets = Sets.newHashSet();

		for (ContoinerBillReturnCancelVO contoinerBillReturnCancelVO : contoinerBillReturnCancelVOs) {
			ToolKits.isParamNull(contoinerBillReturnCancelVO.getContainerBillNo(), "容器归还单号不为空");

			// 容器归还单对象
			TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(contoinerBillReturnCancelVO.getContainerBillNo(), TmsContainerBillHeaderModel.class);
			Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillHeaderModel), "容器归还单号【" + contoinerBillReturnCancelVO.getContainerBillNo() + "】不存在");

			// 判断是否已经取消
			Assert.isTrue(tmsContainerBillHeaderModel.getStatus().intValue() != DataDictionaryConstant.CONTAINER_BILL_STATUS_QX.intValue(), "容器归还单【" + contoinerBillReturnCancelVO.getContainerBillNo() + "】已取消");

			Assert.isTrue(tmsContainerBillHeaderModel.getStatus().intValue() < DataDictionaryConstant.CONTAINER_BILL_STATUS_HT.intValue(), "容器归还单【" + contoinerBillReturnCancelVO.getContainerBillNo() + "】已回途，不能取消");

			// 取消主单
			tmsContainerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_QX);
			tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
			tmsContainerBillHeaderModel.setUpdatetime(date);
			tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);

			// 取消明细
			Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
			Restriction r1 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillNo, tmsContainerBillHeaderModel.getContainerBillNo());
			Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, tmsContainerBillHeaderModel.getContainerBillType());
			Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
			criteria.setRestriction(Restrictions.and(r1, r2, r3));
			List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);
			for (TmsContainerBillDetailModel tmsContainerBillDetailModel : tmsContainerBillDetailModels) {
				tmsContainerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_QX);
				tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
				tmsContainerBillDetailModel.setUpdatetime(date);
				tmsContainerBillDetailMapper.update(tmsContainerBillDetailModel);

				// 记录容器日志
				TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
				tmsContainerBillLogModel.setId(ToolKits.getUUID());
				tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
				tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
				tmsContainerBillLogModel.setOperateDate(date);
				tmsContainerBillLogModel.setOperateEvent("容器归还取消");
				tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
				tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
				tmsContainerBillLogModel.setCreateperson(user.getNickname());
				tmsContainerBillLogModel.setCreatetime(date);
				tmsContainerBillLogModel.setUpdateperson(user.getNickname());
				tmsContainerBillLogModel.setUpdatetime(date);
				tmsContainerBillLogMapper.save(tmsContainerBillLogModel);

				// 对应的随车单明细状态更新为在店
				TmsContainerBillDetailModel containerBillDetailModel = tmsContainerBillDetailMapper.findById(tmsContainerBillDetailModel.getRelevancyId(), TmsContainerBillDetailModel.class);
				containerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
				containerBillDetailModel.setUpdateperson(user.getNickname());
				containerBillDetailModel.setUpdatetime(date);
				tmsContainerBillDetailMapper.update(containerBillDetailModel);

				// 赋值容器随车单
				sets.add(tmsContainerBillDetailModel.getRelevancyBillNo());
			}
		}

		// 容器归还单取消,对应容器随车单状态更新为在店
		for (String billno : sets) {
			// 容器随车单对象
			TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(billno, TmsContainerBillHeaderModel.class);

			tmsContainerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
			tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
			tmsContainerBillHeaderModel.setUpdatetime(date);
			tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);
		}
	}

	/**
	 * 容器归还单修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyContoinerBillReturnData(ContoinerBillReturnModifyVO contoinerBillReturnModifyVO) {
		ToolKits.isParamNull(contoinerBillReturnModifyVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillReturnModifyVO.getContainerBillNo(), "容器归还单号不为空");

		if (ToolKits.isNotEmpty(contoinerBillReturnModifyVO.getIds())) {
			ToolKits.isParamNull(contoinerBillReturnModifyVO.getDriverName(), "司机名称不为空");
			ToolKits.isParamNull(contoinerBillReturnModifyVO.getDriverPhoneNumber(), "司机手机号不为空");
			ToolKits.isParamNull(contoinerBillReturnModifyVO.getVehiclePlateNumber(), "车牌号不为空");
		}

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

		Assert.isTrue(user.getUserType().equals(BussinessConstant.USER_TYPE_KH), "当前用户没有修改权限");

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

		// 存放容器随车单号
		Set<String> sets = Sets.newHashSet();

		// 查询容器归还单明细
		Criteria criteria = Criteria.forClass(TmsContainerBillDetailModel.class);
		Restriction r1 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillNo, contoinerBillReturnModifyVO.getContainerBillNo());
		Restriction r2 = Restrictions.eq(TmsContainerBillDetailModel::getContainerBillType, DataDictionaryConstant.CONTAINER_BILL_TYPE_GHD);
		Restriction r3 = Restrictions.eq(TmsContainerBillDetailModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsContainerBillDetailModel> tmsContainerBillDetailModels = tmsContainerBillDetailMapper.findByCriteria(criteria);

		// ids>0 部分删除,ids<=0全部删除
		if (contoinerBillReturnModifyVO.getIds().size() > 0) {
			// 删除的托盘数量
			BigDecimal delPalletsNumber = BigDecimal.ZERO;
			// 删除的周转箱数量
			BigDecimal delTotesNumber = BigDecimal.ZERO;

			for (TmsContainerBillDetailModel tmsContainerBillDetailModel : tmsContainerBillDetailModels) {
				if (!contoinerBillReturnModifyVO.getIds().contains(tmsContainerBillDetailModel.getId())) {
					Assert.isTrue(tmsContainerBillDetailModel.getStatus().intValue() < DataDictionaryConstant.CONTAINER_BILL_STATUS_HT.intValue(), "容器归还单容器【" + tmsContainerBillDetailModel.getContainerCode() + "】已回途，不能修改");

					// 删除容器归还单明细
					tmsContainerBillDetailMapper.deleteById(tmsContainerBillDetailModel.getId(), TmsContainerBillDetailModel.class);

					if (tmsContainerBillDetailModel.getContainerType().intValue() == DataDictionaryConstant.CONTAINER_TYPE_1.intValue()) {
						delPalletsNumber = delPalletsNumber.add(new BigDecimal("1"));
					} else {
						delTotesNumber = delTotesNumber.add(new BigDecimal("1"));
					}

					// 更新容器随车单明细为到店,后续可继续操作容器归还
					// 容器随车单明细对象
					TmsContainerBillDetailModel containerBillDetailModel = tmsContainerBillDetailMapper.findById(tmsContainerBillDetailModel.getRelevancyId(), TmsContainerBillDetailModel.class);
					containerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
					containerBillDetailModel.setUpdateperson(user.getNickname());
					containerBillDetailModel.setUpdatetime(date);
					tmsContainerBillDetailMapper.update(containerBillDetailModel);

					// 记录容器日志
					TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
					tmsContainerBillLogModel.setId(ToolKits.getUUID());
					tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
					tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
					tmsContainerBillLogModel.setOperateDate(date);
					tmsContainerBillLogModel.setOperateEvent("容器归还修改");
					tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
					tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
					tmsContainerBillLogModel.setCreateperson(user.getNickname());
					tmsContainerBillLogModel.setCreatetime(date);
					tmsContainerBillLogModel.setUpdateperson(user.getNickname());
					tmsContainerBillLogModel.setUpdatetime(date);
					tmsContainerBillLogMapper.save(tmsContainerBillLogModel);

					// 赋值容器随车单
					sets.add(tmsContainerBillDetailModel.getRelevancyBillNo());
				}
			}

			// 更新相应的数量信息
			TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(contoinerBillReturnModifyVO.getContainerBillNo(), TmsContainerBillHeaderModel.class);
			tmsContainerBillHeaderModel.setDriverId(contoinerBillReturnModifyVO.getDriverId());
			tmsContainerBillHeaderModel.setDriverCode(contoinerBillReturnModifyVO.getDriverCode());
			tmsContainerBillHeaderModel.setDriverName(contoinerBillReturnModifyVO.getDriverName());
			tmsContainerBillHeaderModel.setDriverPhoneNumber(contoinerBillReturnModifyVO.getDriverPhoneNumber());
			tmsContainerBillHeaderModel.setVehicleId(contoinerBillReturnModifyVO.getVehicleId());
			tmsContainerBillHeaderModel.setVehiclePlateNumber(contoinerBillReturnModifyVO.getVehiclePlateNumber());
			tmsContainerBillHeaderModel.setPalletsNumber(tmsContainerBillHeaderModel.getPalletsNumber().subtract(delPalletsNumber));
			tmsContainerBillHeaderModel.setTotesNumber(tmsContainerBillHeaderModel.getTotesNumber().subtract(delTotesNumber));
			tmsContainerBillHeaderModel.setContainersNumber(tmsContainerBillHeaderModel.getPalletsNumber().add(tmsContainerBillHeaderModel.getTotesNumber()));
			tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
			tmsContainerBillHeaderModel.setUpdatetime(date);
			tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);
		} else {
			// 删除容器归还单
			tmsContainerBillHeaderMapper.deleteById(contoinerBillReturnModifyVO.getContainerBillNo(), TmsContainerBillHeaderModel.class);

			// 删除容器归还单明细
			for (TmsContainerBillDetailModel tmsContainerBillDetailModel : tmsContainerBillDetailModels) {
				Assert.isTrue(tmsContainerBillDetailModel.getStatus().intValue() < DataDictionaryConstant.CONTAINER_BILL_STATUS_HT.intValue(), "容器归还单容器【" + tmsContainerBillDetailModel.getContainerCode() + "】已回途，不能修改");

				// 删除容器归还单明细
				tmsContainerBillDetailMapper.deleteById(tmsContainerBillDetailModel.getId(), TmsContainerBillDetailModel.class);

				// 更新容器随车单明细为到店,后续可继续操作容器归还
				// 容器随车单明细对象
				TmsContainerBillDetailModel containerBillDetailModel = tmsContainerBillDetailMapper.findById(tmsContainerBillDetailModel.getRelevancyId(), TmsContainerBillDetailModel.class);
				containerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
				containerBillDetailModel.setUpdateperson(user.getNickname());
				containerBillDetailModel.setUpdatetime(date);
				tmsContainerBillDetailMapper.update(containerBillDetailModel);

				// 记录容器日志
				TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
				tmsContainerBillLogModel.setId(ToolKits.getUUID());
				tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
				tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
				tmsContainerBillLogModel.setOperateDate(date);
				tmsContainerBillLogModel.setOperateEvent("容器归还修改");
				tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
				tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
				tmsContainerBillLogModel.setCreateperson(user.getNickname());
				tmsContainerBillLogModel.setCreatetime(date);
				tmsContainerBillLogModel.setUpdateperson(user.getNickname());
				tmsContainerBillLogModel.setUpdatetime(date);
				tmsContainerBillLogMapper.save(tmsContainerBillLogModel);

				// 赋值容器随车单
				sets.add(tmsContainerBillDetailModel.getRelevancyBillNo());
			}
		}

		// 容器归还明细删除,对应容器随车明细的主单状态更新为在店
		for (String billno : sets) {
			// 容器随车单对象
			TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(billno, TmsContainerBillHeaderModel.class);

			tmsContainerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_ZD);
			tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
			tmsContainerBillHeaderModel.setUpdatetime(date);
			tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);
		}
	}

	/**
	 * 容器归还单签收
	 */
	@Override
	public void receiptedContoinerBillReturnData(ContoinerBillReturnReceiptedVO contoinerBillReturnReceiptedVO) {
		ToolKits.isParamNull(contoinerBillReturnReceiptedVO, "请求不为空！");
		ToolKits.isParamNull(contoinerBillReturnReceiptedVO.getContainerBillNo(), "容器归还单号不为空");
		ToolKits.isParamNull(contoinerBillReturnReceiptedVO.getDetails(), "签收明细不为空");

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

		Assert.isTrue(user.getUserType().equals(BussinessConstant.USER_TYPE_ZY), "当前用户没有修改权限");

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

		// 容器归还单对象
		TmsContainerBillHeaderModel tmsContainerBillHeaderModel = tmsContainerBillHeaderMapper.findById(contoinerBillReturnReceiptedVO.getContainerBillNo(), TmsContainerBillHeaderModel.class);
		Assert.isTrue(ToolKits.isNotEmpty(tmsContainerBillHeaderModel), "容器归还单号【" + contoinerBillReturnReceiptedVO.getContainerBillNo() + "】不存在");

		// 签收明细
		List<ContoinerBillReturnReceiptedDetailsVO> contoinerBillReturnReceiptedDetailsVOs = contoinerBillReturnReceiptedVO.getDetails();
		for (ContoinerBillReturnReceiptedDetailsVO contoinerBillReturnReceiptedDetailsVO : contoinerBillReturnReceiptedDetailsVOs) {
			// 判断明细id是否为空，为空说明是多货
			if (ToolKits.isEmpty(contoinerBillReturnReceiptedDetailsVO.getId())) {
				// 获取包裹ID
				String packageId = mainDataProxy.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_PACKAGE_ID);

				TmsContainerBillDetailModel tmsContainerBillDetailModel = new TmsContainerBillDetailModel();
				tmsContainerBillDetailModel.setId(ToolKits.getUUID());
				tmsContainerBillDetailModel.setContainerBillNo(tmsContainerBillHeaderModel.getContainerBillNo());
				tmsContainerBillDetailModel.setContainerBillType(tmsContainerBillHeaderModel.getContainerBillType());
				tmsContainerBillDetailModel.setContainerId(Long.valueOf(packageId));
				tmsContainerBillDetailModel.setContainerCode(contoinerBillReturnReceiptedDetailsVO.getContainerCode());
				tmsContainerBillDetailModel.setContainerName(contoinerBillReturnReceiptedDetailsVO.getContainerCode());
				tmsContainerBillDetailModel.setContainerType(contoinerBillReturnReceiptedDetailsVO.getContainerType());
				tmsContainerBillDetailModel.setContainerPackageType(null);
				tmsContainerBillDetailModel.setMemo(contoinerBillReturnReceiptedDetailsVO.getMemo());
				tmsContainerBillDetailModel.setAbnormalStatus(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_DH);
				tmsContainerBillDetailModel.setAbnormalMemo(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_DH + " " + (ToolKits.isEmpty(contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()) ? "" : contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()));
				tmsContainerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_DC);
				tmsContainerBillDetailModel.setEnterpriseId(user.getEnterpriseId());
				tmsContainerBillDetailModel.setEnterpriseName(user.getEnterpriseName());
				tmsContainerBillDetailModel.setCreatetime(date);
				tmsContainerBillDetailModel.setCreateperson(user.getNickname());
				tmsContainerBillDetailModel.setUpdatetime(date);
				tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
				tmsContainerBillDetailMapper.save(tmsContainerBillDetailModel);

				// 记录容器日志
				TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
				tmsContainerBillLogModel.setId(ToolKits.getUUID());
				tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
				tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
				tmsContainerBillLogModel.setOperateDate(date);
				tmsContainerBillLogModel.setOperateEvent(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_DH);
				tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
				tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
				tmsContainerBillLogModel.setCreateperson(user.getNickname());
				tmsContainerBillLogModel.setCreatetime(date);
				tmsContainerBillLogModel.setUpdateperson(user.getNickname());
				tmsContainerBillLogModel.setUpdatetime(date);
				tmsContainerBillLogMapper.save(tmsContainerBillLogModel);
			} else {
				// 归还单明细对象
				TmsContainerBillDetailModel tmsContainerBillDetailModel = tmsContainerBillDetailMapper.findById(contoinerBillReturnReceiptedDetailsVO.getId(), TmsContainerBillDetailModel.class);
				tmsContainerBillDetailModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_DC);

				if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillDetailModel.setAbnormalMemo(null);
				} else if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_QH.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillDetailModel.setAbnormalMemo(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_QH);
				} else if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_DS.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillDetailModel.setAbnormalMemo(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_DS + " " + (ToolKits.isEmpty(contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()) ? "" : contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()));
				} else {
					tmsContainerBillDetailModel.setAbnormalMemo(contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo());
				}

				tmsContainerBillDetailModel.setAbnormalStatus(contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus());
				tmsContainerBillDetailModel.setUpdatetime(date);
				tmsContainerBillDetailModel.setUpdateperson(user.getNickname());
				tmsContainerBillDetailMapper.update(tmsContainerBillDetailModel);

				// 记录容器日志
				TmsContainerBillLogModel tmsContainerBillLogModel = new TmsContainerBillLogModel();
				tmsContainerBillLogModel.setId(ToolKits.getUUID());
				tmsContainerBillLogModel.setContainerBillNo(tmsContainerBillDetailModel.getContainerBillNo());
				tmsContainerBillLogModel.setContainerId(tmsContainerBillDetailModel.getContainerId());
				tmsContainerBillLogModel.setOperateDate(date);
				if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_ZC.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillLogModel.setOperateEvent(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_DC);
				} else if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_QH.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillLogModel.setOperateEvent(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_QH);
				} else if (DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_DS.intValue() == contoinerBillReturnReceiptedDetailsVO.getAbnormalStatus().intValue()) {
					tmsContainerBillLogModel.setOperateEvent(DataDictionaryConstant.CONTAINER_ABNORMAL_STATUS_NAME_DS + " " + (ToolKits.isEmpty(contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()) ? "" : contoinerBillReturnReceiptedDetailsVO.getAbnormalMemo()));
				}
				tmsContainerBillLogModel.setEnterpriseId(user.getEnterpriseId());
				tmsContainerBillLogModel.setEnterpriseName(user.getEnterpriseName());
				tmsContainerBillLogModel.setCreateperson(user.getNickname());
				tmsContainerBillLogModel.setCreatetime(date);
				tmsContainerBillLogModel.setUpdateperson(user.getNickname());
				tmsContainerBillLogModel.setUpdatetime(date);
				tmsContainerBillLogMapper.save(tmsContainerBillLogModel);
			}
		}

		// 更新容器归还单的状态
		tmsContainerBillHeaderModel.setStatus(DataDictionaryConstant.CONTAINER_BILL_STATUS_DC);
		tmsContainerBillHeaderModel.setUpdateperson(user.getNickname());
		tmsContainerBillHeaderModel.setUpdatetime(date);
		tmsContainerBillHeaderMapper.update(tmsContainerBillHeaderModel);
	}

}
