package com.mortals.xhx.busiz.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.module.hik.event.model.req.callback.EventRecognBlackReq;
import com.mortals.xhx.module.hik.event.service.IHikEventService;
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;

@RestController
@Slf4j
@RequestMapping("hik")
public class HikCallbackController {

    @Autowired
    private IHikEventService hikEventService;

    @PostMapping("eventRcv")
    @UnAuth
    public Rest<String> refreshMessage(@RequestBody EventRecognBlackReq req) {
        log.info("【接收事件数据】【请求体】-->  " + JSONObject.toJSONString(req));
        try {
            if ("OnEventNotify".equals(req.getMethod())) {
                Rest<String> rest = hikEventService.handleEvent(req);
                return rest;
            }

            return Rest.ok("接收成功");
            //todo
        } catch (Exception e) {
            log.error("接收数据失败", e);
            return Rest.fail();
        }
    }

}
