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.Properties;
import java.util.stream.Collectors;

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

import com.google.common.collect.Lists;
import com.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.TmsBaseBranchMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCargoOwnerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseItemCategoryMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseItemMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StandardTemplateDetailsListDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemByCargoOwnerPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemImportPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemUnitDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseBranchModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCargoOwnerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseItemCategoryModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseItemModel;
import com.prolog.rdc.tms.model.vo.IntfItemSaveVO;
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.ItemAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemBatchDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemByCargoOwnerPageVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemByIdVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCopyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ItemImportVO;
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.ItemService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import com.prolog.rdc.tms.util.FileUtils;
import com.prolog.rdc.tms.util.ImportUtils;
import com.prolog.rdc.tms.util.PublicMethods;
import com.prolog.wms.base.util.ExcelUtils;
import com.prolog.wms.base.util.JsonMapper;

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

/**
 *
 *
 * 基础数据-商品管理实现层
 *
 * @className :ItemServiceImpl
 * @author :fanzw
 * @creationTime :2022/02/25
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/02/25
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TmsBaseItemMapper tmsBaseItemMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@Autowired
	private TmsBaseCargoOwnerMapper tmsBaseCargoOwnerMapper;

	@Autowired
	private TmsBaseItemCategoryMapper tmsBaseItemCategoryMapper;

	@Autowired
	private UserFeign userFeign;

	@Autowired
	private AmqpTemplate amqpTemplate;

	@Autowired
	private TmsBaseBranchMapper tmsBaseBranchMapper;

	@Autowired
	private UserFeign userTmsResource;

	/**
	 * 商品资料分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<ItemPagingQueryDTO> pageItemData(ItemPagingQueryVO itemPagingQueryVO) {
		ToolKits.isParamNull(itemPagingQueryVO, "商品资料查询请求不为空");
		ToolKits.isParamNull(itemPagingQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(itemPagingQueryVO.getPageSize(), "页数不为空");

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

		List<String> params = Lists.newArrayList(DataDictionaryConstant.PACK_TYPE, // 包装类型
				DataDictionaryConstant.MAINDATA_STATUS, // 商品状态
				DataDictionaryConstant.CARGO_THERMOSPHERE, //货物温层
				DataDictionaryConstant.ITEM_DOSE_TYPE // 商品剂型
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 start
		itemPagingQueryVO.setEnterpriseId(user.getEnterpriseId());
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			itemPagingQueryVO.setIsDept("Y");
			// 判断是否为部门权限
			if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				itemPagingQueryVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				itemPagingQueryVO.setIsData("Y");
				itemPagingQueryVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				itemPagingQueryVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			// 显示所属货主为当前登录账号单位id的商品
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			itemPagingQueryVO.setCargoOwnerIds(ownerIds);
			itemPagingQueryVO.setIsData("Y");
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}
		// 分页
		PageUtils.startPage(itemPagingQueryVO.getPageNum(), itemPagingQueryVO.getPageSize());
		List<ItemPagingQueryDTO> tmsBaseItemModels = tmsBaseItemMapper.pageItemData(itemPagingQueryVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 end
		for (ItemPagingQueryDTO tmsBaseItemModel : tmsBaseItemModels) {
			// 响应对象
			tmsBaseItemModel.setPackTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.PACK_TYPE, String.valueOf(tmsBaseItemModel.getPackType())));
			tmsBaseItemModel.setCargoThermosphereName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARGO_THERMOSPHERE, String.valueOf(tmsBaseItemModel.getCargoThermosphere())));
			tmsBaseItemModel.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.MAINDATA_STATUS, String.valueOf(tmsBaseItemModel.getStatus())));
			tmsBaseItemModel.setDoseTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ITEM_DOSE_TYPE,String.valueOf(tmsBaseItemModel.getDoseType())));
		}

		return PageUtils.getPage(tmsBaseItemModels);
	}

	/**
	 * 商品资料查看
	 */
	@Override
	public ItemCheckDTO getItemData(ItemCheckVO itemCheckVO) {
		ToolKits.isParamNull(itemCheckVO, "商品资料查看请求不为空");
		ToolKits.isParamNull(itemCheckVO.getItemId(), "商品ID不为空");

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

		// 商品状态
		List<Map<String, Object>> restMessage = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);
		List<Map<String, Object>> itemDoseTypeMap = sysRefdatadService.getDropDownById(DataDictionaryConstant.ITEM_DOSE_TYPE, null, null, null);

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = ToolKits.isEmpty(itemCheckVO.getItemId()) ? null : Restrictions.eq("itemId", itemCheckVO.getItemId());
		Restriction r3 = ToolKits.isEmpty(itemCheckVO.getItemCode()) ? null : Restrictions.eq(TmsBaseItemModel::getItemCode, itemCheckVO.getItemCode());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);
		Assert.isTrue(tmsBaseItemModels.size() > 0, "商品ID【" + itemCheckVO.getItemId() + "】不存在，查询失败");
		Assert.isTrue(tmsBaseItemModels.size() == 1, "商品ID【" + itemCheckVO.getItemId() + "】存在重复数据，查询失败");

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = tmsBaseItemModels.get(0);

		// 响应对象
		ItemCheckDTO ttemCheckDTO = new ItemCheckDTO();
		BeanUtils.copyProperties(tmsBaseItemModel, ttemCheckDTO);
		ttemCheckDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(ttemCheckDTO.getStatus())));
		ttemCheckDTO.setDoseTypeName(PublicMethods.change(itemDoseTypeMap, String.valueOf(ttemCheckDTO.getDoseType())));

		return ttemCheckDTO;
	}

	/**
	 * 商品资料弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<ItemPopupDTO> popupItemData(ItemPopupVO itemPopupVO) {
		ToolKits.isParamNull(itemPopupVO, "商品资料弹窗请求不为空");
		ToolKits.isParamNull(itemPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(itemPopupVO.getPageSize(), "页数不为空");

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

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

		// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 start
		itemPopupVO.setEnterpriseId(user.getEnterpriseId());
		itemPopupVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			itemPopupVO.setIsDept("Y");
			// 判断是否为部门权限
			if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				itemPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				itemPopupVO.setIsData("Y");
				itemPopupVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				itemPopupVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			// 显示所属货主为当前登录账号单位id的商品
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			itemPopupVO.setCargoOwnerIds(ownerIds);
			itemPopupVO.setIsData("Y");
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}
		// 分页
		PageUtils.startPage(itemPopupVO.getPageNum(), itemPopupVO.getPageSize());
		List<ItemPopupDTO> tmsBaseItemModels = tmsBaseItemMapper.popupItemData(itemPopupVO);
		// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 end

		return PageUtils.getPage(tmsBaseItemModels);
	}

	/**
	 * 商品资料根据货主分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<ItemByCargoOwnerPageDTO> pageItemByCargoOwnerData(ItemByCargoOwnerPageVO itemByCargoOwnerPageVO) {
		ToolKits.isParamNull(itemByCargoOwnerPageVO, "商品资料根据货主分页查询请求不为空");
		ToolKits.isParamNull(itemByCargoOwnerPageVO.getCargoOwnerId(), "货主ID不为空");
		ToolKits.isParamNull(itemByCargoOwnerPageVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(itemByCargoOwnerPageVO.getPageSize(), "页数不为空");

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

		List<String> params = Lists.newArrayList(DataDictionaryConstant.PACK_TYPE, // 包装类型
				DataDictionaryConstant.CARGO_THERMOSPHERE //货物温层
		);
		Map<String, Object> dicData = ConvertorUtil.listDropDownData(sysRefdatadService, params);

		itemByCargoOwnerPageVO.setEnterpriseId(user.getEnterpriseId());
		itemByCargoOwnerPageVO.setStatus(StatusConstant.STATUS_ENABLE);
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			itemByCargoOwnerPageVO.setIsDept("Y");
			// 判断是否为部门权限
			if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				itemByCargoOwnerPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
			} else {
				itemByCargoOwnerPageVO.setIsData("Y");
				itemByCargoOwnerPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
				itemByCargoOwnerPageVO.setCargoOwnerIds(user.getConsignors().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			// 显示所属货主为当前登录账号单位id的商品
			Long[] ownerIds = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			itemByCargoOwnerPageVO.setCargoOwnerIds(ownerIds);
			itemByCargoOwnerPageVO.setIsData("Y");
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}
		// 分页
		PageUtils.startPage(itemByCargoOwnerPageVO.getPageNum(), itemByCargoOwnerPageVO.getPageSize());
		List<ItemByCargoOwnerPageDTO> tmsBaseItemModels = tmsBaseItemMapper.pageItemByCargoOwnerData(itemByCargoOwnerPageVO);

		for (ItemByCargoOwnerPageDTO itemByCargoOwnerPageDTO : tmsBaseItemModels) {
			// 响应对象
			itemByCargoOwnerPageDTO.setPackTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.PACK_TYPE, String.valueOf(itemByCargoOwnerPageDTO.getPackType())));
			itemByCargoOwnerPageDTO.setCargoThermosphereName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARGO_THERMOSPHERE, String.valueOf(itemByCargoOwnerPageDTO.getCargoThermosphere())));
			itemByCargoOwnerPageDTO.setHeight(itemByCargoOwnerPageDTO.getHeigth());
		}

		return PageUtils.getPage(tmsBaseItemModels);
	}

	/**
	 * 商品资料新增
	 */
	@Override
	public void addItemData(ItemAddVO itemAddVO) {
		ToolKits.isParamNull(itemAddVO, "商品资料新增请求不为空");
		ToolKits.isParamNull(itemAddVO.getItemCode(), "商品编码不为空");
		ToolKits.isParamNull(itemAddVO.getItemName(), "商品名称不为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerId(), "货主ID不能为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerCode(), "货主编号不能为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerName(), "货主名称不能为空");

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

		Map<String, Object> map = Maps.newHashMap();
		map.put("itemCode", itemAddVO.getItemCode());
		map.put("enterpriseId", user.getEnterpriseId());
		map.put("cargoOwnerId", itemAddVO.getCargoOwnerId());
		map.put("status", StatusConstant.STATUS_ENABLE);

		// 判断商品编码 商品名称 是否已存在
		PublicMethods.doesItExistByMap(tmsBaseItemMapper, map, TmsBaseItemModel.class, "货主[" + itemAddVO.getCargoOwnerName() + "]商品编码[" + itemAddVO.getItemCode() + "]已存在");

		// 判断商品编码 商品名称 是否已停用
		map.put("status", StatusConstant.STATUS_DISABLE);
		PublicMethods.doesItExistByMap(tmsBaseItemMapper, map, TmsBaseItemModel.class, "货主[" + itemAddVO.getCargoOwnerName() + "]商品编码[" + itemAddVO.getItemCode() + "]已停用,请直接启用");

		// 商品ID
		String itemId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_ID, "L");
		ToolKits.isParamNull(itemId, "商品ID生成失败");

		if (ToolKits.isNotEmpty(itemAddVO.getTemperatureCtlUpperLimit()) && ToolKits.isNotEmpty(itemAddVO.getTemperatureCtlLowerLimit())) {
			if (NumberUtil.isGreater(itemAddVO.getTemperatureCtlLowerLimit(), itemAddVO.getTemperatureCtlUpperLimit())) {
				throw new PrologException("温控区间的下限值不能大于上限值");
			}
		}

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

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = new TmsBaseItemModel();
		BeanUtils.copyProperties(itemAddVO, tmsBaseItemModel);

		tmsBaseItemModel.setOrganizationId(user.getOrganizationId());
		tmsBaseItemModel.setOrganizationCode(String.valueOf(user.getOrganizationId()));
		tmsBaseItemModel.setOrganizationName(user.getOrgnaizationName());
		tmsBaseItemModel.setDepartmentId(itemAddVO.getDepartmentId());
		tmsBaseItemModel.setDepartmentCode(itemAddVO.getDepartmentCode());
		tmsBaseItemModel.setDepartmentName(itemAddVO.getDepartmentName());
		tmsBaseItemModel.setVolume(itemAddVO.getVolume() == null ? BigDecimal.ZERO : itemAddVO.getVolume());
		tmsBaseItemModel.setWeight(itemAddVO.getWeight() == null ? BigDecimal.ZERO : itemAddVO.getWeight());
		tmsBaseItemModel.setItemId(Long.valueOf(itemId));
		tmsBaseItemModel.setStatus(StatusConstant.STATUS_ENABLE);
		tmsBaseItemModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseItemModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseItemModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseItemModel.setCreateperson(user.getNickname());
		tmsBaseItemModel.setCreatetime(date);
		tmsBaseItemModel.setUpdateperson(user.getNickname());
		tmsBaseItemModel.setUpdatetime(date);
		tmsBaseItemMapper.save(tmsBaseItemModel);
	}

	/**
	 * 商品资料复制
	 */
	@Override
	public void copyItemData(ItemCopyVO itemCopyVO) {
		ToolKits.isParamNull(itemCopyVO, "商品资料复制请求不为空");
		ToolKits.isParamNull(itemCopyVO.getItemCode(), "商品编码不为空");
		ToolKits.isParamNull(itemCopyVO.getItemName(), "商品名称不为空");
		ToolKits.isParamNull(itemCopyVO.getCargoOwnerId(), "货主ID不能为空");
		ToolKits.isParamNull(itemCopyVO.getCargoOwnerCode(), "货主编号不能为空");
		ToolKits.isParamNull(itemCopyVO.getCargoOwnerName(), "货主名称不能为空");

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

		Map<String, Object> map = Maps.newHashMap();
		map.put("itemCode", itemCopyVO.getItemCode());
		map.put("enterpriseId", user.getEnterpriseId());
		map.put("cargoOwnerId", itemCopyVO.getCargoOwnerId());
		map.put("status", StatusConstant.STATUS_ENABLE);

		// 判断商品编码 商品名称 是否已存在
		PublicMethods.doesItExistByMap(tmsBaseItemMapper, map, TmsBaseItemModel.class, "商品编码已存在");

		// 判断商品编码 商品名称 是否已停用
		map.put("status", StatusConstant.STATUS_DISABLE);
		PublicMethods.doesItExistByMap(tmsBaseItemMapper, map, TmsBaseItemModel.class, "商品编码已停用,请直接启用");

		// 商品ID
		String itemId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_ID, "L");
		ToolKits.isParamNull(itemId, "商品ID生成失败");

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

		if (ToolKits.isNotEmpty(itemCopyVO.getTemperatureCtlUpperLimit()) && ToolKits.isNotEmpty(itemCopyVO.getTemperatureCtlLowerLimit())) {
			if (NumberUtil.isGreater(itemCopyVO.getTemperatureCtlLowerLimit(), itemCopyVO.getTemperatureCtlUpperLimit())) {
				throw new PrologException("温控区间的下限值不能大于上限值");
			}
		}
		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = new TmsBaseItemModel();
		BeanUtils.copyProperties(itemCopyVO, tmsBaseItemModel);

		tmsBaseItemModel.setOrganizationId(user.getOrganizationId());
		tmsBaseItemModel.setOrganizationCode(String.valueOf(user.getOrganizationId()));
		tmsBaseItemModel.setOrganizationName(user.getOrgnaizationName());
		tmsBaseItemModel.setDepartmentId(itemCopyVO.getDepartmentId());
		tmsBaseItemModel.setDepartmentCode(itemCopyVO.getDepartmentCode());
		tmsBaseItemModel.setDepartmentName(itemCopyVO.getDepartmentName());
		tmsBaseItemModel.setVolume(itemCopyVO.getVolume() == null ? BigDecimal.ZERO : itemCopyVO.getVolume());
		tmsBaseItemModel.setWeight(itemCopyVO.getWeight() == null ? BigDecimal.ZERO : itemCopyVO.getWeight());
		tmsBaseItemModel.setItemId(Long.valueOf(itemId));
		tmsBaseItemModel.setStatus(StatusConstant.STATUS_ENABLE);
		tmsBaseItemModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseItemModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseItemModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseItemModel.setCreateperson(user.getNickname());
		tmsBaseItemModel.setCreatetime(date);
		tmsBaseItemModel.setUpdateperson(user.getNickname());
		tmsBaseItemModel.setUpdatetime(date);
		tmsBaseItemMapper.save(tmsBaseItemModel);
	}

	/**
	 * 商品资料修改
	 */
	@Override
	public void modifyItemData(ItemModifyVO itemModifyVO) {
		ToolKits.isParamNull(itemModifyVO, "商品资料修改请求不为空");
		ToolKits.isParamNull(itemModifyVO.getItemId(), "商品ID不为空");

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

		if (ToolKits.isNotEmpty(itemModifyVO.getTemperatureCtlUpperLimit()) && ToolKits.isNotEmpty(itemModifyVO.getTemperatureCtlLowerLimit())) {
			if (NumberUtil.isGreater(itemModifyVO.getTemperatureCtlLowerLimit(), itemModifyVO.getTemperatureCtlUpperLimit())) {
				throw new PrologException("温控区间的下限值不能大于上限值");
			}
		}
		// 根据ID查询商品资料
		TmsBaseItemModel tmsBaseItemModel = tmsBaseItemMapper.findById(itemModifyVO.getItemId(), TmsBaseItemModel.class);
		ToolKits.isParamNull(tmsBaseItemModel, "商品ID【" + itemModifyVO.getItemId() + "】不存在");

		Assert.isTrue(tmsBaseItemModel.getStatus() == StatusConstant.STATUS_ENABLE, "商品已停用不能修改");

		tmsBaseItemModel.setCargoOwnerId(itemModifyVO.getCargoOwnerId());
		tmsBaseItemModel.setCargoOwnerCode(itemModifyVO.getCargoOwnerCode());
		tmsBaseItemModel.setCargoOwnerName(itemModifyVO.getCargoOwnerName());
		tmsBaseItemModel.setItemCategoryId(itemModifyVO.getItemCategoryId());
		tmsBaseItemModel.setItemCategoryCode(itemModifyVO.getItemCategoryCode());
		tmsBaseItemModel.setItemCategoryName(itemModifyVO.getItemCategoryName());
		tmsBaseItemModel.setItemCategoryLevel(itemModifyVO.getItemCategoryLevel());
		tmsBaseItemModel.setPackType(itemModifyVO.getPackType());
		tmsBaseItemModel.setCargoThermosphere(itemModifyVO.getCargoThermosphere());
		tmsBaseItemModel.setLength(itemModifyVO.getLength());
		tmsBaseItemModel.setWidth(itemModifyVO.getWidth());
		tmsBaseItemModel.setHeigth(itemModifyVO.getHeigth());
		tmsBaseItemModel.setWeight(itemModifyVO.getWeight() == null ? BigDecimal.ZERO : itemModifyVO.getWeight());
		tmsBaseItemModel.setVolume(itemModifyVO.getVolume() == null ? BigDecimal.ZERO : itemModifyVO.getVolume());
		tmsBaseItemModel.setCargoValue(itemModifyVO.getCargoValue());
		tmsBaseItemModel.setStatus(itemModifyVO.getStatus());
		tmsBaseItemModel.setMemo(itemModifyVO.getMemo());
		tmsBaseItemModel.setUpdateperson(user.getNickname());
		tmsBaseItemModel.setUpdatetime(DateUtil.date());
		tmsBaseItemModel.setExtStr1(itemModifyVO.getExtStr1());
		tmsBaseItemModel.setExtStr2(itemModifyVO.getExtStr2());
		tmsBaseItemModel.setExtStr3(itemModifyVO.getExtStr3());
		tmsBaseItemModel.setExtStr4(itemModifyVO.getExtStr4());
		tmsBaseItemModel.setExtStr5(itemModifyVO.getExtStr5());
		tmsBaseItemModel.setExtNum1(itemModifyVO.getExtNum1());
		tmsBaseItemModel.setExtNum2(itemModifyVO.getExtNum2());
		tmsBaseItemModel.setExtNum3(itemModifyVO.getExtNum3());
		tmsBaseItemModel.setExtNum4(itemModifyVO.getExtNum4());
		tmsBaseItemModel.setExtNum5(itemModifyVO.getExtNum5());
		tmsBaseItemModel.setExtDate1(itemModifyVO.getExtDate1());
		tmsBaseItemModel.setExtDate2(itemModifyVO.getExtDate2());
		tmsBaseItemModel.setExtDate3(itemModifyVO.getExtDate3());
		tmsBaseItemModel.setExtDate4(itemModifyVO.getExtDate4());
		tmsBaseItemModel.setExtDate5(itemModifyVO.getExtDate5());
		tmsBaseItemModel.setBranchId(itemModifyVO.getBranchId());
		tmsBaseItemModel.setBranchCode(itemModifyVO.getBranchCode());
		tmsBaseItemModel.setBranchName(itemModifyVO.getBranchName());
		tmsBaseItemModel.setInventoryPcs(itemModifyVO.getInventoryPcs());
		tmsBaseItemModel.setStagingLocation(itemModifyVO.getStagingLocation());
		tmsBaseItemModel.setDepartmentId(itemModifyVO.getDepartmentId());
		tmsBaseItemModel.setDepartmentCode(itemModifyVO.getDepartmentCode());
		tmsBaseItemModel.setDepartmentName(itemModifyVO.getDepartmentName());
		tmsBaseItemModel.setTemperatureCtlLowerLimit(itemModifyVO.getTemperatureCtlLowerLimit());
		tmsBaseItemModel.setTemperatureCtlUpperLimit(itemModifyVO.getTemperatureCtlUpperLimit());
		tmsBaseItemModel.setSymbol(itemModifyVO.getSymbol());
		tmsBaseItemModel.setSymbolExpirationDate(itemModifyVO.getSymbolExpirationDate());
		tmsBaseItemModel.setLicensor(itemModifyVO.getLicensor());
		tmsBaseItemModel.setCapacity(itemModifyVO.getCapacity());
		tmsBaseItemModel.setPackSize(itemModifyVO.getPackSize());
		tmsBaseItemModel.setDoseType(itemModifyVO.getDoseType());
		tmsBaseItemModel.setFactory(itemModifyVO.getFactory());
		tmsBaseItemModel.setOrigin(itemModifyVO.getOrigin());
		tmsBaseItemMapper.update(tmsBaseItemModel);
	}

	/**
	 * 商品资料批量删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDeleteItemData(List<ItemBatchDeleteVO> itemBatchDeleteVOs) {
		ToolKits.isParamNull(itemBatchDeleteVOs, "商品资料批量删除请求不为空");

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

		for (ItemBatchDeleteVO itemBatchDeleteVO : itemBatchDeleteVOs) {
			ToolKits.isParamNull(itemBatchDeleteVO.getItemId(), "商品ID不为空");

			tmsBaseItemMapper.deleteById(itemBatchDeleteVO.getItemId(), TmsBaseItemModel.class);
		}
	}

	/**
	 * 商品资料是否启用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void whetherEnableItemData(ItemWhetherEnableVO itemWhetherEnableVO) {
		ToolKits.isParamNull(itemWhetherEnableVO, "商品资料是否启用请求不为空");
		ToolKits.isParamNull(itemWhetherEnableVO.getItemIds(), "商品ID集不为空");
		ToolKits.isParamNull(itemWhetherEnableVO.getStatus(), "状态不为空");

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

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

		for (ItemByIdVO itemByIdVO : itemWhetherEnableVO.getItemIds()) {
			ToolKits.isParamNull(itemByIdVO.getItemId(), "商品ID不为空");

			// 根据ID查询商品
			TmsBaseItemModel tmsBaseItemModel = tmsBaseItemMapper.findById(itemByIdVO.getItemId(), TmsBaseItemModel.class);
			ToolKits.isParamNull(tmsBaseItemModel, "商品ID【" + itemByIdVO.getItemId() + "】不存在");

			tmsBaseItemModel.setStatus(itemWhetherEnableVO.getStatus() == 10 ? StatusConstant.STATUS_ENABLE : StatusConstant.STATUS_DISABLE);
			tmsBaseItemModel.setUpdateperson(user.getNickname());
			tmsBaseItemModel.setUpdatetime(date);
			tmsBaseItemMapper.update(tmsBaseItemModel);
		}
	}

	/**
	 * 商品资料导入
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void improtItemData(MultipartFile file, String importBatchNumber) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 导入文件判断
		FileUtils.multipartFileJudge(file);
		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.ITEM_IMPORT);
		logAddVO.setImportBatchNumber(importBatchNumber);
		// 导入统计
		int count = 0;
		try {
			// 导入执行
			count = this.importProcess(file, importBatchNumber, user);
			logAddVO.setStatus(DataDictionaryConstant.IMPORT_STATUS_YES);
			logAddVO.setSuccessNum(count);
			logAddVO.setFailNum(0);
			importLogService.addImportLogData(logAddVO);
		} catch (Exception e) {
			Class<ItemImportVO> importVOClass = ItemImportVO.class;
			List<ItemImportVO> itemCategoryImportVOs = Lists.newArrayList();
			try {
				itemCategoryImportVOs = ExcelUtils.read(file, importVOClass);
			} catch (Exception e1) {
				e1.printStackTrace();
				throw new BizException("EXCEL转对象失败:" + e1.getMessage());
			}

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

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

	/**
	 * 导入过程
	 *
	 * @param file
	 * @param
	 * @param
	 * @return
	 * @throws Exception
	 */
	public int importProcess(MultipartFile file, String importBatchNumber, UserEntity user) {
		Date date = new Date();
		// 商品资料对象集合
		List<TmsBaseItemModel> tmsBaseItemModelList = Lists.newArrayList();
		Properties properties = new Properties();
		// 待转换的对象
		Class<ItemImportVO> importVOClass = ItemImportVO.class;
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.ITEM_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<ItemImportVO> itemImportVOs = new ArrayList<ItemImportVO>();
		try {
			itemImportVOs = ExcelUtils.read(file, importVOClass);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException("EXCEL转对象失败:" + e.getMessage());
		}

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

		int i = 1;
		for (ItemImportVO itemImportStdVO : itemImportVOs) {
			if (ToolKits.isEmpty(itemImportStdVO.getItemCode())) {
				throw new PrologException("第" + i + "行导入数据,商品编码不能为空");
			}
			if (ToolKits.isEmpty(itemImportStdVO.getItemName())) {
				throw new PrologException("第" + i + "行导入数据,商品名称不能为空");
			}
			if (ToolKits.isEmpty(itemImportStdVO.getCargoOwnerName())) {
				throw new PrologException("第" + i + "行导入数据,货主名称不能为空");
			}
			if (ToolKits.isEmpty(itemImportStdVO.getDepartmentName())) {
				throw new PrologException("第" + i + "行导入数据,部门名称不能为空");
			}
			if (ToolKits.isNotEmpty(itemImportStdVO.getTemperatureCtlUpperLimit()) && ToolKits.isNotEmpty(itemImportStdVO.getTemperatureCtlLowerLimit())) {
				if (NumberUtil.isGreater(itemImportStdVO.getTemperatureCtlLowerLimit(), itemImportStdVO.getTemperatureCtlUpperLimit())) {
					throw new PrologException("第" + i + "温控区间的下限值不能大于上限值");
				}
			}
			i++;
		}
		// 验证导入文件中商品编号是否重复
		Map<String, Long> codeMap = itemImportVOs.stream().collect(Collectors.groupingBy(ItemImportVO::getItemCode, Collectors.counting()));
		for (Map.Entry<String, Long> longEntry : codeMap.entrySet()) {
			if (longEntry.getValue() > 1) {
				throw new BizException(String.format("导入文件中，商品编号[%s]重复", longEntry.getKey()));
			}
		}

		for (ItemImportVO itemImportStdVO : itemImportVOs) {
			// 商品资料对象
			TmsBaseItemModel tmsBaseItemModel = new TmsBaseItemModel();
			BeanUtils.copyProperties(itemImportStdVO, tmsBaseItemModel);

			// 判断商品编码是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseItemModel.class, "itemCode", itemImportStdVO.getItemCode(), "enterpriseId", user.getEnterpriseId(), "cargoOwnerName", itemImportStdVO.getCargoOwnerName(), tmsBaseItemMapper, "货主【" + itemImportStdVO.getCargoOwnerName() + "】商品编码【" + itemImportStdVO.getItemCode() + "】已存在");
			PublicMethods.isThereAnyinformation(TmsBaseItemModel.class, "itemCode", itemImportStdVO.getItemCode(), "enterpriseId", user.getEnterpriseId(), "cargoOwnerName", itemImportStdVO.getCargoOwnerName(), "status", StatusConstant.STATUS_DISABLE, tmsBaseItemMapper, "货主【" + itemImportStdVO.getCargoOwnerName() + "】商品编码【" + itemImportStdVO.getItemCode() + "】已停用,请直接启用");
			// 包装类型
			List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.PACK_TYPE, null, null, null);
			if (ToolKits.isNotEmpty(itemImportStdVO.getPackType())) {
				if (FileUtils.StringType(itemImportStdVO.getPackType())) {
					// 数据字典code处理
					Assert.notNull(PublicMethods.change(restMessage1, itemImportStdVO.getPackType()), "商品包装类型【" + itemImportStdVO.getPackType() + "】未维护");
					tmsBaseItemModel.setPackType(Integer.valueOf(itemImportStdVO.getPackType()));
				} else {
					// 数据字典name处理
					Assert.notNull(PublicMethods.reverseChange(restMessage1, itemImportStdVO.getPackType()), "商品包装类型【" + itemImportStdVO.getPackType() + "】未维护");
					tmsBaseItemModel.setPackType(Integer.valueOf(PublicMethods.reverseChange(restMessage1, itemImportStdVO.getPackType())));
				}
			}
			// 货物温层
			List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.CARGO_THERMOSPHERE, null, null, null);
			if (ToolKits.isNotEmpty(itemImportStdVO.getCargoThermosphere())) {
				if (FileUtils.StringType(itemImportStdVO.getCargoThermosphere())) {
					// 数据字典code处理
					Assert.notNull(PublicMethods.change(restMessage2, itemImportStdVO.getCargoThermosphere()), "商品货物温层【" + itemImportStdVO.getCargoThermosphere() + "】未维护");
					tmsBaseItemModel.setCargoThermosphere(Integer.valueOf(itemImportStdVO.getCargoThermosphere()));
				} else {
					// 数据字典name处理
					Assert.notNull(PublicMethods.reverseChange(restMessage2, itemImportStdVO.getCargoThermosphere()), "商品货物温层【" + itemImportStdVO.getCargoThermosphere() + "】未维护");
					tmsBaseItemModel.setCargoThermosphere(Integer.valueOf(PublicMethods.reverseChange(restMessage2, itemImportStdVO.getCargoThermosphere())));
				}
			}
			// 货主校验
			Criteria criteria = Criteria.forClass(TmsBaseCargoOwnerModel.class);
			Restriction r2 = Restrictions.eq(TmsBaseCargoOwnerModel::getCargoOwnerName, itemImportStdVO.getCargoOwnerName());
			Restriction r3 = Restrictions.eq(TmsBaseCargoOwnerModel::getEnterpriseId, user.getEnterpriseId());
			criteria.setRestriction(Restrictions.and(r2, r3));
			List<TmsBaseCargoOwnerModel> tmsBaseCargoOwnerModels = tmsBaseCargoOwnerMapper.findByCriteria(criteria);
			Assert.isTrue(tmsBaseCargoOwnerModels.size() > 0, "货主【" + itemImportStdVO.getCargoOwnerName() + "】不存在");
			Assert.isTrue(tmsBaseCargoOwnerModels.size() == 1, "货主【" + itemImportStdVO.getCargoOwnerName() + "】存在重复数据");
			// 货主对象
			TmsBaseCargoOwnerModel tmsBaseCargoOwnerModel = tmsBaseCargoOwnerModels.get(0);

			// 仓库对象
			TmsBaseBranchModel tmsBaseBranchModel = null;
			if (ToolKits.isNotEmpty(itemImportStdVO.getBranchName())) {
				// 仓库校验
				Criteria criteria2 = Criteria.forClass(TmsBaseBranchModel.class);
				Restriction r21 = Restrictions.eq(TmsBaseBranchModel::getName, itemImportStdVO.getBranchName());
				Restriction r22 = Restrictions.eq(TmsBaseBranchModel::getEnterpriseId, user.getEnterpriseId());
				criteria2.setRestriction(Restrictions.and(r21, r22));
				List<TmsBaseBranchModel> tmsBaseBranchModels = tmsBaseBranchMapper.findByCriteria(criteria2);
				Assert.isTrue(tmsBaseBranchModels.size() > 0, "仓库【" + itemImportStdVO.getBranchName() + "】不存在");
				Assert.isTrue(tmsBaseCargoOwnerModels.size() == 1, "仓库【" + itemImportStdVO.getBranchName() + "】存在重复数据");
				// 仓库对象
				tmsBaseBranchModel = tmsBaseBranchModels.get(0);
			}

			Assert.isTrue(tmsBaseCargoOwnerModel.getStatus() == StatusConstant.STATUS_ENABLE, "货主【" + itemImportStdVO.getCargoOwnerName() + "】未启用");
			// 判断商品品类
			if (ToolKits.isNotEmpty(itemImportStdVO.getItemCategoryName())) {
				// 查询上级商品品类资料
				Criteria criteria1 = Criteria.forClass(TmsBaseItemCategoryModel.class);
				Restriction r11 = Restrictions.eq("itemCategoryName", itemImportStdVO.getItemCategoryName());
				Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r12));
				List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria1);
				Assert.isTrue(tmsBaseItemCategoryModels.size() > 0, "商品品类【" + itemImportStdVO.getItemCategoryName() + "】不存在");
				Assert.isTrue(tmsBaseItemCategoryModels.size() == 1, "商品品类【" + itemImportStdVO.getItemCategoryName() + "】存在重复数据");
				// 上级商品品类对象
				TmsBaseItemCategoryModel itemCategoryModel = tmsBaseItemCategoryModels.get(0);
				Assert.isTrue(itemCategoryModel.getStatus() == StatusConstant.STATUS_ENABLE, "上级商品品类【" + itemImportStdVO.getItemCategoryName() + "】未启用");
				// 赋值商品品类信息
				tmsBaseItemModel.setItemCategoryId(itemCategoryModel.getItemCategoryId());
				tmsBaseItemModel.setItemCategoryCode(itemCategoryModel.getItemCategoryCode());
				tmsBaseItemModel.setItemCategoryLevel(itemCategoryModel.getItemCategoryLevel());
			}

			// 登录为自有账号时
			if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
				ToolKits.isParamNull(itemImportStdVO.getDepartmentName(), "所属部门名称不为空");
				// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 start
				// 所属部门名称判定是否存在
				String[] departments = itemImportStdVO.getDepartmentName().split(",");
				List<String> deptIds = Lists.newArrayList();
				List<String> deptCodes = Lists.newArrayList();
				for (String dept : departments) {
					RestMessage<List<Organization>> departMents = userFeign.search(null, dept, null, String.valueOf(user.getEnterpriseId()));
					if (!departMents.isSuccess()) {
						throw new RuntimeException("用户部门信息查询失败:" + departMents.getMessage());
					} else {
						List<Organization> 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());
					}
				}
				tmsBaseItemModel.setDepartmentName(itemImportStdVO.getDepartmentName());
				tmsBaseItemModel.setDepartmentId(deptIds.stream().collect(Collectors.joining(",")));
				tmsBaseItemModel.setDepartmentCode(deptCodes.stream().collect(Collectors.joining(",")));
				// STD-DEVELOP20220823 lhb JC-HZ-001 商品支持多部门 20220915 end
			} else {

			}

			// 商品ID
			String itemId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_ID, "L");
			ToolKits.isParamNull(itemId, "商品ID生成失败");

			// 商品资料对象保存
			tmsBaseItemModel.setItemId(Long.valueOf(itemId));
			tmsBaseItemModel.setCargoOwnerId(tmsBaseCargoOwnerModel.getCargoOwnerId());
			tmsBaseItemModel.setCargoOwnerCode(tmsBaseCargoOwnerModel.getCargoOwnerCode());
			tmsBaseItemModel.setStatus(StatusConstant.STATUS_ENABLE);
			tmsBaseItemModel.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			tmsBaseItemModel.setImportBatchNumber(importBatchNumber);
//			if (ToolKits.isNotEmpty(itemImportStdVO.getPackType())) {
//				tmsBaseItemModel.setPackType(Integer.parseInt(itemImportStdVO.getPackType()));
//			}
//			if (ToolKits.isNotEmpty(itemImportStdVO.getCargoThermosphere())) {
//				tmsBaseItemModel.setCargoThermosphere(Integer.parseInt(itemImportStdVO.getCargoThermosphere()));
//			}
			tmsBaseItemModel.setWeight(itemImportStdVO.getWeight() == null ? BigDecimal.ZERO : itemImportStdVO.getWeight());
			tmsBaseItemModel.setVolume(itemImportStdVO.getVolume() == null ? BigDecimal.ZERO : itemImportStdVO.getVolume());
			tmsBaseItemModel.setBranchId(tmsBaseBranchModel == null ? null : tmsBaseBranchModel.getId());
			tmsBaseItemModel.setBranchCode(tmsBaseBranchModel == null ? null : tmsBaseBranchModel.getCode());
			tmsBaseItemModel.setBranchName(tmsBaseBranchModel == null ? null : tmsBaseBranchModel.getName());
			tmsBaseItemModel.setEnterpriseId(user.getEnterpriseId());
			tmsBaseItemModel.setEnterpriseName(user.getEnterpriseName());
			tmsBaseItemModel.setCreateperson(user.getNickname());
			tmsBaseItemModel.setCreatetime(date);
			tmsBaseItemModel.setUpdateperson(user.getNickname());
			tmsBaseItemModel.setUpdatetime(date);
			tmsBaseItemMapper.save(tmsBaseItemModel);

			tmsBaseItemModelList.add(tmsBaseItemModel);
		}
		return tmsBaseItemModelList.size();
	}

	/**
	 * 导入撤销
	 *
	 * @param batchno
	 */
	@Override
	public void rollbackData(String batchno) {
		ToolKits.isParamNull(batchno, "导入批次号不为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.eq("importBatchNumber", batchno);
		Restriction r2 = Restrictions.eq("source", DataDictionaryConstant.SOURCE_EXCEL);
		criteria.setRestriction(Restrictions.and(r1, r2));

		tmsBaseItemMapper.deleteByCriteria(criteria);

	}

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

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

		// 包装类型
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.PACK_TYPE, null, null, null);
		// 货物温层
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.CARGO_THERMOSPHERE, null, null, null);
		// 商品状态
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.MAINDATA_STATUS, null, null, null);

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.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<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);

		for (TmsBaseItemModel tmsBaseItemModel : tmsBaseItemModels) {
			// 响应对象
			ItemImportPageDTO itemImportPageDTO = new ItemImportPageDTO();
			BeanUtils.copyProperties(tmsBaseItemModel, itemImportPageDTO);
			itemImportPageDTO.setPackTypeName(PublicMethods.change(restMessage1, String.valueOf(itemImportPageDTO.getPackType())));
			itemImportPageDTO.setCargoThermosphereName(PublicMethods.change(restMessage2, String.valueOf(itemImportPageDTO.getCargoThermosphere())));
			itemImportPageDTO.setStatusName(PublicMethods.change(restMessage3, String.valueOf(itemImportPageDTO.getStatus())));
			itemImportPageDTOs.add(itemImportPageDTO);
		}

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

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

	/**
	 * 根据商品编码名称查询商品资料
	 */
	@Override
	public ItemByEncodingNameDTO getItemByEncodingNameData(ItemByEncodingNameVO itemByEncodingNameVO) {
		ToolKits.isParamNull(itemByEncodingNameVO, "根据商品编码名称查询商品资料请求不为空");
		ToolKits.isParamNull(itemByEncodingNameVO.getCode(), "商品编码不为空");
		// WEICHAI-DEVELOP20220823 fanzw 改成可只按编码查询 start
		// ToolKits.isParamNull(itemByEncodingNameVO.getName(), "商品名称不为空");
		// WEICHAI-DEVELOP20220823 fanzw 改成可只按编码查询 end
		ToolKits.isParamNull(itemByEncodingNameVO.getCargoOwnerId(), "货主ID为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.eq("itemCode", itemByEncodingNameVO.getCode());
		// WEICHAI-DEVELOP20220823 fanzw 改成可只按编码查询 start
		Restriction r2 = ToolKits.isEmpty(itemByEncodingNameVO.getName()) ? null : Restrictions.eq("itemName", itemByEncodingNameVO.getName());
		// WEICHAI-DEVELOP20220823 fanzw 改成可只按编码查询 end
		Restriction r3 = Restrictions.eq("cargoOwnerId", itemByEncodingNameVO.getCargoOwnerId());
		Restriction r4 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);
		Assert.isTrue(tmsBaseItemModels.size() > 0, "不存在编码为【" + itemByEncodingNameVO.getCode() + "】,名称为【" + itemByEncodingNameVO.getName() + "】的商品");
		Assert.isTrue(tmsBaseItemModels.size() == 1, "编码为【" + itemByEncodingNameVO.getCode() + "】,名称为【" + itemByEncodingNameVO.getName() + "】的商品资料重复");

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = tmsBaseItemModels.get(0);
		Assert.isTrue(tmsBaseItemModel.getStatus() == StatusConstant.STATUS_ENABLE, "商品【" + itemByEncodingNameVO.getName() + "】未启用");

		// 响应对象
		ItemByEncodingNameDTO itemByEncodingNameDTO = new ItemByEncodingNameDTO();
		BeanUtils.copyProperties(tmsBaseItemModel, itemByEncodingNameDTO);

		return itemByEncodingNameDTO;
	}

	@Override
	public ItemByEncodingNameDTO getItemByEncodingNameDataIntf(ItemByEncodingNameVO itemByEncodingNameVO) {
		ToolKits.isParamNull(itemByEncodingNameVO, "根据商品编码名称查询商品资料请求不为空");
		ToolKits.isParamNull(itemByEncodingNameVO.getCode(), "商品编码不为空");
		ToolKits.isParamNull(itemByEncodingNameVO.getCargoOwnerId(), "货主ID为空");

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.eq("itemCode", itemByEncodingNameVO.getCode());
		Restriction r2 = ToolKits.isEmpty(itemByEncodingNameVO.getName()) ? null : Restrictions.eq("itemName", itemByEncodingNameVO.getName());
		Restriction r3 = Restrictions.eq("cargoOwnerId", itemByEncodingNameVO.getCargoOwnerId());
		Restriction r4 = Restrictions.eq("enterpriseId", itemByEncodingNameVO.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);
		Assert.isTrue(tmsBaseItemModels.size() > 0, "不存在编码为【" + itemByEncodingNameVO.getCode() + "】,名称为【" + itemByEncodingNameVO.getName() + "】的商品");
		Assert.isTrue(tmsBaseItemModels.size() == 1, "编码为【" + itemByEncodingNameVO.getCode() + "】,名称为【" + itemByEncodingNameVO.getName() + "】的商品资料重复");

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = tmsBaseItemModels.get(0);
		Assert.isTrue(tmsBaseItemModel.getStatus() == StatusConstant.STATUS_ENABLE, "商品【" + itemByEncodingNameVO.getName() + "】未启用");

		// 响应对象
		ItemByEncodingNameDTO itemByEncodingNameDTO = new ItemByEncodingNameDTO();
		BeanUtils.copyProperties(tmsBaseItemModel, itemByEncodingNameDTO);

		return itemByEncodingNameDTO;
	}

	/**
	 * 根据商品ID查询单位重量体积
	 */
	@Override
	public List<ItemUnitDTO> listItemUnitData(List<BigDecimal> itemIds) {
		ToolKits.isParamNull(itemIds, "请求参数不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.in(TmsBaseItemModel::getItemId, itemIds.stream().toArray(BigDecimal[]::new));
		Restriction r2 = Restrictions.eq(TmsBaseItemModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);
		for (TmsBaseItemModel tmsBaseItemModel : tmsBaseItemModels) {
			// 响应对象
			ItemUnitDTO itemUnitDTO = new ItemUnitDTO();
			itemUnitDTO.setItemId(tmsBaseItemModel.getItemId());
			itemUnitDTO.setVolume(tmsBaseItemModel.getVolume() == null ? BigDecimal.ZERO : tmsBaseItemModel.getVolume());
			itemUnitDTO.setWeight(tmsBaseItemModel.getWeight() == null ? BigDecimal.ZERO : tmsBaseItemModel.getWeight());

			itemUnitDTOs.add(itemUnitDTO);
		}

		return itemUnitDTOs;
	}

	/**
	 * 商品资料接口数据处理
	 */
	@Override
	public List<IntfErrorLogDTO> saveItemInterfaceData(List<IntfItemSaveVO> intfItemSaveVOs) {
		ToolKits.isParamNull(intfItemSaveVOs, "商品资料接口请求不为空");

		// 响应集合
		List<IntfErrorLogDTO> intfErrorLogDTOs = Lists.newArrayList();

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

		for (IntfItemSaveVO intfItemSaveVO : intfItemSaveVOs) {
			// 响应对象
			IntfErrorLogDTO intfErrorLogDTO = new IntfErrorLogDTO();

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

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

				// 校验商品品类是否存在
				Criteria criteria1 = Criteria.forClass(TmsBaseItemCategoryModel.class);
				Restriction r11 = ToolKits.isEmpty(intfItemSaveVO.getItemCategoryCode()) ? null : Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryCode, intfItemSaveVO.getItemCategoryCode());
				Restriction r12 = ToolKits.isEmpty(intfItemSaveVO.getItemCategoryName()) ? null : Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryName, intfItemSaveVO.getItemCategoryName());
				Restriction r13 = Restrictions.eq(TmsBaseItemCategoryModel::getEnterpriseId, intfItemSaveVO.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r12, r13));
				List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria1);
				ToolKits.isParamNull(tmsBaseItemCategoryModels, "商品品类【" + intfItemSaveVO.getItemCategoryCode() + "】不存在");
				Assert.isTrue(tmsBaseItemCategoryModels.size() <= 1, "商品品类【" + intfItemSaveVO.getItemCategoryCode() + "】资料重复");

				// 商品品类对象
				TmsBaseItemCategoryModel baseItemCategoryModel = tmsBaseItemCategoryModels.get(0);
				Assert.isTrue(baseItemCategoryModel.getStatus().intValue() == StatusConstant.STATUS_ENABLE.intValue(), "商品品类【" + intfItemSaveVO.getItemCategoryCode() + "】未启用");

				// 商品资料校验
				Criteria criteria2 = Criteria.forClass(TmsBaseItemModel.class);
				Restriction r21 = Restrictions.eq(TmsBaseItemModel::getItemCode, intfItemSaveVO.getItemCode());
				Restriction r22 = Restrictions.eq(TmsBaseItemModel::getCargoOwnerId, baseCargoOwnerModel.getCargoOwnerId());
				Restriction r23 = Restrictions.eq(TmsBaseItemModel::getEnterpriseId, intfItemSaveVO.getEnterpriseId());
				criteria2.setRestriction(Restrictions.and(r21, r22, r23));
				List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria2);
				if (tmsBaseItemModels.size() > 0) {
					Assert.isTrue(tmsBaseItemModels.size() <= 1, "商品编码【" + intfItemSaveVO.getItemCode() + "】资料重复");

					TmsBaseItemModel tmsBaseItemModel = tmsBaseItemModels.get(0);
					tmsBaseItemModel.setItemName(intfItemSaveVO.getItemName());
					tmsBaseItemModel.setCargoOwnerId(baseCargoOwnerModel.getCargoOwnerId());
					tmsBaseItemModel.setCargoOwnerCode(baseCargoOwnerModel.getCargoOwnerCode());
					tmsBaseItemModel.setCargoOwnerName(baseCargoOwnerModel.getCargoOwnerName());
					tmsBaseItemModel.setLength(intfItemSaveVO.getLength());
					tmsBaseItemModel.setWidth(intfItemSaveVO.getWidth());
					tmsBaseItemModel.setHeigth(intfItemSaveVO.getHeigth());
					tmsBaseItemModel.setWeight(intfItemSaveVO.getWeight() == null ? tmsBaseItemModel.getWeight() : intfItemSaveVO.getWeight());
					tmsBaseItemModel.setVolume(intfItemSaveVO.getVolume() == null ? tmsBaseItemModel.getVolume() : intfItemSaveVO.getVolume());
					tmsBaseItemModel.setItemCategoryId(baseItemCategoryModel.getItemCategoryId());
					tmsBaseItemModel.setItemCategoryCode(baseItemCategoryModel.getItemCategoryCode());
					tmsBaseItemModel.setItemCategoryName(baseItemCategoryModel.getItemCategoryName());
					tmsBaseItemModel.setItemCategoryLevel(baseItemCategoryModel.getItemCategoryLevel());
					tmsBaseItemModel.setStatus(intfItemSaveVO.getStatus());
					tmsBaseItemModel.setUpdatetime(date);
					tmsBaseItemModel.setTemperatureCtlUpperLimit(intfItemSaveVO.getTemperatureCtlUpperLimit());
					tmsBaseItemModel.setTemperatureCtlLowerLimit(intfItemSaveVO.getTemperatureCtlLowerLimit());
					tmsBaseItemModel.setSymbol(intfItemSaveVO.getSymbol());
					tmsBaseItemModel.setSymbolExpirationDate(intfItemSaveVO.getSymbolExpirationDate());
					tmsBaseItemModel.setLicensor(intfItemSaveVO.getLicensor());
					tmsBaseItemModel.setCapacity(intfItemSaveVO.getCapacity());
					tmsBaseItemModel.setPackSize(intfItemSaveVO.getPackSize());
					tmsBaseItemModel.setDoseType(intfItemSaveVO.getDoseType());
					tmsBaseItemModel.setFactory(intfItemSaveVO.getFactory());
					tmsBaseItemModel.setOrigin(intfItemSaveVO.getOrigin());
					tmsBaseItemMapper.update(tmsBaseItemModel);
				} else {
					// 商品ID
					String itemId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_ID, "L");
					ToolKits.isParamNull(itemId, "商品ID生成失败");

					TmsBaseItemModel tmsBaseItemModel = new TmsBaseItemModel();
					BeanUtils.copyProperties(intfItemSaveVO, tmsBaseItemModel);
					tmsBaseItemModel.setItemId(Long.valueOf(itemId));
					tmsBaseItemModel.setCargoOwnerId(baseCargoOwnerModel.getCargoOwnerId());
					tmsBaseItemModel.setItemCategoryId(baseItemCategoryModel.getItemCategoryId());
					tmsBaseItemModel.setItemCategoryCode(baseItemCategoryModel.getItemCategoryCode());
					tmsBaseItemModel.setItemCategoryName(baseItemCategoryModel.getItemCategoryName());
					tmsBaseItemModel.setItemCategoryLevel(baseItemCategoryModel.getItemCategoryLevel());
					tmsBaseItemModel.setWeight(intfItemSaveVO.getWeight() == null ? BigDecimal.ZERO : intfItemSaveVO.getWeight());
					tmsBaseItemModel.setVolume(intfItemSaveVO.getVolume() == null ? BigDecimal.ZERO : intfItemSaveVO.getVolume());
					tmsBaseItemModel.setStatus(StatusConstant.STATUS_ENABLE);
					tmsBaseItemModel.setSource(DataDictionaryConstant.SOURCE_INTERFACE);
					tmsBaseItemModel.setCreatetime(date);
					tmsBaseItemModel.setUpdatetime(date);
					tmsBaseItemModel.setTemperatureCtlUpperLimit(intfItemSaveVO.getTemperatureCtlUpperLimit());
					tmsBaseItemModel.setTemperatureCtlLowerLimit(intfItemSaveVO.getTemperatureCtlLowerLimit());
					tmsBaseItemMapper.save(tmsBaseItemModel);
				}

				// 成功信息
				intfErrorLogDTO.setErrorInfo(null);
				intfErrorLogDTO.setDataId(intfItemSaveVO.getItemId());
				intfErrorLogDTO.setStatus(true);

				intfErrorLogDTOs.add(intfErrorLogDTO);
			} catch (Exception e) {
				e.printStackTrace();
				log.info("商品资料接口处理失败:{}", e);

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

				intfErrorLogDTOs.add(intfErrorLogDTO);
			}
		}

		return intfErrorLogDTOs;
	}

	/**
	 * 临时商品资料新增
	 */
	@Override
	public void addTempItemData(ItemAddVO itemAddVO) {
		ToolKits.isParamNull(itemAddVO, "商品资料新增请求不为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerId(), "货主ID不能为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerCode(), "货主编号不能为空");
		ToolKits.isParamNull(itemAddVO.getCargoOwnerName(), "货主名称不能为空");
		ToolKits.isParamNull(itemAddVO.getItemCategoryName(), "商品品类不能为空");

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

		// 商品ID
		String itemId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_ID, "L");
		ToolKits.isParamNull(itemId, "商品ID生成失败");

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

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseItemModel::getItemName, itemAddVO.getItemCategoryName());
		Restriction r2 = Restrictions.eq(TmsBaseItemModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(tmsBaseItemModels)) {
			throw new PrologException("临时商品【" + itemAddVO.getItemCategoryName() + "】已存在");
		}

		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.search(null, null, null, String.valueOf(user.getEnterpriseId()));
		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());
			});
		}

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = new TmsBaseItemModel();
		BeanUtils.copyProperties(itemAddVO, tmsBaseItemModel);

		tmsBaseItemModel.setOrganizationId(user.getOrganizationId());
		tmsBaseItemModel.setOrganizationCode(String.valueOf(user.getOrganizationId()));
		tmsBaseItemModel.setOrganizationName(user.getOrgnaizationName());
		tmsBaseItemModel.setDepartmentId(deptIds.stream().collect(Collectors.joining(",")));
		tmsBaseItemModel.setDepartmentCode(deptCodes.stream().collect(Collectors.joining(",")));
		tmsBaseItemModel.setDepartmentName(deptNames.stream().collect(Collectors.joining(",")));
		tmsBaseItemModel.setVolume(itemAddVO.getVolume() == null ? BigDecimal.ZERO : itemAddVO.getVolume());
		tmsBaseItemModel.setWeight(itemAddVO.getWeight() == null ? BigDecimal.ZERO : itemAddVO.getWeight());
		tmsBaseItemModel.setItemId(Long.valueOf(itemId));
		tmsBaseItemModel.setItemCode(itemId);
		tmsBaseItemModel.setItemName(itemAddVO.getItemCategoryName());
		tmsBaseItemModel.setStatus(StatusConstant.STATUS_ENABLE);
		tmsBaseItemModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseItemModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseItemModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseItemModel.setCreateperson(user.getNickname());
		tmsBaseItemModel.setCreatetime(date);
		tmsBaseItemModel.setUpdateperson(user.getNickname());
		tmsBaseItemModel.setUpdatetime(date);

		tmsBaseItemModel.setItemCategoryLevel(DataDictionaryConstant.ITEM_CATEGORY_LEVEL_ONE);
		tmsBaseItemMapper.save(tmsBaseItemModel);
	}

	/**
	 * 根据商品名称取商品资料
	 *
	 * @param itemCheckVO
	 * @return
	 */
	public ItemCheckDTO getItemByNameData(ItemCheckVO itemCheckVO) {
		ToolKits.isParamNull(itemCheckVO, "商品资料查看请求不为空");
		if (ToolKits.isEmpty(itemCheckVO.getItemName())) {
			throw new PrologException("商品名称不为空");
		}
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseItemModel::getItemName, itemCheckVO.getItemName());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsBaseItemModel> tmsBaseItemModels = tmsBaseItemMapper.findByCriteria(criteria);

		if (tmsBaseItemModels.size() == 0) {
			throw new ParameterException("商品【" + itemCheckVO.getItemName() + "】商品不存在，查询失败");
		} else if (tmsBaseItemModels.size() > 1) {
			throw new ParameterException("商品【" + itemCheckVO.getItemName() + "】商品存在重复数据，查询失败");
		}

		// 商品资料对象
		TmsBaseItemModel tmsBaseItemModel = tmsBaseItemModels.get(0);

		// 响应对象
		ItemCheckDTO ttemCheckDTO = new ItemCheckDTO();
		BeanUtils.copyProperties(tmsBaseItemModel, ttemCheckDTO);
		ttemCheckDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(ttemCheckDTO.getStatus())));
		ttemCheckDTO.setVolume(tmsBaseItemModel.getVolume() == null ? BigDecimal.ZERO : tmsBaseItemModel.getVolume());
		ttemCheckDTO.setWeight(tmsBaseItemModel.getWeight() == null ? BigDecimal.ZERO : tmsBaseItemModel.getWeight());

		return ttemCheckDTO;
	}

}
