package com.bonus.core.order.android.dto.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.constant.LeConstants;
import com.bonus.core.allocation.api.AllocStallApi;
import com.bonus.core.allocation.canteen.model.AllocStall;
import com.bonus.core.common.redis.RedisUtil;
import com.bonus.core.common.utils.LeCodeUtil;
import com.bonus.core.customer.vo.CustPayVO;
import com.bonus.core.order.android.dto.business.OrderDishesAndroidBusiness;
import com.bonus.core.order.android.vo.OrderCallListVO;
import com.bonus.core.order.android.vo.OrderListAndroidVO;
import com.bonus.core.order.client.OrderModuleClient;
import com.bonus.core.order.client.po.CustPayInfoQueryParam;
import com.bonus.core.order.common.constants.DishesStateEnum;
import com.bonus.core.order.common.constants.OrderCacheConstants;
import com.bonus.core.order.common.constants.OrderStateEnum;
import com.bonus.core.order.common.dto.OrderSearchParam;
import com.bonus.core.order.common.model.OrderInfo;
import com.bonus.core.order.common.query.OrderQueryHelper;
import com.bonus.core.order.common.service.OrderDetailService;
import com.bonus.core.order.common.service.OrderInfoService;
import com.bonus.core.order.common.vo.OrderIdDateVO;
import com.bonus.core.order.custom.OrderCustomBusiness;
import com.bonus.core.order.deprecated.constants.OrderStateOldEnum;
import com.bonus.core.order.deprecated.dto.OrdLeCodeDTO;
import com.bonus.core.order.mq.OrderMessageSend;
import com.bonus.core.order.utils.LeNumUtil;
import com.bonus.core.order.web.dto.OrderUserPermissionDTO;
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 OrderInfoService orderInfoService;
   @Autowired
   @Lazy
   protected OrderDetailService orderDetailService;
//   @Autowired
//   @Lazy
//   protected OrderDeliveryService orderDeliveryService;
//   @Autowired
//   @Lazy
//   protected OrderWebBusiness orderWebBusiness;
//   @Autowired
//   @Lazy
//   protected OrderInfoAndroidBusiness orderInfoAndroidBusiness;
//   @Autowired
//   @Lazy
//   protected OrderPlaceBusiness orderPlaceBusiness;
   @Autowired
   @Lazy
   protected OrderCustomBusiness orderCustomBusiness;
   @Autowired
   @Lazy
   protected OrderModuleClient orderModuleClient;
   @Autowired
   @Lazy
   protected AllocStallApi allocStallApi;
//   @Autowired
//   @Lazy
//   protected SupermarketApi supermarketApi;
//   @Autowired
//   @Lazy
//   protected AccInfoServiceV3Api accInfoServiceV3Api;
//   @Autowired
//   @Lazy
//   protected DeviceApi deviceApi;
//   @Lazy
//   @Autowired
//   protected LeNumUtil numUtil;
//   @Lazy
//   @Autowired
//   protected MarketApi marketApi;

//   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);
//      }
//   }
//
//   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 OrderAndroidSignInVO orderSignIn(OrderSignInAndroidDTO param) {
//      this.addNxWithOrderId(param.getOrderId());
//
//      OrderAndroidSignInVO var7;
//      try {
//         OrderInfo orderInfo = this.orderInfoService.checkAndGetOrderInfo(param.getOrderId());
//         OrderAndroidSignInVO customVO = this.orderCustomBusiness.android().willSignIn(orderInfo, param);
//         if (customVO != null) {
//            OrderAndroidSignInVO var11 = this.orderCustomBusiness.android().didSignIn(orderInfo, param, customVO);
//            return var11;
//         }
//
//         if (!LeNumUtil.isValidId(orderInfo.getStallId())) {
//            throw new LeException(I18n.getMessage("order.mac.no-stall", new Object[0]));
//         }
//
//         AllocStall allocStall = this.allocStallApi.getAllocStall(orderInfo.getStallId());
//         if (allocStall == null) {
//            throw new LeException(I18n.getMessage("order.mac.no-stall", new Object[0]));
//         }
//
//         if (!LeConstants.COMMON_YES.equals(allocStall.getIfUseCallNum())) {
//            throw new LeException(I18n.getMessage("order.mac.stall-not-use-call", new Object[0]));
//         }
//
//         if (!OrderStateEnum.isPlaceState(orderInfo.getOrderState())) {
//            throw new LeException(OrderTips.orderStateWrong(orderInfo.getOrderState()));
//         }
//
//         Integer targetDishesState = DishesStateEnum.UN_MAKE.getKey().equals(orderInfo.getDishesState()) ? DishesStateEnum.MAKING.getKey() : orderInfo.getDishesState();
//         this.doUpdateDishesState(orderInfo, OrderDishesUpdateDTO.of(orderInfo.getOrderId(), targetDishesState));
//         OrderAndroidSignInVO result = OrderAndroidSignInVO.of(orderInfo.getMealCode());
//         result = this.orderCustomBusiness.android().didSignIn(orderInfo, param, result);
//         var7 = result;
//      } finally {
//         this.removeNxWithOrderId(param.getOrderId());
//      }
//
//      return var7;
//   }
//
//   public void orderUpdateDishesState(OrderDishesUpdateDTO dishesUpdateDTO) {
//      if (CharSequenceUtil.isNotEmpty(dishesUpdateDTO.getQrCode()) && LeCodeUtil.isLeCode(dishesUpdateDTO.getQrCode())) {
//         dishesUpdateDTO.setOrderId(LeCodeUtil.getPlatformIdThenExpire(dishesUpdateDTO.getQrCode()));
//      }
//
//      JavaxValidateUtils.validate(dishesUpdateDTO);
//      this.addNxWithOrderId(dishesUpdateDTO.getOrderId());
//
//      try {
//         OrderInfo orderInfo = this.orderInfoService.checkAndGetOrderInfo(dishesUpdateDTO.getOrderId());
//         if (!OrderStateEnum.isPlaceState(orderInfo.getOrderState())) {
//            throw new LeException(OrderTips.orderStateWrong(orderInfo.getOrderState()));
//         }
//
//         this.doUpdateDishesState(orderInfo, dishesUpdateDTO);
//      } finally {
//         this.removeNxWithOrderId(dishesUpdateDTO.getOrderId());
//      }
//
//   }
//
//   public void orderUpdateDishesState(List<Long> orderIds, Integer dishesState) {
//      if (!CollUtil.isEmpty(orderIds)) {
//         Iterator var3 = CollUtil.newHashSet(orderIds).iterator();
//
//         while(var3.hasNext()) {
//            Long orderId = (Long)var3.next();
//            OrderDishesUpdateDTO dishesUpdateDTO = OrderDishesUpdateDTO.of(orderId, dishesState);
//            log.info("[批量]订单制作状态更新：{}", dishesUpdateDTO);
//            this.orderUpdateDishesState(dishesUpdateDTO);
//         }
//
//      }
//   }

   public void didUpdateDishesState(Long orderId, Integer dishesStateBeforeUpdated) {
      OrderInfo orderInfo = this.orderInfoService.getById(orderId);
      CustPayVO custInfo = this.orderModuleClient.custPayInfoQuery(CustPayInfoQueryParam.ofCustId(orderInfo.getCustId()));
      this.updateCallCacheIfNeed(OrderCallListVO.of(orderInfo, custInfo != null ? custInfo.getCustName() : null));
      if (!ObjectUtil.equal(dishesStateBeforeUpdated, orderInfo.getDishesState())) {
         OrderMessageSend.orderV3DishesStateUpdated(orderInfo, dishesStateBeforeUpdated);
      }

      this.orderCustomBusiness.android().didUpdateOrderDishesStateV2(orderInfo, dishesStateBeforeUpdated);
   }

//   public OrderTakeAndroidVO custTakeMeal(OrderTakeAndroidDTO takeAndroidDTO) {
//      OrderTakeAndroidVO customVO = this.orderCustomBusiness.android().willCustTakeMeal(takeAndroidDTO);
//      if (customVO != null) {
//         customVO = this.orderCustomBusiness.android().didCustTakeMeal(takeAndroidDTO, customVO);
//         return customVO;
//      } else {
//         CustPayVO custPayInfo = this.orderInfoAndroidBusiness.checkCustPayInfo(takeAndroidDTO.getNuClearMode(), takeAndroidDTO.getCustId(), takeAndroidDTO.getOpenId(), takeAndroidDTO.getSerialNum(), takeAndroidDTO.getQrCode());
//         Long orderId = null;
//         if (LeConstants.COMMON_YES.equals(takeAndroidDTO.getIfTakeImmediately()) && NuclearModeEnum.isQrCode(takeAndroidDTO.getNuClearMode()) && LeCodeUtil.isLeCode(takeAndroidDTO.getQrCode())) {
//            OrdLeCodeDTO ordLeCodeDTO = LeCodeUtil.readLeCode(takeAndroidDTO.getQrCode());
//            if (ordLeCodeDTO.ifTake()) {
//               orderId = ordLeCodeDTO.getP();
//            }
//         }
//
//         AccInfoDetailVO accInfoDetail = this.accInfoServiceV3Api.getAccInfoDetail(custPayInfo.getCustId());
//         boolean isSupermarketDevice = false;
//         DeviceInfoVO deviceInfoVO = this.deviceApi.getDeviceInfoVoBySnCacheable(takeAndroidDTO.getMachineSn());
//         List<String> devicePermissionCodes = ListUtil.empty();
//         if (deviceInfoVO != null && CollUtil.isNotEmpty(deviceInfoVO.getBindList())) {
//            devicePermissionCodes = (List)deviceInfoVO.getBindList().stream().filter((s) -> {
//               return LeNumUtil.isValidId(s.getCanteenId());
//            }).map((s) -> {
//               Long var10000 = s.getCanteenId();
//               return "" + var10000 + "_" + (LeNumUtil.isValidId(s.getStallId()) ? s.getStallId().toString() : "-1");
//            }).collect(Collectors.toList());
//            isSupermarketDevice = SourceTypeEnum.DUAL_SCREEN_CASH_REGISTER.getKey().equals(deviceInfoVO.getDeviceType());
//         }
//
//         OrderSearchParam orderListAndroidQueryDTO = new OrderSearchParam();
//         orderListAndroidQueryDTO.setCustId(custPayInfo.getCustId());
//         orderListAndroidQueryDTO.setStartDate(LocalDate.now());
//         orderListAndroidQueryDTO.setEndDate(LocalDate.now());
//         orderListAndroidQueryDTO.setOrderStateList(OrderStateEnum.validStateList());
//         orderListAndroidQueryDTO.setExistsRefund(LeConstants.COMMON_NO);
//         orderListAndroidQueryDTO.setDeliveryTypeList(CollUtil.toList(new Integer[]{DeliveryTypeEnum.SELF_TAKE.getKey()}));
//         orderListAndroidQueryDTO.setOrderIdList(LeNumUtil.isValidId(orderId) ? CollUtil.toList(new Long[]{orderId}) : null);
//         if (isSupermarketDevice) {
//            orderListAndroidQueryDTO.setOrderTypeList(CollUtil.toList(new Integer[]{OrderTypeEnum.SHOP.getKey(), OrderTypeEnum.RESERVE_SHOP.getKey()}));
//         } else {
//            AllocMealtimeModel nowMealtime = this.orderModuleClient.mealtimeCheckNow(MealtimeCheckNowParam.of(deviceInfoVO != null ? deviceInfoVO.getFirstCanteenId() : null, deviceInfoVO != null ? deviceInfoVO.getFirstShopstallId() : null));
//            orderListAndroidQueryDTO.setMealtimeTypeList(CollUtil.toList(new Integer[]{nowMealtime.getMealtimeType()}));
//            orderListAndroidQueryDTO.setOrderTypeList(CollUtil.toList(new Integer[]{OrderTypeEnum.CURR_MEAL.getKey(), OrderTypeEnum.RESERVE_MEAL.getKey(), OrderTypeEnum.BOOK_MEAL.getKey(), OrderTypeEnum.BOOK_MEAL_3DAY.getKey(), OrderTypeEnum.BOOK_MEAL_7DAY.getKey()}));
//         }
//
//         OrderTakeAndroidVO vo = OrderTakeAndroidVO.of(custPayInfo, accInfoDetail);
//         List<OrderIdDateVO> orderIdDatePage = this.orderInfoService.queryOrderId(orderListAndroidQueryDTO, (OrderUserPermissionDTO)null);
//         List<OrderListAndroidVO> orderInfoList = this.orderInfoService.listOrderForAndroid((List)orderIdDatePage.stream().map(OrderIdDateVO::getId).collect(Collectors.toList()), (List)orderIdDatePage.stream().map(OrderIdDateVO::getOrderDate).collect(Collectors.toList()));
//         this.orderInfoAndroidBusiness.fillOrderDetail(orderInfoList, true);
//         Iterator var13 = orderInfoList.iterator();
//
//         while(var13.hasNext()) {
//            OrderListAndroidVO orderInfo = (OrderListAndroidVO)var13.next();
//            if (this.isIncludePermission(devicePermissionCodes, orderInfo.getPermissionCode())) {
//               if (DishesStateEnum.ALREADY_TAKE.getKey().equals(orderInfo.getDishesState())) {
//                  vo.getAlreadyTakeOrderList().add(orderInfo);
//               } else {
//                  vo.getNeedTakeOrderList().add(orderInfo);
//               }
//            }
//         }
//
//         if (CollUtil.isNotEmpty(vo.getNeedTakeOrderList())) {
//            RulePriceDTO rulePriceDTO = this.packageRulePriceDTO(vo.getNeedTakeOrderList(), deviceInfoVO);
//            this.marketApi.ruleMealCompute(rulePriceDTO);
//            if (LeConstants.COMMON_YES.equals(takeAndroidDTO.getIfTakeImmediately())) {
//               Iterator var19 = vo.getNeedTakeOrderList().iterator();
//
//               while(var19.hasNext()) {
//                  OrderListAndroidVO orderListAndroidVO = (OrderListAndroidVO)var19.next();
//                  this.orderUpdateDishesState(OrderDishesUpdateDTO.of(orderListAndroidVO.getOrderId(), DishesStateEnum.ALREADY_TAKE.getKey()));
//               }
//            }
//         }
//
//         vo = this.orderCustomBusiness.android().didCustTakeMeal(takeAndroidDTO, vo);
//         return vo;
//      }
//   }
//
//   public PageVO<OrderCallListVO> queryOrderCallPage(PageDTO pageDTO, OrderCallQueryDTO queryDTO) {
//      LogUtil.info("[叫号]查询叫号列表", queryDTO);
//      if (CharSequenceUtil.isNotBlank(queryDTO.getMachineSn()) && !LeNumUtil.isValidId(queryDTO.getStallId())) {
//         log.info("[叫号]未指定档口，根据设备sn查询档口id");
//         DeviceInfoVO deviceInfoVO = this.deviceApi.getDeviceInfoVoBySnCacheable(queryDTO.getMachineSn());
//         if (deviceInfoVO != null) {
//            queryDTO.setStallId(deviceInfoVO.getFirstShopstallId());
//         }
//      }
//
//      if (!LeNumUtil.isValidId(queryDTO.getMealtimeType()) && LeNumUtil.isValidId(queryDTO.getStallId())) {
//         log.info("[叫号]未指定餐次，根据档口id查询餐次");
//         AllocMealtimeModel assignTimeMealtime = this.orderModuleClient.mealtimeQuery(MealtimeQueryParam.ofAssignTime(LocalTime.now(), (Long)null, queryDTO.getStallId()));
//         if (assignTimeMealtime != null) {
//            queryDTO.setMealtimeType(assignTimeMealtime.getMealtimeType());
//         }
//      }
//
//      if (LeNumUtil.isValidId(queryDTO.getStallId()) && LeNumUtil.isValidId(queryDTO.getMealtimeType())) {
//         AllocStall allocStall = this.allocStallApi.getAllocStall(queryDTO.getStallId());
//         if (allocStall != null && LeConstants.COMMON_YES.equals(allocStall.getIfUseCallNum())) {
//            List<OrderCallListVO> orderCallListVOS = this.readCallCache(queryDTO.getStallId(), queryDTO.getDishesState(), queryDTO.getMealtimeType());
//            PageVO<OrderCallListVO> pageVO = PageVO.of(pageDTO, orderCallListVOS);
//            this.orderCustomBusiness.android().didQueryOrderCallPage(queryDTO, pageVO);
//            return pageVO;
//         } else {
//            log.warn("[叫号]档口不存在或未开启叫号，返回空数据");
//            return PageVO.empty();
//         }
//      } else {
//         log.warn("[叫号]档口或餐次为空，返回空数据");
//         return PageVO.empty();
//      }
//   }
//
//   public void fillDetailMakingPartComplete(Long orderId, Integer dishesState, List<OrderDetailAndroidVO> orderDetailList) {
//      if (DishesStateEnum.MAKING.getKey().equals(dishesState)) {
//         if (CollUtil.isNotEmpty(orderDetailList)) {
//            Object cacheObj = RedisUtil.getObj(OrderCacheConstants.orderMakingPartCompleteCacheKey(orderId));
//            List<OrderDishesUpdateDTO.DetailDishesMakeCompleteDTO> makingComplteList = cacheObj instanceof List ? (List)cacheObj : ListUtil.empty();
//            Map<Long, Integer> detailMakeCompleteMap = (Map)makingComplteList.stream().collect(Collectors.toMap(OrderDishesUpdateDTO.DetailDishesMakeCompleteDTO::getDetailId, OrderDishesUpdateDTO.DetailDishesMakeCompleteDTO::getCompleteMaking, (k1, k2) -> {
//               return k1;
//            }));
//            orderDetailList.forEach((s) -> {
//               s.setMakingComplete((Integer)detailMakeCompleteMap.getOrDefault(s.getDetailId(), (Object)null));
//            });
//         }
//      } else if (DishesStateEnum.UN_MAKE.getKey().equals(dishesState)) {
//         orderDetailList.forEach((s) -> {
//            s.setMakingComplete(LeConstants.COMMON_NO);
//         });
//      } else {
//         orderDetailList.forEach((s) -> {
//            s.setMakingComplete(LeConstants.COMMON_YES);
//         });
//      }
//
//   }
//
//   public void updateCallOrd(OrdCallUpdateDTO ordCallUpdateDTO) {
//      DeviceInfoVO deviceInfoVO = this.deviceApi.getDeviceInfoVoBySnCacheable(ordCallUpdateDTO.getMachineSn());
//      if (SourceTypeEnum.BUFFET.getKey().equals(deviceInfoVO.getDeviceType()) && OrderStateOldEnum.MAKEING.getKey().equals(ordCallUpdateDTO.getOrdState())) {
//         this.orderSignIn((new OrderSignInAndroidDTO()).setOrderId(ordCallUpdateDTO.getOrdId()));
//      } else {
//         OrderDishesUpdateDTO orderDishesUpdateDTO = new OrderDishesUpdateDTO();
//         orderDishesUpdateDTO.setOrderId(ordCallUpdateDTO.getOrdId());
//         if (OrderStateOldEnum.COMPLETE.getKey().equals(ordCallUpdateDTO.getOrdState())) {
//            orderDishesUpdateDTO.setDishesState(DishesStateEnum.ALREADY_TAKE.getKey());
//         } else if (OrderStateOldEnum.WAITTAKE.getKey().equals(ordCallUpdateDTO.getOrdState())) {
//            orderDishesUpdateDTO.setDishesState(DishesStateEnum.WAIT_TAKE.getKey());
//         } else if (OrderStateOldEnum.MAKEING.getKey().equals(ordCallUpdateDTO.getOrdState())) {
//            orderDishesUpdateDTO.setDishesState(DishesStateEnum.MAKING.getKey());
//         } else {
//            orderDishesUpdateDTO.setDishesState(DishesStateEnum.MAKING.getKey());
//         }
//
//         this.orderUpdateDishesState(orderDishesUpdateDTO);
//      }
//   }
//
//   public void doUpdateDishesState(OrderInfo orderInfo, OrderDishesUpdateDTO dishesUpdateDTO) {
//      Integer dishesStateBeforeUpdated = orderInfo.getDishesState();
//      if (!this.orderCustomBusiness.android().willUpdateOrderDishesStateV2(orderInfo, dishesUpdateDTO)) {
//         this.orderCustomBusiness.android().didUpdateOrderDishesStateV2(orderInfo, dishesStateBeforeUpdated);
//      }
//
//      if (!OrderStateEnum.isPlaceState(orderInfo.getOrderState())) {
//         throw new LeException(OrderTips.orderStateWrong(orderInfo.getOrderState()));
//      } else {
//         if (DishesStateEnum.ALREADY_TAKE.getKey().equals(dishesUpdateDTO.getDishesState())) {
//            this.orderInfoService.updateOrderFinishByOrderIds(CollUtil.toList(new Long[]{orderInfo.getOrderId()}), CollUtil.toList(new LocalDate[]{orderInfo.getOrderDate()}));
//         } else if (DishesStateEnum.WAIT_TAKE.getKey().equals(dishesUpdateDTO.getDishesState()) && dishesUpdateDTO.ifPartMakeComplete()) {
//            String cacheKey = OrderCacheConstants.orderMakingPartCompleteCacheKey(dishesUpdateDTO.getOrderId());
//            List<OrderDishesUpdateDTO.DetailDishesMakeCompleteDTO> cacheObj = CollUtil.isNotEmpty(dishesUpdateDTO.getDetailDishesMakeList()) ? dishesUpdateDTO.getDetailDishesMakeList() : CollUtil.newArrayList(new OrderDishesUpdateDTO.DetailDishesMakeCompleteDTO[0]);
//            RedisUtil.setObj(cacheKey, cacheObj);
//            this.orderInfoService.updateOrderState(orderInfo.getOrderId(), orderInfo.getOrderDate(), (Integer)null, (Integer)null, DishesStateEnum.MAKING.getKey(), (String)null);
//         } else {
//            this.orderInfoService.updateOrderState(orderInfo.getOrderId(), orderInfo.getOrderDate(), (Integer)null, (Integer)null, dishesUpdateDTO.getDishesState(), (String)null);
//         }
//
//         if (CharSequenceUtil.isNotEmpty(dishesUpdateDTO.getDeliveryCode())) {
//            this.orderDeliveryService.updateOrderDeliveryCode(orderInfo.getOrderId(), orderInfo.getOrderDate(), dishesUpdateDTO.getDeliveryCode());
//         }
//
//         this.didUpdateDishesState(orderInfo.getOrderId(), dishesStateBeforeUpdated);
//      }
//   }
//
//   protected boolean isIncludePermission(List<String> devicePermissionCodes, String orderPermissionCode) {
//      if (CollUtil.isEmpty(devicePermissionCodes)) {
//         return false;
//      } else {
//         String[] var10001 = orderPermissionCode.split("_");
//         return devicePermissionCodes.contains(var10001[0] + "_-1") ? true : devicePermissionCodes.contains(orderPermissionCode);
//      }
//   }
//
//   protected void addNxWithOrderId(Long orderId) {
//      String lockKey = OrderCacheConstants.orderHandleLockKey(OrderCacheConstants.orderCallHandleLockKey(orderId));
//      if (!RedisUtil.setNx(lockKey, LeConstants.COMMON_YES, 10)) {
//         log.info("[叫号处理中] 处理中 {} ", orderId);
//         throw new LeException(I18n.getMessage("order.mobile.order-processing", new Object[0]));
//      } else {
//         log.info("[叫号处理中] 加锁 {} {}", orderId, 10);
//      }
//   }
//
//   protected void removeNxWithOrderId(Long orderId) {
//      log.info("[叫号处理中] 移除锁定 {} ", orderId);
//      String lockKey = OrderCacheConstants.orderHandleLockKey(OrderCacheConstants.orderCallHandleLockKey(orderId));
//      RedisUtil.delete(lockKey);
//   }

   protected void updateCallCacheIfNeed(OrderCallListVO orderCallListVO) {
      if (orderCallListVO != null && LeNumUtil.isValidId(orderCallListVO.getStallId()) && !CharSequenceUtil.isEmpty(orderCallListVO.getMealCode())) {
         RLock lock = RedisUtil.getLock(OrderCacheConstants.orderCallCacheLockKey(orderCallListVO.getStallId(), orderCallListVO.getMealtimeType()));
         lock.lock(10L, TimeUnit.SECONDS);

         try {
            Object obj = RedisUtil.getObj(OrderCacheConstants.orderCallCacheKey(orderCallListVO.getStallId(), orderCallListVO.getMealtimeType()));
            Map orderCallMap;
            if (obj instanceof Map) {
               orderCallMap = (Map)obj;
            } else {
               orderCallMap = this.queryOrderCallMap(orderCallListVO.getStallId(), orderCallListVO.getMealtimeType());
            }

            if (!OrderStateOldEnum.MAKEING.getKey().equals(orderCallListVO.getOrdState()) && !OrderStateOldEnum.WAITTAKE.getKey().equals(orderCallListVO.getOrdState())) {
               orderCallMap.remove(orderCallListVO.getOrderId().toString());
            } else {
               orderCallMap.put(orderCallListVO.getOrderId().toString(), orderCallListVO);
            }

            RedisUtil.setObj(OrderCacheConstants.orderCallCacheKey(orderCallListVO.getStallId(), orderCallListVO.getMealtimeType()), orderCallMap, OrderCacheConstants.CALL_CACHE_SECONDS);
         } finally {
            try {
               if (lock.isHeldByCurrentThread() && lock.isLocked()) {
                  lock.unlock();
               }
            } catch (Exception var10) {
               log.error("解锁异常", var10);
            }

         }

      }
   }
//
//   protected List<OrderCallListVO> readCallCache(Long stallId, Integer dishesState, Integer mealtimeType) {
//      log.info("[叫号查询]读取缓存 {} {} {}", new Object[]{stallId, dishesState, mealtimeType});
//      Object obj = RedisUtil.getObj(OrderCacheConstants.orderCallCacheKey(stallId, mealtimeType));
//      if (obj instanceof Map<String, OrderCallListVO> orderCallMap) {
//         log.info("[叫号查询]从缓存读取 {} 个", CollUtil.size(orderCallMap.values()));
//      } else {
//         RLock lock = RedisUtil.getLock(OrderCacheConstants.orderCallCacheLockKey(stallId, mealtimeType));
//         lock.lock(10L, TimeUnit.SECONDS);
//
//         try {
//            orderCallMap = this.queryOrderCallMap(stallId, mealtimeType);
//            log.info("[叫号查询]从数据库读取 {} 个", CollUtil.size(orderCallMap.values()));
//            RedisUtil.setObj(OrderCacheConstants.orderCallCacheKey(stallId, mealtimeType), orderCallMap, OrderCacheConstants.CALL_CACHE_SECONDS);
//         } finally {
//            try {
//               if (lock.isHeldByCurrentThread() && lock.isLocked()) {
//                  lock.unlock();
//               }
//            } catch (Exception var13) {
//               log.error("解锁异常", var13);
//            }
//
//         }
//      }
//
//      return (List)orderCallMap.values().stream().filter((s) -> {
//         return ObjectUtil.equal(dishesState, s.getDishesState());
//      }).sorted(Comparator.comparing(OrderCallListVO::getMealCode)).collect(Collectors.toList());
//   }

   protected Map<String, OrderCallListVO> queryOrderCallMap(Long stallId, Integer mealtimeType) {
      OrderSearchParam androidQueryDTO = new OrderSearchParam();
      androidQueryDTO.setStartDate(LocalDate.now());
      androidQueryDTO.setEndDate(LocalDate.now());
      androidQueryDTO.setOrderStateList(CollUtil.toList(new Integer[]{OrderStateEnum.PLACE.getKey()}));
      androidQueryDTO.setDishesStateList(CollUtil.toList(new Integer[]{DishesStateEnum.MAKING.getKey(), DishesStateEnum.WAIT_TAKE.getKey()}));
      androidQueryDTO.setMealtimeTypeList(CollUtil.toList(new Integer[]{mealtimeType}));
      androidQueryDTO.setStallIdList(CollUtil.toList(new Long[]{stallId}));
      if (LeNumUtil.isValidId(stallId)) {
         AllocStall allocStall = this.allocStallApi.getAllocStall(stallId);
         if (allocStall != null) {
            androidQueryDTO.setCanteenIdList(CollUtil.toList(new Long[]{allocStall.getCanteenId()}));
         }
      }

      OrderQueryHelper.initQueryParam(androidQueryDTO);
      List<OrderIdDateVO> orderIdDatePage = this.orderInfoService.queryOrderId(androidQueryDTO, (OrderUserPermissionDTO)null);
      List<OrderListAndroidVO> orderListAndroidVOS = this.orderInfoService.listOrderForAndroid((List)orderIdDatePage.stream().map(OrderIdDateVO::getId).collect(Collectors.toList()), (List)orderIdDatePage.stream().map(OrderIdDateVO::getOrderDate).collect(Collectors.toList()));
      return orderListAndroidVOS.stream().filter((s) -> {
         return !DishesStateEnum.ALREADY_TAKE.getKey().equals(s.getDishesState());
      }).map(OrderCallListVO::of).collect(Collectors.toMap((s) -> {
         return s.getOrderId().toString();
      }, Function.identity()));
   }
//
//   protected RulePriceDTO packageRulePriceDTO(List<OrderListAndroidVO> orderInfoList, DeviceInfoVO deviceInfoVO) {
//      RulePriceDTO rulePriceDTO = new RulePriceDTO();
//      rulePriceDTO.setTryFlag(LeConstants.COMMON_NO);
//      rulePriceDTO.setVerifyFlag(LeConstants.COMMON_YES);
//      List<RulePriceOrderDTO> ruleOrderList = (List)orderInfoList.stream().map((orderInfo) -> {
//         return (new RulePriceOrderDTO()).setOrderId(orderInfo.getOrderId()).setMacOrderId(orderInfo.getMacOrderId()).setCustId(LeNumUtil.isValidId(orderInfo.getCustId()) ? orderInfo.getCustId() : LeConstants.DATA_DEFAULT_LONG).setOrderType(orderInfo.getOrderType()).setMealtimeType(orderInfo.getMealtimeType()).setOrderDate(orderInfo.getOrderDate()).setOrderTime(orderInfo.getOrderTime()).setCanteenId(orderInfo.getCanteenId()).setStallId(orderInfo.getStallId()).setPayableAmount(orderInfo.calcNeedPayAmount()).setIfOnline(orderInfo.getIfOnline()).setDeviceSn(deviceInfoVO != null ? deviceInfoVO.getDeviceSn() : null);
//      }).collect(Collectors.toList());
//      rulePriceDTO.setOrders(ruleOrderList);
//      rulePriceDTO.setCustId(LeNumUtil.isValidId(((OrderListAndroidVO)orderInfoList.get(0)).getCustId()) ? ((OrderListAndroidVO)orderInfoList.get(0)).getCustId() : LeConstants.DATA_DEFAULT_LONG);
//      rulePriceDTO.setPayChannel(((OrderListAndroidVO)orderInfoList.get(0)).getPayChannel());
//      return rulePriceDTO;
//   }
}
