package com.bonus.canteen.core.supply.purchase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.menu.domain.MenuMaterial;
import com.bonus.canteen.core.menu.mapper.MenuMaterialMapper;
import com.bonus.canteen.core.supply.purchase.constants.DrpApproveStatusEnum;
import com.bonus.canteen.core.supply.purchase.constants.DrpInquiryStatusEnum;
import com.bonus.canteen.core.supply.purchase.constants.DrpMaterialTenderEnum;
import com.bonus.canteen.core.supply.purchase.constants.DrpOrderGoodsStatusEnum;
import com.bonus.canteen.core.supply.purchase.domain.bo.*;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpGeneratePurPlanDetailDTO;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpProductionGeneratePurPlanDTO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpPurPlanDetailPageVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpPurchasePlanPageVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpSupplierSimpleVo;
import com.bonus.canteen.core.supply.purchase.mapper.*;
import com.bonus.canteen.core.supply.purchase.service.DrpMenuMaterialSupplierService;
import com.bonus.canteen.core.supply.purchase.service.DrpPurchasePlanDisassemblyService;
import com.bonus.canteen.core.supply.purchase.utils.CustomIdGenerateUtil;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.utils.id.Id;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DrpPurchasePlanDisassemblyServiceImpl implements DrpPurchasePlanDisassemblyService {
   private static final Logger log = LoggerFactory.getLogger(DrpPurchasePlanDisassemblyServiceImpl.class);
   @Autowired
   @Lazy
   private DrpPurchasePlanMapper drpPurchasePlanMapper;
   @Autowired
   @Lazy
   private DrpPurPlanDetailMapper drpPurPlanDetailMapper;
   @Autowired
   @Lazy
   private DrpPurchaseInquiryMapper drpPurchaseInquiryMapper;
   @Autowired
   @Lazy
   private DrpPurchaseInquiryDetailMapper drpPurchaseInquiryDetailMapper;
   @Autowired
   @Lazy
   private DrpPurchaseInquirySupplierMapper drpPurchaseInquirySupplierMapper;
   @Autowired
   @Lazy
   private DrpPurchaseQuoteMapper drpPurchaseQuoteMapper;
   @Autowired
   @Lazy
   private DrpPurchaseOrderGoodsMapper drpPurchaseOrderGoodsMapper;
   @Autowired
   @Lazy
   private DrpOrderGoodsDetailMapper drpOrderGoodsDetailMapper;
   @Autowired
   @Lazy
   private DrpProductionPlanMapper drpProductionPlanMapper;
   @Autowired
   @Lazy
   private DrpWarehouseMapper drpWarehouseMapper;
   @Autowired
   @Lazy
   private MenuMaterialMapper menuMaterialMapper;
   @Autowired
   @Lazy
   private DrpUnitMapper drpUnitMapper;
   @Autowired
   @Lazy
   private DrpProductionPurchaseOrderMapper drpProductionPurchaseOrderMapper;
//   @Autowired
//   @Lazy
//   private DrpIntoInventoryMapper drpIntoInventoryMapper;
//   @Autowired
//   @Lazy
//   private DrpMenuMaterialSupplierMapper drpMenuMaterialSupplierMapper;
//   @Autowired
//   @Lazy
//   private DrpMaterialWinBidRecordsService drpMaterialWinBidRecordsService;
//   @Autowired
//   @Lazy
//   private SupermarketProductMapper supermarketProductMapper;
   @Autowired
   @Lazy
   private DrpMenuMaterialSupplierService drpMenuMaterialSupplierService;
//   @Autowired
//   @Lazy
//   private AllocCanteenApi allocCanteenApi;

   @Transactional
   public String directGenerateInquiry(String content) {
      DrpPurchasePlanPageVO drpPurchasePlanPageVO = this.drpPurchasePlanMapper.selectByPlanId(content, DelFlagEnum.DEL_FALSE.key());
      List<DrpPurPlanDetailPageVO> detailList = this.drpPurPlanDetailMapper.selectListByPlanId(content);
      DrpInquiry drpInquiry = new DrpInquiry();
      drpInquiry.setAreaId(drpPurchasePlanPageVO.getAreaId());
      String inquiryId = CustomIdGenerateUtil.generateInquiryId();
      drpInquiry.setInquiryId(inquiryId);
      drpInquiry.setStatus(DrpInquiryStatusEnum.NO_COMMIT.key());
      this.drpPurchaseInquiryMapper.insert(drpInquiry);
      List<DrpInquiryDetail> inquiryDetails = new ArrayList();
      Map<String, DrpPurPlanDetailPageVO> mergeMap = detailList.stream().collect(Collectors.toMap((detail) -> {
         Long var10000 = detail.getMaterialId();
         return "" + var10000 + "-" + detail.getUnitId();
      }, Function.identity(), (key1, key2) -> {
         key1.setPurNum(Optional.ofNullable(key1.getPurNum()).orElse(BigDecimal.ZERO).add(Optional.ofNullable(key2.getPurNum()).orElse(BigDecimal.ZERO)));
         return key1;
      }));
      Iterator var8 = mergeMap.values().iterator();

      while(var8.hasNext()) {
         DrpPurPlanDetailPageVO drpPurPlanDetailPageVO = (DrpPurPlanDetailPageVO)var8.next();
         DrpInquiryDetail drpInquiryDetail = new DrpInquiryDetail();
         BeanUtil.copyProperties(drpPurPlanDetailPageVO, drpInquiryDetail, new String[0]);
         drpInquiryDetail.setInquiryDetailId(Id.next());
         drpInquiryDetail.setInquiryId(inquiryId);
         this.drpPurchaseInquiryDetailMapper.insert(drpInquiryDetail);
         inquiryDetails.add(drpInquiryDetail);
      }

      if (CharSequenceUtil.isEmpty(drpPurchasePlanPageVO.getSupplierIds())) {
         this.saveInquiryAndSendMsgToSupplier(new ArrayList(), inquiryId, inquiryDetails);
      } else {
         this.saveInquiryAndSendMsgToSupplier(Arrays.stream(Optional.ofNullable(drpPurchasePlanPageVO.getSupplierIds()).orElse("").split(",")).map(Long::parseLong).collect(Collectors.toList()), inquiryId, inquiryDetails);
      }

      return inquiryId;
   }

//   @Transactional
//   public String disassemblyGenerateInquiry(DisassemblyGenerateInquiryDTO content) {
//      if (content == null) {
//         throw new LeException(I18n.getMessage("drp.pur-generate-order-not-empty", new Object[0]));
//      } else {
//         Map<String, DrpPurDismantleGenerateInquiryMaterialDTO> mergeMap = (Map)content.getMaterialList().stream().collect(Collectors.toMap((detail) -> {
//            Long var10000 = detail.getMaterialId();
//            return "" + var10000 + "-" + detail.getUnitId();
//         }, Function.identity(), (key1, key2) -> {
//            key1.setOrderNum(((BigDecimal)Optional.ofNullable(key1.getOrderNum()).orElse(BigDecimal.ZERO)).add((BigDecimal)Optional.ofNullable(key2.getOrderNum()).orElse(BigDecimal.ZERO)));
//            return key1;
//         }));
//         AllocCanteen allocCanteen = this.allocCanteenApi.getAllocCanteen(content.getCanteenId());
//         content.setAreaId(allocCanteen != null ? allocCanteen.getAreaId() : null);
//         content.setMaterialList(new ArrayList(mergeMap.values()));
//         List materialList;
//         if (content.getDisassemblyMethod().equals(DrpMaterialTenderEnum.DisassemblyMethod.CATEGORY.key())) {
//            materialList = content.getMaterialList();
//            if (content.getIfMergeSupplier() != null && LeConstants.COMMON_YES.equals(content.getIfMergeSupplier())) {
//               this.mergeSupplierDisassemblyInquiry(content, materialList);
//            } else {
//               Map<Long, List<DrpPurDismantleGenerateInquiryMaterialDTO>> categoryListMap = (Map)materialList.stream().collect(Collectors.groupingBy(DrpPurDismantleGenerateInquiryMaterialDTO::getCategoryId));
//               categoryListMap.forEach((key, value) -> {
//                  String inquiryId = CustomIdGenerateUtil.generateInquiryId();
//                  List<DrpInquiryDetail> drpInquiryDetails = this.packageInquiryDetails(inquiryId, content, value);
//                  this.saveInquiryAndSendMsgToSupplier(((DrpPurDismantleGenerateInquiryMaterialDTO)value.get(0)).getSupplierIds(), inquiryId, drpInquiryDetails);
//               });
//            }
//         } else if (content.getIfMergeSupplier() != null && LeConstants.COMMON_YES.equals(content.getIfMergeSupplier())) {
//            materialList = content.getMaterialList();
//            this.mergeSupplierDisassemblyInquiry(content, materialList);
//         } else if (CollUtil.isNotEmpty(content.getMaterialList())) {
//            Iterator var8 = content.getMaterialList().iterator();
//
//            while(var8.hasNext()) {
//               DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var8.next();
//               String inquiryId = CustomIdGenerateUtil.generateInquiryId();
//               List<DrpInquiryDetail> drpInquiryDetails = this.packageInquiryDetails(inquiryId, content, Collections.singletonList(drpPurDismantleGenerateInquiryMaterialDTO));
//               this.saveInquiryAndSendMsgToSupplier(drpPurDismantleGenerateInquiryMaterialDTO.getSupplierIds(), inquiryId, drpInquiryDetails);
//            }
//         }
//
//         return null;
//      }
//   }
//
//   protected List<DrpInquiryDetail> packageInquiryDetails(String inquiryId, DisassemblyGenerateInquiryDTO content, List<DrpPurDismantleGenerateInquiryMaterialDTO> value) {
//      this.packageInquiry(inquiryId, content);
//      List<DrpInquiryDetail> inquiryDetails = new ArrayList();
//      this.iterationInquiryDetail(inquiryId, value, inquiryDetails);
//      return inquiryDetails;
//   }
//
//   public void iterationInquiryDetail(String inquiryId, List<DrpPurDismantleGenerateInquiryMaterialDTO> value, List<DrpInquiryDetail> inquiryDetails) {
//      Iterator var4 = value.iterator();
//
//      while(var4.hasNext()) {
//         DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var4.next();
//         this.insertInquiryDetail(inquiryId, inquiryDetails, drpPurDismantleGenerateInquiryMaterialDTO);
//      }
//
//   }
//
//   protected void insertInquiryDetail(String inquiryId, List<DrpInquiryDetail> inquiryDetails, DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO) {
//      DrpInquiryDetail drpInquiryDetail = new DrpInquiryDetail();
//      BeanUtil.copyProperties(drpPurDismantleGenerateInquiryMaterialDTO, drpInquiryDetail, new String[0]);
//      drpInquiryDetail.setInquiryDetailId(Id.next());
//      drpInquiryDetail.setInquiryId(inquiryId);
//      drpInquiryDetail.setPurNum(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum());
//      this.drpInquiryDetailMapper.insert(drpInquiryDetail);
//      inquiryDetails.add(drpInquiryDetail);
//   }
//
//   protected void mergeSupplierDisassemblyInquiry(DisassemblyGenerateInquiryDTO content, List<DrpPurDismantleGenerateInquiryMaterialDTO> materialList) {
//      Map<List<Long>, List<DrpPurDismantleGenerateInquiryMaterialDTO>> supplierIdsByMaterialId = (Map)materialList.stream().collect(Collectors.groupingBy(DrpPurDismantleGenerateInquiryMaterialDTO::getSupplierIds));
//      if (CollUtil.isNotEmpty(supplierIdsByMaterialId)) {
//         supplierIdsByMaterialId.forEach((key, value) -> {
//            String inquiryId = CustomIdGenerateUtil.generateInquiryId();
//            List<DrpInquiryDetail> drpInquiryDetails = this.packageInquiryDetails(inquiryId, content, value);
//            this.saveInquiryAndSendMsgToSupplier(key, inquiryId, drpInquiryDetails);
//         });
//      }
//
//   }
//
//   public void disassemblyGenerateInquiryManual(List<DisassemblyGenerateInquiryDTO> content) {
//      if (CollUtil.isNotEmpty(content)) {
//         content.forEach((dto) -> {
//            Map<String, DrpPurDismantleGenerateInquiryMaterialDTO> mergeMap = (Map)dto.getMaterialList().stream().collect(Collectors.toMap((detail) -> {
//               Long var10000 = detail.getMaterialId();
//               return "" + var10000 + "-" + detail.getUnitId();
//            }, Function.identity(), (key1, key2) -> {
//               key1.setOrderNum(((BigDecimal)Optional.ofNullable(key1.getOrderNum()).orElse(BigDecimal.ZERO)).add((BigDecimal)Optional.ofNullable(key2.getOrderNum()).orElse(BigDecimal.ZERO)));
//               return key1;
//            }));
//            AllocCanteen allocCanteen = this.allocCanteenApi.getAllocCanteen(dto.getCanteenId());
//            dto.setAreaId(allocCanteen != null ? allocCanteen.getAreaId() : null);
//            dto.setMaterialList(new ArrayList(mergeMap.values()));
//            String inquiryId = CustomIdGenerateUtil.generateInquiryId();
//            this.packageInquiry(inquiryId, dto);
//            List<DrpInquiryDetail> inquiryDetails = new ArrayList();
//            Iterator var6 = dto.getMaterialList().iterator();
//
//            while(var6.hasNext()) {
//               DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var6.next();
//               this.insertInquiryDetail(inquiryId, inquiryDetails, drpPurDismantleGenerateInquiryMaterialDTO);
//            }
//
//            this.saveInquiryAndSendMsgToSupplier(dto.getSupplierIds(), inquiryId, inquiryDetails);
//         });
//      }
//
//   }

   public String directGenerateOrder(String content) {
      DrpPurchasePlanPageVO drpPurchasePlanPageVO = this.drpPurchasePlanMapper.selectByPlanId(content, DelFlagEnum.DEL_FALSE.key());
      List<DrpPurPlanDetailPageVO> detailList = this.drpPurPlanDetailMapper.selectListByPlanId(content);
      DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
      String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
      drpOrderGoods.setOrderGoodsId(orderGoodsId);
      drpOrderGoods.setSupplierId(null);
      drpOrderGoods.setOrderTitle("采购计划单生成采购订单");
      drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
      drpOrderGoods.setCanteenId(drpPurchasePlanPageVO.getCanteenId());
      drpOrderGoods.setStallId(drpPurchasePlanPageVO.getStallId());
      drpOrderGoods.setWarehouseId(this.getWarehouseId(drpPurchasePlanPageVO.getCanteenId()));
      drpOrderGoods.setRemark(drpPurchasePlanPageVO.getRemark());
      drpOrderGoods.setRequestArrivalTime(drpPurchasePlanPageVO.getDeliverGoodsDate());
      this.drpPurchaseOrderGoodsMapper.insert(drpOrderGoods);
      List<Long> materialIds = detailList.stream().map(DrpPurPlanDetailPageVO::getMaterialId).collect(Collectors.toList());
      List<MenuMaterial> menuMaterials = this.menuMaterialMapper.selectMenuMaterialMaterialIdList(materialIds.toArray(new Long[0]));
      Map<Long, Long> collect = menuMaterials.stream().collect(Collectors.toMap(MenuMaterial::getMaterialId, MenuMaterial::getUnitPrice, (key1, key2) -> {
         return key1;
      }));
      Iterator var9 = detailList.iterator();

      while(var9.hasNext()) {
         DrpPurPlanDetailPageVO drpPurPlanDetailPageVO = (DrpPurPlanDetailPageVO)var9.next();
         DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
         BeanUtil.copyProperties(drpPurPlanDetailPageVO, drpOrderGoodsDetail);
         drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
         Integer unitPrice = collect.get(drpPurPlanDetailPageVO.getMaterialId()) != null ? collect.get(drpPurPlanDetailPageVO.getMaterialId()).intValue() : 0;
         drpOrderGoodsDetail.setSinglePrice(unitPrice);
         drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurPlanDetailPageVO.getPurNum()).setScale(0, RoundingMode.HALF_UP).intValue());
         drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
         drpOrderGoodsDetail.setOrderNum(drpPurPlanDetailPageVO.getPurNum());
         drpOrderGoodsDetail.setRemark(drpPurPlanDetailPageVO.getRemark());
         this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
      }

      DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
      drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
      drpProductionPurchaseOrder.setPlanId(content);
      drpProductionPurchaseOrder.setOrderType(2);
      this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
      return orderGoodsId;
   }
//
//   @Transactional
//   public String disassemblyGenerateOrder(DisassemblyGenerateInquiryDTO content) {
//      List materialList;
//      if (content.getDisassemblyMethod().equals(DrpMaterialTenderEnum.DisassemblyMethod.CATEGORY.key())) {
//         materialList = content.getMaterialList();
//         if (content.getIfMergeSupplier() != null && LeConstants.COMMON_YES.equals(content.getIfMergeSupplier())) {
//            this.createInquiryOrder(content, materialList);
//         } else {
//            Map<Long, List<DrpPurDismantleGenerateInquiryMaterialDTO>> categoryListMap = (Map)materialList.stream().collect(Collectors.groupingBy(DrpPurDismantleGenerateInquiryMaterialDTO::getCategoryId));
//            categoryListMap.forEach((key, value) -> {
//               String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
//               String stallIds = (String)value.stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getStallId).distinct().map(Object::toString).collect(Collectors.joining(","));
//               this.saveOrderGood(orderGoodsId, content, (Long)((DrpPurDismantleGenerateInquiryMaterialDTO)value.get(0)).getSupplierIds().get(0), stallIds);
//               Map<String, Integer> materialPrice = this.getMaterialPrice((Long)((DrpPurDismantleGenerateInquiryMaterialDTO)value.get(0)).getSupplierIds().get(0), value.stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getMaterialId).toList());
//               BigDecimal orderAmount = BigDecimal.ZERO;
//               Iterator var8 = value.iterator();
//
//               while(var8.hasNext()) {
//                  DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var8.next();
//                  DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
//                  BeanUtil.copyProperties(drpPurDismantleGenerateInquiryMaterialDTO, drpOrderGoodsDetail, new String[0]);
//                  drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
//                  drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
//                  Long var10001 = drpPurDismantleGenerateInquiryMaterialDTO.getMaterialId();
//                  Integer price = (Integer)materialPrice.get("" + var10001 + "-" + drpPurDismantleGenerateInquiryMaterialDTO.getUnitId());
//                  drpOrderGoodsDetail.setSinglePrice(price != null ? price : 0);
//                  drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum()).setScale(0, RoundingMode.HALF_UP).intValue());
//                  drpOrderGoodsDetail.setOrderNum(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum());
//                  drpOrderGoodsDetail.setRemark(drpPurDismantleGenerateInquiryMaterialDTO.getRemark());
//                  orderAmount = orderAmount.add(new BigDecimal(drpOrderGoodsDetail.getTotalPrice()));
//                  this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
//               }
//
//               if (CharSequenceUtil.isNotEmpty(content.getPlanIdList())) {
//                  String[] split = content.getPlanIdList().split(",");
//                  String[] var15 = split;
//                  int var16 = split.length;
//
//                  for(int var17 = 0; var17 < var16; ++var17) {
//                     String s = var15[var17];
//                     DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
//                     drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
//                     drpProductionPurchaseOrder.setPlanId(s);
//                     drpProductionPurchaseOrder.setOrderType(2);
//                     this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
//                  }
//               }
//
//            });
//         }
//      } else if (content.getIfMergeSupplier() != null && LeConstants.COMMON_YES.equals(content.getIfMergeSupplier())) {
//         materialList = content.getMaterialList();
//         this.createInquiryOrder(content, materialList);
//      } else if (CollUtil.isNotEmpty(content.getMaterialList())) {
//         Iterator var14 = content.getMaterialList().iterator();
//
//         while(true) {
//            String orderGoodsId;
//            do {
//               if (!var14.hasNext()) {
//                  return null;
//               }
//
//               DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var14.next();
//               orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
//               this.saveOrderGood(orderGoodsId, content, (Long)drpPurDismantleGenerateInquiryMaterialDTO.getSupplierIds().get(0), String.valueOf(drpPurDismantleGenerateInquiryMaterialDTO.getStallId()));
//               Map<String, Integer> materialPrice = this.getMaterialPrice((Long)drpPurDismantleGenerateInquiryMaterialDTO.getSupplierIds().get(0), Collections.singletonList(drpPurDismantleGenerateInquiryMaterialDTO.getMaterialId()));
//               DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
//               BeanUtil.copyProperties(drpPurDismantleGenerateInquiryMaterialDTO, drpOrderGoodsDetail, new String[0]);
//               drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
//               drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
//               Long var10001 = drpPurDismantleGenerateInquiryMaterialDTO.getMaterialId();
//               Integer price = (Integer)materialPrice.get("" + var10001 + "-" + drpPurDismantleGenerateInquiryMaterialDTO.getUnitId());
//               drpOrderGoodsDetail.setSinglePrice(price != null ? price : 0);
//               drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum()).setScale(0, RoundingMode.HALF_UP).intValue());
//               drpOrderGoodsDetail.setOrderNum(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum());
//               drpOrderGoodsDetail.setRemark(drpPurDismantleGenerateInquiryMaterialDTO.getRemark());
//               this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
//            } while(!CharSequenceUtil.isNotEmpty(content.getPlanIdList()));
//
//            String[] split = content.getPlanIdList().split(",");
//            String[] var9 = split;
//            int var10 = split.length;
//
//            for(int var11 = 0; var11 < var10; ++var11) {
//               String s = var9[var11];
//               DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
//               drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
//               drpProductionPurchaseOrder.setPlanId(s);
//               drpProductionPurchaseOrder.setOrderType(2);
//               this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
//            }
//         }
//      }
//
//      return null;
//   }
//
//   protected Map<String, Integer> getMaterialPrice(Long supplierId, List<Long> materialIdList) {
//      DrpMaterialSupplierWinRecordsDTO dto = new DrpMaterialSupplierWinRecordsDTO();
//      dto.setSupplierId(supplierId);
//      dto.setMaterialIdList(materialIdList);
//      List<DrpMaterialSupplierWinRecordsVO> drpMaterialSupplierWinRecordsVOS = this.drpMaterialWinBidRecordsService.listSupplierWinRecords(dto);
//      return (Map)drpMaterialSupplierWinRecordsVOS.stream().collect(Collectors.toMap((winRecordsVO) -> {
//         Long var10000 = winRecordsVO.getMaterialId();
//         return "" + var10000 + "-" + winRecordsVO.getUnitId();
//      }, DrpMaterialSupplierWinRecordsVO::getWinPrice, (key1, key2) -> {
//         return key1;
//      }));
//   }
//
//   protected Map<Long, DrpPriceByMaterialIdVO> getSinglePrice(DisassemblyGenerateInquiryDTO content) {
//      List<Long> materialIds = content.getMaterialList().stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getMaterialId).toList();
//      List<DrpPriceByMaterialIdVO> priceByMaterialIdVOList = this.drpIntoInventoryMapper.getPriceByMaterialIds(materialIds, (Long)null);
//      return (Map)priceByMaterialIdVOList.stream().collect(Collectors.toMap(DrpPriceByMaterialIdVO::getMaterialId, (e) -> {
//         return e;
//      }, (key1, key2) -> {
//         return key1;
//      }));
//   }
//
//   protected void orderGoodDetail(DisassemblyGenerateInquiryDTO content, String orderGoodsId) {
//      Map<String, Integer> materialPrice = this.getMaterialPrice((Long)content.getSupplierIds().get(0), content.getMaterialList().stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getMaterialId).toList());
//      BigDecimal orderAmount = BigDecimal.ZERO;
//      Iterator var5 = content.getMaterialList().iterator();
//
//      while(var5.hasNext()) {
//         DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var5.next();
//         DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
//         BeanUtil.copyProperties(drpPurDismantleGenerateInquiryMaterialDTO, drpOrderGoodsDetail, new String[0]);
//         drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
//         drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
//         drpOrderGoodsDetail.setRemark(drpPurDismantleGenerateInquiryMaterialDTO.getRemark());
//         drpOrderGoodsDetail.setOrderNum(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum());
//         Long var10001 = drpPurDismantleGenerateInquiryMaterialDTO.getMaterialId();
//         Integer price = (Integer)materialPrice.get("" + var10001 + "-" + drpPurDismantleGenerateInquiryMaterialDTO.getUnitId());
//         drpOrderGoodsDetail.setSinglePrice(price != null ? price : 0);
//         drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum()).setScale(0, RoundingMode.HALF_UP).intValue());
//         orderAmount = orderAmount.add(new BigDecimal(drpOrderGoodsDetail.getTotalPrice()));
//         this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
//      }
//
//   }
//
//   protected void saveOrderGood(String orderGoodsId, DisassemblyGenerateInquiryDTO content, Long supplierId, String stallId) {
//      DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
//      drpOrderGoods.setOrderGoodsId(orderGoodsId);
//      drpOrderGoods.setSupplierId(supplierId);
//      drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
//      drpOrderGoods.setRequestArrivalTime(content.getRequestArrivalTime());
//      drpOrderGoods.setCanteenId(content.getCanteenId());
//      drpOrderGoods.setStallId(stallId);
//      drpOrderGoods.setRemark(content.getRemark());
//      drpOrderGoods.setOrderTitle("采购计划单生成采购订单");
//      drpOrderGoods.setWarehouseId(this.getWarehouseId(content.getCanteenId()));
//      drpOrderGoods.setSupplyAddress(this.getSupplyAddressByWarehouseId(drpOrderGoods.getWarehouseId()));
//      this.drpOrderGoodsMapper.insert(drpOrderGoods);
//   }
//
//   protected String getSupplyAddressByWarehouseId(Long warehouseId) {
//      DrpWarehouse drpWarehouse = (DrpWarehouse)this.drpWarehouseMapper.selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, warehouseId)).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//      return drpWarehouse != null ? drpWarehouse.getRegionProvince() + drpWarehouse.getRegionCity() + drpWarehouse.getRegionDistrict() + (drpWarehouse.getAddress() == null ? "" : drpWarehouse.getAddress()) : "";
//   }
//
//   protected void createInquiryOrder(DisassemblyGenerateInquiryDTO content, List<DrpPurDismantleGenerateInquiryMaterialDTO> materialList) {
//      Map<List<Long>, List<DrpPurDismantleGenerateInquiryMaterialDTO>> supplierIdsByMaterialId = (Map)materialList.stream().collect(Collectors.groupingBy(DrpPurDismantleGenerateInquiryMaterialDTO::getSupplierIds));
//      if (CollUtil.isNotEmpty(supplierIdsByMaterialId)) {
//         supplierIdsByMaterialId.forEach((key, value) -> {
//            String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
//            DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
//            drpOrderGoods.setOrderGoodsId(orderGoodsId);
//            drpOrderGoods.setSupplierId((Long)key.get(0));
//            drpOrderGoods.setOrderTitle("采购计划单生成采购订单");
//            drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
//            drpOrderGoods.setRequestArrivalTime(content.getRequestArrivalTime());
//            drpOrderGoods.setCanteenId(content.getCanteenId());
//            drpOrderGoods.setWarehouseId(this.getWarehouseId(content.getCanteenId()));
//            drpOrderGoods.setSupplyAddress(this.getSupplyAddressByWarehouseId(drpOrderGoods.getWarehouseId()));
//            drpOrderGoods.setRemark(content.getRemark());
//            String stallIds = (String)value.stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getStallId).distinct().map(Object::toString).collect(Collectors.joining(","));
//            drpOrderGoods.setStallId(stallIds);
//            this.drpOrderGoodsMapper.insert(drpOrderGoods);
//            Map<String, Integer> materialPrice = this.getMaterialPrice(drpOrderGoods.getSupplierId(), value.stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getMaterialId).toList());
//            BigDecimal orderAmount = BigDecimal.ZERO;
//            Iterator var9 = value.iterator();
//
//            while(var9.hasNext()) {
//               DrpPurDismantleGenerateInquiryMaterialDTO drpPurDismantleGenerateInquiryMaterialDTO = (DrpPurDismantleGenerateInquiryMaterialDTO)var9.next();
//               DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
//               BeanUtil.copyProperties(drpPurDismantleGenerateInquiryMaterialDTO, drpOrderGoodsDetail, new String[0]);
//               drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
//               drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
//               drpOrderGoodsDetail.setRemark(drpPurDismantleGenerateInquiryMaterialDTO.getRemark());
//               drpOrderGoodsDetail.setOrderNum(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum());
//               Long var10001 = drpPurDismantleGenerateInquiryMaterialDTO.getMaterialId();
//               Integer price = (Integer)materialPrice.get("" + var10001 + "-" + drpPurDismantleGenerateInquiryMaterialDTO.getUnitId());
//               drpOrderGoodsDetail.setSinglePrice(price != null ? price : 0);
//               drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurDismantleGenerateInquiryMaterialDTO.getOrderNum()).setScale(0, RoundingMode.HALF_UP).intValue());
//               orderAmount = orderAmount.add(new BigDecimal(drpOrderGoodsDetail.getTotalPrice()));
//               this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
//            }
//
//            if (CharSequenceUtil.isNotEmpty(content.getPlanIdList())) {
//               String[] split = content.getPlanIdList().split(",");
//               String[] var16 = split;
//               int var17 = split.length;
//
//               for(int var18 = 0; var18 < var17; ++var18) {
//                  String s = var16[var18];
//                  DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
//                  drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
//                  drpProductionPurchaseOrder.setPlanId(s);
//                  drpProductionPurchaseOrder.setOrderType(2);
//                  this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
//               }
//            }
//
//         });
//      }
//
//   }

   protected Long getWarehouseId(Long canteenId) {
      DrpWarehouse fc = (DrpWarehouse)this.drpWarehouseMapper.selectOne((Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getCanteenId, canteenId)).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      return fc != null ? fc.getWarehouseId() : null;
   }
//
//   public void disassemblyGenerateOrderManual(List<DisassemblyGenerateInquiryDTO> content) {
//      if (CollUtil.isNotEmpty(content)) {
//         content.forEach((dto) -> {
//            dto.setIfMergeSupplier(LeConstants.COMMON_NO);
//            DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
//            String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
//            drpOrderGoods.setOrderGoodsId(orderGoodsId);
//            drpOrderGoods.setSupplierId((Long)dto.getSupplierIds().get(0));
//            drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
//            drpOrderGoods.setRequestArrivalTime(dto.getRequestArrivalTime());
//            drpOrderGoods.setOrderTitle("采购计划单生成采购订单");
//            drpOrderGoods.setWarehouseId(this.getWarehouseId(dto.getCanteenId()));
//            drpOrderGoods.setCanteenId(dto.getCanteenId());
//            drpOrderGoods.setRemark(dto.getRemark());
//            String stallIds = (String)dto.getMaterialList().stream().map(DrpPurDismantleGenerateInquiryMaterialDTO::getStallId).distinct().map(Object::toString).collect(Collectors.joining(","));
//            drpOrderGoods.setStallId(stallIds);
//            drpOrderGoods.setSupplyAddress(this.getSupplyAddressByWarehouseId(drpOrderGoods.getWarehouseId()));
//            this.drpOrderGoodsMapper.insert(drpOrderGoods);
//            this.orderGoodDetail(dto, orderGoodsId);
//            if (CharSequenceUtil.isNotEmpty(dto.getPlanIdList())) {
//               String[] split = dto.getPlanIdList().split(",");
//               String[] var6 = split;
//               int var7 = split.length;
//
//               for(int var8 = 0; var8 < var7; ++var8) {
//                  String s = var6[var8];
//                  DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
//                  drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
//                  drpProductionPurchaseOrder.setPlanId(s);
//                  drpProductionPurchaseOrder.setOrderType(2);
//                  this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
//               }
//            }
//
//         });
//      }
//
//   }
//
//   public List<DrpSuggestedSuppliersVO> suggestedSuppliers(List<Long> content) {
//      return this.drpOrderGoodsMapper.suggestedSuppliers(content);
//   }
//
//   public List<DrpSuggestedSuppliersVO> suggestedSuppliersByCategory(List<String> content) {
//      return this.drpOrderGoodsMapper.suggestedSuppliersByCategory(content);
//   }

   @Transactional
   public String generateBatchPurPlan(DrpProductionGeneratePurPlanDTO content) {
      DrpProductionPlan drpProductionPlan = this.drpProductionPlanMapper.selectById(content.getProductionPlanId());
      List<DrpGeneratePurPlanDetailDTO> list = content.getDetailDTOList();
      DrpPurchasePlan drpPurchasePlan = new DrpPurchasePlan();
      drpPurchasePlan.setStatus(DrpMaterialTenderEnum.WAIT_COMMIT.key());
      drpPurchasePlan.setRemark("生产计划生成采购计划");
      drpPurchasePlan.setApproveStatus(DrpApproveStatusEnum.WAIT_APPROVE.key());
      drpPurchasePlan.setDelFlag(DelFlagEnum.DEL_FALSE.key());
      drpPurchasePlan.setProductionPlanId(content.getProductionPlanId());
      drpPurchasePlan.setCanteenId(drpProductionPlan.getCanteenId());
      drpPurchasePlan.setStallId("" + drpProductionPlan.getStallId());
      String planId = CustomIdGenerateUtil.generatePurchaseOrdId();
      drpPurchasePlan.setPlanId(planId);
      drpPurchasePlan.setPurDate(LocalDateTime.now());
      this.drpPurchasePlanMapper.insert(drpPurchasePlan);
      list.forEach((d) -> {
         DrpPurPlanDetail drpPurPlanDetail = new DrpPurPlanDetail();
         MenuMaterial menuMaterial = this.menuMaterialMapper.selectMenuMaterialMaterialId(d.getMaterialId());
         List<DrpWarehouseInventoryNum> inventoryNumList = this.getInventoryNum(drpProductionPlan.getCanteenId(), d.getMaterialId(), menuMaterial.getUnitId());
         if (CollUtil.isNotEmpty(inventoryNumList)) {
            drpPurPlanDetail.setInventoryNum((BigDecimal)inventoryNumList.stream().map(DrpWarehouseInventoryNum::getInventoryNum).reduce(BigDecimal.ZERO, BigDecimal::add));
         }

         BeanUtil.copyProperties(d, drpPurPlanDetail, new String[0]);
         drpPurPlanDetail.setDetailId(Id.next());
         drpPurPlanDetail.setPlanId(planId);
         drpPurPlanDetail.setUnitId(menuMaterial.getUnitId());
         DrpUnitModel unit = this.drpUnitMapper.getUnit(menuMaterial.getUnitId());
         if (unit != null && unit.getRate().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal purNumByg = d.getPurNum().multiply(new BigDecimal(1000));
            drpPurPlanDetail.setPurNum(purNumByg.divide(unit.getRate(), 3, RoundingMode.HALF_UP));
         } else {
            drpPurPlanDetail.setPurNum(BigDecimal.ZERO);
         }

         drpPurPlanDetail.setProPlanNum(drpPurPlanDetail.getPurNum());
         this.drpPurPlanDetailMapper.insert(drpPurPlanDetail);
      });
      DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
      drpProductionPurchaseOrder.setProductionPlanId(content.getProductionPlanId());
      drpProductionPurchaseOrder.setPlanId(planId);
      drpProductionPurchaseOrder.setOrderType(1);
      this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
      return planId;
   }

//   public String mergeGenerateOrderGoods(List<String> content) {
//      DrpPurchasePlanPageVO drpPurchasePlanPageVO = this.drpPurchasePlanMapper.selectByPlanId((String)content.get(0), DelFlagEnum.DEL_FALSE.key());
//      if (drpPurchasePlanPageVO == null) {
//         throw new LeException("采购计划不存在！");
//      } else {
//         List<DrpPurPlanDetailPageVO> mergeList = Lists.newArrayList();
//         content.forEach((planId) -> {
//            List<DrpPurPlanDetailPageVO> detailList = this.drpPurPlanDetailMapper.selectListByPlanId(planId);
//            mergeList.addAll(detailList);
//         });
//         if (CollUtil.isEmpty(mergeList)) {
//            return "";
//         } else {
//            DrpMenuMaterialSupplierQueryDTO dto = new DrpMenuMaterialSupplierQueryDTO();
//            dto.setMaterialCategoryIdList(mergeList.stream().map(DrpPurPlanDetailPageVO::getMaterialId).toList());
//            dto.setType(1);
//            List<DrpMenuMaterialSupplierCanteenVO> supplierByMaterialIdAndCanteenId = this.drpMenuMaterialSupplierMapper.getSupplierByMaterialIdAndCanteenId(dto);
//            Map<Long, Long> supplierMap = (Map)supplierByMaterialIdAndCanteenId.stream().collect(Collectors.toMap(DrpMenuMaterialSupplierCanteenVO::getMaterialCategoryId, DrpMenuMaterialSupplierCanteenVO::getSupplierId, (key1, key2) -> {
//               return key1;
//            }));
//            mergeList.forEach((d) -> {
//               d.setSupllerId((Long)supplierMap.get(d.getMaterialId()));
//            });
//            Map<Long, List<DrpPurPlanDetailPageVO>> supplierMapList = (Map)mergeList.stream().collect(Collectors.groupingBy((vo) -> {
//               return vo.getSupllerId() != null ? vo.getSupllerId() : -1L;
//            }));
//            List<String> orderGoodsIdList = Lists.newArrayList();
//            supplierMapList.forEach((supplierId, list) -> {
//               DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
//               String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
//               drpOrderGoods.setOrderGoodsId(orderGoodsId);
//               drpOrderGoods.setSupplierId(supplierId != null && supplierId != -1L ? supplierId : null);
//               drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
//               drpOrderGoods.setOrderTitle("采购计划单生成采购订单");
//               drpOrderGoods.setCanteenId(drpPurchasePlanPageVO.getCanteenId());
//               drpOrderGoods.setStallId(drpPurchasePlanPageVO.getStallId());
//               drpOrderGoods.setWarehouseId(this.getWarehouseId(drpPurchasePlanPageVO.getCanteenId()));
//               drpOrderGoods.setSupplyAddress(this.getSupplyAddressByWarehouseId(drpOrderGoods.getWarehouseId()));
//               drpOrderGoods.setRequestArrivalTime(drpPurchasePlanPageVO.getDeliverGoodsDate());
//               this.drpOrderGoodsMapper.insert(drpOrderGoods);
//               List<Long> materialIds = list.stream().map(DrpPurPlanDetailPageVO::getMaterialId).toList();
//               Map<String, Integer> materialPrice = this.getMaterialPrice(supplierId, materialIds);
//               BigDecimal orderAmount = BigDecimal.ZERO;
//               Iterator var10 = list.iterator();
//
//               while(var10.hasNext()) {
//                  DrpPurPlanDetailPageVO drpPurPlanDetailPageVO = (DrpPurPlanDetailPageVO)var10.next();
//                  DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
//                  BeanUtil.copyProperties(drpPurPlanDetailPageVO, drpOrderGoodsDetail, new String[0]);
//                  drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
//                  Long var10001 = drpPurPlanDetailPageVO.getMaterialId();
//                  Integer price = (Integer)materialPrice.get("" + var10001 + "-" + drpPurPlanDetailPageVO.getUnitId());
//                  drpOrderGoodsDetail.setSinglePrice(price != null ? price : 0);
//                  drpOrderGoodsDetail.setTotalPrice((new BigDecimal(drpOrderGoodsDetail.getSinglePrice())).multiply(drpPurPlanDetailPageVO.getPurNum()).setScale(0, RoundingMode.HALF_UP).intValue());
//                  drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
//                  drpOrderGoodsDetail.setOrderNum(drpPurPlanDetailPageVO.getPurNum());
//                  orderAmount = orderAmount.add(new BigDecimal(drpOrderGoodsDetail.getTotalPrice()));
//                  this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
//               }
//
//               orderGoodsIdList.add(orderGoodsId);
//            });
//            content.forEach((d) -> {
//               orderGoodsIdList.forEach((orderGoodsId) -> {
//                  DrpProductionPurchaseOrder drpProductionPurchaseOrder = new DrpProductionPurchaseOrder();
//                  drpProductionPurchaseOrder.setOrderGoodsId(orderGoodsId);
//                  drpProductionPurchaseOrder.setPlanId(d);
//                  drpProductionPurchaseOrder.setOrderType(2);
//                  this.drpProductionPurchaseOrderMapper.insert(drpProductionPurchaseOrder);
//               });
//            });
//            return null;
//         }
//      }
//   }

   public List<DrpPurPlanDetailPageVO> planMergeDetail(List<String> content) {
      List<DrpPurPlanDetailPageVO> list = Lists.newArrayList();
      content.forEach((planId) -> {
         List<DrpPurPlanDetailPageVO> detailList = this.drpPurPlanDetailMapper.selectListByPlanId(planId);
         detailList.forEach((d) -> {
            try {
               ObjectMapper mapper = new ObjectMapper();
               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
               HashSet<DrpSupplierSimpleVo> finalResult = new HashSet();
               if (ObjectUtil.isNotNull(d.getSupplierId()) && ObjectUtil.isNotNull(d.getSupplierName())) {
                  DrpSupplierSimpleVo supplier = new DrpSupplierSimpleVo();
                  supplier.setSupplierId(d.getSupplierId());
                  supplier.setSupplierName(d.getSupplierName());
                  finalResult.add(supplier);
               }

               if (CharSequenceUtil.isNotBlank(d.getAlternativeSupplier())) {
                  List<DrpSupplierSimpleVo> alternativeSupplierList = mapper.readValue(d.getAlternativeSupplier(), new TypeReference<List<DrpSupplierSimpleVo>>() {
                  });
                  finalResult.addAll(alternativeSupplierList);
               }

               if (ObjectUtil.isNotNull(d.getCertId())) {
                  finalResult = this.drpMenuMaterialSupplierService.dealSupplierCertificate(d.getCertId(), finalResult);
               }

               d.setDrpSupplierSimpleVos(finalResult);
               HashSet<DrpSupplierSimpleVo> caFinalResult = new HashSet();
               if (ObjectUtil.isNotNull(d.getCaSupplierId()) && ObjectUtil.isNotNull(d.getCaSupplierName())) {
                  DrpSupplierSimpleVo supplierx = new DrpSupplierSimpleVo();
                  supplierx.setSupplierId(d.getCaSupplierId());
                  supplierx.setSupplierName(d.getCaSupplierName());
                  caFinalResult.add(supplierx);
               }

               if (CharSequenceUtil.isNotBlank(d.getCaAlternativeSupplier())) {
                  List<DrpSupplierSimpleVo> alternativeSupplierListx = mapper.readValue(d.getCaAlternativeSupplier(), new TypeReference<List<DrpSupplierSimpleVo>>() {
                  });
                  caFinalResult.addAll(alternativeSupplierListx);
               }

               d.setCaDrpSuppliers(caFinalResult);
            } catch (Exception var6) {
               log.error("系统异常", var6);
            }

         });
         list.addAll(detailList);
      });
      return list;
   }

   private List<DrpWarehouseInventoryNum> getInventoryNum(Long canteenId, Long materialId, Long unitId) {
      DrpWarehouse fc = this.drpWarehouseMapper.selectOne((Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getCanteenId, canteenId)).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (fc == null) {
         return new ArrayList();
      } else {
         DrpWarehouse zc = this.drpWarehouseMapper.selectOne(((Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, fc.getParentId())).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).select(DrpWarehouse::getWarehouseId));
         List<Long> warehouseIds = Arrays.asList(fc.getWarehouseId(), zc != null ? zc.getWarehouseId() : null);
         return this.drpWarehouseMapper.getInventoryNumByWarehouseIds(warehouseIds, materialId, unitId);
      }
   }

   private void saveInquiryAndSendMsgToSupplier(List<Long> key, String inquiryId, List<DrpInquiryDetail> inquiryDetails) {
      Iterator var4 = key.iterator();

      while(var4.hasNext()) {
         Long supplierId = (Long)var4.next();
         DrpInquirySupplier drpInquirySupplier = new DrpInquirySupplier();
         drpInquirySupplier.setInquirySupplierId(Id.next());
         drpInquirySupplier.setInquiryId(inquiryId);
         drpInquirySupplier.setSupplierId(supplierId);
         this.drpPurchaseInquirySupplierMapper.insert(drpInquirySupplier);
         Long inquirySupplierId = drpInquirySupplier.getInquirySupplierId();
         Iterator var8 = inquiryDetails.iterator();

         while(var8.hasNext()) {
            DrpInquiryDetail detail = (DrpInquiryDetail)var8.next();
            DrpQuote quote = new DrpQuote();
            BeanUtil.copyProperties(detail, quote, new String[0]);
            quote.setQuoteId(Id.next());
            quote.setInquirySupplierId(inquirySupplierId);
            quote.setQuoteNum(detail.getPurNum());
            quote.setId(null);
            this.drpPurchaseQuoteMapper.insert(quote);
         }
      }

   }
//
//   protected void packageInquiry(String inquiryId, DisassemblyGenerateInquiryDTO dto) {
//      DrpInquiry drpInquiry = new DrpInquiry();
//      drpInquiry.setInquiryId(inquiryId);
//      drpInquiry.setTitle("采购询价");
//      drpInquiry.setAreaId(dto.getAreaId());
//      drpInquiry.setStartTime(dto.getStartTime());
//      drpInquiry.setEndTime(dto.getEndTime());
//      drpInquiry.setRequestArrivalTime(dto.getRequestArrivalTime());
//      drpInquiry.setSupplierId(0L);
//      drpInquiry.setStatus(DrpInquiryStatusEnum.NO_COMMIT.key());
//      this.drpInquiryMapper.insert(drpInquiry);
//   }
}
