package cn.sikey.hmd.service;

import cn.hutool.core.util.StrUtil;
import cn.sikey.framework.common.exception.ServiceException;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.hmd.api.hmd.dto.activation.DeviceActivationDTO;
import cn.sikey.hmd.api.hmd.vo.payments.PaymentsReqVO;
import cn.sikey.hmd.enums.PayStatusEnum;
import cn.sikey.hmd.service.activation.DeviceActivationService;
import cn.sikey.hmd.service.payments.PaymentService;
import cn.sikey.order.api.order.OrderApi;
import cn.sikey.order.api.order.dto.GenerateOrderReqDTO;
import cn.sikey.order.api.order.vo.GenerateOrderReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author: nelson
 * @Date: 2025/5/8
 * @Description: 抽象支付
 */
@Slf4j
public abstract class AbstractPaymentService {

    // private static final Integer VIP_LIFE_LONG = 9999;

    @Resource
    private DeviceActivationService deviceActivationService;

    @Resource
    private OrderApi orderApi;

    @Resource
    private PaymentService paymentService;

    /**
     * 校验支付
     *
     * @param merchantProjectApplication 商户
     * @param deviceId                   设备编号
     * @param payType                    支付类型
     * @return
     */
    public void checkPayMent(String merchantProjectApplication, String deviceId, String payType) {
        if (StrUtil.isBlank(merchantProjectApplication)) {
            log.warn("[手机网站支付-{}]请配置商户配置", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请配置商户配置");
        }

        // 校验设备激活
        DeviceActivationDTO deviceActivationDTO = deviceActivationService.queryDeviceActivation(deviceId);
        if (Objects.isNull(deviceActivationDTO)) {
            log.warn("[手机网站支付-{}]设备还未激活", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "设备还未激活");
        }

        /*boolean isExpire = Objects.nonNull(remainingTimesExpireTimeRespVO.getPlansValidity()) && remainingTimesExpireTimeRespVO.getPlansValidity().intValue() == ValidityEnum.YEAR.getValue().intValue();
        if (isExpire) {
            log.warn("[手机网站支付-{}]您已是会员，勿需再支付", payType);
            throw new ServiceException(VIP_LIFE_LONG, "您已是会员，勿需再支付");
        }*/
    }

    /**
     * 获取订单
     *
     * @param plansId                    套餐编号
     * @param deviceId                   设备编号
     * @param merchantProjectApplication 商户
     * @param price                      价格
     * @param payType                    支付类型
     * @return
     */
    public GenerateOrderReqVO getGenerateOrderReqVO(Long plansId, String deviceId, String merchantProjectApplication, Integer price, String payType) {
        GenerateOrderReqDTO generateOrderReqDTO = new GenerateOrderReqDTO();
        generateOrderReqDTO.setPlansId(plansId);
        generateOrderReqDTO.setDeviceId(deviceId);
        generateOrderReqDTO.setMerchantProjectApplication(merchantProjectApplication);
        generateOrderReqDTO.setTotalAmount(price);
        CommonResult<GenerateOrderReqVO> generateOrder = orderApi.generateOrder(generateOrderReqDTO);
        if (!generateOrder.getCode().equals(HttpStatus.SC_OK)) {
            log.warn("[手机网站支付-{}]调用订单服务失败", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "调用订单服务失败");
        }

        GenerateOrderReqVO generateOrderReqVO = generateOrder.getData();
        if (Objects.isNull(generateOrderReqVO) || Objects.isNull(generateOrderReqVO.getOrderSn())) {
            log.warn("[手机网站支付-{}]调用订单服务数据是空", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "调用订单服务数据是空");
        }
        return generateOrderReqVO;
    }

    /**
     * 获取razorpay订单
     *
     * @param plansId                    套餐编号
     * @param deviceId                   设备编号
     * @param merchantProjectApplication 商户
     * @param price                      价格
     * @param payType                    支付类型
     * @return
     */
    public GenerateOrderReqVO getRazorpayGenerateOrderReqVO(Long plansId, String deviceId, String merchantProjectApplication, Integer price, String payType) {
        GenerateOrderReqDTO generateOrderReqDTO = new GenerateOrderReqDTO();
        generateOrderReqDTO.setPlansId(plansId);
        generateOrderReqDTO.setDeviceId(deviceId);
        generateOrderReqDTO.setMerchantProjectApplication(merchantProjectApplication);
        generateOrderReqDTO.setTotalAmount(price);
        CommonResult<GenerateOrderReqVO> generateOrder = orderApi.razorpayGenerateOrder(generateOrderReqDTO);
        if (!generateOrder.getCode().equals(HttpStatus.SC_OK)) {
            log.warn("[手机网站支付-{}]调用订单服务失败", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "调用订单服务失败");
        }

        GenerateOrderReqVO generateOrderReqVO = generateOrder.getData();
        if (Objects.isNull(generateOrderReqVO) || Objects.isNull(generateOrderReqVO.getOrderSn())) {
            log.warn("[手机网站支付-{}]调用订单服务数据是空", payType);
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "调用订单服务数据是空");
        }
        return generateOrderReqVO;
    }

    /**
     * 保存付款
     *
     * @param orderId                    订单id
     * @param plansId                    套餐id
     * @param deviceId                   设备id
     * @param amount                     金额
     * @param channel                    渠道
     * @param merchantProjectApplication 商户项目应用
     * @return
     */
    public void savePayments(Long orderId, Long plansId, String deviceId, Integer amount, Integer channel, String merchantProjectApplication) {
        // 保存支付记录
        PaymentsReqVO paymentsReqVO = new PaymentsReqVO();
        paymentsReqVO.setOrderId(orderId);
        paymentsReqVO.setPlansId(plansId);
        paymentsReqVO.setDeviceId(deviceId);
        paymentsReqVO.setAmount(amount);
        paymentsReqVO.setChannel(channel);
        paymentsReqVO.setStatus(PayStatusEnum.TO_BE_PAID.getValue());
        paymentsReqVO.setPaymentTime(LocalDateTime.now());
        paymentsReqVO.setMerchantProjectApplication(merchantProjectApplication);
        paymentService.savePayments(paymentsReqVO);
    }
}
