package com.icloud.server.payutils.notify;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.service.WxPayService;
import com.icloud.server.payutils.PayClient;
import com.icloud.server.payutils.PayClientFactory;
import com.icloud.server.payutils.dto.PayNotifyDataDTO;
import com.icloud.server.payutils.enums.PayTypeEnum;
import com.icloud.server.payutils.order.PayOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

import static com.icloud.server.web.err.ServiceExceptionUtil.exception;
import static com.icloud.server.web.err.enums.ErrorCodeConstants.PAY_CHANNEL_CLIENT_NOT_FOUND;

@Api(tags = "用户 APP - 支付订单")
@RestController
@RequestMapping("/pay/order")
@Validated
@Slf4j
public class AppPayOrderController {
    //    @Resource
//    private PayRefundService refundService;
    @Resource
    private PayOrderService orderService;
    @Resource
    private PayClientFactory payClientFactory;


    @PostMapping(value = "/wxnotify/{channelId}")
    @ApiOperation("渠道统一的支付成功,或退款成功 通知url")
    public String notifyChannelPay(@PathVariable("channelId") Long channelId,
                                   @RequestParam Map<String, String> params,
                                   @RequestBody String originData) throws Exception {

        System.out.println("channelId:"+channelId);
        for(String value: params.values()) {
            // 输出返回的值
            System.out.println("====微信回传参数:"+value + ";======");
        }
        // 校验支付渠道是否存在
        PayClient payClient = payClientFactory.getPayClient(channelId);
        if (payClient == null) {
            log.error("[notifyPayOrder][渠道编号({}) 找不到对应的支付客户端]", channelId);
            throw exception(PAY_CHANNEL_CLIENT_NOT_FOUND);
        }

        // 校验通知数据是否合法
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().params(params).body(originData).build();
        payClient.verifyNotifyData(notifyData);

        // 如果是退款，则发起退款通知
//        if (payClient.isRefundNotify(notifyData)) {
//            refundService.notifyPayRefund(channelId, PayNotifyDataDTO.builder().params(params).body(originData).build());
//            return "success";
//        }

        // 如果非退款，则发起支付通知
        orderService.notifyPayOrder(PayTypeEnum.WXPAY,channelId, PayNotifyDataDTO.builder().params(params).body(originData).build());
        return "success";
    }
    @PostMapping(value = "/alinotify/{channelId}")
    @ApiOperation("渠道统一的支付成功,或退款成功 通知url")
    public String notifyChannelaliPay(@PathVariable("channelId") Long channelId,
                                   @RequestParam Map<String, String> params,
                                   @RequestBody String originData) throws Exception {

        System.out.println("channelId:"+channelId);
        for(String value: params.values()) {
// 输出返回的值
            System.out.print(value + ", ");
        }
        // 校验支付渠道是否存在
        PayClient payClient = payClientFactory.getPayClient(channelId);
        if (payClient == null) {
            log.error("[notifyPayOrder][渠道编号({}) 找不到对应的支付客户端]", channelId);
            throw exception(PAY_CHANNEL_CLIENT_NOT_FOUND);
        }

        // 校验通知数据是否合法
        PayNotifyDataDTO notifyData = PayNotifyDataDTO.builder().params(params).body(originData).build();
        payClient.verifyNotifyData(notifyData);

        // 如果是退款，则发起退款通知
//        if (payClient.isRefundNotify(notifyData)) {
//            refundService.notifyPayRefund(channelId, PayNotifyDataDTO.builder().params(params).body(originData).build());
//            return "success";
//        }

        // 如果非退款，则发起支付通知
        orderService.notifyPayOrder(PayTypeEnum.ALIPAY,channelId, PayNotifyDataDTO.builder().params(params).body(originData).build());
        return "success";
    }
}
