package com.czg.controller;

import com.alibaba.fastjson2.JSONObject;
import com.czg.CzgPayUtils;
import com.czg.entity.CzgBaseRespParams;
import com.czg.mq.PrintMqListener;
import com.czg.order.service.OrderInfoService;
import com.czg.task.StatisticTask;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ww
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/notify")
public class NotifyController {
    private static final String SUCCESS = "SUCCESS";

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private StatisticTask statisticTask;


    @RequestMapping("/payCallBack")
    public String notifyCallBack(@RequestBody CzgBaseRespParams respParams){
        JSONObject czg = CzgPayUtils.getCzg(respParams);
        AssertUtil.isNull(czg, "支付回调数据为空");
        log.info("支付回调数据为:{}", czg);
        orderInfoService.payCallBackOrder(czg.getString("mchOrderNo"), czg);
        return SUCCESS;
    }

    @RequestMapping("/refundCallBack")
    public String refundCallBack(@RequestBody CzgBaseRespParams respParams){
        JSONObject czg = CzgPayUtils.getCzg(respParams);
        AssertUtil.isNull(czg, "退款回调数据为空");
        log.info("退款回调数据为:{}", czg);
        orderInfoService.refundCallBackOrder(czg.getString("mchOrderNo"), czg);
        return SUCCESS;
    }

    @Resource
    private PrintMqListener printMqListener;
    @RequestMapping("/test")
    public void test(@RequestParam String id) {
        printMqListener.orderPrint(id);
    }
}
