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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.common.redis.RedisUtil;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.menu.dto.OrderRecipeDishesDto;
import com.bonus.core.menu.entity.MenuDishes;
import com.bonus.core.menu.entity.MenuRecipeDetail;
import com.bonus.core.menu.entity.MenuRecipeDishes;
import com.bonus.core.menu.mapper.MenuRecipeDishesMapper;
import com.bonus.core.menu.service.MenuDishesService;
import com.bonus.core.menu.service.MenuRecipeDetailService;
import com.bonus.core.menu.service.MenuRecipeDishesService;
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 javax.annotation.Resource;
import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MenuRecipeDishesServiceImpl extends ServiceImpl<MenuRecipeDishesMapper, MenuRecipeDishes> implements MenuRecipeDishesService {
   private static final Logger log = LoggerFactory.getLogger(MenuRecipeDishesServiceImpl.class);

   @Resource
   private MenuRecipeDishesMapper menuRecipeDishesMapper;
   @Autowired
   @Lazy
   private MenuRecipeDetailService menuRecipeDetailService;
   @Autowired
   @Lazy
   private MenuDishesService menuDishesService;

   @Override
   public boolean saveBatch(Collection<MenuRecipeDishes> entityList) {
      Iterator var2 = CollUtil.split(entityList, 500).iterator();

      while(var2.hasNext()) {
         List<MenuRecipeDishes> menuRecipeDishes = (List)var2.next();
         menuRecipeDishesMapper.insertValues(menuRecipeDishes);
      }

      return true;
   }

   public OrderRecipeDishesDto getOrderDishesDetail(Long detailId, Long dishesId) {
      MenuRecipeDetail menuRecipeDetail = this.menuRecipeDetailService.getOne(Wrappers
              .lambdaQuery(MenuRecipeDetail.class).select(MenuRecipeDetail::getRecipeId)
              .eq(MenuRecipeDetail::getDetailId, detailId));
      if (ObjectUtil.isNull(menuRecipeDetail)) {
         throw new ServiceException("菜谱已更新,请刷新页面重新点餐");
      } else {
         String redisKey = String.format("yst:%s:recipe:%s:detail:%s:dishes:%s:order:query", TenantContextHolder.getTenantId(), menuRecipeDetail.getRecipeId(), detailId, dishesId);
         Object obj = RedisUtil.getObj(redisKey);
         if (obj instanceof OrderRecipeDishesDto) {
            OrderRecipeDishesDto o = (OrderRecipeDishesDto)obj;
            log.info("取redis缓存,详情: {}", JSON.toJSONString(obj));
            return o;
         } else {
            MenuRecipeDishes menuRecipeDishes = this.getOne(Wrappers
                    .lambdaQuery(MenuRecipeDishes.class).eq(MenuRecipeDishes::getDishesId, dishesId)
                    .eq(MenuRecipeDishes::getDetailId, detailId));
            MenuDishes menuDishes = this.menuDishesService.getOne(Wrappers
                    .lambdaQuery(MenuDishes.class).eq(MenuDishes::getDishesId, dishesId));
            OrderRecipeDishesDto orderRecipeDishesDto = BeanUtil.copyProperties(menuRecipeDishes, OrderRecipeDishesDto.class);
            orderRecipeDishesDto.setSalesMode(menuDishes.getSalesMode());
            orderRecipeDishesDto.setUnitPrice(menuDishes.getUnitPrice());
            orderRecipeDishesDto.setImageUrl(menuDishes.getImageUrl());
            orderRecipeDishesDto.setDishesName(menuDishes.getDishesName());
            log.info("查询数据库信息,详情: {}", JSON.toJSONString(orderRecipeDishesDto));
            RedisUtil.setObj(redisKey, orderRecipeDishesDto, 86400L);
            return orderRecipeDishesDto;
         }
      }
   }

}
