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

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.common.constant.LeMqConstant;
import com.bonus.canteen.core.common.utils.JacksonUtil;
import com.bonus.canteen.core.common.utils.LogUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.order.android.dto.MacOrderImageSaveDTO;
import com.bonus.canteen.core.order.common.model.*;
import com.bonus.canteen.core.order.mq.po.*;
import com.bonus.canteen.core.pay.common.po.PayResultPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OrderMessageSend {
   private static final Logger log = LoggerFactory.getLogger(OrderMessageSend.class);
   private static final String MQ_ERROR_LOG = "发送MQ消息失败";

//   /** @deprecated */
//   @Deprecated
//   public static void updateOrderMessageSend(OrderInfo orderInfo, List<OrderDetail> orderDetailList, List<OrderAmountChange> amountChangeList, List<OrderAmountDetail> amountDetailList, String outTradeNo) {
//      OrderChangePO orderChangePO = new OrderChangePO(orderInfo, orderDetailList, amountChangeList);
//      orderChangePO.setOutTradeNo(outTradeNo);
//      orderChangePO.setMerchantId(TenantContextHolder.getTenantId());
//      orderChangePO.setChangeType(OrderChangeTypeEnum.UPDATE.getKey());
//      orderChangePO.setAmountDetailList(amountDetailList);
//      String changePOStr = JacksonUtil.writeValueAsStringIgnoreNull(orderChangePO);
//      log.info("[MQ]修改订单,{}", changePOStr);
//      MqUtil.send(changePOStr, LeMqConstant.Topic.ORDER_CHANGE);
//   }
//
//   /** @deprecated */
//   @Deprecated
//   public static void batchUpdateOrderMessageSend(List<OrderInfo> orderInfoList, List<OrderDetail> orderDetailList, List<OrderAmountChange> amountChangeList, List<OrderAmountDetail> amountDetailList, String outTradeNo) {
//      Map<Long, List<OrderDetail>> detailListMap = (Map)orderDetailList.stream().collect(Collectors.groupingBy(OrderDetail::getOrderId));
//      Map<Long, List<OrderAmountChange>> amountChangeMap = amontChangeListHandler(amountChangeList);
//      Map<Long, List<OrderAmountDetail>> amountDetailMap = amontDetailListHandler(amountDetailList);
//      Iterator var8 = orderInfoList.iterator();
//
//      while(var8.hasNext()) {
//         OrderInfo orderInfo = (OrderInfo)var8.next();
//         updateOrderMessageSend(orderInfo, (List)detailListMap.get(orderInfo.getOrderId()), (List)amountChangeMap.get(orderInfo.getOrderId()), (List)amountDetailMap.get(orderInfo.getOrderId()), outTradeNo);
//      }
//
//   }
//
//   /** @deprecated */
//   @Deprecated
//   public static void refundOrderMessageSend(OrderInfo orderInfo, List<OrderDetail> orderDetailList, List<OrderAmountDetail> orderRefundAmountDetailList, OrderRefund orderRefund, List<OrderRefundDetail> refundDetailList, String outTradeNo) {
//      OrderChangePO orderChangePO = new OrderChangePO(orderInfo, orderDetailList, orderRefund, refundDetailList);
//      orderChangePO.setOutTradeNo(outTradeNo);
//      orderChangePO.setMerchantId(TenantContextHolder.getTenantId());
//      orderChangePO.setChangeType(OrderChangeTypeEnum.REFUND.getKey());
//      orderChangePO.setRefundAmountDetailList(orderRefundAmountDetailList);
//      String changePOStr = JacksonUtil.writeValueAsStringIgnoreNull(orderChangePO);
//      log.info("[MQ]退单,{}", changePOStr);
//      MqUtil.send(changePOStr, LeMqConstant.Topic.ORDER_CHANGE);
//   }
//
//   private static Map<Long, List<OrderAmountChange>> amontChangeListHandler(List<OrderAmountChange> amountChangeList) {
//      Object amountChangeMap;
//      if (CollUtil.isEmpty(amountChangeList)) {
//         amountChangeMap = new HashMap();
//      } else {
//         amountChangeMap = (Map)amountChangeList.stream().collect(Collectors.groupingBy(OrderAmountChange::getOrderId));
//      }
//
//      return (Map)amountChangeMap;
//   }
//
//   private static Map<Long, List<OrderAmountDetail>> amontDetailListHandler(List<OrderAmountDetail> amountChangeList) {
//      Object amountChangeMap;
//      if (CollUtil.isEmpty(amountChangeList)) {
//         amountChangeMap = new HashMap();
//      } else {
//         amountChangeMap = (Map)amountChangeList.stream().collect(Collectors.groupingBy(OrderAmountDetail::getOrderId));
//      }
//
//      return (Map)amountChangeMap;
//   }
//
//   /** @deprecated */
//   @Deprecated
//   public static void updateOrderStateMessageSend(OrderInfo orderInfo) {
//      OrderStateUpdatePO orderStateUpdatePO = new OrderStateUpdatePO();
//      orderStateUpdatePO.setOrderId(orderInfo.getOrderId());
//      orderStateUpdatePO.setOrderType(orderInfo.getOrderType());
//      orderStateUpdatePO.setOrderState(orderInfo.getOrderState());
//      orderStateUpdatePO.setDishesState(orderInfo.getDishesState());
//      orderStateUpdatePO.setPayState(orderInfo.getPayState());
//      orderStateUpdatePO.setOrderRefundState(orderInfo.getOrderRefundState());
//      orderStateUpdatePO.setDeliveryState(orderInfo.getDishesState());
//      updateOrderStateMessageSend(orderStateUpdatePO);
//   }
//
//   /** @deprecated */
//   @Deprecated
//   public static void updateOrderStateMessageSend(OrderStateUpdatePO orderStateUpdatePO) {
//      log.info("[MQ]订单状态更新,{}", JacksonUtil.writeValueAsStringIgnoreNull(orderStateUpdatePO));
//      MqUtil.sendDataChange(orderStateUpdatePO, LeMqConstant.DataChangeType.UPDATE, LeMqConstant.Topic.DATA_CHANGE_ORDER);
//   }
//
//   public static void orderV3AsyncSave(OrderSavePO orderSavePO) {
//      log.info("[订单MQv3]发送订单异步保存");
//      orderSavePO.setTraceId(LogUtil.getCurrentTraceId());
//      orderSavePO.setTenantId(TenantContextHolder.getTenantId());
//
//      try {
//         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(orderSavePO), LeMqConstant.Topic.ORDER_V3_ASYNC_SAVE);
//      } catch (Exception var2) {
//         log.error("发送MQ消息失败", var2);
//      }
//
//   }
//
//   public static void orderV3AsyncSaveWeight(String macOrderId) {
//      log.info("[订单MQv3]发送自助餐异步保存");
//      OrderSaveWeightPO po = new OrderSaveWeightPO();
//      po.setTraceId(LogUtil.getCurrentTraceId());
//      po.setTenantId(TenantContextHolder.getTenantId());
//      po.setMacOrderId(macOrderId);
//      MqUtil.send(JacksonUtil.writeValueAsString(po), LeMqConstant.Topic.ORDER_V3_ASYNC_SAVE_WEIGHT);
//   }

   public static void orderV3AsyncPayResult(PayResultPO payResultPO) {
      log.info("[订单MQv3]发送订单异步支付结果通知");
      payResultPO.setTraceId(LogUtil.getCurrentTraceId());
      payResultPO.setMerchantId(TenantContextHolder.getTenantId());

      try {
         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(payResultPO), LeMqConstant.Topic.ORDER_V3_ASYNC_PAY_RESULT);
      } catch (Exception var2) {
         log.error("发送MQ消息失败", var2);
      }

   }

   public static LocalDateTime orderV3AsyncTimeoutByMacOrderId(String macOrderId, int delaySecond) {
      log.info("[订单MQv3]发送未支付订单异步支付超时通知");
      OrderCancelPO orderCancelPO = new OrderCancelPO();
      orderCancelPO.setMacOrderId(macOrderId);
      orderCancelPO.setMerchantId(TenantContextHolder.getTenantId());
      orderCancelPO.setTraceId(LogUtil.getCurrentTraceId());
      MqUtil.sendDelay(JacksonUtil.writeValueAsString(orderCancelPO), LeMqConstant.Topic.ORDER_V3_ASYNC_TIMEOUT,
              delaySecond * 1000);
      return LocalDateTime.now().plusSeconds((long)delaySecond);
   }
//
//   public static void orderV3AsyncImageSave(List<MacOrderImageSaveDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         log.info("[订单MQv3]发送异步保存订单图片");
//         Long tenantId = TenantContextHolder.getTenantId();
//         OrderImagePO imagePO = (new OrderImagePO()).setMerchantId(tenantId).setTraceId(LogUtil.getCurrentTraceId()).setSaveList(dtoList);
//
//         try {
//            MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(imagePO), LeMqConstant.Topic.ORDER_V3_ASYNC_IMAGE_UPDATE);
//         } catch (Exception var4) {
//            log.error("发送MQ消息失败", var4);
//         }
//
//      }
//   }

   public static void orderV3Placed(String outTradeNo, List<OrderInfo> orderInfoList, List<OrderDetail> detailList, List<OrderDelivery> orderDeliveryList, List<OrderAmountChange> amountChangeList, List<OrderAmountDetail> amountDetailList) {
      log.info("[订单MQv3]发送订单已下单通知");
      OrderPlacedPO orderSavePO = new OrderPlacedPO();
      orderSavePO.setTraceId(LogUtil.getCurrentTraceId());
      orderSavePO.setTenantId(TenantContextHolder.getTenantId());
      orderSavePO.setOutTradeNo(outTradeNo);
      orderSavePO.setOrderInfoList(orderInfoList);
      orderSavePO.setDetailList(detailList);
      orderSavePO.setOrderDeliveryList(orderDeliveryList);
      orderSavePO.setAmountChangeList(amountChangeList);
      orderSavePO.setAmountDetailList(amountDetailList);

      try {
         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(orderSavePO), LeMqConstant.Topic.ORDER_V3_PLACED);
      } catch (Exception var8) {
         log.error("发送MQ消息失败", var8);
      }

   }

   public static void orderV3Canceled(OrderInfo orderInfo, List<OrderDetail> detailList) {
      log.info("[订单MQv3]发送订单已取消通知");
      OrderCanceledPO po = new OrderCanceledPO();
      po.setTraceId(LogUtil.getCurrentTraceId());
      po.setTenantId(TenantContextHolder.getTenantId());
      po.setOrderInfo(orderInfo);
      po.setDetailList(detailList);

      try {
         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(po), LeMqConstant.Topic.ORDER_V3_CANCELED);
      } catch (Exception var4) {
         log.error("发送MQ消息失败", var4);
      }

   }

   public static void orderV3DishesStateUpdated(OrderInfo orderInfo, Integer dishesStateBeforeUpdated) {
      OrderDishesStateUpdatePO orderStateUpdatePO = OrderDishesStateUpdatePO.of(orderInfo);
      orderStateUpdatePO.setTraceId(LogUtil.getCurrentTraceId());
      orderStateUpdatePO.setTenantId(TenantContextHolder.getTenantId());
      orderStateUpdatePO.setDishesStateBeforeUpdated(dishesStateBeforeUpdated);
      String str = JacksonUtil.writeValueAsStringIgnoreNull(orderStateUpdatePO);
      log.info("[订单MQv3]订单制作配送状态更新,{}", str);
      MqUtil.send(str, LeMqConstant.Topic.ORDER_V3_DISHES_STATE_UPDATED);
   }

   public static void orderV3Refunded(OrderInfo orderInfo, List<OrderDetail> orderDetailList,
                                      List<OrderAmountDetail> orderRefundAmountDetailList,
                                      OrderRefund orderRefund, List<OrderRefundDetail> refundDetailList, String outTradeNo) {
      log.info("[订单MQv3]订单退款通知");
      OrderRefundPO orderChangePO = new OrderRefundPO();
      orderChangePO.setTraceId(LogUtil.getCurrentTraceId());
      orderChangePO.setTenantId(TenantContextHolder.getTenantId());
      orderChangePO.setOutTradeNo(outTradeNo);
      orderChangePO.setOrderInfo(orderInfo);
      orderChangePO.setOrderDetailList(orderDetailList);
      orderChangePO.setOrderRefund(orderRefund);
      orderChangePO.setOrderRefundDetailList(refundDetailList);
      orderChangePO.setRefundAmountDetailList(orderRefundAmountDetailList);

      try {
         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(orderChangePO), LeMqConstant.Topic.ORDER_V3_REFUNDED);
      } catch (Exception var8) {
         log.error("发送MQ消息失败", var8);
      }

   }
//
//   public static void orderV3DishesAdd(OrderInfo orderInfo, List<OrderDetail> orderDetailList, List<OrderDetail> addOrderDetailList) {
//      log.info("[订单MQv3]订单加菜");
//      OrderDishesEditPO orderChangePO = new OrderDishesEditPO();
//      orderChangePO.setTraceId(LogUtil.getCurrentTraceId());
//      orderChangePO.setTenantId(TenantContextHolder.getTenantId());
//      orderChangePO.setOrderInfo(orderInfo);
//      orderChangePO.setDetailList(orderDetailList);
//      orderChangePO.setEditDetailList(addOrderDetailList);
//
//      try {
//         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(orderChangePO), LeMqConstant.Topic.ORDER_V3_ADD_DISHES);
//      } catch (Exception var5) {
//         log.error("发送MQ消息失败", var5);
//      }
//
//   }
//
//   public static void orderV3DishesRemove(OrderInfo orderInfo, List<OrderDetail> orderDetailList, List<OrderDetail> removedOrderDetailList) {
//      log.info("[订单MQv3]订单减菜");
//      OrderDishesEditPO orderChangePO = new OrderDishesEditPO();
//      orderChangePO.setTraceId(LogUtil.getCurrentTraceId());
//      orderChangePO.setTenantId(TenantContextHolder.getTenantId());
//      orderChangePO.setOrderInfo(orderInfo);
//      orderChangePO.setDetailList(orderDetailList);
//      orderChangePO.setEditDetailList(removedOrderDetailList);
//
//      try {
//         MqUtil.sendByTxEnd(JacksonUtil.writeValueAsString(orderChangePO), LeMqConstant.Topic.ORDER_V3_REMOVE_DISHES);
//      } catch (Exception var5) {
//         log.error("发送MQ消息失败", var5);
//      }
//
//   }
}
