package com.bonus.core.allocation.api;

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.constant.LeConstants;
import com.bonus.core.allocation.canteen.model.AllocCanteen;
import com.bonus.core.allocation.canteen.model.AllocMealLine;
import com.bonus.core.allocation.canteen.service.AllocAreaService;
import com.bonus.core.allocation.canteen.service.AllocCanteenService;
import com.bonus.core.allocation.canteen.service.AllocMealLineService;
import com.bonus.core.allocation.canteen.vo.AllocAreaFullNameVO;
import com.bonus.core.menu.vo.AppletReserveCanteenVO;
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.Component;

import java.util.List;
import java.util.Set;

@Component
public class AllocCanteenApi {
   private static final Logger log = LoggerFactory.getLogger(AllocCanteenApi.class);
//   @Autowired
//   @Lazy
//   private AllocCanteenBusiness allocCanteenBusiness;
   @Autowired
   @Lazy
   private AllocAreaService allocAreaService;
   @Autowired
   @Lazy
   private AllocCanteenService allocCanteenService;
//   @Resource
//   private AllocCanteenMapper allocCanteenMapper;
//   @Resource
//   private AllocStallService allocStallService;
   @Autowired
   private AllocMealLineService allocMealLineService;
//   @Autowired
//   @Lazy
//   private MercMerchantApi mercMerchantApi;
//   @Autowired
//   @Lazy
//   private AllocStallMapper allocStallMapper;
//   @Autowired
//   @Lazy
//   private AllocAreaMapper allocAreaMapper;

   public List<AllocAreaFullNameVO> listAreaFullName(List<Long> areaIdList) {
      return this.allocAreaService.listAreaFullName(areaIdList);
   }
//
//   public List<Long> getAllChildrenId(Long areaId) {
//      return this.allocAreaService.getAllChildrenId(areaId);
//   }
//
//   public Long getAreaIdByName(String areaName) {
//      AllocArea allocArea = this.allocAreaMapper.getAreaIdByName(areaName);
//      return allocArea == null ? null : allocArea.getAreaId();
//   }
//
//   public AllocArea getAndCreatDefaultArea() {
//      AllocArea defaultArea = (AllocArea)this.allocAreaService.getOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocArea.class).eq(AllocArea::getAreaNum, "99")).last(" limit 1 "));
//      if (ObjectUtil.isNotNull(defaultArea)) {
//         return defaultArea;
//      } else {
//         AllocAreaAddDTO areaDTO = new AllocAreaAddDTO();
//         areaDTO.setAreaName("默认区域");
//         areaDTO.setAreaNum("99");
//         areaDTO.setSuperId(TenantContextHolder.getTenantId());
//         this.allocAreaService.addArea(areaDTO);
//         return (AllocArea)this.allocAreaService.getOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocArea.class).eq(AllocArea::getAreaNum, "99")).last(" limit 1 "));
//      }
//   }
//
//   public AllocCanteen getAllocCanteen(Long canteenId) {
//      return this.allocCanteenService.getAllocCanteenCache(canteenId);
//   }
//
//   public void checkCanteenBusinessState(Long canteenId, LocalTime checkTime) {
//      if (!LeNumUtil.isValidId(canteenId)) {
//         log.warn("食堂id不合法,无法校验营业状态");
//      } else {
//         AllocCanteen allocCanteen = this.getAllocCanteen(canteenId);
//         if (!LeConstants.COMMON_YES.equals(allocCanteen.getIfDel()) && AllocBusinessStateEnum.OPEN.getKey().equals(allocCanteen.getBusinessState())) {
//            if (!ObjectUtil.isNull(checkTime)) {
//               if (!ObjectUtil.isNull(allocCanteen.getStartBusinessTime()) && !ObjectUtil.isNull(allocCanteen.getEndBusinessTime())) {
//                  if (allocCanteen.getStartBusinessTime().isAfter(allocCanteen.getEndBusinessTime())) {
//                     if (checkTime.isBefore(allocCanteen.getStartBusinessTime()) && checkTime.isAfter(allocCanteen.getEndBusinessTime())) {
//                        throw new LeException(I18n.getMessage("alloc_canteen_is_rest", new Object[]{allocCanteen.getCanteenName()}));
//                     }
//                  } else if (checkTime.isBefore(allocCanteen.getStartBusinessTime()) || checkTime.isAfter(allocCanteen.getEndBusinessTime())) {
//                     throw new LeException(I18n.getMessage("alloc_canteen_is_rest", new Object[]{allocCanteen.getCanteenName()}));
//                  }
//
//               }
//            }
//         } else {
//            throw new LeException(I18n.getMessage("alloc_canteen_is_rest", new Object[]{allocCanteen.getCanteenName()}));
//         }
//      }
//   }
//
//   public AllocStall getAllocStall(Long stallId) {
//      return this.allocStallService.getAllocStallCache(stallId);
//   }
//
//   public List<AllocCanteen> listAllCanteen(Boolean containMarketFlag) {
//      LambdaQueryWrapper<AllocCanteen> canteenWrapper = (LambdaQueryWrapper)Wrappers.lambdaQuery(AllocCanteen.class).eq(AllocCanteen::getIfDel, LeConstants.COMMON_NO);
//      if (!Boolean.TRUE.equals(containMarketFlag)) {
//         canteenWrapper.eq(AllocCanteen::getCanteenType, AllocCanteenTypeEnum.CANTEEN.getKey());
//      }
//
//      return this.allocCanteenService.list(canteenWrapper);
//   }
//
//   public List<AllocCanteen> queryCanteenByCanteenNameList(List<String> canteenNameList) {
//      return this.allocCanteenService.list((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocCanteen.class).in(AllocCanteen::getCanteenName, canteenNameList)).eq(AllocCanteen::getIfDel, LeConstants.COMMON_NO));
//   }
//
//   public DeductCanteenStallVO queryCanteenStallByName(String canteenName, String stallName) {
//      return this.allocCanteenService.queryCanteenStallByName(canteenName, stallName);
//   }

   public List<AllocCanteen> queryCanteenByCanteenIdList(Set<Long> canteenIdList) {
      return this.allocCanteenService.list(Wrappers.lambdaQuery(AllocCanteen.class)
              .in(AllocCanteen::getCanteenId, canteenIdList).eq(AllocCanteen::getIfDel, LeConstants.COMMON_NO));
   }

   public List<AllocMealLine> queryMealLineByMealLineIdList(Set<Long> mealLineIds) {
      return this.allocMealLineService.list(Wrappers.lambdaQuery(AllocMealLine.class)
              .in(AllocMealLine::getMealLineId, mealLineIds).eq(AllocMealLine::getIfDel, LeConstants.COMMON_NO));
   }
//
//   public AllocMealLine queryFirstMealLine() {
//      return (AllocMealLine)this.allocMealLineService.getBaseMapper().selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocMealLine.class).orderByDesc(AllocMealLine::getCrtime)).last("limit 1"));
//   }
//
//   public String getCanteenNameByIdSet(Set<Long> canteenIdSet) {
//      return this.allocCanteenMapper.getCanteenNameByIdSet(canteenIdSet);
//   }
//
//   public boolean checkCanteenIfReserve(Long canteenId, Long stallId) {
//      return this.allocCanteenService.checkCanteenIfReserve(canteenId, stallId);
//   }
//
   public void checkAndDelReserveCanteen(List<AppletReserveCanteenVO> reserveCanteenList) {
      this.allocCanteenService.checkAndDelReserveCanteen(reserveCanteenList);
   }
//
//   public boolean checkCanteenBookExist() {
//      long bookNum = this.allocCanteenService.count((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocCanteen.class).eq(AllocCanteen::getIfBook, LeConstants.COMMON_YES)).eq(AllocCanteen::getIfDel, LeConstants.COMMON_NO));
//      return bookNum > 0L;
//   }
//
//   public boolean checkCanteenReserveExist() {
//      long reserveNum = this.allocCanteenService.count((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocCanteen.class).eq(AllocCanteen::getIfReserve, LeConstants.COMMON_YES)).eq(AllocCanteen::getIfDel, LeConstants.COMMON_NO));
//      return reserveNum > 0L;
//   }

   public List<Long> getCanteenIdListByAreaIdList(List<Long> areaIdList) {
      return this.allocCanteenService.getCanteenIdListByAreaIdList(areaIdList);
   }

//   public AllocCanteenDeliveryModel queryThirdCanteenDelivery(Long canteenId) {
//      return this.allocCanteenService.queryThirdCanteenDelivery(canteenId);
//   }
//
//   public List<CanteenQueryVO> queryAllThirdCanteen(CanteenQueryDTO queryDTO) {
//      return this.allocCanteenService.queryAllThirdCanteen(queryDTO);
//   }
//
//   public Long saveMarketCanteen(MarketCanteenSaveDTO marketSaveDTO) {
//      return this.allocCanteenService.saveMarketCanteen(marketSaveDTO);
//   }
//
//   public List<AllocCanteenAppVO> listAuthAppCanteen() {
//      return this.allocCanteenService.listAuthAppCanteen(new AllocCanteenStallParam());
//   }
//
//   public Long saveThirdCanteen(String thirdCanteenId, String canteenName) {
//      return this.allocCanteenService.saveThirdCanteen(thirdCanteenId, canteenName);
//   }
//
//   public void updateThirdCanteen(Long canteenId, String canteenName) {
//      this.allocCanteenService.modifyCanteenName(canteenId, canteenName);
//   }
//
//   public void removeThirdCanteenById(Long canteenId) {
//      AllocCanteen allocCanteen = (AllocCanteen)this.allocCanteenService.getOne((Wrapper)Wrappers.lambdaQuery(AllocCanteen.class).eq(AllocCanteen::getCanteenId, canteenId));
//      if (ObjectUtil.isNull(allocCanteen)) {
//         throw new LeException("食堂数据已经不存在");
//      } else {
//         this.allocCanteenBusiness.removeCanteen(canteenId);
//      }
//   }
//
//   public MenuaiStallModel getStallDetail(Long stallId) {
//      return this.allocCanteenMapper.selectStallDetail(stallId);
//   }
//
//   public Long getcanteenIdByThirdId(String thirdCanteenId) {
//      AllocCanteen allocCanteen = this.allocCanteenService.getAllocCanteenByThirdId(thirdCanteenId);
//      return allocCanteen != null ? allocCanteen.getCanteenId() : null;
//   }
//
//   public List<AllocCanteenTreeVO> listTenantCanteen() {
//      return this.allocCanteenService.listTenantCanteen();
//   }
//
//   public Long getStallIdByThirdId(String thirdStallId) {
//      AllocStall stall = (AllocStall)this.allocStallService.getOne((Wrapper)Wrappers.lambdaQuery(AllocStall.class).eq(AllocStall::getThirdStallId, thirdStallId));
//      return ObjectUtil.isNotNull(stall) ? stall.getStallId() : null;
//   }
//
//   public Map<Long, String> getAreaNameMap(List<Long> areaIdList) {
//      Map<Long, String> areaNameMap = Maps.newHashMap();
//      areaNameMap.put(TenantContextHolder.getTenantId(), this.mercMerchantApi.getMerchantName());
//      if (ObjectUtil.isEmpty(areaIdList)) {
//         return areaNameMap;
//      } else {
//         List<AllocArea> areaList = this.allocAreaService.list((Wrapper)Wrappers.lambdaQuery(AllocArea.class).select(new SFunction[]{AllocArea::getAreaId, AllocArea::getAreaName}).in(AllocArea::getAreaId, areaIdList));
//         areaList.forEach((area) -> {
//            areaNameMap.put(area.getAreaId(), area.getAreaName());
//         });
//         return areaNameMap;
//      }
//   }
//
//   public List<AllocCanteenVO> listCanteenByUserRole() {
//      return this.allocCanteenMapper.listAuthCanteen();
//   }
//
//   public List<AllocStallVO> listStallByUserRole() {
//      return this.allocStallMapper.listAuthStallByUserRole();
//   }
//
//   public List<AllocAreaVO> listAreaByUserRole(MgrUserAuthPO mgrUserAuthPO) {
//      return this.allocAreaMapper.listAreaByUserRole(mgrUserAuthPO);
//   }
}
