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


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.BooleanUtil;
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.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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author tmerclub
 */
@RestController("multishopRechargePayController")
@RequestMapping("/m/pay")
@Tag(name = "商家余额充值接口")
@RefreshScope
public class RechargePayController {

    @Autowired
    private PayInfoService payInfoService;
    @Autowired
    private PayManager payManager;
    @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("不支持该充值方式");
        }
        // 查询支付类型（默认支付/通联支付）
        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);
        if (Objects.equals(paySettlementConfigVO.getPaySettlementType(), 1)) {
            throw new LuckException("该接口不支持通联支付");
        }
        LoginInfoBO loginInfoBO = tokenFeignClient.getLoginInfo();
        Long shopId = AuthUserContext.getShopId();

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

        payInfo.setBizUserId(loginInfoBO.getBizUserId());
        payInfo.setPayType(payParam.getPayType());
        // 回调地址
        payInfo.setApiNoticeUrl(domainUrl + "/ua/notice/pay/" + payInfo.getPaySysType() + "/" + PayEntry.RECHARGE.value() + "/" + payParam.getPayType());
        payInfo.setReturnUrl(payParam.getReturnUrl());
        return payManager.doPay(payInfo);
    }


    @GetMapping("/isPay/{payEntry}/{orderIds}")
    @Operation(summary = "根据订单号查询该订单是否已经支付", description = "根据订单号查询该订单是否已经支付")
    public ResponseEntity<Boolean> isPay(@PathVariable Integer payEntry, @PathVariable String orderIds) {
        Long shopId = AuthUserContext.getShopId();

        Integer isPay = payInfoService.isPay(orderIds, shopId, payEntry, 0);
        return ResponseEntity.ok(BooleanUtil.isTrue(isPay));
    }


}
