package com.bonus.canteen.core.order.api;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.marketing.coupon.model.MktCoupon;
import com.bonus.canteen.core.order.common.business.OrderPlaceBusiness;
import com.bonus.canteen.core.order.common.constants.DetailStateEnum;
import com.bonus.canteen.core.order.common.constants.DetailTypeEnum;
import com.bonus.canteen.core.order.common.constants.OrderAmountDetailTypeEnum;
import com.bonus.canteen.core.order.common.constants.OrderStateEnum;
import com.bonus.canteen.core.order.common.model.OrderAmountChange;
import com.bonus.canteen.core.order.common.model.OrderAmountDetail;
import com.bonus.canteen.core.order.common.model.OrderDelivery;
import com.bonus.canteen.core.order.common.model.OrderInfo;
import com.bonus.canteen.core.order.common.service.OrderDeliveryService;
import com.bonus.canteen.core.order.common.service.OrderDetailService;
import com.bonus.canteen.core.order.common.service.OrderInfoService;
import com.bonus.canteen.core.order.mq.OrderMessageSend;
import com.bonus.canteen.core.order.mq.po.OrderSavePO;
import com.bonus.canteen.core.pay.api.TradeRecordApi;
import com.bonus.canteen.core.pay.common.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.bonus.canteen.core.pay.common.constants.TradeTypeEnum;
import com.bonus.canteen.core.pay.common.model.TradeOrderRecharge;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.utils.id.Id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderAllocApi {
   private static final Logger log = LoggerFactory.getLogger(OrderAllocApi.class);
   @Lazy
   @Autowired
   private OrderDeliveryService orderDeliveryService;
   @Lazy
   @Autowired
   private OrderInfoService orderInfoService;
   @Lazy
   @Autowired
   private OrderDetailService orderDetailService;
   @Lazy
   @Autowired
   protected OrderPlaceBusiness orderPlaceBusiness;
   @Lazy
   @Autowired
   private TradeRecordApi tradeRecordApi;

   public boolean existOrderUnTake(String deliveryAddr) {
      Integer count = this.orderDeliveryService.countOrderUnTakeNum(deliveryAddr);
      return count > 0;
   }

   public boolean existOrderUnTake(Long canteenId, Long stallId) {
      long count = this.orderInfoService.existOrderUnTake(canteenId, stallId);
      return count > 0L;
   }

   public boolean existReserveOrderUnPrint(Long canteenId, Long stallId, Integer mealtimeType) {
      long count = this.orderInfoService.existReserveOrderUnPrint(canteenId, stallId, mealtimeType);
      return count > 0L;
   }

//   public void saveOrdAppInfoOfCoupon(MktCouponWriteOffDTO writeOffDTO, List<MktCoupon> couponList) {
//      if (!CollUtil.isEmpty(couponList)) {
//         Iterator var3 = couponList.iterator();
//
//         while(var3.hasNext()) {
//            MktCoupon coupon = (MktCoupon)var3.next();
//            OrderInfo orderInfo = new OrderInfo();
//            BeanUtils.copyProperties(writeOffDTO, orderInfo);
//            long orderId = Id.next();
//            orderInfo.setOrderId(orderId);
//            orderInfo.setMacOrderId(Id.nextString());
//            orderInfo.setMachineNum(writeOffDTO.getMchNum());
//            orderInfo.setOrderDate(writeOffDTO.getWriteOffDate());
//            orderInfo.setOrderTime(writeOffDTO.getOrdTime());
//            orderInfo.setCanteenId(writeOffDTO.getCanteenId());
//            orderInfo.setCanteenName(writeOffDTO.getCanteenName());
//            orderInfo.setStallId(writeOffDTO.getStallId());
//            orderInfo.setStallName(writeOffDTO.getStallName());
//            orderInfo.setPayableAmount(BigDecimal.valueOf((long)writeOffDTO.getAmount()));
//            orderInfo.setSourceType(writeOffDTO.getSourceType());
//            this.orderInfoService.initOrderCustInfo(CollUtil.toList(new OrderInfo[]{orderInfo}), coupon.getCustId());
//            orderInfo.setCouponId(coupon.getCouponNum().toString());
//            orderInfo.setCouponAmount(orderInfo.getPayableAmount());
//            orderInfo.setRealAmount(BigDecimal.ZERO);
//            orderInfo.setPayTime(LocalDateTime.now());
//            orderInfo.setPayType(PayTypeEnum.COUPON.getKey());
//            orderInfo.setPayState(PayStateEnum.PAY_SUCC.getKey());
//            orderInfo.setOrderState(OrderStateEnum.FINISH.getKey());
//            orderInfo.setOrderType(OrderTypeEnum.ANDROID.getKey());
//            OrderDetail detail = new OrderDetail();
//            detail.setDetailId(Id.next());
//            detail.setOrderId(orderId);
//            detail.setGoodsDishesId(0L);
//            detail.setGoodsDishesName("按键金额");
//            detail.setSalePrice(orderInfo.getPayableAmount());
//            detail.setPrefPrice(orderInfo.getPayableAmount());
//            detail.setPrice(orderInfo.getPayableAmount());
//            detail.setTotalAmount(orderInfo.getPayableAmount());
//            detail.setCalcTotalAmount(orderInfo.getPayableAmount());
//            detail.setRealAmount(orderInfo.getRealAmount());
//            detail.setQuantity(1);
//            detail.setSalesMode(LeConstants.COMMON_YES);
//            detail.setDetailType(DetailTypeEnum.KEYAMOUNT.getKey());
//            detail.setDetailState(DetailStateEnum.NORMAL.getKey());
//            Long tradeId = Id.next();
//            TradeRecord tradeRecord = new TradeRecord();
//            tradeRecord.setId(tradeId);
//            tradeRecord.setTradeType(TradeTypeEnum.CONSUME.getKey());
//            tradeRecord.setTradeState(PayStateEnum.PAY_SUCC.getKey());
//            tradeRecord.setAmount(orderInfo.getRealAmount());
//            tradeRecord.setCustId(orderInfo.getCustId());
//            tradeRecord.setPayType(PayTypeEnum.COUPON.getKey());
//            tradeRecord.setPayChannel(PayChannelEnum.COUPON.getKey());
//            tradeRecord.setOutTradeNo((String)null);
//            tradeRecord.setRemark(orderInfo.getRemark());
//            tradeRecord.setTradeResult(PayStateEnum.PAY_SUCC.getDesc());
//            tradeRecord.setSourceType(orderInfo.getSourceType());
//            tradeRecord.setCanteenId(orderInfo.getCanteenId());
//            TradeOrderRecharge tradeOrderRecharge = new TradeOrderRecharge(tradeId, orderInfo.getOrderId(), orderInfo.getMacOrderId(), (LocalDateTime)null, (LocalDateTime)null);
//            OrderAmountDetail orderAmountDetail = new OrderAmountDetail();
//            orderAmountDetail.setAmountDetailId(Id.next());
//            orderAmountDetail.setTradeChannelId(tradeId);
//            orderAmountDetail.setOrderId(orderInfo.getOrderId());
//            orderAmountDetail.setAmount(orderInfo.getRealAmount());
//            orderAmountDetail.setAmountDetailType(OrderAmountDetailTypeEnum.PAY.getKey());
//            orderAmountDetail.setPayType(PayTypeEnum.COUPON.getKey());
//            orderAmountDetail.setPayChannel(PayChannelEnum.COUPON.getKey());
//            OrderSavePO orderSavePO = OrderSavePO.byOrderInfo(CollUtil.toList(new OrderInfo[]{orderInfo}), CollUtil.toList(new OrderDetail[]{detail}), (List)null, CollUtil.toList(new OrderAmountDetail[]{orderAmountDetail}));
//            this.orderPlaceBusiness.save(orderSavePO, false, false);
//            this.tradeRecordApi.insertManualTradeRecord(tradeRecord, CollUtil.toList(new TradeOrderRecharge[]{tradeOrderRecharge}));
//            OrderMessageSend.orderV3Placed("", CollUtil.toList(new OrderInfo[]{this.orderInfoService.getById(orderInfo.getOrderId())}), this.orderDetailService.getOrderDetailList(orderInfo.getOrderId(), orderInfo.getOrderDate()), CollUtil.newArrayList(new OrderDelivery[0]), CollUtil.newArrayList(new OrderAmountChange[0]), CollUtil.toList(new OrderAmountDetail[]{orderAmountDetail}));
//         }
//
//      }
//   }
//
//   public List<OrderInfo> listBasicRoomDinningOrder(List<Long> roomIds, LocalDate orderDate, Integer mealtimeType) {
//      return this.orderInfoService.listBasicRoomWaitPlacedOrder(roomIds, orderDate, mealtimeType);
//   }
}
