package com.seer.teach.mall.app.api;

import com.seer.teach.mall.api.MallOrderApi;
import com.seer.teach.mall.api.req.OrderPayNotifyReq;
import com.seer.teach.mall.api.req.OrderRefundNotifyReq;
import com.seer.teach.mall.api.resp.OrderDTO;
import com.seer.teach.mall.app.order.service.IAppOrderRefundCallbackService;
import com.seer.teach.mall.convert.OrderConverter;
import com.seer.teach.mall.entity.OrderChildEntity;
import com.seer.teach.mall.entity.OrderEntity;
import com.seer.teach.mall.entity.OrderGoodsEntity;
import com.seer.teach.mall.service.IOrderChildService;
import com.seer.teach.mall.service.IOrderGoodsService;
import com.seer.teach.mall.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 商城订单API实现
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class MallOrderApiImpl implements MallOrderApi {

    private final IOrderService orderService;

    private final IAppOrderRefundCallbackService orderRefundCallbackService;

    private final IOrderGoodsService orderGoodsService;

    private final IOrderChildService orderChildService;

    @Override
    public OrderDTO getOrder(Integer orderId) {
        log.info("getOrder orderId:{}", orderId);
        OrderEntity order = orderService.getById(orderId);

        if (Objects.isNull(order)) {
            return null;
        }
        List<OrderChildEntity> childOrders = orderChildService.getListByParentId(orderId);

        List<OrderGoodsEntity> orderGoods = orderGoodsService.listByOrderId(orderId);

        return OrderConverter.INSTANCE.convertOne2DTO(order, childOrders, orderGoods);
    }

    @Override
    public OrderDTO getOrderByOrderSn(String orderSn) {
        log.info("getOrderByOrderSn orderSn:{}", orderSn);
        OrderEntity orderEntity = orderService.getOneByOrderSn(orderSn);
        if(Objects.isNull(orderEntity)){
            return null;
        }
        return this.getOrder(orderEntity.getId());
    }

    @Override
    public boolean orderPayAfterSuccessNotify(OrderPayNotifyReq orderPayNotifyReq) {
        log.info("MallOrderApiImpl updateOrderStatus orderSn:{}, payStatus:{}",
            orderPayNotifyReq.getOrderSn(), orderPayNotifyReq.getPayStatus());
        boolean result = orderService.orderPayAfterSuccessNotify(orderPayNotifyReq);
        log.info("updateOrderStatus result:{}", result);
        return result;
    }

    @Override
    public boolean orderPayAfterFailNotify(OrderPayNotifyReq orderPayNotifyReq) {
        log.info("MallOrderApiImpl orderPayAfterFailNotify orderSn:{}, payStatus:{}",
            orderPayNotifyReq.getOrderSn(), orderPayNotifyReq.getPayStatus());
        boolean result = orderService.orderPayAfterFailNotify(orderPayNotifyReq);
        log.info("orderPayAfterFailNotify result:{}", result);
        return result;
    }

    @Override
    public boolean cancelOrderRefundAfterSuccessNotify(OrderRefundNotifyReq orderRefundNotifyReq) {
        log.info("收到订单取消退款成功通知，订单号:{}，退款状态:{}",
                orderRefundNotifyReq.getOrderSn(), orderRefundNotifyReq.getRefundStatus());
        try {
            boolean result = orderRefundCallbackService.cancelOrderRefundAfterSuccessNotify(orderRefundNotifyReq.getOrderSn());
            log.info("订单取消退款成功处理结果:{}，订单号:{}", result, orderRefundNotifyReq.getOrderSn());
            return result;
        } catch (Exception e) {
            log.error("处理订单取消退款成功通知异常，订单号:{}", orderRefundNotifyReq.getOrderSn(), e);
            return false;
        }
    }

    @Override
    public boolean cancelOrderRefundAfterFailNotify(OrderRefundNotifyReq orderRefundNotifyReq) {
        log.info("收到订单取消退款失败通知，订单号:{}，退款状态:{}",
                orderRefundNotifyReq.getOrderSn(), orderRefundNotifyReq.getRefundStatus());
        try {
            boolean result = orderRefundCallbackService.cancelOrderRefundAfterFailNotify(orderRefundNotifyReq.getOrderSn());
            log.info("订单取消退款失败处理结果:{}，订单号:{}", result, orderRefundNotifyReq.getOrderSn());
            return result;
        } catch (Exception e) {
            log.error("处理订单取消退款失败通知异常，订单号:{}", orderRefundNotifyReq.getOrderSn(), e);
            return false;
        }
    }
}