package com.yonge.cooleshow.teacher.controller.open;

import cn.hutool.extra.servlet.ServletUtil;
import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
import com.yonge.cooleshow.biz.dal.entity.UserOrder;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.UserOrderService;
import com.yonge.cooleshow.biz.dal.service.UserPaymentCoreService;
import com.yonge.cooleshow.biz.dal.vo.UserOrderVo;
import com.yonge.cooleshow.biz.dal.wrapper.UserPaymentOrderWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.teacher.vo.UserPaymentOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpServletRequest;
import java.util.Objects;

import static com.yonge.cooleshow.common.entity.HttpResponseResult.succeed;

@Slf4j
@RestController
@RequestMapping("${app-config.url.teacher:}/open/userOrder")
@Api(tags = "开放权限接口-支付回调")
public class UserPaymentClient {

    @Autowired
    private UserPaymentCoreService userPaymentCoreService;

    @Autowired
    private UserOrderService userOrderService;

    @ApiOperation(value = "用户付款", notes = "用户付款")
    @PostMapping("/executePayment/v2")
    public R<UserPaymentOrderWrapper.PaymentReq> executePayment(@Validated @RequestBody UserPaymentOrderVo.PaymentReqConfig config, HttpServletRequest request) {
        // 设置下单用户信息


        // 用户下单请求
        UserPaymentOrderWrapper.PaymentOrderReqConfig reqConfig = UserPaymentOrderWrapper.PaymentOrderReqConfig.from(config.jsonString());

        // 支付三方
        reqConfig.setPaymentVendor(config.getPaymentVendor());
        // 请示IP地址
        reqConfig.setIp(ServletUtil.getClientIP(request));
        // 创建用户支付数据
        UserPaymentOrderWrapper.PaymentReq paymentConfig = userPaymentCoreService.executePayment(JwtUserInfo.builder()
                .userId(config.getUserId()).clientType(ClientEnum.TENANT.getCode()).build(), reqConfig);
        if (Objects.isNull(paymentConfig)) {
            throw BizException.from("付款失败");
        }

        return R.from(paymentConfig);
    }

    /**
     * 查询单条
     */
    @GetMapping("/detailByOrderNo/{orderNo}")
    @ApiOperation(value = "通过订单号查询详情", notes = "传入orderNo")
    public HttpResponseResult<UserOrderVo> detailByOrderNo(@PathVariable("orderNo") String orderNo) {
        UserOrder param = new UserOrder();
        param.setOrderNo(orderNo);
        UserOrderVo detail = userOrderService.detailApp(param);
        return succeed(detail);
    }

}
