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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.constant.LeConstants;
import com.bonus.core.allocation.api.AllocPayMetadataApi;
import com.bonus.core.allocation.canteen.mapper.AllocCanteenMapper;
import com.bonus.core.allocation.canteen.mapper.AllocStallMapper;
import com.bonus.core.allocation.canteen.model.AllocCanteen;
import com.bonus.core.allocation.canteen.model.AllocStall;
import com.bonus.core.allocation.canteen.service.AllocAreaService;
import com.bonus.core.allocation.canteen.service.AllocCanteenService;
import com.bonus.core.allocation.canteen.vo.AllocCanteenStallVO;
import com.bonus.core.common.enums.AllocCanteenTypeEnum;
import com.bonus.core.menu.vo.AppletReserveCanteenVO;
import com.bonus.core.menu.vo.AppletReserveStallVO;
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.util.*;
import java.util.stream.Collectors;

@Service
public class AllocCanteenServiceImpl extends ServiceImpl<AllocCanteenMapper, AllocCanteen> implements AllocCanteenService {
   private static final Logger log = LoggerFactory.getLogger(AllocCanteenServiceImpl.class);
   private static final String CACHE_PREFIX = "yst:";
   private static final String CACHE_KEY = "alloc-canteen";
   private static final long CACHE_SECONDS = 3600L;
   @Resource
   @Lazy
   private AllocPayMetadataApi allocPayMetadataApi;
   @Autowired
   @Lazy
   private AllocAreaService allocAreaService;
   @Resource
   private AllocStallMapper allocStallMapper;

   @Override
   public List<Integer> listAvailPayTypeForApp(Long canteenId) {
      return this.allocPayMetadataApi.supportPayChannels(canteenId);
   }

   @Override
   public List<Long> getCanteenIdListByAreaIdList(List<Long> areaIdList) {
      if (CollUtil.isEmpty(areaIdList)) {
         return new ArrayList();
      } else {
         List<Long> allAreaIdList = new ArrayList();
         Iterator var3 = areaIdList.iterator();

         while(var3.hasNext()) {
            Long areaId = (Long)var3.next();
            allAreaIdList.addAll(this.allocAreaService.getAllChildrenId(areaId));
         }

         allAreaIdList = new ArrayList(new HashSet(allAreaIdList));
         return ((AllocCanteenMapper)this.baseMapper).getCanteenIdListByAreaIdList(allAreaIdList);
      }
   }

   @Override
   public List<AllocCanteenStallVO> listAllCanteenStall() {
      List<AllocCanteenStallVO> canteenList = ((AllocCanteenMapper)this.baseMapper).listAllCanteenStall(LeConstants.COMMON_NO, AllocCanteenTypeEnum.CANTEEN.getKey());
      if (CollUtil.isEmpty(canteenList)) {
         return canteenList;
      } else {
         canteenList.forEach((canteen) -> {
            canteen.setStallList(this.allocStallMapper.listAllCanteenStall(canteen.getCanteenId()));
         });
         return canteenList;
      }
   }

   @Override
   public void checkAndDelReserveCanteen(List<AppletReserveCanteenVO> reserveCanteenList) {
      List<Long> canteenIdList = new ArrayList();
      List<Long> stallIdList = new ArrayList();
      Iterator var4 = reserveCanteenList.iterator();

      while(true) {
         AppletReserveCanteenVO reserveCanteen;
         do {
            if (!var4.hasNext()) {
               List<AllocCanteen> allocCanteenList = this.list((Wrapper<AllocCanteen>) Wrappers.lambdaQuery(AllocCanteen.class).select(AllocCanteen::getCanteenId, AllocCanteen::getIfReserve).in(AllocCanteen::getCanteenId, canteenIdList));
               Map<Long, Integer> allocCanteenMap = (Map)allocCanteenList.stream().collect(Collectors.toMap(AllocCanteen::getCanteenId, AllocCanteen::getIfReserve));
               Map<Long, Integer> allocStallMap = new HashMap();
               if (CollUtil.isNotEmpty(stallIdList)) {
                  List<AllocStall> allocStallList = this.allocStallMapper.selectList((Wrapper<AllocStall>)Wrappers.lambdaQuery(AllocStall.class).select(AllocStall::getStallId, AllocStall::getIfReserve).in(AllocStall::getStallId, stallIdList));
                  allocStallMap = (Map)allocStallList.stream().collect(Collectors.toMap(AllocStall::getStallId, AllocStall::getIfReserve));
               }

               Iterator<AppletReserveCanteenVO> canteenIterator = reserveCanteenList.iterator();

               while(true) {
                  while(canteenIterator.hasNext()) {
                     AppletReserveCanteenVO reserveCanteenVO = (AppletReserveCanteenVO)canteenIterator.next();
                     Integer canteenIfReserve = (Integer)allocCanteenMap.get(reserveCanteenVO.getCanteenId());
                     if (!LeConstants.COMMON_YES.equals(canteenIfReserve)) {
                        canteenIterator.remove();
                     } else if (CollUtil.isNotEmpty(reserveCanteenVO.getStallList())) {
                        Iterator<AppletReserveStallVO> stallIterator = reserveCanteenVO.getStallList().iterator();

                        while(stallIterator.hasNext()) {
                           AppletReserveStallVO reserveStallVO = (AppletReserveStallVO)stallIterator.next();
                           Integer stallIfReserve = (Integer)((Map)allocStallMap).get(reserveStallVO.getStallId());
                           if (!LeConstants.COMMON_YES.equals(stallIfReserve)) {
                              stallIterator.remove();
                           }
                        }
                     }
                  }

                  return;
               }
            }

            reserveCanteen = (AppletReserveCanteenVO)var4.next();
            canteenIdList.add(reserveCanteen.getCanteenId());
         } while(!CollUtil.isNotEmpty(reserveCanteen.getStallList()));

         Iterator var6 = reserveCanteen.getStallList().iterator();

         while(var6.hasNext()) {
            AppletReserveStallVO reserveStall = (AppletReserveStallVO)var6.next();
            stallIdList.add(reserveStall.getStallId());
         }
      }
   }
}
