package com.cool.store.controller.webc;

import com.cool.store.dto.wx.PreOrderDTO;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.WxPayService;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @FileName: WechatPayController
 * @Description:
 * @date 2024-03-13 14:28
 */

@Slf4j
@Api(tags = "微信小程序支付接口")
@RestController
@RequestMapping({"/mini/wechatPay"})
public class WechatPayController {

    @Resource
    private WxPayService wxPayService;

    @ApiOperation(value = "预支付订单", notes = "预支付订单")
    @PostMapping("/create")
    public ResponseResult<PrepayWithRequestPaymentResponse> createOrder(@Validated @RequestBody PreOrderDTO preOrderDTO) {
        //创建初始化订单
        PrepayWithRequestPaymentResponse response = wxPayService.jsApiOrder(preOrderDTO);
        //更新订单状态
        return ResponseResult.success(response);
    }

    @ApiOperation("根据商户订单号查询订单")
    @GetMapping("/queryOrderByOutTradeNo")
    public ResponseResult<Transaction> queryOrderByOutTradeNo(@RequestParam(value = "outTradeNo",required = true)String outTradeNo) {
        return ResponseResult.success(wxPayService.queryOrderByOutTradeNo(outTradeNo));
    }

    @ApiOperation("根据支付订单号查询订单")
    @GetMapping("/queryOrderByPaymentNo")
    public ResponseResult<Transaction> queryOrderByPaymentNo(@RequestParam(value = "paymentNo",required = true)String paymentNo) {
        return ResponseResult.success(wxPayService.queryOrderByPaymentNo(paymentNo));
    }

    @ApiOperation(value = "预支付-回调")
    @PostMapping("/payNotify")
    public ResponseEntity payNotify(HttpServletRequest request) throws IOException {
        ResponseEntity responseEntity = wxPayService.payNotify(request);
        return responseEntity;
    }

    @ApiOperation(value = "退款申请")
    @PostMapping("/applyRefund")
    public ResponseResult applyRefund(@RequestBody Map<String, Object> params){
        String orderID = String.valueOf(params.get("orderID"));
        String backID = String.valueOf(params.get("backID"));
        BigDecimal backAmount = new BigDecimal(String.valueOf(params.get("backAmount")));
        wxPayService.applyRefund(orderID, backID, backAmount);
        return ResponseResult.success(true);
    }

    @ApiOperation(value = "退款回调")
    @PostMapping("/refundNotify")
    public ResponseEntity refundNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResponseEntity responseEntity = wxPayService.refundNotify(request);
        return responseEntity;
    }

    @GetMapping("/getRefundOrderInfo")
    @ApiOperation("退款查询")
    public ResponseResult<Refund> getRefundOrderInfo(@RequestParam(value = "outRefundNo",required = true)String outRefundNo){
        Refund refund = wxPayService.getRefundOrderInfo(outRefundNo);
        return ResponseResult.success(refund);
    }

}
