package com.bonus.core.drp.api;

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 cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.constant.LeConstants;
import com.bonus.core.allocation.alloc.spare.AllocDrpModel;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.drp.constant.DrpIntoInventoryStatusEnum;
import com.bonus.core.drp.constant.DrpIntoInventoryTypeEnum;
import com.bonus.core.drp.constant.DrpOrderTypeEnum;
import com.bonus.core.drp.constant.DrpOutInventoryStatusEnum;
import com.bonus.core.drp.dto.*;
import com.bonus.core.drp.mapper.*;
import com.bonus.core.drp.model.*;
import com.bonus.core.drp.po.DrpOutDetailModel;
import com.bonus.core.drp.po.DrpUnitModel;
import com.bonus.core.drp.po.IntoDetailMaterialModel;
import com.bonus.core.drp.po.SupermarketInventoryRepairDetailModel;
import com.bonus.core.drp.service.DrpDetailFlowService;
import com.bonus.core.drp.service.impl.DrpMetadataService;
import com.bonus.core.drp.utils.CustomIdGenerateUtil;
import com.bonus.core.drp.utils.InventoryUtil;
import com.bonus.core.menu.constant.MenuSalesTypeEnum;
import com.bonus.core.supermarket.api.SupermarketApi;
import com.bonus.core.supermarket.constant.SupermarketConstants;
import com.bonus.core.supermarket.dto.MinusSupermarketInventoryDetailDTO;
import com.bonus.core.supermarket.dto.PlusSupermarketInventoryDetailDTO;
import com.bonus.core.supermarket.mapper.SupermarketProductMapper;
import com.bonus.core.supermarket.po.InventoryNumModel;
import com.bonus.core.supermarket.po.WarehouseModel;
import com.bonus.i18n.I18n;
import com.bonus.constant.DelFlagEnum;
import com.bonus.utils.id.Id;
import com.google.common.collect.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.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class DrpServiceApi {
   private static final Logger log = LoggerFactory.getLogger(DrpServiceApi.class);
   @Autowired
   @Lazy
   private DrpInventoryMapper drpInventoryMapper;
   @Autowired
   @Lazy
   private DrpWarehouseMapper drpWarehouseMapper;
   @Autowired
   @Lazy
   private DrpIntoDetailMapper drpIntoDetailMapper;
   @Autowired
   @Lazy
   private DrpOutInventoryMapper drpOutInventoryMapper;
   @Autowired
   @Lazy
   private DrpOutDetailMapper drpOutDetailMapper;
   @Autowired
   @Lazy
   private DrpIntoInventoryMapper drpIntoInventoryMapper;
   @Autowired
   @Lazy
   private DrpUnitMapper drpUnitMapper;
   @Autowired
   @Lazy
   private SupermarketProductMapper supermarketProductMapper;
//   @Autowired
//   @Lazy
//   private AllocMetadataApi allocMetadataApi;
   @Autowired
   @Lazy
   private DrpOverdraftInventoryMapper drpOverdraftInventoryMapper;
   @Autowired
   @Lazy
   private DrpDetailFlowService drpDetailFlowService;
   @Autowired
   @Lazy
   private DrpDetailFlowMapper drpDetailFlowMapper;
   @Autowired
   @Lazy
   private SupermarketApi supermarketApi;
   @Autowired
   @Lazy
   private RedisTemplate<Object, Object> redisTemplate;
//   @Autowired
//   @Lazy
//   private MenuMaterialMapper menuMaterialMapper;
//   @Autowired
//   @Lazy
//   private DrpProductionPlanDetailMapper drpProductionPlanDetailMapper;
//   @Autowired
//   @Lazy
//   private DrpOtherService drpOtherService;
   @Autowired
   @Lazy
   private DrpMetadataService drpMetadataService;

//   public InventoryModel getInventoryNumByMaterialId(Long materialId, Long warehouseId, Long unitId) {
//      return this.drpInventoryMapper.getInventoryNumByMaterialId(materialId, warehouseId, unitId);
//   }

   public String getWarehouseNameById(Long warehouseId) {
      return this.drpWarehouseMapper.selectNameById(warehouseId);
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public String outInventoryBySupermarketPay(AndroidSupermarketOutDTO content) {
      log.info("[商超-库存扣减]_入参：{}", JSONUtil.toJsonStr(content));
      boolean ifRelateDrp = this.supermarketApi.ifRelateDrp(content.getSupermarketId());
      Long areaId = this.supermarketApi.getAreaIdBySupermarketId(content.getSupermarketId());
      AllocDrpModel allocDrpModel = this.drpMetadataService.queryMetadata(areaId);
      String ifAllowOverdraftInventoryStr = allocDrpModel.getIfAllowOverdraftInventory();
      boolean ifAllowOverdraftInventory = false;
      if (CharSequenceUtil.isNotEmpty(ifAllowOverdraftInventoryStr) && ObjectUtil.equal(ifAllowOverdraftInventoryStr, "1")) {
         ifAllowOverdraftInventory = true;
      }

      log.info("[商超-库存扣减]_是否关联出入库：{}", ifRelateDrp);
      if (!ifRelateDrp) {
         this.noRelateDrpMinusInventory(content, ifAllowOverdraftInventory);
         return String.valueOf(content.getOrdId());
      } else {
         return this.relateDrpMinusInventory(content, ifAllowOverdraftInventory);
      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public String relateDrpMinusInventory(AndroidSupermarketOutDTO content, boolean ifAllowOverdraftInventory) {
      WarehouseModel warehouseModel = this.supermarketApi.getWarehouseBySupermarketId(content.getSupermarketId());
      DrpOutInventory outInventory = new DrpOutInventory();
      BeanUtil.copyProperties(content, outInventory, new String[0]);
      String outId = CustomIdGenerateUtil.generateOutId();
      outInventory.setOutId(outId).setTotalAmount(0L).setFetchUserId(-1L);
      outInventory.setStatus(DrpOutInventoryStatusEnum.COMMIT.key());
      outInventory.setWarehouseId(warehouseModel.getWarehouseId());
      BigDecimal totalNum = BigDecimal.ZERO;
      List<AndroidSupermarketOutDetailDTO> detailList = content.getDetailList();
      List<SupermarketInventoryRepairDetailModel> supermarketInventoryRepairDetailModelList = new ArrayList();
      Iterator var9 = detailList.iterator();

      while(var9.hasNext()) {
         AndroidSupermarketOutDetailDTO temp = (AndroidSupermarketOutDetailDTO)var9.next();
         if (ObjectUtil.isEmpty(temp.getInventoryId())) {
            throw new ServiceException(I18n.getMessage("drp.material-inventory-is-not-exist", new Object[0]));
         }

         if (ObjectUtil.isEmpty(warehouseModel.getWarehouseId())) {
            throw new ServiceException(I18n.getMessage("drp.warehouse-id-is-null", new Object[0]));
         }

         Long checkInventory = this.drpInventoryMapper.selectCount(Wrappers.lambdaQuery(DrpInventory.class)
                 .eq(DrpInventory::getWarehouseId, warehouseModel.getWarehouseId())
                 .eq(DrpInventory::getInventoryId, temp.getInventoryId()));
         if (checkInventory <= 0L) {
            log.info("库存:" + temp.getInventoryId() + "-不属于该仓库!");
            throw new ServiceException(I18n.getMessage("drp.product-not-into-inventory", new Object[0]));
         }

         DrpInventory drpInventory = (DrpInventory)this.drpInventoryMapper.selectOne(Wrappers
                 .lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, temp.getInventoryId())
                 .eq(DrpInventory::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .select(DrpInventory::getInventoryId, DrpInventory::getMaterialNum,
                         DrpInventory::getMinNum, DrpInventory::getMaxNum, DrpInventory::getMaterialId, DrpInventory::getUnitId));
         if (drpInventory == null) {
            log.info("***[商超_出库]_不存在该库存**************");
            throw new ServiceException(I18n.getMessage("drp.no-inventory", new Object[0]));
         }

         if (temp.getSalesMode().equals(MenuSalesTypeEnum.WEIGHT_ON.key())) {
            DrpUnitModel rateUnit = this.drpUnitMapper.getUnit(drpInventory.getUnitId());
            BigDecimal converFetchNum = temp.getFetchNum().divide(rateUnit.getRate(), 3, RoundingMode.HALF_UP);
            temp.setFetchNum(converFetchNum);
         }

         BigDecimal fetchNum = temp.getFetchNum();
         totalNum = totalNum.add(fetchNum);
         String materialName = this.drpInventoryMapper.selectMaterialName(drpInventory.getMaterialId());
         if (!ifAllowOverdraftInventory) {
            if (drpInventory.getMaterialNum().compareTo(fetchNum) < 0) {
               log.info("***[商超_出库]_原料库存不足**************");
               throw new ServiceException(I18n.getMessage("drp.material-lack-of-inventory", new Object[]{materialName}));
            }

            BigDecimal materialNum = drpInventory.getMaterialNum().subtract(fetchNum);
            Integer inventoryStatus = InventoryUtil.getInventoryStatusByNum(drpInventory.getMaxNum(), drpInventory.getMinNum(), materialNum);
            this.drpInventoryMapper.update(null, Wrappers.lambdaUpdate(DrpInventory.class)
                    .eq(DrpInventory::getInventoryId, temp.getInventoryId()).set(DrpInventory::getInventoryStatus, inventoryStatus));
            this.drpInventoryMapper.minusInventoryNumById(temp.getInventoryId(), fetchNum);
            List<DrpIntoDetail> drpIntoDetailList = this.drpIntoDetailMapper.listIntoDetailByInventoryId(temp.getInventoryId(),
                    DrpIntoInventoryStatusEnum.COMMIT.key(), 0);
            if (CollUtil.isEmpty(drpIntoDetailList)) {
               log.info("该原料当前无库存!");
               throw new ServiceException(I18n.getMessage("drp.material-no-inventory", new Object[]{materialName}));
            }

            BigDecimal currentInventorySum = (BigDecimal)drpIntoDetailList.stream().map(DrpIntoDetail::getInventoryNum).reduce(BigDecimal.ZERO, BigDecimal::add);
            if (fetchNum.compareTo(currentInventorySum) > 0) {
               log.info("存在原料库存不足!");
               throw new ServiceException(I18n.getMessage("drp.material-no-inventory", new Object[]{materialName}));
            }

            BigDecimal nowLeftNum = fetchNum;
            Iterator var38 = drpIntoDetailList.iterator();

            while(var38.hasNext()) {
               DrpIntoDetail drpIntoDetail = (DrpIntoDetail)var38.next();
               DrpOutDetail outDetail = new DrpOutDetail();
               BeanUtil.copyProperties(temp, outDetail, new String[0]);
               outDetail.setOutId(outId);
               outDetail.setDetailId(Id.next());
               outDetail.setIntoDetailId(drpIntoDetail.getDetailId());
               outDetail.setInventoryId(temp.getInventoryId());
               SupermarketInventoryRepairDetailModel supermarketInventoryRepairDetailModel = new SupermarketInventoryRepairDetailModel();
               supermarketInventoryRepairDetailModel.setInventoryId(temp.getInventoryId());
               supermarketInventoryRepairDetailModel.setIntoDetailId(drpIntoDetail.getDetailId());
               BigDecimal checkInventoryNum = drpIntoDetail.getInventoryNum().subtract(nowLeftNum);
               DrpOutDetailModel drpOutDetailModel;
               if (checkInventoryNum.compareTo(BigDecimal.ZERO) >= 0) {
                  outDetail.setFetchNum(nowLeftNum);
                  outDetail.setTotalPrice((new BigDecimal(drpIntoDetail.getUnitPrice())).multiply(outDetail.getFetchNum()).setScale(0, RoundingMode.HALF_UP).intValue());
                  this.drpOutDetailMapper.insert(outDetail);
                  this.drpIntoDetailMapper.minusInventoryNumByDetailId(drpIntoDetail.getDetailId(), nowLeftNum);
                  outInventory.setTotalAmount(outInventory.getTotalAmount() + (long)outDetail.getTotalPrice());
                  supermarketInventoryRepairDetailModel.setOperateNum(nowLeftNum);
                  supermarketInventoryRepairDetailModelList.add(supermarketInventoryRepairDetailModel);
                  drpOutDetailModel = new DrpOutDetailModel();
                  BeanUtil.copyProperties(outDetail, drpOutDetailModel, new String[0]);
                  drpOutDetailModel.setMaterialId(drpIntoDetail.getMaterialId());
                  drpOutDetailModel.setUnitId(drpIntoDetail.getUnitId());
                  drpOutDetailModel.setUnitPrice(drpIntoDetail.getUnitPrice());
                  drpOutDetailModel.setSupplierId(drpIntoDetail.getSupplierId());
                  this.drpDetailFlowService.saveDetailFlow((DrpIntoInventory)null, (DrpIntoDetail)null,
                          outInventory, drpOutDetailModel, (DrpDetailFlow)null, DrpOrderTypeEnum.OUT.key());
                  break;
               }

               outDetail.setFetchNum(drpIntoDetail.getInventoryNum());
               outDetail.setTotalPrice((new BigDecimal(drpIntoDetail.getUnitPrice())).multiply(outDetail.getFetchNum()).setScale(0, RoundingMode.HALF_UP).intValue());
               this.drpOutDetailMapper.insert(outDetail);
               this.drpIntoDetailMapper.minusInventoryNumByDetailId(drpIntoDetail.getDetailId(), drpIntoDetail.getInventoryNum());
               nowLeftNum = nowLeftNum.subtract(drpIntoDetail.getInventoryNum());
               supermarketInventoryRepairDetailModel.setOperateNum(drpIntoDetail.getInventoryNum());
               supermarketInventoryRepairDetailModelList.add(supermarketInventoryRepairDetailModel);
               outInventory.setTotalAmount(outInventory.getTotalAmount() + (long)outDetail.getTotalPrice());
               drpOutDetailModel = new DrpOutDetailModel();
               BeanUtil.copyProperties(outDetail, drpOutDetailModel, new String[0]);
               drpOutDetailModel.setMaterialId(drpIntoDetail.getMaterialId());
               drpOutDetailModel.setUnitId(drpIntoDetail.getUnitId());
               drpOutDetailModel.setUnitPrice(drpIntoDetail.getUnitPrice());
               drpOutDetailModel.setSupplierId(drpIntoDetail.getSupplierId());
               this.drpDetailFlowService.saveDetailFlow(null, null, outInventory, drpOutDetailModel, (DrpDetailFlow)null, DrpOrderTypeEnum.OUT.key());
            }
         } else {
            List<DrpIntoDetail> drpIntoDetailList = this.drpIntoDetailMapper.listIntoDetailByInventoryId(temp.getInventoryId(), DrpIntoInventoryStatusEnum.COMMIT.key(), 0);
            BigDecimal nowLeftNum = fetchNum;
            Iterator var17 = drpIntoDetailList.iterator();

            while(var17.hasNext()) {
               DrpIntoDetail drpIntoDetail = (DrpIntoDetail)var17.next();
               DrpOutDetail outDetail = new DrpOutDetail();
               BeanUtil.copyProperties(temp, outDetail, new String[0]);
               outDetail.setOutId(outId);
               outDetail.setDetailId(Id.next());
               outDetail.setIntoDetailId(drpIntoDetail.getDetailId());
               outDetail.setInventoryId(temp.getInventoryId());
               SupermarketInventoryRepairDetailModel supermarketInventoryRepairDetailModel = new SupermarketInventoryRepairDetailModel();
               supermarketInventoryRepairDetailModel.setInventoryId(temp.getInventoryId());
               supermarketInventoryRepairDetailModel.setIntoDetailId(drpIntoDetail.getDetailId());
               BigDecimal checkInventoryNum = drpIntoDetail.getInventoryNum().subtract(nowLeftNum);
               DrpOutDetailModel drpOutDetailModel;
               if (checkInventoryNum.compareTo(BigDecimal.ZERO) >= 0) {
                  outDetail.setFetchNum(nowLeftNum);
                  outDetail.setTotalPrice((new BigDecimal(drpIntoDetail.getUnitPrice())).multiply(outDetail.getFetchNum()).setScale(0, RoundingMode.HALF_UP).intValue());
                  this.drpOutDetailMapper.insert(outDetail);
                  this.drpIntoDetailMapper.minusInventoryNumByDetailId(drpIntoDetail.getDetailId(), nowLeftNum);
                  outInventory.setTotalAmount(outInventory.getTotalAmount() + (long)outDetail.getTotalPrice());
                  supermarketInventoryRepairDetailModel.setOperateNum(nowLeftNum);
                  supermarketInventoryRepairDetailModelList.add(supermarketInventoryRepairDetailModel);
                  nowLeftNum = BigDecimal.ZERO;
                  drpOutDetailModel = new DrpOutDetailModel();
                  BeanUtil.copyProperties(outDetail, drpOutDetailModel, new String[0]);
                  drpOutDetailModel.setMaterialId(drpIntoDetail.getMaterialId());
                  drpOutDetailModel.setUnitId(drpIntoDetail.getUnitId());
                  drpOutDetailModel.setUnitPrice(drpIntoDetail.getUnitPrice());
                  drpOutDetailModel.setSupplierId(drpIntoDetail.getSupplierId());
                  this.drpDetailFlowService.saveDetailFlow((DrpIntoInventory)null, (DrpIntoDetail)null, outInventory, drpOutDetailModel, (DrpDetailFlow)null, DrpOrderTypeEnum.OUT.key());
                  break;
               }

               outDetail.setFetchNum(drpIntoDetail.getInventoryNum());
               outDetail.setTotalPrice((new BigDecimal(drpIntoDetail.getUnitPrice())).multiply(outDetail.getFetchNum()).setScale(0, RoundingMode.HALF_UP).intValue());
               this.drpOutDetailMapper.insert(outDetail);
               this.drpIntoDetailMapper.minusInventoryNumByDetailId(drpIntoDetail.getDetailId(), drpIntoDetail.getInventoryNum());
               nowLeftNum = nowLeftNum.subtract(drpIntoDetail.getInventoryNum());
               supermarketInventoryRepairDetailModel.setOperateNum(drpIntoDetail.getInventoryNum());
               supermarketInventoryRepairDetailModelList.add(supermarketInventoryRepairDetailModel);
               outInventory.setTotalAmount(outInventory.getTotalAmount() + (long)outDetail.getTotalPrice());
               drpOutDetailModel = new DrpOutDetailModel();
               BeanUtil.copyProperties(outDetail, drpOutDetailModel, new String[0]);
               drpOutDetailModel.setMaterialId(drpIntoDetail.getMaterialId());
               drpOutDetailModel.setUnitId(drpIntoDetail.getUnitId());
               drpOutDetailModel.setUnitPrice(drpIntoDetail.getUnitPrice());
               drpOutDetailModel.setSupplierId(drpIntoDetail.getSupplierId());
               this.drpDetailFlowService.saveDetailFlow((DrpIntoInventory)null, (DrpIntoDetail)null, outInventory, drpOutDetailModel, (DrpDetailFlow)null, DrpOrderTypeEnum.OUT.key());
            }

            BigDecimal materialNum = drpInventory.getMaterialNum().subtract(fetchNum);
            Integer inventoryStatus = InventoryUtil.getInventoryStatusByNum(drpInventory.getMaxNum(), drpInventory.getMinNum(), materialNum);
            this.drpInventoryMapper.update(null, Wrappers
                    .lambdaUpdate(DrpInventory.class).eq(DrpInventory::getInventoryId, drpInventory.getInventoryId())
                    .set(DrpInventory::getInventoryStatus, inventoryStatus));
            this.drpInventoryMapper.minusInventoryNumById(drpInventory.getInventoryId(), fetchNum);
            if (nowLeftNum.compareTo(BigDecimal.ZERO) > 0) {
               DrpOverdraftInventory drpOverdraftInventory = this.drpOverdraftInventoryMapper
                       .selectOne(Wrappers.lambdaQuery(DrpOverdraftInventory.class)
                               .eq(DrpOverdraftInventory::getWarehouseInventoryId, drpInventory.getInventoryId()));
               if (ObjectUtil.isNotEmpty(drpOverdraftInventory)) {
                  this.drpOverdraftInventoryMapper.minusInventoryNum(drpOverdraftInventory.getWarehouseInventoryId(), nowLeftNum);
               } else {
                  drpOverdraftInventory = new DrpOverdraftInventory();
                  drpOverdraftInventory.setOverdraftInventoryId(Id.next());
                  drpOverdraftInventory.setWarehouseId(warehouseModel.getWarehouseId());
                  drpOverdraftInventory.setMaterialId(drpInventory.getMaterialId());
                  drpOverdraftInventory.setUnitId(drpInventory.getUnitId());
                  drpOverdraftInventory.setWarehouseInventoryId(drpInventory.getInventoryId());
                  drpOverdraftInventory.setInventoryNum(BigDecimal.ZERO.subtract(nowLeftNum));
                  this.drpOverdraftInventoryMapper.insert(drpOverdraftInventory);
               }

               DrpOutDetail outDetail = new DrpOutDetail();
               BeanUtil.copyProperties(temp, outDetail, new String[0]);
               outDetail.setOutId(outId);
               outDetail.setDetailId(Id.next());
               outDetail.setInventoryId(drpInventory.getInventoryId());
               outDetail.setFetchNum(nowLeftNum);
               outDetail.setTotalPrice(0);
               outDetail.setIfOverdraft(LeConstants.COMMON_YES);
               this.drpOutDetailMapper.insert(outDetail);
               DrpDetailFlow drpDetailFlow = new DrpDetailFlow();
               BeanUtil.copyProperties(temp, drpDetailFlow, new String[0]);
               drpDetailFlow.setInventoryId(temp.getInventoryId());
               drpDetailFlow.setDrpFlowId(Id.next());
               drpDetailFlow.setDrpDetailId(outDetail.getDetailId());
               drpDetailFlow.setDrpOrderId(outInventory.getOutId());
               drpDetailFlow.setNum(nowLeftNum);
               drpDetailFlow.setWarehouseId(outInventory.getWarehouseId());
               drpDetailFlow.setDrpOrderTime(outInventory.getOutDate());
               drpDetailFlow.setRecordTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS").format(LocalDateTime.now()));
               drpDetailFlow.setDrpOrderType(DrpOrderTypeEnum.OVERDRAFT.key());
               DrpInventory materialInfo = (DrpInventory)this.drpInventoryMapper
                       .selectOne(Wrappers.lambdaQuery(DrpInventory.class)
                               .eq(DrpInventory::getInventoryId, drpDetailFlow.getInventoryId())
                               .select(DrpInventory::getMaterialId, DrpInventory::getUnitId));
               drpDetailFlow.setMaterialId(materialInfo.getMaterialId());
               drpDetailFlow.setUnitId(materialInfo.getUnitId());
               this.drpDetailFlowService.saveDetailFlow((DrpIntoInventory)null, (DrpIntoDetail)null,
                       (DrpOutInventory)null, (DrpOutDetailModel)null, drpDetailFlow, DrpOrderTypeEnum.OVERDRAFT.key());
            }
         }
      }

      outInventory.setTotalNum(totalNum);
      this.drpOutInventoryMapper.insert(outInventory);
      String supermarketRepairDetailStr = "yst:merchant-id:supermarket:repair:outId"
              .replace("merchant-id", TenantContextHolder.getTenantId().toString()).replace("outId", outId);
      this.redisTemplate.opsForValue().set(supermarketRepairDetailStr, supermarketInventoryRepairDetailModelList, 60L, TimeUnit.MINUTES);
      return outInventory.getOutId();
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void supermarketInventoryRepair(SupermarketInventoryRepairDTO content, Long supermarketId) {
      log.info("[商超-库存补救]_入参：{}", JSONUtil.toJsonStr(content));
      log.info("[商超-库存补救]_超市id：{}", supermarketId);
      boolean ifRelateDrp = this.supermarketApi.ifRelateDrp(supermarketId);
      String outId;
      Iterator var7;
      SupermarketInventoryRepairDetailModel supermarketInventoryRepairDetailModel;
      if (!ifRelateDrp) {
         outId = "yst:merchant-id:supermarket:repair:outId".replace("merchant-id", TenantContextHolder.getTenantId().toString()).replace("outId", String.valueOf(content.getOrdId()));
         log.info("从redis中获取出库单：{}，信息:{}", content.getOrdId(), outId);
         List<SupermarketInventoryRepairDetailModel> supermarketInventoryRepairDetailList = (List)this.redisTemplate.opsForValue().get(outId);
         List<PlusSupermarketInventoryDetailDTO> detailList = new ArrayList();
         if (supermarketInventoryRepairDetailList != null) {
            var7 = supermarketInventoryRepairDetailList.iterator();

            while(var7.hasNext()) {
               supermarketInventoryRepairDetailModel = (SupermarketInventoryRepairDetailModel)var7.next();
               PlusSupermarketInventoryDetailDTO detail = new PlusSupermarketInventoryDetailDTO();
               detail.setDetailId(supermarketInventoryRepairDetailModel.getSupermarketDetailId());
               detail.setOperateNum(supermarketInventoryRepairDetailModel.getOperateNum());
               detailList.add(detail);
            }
         }

         this.supermarketApi.plusSupermarketInventory(detailList);
      } else {
         outId = content.getOutId();
         if (CharSequenceUtil.isBlank(outId)) {
            throw new ServiceException(I18n.getMessage("drp.supermarket-repair-out-id-not-null"));
         } else {
            String supermarketRepairDetailStr = "yst:merchant-id:supermarket:repair:outId".replace("merchant-id", TenantContextHolder.getTenantId().toString()).replace("outId", outId);
            log.info("从redis中获取出库单：{}，信息:{}", content.getOrdId(), supermarketRepairDetailStr);
            List<SupermarketInventoryRepairDetailModel> supermarketInventoryRepairDetailModelList = (List)this.redisTemplate.opsForValue().get(supermarketRepairDetailStr);
            if (CollUtil.isEmpty(supermarketInventoryRepairDetailModelList)) {
               log.info("[商超-库存补救]_redis中获取出库信息不存在!");
               throw new ServiceException(I18n.getMessage("drp.supermarket-repair-out-info-not-exist", new Object[0]));
            } else {
               var7 = supermarketInventoryRepairDetailModelList.iterator();

               while(var7.hasNext()) {
                  supermarketInventoryRepairDetailModel = (SupermarketInventoryRepairDetailModel)var7.next();
                  DrpInventory drpInventory = this.drpInventoryMapper.selectOne(Wrappers
                          .lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, supermarketInventoryRepairDetailModel
                                  .getInventoryId()).select(DrpInventory::getMaterialNum, DrpInventory::getMinNum,
                          DrpInventory::getMaxNum));
                  BigDecimal inventoryNum = drpInventory.getMaterialNum().add(supermarketInventoryRepairDetailModel.getOperateNum());
                  Integer inventoryStatus = InventoryUtil.getInventoryStatusByNum(drpInventory.getMaxNum(), drpInventory.getMinNum(), inventoryNum);
                  this.drpInventoryMapper.update(null, Wrappers
                          .lambdaUpdate(DrpInventory.class).eq(DrpInventory::getInventoryId, supermarketInventoryRepairDetailModel
                                  .getInventoryId()).set(DrpInventory::getInventoryStatus, inventoryStatus));
                  this.drpInventoryMapper.plusInventoryNumById(supermarketInventoryRepairDetailModel.getInventoryId(), supermarketInventoryRepairDetailModel.getOperateNum());
                  this.drpIntoDetailMapper.plusInventoryNumByDetailId(supermarketInventoryRepairDetailModel.getIntoDetailId(), supermarketInventoryRepairDetailModel.getOperateNum());
               }

               this.drpDetailFlowMapper.delete(Wrappers.lambdaUpdate(DrpDetailFlow.class).eq(DrpDetailFlow::getDrpOrderId, outId));
               this.drpOutDetailMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpOutDetail.class).eq(DrpOutDetail::getOutId, outId));
               this.drpOutInventoryMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpOutInventory.class).eq(DrpOutInventory::getOutId, outId));
            }
         }
      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void refundSuccPlusInventory(RefundRepairInventoryDTO refundRepairInventoryDTO) {
      log.info("[商超_退款补库存]_入参：{}", refundRepairInventoryDTO);
      boolean ifRelateDrp = this.supermarketApi.ifRelateDrp(refundRepairInventoryDTO.getSupermarketId());
      if (!ifRelateDrp) {
         List<PlusSupermarketInventoryDetailDTO> detailList = new ArrayList();
         Iterator var4 = refundRepairInventoryDTO.getDetailList().iterator();

         while(var4.hasNext()) {
            RefundRepairInventoryDetailDTO refundRepairInventoryDetail = (RefundRepairInventoryDetailDTO)var4.next();
            PlusSupermarketInventoryDetailDTO detail = new PlusSupermarketInventoryDetailDTO();
            detail.setDetailId(refundRepairInventoryDetail.getSupermarketDetailId());
            if (refundRepairInventoryDetail.getSalesMode().equals(MenuSalesTypeEnum.WEIGHT_ON.key())) {
               refundRepairInventoryDetail.setRefundNum(refundRepairInventoryDetail.getRefundNum()
                       .divide(new BigDecimal(SupermarketConstants.WEIGHT_UNIT_PRICE), 3, RoundingMode.HALF_UP));
            }

            detail.setOperateNum(refundRepairInventoryDetail.getRefundNum());
            detailList.add(detail);
         }

         this.supermarketApi.plusSupermarketInventory(detailList);
      } else {
         this.refundGenerateInto(refundRepairInventoryDTO);
      }
   }

   protected void refundGenerateInto(RefundRepairInventoryDTO refundRepairInventoryDTO) {
      if (CharSequenceUtil.isBlank(refundRepairInventoryDTO.getOrderId())) {
         throw new ServiceException(I18n.getMessage("drp.supermarket-repair-order-id-not-null", new Object[0]));
      } else {
         DrpOutInventory drpOutInventory = this.drpOutInventoryMapper.selectOne(Wrappers.lambdaQuery(DrpOutInventory.class)
                 .eq(DrpOutInventory::getOrdId, refundRepairInventoryDTO.getOrderId()).select(DrpOutInventory::getOutId));
         if (ObjectUtil.isEmpty(drpOutInventory)) {
            throw new ServiceException(I18n.getMessage("drp.out-not-bind-order", new Object[0]));
         } else {
            String outId = drpOutInventory.getOutId();
            DrpIntoInventory drpIntoInventory = new DrpIntoInventory();
            WarehouseModel warehouseModel = this.drpOutInventoryMapper.getWarehouseByOutId(outId);
            String intoId = CustomIdGenerateUtil.generateIntoId();
            drpIntoInventory.setIntoId(intoId);
            drpIntoInventory.setIntoDate(LocalDateTime.now());
            drpIntoInventory.setIntoType(DrpIntoInventoryTypeEnum.SUPERMARKET_REFUND.key());
            drpIntoInventory.setRefundOutId(outId);
            drpIntoInventory.setWarehouseId(warehouseModel.getWarehouseId());
            drpIntoInventory.setStatus(DrpIntoInventoryStatusEnum.COMMIT.key());
            List<RefundRepairInventoryDetailDTO> detailList = refundRepairInventoryDTO.getDetailList();
            BigDecimal totalNum = new BigDecimal(BigInteger.ZERO);
            Long areaId = this.supermarketApi.getAreaIdBySupermarketId(refundRepairInventoryDTO.getSupermarketId());
            AllocDrpModel allocDrpModel = this.drpMetadataService.queryMetadata(areaId);
            String ifAllowOverdraftInventoryStr = allocDrpModel.getIfAllowOverdraftInventory();
            long totalAmount = 0L;
            Iterator var14 = detailList.iterator();

            while(true) {
               while(var14.hasNext()) {
                  RefundRepairInventoryDetailDTO detail = (RefundRepairInventoryDetailDTO)var14.next();
                  Long inventoryId = detail.getInventoryId();
                  List<IntoDetailMaterialModel> intoDetailMaterialList = this.drpOutDetailMapper.listIntoDetailMaterial(outId, inventoryId);
                  Long unitId = ((IntoDetailMaterialModel)intoDetailMaterialList.get(0)).getUnitId();
                  if (detail.getSalesMode().equals(MenuSalesTypeEnum.WEIGHT_ON.key())) {
                     DrpUnitModel converUnit = this.drpUnitMapper.getUnit(unitId);
                     detail.setRefundNum(detail.getRefundNum().divide(converUnit.getRate(), 3, RoundingMode.HALF_UP));
                  }

                  BigDecimal totalRefundNum = detail.getRefundNum();
                  if (CharSequenceUtil.isNotEmpty(ifAllowOverdraftInventoryStr) && ObjectUtil.equal(ifAllowOverdraftInventoryStr, "1")) {
                     DrpOutDetail overdraftOutDetail = this.drpOutDetailMapper.selectOne(Wrappers.lambdaQuery(DrpOutDetail.class).eq(DrpOutDetail::getOutId, outId).eq(DrpOutDetail::getIfOverdraft, LeConstants.COMMON_YES));
                     if (ObjectUtil.isNotEmpty(overdraftOutDetail)) {
                        BigDecimal canRefundNum = overdraftOutDetail.getFetchNum();
                        if (totalRefundNum.compareTo(canRefundNum) < 0) {
                           BigDecimal subtract = canRefundNum.subtract(totalRefundNum);
                           this.drpOutDetailMapper.update(null, Wrappers.lambdaUpdate(DrpOutDetail.class).eq(DrpOutDetail::getDetailId, overdraftOutDetail.getDetailId()).set(DrpOutDetail::getFetchNum, subtract));
                           this.plusInventoryNum(inventoryId, totalRefundNum);
                           this.drpOverdraftInventoryMapper.plusInventoryNum(inventoryId, totalRefundNum);
                           this.drpDetailFlowMapper.update(null, Wrappers.lambdaUpdate(DrpDetailFlow.class).eq(DrpDetailFlow::getDrpDetailId, overdraftOutDetail.getDetailId()).set(DrpDetailFlow::getNum, subtract));
                           continue;
                        }

                        this.drpOutDetailMapper.delete(Wrappers.lambdaQuery(DrpOutDetail.class).eq(DrpOutDetail::getDetailId, overdraftOutDetail.getDetailId()));
                        totalRefundNum = totalRefundNum.subtract(canRefundNum);
                        this.plusInventoryNum(inventoryId, canRefundNum);
                        this.drpOverdraftInventoryMapper.plusInventoryNum(inventoryId, canRefundNum);
                        this.drpDetailFlowMapper.update(null, Wrappers.lambdaUpdate(DrpDetailFlow.class).eq(DrpDetailFlow::getDrpDetailId, overdraftOutDetail.getDetailId()).set(DrpDetailFlow::getNum, BigDecimal.ZERO));
                     }
                  }

                  totalNum = totalNum.add(totalRefundNum);
                  Iterator var28 = intoDetailMaterialList.iterator();

                  while(var28.hasNext()) {
                     IntoDetailMaterialModel intoDetailMaterialModel = (IntoDetailMaterialModel)var28.next();
                     DrpIntoDetail drpIntoDetail = new DrpIntoDetail();
                     drpIntoDetail.setDetailId(Id.next());
                     drpIntoDetail.setIntoId(intoId);
                     drpIntoDetail.setInventoryId(inventoryId);
                     drpIntoDetail.setMaterialId(intoDetailMaterialModel.getMaterialId());
                     drpIntoDetail.setSupplierId(intoDetailMaterialModel.getSupplierId());
                     drpIntoDetail.setUnitId(intoDetailMaterialModel.getUnitId());
                     drpIntoDetail.setUnitPrice(intoDetailMaterialModel.getUnitPrice());
                     drpIntoDetail.setSize(intoDetailMaterialModel.getSize());
                     BigDecimal fetchNum = intoDetailMaterialModel.getFetchNum();
                     int totalPrice;
                     if (totalRefundNum.compareTo(fetchNum) <= 0) {
                        drpIntoDetail.setPurNum(totalRefundNum);
                        drpIntoDetail.setInventoryNum(totalRefundNum);
                        totalPrice = totalRefundNum.multiply(new BigDecimal(intoDetailMaterialModel.getUnitPrice())).setScale(0, RoundingMode.HALF_UP).intValue();
                        drpIntoDetail.setTotalPrice(totalPrice);
                        totalAmount += (long)totalPrice;
                        this.drpIntoDetailMapper.insert(drpIntoDetail);
                        this.plusInventoryNum(inventoryId, totalRefundNum);
                        this.drpDetailFlowService.saveDetailFlow(drpIntoInventory, drpIntoDetail, (DrpOutInventory)null, (DrpOutDetailModel)null, (DrpDetailFlow)null, DrpOrderTypeEnum.INTO.key());
                        break;
                     }

                     drpIntoDetail.setPurNum(fetchNum);
                     drpIntoDetail.setInventoryNum(fetchNum);
                     totalPrice = fetchNum.multiply(new BigDecimal(intoDetailMaterialModel.getUnitPrice())).setScale(0, RoundingMode.HALF_UP).intValue();
                     drpIntoDetail.setTotalPrice(totalPrice);
                     totalAmount += (long)totalPrice;
                     this.drpIntoDetailMapper.insert(drpIntoDetail);
                     totalRefundNum = totalRefundNum.subtract(fetchNum);
                     this.plusInventoryNum(inventoryId, fetchNum);
                     this.drpDetailFlowService.saveDetailFlow(drpIntoInventory, drpIntoDetail, (DrpOutInventory)null, (DrpOutDetailModel)null, (DrpDetailFlow)null, DrpOrderTypeEnum.INTO.key());
                  }
               }

               drpIntoInventory.setTotalAmount(totalAmount);
               drpIntoInventory.setTotalNum(totalNum);
               this.drpIntoInventoryMapper.insert(drpIntoInventory);
               return;
            }
         }
      }
   }

   protected void plusInventoryNum(Long inventoryId, BigDecimal totalRefundNum) {
      DrpInventory drpInventory = (DrpInventory)this.drpInventoryMapper.selectOne(Wrappers.lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, inventoryId).eq(DrpInventory::getDelFlag, DelFlagEnum.DEL_FALSE.key()).select(DrpInventory::getMaterialNum, DrpInventory::getMinNum, DrpInventory::getMaxNum));
      BigDecimal materialNum = drpInventory.getMaterialNum().add(totalRefundNum);
      Integer inventoryStatus = InventoryUtil.getInventoryStatusByNum(drpInventory.getMaxNum(), drpInventory.getMinNum(), materialNum);
      this.drpInventoryMapper.plusInventoryNumById(inventoryId, totalRefundNum);
      this.drpInventoryMapper.update(null, Wrappers.lambdaUpdate(DrpInventory.class).eq(DrpInventory::getInventoryId, inventoryId).set(DrpInventory::getInventoryStatus, inventoryStatus));
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void noRelateDrpMinusInventory(AndroidSupermarketOutDTO content, boolean ifAllowOverdraftInventory) {
      List<SupermarketInventoryRepairDetailModel> detailList = new ArrayList();

      AndroidSupermarketOutDetailDTO androidSupermarketOutDetail;
      for(Iterator var4 = content.getDetailList().iterator(); var4.hasNext(); this.supermarketProductMapper.minusInventoryByDetailId(content.getSupermarketId(), androidSupermarketOutDetail.getSupermarketDetailId(), androidSupermarketOutDetail.getFetchNum())) {
         androidSupermarketOutDetail = (AndroidSupermarketOutDetailDTO)var4.next();
         SupermarketInventoryRepairDetailModel detail = new SupermarketInventoryRepairDetailModel();
         detail.setSupermarketDetailId(androidSupermarketOutDetail.getSupermarketDetailId());
         if (ObjectUtil.isNotEmpty(androidSupermarketOutDetail.getSalesMode()) && androidSupermarketOutDetail.getSalesMode().equals(MenuSalesTypeEnum.WEIGHT_ON.key())) {
            androidSupermarketOutDetail.setFetchNum(androidSupermarketOutDetail.getFetchNum().divide(new BigDecimal(SupermarketConstants.WEIGHT_UNIT_PRICE), 3, RoundingMode.HALF_UP));
         }

         detail.setOperateNum(androidSupermarketOutDetail.getFetchNum());
         detailList.add(detail);
         MinusSupermarketInventoryDetailDTO minusInventoryDetail = new MinusSupermarketInventoryDetailDTO();
         minusInventoryDetail.setDetailId(androidSupermarketOutDetail.getSupermarketDetailId());
         minusInventoryDetail.setSaleNum(androidSupermarketOutDetail.getFetchNum());
         if (!ifAllowOverdraftInventory) {
            InventoryNumModel inventoryNumModel = this.supermarketProductMapper.getProductInventory(content.getSupermarketId(), androidSupermarketOutDetail.getSupermarketDetailId());
            if (ObjectUtil.isEmpty(inventoryNumModel)) {
               log.info("商品不存在!");
               throw new ServiceException(I18n.getMessage("drp.pay-product-not-exit", new Object[0]));
            }

            if (inventoryNumModel.getInventoryNum().compareTo(androidSupermarketOutDetail.getFetchNum()) < 0) {
               throw new ServiceException(I18n.getMessage("supermarket.product-num-not-enough", new Object[]{inventoryNumModel.getMaterialName()}));
            }
         }
      }

      String supermarketRepairDetailStr = "yst:merchant-id:supermarket:repair:outId".replace("merchant-id", TenantContextHolder.getTenantId().toString()).replace("outId", String.valueOf(content.getOrdId()));
      this.redisTemplate.opsForValue().set(supermarketRepairDetailStr, detailList, 60L, TimeUnit.MINUTES);
   }
//
//   public List<IntoIdAndOutIdByMaterialIdListModel> listIntoIdAndOutIdByMaterialId(IntoIdAndOutIdByMaterialIdListDTO content) {
//      if (ObjectUtil.isEmpty(content.getStartTime())) {
//         LocalDateTime endTime = LocalDateTime.now();
//         LocalDateTime startTime = endTime.minusDays(7L);
//         content.setStartTime(startTime);
//         content.setEndTime(endTime);
//      }
//
//      List<IntoIdAndOutIdByMaterialIdListModel> addResultList = new ArrayList();
//      List<IntoIdAndOutIdByMaterialIdListModel> resultList = this.drpIntoDetailMapper.listIntoIdAndOutIdByMaterialId(content, DrpIntoInventoryStatusEnum.COMMIT.key());
//      if (CollUtil.isEmpty(resultList)) {
//         return new ArrayList();
//      } else {
//         Iterator var4 = resultList.iterator();
//
//         while(var4.hasNext()) {
//            IntoIdAndOutIdByMaterialIdListModel result = (IntoIdAndOutIdByMaterialIdListModel)var4.next();
//            List<DrpIntoModelVO> intoIdList = this.drpOutDetailMapper.getIntoIdByMaterialIdAndOutId(content.getMaterialId(), result.getOutId());
//            Iterator var7 = intoIdList.iterator();
//
//            while(var7.hasNext()) {
//               DrpIntoModelVO intoId = (DrpIntoModelVO)var7.next();
//               IntoIdAndOutIdByMaterialIdListModel intoIdModel = (IntoIdAndOutIdByMaterialIdListModel)BeanUtil.copyProperties(result, IntoIdAndOutIdByMaterialIdListModel.class, new String[0]);
//               intoIdModel.setIntoId(intoId.getIntoId());
//               intoIdModel.setIntoTime(intoId.getIntoTime());
//               addResultList.add(intoIdModel);
//            }
//         }
//
//         return addResultList;
//      }
//   }
//
//   public List<IntoModel> listIntoIdByMaterialId(IntoIdAndOutIdByMaterialIdListDTO content) {
//      return this.drpIntoDetailMapper.listIntoIdByMaterialId(content, DrpIntoInventoryStatusEnum.COMMIT.key());
//   }
//
//   public List<OutModel> listOutIdByIntoId(String intoId) {
//      return this.drpOutDetailMapper.listOutIdByIntoId(intoId, DrpOutInventoryStatusEnum.COMMIT.key());
//   }
//
//   public List<LeastPriceAndDateModel> listLeastPriceAndDate(List<Long> materialIdList) {
//      return this.drpIntoDetailMapper.listLeastPriceAndDate(materialIdList, DrpIntoInventoryStatusEnum.COMMIT.key());
//   }
//
//   public List<LeastPriceAndDateModel> listMonthPrice(List<Long> materialIdList) {
//      LocalDateTime endTime = LocalDateTime.now();
//      LocalDateTime startTime = endTime.minusMonths(1L);
//      return this.drpIntoDetailMapper.listMonthPrice(materialIdList, startTime, endTime, DrpIntoInventoryStatusEnum.COMMIT.key());
//   }
//
//   public List<DishesMaterialCostModel> listDishesMaterialCost(List<Long> materialIdList) {
//      List<DishesMaterialCostModel> resultList = new ArrayList();
//      String metadataValue = this.allocMetadataApi.getMetadataValue(MetadataModelTypeEnum.DRP, "dishesMaterialCostPriceStyle");
//      if (!CharSequenceUtil.isBlank(metadataValue) && !ObjectUtil.equal(metadataValue, DishesMaterialCostPriceStyleEnum.LEAST_PRICE.key().toString())) {
//         this.calCost(resultList, this.listMonthPrice(materialIdList), false);
//      } else {
//         this.calCost(resultList, this.listLeastPriceAndDate(materialIdList), true);
//      }
//
//      return resultList;
//   }
//
//   private List<DishesMaterialCostModel> calCost(List<DishesMaterialCostModel> resultList, List<LeastPriceAndDateModel> leastPriceAndDateModels, boolean recentFlag) {
//      if (CollUtil.isEmpty(leastPriceAndDateModels)) {
//         return resultList;
//      } else {
//         Iterator var4 = leastPriceAndDateModels.iterator();
//
//         while(var4.hasNext()) {
//            LeastPriceAndDateModel leastPriceAndDateModel = (LeastPriceAndDateModel)var4.next();
//            DishesMaterialCostModel result = new DishesMaterialCostModel();
//            BeanUtil.copyProperties(leastPriceAndDateModel, result, new String[0]);
//            if (leastPriceAndDateModel.getRate() != 0) {
//               BigDecimal costPrice;
//               if (recentFlag) {
//                  costPrice = (new BigDecimal(leastPriceAndDateModel.getLeastIntoPrice())).divide(new BigDecimal(leastPriceAndDateModel.getRate()), 3, RoundingMode.HALF_UP);
//               } else {
//                  costPrice = (new BigDecimal(leastPriceAndDateModel.getMonthAvgPrice())).divide(new BigDecimal(leastPriceAndDateModel.getRate()), 3, RoundingMode.HALF_UP);
//               }
//
//               result.setCostPrice(costPrice);
//               resultList.add(result);
//            }
//         }
//
//         return resultList;
//      }
//   }
//
//   public List<DrpIntoInventoryListModel> queryIntoModel(LocalDateTime startTime, LocalDateTime endTime, Long warehouseId, String keywords) {
//      return this.drpIntoInventoryMapper.queryIntoInventoryList(startTime, endTime, warehouseId, keywords);
//   }
//
//   public void sendIntoAndOutDetailMq(Integer intoAndOutType, DrpIntoInventory intoInventory, DrpIntoDetail intoDetail, DrpOutInventory outInventory, DrpOutDetail outDetail) {
//      DrpMqIntoAndOutModel mqModel;
//      if (Objects.equals(DrpOrderTypeEnum.INTO.key(), intoAndOutType)) {
//         mqModel = new DrpMqIntoAndOutModel();
//         BeanUtil.copyProperties(intoDetail, mqModel, new String[0]);
//         mqModel.setRecordId(intoInventory.getIntoId());
//         mqModel.setWarehouseId(intoInventory.getWarehouseId());
//         mqModel.setRecordType(DrpOrderTypeEnum.INTO.key());
//         mqModel.setRecordDate(intoInventory.getIntoDate().toLocalDate());
//         mqModel.setRecordTime(intoInventory.getIntoDate());
//         mqModel.setOutIntoType(intoInventory.getIntoType());
//         mqModel.setOutIntoAmount(intoDetail.getTotalPrice());
//         mqModel.setOutIntoNum(intoDetail.getPurNum());
//         mqModel.setOperatorId(intoInventory.getCrby());
//         mqModel.setOperateTime(LocalDateTime.now());
//         DrpInventory drpInventory = (DrpInventory)this.drpInventoryMapper.selectOne(((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpInventory.class).eq(DrpInventory::getInventoryId, intoDetail.getInventoryId())).select(new SFunction[]{DrpInventory::getMaterialNum}));
//         mqModel.setInventoryNum(drpInventory.getMaterialNum());
//         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(mqModel), LeMqConstant.Topic.DRP_INTO_AND_OUT_DETAIL);
//      } else if (Objects.equals(DrpOrderTypeEnum.OUT.key(), intoAndOutType)) {
//         mqModel = new DrpMqIntoAndOutModel();
//         mqModel.setDetailId(outDetail.getDetailId());
//         mqModel.setRecordId(outInventory.getOutId());
//         mqModel.setWarehouseId(outInventory.getWarehouseId());
//         mqModel.setRecordType(DrpOrderTypeEnum.OUT.key());
//         mqModel.setRecordDate(outInventory.getOutDate().toLocalDate());
//         mqModel.setRecordTime(outInventory.getOutDate());
//         mqModel.setOutIntoType(outInventory.getOutType());
//         mqModel.setOutIntoAmount(outDetail.getTotalPrice());
//         mqModel.setOutIntoNum(outDetail.getFetchNum());
//         mqModel.setFetchUserId(outInventory.getFetchUserId());
//         mqModel.setOperatorId(outInventory.getCrby());
//         mqModel.setOperateTime(LocalDateTime.now());
//         DrpIntoDetailByIdGetModel intoDetailModel = this.drpOutDetailMapper.getIntoDetailById(outDetail.getIntoDetailId());
//         mqModel.setMaterialId(intoDetailModel.getMaterialId());
//         mqModel.setSupplierId(intoDetailModel.getSupplierId());
//         mqModel.setUnitId(intoDetailModel.getUnitId());
//         mqModel.setUnitPrice(intoDetailModel.getUnitPrice());
//         mqModel.setInventoryNum(intoDetailModel.getInventoryNum());
//         mqModel.setProductDate(intoDetailModel.getProductDate());
//         mqModel.setExpireTime(intoDetailModel.getExpireTime());
//         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(mqModel), LeMqConstant.Topic.DRP_INTO_AND_OUT_DETAIL);
//      }
//
//   }
//
//   public void sendAllocationMq(DrpGoodsAllocation drpGoodsAllocation, DrpGoodsAllocationDetail drpGoodsAllocationDetail) {
//      DrpMqAllocationModel allocationModel = new DrpMqAllocationModel();
//      BeanUtil.copyProperties(drpGoodsAllocationDetail, allocationModel, new String[0]);
//      allocationModel.setGoodsAllocationId(drpGoodsAllocation.getGoodsAllocationId());
//      allocationModel.setOutWarehouseId(drpGoodsAllocation.getOutWarehouseId());
//      allocationModel.setIntoWarehouseId(drpGoodsAllocation.getIntoWarehouseId());
//      allocationModel.setTransferTime(drpGoodsAllocation.getRequestArrivalTime());
//      allocationModel.setOperateTime(drpGoodsAllocation.getCrtime());
//      allocationModel.setOperatorId(drpGoodsAllocation.getCrby());
//      MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(allocationModel), LeMqConstant.Topic.DRP_ALLOCATION_DETAIL);
//   }
//
//   public void sendCancelIntoAndOutMq(Integer intoAndOutType, String recordId) {
//      List mqModelList;
//      if (Objects.equals(DrpOrderTypeEnum.INTO.key(), intoAndOutType)) {
//         mqModelList = this.drpIntoDetailMapper.listCancelIntoDetailMq(recordId);
//         mqModelList.forEach((s) -> {
//            MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(s), LeMqConstant.Topic.DRP_CANCEL_INTO_AND_OUT);
//         });
//      } else if (Objects.equals(DrpOrderTypeEnum.OUT.key(), intoAndOutType)) {
//         mqModelList = this.drpOutDetailMapper.listCancelOutDetailMq(recordId);
//         mqModelList.forEach((s) -> {
//            MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(s), LeMqConstant.Topic.DRP_CANCEL_INTO_AND_OUT);
//         });
//      }
//
//   }
//
//   public List<DrpMaterialDetectModel> queryMaterialDetect(Long warehouseId) {
//      return this.drpIntoInventoryMapper.queryDrpMaterialDetect(warehouseId);
//   }
//
//   public List<DrpMealTimeTypeDishesConvertVO> queryDishesConvertList(String productionPlanId) {
//      List<DrpMealTimeTypeDishesConvertVO> drpMealTimeTypeDishesConvertVOList = Lists.newArrayList();
//      List<DrpProductionDishesNumVO> drpProductionDishesNumVOList = this.drpProductionPlanDetailMapper.listProductionDishesSumByDate(productionPlanId);
//      Map<LocalDate, List<DrpProductionDishesNumVO>> detailDateMap = (Map)drpProductionDishesNumVOList.stream().collect(Collectors.groupingBy(DrpProductionDishesNumVO::getDetailDate));
//      detailDateMap.forEach((detailDate, detailDateList) -> {
//         Map<Integer, List<DrpProductionDishesNumVO>> mealtimeTypeMap = (Map)detailDateList.stream().collect(Collectors.groupingBy(DrpProductionDishesNumVO::getMealtimeType));
//         mealtimeTypeMap.forEach((key, value) -> {
//            DrpMealTimeTypeDishesConvertVO drpMealTimeTypeDishesConvertVO = new DrpMealTimeTypeDishesConvertVO();
//            drpMealTimeTypeDishesConvertVO.setDetailDate(detailDate);
//            drpMealTimeTypeDishesConvertVO.setMealtimeType(key);
//            List<DrpDishesConvertDTO> dtoList = this.getDrpDishesConvertDTOList(value);
//            if (CollUtil.isNotEmpty(dtoList)) {
//               List<DrpDishesConvertVO> drpDishesConvertVOS = this.drpOtherService.dishesConvert(dtoList);
//               List<MenuMaterialUnitVO> menuMaterialUnitVOList = this.menuMaterialMapper.getMenuMaterialAndUnitByMaterialIdList((List)drpDishesConvertVOS.stream().map(DrpDishesConvertVO::getMaterialId).collect(Collectors.toList()));
//               Map<Long, MenuMaterialUnitVO> materialUnitMap = (Map)menuMaterialUnitVOList.stream().collect(Collectors.toMap(MenuMaterialUnitVO::getMaterialId, (e) -> {
//                  return e;
//               }, (k1, k2) -> {
//                  return k1;
//               }));
//               drpDishesConvertVOS.forEach((drpDishesConvertVO) -> {
//                  MenuMaterialUnitVO menuMaterialUnitVO = (MenuMaterialUnitVO)materialUnitMap.get(drpDishesConvertVO.getMaterialId());
//                  drpDishesConvertVO.setUnitId(menuMaterialUnitVO.getUnitId());
//                  if (menuMaterialUnitVO.getRate().compareTo(BigDecimal.ZERO) > 0) {
//                     drpDishesConvertVO.setUnitName(menuMaterialUnitVO.getUnitName());
//                     BigDecimal purNumByg = drpDishesConvertVO.getTotalConvertWeight().multiply(new BigDecimal(1000));
//                     drpDishesConvertVO.setTotalConvertWeight(purNumByg.divide(menuMaterialUnitVO.getRate(), 3, RoundingMode.HALF_UP));
//                  }
//
//               });
//               drpMealTimeTypeDishesConvertVO.setDishesConvertVOList(drpDishesConvertVOS);
//               drpMealTimeTypeDishesConvertVOList.add(drpMealTimeTypeDishesConvertVO);
//            }
//
//         });
//      });
//      return drpMealTimeTypeDishesConvertVOList;
//   }
//
//   protected List<DrpDishesConvertDTO> getDrpDishesConvertDTOList(List<DrpProductionDishesNumVO> value) {
//      List<DrpDishesConvertDTO> dtoList = Lists.newArrayListWithCapacity(value.size());
//      Iterator var3 = value.iterator();
//
//      while(var3.hasNext()) {
//         DrpProductionDishesNumVO drpProductionDishesNumVO = (DrpProductionDishesNumVO)var3.next();
//         DrpDishesConvertDTO dto = new DrpDishesConvertDTO();
//         dto.setDishesId(drpProductionDishesNumVO.getDishesId());
//         dto.setDishesNum(drpProductionDishesNumVO.getDishesNum());
//         dto.setSizeType((long)drpProductionDishesNumVO.getSizeType());
//         dtoList.add(dto);
//      }
//
//      return dtoList;
//   }
//
//   public List<MenuMaterialUnitVO> getMaterialUnitCost(List<MenuMaterialCostDTO> content) {
//      if (CollUtil.isEmpty(content)) {
//         throw new LeException("缺少必要参数！");
//      } else {
//         List<MenuMaterialUnitVO> menuMaterialList = this.menuMaterialMapper.getMenuMaterialAndUnitByMaterialIdList((List)content.stream().map(MenuMaterialCostDTO::getMaterialId).collect(Collectors.toList()));
//         Map<Long, BigDecimal> materialWeightMap = (Map)content.stream().collect(Collectors.toMap(MenuMaterialCostDTO::getMaterialId, MenuMaterialCostDTO::getWeight, (k1, k2) -> {
//            return k1;
//         }));
//         menuMaterialList.forEach((menuMaterialUnitVO) -> {
//            menuMaterialUnitVO.setNum(menuMaterialUnitVO.getRate() != null && menuMaterialUnitVO.getRate().compareTo(BigDecimal.ZERO) > 0 ? ((BigDecimal)Optional.ofNullable((BigDecimal)materialWeightMap.get(menuMaterialUnitVO.getMaterialId())).orElse(BigDecimal.ZERO)).divide(menuMaterialUnitVO.getRate(), 3, RoundingMode.HALF_UP) : BigDecimal.ZERO);
//         });
//         return menuMaterialList;
//      }
//   }

}
