
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.RechargePayInfoDTO;
import com.tmerclub.cloud.payment.manager.PayManager;
import com.tmerclub.cloud.payment.service.AllinpayService;
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 = "余额充值接口")
@RefreshScope
public class RechargePayController {

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


    /**
     * 支付接口
     */
    @PostMapping("/recharge")
    @Operation(summary = "余额充值接口", description = "进行余额充值")
    public ServerResponseEntity<?> pay(@Valid @RequestBody RechargePayInfoDTO payParam) throws AlipayApiException, WxPayException {
        if (Objects.equals(payParam.getPayType(), PayType.BALANCE.value()) || Objects.equals(payParam.getPayType(), PayType.SCOREPAY.value())) {
            throw new LuckException("不支持该充值方式");
        }
        LoginInfoBO loginInfoBO = tokenFeignClient.getLoginInfo();

        Long userId = AuthUserContext.getUserId();

        PayInfoBO payInfo = payInfoService.recharge(userId, payParam);


        payInfo.setPayType(payParam.getPayType());
        // 查询支付类型（默认支付/通联支付）
        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();
        payInfo.setAllinpayUserId(String.valueOf(userId));
        payInfo.setBizUserId(loginInfoBO.getBizUserId());
        // 回调地址
        payInfo.setApiNoticeUrl(domainUrl + "/ua/notice/pay/" + paySysType + "/" + PayEntry.RECHARGE.value() + "/" + payParam.getPayType());
        payInfo.setReturnUrl(payParam.getReturnUrl());
        return payManager.doPay(payInfo);
    }
}
