package com.liquidnet.service.goblin.controller;

import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.param.shouqianba.callback.CallbackParams;
import com.liquidnet.service.goblin.param.shouqianba.callback.CouponCallbackContent;
import com.liquidnet.service.goblin.param.shouqianba.callback.OrderCallbackContent;
import com.liquidnet.service.goblin.param.shouqianba.callback.RefundCallbackContent;
import com.liquidnet.service.goblin.service.IGoblinShouQianBaService;
import com.liquidnet.service.goblin.service.IGoblinSqbOrderService;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "收钱吧相关")
@RestController
@RequestMapping("/sqb")
public class GoblinShouqianbaController {

    @Autowired
    private IGoblinShouQianBaService shouQianBaService;

    @Autowired
    private IGoblinSqbOrderService sqbOrderService;

    @ApiOperation("下单回调")
    @PostMapping("/order/callback")
    public ResponseDto<String> orderCallback(@RequestBody CallbackParams callbackParams) {
        if (null == callbackParams) {
            log.error("下单回调参数为空");
            return ResponseDto.failure("下单回调参数为空");
        }
        boolean verified = shouQianBaService.verifySignature(callbackParams);
        if (!verified) {
            log.error("下单回调验签失败");
            return ResponseDto.failure("验签失败");
        }

        OrderCallbackContent orderCallbackContent = JsonUtils.fromJson(callbackParams.getContent(), OrderCallbackContent.class);
        log.info("下单回调content: {}", callbackParams.getContent());
        // 业务
        return sqbOrderService.handlePayCallback(orderCallbackContent);
    }

    @ApiOperation("退款回调")
    @PostMapping("/refund/callback")
    public ResponseDto<String> refundCallback(@RequestBody CallbackParams callbackParams) {
        if (null == callbackParams) {
            log.error("退款回调参数为空");
            return ResponseDto.failure("退款回调参数为空");
        }
        boolean verified = shouQianBaService.verifySignature(callbackParams);
        if (!verified) {
            log.error("退款回调验签失败");
            return ResponseDto.failure("验签失败");
        }

        RefundCallbackContent refundCallbackContent = JsonUtils.fromJson(callbackParams.getContent(), RefundCallbackContent.class);
        log.info("退款回调content: {}", callbackParams.getContent());
        // 业务
        return sqbOrderService.handleRefundCallback(refundCallbackContent);
    }

    @ApiOperation("券状态回调")
    @PostMapping("/coupon/callback")
    public ResponseDto<String> couponCallback(@RequestBody CallbackParams callbackParams) {
        if (null == callbackParams) {
            log.error("券状态回调参数为空");
            return ResponseDto.failure("券状态回调参数为空");
        }
        boolean verified = shouQianBaService.verifySignature(callbackParams);
        if (!verified) {
            log.error("券状态回调验签失败");
            return ResponseDto.failure("验签失败");
        }

        CouponCallbackContent callbackContent = JsonUtils.fromJson(callbackParams.getContent(), CouponCallbackContent.class);
        log.info("券状态回调content: {}", callbackParams.getContent());
        // 业务

        return sqbOrderService.handleCouponCallback(callbackContent);
    }

}
