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.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.menu.domain.MenuDishes;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.supply.purchase.api.MenuDishesApi;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpDishesConvertDTO;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpUnit;
import com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesMaterialModel;
import com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesSizeModel;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpDishesConvertVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.MaterialInventoryVO;
import com.bonus.canteen.core.supply.purchase.mapper.DrpInventoryMapper;
import com.bonus.canteen.core.supply.purchase.mapper.DrpUnitMapper;
import com.bonus.canteen.core.supply.purchase.service.DrpOtherService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.DelFlagEnum;
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 java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DrpOtherServiceImpl implements DrpOtherService {
   private static final Logger log = LoggerFactory.getLogger(DrpOtherServiceImpl.class);
   @Autowired
   @Lazy
   private DrpUnitMapper drpUnitMapper;
   @Autowired
   @Lazy
   private DrpInventoryMapper drpInventoryMapper;
   @Autowired
   @Lazy
   private MenuDishesApi menuDishesApi;
   @Autowired
   @Lazy
   private MenuDishesMapper menuDishesMapper;

   public List<DrpDishesConvertVO> dishesConvert(List<DrpDishesConvertDTO> content) {
      if (CollUtil.isEmpty(content)) {
         throw new ServiceException("换算菜品不能为空");
      } else {
         List<Long> dishesIdList = content.stream().map(DrpDishesConvertDTO::getDishesId).collect(Collectors.toList());
         if (CollUtil.isEmpty(dishesIdList)) {
            throw new ServiceException("换算菜品id集合为空");
         } else {
            List<MenuDishesSizeModel> menuDishesSizeModels = this.menuDishesApi.listDishesSizeModel();
            HashMap<String, Long> dishesIdMap = new HashMap();
            Iterator var5 = menuDishesSizeModels.iterator();

            Long var10000;
            while(var5.hasNext()) {
               MenuDishesSizeModel menuDishesSizeModel = (MenuDishesSizeModel)var5.next();
               var10000 = menuDishesSizeModel.getBaseDishesId();
               String key = "" + var10000 + "_" + menuDishesSizeModel.getSizeType();
               dishesIdMap.put(key, menuDishesSizeModel.getDishesId());
            }

            List<Long> realDishesIdList = new ArrayList();
            Iterator var18 = content.iterator();

            while(var18.hasNext()) {
               DrpDishesConvertDTO drpDishesConvertDTO = (DrpDishesConvertDTO)var18.next();
               log.info("生产计划换算，明细:{}", drpDishesConvertDTO);
               if (ObjectUtil.isEmpty(drpDishesConvertDTO.getSizeType())) {
                  log.info("赋值sizeType");
                  MenuDishes sizeTypeModel = (MenuDishes)this.menuDishesMapper.selectOne((Wrappers.lambdaQuery(MenuDishes.class).eq(MenuDishes::getBaseDishesId, drpDishesConvertDTO.getDishesId())).select(MenuDishes::getSizeType));
                  drpDishesConvertDTO.setSizeType(sizeTypeModel.getSizeType());
               }

               var10000 = drpDishesConvertDTO.getDishesId();
               String key = "" + var10000 + "_" + drpDishesConvertDTO.getSizeType();
               Long dishesId = (Long)dishesIdMap.get(key);
               realDishesIdList.add(dishesId);
            }

            List<MenuDishesMaterialModel> menuDishesMaterialModelList = this.menuDishesApi.listDishesMaterial(realDishesIdList);
            if (CollUtil.isEmpty(menuDishesMaterialModelList)) {
               throw new ServiceException("菜品组成原料不存在");
            } else {
               Iterator var21 = menuDishesMaterialModelList.iterator();

               while(true) {
                  while(var21.hasNext()) {
                     MenuDishesMaterialModel menuDishesMaterialModel = (MenuDishesMaterialModel)var21.next();
                     Iterator var26 = content.iterator();

                     while(var26.hasNext()) {
                        DrpDishesConvertDTO drpDishesConvertDTO = (DrpDishesConvertDTO)var26.next();
                        var10000 = drpDishesConvertDTO.getDishesId();
                        String key = "" + var10000 + "_" + drpDishesConvertDTO.getSizeType();
                        Long dishesId = (Long)dishesIdMap.get(key);
                        if (ObjectUtil.equal(menuDishesMaterialModel.getDishesId(), dishesId)) {
                           menuDishesMaterialModel.setCalWeight(menuDishesMaterialModel.getWeight().multiply(new BigDecimal(drpDishesConvertDTO.getDishesNum())));
                           menuDishesMaterialModel.setCalWeight(menuDishesMaterialModel.getCalWeight().divide(menuDishesMaterialModel.getEdible().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP), 0, RoundingMode.HALF_UP));
                           break;
                        }
                     }
                  }

                  Map<Long, BigDecimal> materialTotalWeightMap = menuDishesMaterialModelList.stream().collect(Collectors.groupingBy(MenuDishesMaterialModel::getMaterialId, Collectors.reducing(BigDecimal.ZERO, MenuDishesMaterialModel::getCalWeight, BigDecimal::add)));
                  DrpUnit drpUnit = (DrpUnit)this.drpUnitMapper.selectOne((Wrappers.lambdaQuery(DrpUnit.class).eq(DrpUnit::getUnitName, "公斤")).eq(DrpUnit::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
                  if (ObjectUtil.isEmpty(drpUnit)) {
                     throw new ServiceException("公斤单位不存在");
                  }

                  Map<Long, MenuDishesMaterialModel> collect = menuDishesMaterialModelList.stream().collect(Collectors.toMap(MenuDishesMaterialModel::getMaterialId, Function.identity(), (k1, k2) -> {
                     return k1;
                  }));
                  List<DrpDishesConvertVO> resultList = new ArrayList();
                  Iterator var29 = collect.values().iterator();

                  while(var29.hasNext()) {
                     MenuDishesMaterialModel menuDishesMaterialModel = (MenuDishesMaterialModel)var29.next();
                     DrpDishesConvertVO drpDishesConvertVO = new DrpDishesConvertVO();
                     BeanUtil.copyProperties(menuDishesMaterialModel, drpDishesConvertVO, new String[0]);
                     drpDishesConvertVO.setUnitId(drpUnit.getUnitId());
                     drpDishesConvertVO.setUnitName(drpUnit.getUnitName());
                     drpDishesConvertVO.setTotalConvertWeight(((BigDecimal)materialTotalWeightMap.get(menuDishesMaterialModel.getMaterialId())).divide(drpUnit.getRate(), 3, RoundingMode.HALF_UP));
                     resultList.add(drpDishesConvertVO);
                  }

                  List<Long> materialIdList = resultList.stream().map(DrpDishesConvertVO::getMaterialId).collect(Collectors.toList());
                  List<MaterialInventoryVO> materialInventoryVOList = this.drpInventoryMapper.selectInventoryByMaterialIdList(materialIdList, (Long)null);
                  Iterator var33 = resultList.iterator();

                  while(true) {
                     while(var33.hasNext()) {
                        DrpDishesConvertVO drpDishesConvertVO = (DrpDishesConvertVO)var33.next();
                        Iterator var15 = materialInventoryVOList.iterator();

                        while(var15.hasNext()) {
                           MaterialInventoryVO materialInventoryVO = (MaterialInventoryVO)var15.next();
                           if (ObjectUtil.equal(drpDishesConvertVO.getMaterialId(), materialInventoryVO.getMaterialId()) && ObjectUtil.equal(drpDishesConvertVO.getUnitId(), materialInventoryVO.getUnitId())) {
                              drpDishesConvertVO.setInventoryNum(materialInventoryVO.getInventoryNum());
                              break;
                           }
                        }
                     }

                     return resultList;
                  }
               }
            }
         }
      }
   }
}
