package com.bonus.canteen.core.order.repair.service;

import com.bonus.canteen.core.order.common.constants.*;
import com.bonus.canteen.core.order.common.model.*;
import com.bonus.canteen.core.order.custom.OrderCustomBusiness;
import com.bonus.canteen.core.order.mq.po.OrderSavePO;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.common.houqin.utils.id.Id;
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.LocalDateTime;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderRepairService {
   @Autowired
   @Lazy
   protected OrderCustomBusiness orderCustomBusiness;
   

   public OrderSavePO packageNewPaidOrderInfo(List<OrderInfo> orderInfoList) {
      String newMacOrderId = Id.nextString();
      OrderSavePO orderSavePO = OrderSavePO.byLockKey(newMacOrderId);
      Iterator var4 = orderInfoList.iterator();

      while(var4.hasNext()) {
         OrderInfo existOrderInfo = (OrderInfo)var4.next();
         OrderInfo newOrderInfo = existOrderInfo.copyInstance();
         newOrderInfo.setOrderId(Id.next());
         newOrderInfo.setMacOrderId(newMacOrderId);
         newOrderInfo.resetAmountsZero();
         newOrderInfo.setOrderType(OrderTypeEnum.DEDUCT.getKey());
         newOrderInfo.setOrderState(OrderStateEnum.WAIT_PLACE.getKey());
         newOrderInfo.setPayState(PayStateEnum.UN_PAY.getKey());
         newOrderInfo.setOrderRefundState(OrderRefundStateEnum.UN_APPLY.getKey());
         newOrderInfo.setPayableAmount(existOrderInfo.calcUnRefundAmount());
         orderSavePO.getOrderInfoList().add(newOrderInfo);
         OrderDetail newOrderDetail = OrderDetail.newDefaultDetail(existOrderInfo.calcUnRefundAmount(), DetailTypeEnum.DEDUCT.getKey());
         newOrderDetail.setDetailId(Id.next());
         newOrderDetail.setOrderId(newOrderInfo.getOrderId());
         newOrderDetail.setOrderDate(newOrderInfo.getOrderDate());
         orderSavePO.getOrderDetailList().add(newOrderDetail);
      }

      return this.orderCustomBusiness.repair().didPackageNewPaidOrderInfo(orderInfoList, orderSavePO);
   }

   
}
