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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.menu.dto.AppletDishesDetailDTO;
import com.bonus.canteen.core.menu.entity.MenuCollectionDishes;
import com.bonus.canteen.core.menu.entity.MenuDishes;
import com.bonus.canteen.core.menu.mapper.MenuCollectionDishesMapper;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.menu.mapper.MenuMaterialDishesMapper;
import com.bonus.canteen.core.menu.mapper.MenuPackageDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesService;
import com.bonus.canteen.core.menu.utils.NutritionEntity;
import com.bonus.canteen.core.menu.vo.AppletDishesDetailVO;
import com.bonus.canteen.core.menu.vo.AppletDishesSizeV2VO;
import com.bonus.common.houqin.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);
   }

   public List<MenuDishes> getMenuDishesList(List<Long> dishesIdList) {
      return this.list(Wrappers.lambdaQuery(MenuDishes.class)
              .in(MenuDishes::getDishesId, dishesIdList).eq(MenuDishes::getDelFlag, LeConstants.COMMON_NO));
   }

}
