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

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.allocation.api.AllocHolidayApi;
import com.bonus.core.allocation.api.AllocOrderMetadataApi;
import com.bonus.core.allocation.canteen.dto.AllocCanteenOrderDTO;
import com.bonus.core.allocation.canteen.dto.AllocMobileCanteenQueryDTO;
import com.bonus.core.allocation.canteen.mapper.AllocStallMapper;
import com.bonus.core.allocation.canteen.model.AllocStall;
import com.bonus.core.allocation.canteen.service.AllocStallService;
import com.bonus.core.common.redis.RedisUtil;
import com.bonus.core.common.utils.TenantContextHolder;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class AllocStallServiceImpl extends ServiceImpl<AllocStallMapper, AllocStall> implements AllocStallService{
   private static final Logger log = LoggerFactory.getLogger(AllocStallServiceImpl.class);
   private static final String CACHE_PREFIX = "yst:";
   private static final String CACHE_KEY = "alloc-stall";
   private static final long CACHE_SECONDS = 3600L;

   @Autowired
   @Lazy
   private AllocOrderMetadataApi allocOrderMetadataApi;

   @Autowired
   @Lazy
   private AllocHolidayApi allocHolidayApi;

   @Override
   public AllocStall getAllocStallCache(Long stallId) {
      if (ObjectUtil.isNull(stallId)) {
         return null;
      } else {
         String cacheKey = this.getCacheKey(stallId);
         String allocStallStr = RedisUtil.getString(cacheKey);
         if (CharSequenceUtil.isNotBlank(allocStallStr)) {
            return (AllocStall)JSONUtil.toBean(allocStallStr, AllocStall.class);
         } else {
            AllocStall allocStall = this.getOne(new LambdaQueryWrapper<AllocStall>().eq(AllocStall::getStallId, stallId));
            allocStallStr = JSONUtil.toJsonStr(allocStall);
            RedisUtil.setString(cacheKey, allocStallStr, 3600L);
            return allocStall;
         }
      }
   }

   @Override
   public List<LocalDate> listReserveDate(AllocMobileCanteenQueryDTO queryDTO) {
      AllocCanteenOrderDTO allocCanteenOrderDTO = this.allocOrderMetadataApi.queryAvailableOrder(queryDTO.getCanteenId(), queryDTO.getStallId());
      String ifReserveSkipHoliday = allocCanteenOrderDTO.getIfReserveSkipHoliday();
      Integer reserveLimitDayNum = Integer.parseInt(allocCanteenOrderDTO.getReserveLimitDay());
      return this.listMatchData(ifReserveSkipHoliday, reserveLimitDayNum, allocCanteenOrderDTO.getIfAllowReserveToday());
   }

   private String getCacheKey(Long stallId) {
      Long var10000 = TenantContextHolder.getTenantId();
      return "yst:" + var10000 + ":alloc-stall:" + stallId;
   }

   private List<LocalDate> listMatchData(String skipFlagStr, Integer limitNum, String ifAllowReserveToday) {
      log.info(String.format("查询日期,是否调过节假日:%s,天数:%d", skipFlagStr, limitNum));
      List<LocalDate> reserveDateList = new ArrayList();
      LocalDate startDate;
      if ("1".equals(ifAllowReserveToday)) {
         log.info("允许预定当天");
         startDate = LocalDate.now();
      } else {
         log.info("不允许预定当天");
         startDate = LocalDate.now().plusDays(1L);
      }

      if ("2".equals(skipFlagStr)) {
         for(int i = 0; i < limitNum; ++i) {
            reserveDateList.add(startDate.plusDays((long)i));
         }
      } else {
         List<LocalDate> holidayList = this.allocHolidayApi.getHolidayList();

         for(int i = 0; i < limitNum; ++i) {
            LocalDate reserveDate = startDate.plusDays((long)i);
            boolean skipFlag = false;
            Iterator var10 = holidayList.iterator();

            while(var10.hasNext()) {
               LocalDate holiday = (LocalDate)var10.next();
               if (reserveDate.isEqual(holiday)) {
                  skipFlag = true;
                  limitNum = limitNum + 1;
               }
            }

            if (!skipFlag) {
               reserveDateList.add(reserveDate);
            }
         }
      }

      return reserveDateList;
   }
}
