
package com.tmerclub.cloud.payment.controller.user;


import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.tmerclub.cloud.api.auth.bo.LoginInfoBO;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.feign.TokenFeignClient;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.PayType;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.payment.bo.PayInfoBO;
import com.tmerclub.cloud.payment.constant.PayEntry;
import com.tmerclub.cloud.payment.dto.BuyVipPayInfoDTO;
import com.tmerclub.cloud.payment.manager.PayManager;
import com.tmerclub.cloud.payment.service.PayInfoService;
import com.tmerclub.cloud.payment.vo.PaySettlementConfigVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author tmerclub
 */
@RestController
@RequestMapping("/pay")
@Tag(name = "购买vip")
@RefreshScope
public class BuyVipPayController {

    @Autowired
    private PayInfoService payInfoService;
    @Autowired
    private PayManager payManager;
    @DubboReference
    private ConfigFeignClient configFeignClient;
    @DubboReference
    private TokenFeignClient tokenFeignClient;
    @Value("${application.domainUrl}")
    private String domainUrl;

    /**
     * 支付接口
     */
    @PostMapping("/vip")
    @Operation(summary = "购买vip", description = "购买vip，将普通会员变成付费会员")
    public ServerResponseEntity<?> vipBuy(@Valid @RequestBody BuyVipPayInfoDTO payParam) throws AlipayApiException, WxPayException {
        if (Objects.equals(payParam.getPayType(), PayType.SCOREPAY.value())) {
            throw new LuckException("不支持该充值方式");
        }
        // 查询支付类型（默认支付/通联支付）
        ServerResponseEntity<String> configResponse = configFeignClient.getConfig(Constant.PAY_SETTLEMENT_CONFIG);
        if (!configResponse.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        PaySettlementConfigVO paySettlementConfigVO = Json.parseObject(configResponse.getData(), PaySettlementConfigVO.class);
        Integer paySysType = paySettlementConfigVO.getPaySettlementType();
        payParam.setPaySysType(paySysType);
        LoginInfoBO loginInfoBO = tokenFeignClient.getLoginInfo();
        Long userId = AuthUserContext.getUserId();
        // 构建支付信息
        PayInfoBO payInfo = payInfoService.vipBuy(userId, payParam);
        payInfo.setAllinpayUserId(String.valueOf(userId));
        payInfo.setBizUserId(loginInfoBO.getBizUserId());
        payInfo.setPayType(payParam.getPayType());
        payInfo.setApiNoticeUrl(domainUrl + "/ua/notice/pay/" + paySysType + "/" + PayEntry.VIP.value() + "/" + payParam.getPayType());
        payInfo.setReturnUrl(payParam.getReturnUrl());
        payInfo.setIsVip(true);
        // 调用第三方接口进行支付
        return payManager.doPay(payInfo);
    }
}
