package cn.sikey.hmd.service.razorpay;

import cn.sikey.framework.common.exception.ServiceException;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.hmd.api.hmd.vo.plans.PlansRespVO;
import cn.sikey.hmd.controller.app.alipay.vo.CheckPayReqVO;
import cn.sikey.hmd.enums.PayChannelEnum;
import cn.sikey.hmd.service.AbstractPaymentService;
import cn.sikey.hmd.service.plans.PlansService;
import cn.sikey.order.api.order.vo.GenerateOrderReqVO;
import cn.sikey.pay.api.alipay.AlipayApi;
import cn.sikey.pay.api.razorpay.dto.RazorpayTradeDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Objects;

/**
 * @Author: nelson
 * @Date: 2025/3/31
 * @Description: razorpay
 */
@Service("razorpayPaymentService")
@Slf4j
public class RazorpayPaymentServiceImpl extends AbstractPaymentService implements RazorpayPaymentService {

    @Resource
    private PlansService plansService;

    @Resource
    private AlipayApi alipayApi;

    @Resource
    private TransactionTemplate transactionTemplate;

    private static final String PAY_TYPE = "razorPay";

    /**
     * 手机网站支付
     *
     * @param razorPayTradeDTO razorpay
     * @return
     */
    @Override
    public CommonResult<CheckPayReqVO> razorpayTradeWapPay(RazorpayTradeDTO razorPayTradeDTO) {
        String merchantProjectApplication = razorPayTradeDTO.getMerchantProjectApplication();
        String deviceId = razorPayTradeDTO.getDeviceId();

        super.checkPayMent(merchantProjectApplication, deviceId, PAY_TYPE);

        // 校验支付，生成订单
        CheckPayReqVO checkPayReqVO = checkPayAndGenerateOrder(razorPayTradeDTO);
        // 保存支付记录
        savePayments(checkPayReqVO.getOrderId(), checkPayReqVO.getPlansId(), deviceId, checkPayReqVO.getAmount(), PayChannelEnum.RAZOR_PAY.getValue(), merchantProjectApplication);

        return CommonResult.success(checkPayReqVO);
    }

    /**
     * 校验支付，生成订单
     *
     * @param razorPayTradeDTO razorPay交易
     * @return
     */
    private CheckPayReqVO checkPayAndGenerateOrder(RazorpayTradeDTO razorPayTradeDTO) {
        int validity = razorPayTradeDTO.getValidity();
        PlansRespVO plansRespVO = plansService.queryPlans(validity);
        if (Objects.isNull(plansRespVO)) {
            log.warn("[手机网站支付-razorPay]请配置套餐");
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请配置套餐");
        }

        int price = plansRespVO.getPrice();

        String deviceId = razorPayTradeDTO.getDeviceId();

        GenerateOrderReqVO generateOrderReqVO = super.getRazorpayGenerateOrderReqVO(plansRespVO.getId(), deviceId, razorPayTradeDTO.getMerchantProjectApplication(), price, PAY_TYPE);

        String outTradeNo = generateOrderReqVO.getOrderSn();

        CheckPayReqVO checkPayReqVO = new CheckPayReqVO();
        checkPayReqVO.setOutTradeNo(outTradeNo);
        checkPayReqVO.setOrderId(generateOrderReqVO.getId());
        checkPayReqVO.setPlansId(plansRespVO.getId());
        checkPayReqVO.setAmount(price);
        return checkPayReqVO;
    }

}


