package com.bonus.canteen.core.allocation.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.allocation.canteen.dto.AllocRecipeStallDTO;
import com.bonus.canteen.core.allocation.canteen.mapper.AllocStallMapper;
import com.bonus.canteen.core.allocation.canteen.model.AllocStall;
import com.bonus.canteen.core.allocation.canteen.service.AllocStallService;
import com.bonus.canteen.core.allocation.canteen.vo.AllocRecipeStallVO;
import com.bonus.canteen.core.allocation.canteen.vo.CanteenStallVO;
import com.bonus.canteen.core.common.enums.AllocBusinessStateEnum;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.Set;

@Component
public class AllocStallApi {
   private static final Logger log = LoggerFactory.getLogger(AllocStallApi.class);
   @Resource
   @Lazy
   private AllocStallService allocStallService;
   @Resource
   private AllocStallMapper allocStallMapper;
   public AllocStall getAllocStall(Long stallId) {
      return this.allocStallService.getAllocStallCache(stallId);
   }

   public void checkStallBusinessState(Long stallId, LocalTime checkTime) {
      if (!LeNumUtil.isValidId(stallId)) {
         log.warn("档口id不合法,无法校验营业状态");
      } else {
         AllocStall allocStall = this.getAllocStall(stallId);
         if (!LeConstants.COMMON_YES.equals(allocStall.getIfDel()) && AllocBusinessStateEnum.OPEN.getKey().equals(allocStall.getBusinessState())) {
            if (!ObjectUtil.isNull(checkTime)) {
               if (!ObjectUtil.isNull(allocStall.getStartBusinessTime()) && !ObjectUtil.isNull(allocStall.getEndBusinessTime())) {
                  if (allocStall.getStartBusinessTime().isAfter(allocStall.getEndBusinessTime())) {
                     if (checkTime.isBefore(allocStall.getStartBusinessTime()) && checkTime.isAfter(allocStall.getEndBusinessTime())) {
                        throw new ServiceException(I18n.getMessage("alloc_stall_is_rest", new Object[]{allocStall.getStallName()}));
                     }
                  } else if (checkTime.isBefore(allocStall.getStartBusinessTime()) || checkTime.isAfter(allocStall.getEndBusinessTime())) {
                     throw new ServiceException(I18n.getMessage("alloc_stall_is_rest", new Object[]{allocStall.getStallName()}));
                  }

               }
            }
         } else {
            throw new ServiceException(I18n.getMessage("alloc_stall_is_rest", new Object[]{allocStall.getStallName()}));
         }
      }
   }

   public List<AllocStall> listStallByStallIdList(Set<Long> stallIdList) {
      return this.allocStallService.list(Wrappers.lambdaQuery(AllocStall.class)
              .in(AllocStall::getStallId, stallIdList).eq(AllocStall::getIfDel, LeConstants.COMMON_NO));
   }

   public Long getCanteenId(Long stallId) {
      AllocStall stall = this.getAllocStall(stallId);
      return ObjectUtil.isNull(stall) ? null : stall.getCanteenId();
   }

   public Page<AllocRecipeStallVO> pageRecipeStallInfo(AllocRecipeStallDTO recipeStallDTO) {
      Page<AllocRecipeStallVO> page = new Page(recipeStallDTO.getCurrent(), recipeStallDTO.getSize());
      recipeStallDTO.setRecipeName(LeBeanUtil.fieldLikeHandle(recipeStallDTO.getRecipeName()));
      if (LeConstants.COMMON_YES.equals(recipeStallDTO.getIfQueryMealLine())) {
         this.allocStallMapper.pageRecipeMealLineInfo(page, recipeStallDTO);
         return page;
      } else {
         this.allocStallMapper.pageRecipeStallInfo(page, recipeStallDTO);
         return page;
      }
   }

   public List<CanteenStallVO> listStallByCanteenId(Long canteenId) {
      return this.allocStallMapper.listStallByCanteenId(canteenId);
   }

}
