package com.dk.mdm.controller.wxapi.basic;

import com.alibaba.fastjson.JSONObject;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.service.wxapi.basic.WechatPayService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @author : admin
 *  * 后台管理设置的金额 从dkic_ms里取数据 金额
 *  * 所以放到了 权限的oauth-server 的服务里240327--作废
 * @desc : 微信支付的接口
 * @date : 2024/2/22 17:21
 */
@Api(tags = "微信支付的接口")
@RestController
@RequestMapping("/wxapi/basic/wechat/pay")
@Slf4j
public class WechatPayController {
    private ExecutorService executorService = Executors.newFixedThreadPool(20);

    @Autowired
    private WechatPayService wechatPayService;

    /**
     * 商户 下单选取旗舰版或专业版的订单-预支付订单
     *
     * @param param
     * @return
     * @throws WxPayException
     */
    @ApiOperation(value = "下单选取旗舰版或专业版的订单预支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "openId"),
            @ApiImplicitParam(name = "paymentSn", value = "支付单号")
    })
    @PostMapping("/unified_order")
    public ResponseResultVO unifiedOrder(@RequestBody Map<String, Object> param)
            throws WxPayException {
        return wechatPayService.unifiedOrder(param);
    }

    /**
     * @desc : 微信支付的回调
     * @author : 姜永辉
     * @date : 2024/03/06 11:20
     */
    @PostMapping("/notify/order/{appId}")
    public String parseOrderNotifyResult(@PathVariable(value = "appId") String appId, @RequestBody String xmlData)
            throws WxPayException {
        log.info("parseOrderNotifyResult--0" + appId + "---" + xmlData);
        WxPayService wxPayService = wechatPayService.getWxPayService();
        final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
        // 另起线程处理业务
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                String resultCode = notifyResult.getResultCode();
                log.info("parseOrderNotifyResult--1" + resultCode );
                // 支付成功
                if (resultCode.equals("SUCCESS")) {
                    // 处理支付成功逻辑
                    try {
                        wechatPayService.notifyWechatPay(notifyResult);
                    } catch (Exception e) {
                        log.error("微信回调业务处理报错,params:" + notifyResult, e);
                    }
                } else {
                    log.error("没有处理微信回调业务，交易状态：{},params:{}", resultCode, JSONObject.toJSON(notifyResult));
                }
            }
        });
        log.info("parseOrderNotifyResult--10000-成功"  );
        return WxPayNotifyResponse.success("成功");
    }


}
