package com.yuchen.saas.park.service.util;

import com.alibaba.fastjson.JSON;
import com.thinkunion.park.service.common.enums.parking.NumberEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘通
 * @version 1.0
 * @date 2021/3/27 13:59
 */
@Slf4j
@Component
public class VideoEventsUtils {

    /**
     *  查询人员记录缓存的数据
     * @return
     */
    public Object findMonitorData(String parkId) {
        // return personVideoEventsService.get(MongoCommonEnum.KEY_PERSON_VIDEO.getValue()+"_"+parkId);
        return null;
    }
    /**
     * 将事件信息添加到缓存
     * 一共10条，新增数据位于集合首，去除集合最后一条
     */
    public void savePersonVideoEvents(Map<String, Object> mongoCommonData,String parkId) {
        log.info("mongoCommonData:" + mongoCommonData);
        if(!mongoCommonData.isEmpty()) {
            // 定义缓存人员压线事件集合
            List<Map> personVideoList = new ArrayList<Map>();
            // 查询数据
//            Object personVideo = personVideoEventsService.get(MongoCommonEnum.KEY_PERSON_VIDEO.getValue()+"_"+mongoCommonData.get("parkId"));
            Object personVideo = null;
            System.out.println("personVideo:" + personVideo);
            if(personVideo != null) {
                personVideoList = JSON.parseArray(String.valueOf(personVideo), Map.class);
                if(personVideoList.size()>= NumberEnum.TEN.getValue())
                    personVideoList = personVideoList.subList(NumberEnum.ZERO.getValue(),NumberEnum.NINE.getValue());
                // 将数据添加到首位
                personVideoList.add(NumberEnum.ZERO.getValue(), mongoCommonData);
            } else {
                personVideoList.add(mongoCommonData);
            }
            // 保存到缓存
            if(personVideoList != null && personVideoList.size() > 0) {
//                personVideoEventsService.save(MongoCommonEnum.KEY_PERSON_VIDEO.getValue()+"_"+mongoCommonData.get("parkId"), JSON.toJSONString(personVideoList));
            }
        }
    }
}
