package com.xingxi.common.service;

import com.xingxi.business.Order.domain.Order;
import com.xingxi.business.Order.domain.OrderDetail;
import com.xingxi.business.PaymentInfo.domain.PaymentInfo;
import com.xingxi.common.enums.EOrderDetailStatus;
import com.xingxi.common.enums.EOrderStatus;
import com.xingxi.common.mapper.XingxiUnifiedpayExtMapper;
import com.xingxi.common.mq.bo.order.OrderBO;
import com.xingxi.common.mq.publisher.order.OrderPayedMqPublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
class DefaultXingxiUnifiedpayCallbackClient implements XingxiUnifiedpayCallbackClient {
    private final OrderPayedMqPublisher orderPayedMqPublisher;
    private final XingxiUnifiedpayExtMapper xingxiUnifiedpayExtMapper;

    @Transactional
    @Override
    public void execute(XingxiUnifiedpayCallbackDTO biandanUnifiedpayCallbackDTO) {
        String tradeState = biandanUnifiedpayCallbackDTO.getTradeState();
        String outTradeNo = biandanUnifiedpayCallbackDTO.getOutTradeNo();
        String callUnifiedpayComponent = biandanUnifiedpayCallbackDTO.getCallUnifiedpayComponent();

        PaymentInfo dbPaymentInfo = xingxiUnifiedpayExtMapper.selectLockPaymentInfoByOutTradeNo(outTradeNo);
        if (dbPaymentInfo != null && "SUCCESS".equals(tradeState)) {
            dbPaymentInfo.setUpdateUser(callUnifiedpayComponent);
            dbPaymentInfo.setUpdateTime(new Date());
            updatePaymentInfo(dbPaymentInfo, biandanUnifiedpayCallbackDTO);
            Long orderId = dbPaymentInfo.getOrderId();

            // lock
            Order dbOrder = xingxiUnifiedpayExtMapper.selectLockOrderByOrderId(orderId);
            dbOrder.setUpdateTime(dbPaymentInfo.getUpdateTime());
            dbOrder.setUpdateUser(dbPaymentInfo.getUpdateUser());

            updateOrderStatus(dbOrder);
            // 发送订单支付完成事件消息
            orderPayedEvent(dbOrder);

            // 发送微信订阅消息 mq异步处理
//            WxMaMsgService msgService = wxMaService.getMsgService();
//            msgService.sendSubscribeMsg(WxMaSubscribeMessage.builder().templateId("").build());
        }
    }

    private void updatePaymentInfo(PaymentInfo dbPaymentInfo, XingxiUnifiedpayCallbackDTO biandanUnifiedpayCallbackDTO) {
        String tradeState = biandanUnifiedpayCallbackDTO.getTradeState();
        String tradeType = biandanUnifiedpayCallbackDTO.getTradeType();
        String bankType = biandanUnifiedpayCallbackDTO.getBankType();
        String transactionId = biandanUnifiedpayCallbackDTO.getTransactionId();
        String callUnifiedpayComponent = biandanUnifiedpayCallbackDTO.getCallUnifiedpayComponent();

        PaymentInfo updatePaymentInfo = new PaymentInfo();
        updatePaymentInfo.setPayId(dbPaymentInfo.getPayId());
        updatePaymentInfo.setTradeType(tradeType);
        updatePaymentInfo.setTradeState(tradeState);
        updatePaymentInfo.setTransactionId(transactionId);
        updatePaymentInfo.setSuccessTime(new Date());
        updatePaymentInfo.setPayTime(updatePaymentInfo.getSuccessTime());
        updatePaymentInfo.setBankType(bankType);
        updatePaymentInfo.setUpdateUser(callUnifiedpayComponent);
        updatePaymentInfo.setUpdateTime(dbPaymentInfo.getUpdateTime());
        xingxiUnifiedpayExtMapper.updatePaymentInfo(updatePaymentInfo);
    }

    private void updateOrderStatus(Order dbOrder) {
        Order updateOrderStatus = new Order();
        updateOrderStatus.setOrderId(dbOrder.getOrderId());
        updateOrderStatus.setOrderStatus(EOrderStatus.PAYED.getVal());
        updateOrderStatus.setUpdateUser(dbOrder.getUpdateUser());
        updateOrderStatus.setUpdateTime(dbOrder.getUpdateTime());
        xingxiUnifiedpayExtMapper.updateOrder(updateOrderStatus);

        OrderDetail queryOrderDetail = new OrderDetail();
        queryOrderDetail.setOrderId(dbOrder.getOrderId());
        List<OrderDetail> orderDetails = xingxiUnifiedpayExtMapper.selectOrderDetailList(queryOrderDetail);
        for (OrderDetail dbOrderDetail : orderDetails) {
            OrderDetail updateOrderDetailStatus = new OrderDetail();
            updateOrderDetailStatus.setOrderDetailId(dbOrderDetail.getOrderDetailId());
            updateOrderDetailStatus.setOrderDetailStatus(EOrderDetailStatus.APPROVED.getVal());
            updateOrderDetailStatus.setUpdateUser(dbOrder.getUpdateUser());
            updateOrderDetailStatus.setUpdateTime(dbOrder.getUpdateTime());
            xingxiUnifiedpayExtMapper.updateOrderDetail(updateOrderDetailStatus);
        }
    }

    private void orderPayedEvent(Order orderDTO) {
        // EDIT_ROUTE_FOR_PAYED
        // EDIT_EXCHANGE
        OrderBO orderBO = new OrderBO();
        orderBO.setOrderId(orderDTO.getOrderId());
        orderBO.setOrderNo(orderDTO.getOrderNo());
        orderPayedMqPublisher.sendMessage(orderBO);
    }
}
