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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.allocation.canteen.mapper.AllocStallMapper;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.allocation.canteen.model.AllocStall;
import com.bonus.canteen.core.allocation.canteen.service.AllocStallService;
import com.bonus.canteen.core.common.enums.AllocBusinessStateEnum;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.common.houqin.i18n.I18n;
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 String getStallNameByIdSet(Set<Long> stallIdSet) {
      return this.allocStallMapper.getStallNameByIdSet(stallIdSet);
   }
}
