package com.ycwl.basic.controller.mobile;


import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.enums.BizCodeEnum;
import com.ycwl.basic.model.wx.WXPayOrderReqVO;
import com.ycwl.basic.model.wx.WxPayRespVO;
import com.ycwl.basic.pay.entity.PayResponse;
import com.ycwl.basic.service.mobile.WxPayService;
import com.ycwl.basic.utils.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.security.GeneralSecurityException;

/**
 * @Author: songmingsong
 * @CreateTime: 2024-12-05
 * @Description: 微信支付
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/mobile/wx/pay/v1")
@Api(tags = "微信支付相关接口")
public class AppWxPayController {

    @Autowired
    private WxPayService wxPayService;

    @ApiOperation(value = "微信支付回调", notes = "微信支付回调")
    @PostMapping("/payNotify")
    @IgnoreToken
    public ApiResponse<?> payNotify(HttpServletRequest request) {
        wxPayService.payNotify(request);
        return ApiResponse.success(BizCodeEnum.REQUEST_OK);
    }

    @RequestMapping("/{scenicId}/payNotify")
    @IgnoreToken
    public Object payNotifyByScenicId(@PathVariable Long scenicId, HttpServletRequest request) {
        PayResponse payResponse = wxPayService.payNotify(scenicId, request);
        if (payResponse == null) {
            return ApiResponse.buildResult(BizCodeEnum.ADVANCE_PAYMENT_CALLBACK_FAILED);
        }
        if (StringUtils.isNotBlank(payResponse.getCustomResponse())) {
            return payResponse.getCustomResponse();
        }
        return ApiResponse.success(BizCodeEnum.REQUEST_OK);
    }

    @ApiOperation(value = "微信支付退款回调", notes = "微信支付退款回调")
    @PostMapping("/{scenicId}/refundNotify")
    @IgnoreToken
    public ApiResponse<?> refundNotify(@PathVariable Long scenicId, HttpServletRequest request) throws GeneralSecurityException, IOException {
        return ApiResponse.buildResult(wxPayService.refundNotify(scenicId, request) ?
                BizCodeEnum.SUCCESS :
                BizCodeEnum.ADVANCE_PAYMENT_CALLBACK_REFUND_FAILED);
    }
}
