package com.bonus.core.order.android.business.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.core.allocation.api.AllocStallApi;
import com.bonus.core.allocation.canteen.model.AllocStall;
import com.bonus.core.customer.constants.OrderTypeEnum;
import com.bonus.core.order.android.business.OrderDishesAndroidBusiness;
import com.bonus.core.order.common.model.OrderInfo;
import com.bonus.core.order.custom.OrderCustomBusiness;
import com.bonus.core.order.utils.LeNumUtil;
import com.bonus.core.supermarket.api.SupermarketApi;
import com.bonus.core.supermarket.model.SupermarketInfo;
import org.redisson.api.RLock;
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.time.LocalTime;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrderDishesAndroidBusinessImpl implements OrderDishesAndroidBusiness {
   private static final Logger log = LoggerFactory.getLogger(OrderDishesAndroidBusinessImpl.class);

   @Autowired
   @Lazy
   protected OrderCustomBusiness orderCustomBusiness;

   @Autowired
   @Lazy
   protected AllocStallApi allocStallApi;
   @Autowired
   @Lazy
   protected SupermarketApi supermarketApi;
   @Lazy
   @Autowired
   protected LeNumUtil numUtil;

   public void initOrderMealCode(OrderInfo orderInfo) {
      if (!CharSequenceUtil.isNotEmpty(orderInfo.getMealCode())) {
         String mealCode = this.createMealCode(orderInfo.getOrderType(), orderInfo.getCanteenId(), orderInfo.getStallId(), orderInfo.getOrderDate(), orderInfo.getMealtimeType());
         orderInfo.setMealCode(mealCode);
      }
   }

   public String createMealCode(Integer orderType, Long canteenId, Long stallId, LocalDate orderDate, Integer mealtimeType) {
      String mealCode = this.orderCustomBusiness.android().willCreateOrderMealCode(canteenId, stallId, orderDate, mealtimeType);
      if (CharSequenceUtil.isNotEmpty(mealCode)) {
         return this.orderCustomBusiness.android().didCreateOrderMealCode(canteenId, stallId, orderDate, mealtimeType, mealCode);
      } else {
         String prefix = null;
         if (OrderTypeEnum.isShopCategory(orderType)) {
            SupermarketInfo supermarketInfoById = this.supermarketApi.getSupermarketInfoById(canteenId);
            prefix = supermarketInfoById != null ? supermarketInfoById.getMealCode() : "";
         } else if (OrderTypeEnum.isCanteenCategory(orderType) && LeNumUtil.isValidId(stallId)) {
            AllocStall allocStall = this.allocStallApi.getAllocStall(stallId);
            if (allocStall != null) {
               prefix = OrderTypeEnum.isAndroidCategory(orderType) ? allocStall.getOffLineMealCodePrefix() : allocStall.getOnLineMealCodePrefix();
            }
         }

         mealCode = this.numUtil.getMealCode(prefix, canteenId, stallId, orderDate, mealtimeType);
         log.info("创建叫号码：{}", mealCode);
         return this.orderCustomBusiness.android().didCreateOrderMealCode(canteenId, stallId, orderDate, mealtimeType, mealCode);
      }
   }

}
