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

import cn.hutool.core.collection.CollUtil;
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.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 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.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@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);
      }
   }

   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;
      }
   }
}
