package com.zanxiang.game.module.sdk.controller;

import com.zanxiang.game.module.base.pojo.enums.PayWayEnum;
import com.zanxiang.game.module.sdk.annotation.UnSignCheck;
import com.zanxiang.game.module.sdk.annotation.ValidLogin;
import com.zanxiang.game.module.sdk.pojo.param.ProductPayParam;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.pojo.vo.PayParamVO;
import com.zanxiang.game.module.sdk.service.IOrderPayService;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.pojo.ResultVO;
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.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付公共入口
 *
 * @author xufeng
 * @date 2022/6/8 14:49
 */
@Api(tags = "支付公共入口")
@RestController
@RequestMapping(value = "/api/pay")
@Slf4j
public class PayController {

    @Autowired
    private IOrderPayService orderPayService;

    @UnSignCheck
    @ApiOperation(value = "获取客服支付参数")
    @GetMapping(value = "/param/applet")
    public ResultVO<PayParamVO> getCustomPayParam(@RequestParam String code, @RequestParam String orderId, HttpServletRequest request) {
        return ResultVO.ok(orderPayService.getPayParam(code, null, orderId, request));
    }

    @ApiOperation(value = "获取公众号支付参数")
    @GetMapping(value = "/param/mp")
    public ResultVO<PayParamVO> getMpPayParam(@RequestParam String openId, @RequestParam String orderId,
                                              @ValidLogin UserData userData, HttpServletRequest request) {
        return ResultVO.ok(orderPayService.getPayParam(null, openId, orderId, request));
    }

    @ApiOperation(value = "获取小游戏支付方式")
    @GetMapping(value = "/device/applet")
    public ResultVO<Long> getAppletPayDevice(@ValidLogin UserData userData) {
        return ResultVO.ok(orderPayService.getAppletPayDevice(userData));
    }

    @ApiOperation(value = "支付参数生成")
    @PostMapping(value = "/create")
    public ResultVO<Map> create(@Validated @RequestBody ProductPayParam product, @ValidLogin UserData userData) {
        log.error("接收到支付请求, product : {}", JsonUtil.toString(product));
        return ResultVO.ok(orderPayService.payCreate(product, userData));
    }

    @UnSignCheck
    @ApiOperation(value = "支付宝支付异步回调")
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public String aliPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return orderPayService.notify(request, response, PayWayEnum.ALI_PAY.getPayWayId());
    }

    @UnSignCheck
    @ApiOperation(value = "微信支付异步回调")
    @RequestMapping(value = "/wxPayNotify", method = RequestMethod.POST)
    public String wxPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return orderPayService.notify(request, response, PayWayEnum.WX_PAY.getPayWayId());
    }

    @ApiOperation(value = "米大师支付异步回调<接口已弃用>")
    @RequestMapping(value = "/miPayNotify", method = RequestMethod.GET)
    public ResultVO<Map> miPayNotify(@RequestParam String orderId, @ValidLogin UserData userData) {
        //返回参数
        Map<String, Object> payParamMap = new HashMap<>(2);
        payParamMap.put("status", "0");
        payParamMap.put("billNo", "success");
        return ResultVO.ok(payParamMap);
    }

    @ApiOperation(value = "获取订单支付结果")
    @GetMapping(value = "/get/result")
    public ResultVO<Boolean> getResult(@RequestParam String orderId) {
        return ResultVO.ok(orderPayService.payResult(orderId));
    }
}
