package com.yonge.cooleshow.student.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.result.BaseResult;
import com.yonge.toolset.payment.base.enums.OpenEnum;
import com.yonge.toolset.payment.base.model.OpenAuth;
import com.yonge.toolset.payment.core.props.PaymentProperties;
import com.yonge.toolset.payment.core.service.PaymentClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: liweifan
 * @Data: 2022/3/11 18:16
 */
@RestController
@RequestMapping("/payment")
@Api(value = "付款接口", tags = "付款接口")
public class PaymentController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private PaymentClient paymentClient;
    @Autowired
    private PaymentProperties paymentProperties;

    @ApiOperation(value = "通过用户给的授权码获取三方支付授权信息")
    @GetMapping("/getOpenId")
    public HttpResponseResult<Map<String, Object>> getOpenId(OpenAuth openAuth) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        OpenEnum openType = OpenEnum.valueOf(paymentProperties.getOpenType());
        openAuth.setOpenType(openType);
        BaseResult<Map<String, Object>> res = paymentClient.getOpenAuthMsg(openAuth);
        return HttpResponseResult.status(res);
    }
}
