package com.yida.data.device.consume.controller;

import cn.hutool.json.JSONUtil;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.FebsResponse;
import com.yida.data.device.consume.service.EduConsumeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 消费订单 Controller(不鉴权)
 *
 * @author ZYJ
 * @date 2023/3/23
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("in/consume")
@Api(tags = "消费订单api")
public class InEduConsumeOrderController {

    private final EduConsumeOrderService eduConsumeOrderService;

    @ApiOperation("微信支付通知结果回调 此接口暂时无用(需手动查询订单支付状态)")
    @PostMapping("/wxPayNotify")
    public FebsResponse wxPayNotify(@RequestBody Map<String, Object> res) {
        return new FebsResponse().put("code", "SUCCESS");
    }

    @ApiOperation("微信退款通知结果回调 此接口暂时无用(需手动查询订单退款状态)")
    @PostMapping("/wxRefundNotify")
    public FebsResponse wxRefundNotify(@RequestBody Map<String, Object> res) {
        return new FebsResponse().put("code", "SUCCESS");
    }

    @ApiOperation("银联支付通知结果回调")
    @PostMapping("/unionPayNotify")
    public String unionPayNotify(@RequestParam Map<String, Object> map) {
        log.info("银联支付通知结果回调, result: {}", JSONUtil.toJsonStr(map));
        return null;
//        return eduConsumeOrderService.unionPayNotify(map);
    }

    @ApiOperation("收钱吧支付通知结果回调")
    @PostMapping("/uPayNotify")
    public String uPayNotify(HttpServletRequest request) throws IOException {
        BufferedReader bufferReader = new BufferedReader(request.getReader());
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferReader.readLine()) != null) {
            sb.append(line);
        }
        log.info("收钱吧支付通知结果回调, result: {}", sb.toString());
        log.info("收钱吧支付通知结果回调签名, sign: {}", request.getHeader("Authorization"));
        return eduConsumeOrderService.uPayNotify(sb.toString(), request);
    }

    @GetMapping(value = "updateOrderStatus")
    @ApiOperation(value = "更新超时订单状态")
    public ResultBean<String> updateOrderStatus() {
        eduConsumeOrderService.updateOrderStatus();
        return ResultBean.buildSuccess();
    }

    @GetMapping(value = "updateRefundStatus")
    @ApiOperation(value = "更新退款订单状态")
    public ResultBean<String> updateRefundStatus() {
        eduConsumeOrderService.updateRefundStatus();
        return ResultBean.buildSuccess();
    }
}
