//package com.bonus.canteen.core.utils.excel;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.text.CharSequenceUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.alibaba.excel.context.AnalysisContext;
//import com.alibaba.excel.event.AnalysisEventListener;
//import com.alibaba.excel.exception.ExcelDataConvertException;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.bonus.canteen.core.allocation.api.AllocCanteenApi;
//import com.bonus.canteen.core.menu.constant.MaterialTypeEnum;
//import com.bonus.canteen.core.menu.constant.MenuConstant;
//import com.bonus.canteen.core.menu.constant.MenuFailTypeEnum;
//import com.bonus.canteen.core.menu.constant.MenuSalesTypeEnum;
//import com.bonus.canteen.core.menu.dto.MenuMaterialProductImportDTO;
//import com.bonus.canteen.core.menu.entity.MenuDishesImportFail;
//import com.bonus.canteen.core.menu.entity.MenuMaterial;
//import com.bonus.canteen.core.menu.entity.MenuMaterialCategory;
//import com.bonus.canteen.core.menu.mapper.MenuMaterialCategoryMapper;
//import com.bonus.canteen.core.menu.mapper.MenuMaterialMapper;
//import com.bonus.canteen.core.menu.service.MenuDishesService;
//import com.bonus.canteen.core.menu.service.MenuMaterialService;
//import com.bonus.common.core.exception.ServiceException;
//import com.bonus.common.houqin.constant.DelFlagEnum;
//import com.bonus.common.houqin.utils.id.Id;
//import com.google.common.collect.Lists;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.math.BigDecimal;
//import java.time.LocalDateTime;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
//public class MenuMaterialProductImportListener extends AnalysisEventListener<MenuMaterialProductImportDTO> {
//   private static final Logger log = LoggerFactory.getLogger(MenuMaterialProductImportListener.class);
//   List<String> materialNameList = Lists.newArrayList();
//   List<MenuDishesImportFail> materialImportFailList = Lists.newArrayList();
//   List<MenuMaterialProductImportDTO> materialImportDTOList = Lists.newArrayList();
//   MenuMaterialService menuMaterialService;
//   MenuDishesService menuDishesService;
//   Integer materialType;
//   MenuMaterialMapper menuMaterialMapper;
//   MenuMaterialCategoryMapper menuMaterialCategoryMapper;
//   AllocCanteenApi allocCanteenApi;
//
//   public MenuMaterialProductImportListener(MenuMaterialService menuMaterialService, MenuDishesService menuDishesService, Integer materialType, MenuMaterialMapper menuMaterialMapper, MenuMaterialCategoryMapper menuMaterialCategoryMapper, AllocCanteenApi allocCanteenApi) {
//      this.menuMaterialService = menuMaterialService;
//      this.menuDishesService = menuDishesService;
//      this.materialType = materialType;
//      this.menuMaterialMapper = menuMaterialMapper;
//      this.menuMaterialCategoryMapper = menuMaterialCategoryMapper;
//      this.allocCanteenApi = allocCanteenApi;
//   }
//
//   @Override
//   public void invoke(MenuMaterialProductImportDTO data, AnalysisContext context) {
//      Long areaId = this.allocCanteenApi.getAreaIdByName(data.getAreaName());
//      if (CharSequenceUtil.isNotEmpty(data.getAreaName()) && areaId == null) {
//         throw new ServiceException("区域不存在");
//      } else {
//         MenuDishesImportFail materialImportFail = new MenuDishesImportFail();
//         int row = context.readRowHolder().getRowIndex() + 1;
//         materialImportFail.setFailRow(row);
//         materialImportFail.setFailType(MenuFailTypeEnum.PRODUCT.key());
//         materialImportFail.setImportTime(LocalDateTime.now());
//         if (ObjectUtil.isEmpty(data.getMaterialName())) {
//            materialImportFail.setFailReason("商品名称不存在!");
//            this.materialImportFailList.add(materialImportFail);
//         } else if (ObjectUtil.isEmpty(data.getUnitName())) {
//            materialImportFail.setFailReason("商品单位不存在!");
//            this.materialImportFailList.add(materialImportFail);
//         } else if (CharSequenceUtil.isBlank(data.getCategoryName())) {
//            materialImportFail.setFailReason("商品类别不存在");
//            this.materialImportFailList.add(materialImportFail);
//         } else {
//            List<MenuMaterialProductImportDTO> checkInsertMaterialUnit = this.materialImportDTOList.stream().filter((s) -> {
//               return CharSequenceUtil.isNotBlank(s.getUnitName()) && ObjectUtil.equal(s.getUnitName(), data.getUnitName());
//            }).filter((s) -> {
//               return s.getMaterialName().equals(data.getMaterialName());
//            }).collect(Collectors.toList());
//            if (!checkInsertMaterialUnit.isEmpty()) {
//               materialImportFail.setFailReason("商品名称单位组合重复");
//               this.materialImportFailList.add(materialImportFail);
//            } else {
//               if (CharSequenceUtil.isNotBlank(data.getBarCode())) {
//                  List<MenuMaterialProductImportDTO> checkInsertBarCode = this.materialImportDTOList.stream().filter((s) -> {
//                     return CharSequenceUtil.isNotEmpty(s.getBarCode()) && s.getBarCode().equals(data.getBarCode());
//                  }).collect(Collectors.toList());
//                  if (!checkInsertBarCode.isEmpty()) {
//                     materialImportFail.setFailReason("条码重复");
//                     this.materialImportFailList.add(materialImportFail);
//                     return;
//                  }
//               }
//
//               if (this.materialType.equals(MaterialTypeEnum.GOODS.key()) && ObjectUtil.isEmpty(data.getSalesMode())) {
//                  materialImportFail.setFailReason("商品售卖类型不存在");
//                  this.materialImportFailList.add(materialImportFail);
//               } else if (!ObjectUtil.isEmpty(data.getSalesMode()) && (Objects.equals(data.getSalesMode(), MenuSalesTypeEnum.PORTION_ON.key()) || Objects.equals(data.getSalesMode(), MenuSalesTypeEnum.WEIGHT_ON.key()))) {
//                  MenuMaterial material = this.menuMaterialMapper.getMaterialByNameAndUnit(data.getMaterialName(), data.getUnitName(), DelFlagEnum.DEL_FALSE.key(), areaId);
//                  Long categoryId;
//                  if (ObjectUtil.isNotEmpty(material)) {
//                     try {
//                        BeanUtil.copyProperties(data, material, new String[0]);
//                        Integer salePrice;
//                        if (data.getUnitPrice() != null) {
//                           salePrice = data.getUnitPrice().multiply(new BigDecimal(100)).intValue();
//                           material.setUnitPrice(salePrice);
//                        }
//
//                        if (data.getSalePrice() != null) {
//                           salePrice = data.getSalePrice().multiply(new BigDecimal(100)).intValue();
//                           material.setSalePrice(salePrice);
//                        }
//
//                        categoryId = this.menuMaterialCategoryMapper.selectCategoryIdByWrapper(Wrappers.lambdaQuery(MenuMaterialCategory.class)
//                                .eq(MenuMaterialCategory::getDelFlag, DelFlagEnum.DEL_FALSE.key())
//                                .eq(areaId != null, MenuMaterialCategory::getAreaId, areaId)
//                                .isNull(areaId == null, MenuMaterialCategory::getAreaId)
//                                .eq(MenuMaterialCategory::getCategoryName, data.getCategoryName()));
//                        log.info("查询类别:{},{}", categoryId, data.getCategoryName());
//                        if (categoryId == null) {
//                           Long parentId = this.menuMaterialCategoryMapper.selectIdByType(this.materialType);
//                           MenuMaterialCategory menuMaterialCategory;
//                           if (parentId == null) {
//                              menuMaterialCategory = new MenuMaterialCategory();
//                              menuMaterialCategory.setCategoryId(Id.next());
//                              menuMaterialCategory.setCategoryName("商品");
//                              menuMaterialCategory.setParentId(-1L);
//                              menuMaterialCategory.setCategoryType(this.materialType);
//                              this.menuMaterialCategoryMapper.insert(menuMaterialCategory);
//                              parentId = menuMaterialCategory.getCategoryId();
//                           }
//
//                           menuMaterialCategory = new MenuMaterialCategory();
//                           menuMaterialCategory.setCategoryId(Id.next());
//                           menuMaterialCategory.setCategoryName(data.getCategoryName());
//                           menuMaterialCategory.setParentId(parentId);
//                           menuMaterialCategory.setAreaId(areaId);
//                           menuMaterialCategory.setCategoryType(this.materialType);
//                           this.menuMaterialCategoryMapper.insert(menuMaterialCategory);
//                           material.setCategoryId(menuMaterialCategory.getCategoryId());
//                        } else {
//                           material.setCategoryId(categoryId);
//                        }
//
//                        material.setSalesMode(data.getSalesMode());
//                        material.setAreaId(areaId);
//                        this.menuMaterialMapper.updateById(material);
//                        log.info("更新原料:{}", material);
//                     } catch (Exception var11) {
//                        materialImportFail.setFailReason(var11.getMessage());
//                        this.materialImportFailList.add(materialImportFail);
//                     }
//
//                  } else {
//                     if (CharSequenceUtil.isNotBlank(data.getBarCode())) {
//                        categoryId = this.menuMaterialMapper.selectCount(Wrappers.lambdaQuery(MenuMaterial.class).eq(MenuMaterial::getBarCode, data.getBarCode())
//                                .eq(areaId != null, MenuMaterial::getAreaId, areaId).eq(MenuMaterial::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//                        if (categoryId > 0L) {
//                           materialImportFail.setFailReason("数据库条码重复");
//                           this.materialImportFailList.add(materialImportFail);
//                           return;
//                        }
//                     }
//
//                     data.setMaterialType(this.materialType);
//                     data.setAreaId(areaId);
//                     this.materialImportDTOList.add(data);
//                     if (this.materialImportDTOList.size() >= MenuConstant.INSERT_NUM) {
//                        this.menuMaterialService.importMaterialProduct(this.materialImportDTOList);
//                        this.materialImportDTOList.clear();
//                     }
//
//                  }
//               } else {
//                  materialImportFail.setFailReason("商品计重类型错误");
//                  this.materialImportFailList.add(materialImportFail);
//               }
//            }
//         }
//      }
//   }
//
//   @Override
//   public void onException(Exception exception, AnalysisContext context) throws Exception {
//      if (exception instanceof ExcelDataConvertException) {
//         ExcelDataConvertException exp = (ExcelDataConvertException) exception;
//         int columnIndex = exp.getColumnIndex() + 1;
//         int rowIndex = exp.getRowIndex() + 1;
//         String message = "第" + rowIndex + "行,第" + columnIndex + "列数据格式有误";
//         MenuDishesImportFail materialImportFail = new MenuDishesImportFail();
//         materialImportFail.setFailRow(rowIndex);
//         materialImportFail.setFailType(MenuFailTypeEnum.MATERIAL.key());
//         materialImportFail.setImportTime(LocalDateTime.now());
//         materialImportFail.setFailReason(message);
//         this.materialImportFailList.add(materialImportFail);
//      } else {
//         super.onException(exception, context);
//      }
//
//   }
//
//   @Override
//   public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//      this.materialNameList.clear();
//      this.menuMaterialService.importMaterialProduct(this.materialImportDTOList);
//      this.menuDishesService.importDishesFailSave(this.materialImportFailList);
//   }
//}
