package com.bonus.core.menu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.constant.DelFlagEnum;
import com.bonus.core.menu.dto.AppletDishesDetailDTO;
import com.bonus.core.menu.entity.MenuCollectionDishes;
import com.bonus.core.menu.entity.MenuDishes;
import com.bonus.core.menu.mapper.MenuCollectionDishesMapper;
import com.bonus.core.menu.mapper.MenuDishesMapper;
import com.bonus.core.menu.mapper.MenuMaterialDishesMapper;
import com.bonus.core.menu.mapper.MenuPackageDishesMapper;
import com.bonus.core.menu.service.MenuDishesService;
import com.bonus.core.menu.utils.NutritionEntity;
import com.bonus.core.menu.vo.AppletDishesDetailVO;
import com.bonus.core.menu.vo.AppletDishesSizeV2VO;
import com.bonus.i18n.I18n;
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.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenuDishesServiceImpl extends ServiceImpl<MenuDishesMapper, MenuDishes> implements MenuDishesService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesServiceImpl.class);

   @Autowired
   @Lazy
   private MenuDishesService menuDishesService;

   @Autowired
   @Lazy
   private MenuCollectionDishesMapper menuCollectionDishesMapper;
   @Autowired
   @Lazy
   private MenuMaterialDishesMapper menuMaterialDishesMapper;
   @Autowired
   @Lazy
   private MenuPackageDishesMapper menuPackageDishesMapper;


   public AppletDishesDetailVO getDishesDetailByDishesId(AppletDishesDetailDTO dishesDetailDTO) {
      //AppletDishesDetailDTO dishesDetailDTO = (AppletDishesDetailDTO)JSON.parseObject(content, AppletDishesDetailDTO.class);
      dishesDetailDTO.setDelFlag(DelFlagEnum.DEL_FALSE.key());
      Long baseDishesId = dishesDetailDTO.getBaseDishesId();
      Long custId = dishesDetailDTO.getCustId();
      if (!ObjectUtil.isNull(custId) && custId != -1L) {
         AppletDishesDetailVO appletDishesDetailVO = ((MenuDishesMapper)this.baseMapper).selectDishesDetailByDishesId(dishesDetailDTO);
         Long countDishes = this.menuCollectionDishesMapper.selectCount(Wrappers.lambdaQuery(MenuCollectionDishes.class)
                 .eq(MenuCollectionDishes::getCustId, custId).eq(MenuCollectionDishes::getDishesId, baseDishesId));
         if (ObjectUtil.isNotNull(countDishes) && countDishes > 0L) {
            appletDishesDetailVO.setIsFavorites(1);
         } else {
            appletDishesDetailVO.setIsFavorites(2);
         }

         Long dishesId = ((AppletDishesSizeV2VO)appletDishesDetailVO.getDishesDetailList().get(0)).getDishesId();
         List materialList;
         if (appletDishesDetailVO.getMealType() == 1) {
            materialList = this.menuMaterialDishesMapper.getMenuMaterialById(dishesId);
            appletDishesDetailVO.setMaterialList(materialList);
         } else {
            materialList = this.menuPackageDishesMapper.selectDishesName(dishesId);
            appletDishesDetailVO.setPackageDishesList(materialList);
         }

         return appletDishesDetailVO;
      } else {
         throw new ServiceException(I18n.getMessage("menu_no_cust_info", new Object[0]));
      }
   }

   @Override
   public NutritionEntity getNutrientInfo(Map<Long, Double> dishesQuantityMap) {
      return this.menuDishesService.getNutrientInfo(dishesQuantityMap);
   }

}
