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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.allocation.holiday.mapper.MktHolidayMapper;
import com.bonus.canteen.core.allocation.holiday.model.MktHoliday;
import com.bonus.canteen.core.allocation.holiday.service.MktHolidayService;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class MktHolidayServiceImpl extends ServiceImpl<MktHolidayMapper, MktHoliday> implements MktHolidayService {
   private static final Logger log = LoggerFactory.getLogger(MktHolidayServiceImpl.class);
   @Resource(
      name = "holidayRest"
   )
   private RestTemplate restTemplate;

   @Override
   public Integer getHolidayType(LocalDate searchDate) {
      MktHoliday mktHoliday = (MktHoliday)((MktHolidayMapper)this.baseMapper).selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MktHoliday.class).eq(MktHoliday::getIfDel, LeConstants.COMMON_NO)).apply("hol_date IN ({0})",new Object[]{searchDate.atStartOfDay()}));
      return Objects.isNull(mktHoliday) ? null : mktHoliday.getHolType();
   }

   @Override
   public List<Date> getHolidayList(Integer year) {
      List<Date> holidayCache = this.getHolidayCache(year);
      if (CollectionUtils.isNotEmpty(holidayCache)) {
         return holidayCache;
      } else {
         List<Date> dateList = ((MktHolidayMapper)this.baseMapper).selectAllDate(year);
         this.updateHolidayCache(year);
         return dateList;
      }
   }
   private List<Date> getHolidayCache(Integer year) {
      Long var10000 = TenantContextHolder.getTenantId();
      String key = "yst:" + var10000 + ":mkt:holiday:" + year;
      return this.getRedisCache(key);
   }
   private List<Date> getRedisCache(String key) {
      List<Date> dateList = new ArrayList();
      String deductionConfJsonStr = RedisUtil.getString(key);

      try {
         dateList = (List)JSON.parseObject(deductionConfJsonStr, new TypeReference<List<Date>>() {
         }, new Feature[0]);
      } catch (Exception var5) {
         log.error("[节假日]读取节假日缓存失败," + var5.getMessage(), var5);
      }

      return (List)dateList;
   }

   private void updateHolidayCache(Integer year) {
      List<Date> dateList = ((MktHolidayMapper)this.baseMapper).selectAllDate(year);
      Long var10000 = TenantContextHolder.getTenantId();
      String key = "yst:" + var10000 + ":mkt:holiday:" + year;
      RedisUtil.setString(key, JSON.toJSONString(dateList), (Long)null);
   }

   @Override
   public List<MktHoliday> listHolidayByDate(LocalDate searchDate) {
      return this.list(Wrappers.<MktHoliday>lambdaQuery().eq(MktHoliday::getIfDel,
              LeConstants.COMMON_NO).eq(MktHoliday::getHolDate, searchDate.atStartOfDay()));
   }
}
