package com.mortals.xhx.module.hik.event.service.impl;

import com.mortals.xhx.common.code.HikEventTypeEnum;
import com.mortals.xhx.module.hik.event.model.req.callback.EventData;
import com.mortals.xhx.module.hik.event.model.req.callback.EventsItem;
import com.mortals.xhx.module.hik.event.model.req.callback.FaceRecognitionResult;
import com.mortals.xhx.module.hik.event.model.req.callback.Snap;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowEntity;
import com.mortals.xhx.module.realtime.service.RealtimeDataflowService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 陌生人员识别
 *
 * @author:
 * @date: 2023/4/17 9:27
 */
@Slf4j
public class StrangerRecoginze extends EventTypeAbstract {

    private RealtimeDataflowService dataflowService;

    public StrangerRecoginze(int type) {
        super(type);
        this.dataflowService = SpringUtils.getBean(RealtimeDataflowService.class);
    }


    @Override
    int getType() {
        return HikEventTypeEnum.重点人员识别事件.getValue();
    }

    @Override
    public void saveEventData(EventsItem event) {
        EventData eventData = event.getData();
        //识别结果
        FaceRecognitionResult faceRecognitionResult = eventData.getFaceRecognitionResult();
        //抓拍信息
        Snap snap = faceRecognitionResult.getSnap();
        //不论识别结果 保存流量数据
        RealtimeDataflowEntity realtimeDataflowEntity = new RealtimeDataflowEntity();
        realtimeDataflowEntity.setDetectTime(event.getHappenTime());
        String resIndexCodes = eventData.getResInfo().stream().map(i -> i.getIndexCode()).collect(Collectors.joining(","));
        realtimeDataflowEntity.setDevice(resIndexCodes);
        realtimeDataflowEntity.setPicUri(snap.getFaceUrl());
        realtimeDataflowEntity.setEventId(event.getEventId());
        realtimeDataflowEntity.setEventType(HikEventTypeEnum.陌生人员识别事件.getValue().longValue());
        realtimeDataflowEntity.setCreateUserId(1L);
        realtimeDataflowEntity.setCreateTime(new Date());
        realtimeDataflowEntity.setCreateUserName("system");
        realtimeDataflowEntity.initAttrValue();
        dataflowService.save(realtimeDataflowEntity);
    }

}
