package com.cool.store.controller.webb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.service.VodService;
import com.cool.store.utils.vod.AliResponseUtil;
import com.cool.store.utils.vod.CallbackRequest;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author byd
 * @date 2024-05-07 19:13
 */
@RestController
@RequestMapping("pc/vod")
@Api(tags = "视频回调接口")
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    @RequestMapping("callback")
    public JSONObject callback(HttpServletRequest request) throws IOException {
        log.info("=================================");
        log.info("VOD视频回调");
        String cl = request.getHeader("content-length");
        log.info("content-length:" + cl);
        String vodCallbackBody = AliResponseUtil.GetPostBody(request.getInputStream(), Integer.parseInt(cl));
        log.info("vodCallbackBody:" + vodCallbackBody);
        CallbackRequest callback = JSON.parseObject(vodCallbackBody, CallbackRequest.class);
        log.info("callback" + JSON.toJSONString(callback));
        vodService.callback(callback);
        log.info("=================================");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Status", "OK");
        return jsonObject;
    }
}
