package com.seer.teach.mall.app.order.handler;

import cn.hutool.extra.servlet.JakartaServletUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.seer.teach.mall.app.order.convert.AppOrderConvert;
import com.seer.teach.mall.entity.GoodsEntity;
import com.seer.teach.mall.entity.OrderEntity;
import com.seer.teach.mall.entity.OrderGoodsEntity;
import com.seer.teach.mall.handler.OrderHandler;
import com.seer.teach.mall.service.IOrderService;
import com.seer.teach.pay.api.order.PayOrderApi;
import com.seer.teach.pay.api.order.dto.PayOrderCreateReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 创建支付订单
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class CreatePayOrderHandler implements OrderHandler {

    private final PayOrderApi payOrderApi;

    private final IOrderService orderService;

    @Value("${mall.order.expire.time:30}")
    private int orderExpireTime;

    @Override
    public void afterOrderCreate(OrderEntity order, List<OrderGoodsEntity> orderItems, Map<Integer, GoodsEntity> goodsEntityMap) {
        if(order.getActualPrice().compareTo(BigDecimal.ZERO) > 0){
            OrderGoodsEntity orderGoodsEntity = orderItems.get(0);
            GoodsEntity goods = goodsEntityMap.get(orderGoodsEntity.getGoodsId());
            PayOrderCreateReqDTO createReqDTO = AppOrderConvert.INSTANCE.convertPayOrderCreateReqDTO(order,goods);
            createReqDTO.setTotalAmount(order.getActualPrice());
            createReqDTO.setTotalCoin(order.getOrderTotalCoin());
            createReqDTO.setUserIp(JakartaServletUtil.getClientIP(getRequest()));
            createReqDTO.setExpireTime(getOrderExpireTime(order));
            log.info("商城订单[{}]创建支付订单请求参数：{}", order.getId(),createReqDTO);
            Integer payOrderId = payOrderApi.createOrder(createReqDTO);
            log.info("商城订单[{}]创建的支付订单Id：{}",order.getId(), payOrderId);
            boolean updated = orderService.updatePayOrderIdById(order.getId(), payOrderId);
            log.info("更新订单结果：{}", updated);
        }
    }

    public HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!(requestAttributes instanceof ServletRequestAttributes)) {
            return null;
        }
        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    public LocalDateTime getOrderExpireTime(OrderEntity order) {
        LocalDateTime orderCreateTime = order.getAddTime();
        return orderCreateTime.plusMinutes(orderExpireTime);
    }
}
