package com.kym.miniapp.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.kym.common.R;
import com.kym.common.annotation.ApiLog;
import com.kym.service.wechat.WxPayService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author skyline
 * @description
 * @date 2023-07-22 20:36
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {
    private final WxPayService wxPayService;

    public PaymentController(WxPayService wxPayService) {
        this.wxPayService = wxPayService;
    }

    /**
     * 充值（归属站点由服务端按用户归属确定，客户端传值无效）
     *
     * @param rechargeConfigId
     * @return
     */
    @ApiLog("用户充值微信支付")
    @GetMapping("/wxPay")
    @ResponseBody
    R<?> prepay(@RequestParam Long rechargeConfigId) {
        return R.success(wxPayService.wxPay(rechargeConfigId));
    }

    @ApiLog(value = "微信回调", ignoreParams = true)
    @SneakyThrows
    @SaIgnore
    @PostMapping("/notify")
    ResponseEntity<Object> notify(HttpServletRequest request) {
        return wxPayService.wxNotify(request);
    }


    /**
     * 专属优惠活动充值（归属站点由服务端按用户归属确定，客户端传值无效）
     */
    @ApiLog("用户优惠活动充值微信支付")
    @GetMapping("/promotionPay")
    @ResponseBody
    R<?> promotionPay(@RequestParam String promotionToken) {
        return R.success(wxPayService.promotionPay(promotionToken));
    }

    @ApiLog("用户申请退款")
    @PostMapping("/wxApplyRefund")
    @ResponseBody
    R<?> wxAppRefund(@RequestParam String reason) {
        wxPayService.applyWxRefund(StpUtil.getLoginIdAsLong(), reason);
        return R.success();
    }

    @ApiLog(value = "微信退款回调", ignoreParams = true)
    @SaIgnore
    @PostMapping("/refundNotify")
    ResponseEntity<Object> refundNotify(HttpServletRequest request) {
        return wxPayService.wxRefundNotify(request);
    }
}
