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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.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.framework.common.message.RestMessage;
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.easyexcel.util.ExcelUtils;
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.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.CargoOwnerPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryImportPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryPopupDTO;
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.ItemCategorySaveVO;
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.CargoOwnerPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryBatchDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryByIdVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ItemCategoryImportVO;
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.CargoOwnerService;
import com.prolog.rdc.tms.service.basicData.ItemCategoryService;
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.JsonMapper;

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

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

	@Autowired
	private TmsBaseItemCategoryMapper tmsBaseItemCategoryMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private TmsBaseItemMapper tmsBaseItemMapper;

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@Autowired
	private AmqpTemplate amqpTemplate;

	@Autowired
	private CargoOwnerService cargoOwnerService;

	/**
	 * 商品品类分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<ItemCategoryPagingQueryDTO> pageItemCategoryData(ItemCategoryPagingQueryVO itemCategoryPagingQueryVO) {
		ToolKits.isParamNull(itemCategoryPagingQueryVO, "商品品类查询请求不为空");
		ToolKits.isParamNull(itemCategoryPagingQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(itemCategoryPagingQueryVO.getPageSize(), "页数不为空");

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

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

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

		// 判断账号类型
		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
		Restriction r1 = ToolKits.isEmpty(itemCategoryPagingQueryVO.getItemCategoryCode()) ? null : Restrictions.like("itemCategoryCode", "%" + itemCategoryPagingQueryVO.getItemCategoryCode() + "%");
		Restriction r2 = ToolKits.isEmpty(itemCategoryPagingQueryVO.getItemCategoryName()) ? null : Restrictions.like("itemCategoryName", "%" + itemCategoryPagingQueryVO.getItemCategoryName() + "%");
		Restriction r5 = ToolKits.isEmpty(itemCategoryPagingQueryVO.getCargoOwnerName()) ? null : Restrictions.like("cargoOwnerName", "%" + itemCategoryPagingQueryVO.getCargoOwnerName() + "%");
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		Restriction r4 = null;
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 判断是否为部门权限
			if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				// r4 = Restrictions.in("departmentId",
				// user.getDepts().stream().toArray(String[]::new));
			} else {
				// r4 = Restrictions.in("departmentId",
				// user.getDepts().stream().toArray(String[]::new));
				r4 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			// 显示所属货主为当前登录账号单位id的商品品类
			r4 = Restrictions.eq("cargoOwnerId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		// 分页
		PageUtils.startPage(itemCategoryPagingQueryVO.getPageNum(), itemCategoryPagingQueryVO.getPageSize());
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);
		for (TmsBaseItemCategoryModel tmsBaseItemCategoryModel : tmsBaseItemCategoryModels) {
			// 响应对象
			ItemCategoryPagingQueryDTO itemCategoryPagingQueryDTO = new ItemCategoryPagingQueryDTO();
			BeanUtils.copyProperties(tmsBaseItemCategoryModel, itemCategoryPagingQueryDTO);
			itemCategoryPagingQueryDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(itemCategoryPagingQueryDTO.getStatus())));
			itemCategoryPagingQueryDTOs.add(itemCategoryPagingQueryDTO);
		}

		Page<ItemCategoryPagingQueryDTO> page = PageUtils.getPage(itemCategoryPagingQueryDTOs);
		if (tmsBaseItemCategoryModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseItemCategoryModel> p0 = (com.github.pagehelper.Page<TmsBaseItemCategoryModel>) tmsBaseItemCategoryModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 商品品类查看
	 */
	@Override
	public ItemCategoryCheckDTO getItemCategoryData(ItemCategoryCheckVO itemCategoryCheckVO) {
		ToolKits.isParamNull(itemCategoryCheckVO, "商品品类查看请求不为空");
		ToolKits.isParamNull(itemCategoryCheckVO.getItemCategoryId(), "商品品类ID不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
		Restriction r1 = Restrictions.eq("itemCategoryId", itemCategoryCheckVO.getItemCategoryId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);

		Assert.isTrue(tmsBaseItemCategoryModels.size() == 1, "商品品类ID【" + itemCategoryCheckVO.getItemCategoryId() + "】存在重复数据，查询失败");

		// 商品品类对象
		TmsBaseItemCategoryModel tmsBaseItemCategoryModel = tmsBaseItemCategoryModels.get(0);

		// 响应对象
		ItemCategoryCheckDTO itemCategoryCheckDTO = new ItemCategoryCheckDTO();
		BeanUtils.copyProperties(tmsBaseItemCategoryModel, itemCategoryCheckDTO);
		itemCategoryCheckDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(itemCategoryCheckDTO.getStatus())));

		return itemCategoryCheckDTO;
	}

	/**
	 * 商品品类弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<ItemCategoryPopupDTO> popupItemCategoryData(ItemCategoryPopupVO itemCategoryPopupVO) {
		ToolKits.isParamNull(itemCategoryPopupVO, "商品品类弹窗请求不为空");
		ToolKits.isParamNull(itemCategoryPopupVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(itemCategoryPopupVO.getPageSize(), "页数不为空");

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

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

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

		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
		Restriction r1 = Restrictions.like("itemCategoryCode", "%" + itemCategoryPopupVO.getSearch() + "%");
		Restriction r2 = Restrictions.like("itemCategoryName", "%" + itemCategoryPopupVO.getSearch() + "%");
		Restriction r3 = ToolKits.isEmpty(itemCategoryPopupVO.getSearch()) ? null : Restrictions.or(r1, r2);
		Restriction r4 = Restrictions.eq("status", StatusConstant.STATUS_ENABLE);
		Restriction r5 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
//		Restriction r6 = ToolKits.isEmpty(itemCategoryPopupVO.getCargoOwnerId()) ? null : Restrictions.eq(TmsBaseItemCategoryModel::getCargoOwnerId, itemCategoryPopupVO.getCargoOwnerId());

		Restriction r7 = null;
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 判断是否为部门权限
			if (ObjectUtil.equal(user.getOwnedAllDataAuthFlag(), BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				// r7 = Restrictions.in("departmentId",
				// user.getDepts().stream().toArray(String[]::new));
			} else {
				// r7 = Restrictions.in("departmentId",
				// user.getDepts().stream().toArray(String[]::new));
				r7 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			// 显示所属货主为当前登录账号单位id的商品品类
			r7 = Restrictions.eq("cargoOwnerId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}

		criteria.setRestriction(Restrictions.and(r3, r4, r5, r7));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);
		for (TmsBaseItemCategoryModel tmsBaseItemCategoryModel : tmsBaseItemCategoryModels) {
			// 响应对象
			ItemCategoryPopupDTO itemCategoryPopupDTO = new ItemCategoryPopupDTO();
			BeanUtils.copyProperties(tmsBaseItemCategoryModel, itemCategoryPopupDTO);
			itemCategoryPopupDTOs.add(itemCategoryPopupDTO);
		}

		Page<ItemCategoryPopupDTO> page = PageUtils.getPage(itemCategoryPopupDTOs);
		if (tmsBaseItemCategoryModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseItemCategoryModel> p0 = (com.github.pagehelper.Page<TmsBaseItemCategoryModel>) tmsBaseItemCategoryModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 商品品类新增
	 */
	@Override
	public void addItemCategoryData(ItemCategoryAddVO itemCategoryAddVO) {
		ToolKits.isParamNull(itemCategoryAddVO, "商品品类新增请求不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getItemCategoryCode(), "商品品类编码不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getItemCategoryName(), "商品品类名称不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getItemCategoryLevel(), "商品品类层级不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getCargoOwnerId(), "所属货主ID不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getCargoOwnerCode(), "所属货主编码不为空");
		ToolKits.isParamNull(itemCategoryAddVO.getCargoOwnerName(), "所属货主名称不为空");

		if (ToolKits.isNotEmpty(itemCategoryAddVO.getFatherItemCategoryLevel())) {
			ToolKits.isParamNull(itemCategoryAddVO.getFatherItemCategoryId(), "上级商品品类ID不为空");
			ToolKits.isParamNull(itemCategoryAddVO.getFatherItemCategoryCode(), "上级商品品类编码不为空");
			ToolKits.isParamNull(itemCategoryAddVO.getFatherItemCategoryName(), "上级商品品类名称不为空");

			if (itemCategoryAddVO.getItemCategoryLevel() <= itemCategoryAddVO.getFatherItemCategoryLevel()) {
				throw new ParameterException("商品品类层级异常");
			}
		}

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

		// 判断商品品类编码是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryCode", itemCategoryAddVO.getItemCategoryCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseItemCategoryMapper, "商品品类编码已存在");
		PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryCode", itemCategoryAddVO.getItemCategoryCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseItemCategoryMapper, "商品品类编码已停用,请直接启用!");
		// 判断商品品类名称是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryName", itemCategoryAddVO.getItemCategoryName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseItemCategoryMapper, "商品品类名称已存在");
		PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryName", itemCategoryAddVO.getItemCategoryName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseItemCategoryMapper, "商品品类名称已停用,请直接启用!");

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

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

		// 商品品类对象
		TmsBaseItemCategoryModel tmsBaseItemCategoryModel = new TmsBaseItemCategoryModel();
		BeanUtils.copyProperties(itemCategoryAddVO, tmsBaseItemCategoryModel);
		tmsBaseItemCategoryModel.setItemCategoryId(Long.valueOf(itemCategoryId));
		tmsBaseItemCategoryModel.setStatus(StatusConstant.STATUS_ENABLE);
		tmsBaseItemCategoryModel.setSource(DataDictionaryConstant.SOURCE_ADD);
		tmsBaseItemCategoryModel.setEnterpriseId(user.getEnterpriseId());
		tmsBaseItemCategoryModel.setEnterpriseName(user.getEnterpriseName());
		tmsBaseItemCategoryModel.setCreateperson(user.getNickname());
		tmsBaseItemCategoryModel.setCreatetime(date);
		tmsBaseItemCategoryModel.setUpdateperson(user.getNickname());
		tmsBaseItemCategoryModel.setUpdatetime(date);
		tmsBaseItemCategoryMapper.save(tmsBaseItemCategoryModel);
	}

	/**
	 * 商品品类修改
	 */
	@Override
	public void modifyItemCategoryData(ItemCategoryModifyVO itemCategoryModifyVO) {
		ToolKits.isParamNull(itemCategoryModifyVO, "商品品类修改请求不为空");
		ToolKits.isParamNull(itemCategoryModifyVO.getItemCategoryId(), "商品品类ID不为空");
		ToolKits.isParamNull(itemCategoryModifyVO.getItemCategoryLevel(), "商品品类层级不为空");
		ToolKits.isParamNull(itemCategoryModifyVO.getCargoOwnerId(), "所属货主ID不为空");
		ToolKits.isParamNull(itemCategoryModifyVO.getCargoOwnerCode(), "所属货主编码不为空");
		ToolKits.isParamNull(itemCategoryModifyVO.getCargoOwnerName(), "所属货主名称不为空");

		if (ToolKits.isNotEmpty(itemCategoryModifyVO.getFatherItemCategoryLevel())) {
			ToolKits.isParamNull(itemCategoryModifyVO.getFatherItemCategoryId(), "上级商品品类ID不为空");
			ToolKits.isParamNull(itemCategoryModifyVO.getFatherItemCategoryCode(), "上级商品品类编码不为空");
			ToolKits.isParamNull(itemCategoryModifyVO.getFatherItemCategoryName(), "上级商品品类名称不为空");

			if (itemCategoryModifyVO.getItemCategoryLevel() <= itemCategoryModifyVO.getFatherItemCategoryLevel()) {
				throw new ParameterException("商品品类层级异常");
			}
		}

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

		// 根据ID查询商品品类
		TmsBaseItemCategoryModel tmsBaseItemCategoryModel = tmsBaseItemCategoryMapper.findById(itemCategoryModifyVO.getItemCategoryId(), TmsBaseItemCategoryModel.class);
		ToolKits.isParamNull(tmsBaseItemCategoryModel, "商品品类ID【" + itemCategoryModifyVO.getItemCategoryId() + "】不存在");

		tmsBaseItemCategoryModel.setItemCategoryLevel(itemCategoryModifyVO.getItemCategoryLevel());
		tmsBaseItemCategoryModel.setFatherItemCategoryId(itemCategoryModifyVO.getFatherItemCategoryId());
		tmsBaseItemCategoryModel.setFatherItemCategoryCode(itemCategoryModifyVO.getFatherItemCategoryCode());
		tmsBaseItemCategoryModel.setFatherItemCategoryName(itemCategoryModifyVO.getFatherItemCategoryName());
		tmsBaseItemCategoryModel.setFatherItemCategoryLevel(itemCategoryModifyVO.getFatherItemCategoryLevel());
		tmsBaseItemCategoryModel.setStatus(itemCategoryModifyVO.getStatus());
		tmsBaseItemCategoryModel.setMemo(itemCategoryModifyVO.getMemo());
		tmsBaseItemCategoryModel.setUpdateperson(user.getNickname());
		tmsBaseItemCategoryModel.setUpdatetime(DateUtil.date());
		tmsBaseItemCategoryModel.setExtStr1(itemCategoryModifyVO.getExtStr1());
		tmsBaseItemCategoryModel.setExtStr2(itemCategoryModifyVO.getExtStr2());
		tmsBaseItemCategoryModel.setExtStr3(itemCategoryModifyVO.getExtStr3());
		tmsBaseItemCategoryModel.setExtStr4(itemCategoryModifyVO.getExtStr4());
		tmsBaseItemCategoryModel.setExtStr5(itemCategoryModifyVO.getExtStr5());
		tmsBaseItemCategoryModel.setExtNum1(itemCategoryModifyVO.getExtNum1());
		tmsBaseItemCategoryModel.setExtNum2(itemCategoryModifyVO.getExtNum2());
		tmsBaseItemCategoryModel.setExtNum3(itemCategoryModifyVO.getExtNum3());
		tmsBaseItemCategoryModel.setExtNum4(itemCategoryModifyVO.getExtNum4());
		tmsBaseItemCategoryModel.setExtNum5(itemCategoryModifyVO.getExtNum5());
		tmsBaseItemCategoryModel.setExtDate1(itemCategoryModifyVO.getExtDate1());
		tmsBaseItemCategoryModel.setExtDate2(itemCategoryModifyVO.getExtDate2());
		tmsBaseItemCategoryModel.setExtDate3(itemCategoryModifyVO.getExtDate3());
		tmsBaseItemCategoryModel.setExtDate4(itemCategoryModifyVO.getExtDate4());
		tmsBaseItemCategoryModel.setExtDate5(itemCategoryModifyVO.getExtDate5());

		tmsBaseItemCategoryModel.setCargoOwnerId(itemCategoryModifyVO.getCargoOwnerId());
		tmsBaseItemCategoryModel.setCargoOwnerCode(itemCategoryModifyVO.getCargoOwnerCode());
		tmsBaseItemCategoryModel.setCargoOwnerName(itemCategoryModifyVO.getCargoOwnerName());
		tmsBaseItemCategoryMapper.update(tmsBaseItemCategoryModel);
	}

	/**
	 * 商品品类批量删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDeleteItemCategoryData(List<ItemCategoryBatchDeleteVO> itemCategoryBatchDeleteVOs) {
		ToolKits.isParamNull(itemCategoryBatchDeleteVOs, "商品品类批量删除请求不为空");

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

		for (ItemCategoryBatchDeleteVO itemCategoryBatchDeleteVO : itemCategoryBatchDeleteVOs) {
			ToolKits.isParamNull(itemCategoryBatchDeleteVO.getItemCategoryId(), "商品品类ID不为空");

			// 删除商品品类
			tmsBaseItemCategoryMapper.deleteById(itemCategoryBatchDeleteVO.getItemCategoryId(), TmsBaseItemCategoryModel.class);

			// 清空该商品品类对应商品信息
			Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
			Restriction r1 = Restrictions.eq("itemCategoryId", itemCategoryBatchDeleteVO.getItemCategoryId());
			criteria.setRestriction(r1);
			Map<String, Object> map = Maps.newHashMap();
			map.put("itemCategoryId", null);
			map.put("itemCategoryCode", null);
			map.put("itemCategoryName", null);
			map.put("updatetime", new Date());
			map.put("updateperson", user.getNickname());
			tmsBaseItemMapper.updateMapByCriteria(map, criteria);
		}
	}

	/**
	 * 商品品类是否启用
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void whetherEnableItemCategoryData(ItemCategoryWhetherEnableVO itemCategoryWhetherEnableVO) {
		ToolKits.isParamNull(itemCategoryWhetherEnableVO, "商品品类是否启用请求不为空");
		ToolKits.isParamNull(itemCategoryWhetherEnableVO.getItemCategoryIds(), "商品品类ID集不为空");
		ToolKits.isParamNull(itemCategoryWhetherEnableVO.getStatus(), "状态不为空");

		Assert.isTrue(itemCategoryWhetherEnableVO.getStatus() == StatusConstant.STATUS_ENABLE || itemCategoryWhetherEnableVO.getStatus() == StatusConstant.STATUS_DISABLE, "商品品类状态【" + itemCategoryWhetherEnableVO.getStatus() + "】错误");

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

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

		for (ItemCategoryByIdVO itemCategoryByIdVO : itemCategoryWhetherEnableVO.getItemCategoryIds()) {
			ToolKits.isParamNull(itemCategoryByIdVO.getItemCategoryId(), "商品品类ID不为空");

			// 根据ID查询商品品类
			TmsBaseItemCategoryModel tmsBaseItemCategoryModel = tmsBaseItemCategoryMapper.findById(itemCategoryByIdVO.getItemCategoryId(), TmsBaseItemCategoryModel.class);
			ToolKits.isParamNull(tmsBaseItemCategoryModel, "商品品类ID【" + itemCategoryByIdVO.getItemCategoryId() + "】不存在");

			tmsBaseItemCategoryModel.setStatus(itemCategoryWhetherEnableVO.getStatus());
			tmsBaseItemCategoryModel.setUpdateperson(user.getNickname());
			tmsBaseItemCategoryModel.setUpdatetime(date);
			tmsBaseItemCategoryMapper.update(tmsBaseItemCategoryModel);

			if (itemCategoryWhetherEnableVO.getStatus() == StatusConstant.STATUS_DISABLE) {
				// 清空该商品品类对应商品信息
				Criteria criteria = Criteria.forClass(TmsBaseItemModel.class);
				Restriction r1 = Restrictions.eq("itemCategoryId", itemCategoryByIdVO.getItemCategoryId());
				criteria.setRestriction(r1);
				Map<String, Object> map = Maps.newHashMap();
				map.put("itemCategoryId", null);
				map.put("itemCategoryCode", "");
				map.put("itemCategoryName", "");
				map.put("updatetime", date);
				map.put("updateperson", user.getNickname());
				tmsBaseItemMapper.updateMapByCriteria(map, criteria);
			}
		}
	}

	/**
	 * 商品品类导入
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importItemCategoryData(MultipartFile file) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 导入文件判断
		FileUtils.multipartFileJudge(file);

		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");

		// 导入日志对象
		ImportLogAddVO logAddVO = new ImportLogAddVO();
		logAddVO.setImportObjNo(DataDictionaryConstant.ITEM_CATEGORY_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<ItemCategoryImportVO> importVOClass = ItemCategoryImportVO.class;
			List<ItemCategoryImportVO> itemCategoryImportVOs = ExcelUtils.read(file, importVOClass);

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

			throw new ParameterException(e.getMessage());
		}

	}

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

	/**
	 * 执行导入过程
	 *
	 * @param file
	 * @param importBatchNumber
	 * @return
	 */
	public int importProcess(MultipartFile file, String importBatchNumber, UserEntity user) throws Exception {
		Date date = new Date();
		// 待转换的对象
		Class<ItemCategoryImportVO> importVOClass = ItemCategoryImportVO.class;
		Properties properties = new Properties();
		// 标准模板查询
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(DataDictionaryConstant.ITEM_CATEGORY_IMPORT);
		for (StandardTemplateDetailsListDTO standardTemplateDetailsListDTO : standardTemplateDetailsListDTOs) {
			// 属性字段和注解属性字段值 键值对
			properties.setProperty(standardTemplateDetailsListDTO.getStdColNo(), standardTemplateDetailsListDTO.getCustColName());
		}
		// @ExcelProperty注解属性修改
		ImportUtils.setHeaderValue(properties, importVOClass);
		// EXCEL转对象
		List<ItemCategoryImportVO> itemCategoryImportVOs = ExcelUtils.read(file, importVOClass);

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

		int i = 1;
		for (ItemCategoryImportVO itemCategoryImportVO : itemCategoryImportVOs) {
			if (ToolKits.isEmpty(itemCategoryImportVO.getItemCategoryCode())) {
				throw new PrologException("第" + i + "行导入数据,商品品类编码不能为空");
			}
			if (ToolKits.isEmpty(itemCategoryImportVO.getItemCategoryName())) {
				throw new PrologException("第" + i + "行导入数据,商品品类名称不能为空");
			}
			if (ToolKits.isEmpty(itemCategoryImportVO.getItemCategoryLevel())) {
				throw new PrologException("第" + i + "行导入数据,商品品类层级为必选项");
			}
			if (ObjectUtil.equal("null", itemCategoryImportVO.getItemCategoryLevel())) {
				throw new PrologException("第" + i + "行导入数据【商品品类层级】错误,请选择下拉组件里的商品品类层级");
			}
			if (ToolKits.isEmpty(itemCategoryImportVO.getCargoOwnerName())) {
				throw new PrologException("第" + i + "行导入数据,货主不能为空");
			}
			i++;
		}

		// 验证导入文件中商品品类编号是否重复
		Map<String, Long> codeMap = itemCategoryImportVOs.stream().collect(Collectors.groupingBy(ItemCategoryImportVO::getItemCategoryCode, Collectors.counting()));
		for (Map.Entry<String, Long> longEntry : codeMap.entrySet()) {
			if (longEntry.getValue() > 1) {
				throw new ParameterException(String.format("导入文件中，商品品类编号[%s]重复", longEntry.getKey()));
			}
		}
		// 验证导入文件中商品品类名称是否重复
		Map<String, Long> nameMap = itemCategoryImportVOs.stream().collect(Collectors.groupingBy(ItemCategoryImportVO::getItemCategoryName, Collectors.counting()));
		for (Map.Entry<String, Long> longEntry : nameMap.entrySet()) {
			if (longEntry.getValue() > 1) {
				throw new ParameterException(String.format("导入文件中，商品品类名称[%s]重复", longEntry.getKey()));
			}
		}

		i = 0;
		for (ItemCategoryImportVO itemCategoryImportVO : itemCategoryImportVOs) {
			// 商品品类对象
			TmsBaseItemCategoryModel tmsBaseItemCategoryModel = new TmsBaseItemCategoryModel();
			BeanUtils.copyProperties(itemCategoryImportVO, tmsBaseItemCategoryModel);

			// 商品品类层级数据字典
			List<Map<String, Object>> restMessage = sysRefdatadService.getDropDownById(DataDictionaryConstant.ITEM_CATEGORY_LEVEL, null, null, null);
			if (ToolKits.isNotEmpty(itemCategoryImportVO.getItemCategoryLevel())) {
				// 商品品类层级处理
				if (FileUtils.StringType(itemCategoryImportVO.getItemCategoryLevel())) {
					// 数据字典code处理
					Assert.notNull(PublicMethods.change(restMessage, itemCategoryImportVO.getItemCategoryLevel()), "商品品类层级【" + itemCategoryImportVO.getItemCategoryLevel() + "】不存在");
					tmsBaseItemCategoryModel.setItemCategoryLevel(Integer.valueOf(itemCategoryImportVO.getItemCategoryLevel()));
				} else {
					// 数据字典name处理
					Assert.notNull(PublicMethods.reverseChange(restMessage, itemCategoryImportVO.getItemCategoryLevel()), "商品品类层级【" + itemCategoryImportVO.getItemCategoryLevel() + "】不存在");
					tmsBaseItemCategoryModel.setItemCategoryLevel(Integer.valueOf(PublicMethods.reverseChange(restMessage, itemCategoryImportVO.getItemCategoryLevel())));
				}
			}

			if (ToolKits.isEmpty(itemCategoryImportVO.getFatherItemCategoryLevel()) && (ToolKits.isNotEmpty(itemCategoryImportVO.getFatherItemCategoryCode()) || ToolKits.isNotEmpty(itemCategoryImportVO.getFatherItemCategoryName()))) {
				throw new PrologException("填写了上级商品品类信息后,上级商品品类层级不能为空!");
			}

			// 上级商品品类层级处理
			if (ToolKits.isNotEmpty(itemCategoryImportVO.getFatherItemCategoryLevel())) {
				ToolKits.isParamNull(itemCategoryImportVO.getFatherItemCategoryCode(), "上级商品品类编码不为空");
				ToolKits.isParamNull(itemCategoryImportVO.getFatherItemCategoryName(), "上级商品品类名称不为空");

				if (Integer.valueOf(itemCategoryImportVO.getItemCategoryLevel()) <= Integer.valueOf(itemCategoryImportVO.getFatherItemCategoryLevel())) {
					throw new ParameterException("商品品类编码【" + itemCategoryImportVO.getItemCategoryCode() + "】层级异常");
				}

				if (FileUtils.StringType(itemCategoryImportVO.getFatherItemCategoryLevel())) {
					// 数据字典code处理
					Assert.notNull(PublicMethods.change(restMessage, itemCategoryImportVO.getFatherItemCategoryLevel()), "上级商品品类层级【" + itemCategoryImportVO.getItemCategoryLevel() + "】不存在");
					tmsBaseItemCategoryModel.setFatherItemCategoryLevel(Integer.valueOf(itemCategoryImportVO.getFatherItemCategoryLevel()));
				} else {
					// 数据字典name处理
					Assert.notNull(PublicMethods.reverseChange(restMessage, itemCategoryImportVO.getFatherItemCategoryLevel()), "上级商品品类层级【" + itemCategoryImportVO.getItemCategoryLevel() + "】不存在");
					tmsBaseItemCategoryModel.setFatherItemCategoryLevel(Integer.valueOf(PublicMethods.reverseChange(restMessage, itemCategoryImportVO.getFatherItemCategoryLevel())));
				}

				// 查询上级商品品类资料
				Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
				Restriction r1 = Restrictions.eq("itemCategoryCode", itemCategoryImportVO.getFatherItemCategoryCode());
				Restriction r2 = Restrictions.eq("itemCategoryName", itemCategoryImportVO.getFatherItemCategoryName());
				Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2, r3));
				List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);
				Assert.isTrue(tmsBaseItemCategoryModels.size() > 0, "上级商品品类【" + itemCategoryImportVO.getFatherItemCategoryCode() + "】不存在");
				Assert.isTrue(tmsBaseItemCategoryModels.size() == 1, "上级商品品类【" + itemCategoryImportVO.getFatherItemCategoryCode() + "】存在重复数据");

				// 上级商品品类对象
				TmsBaseItemCategoryModel itemCategoryModel = tmsBaseItemCategoryModels.get(0);
				Assert.isTrue(itemCategoryModel.getStatus() == StatusConstant.STATUS_ENABLE, "上级商品品类【" + itemCategoryImportVO.getFatherItemCategoryCode() + "】已停用");

				// 赋值上级商品品类ID
				tmsBaseItemCategoryModel.setFatherItemCategoryId(itemCategoryModel.getItemCategoryId());
			}

			// 判断商品品类编码是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryCode", itemCategoryImportVO.getItemCategoryCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseItemCategoryMapper, "商品品类编码【" + itemCategoryImportVO.getItemCategoryCode() + "】已存在");
			PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryCode", itemCategoryImportVO.getItemCategoryCode(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseItemCategoryMapper, "商品品类编码【" + itemCategoryImportVO.getItemCategoryCode() + "】已停用,请直接启用");

			// 判断商品品类名称是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryName", itemCategoryImportVO.getItemCategoryName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_ENABLE, tmsBaseItemCategoryMapper, "商品品类名称【" + itemCategoryImportVO.getItemCategoryName() + "】已存在");
			PublicMethods.isThereAnyinformation(TmsBaseItemCategoryModel.class, "itemCategoryName", itemCategoryImportVO.getItemCategoryName(), "enterpriseId", user.getEnterpriseId(), "status", StatusConstant.STATUS_DISABLE, tmsBaseItemCategoryMapper, "商品品类名称【" + itemCategoryImportVO.getItemCategoryName() + "】已停用,请直接启用!");

			// 判断货主是否已存在
			CargoOwnerPopupVO popupVO = new CargoOwnerPopupVO();
			popupVO.setCargoOwnerName(itemCategoryImportVO.getCargoOwnerName());
			popupVO.setPageNum(Integer.valueOf(1));
			popupVO.setPageSize(Integer.valueOf(99));
			Page<CargoOwnerPopupDTO> cargoOwners = cargoOwnerService.popupCargoOwnerDataByName(popupVO);
			if (ToolKits.isEmpty(cargoOwners) || ToolKits.isEmpty(cargoOwners.getList())) {
				throw new PrologException("货主【" + itemCategoryImportVO.getCargoOwnerName() + "】在货主资料中不存在");
			} else {
				tmsBaseItemCategoryModel.setCargoOwnerId(cargoOwners.getList().get(0).getCargoOwnerId());
				tmsBaseItemCategoryModel.setCargoOwnerCode(cargoOwners.getList().get(0).getCargoOwnerCode());
				tmsBaseItemCategoryModel.setCargoOwnerName(cargoOwners.getList().get(0).getCargoOwnerName());
			}

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

			// 商品品类资料对象
			tmsBaseItemCategoryModel.setItemCategoryId(Long.valueOf(itemCategoryId));
			tmsBaseItemCategoryModel.setStatus(StatusConstant.STATUS_ENABLE);
			tmsBaseItemCategoryModel.setSource(DataDictionaryConstant.SOURCE_EXCEL);
			tmsBaseItemCategoryModel.setImportBatchNumber(importBatchNumber);
			tmsBaseItemCategoryModel.setEnterpriseId(user.getEnterpriseId());
			tmsBaseItemCategoryModel.setEnterpriseName(user.getEnterpriseName());
			tmsBaseItemCategoryModel.setCreateperson(user.getNickname());
			tmsBaseItemCategoryModel.setCreatetime(date);
			tmsBaseItemCategoryModel.setUpdateperson(user.getNickname());
			tmsBaseItemCategoryModel.setUpdatetime(date);
			tmsBaseItemCategoryMapper.save(tmsBaseItemCategoryModel);
			i++;
		}
		return i;
	}

	/**
	 * 商品品类导入撤销
	 *
	 * @param batchno
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void rollbackData(String batchno) {
		ToolKits.isParamNull(batchno, "商品品类导入撤销请求不为空");

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

		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
		Restriction r1 = Restrictions.eq("importBatchNumber", batchno);
		criteria.setRestriction(Restrictions.and(r1));
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);
		for (TmsBaseItemCategoryModel tmsBaseItemCategoryModel : tmsBaseItemCategoryModels) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("itemCategoryId", tmsBaseItemCategoryModel.getItemCategoryId());
			map.put("enterpriseId", user.getEnterpriseId());

			// 判断商品品类是否已经使用
			PublicMethods.doesItExistByMap(tmsBaseItemMapper, map, TmsBaseItemModel.class, "商品品类【" + tmsBaseItemCategoryModel.getItemCategoryId() + "】已使用,不能撤销");
		}

		tmsBaseItemCategoryMapper.deleteByCriteria(criteria);

	}

	/**
	 * 商品品类导入分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public RestMessage<Object> pageItemCategoryByImportBatchNumberData(ImportPageVO importPageVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 响应对象集合
		List<ItemCategoryImportPageDTO> itemCategoryImportPageDTOs = new ArrayList<ItemCategoryImportPageDTO>();

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

		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.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" }));
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);
		for (TmsBaseItemCategoryModel tmsBaseItemCategoryModel : tmsBaseItemCategoryModels) {
			// 响应对象
			ItemCategoryImportPageDTO itemCategoryImportPageDTO = new ItemCategoryImportPageDTO();
			BeanUtils.copyProperties(tmsBaseItemCategoryModel, itemCategoryImportPageDTO);
			itemCategoryImportPageDTOs.add(itemCategoryImportPageDTO);
		}

		Page<ItemCategoryImportPageDTO> page = PageUtils.getPage(itemCategoryImportPageDTOs);
		if (tmsBaseItemCategoryModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseItemCategoryModel> p0 = (com.github.pagehelper.Page<TmsBaseItemCategoryModel>) tmsBaseItemCategoryModels;
			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 ItemCategoryCheckDTO getItemCategoryByNameData(ItemCategoryCheckVO itemCategoryCheckVO) {
		ToolKits.isParamNull(itemCategoryCheckVO, "商品品类查看请求不为空");
		ToolKits.isParamNull(itemCategoryCheckVO.getItemCategoryName(), "商品品类名称不为空");

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

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

		Criteria criteria = Criteria.forClass(TmsBaseItemCategoryModel.class);
		Restriction r1 = Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryName, itemCategoryCheckVO.getItemCategoryName());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria);

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

		// 商品品类对象
		TmsBaseItemCategoryModel tmsBaseItemCategoryModel = tmsBaseItemCategoryModels.get(0);

		// 响应对象
		ItemCategoryCheckDTO itemCategoryCheckDTO = new ItemCategoryCheckDTO();
		BeanUtils.copyProperties(tmsBaseItemCategoryModel, itemCategoryCheckDTO);
		itemCategoryCheckDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(itemCategoryCheckDTO.getStatus())));

		return itemCategoryCheckDTO;
	}

	/**
	 * 商品品类接口数据处理
	 */
	@Override
	public List<IntfErrorLogDTO> saveItemCategoryInterfaceData(List<ItemCategorySaveVO> itemCategorySaveVOs) {
		ToolKits.isParamNull(itemCategorySaveVOs, "商品品类接口请求不为空");

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

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

		for (ItemCategorySaveVO itemCategorySaveVO : itemCategorySaveVOs) {
			// 响应对象
			IntfErrorLogDTO intfErrorLogDTO = new IntfErrorLogDTO();

			try {
				// 判断上级品类是否存在
				TmsBaseItemCategoryModel fatherItemCategory = new TmsBaseItemCategoryModel();
				if (ToolKits.isNotEmpty(itemCategorySaveVO.getFatherItemCategoryCode())) {
					Criteria criteria2 = Criteria.forClass(TmsBaseItemCategoryModel.class);
					Restriction r21 = Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryCode, itemCategorySaveVO.getFatherItemCategoryCode());
					Restriction r22 = Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryName, itemCategorySaveVO.getFatherItemCategoryName());
					Restriction r23 = Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryLevel, itemCategorySaveVO.getFatherItemCategoryLevel());
					Restriction r24 = Restrictions.eq(TmsBaseItemCategoryModel::getEnterpriseId, itemCategorySaveVO.getEnterpriseId());
					criteria2.setRestriction(Restrictions.and(r21, r22, r23, r24));
					List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModel2s = tmsBaseItemCategoryMapper.findByCriteria(criteria2);
					Assert.isTrue(tmsBaseItemCategoryModel2s.size() > 0, "上级商品品类【" + itemCategorySaveVO.getFatherItemCategoryName() + "】不存在");
					Assert.isTrue(tmsBaseItemCategoryModel2s.size() <= 1, "上级商品品类【" + itemCategorySaveVO.getFatherItemCategoryName() + "】资料重复");

					fatherItemCategory = tmsBaseItemCategoryModel2s.get(0);
					Assert.isTrue(fatherItemCategory.getStatus().intValue() == StatusConstant.STATUS_ENABLE.intValue(), "上级商品品类【" + itemCategorySaveVO.getFatherItemCategoryName() + "】未启用");
				}

				// 校验商品品类是否存在
				Criteria criteria1 = Criteria.forClass(TmsBaseItemCategoryModel.class);
				Restriction r11 = Restrictions.eq(TmsBaseItemCategoryModel::getItemCategoryCode, itemCategorySaveVO.getItemCategoryCode());
				Restriction r12 = Restrictions.eq(TmsBaseItemCategoryModel::getEnterpriseId, itemCategorySaveVO.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r12));
				List<TmsBaseItemCategoryModel> tmsBaseItemCategoryModels = tmsBaseItemCategoryMapper.findByCriteria(criteria1);
				if (ToolKits.isEmpty(tmsBaseItemCategoryModels)) {
					String itemCategoryId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.ITEM_CATEGORY_ID, "L");
					ToolKits.isParamNull(itemCategoryId, "商品品类ID生成失败");

					// 新增商品品类
					TmsBaseItemCategoryModel tmsBaseItemCategory = new TmsBaseItemCategoryModel();
					BeanUtils.copyProperties(itemCategorySaveVO, tmsBaseItemCategory);
					tmsBaseItemCategory.setItemCategoryId(Long.valueOf(itemCategoryId));
					tmsBaseItemCategory.setFatherItemCategoryId(fatherItemCategory.getItemCategoryId());
					tmsBaseItemCategory.setStatus(StatusConstant.STATUS_ENABLE);
					tmsBaseItemCategory.setEnterpriseId(itemCategorySaveVO.getEnterpriseId());
					tmsBaseItemCategory.setEnterpriseName(itemCategorySaveVO.getEnterpriseName());
					tmsBaseItemCategory.setCreateperson(itemCategorySaveVO.getCreateperson());
					tmsBaseItemCategory.setCreatetime(date);
					tmsBaseItemCategory.setUpdateperson(itemCategorySaveVO.getUpdateperson());
					tmsBaseItemCategory.setUpdatetime(date);
					tmsBaseItemCategoryMapper.save(tmsBaseItemCategory);
				} else {
					// 更新商品品类
					Assert.isTrue(tmsBaseItemCategoryModels.size() <= 1, "商品品类编码【" + itemCategorySaveVO.getItemCategoryCode() + "】资料维护重复");

					TmsBaseItemCategoryModel tmsBaseItemCategory = tmsBaseItemCategoryModels.get(0);
					tmsBaseItemCategory.setItemCategoryLevel(itemCategorySaveVO.getItemCategoryLevel());
					tmsBaseItemCategory.setItemCategoryName(itemCategorySaveVO.getItemCategoryName());
					tmsBaseItemCategory.setFatherItemCategoryId(fatherItemCategory.getItemCategoryId());
					tmsBaseItemCategory.setStatus(itemCategorySaveVO.getStatus());
					tmsBaseItemCategory.setUpdateperson(itemCategorySaveVO.getUpdateperson());
					tmsBaseItemCategory.setUpdatetime(date);
					tmsBaseItemCategoryMapper.update(tmsBaseItemCategory);
				}

				// 成功信息
				intfErrorLogDTO.setErrorInfo(null);
				intfErrorLogDTO.setDataId(itemCategorySaveVO.getItemCategoryId());
				intfErrorLogDTO.setStatus(true);
				intfErrorLogDTOs.add(intfErrorLogDTO);
			} catch (Exception e) {
				log.info("商品资料接口处理失败:{}", e);

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

		return intfErrorLogDTOs;
	}

}
