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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuchen.saas.message.api.feign.ws.WebSocketFeignServer;
import com.yuchen.saas.message.api.vo.NoticeVo;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatParkPeople;
import com.yuchen.saas.park.api.entity.park.PmgBatParkVisitor;
import com.yuchen.saas.park.api.entity.park.PmgLogPeopleCount;
import com.yuchen.saas.park.api.entity.park.PmgLogVideoEventsCount;
import com.yuchen.saas.park.service.config.HikConfig;
import com.yuchen.saas.park.service.pull.entity.FaceUploadResponse;
import com.yuchen.saas.park.service.pull.untils.Base64Img;
import com.yuchen.saas.park.service.pull.untils.StompClient;
import com.yuchen.saas.park.service.pull.vo.VideoEventsCodeTypeVo;
import com.yuchen.saas.park.service.service.IPeopleCountService;
import com.yuchen.saas.park.service.service.PmgBatParkPeopleService;
import com.yuchen.saas.park.service.service.PmgBatParkVisitorService;
import com.yuchen.saas.park.service.service.PmgLogVideoEventsCountService;
import com.yuchen.saas.park.service.util.VideoEventsUtils;
import com.yuchen.saas.parking.api.vo.IoTEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 吕海泉
 * @create 2020/12/22 1:34
 */
@Slf4j
@Service
public class EventHandlerServiceImpl implements EventHandler {

    @Autowired
    private FaceHandler faceHandler;

    @Autowired
    private StompClient stompClient;

    @Autowired
    private HikConfig hikConfig;

    @Autowired
    private PmgBatParkPeopleService pmgBatParkPeopleService;

    @Autowired
    private IPeopleCountService iPeopleCountService;

    @Autowired
    private PmgBatParkVisitorService pmgBatParkVisitorService;

    @Autowired
    private WebSocketFeignServer pushService;

    @Autowired
    private VideoEventsUtils videoEventsUtils;

    @Autowired
    private PmgLogVideoEventsCountService pmgLogVideoEventsCountService;

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(EventHandlerServiceImpl.class);

    /**
     * 面板机
     *
     * @param pmgBatSmartEquipment
     * @param ioTEvent
     */
    @Override
    public void FaceDatahandler(PmgBatSmartEquipment pmgBatSmartEquipment, IoTEvent ioTEvent) {

        if (ioTEvent.getIdentifier().equals("face_than")) {
            PmgLogPeopleCount pmgLogPeopleCount = new PmgLogPeopleCount();
            // 将设备信息写入记录
            pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            // 开始拼接记录信息
            JSONObject jsonObject = JSONObject.parseObject(String.valueOf(ioTEvent.getOut().get("info")));
            Float temperature = jsonObject.getFloat("temperature");
            log.info(String.valueOf(temperature));
            String facePhoto_src = jsonObject.getString("facePhoto_src");
            String result = jsonObject.getString("result");
            long event_time = jsonObject.getLongValue("event_time");
            long time = (event_time - 8 * 60 * 60 * 1000L);
            String user_id = jsonObject.getString("user_id");
            int score = jsonObject.getIntValue("score");
            Map<String, String> faceInfo = new HashMap<>();
            faceInfo.put("temperature", String.valueOf(temperature));
            faceInfo.put("facePhotoSrc", facePhoto_src);
            faceInfo.put("time", String.valueOf(time));
            faceInfo.put("score", String.valueOf(score));
            faceInfo.put("peopleId", user_id);
            faceInfo.put("parkId", String.valueOf(pmgBatSmartEquipment.getParkId()));
            faceInfo.put("equipmentName", pmgBatSmartEquipment.getName());
            faceInfo.put("setPosition", pmgBatSmartEquipment.getSetPosition());
            faceInfo.put("tenantId", pmgBatSmartEquipment.getTenantId());
            faceInfo.put("createDept", String.valueOf(pmgBatSmartEquipment.getCreateDept()));
            // 20210729修改
            faceInfo.put("buildingId", "-1");
            faceInfo.put("floorId", "-1");
            faceInfo.put("roomId", "-1");

            String l = null;
            if (StringUtil.isNotBlank(user_id)) {
                l = user_id.substring(user_id.length() - 1);
            }
            if ("1".equals(l)) {
                PmgBatParkPeople pmgBatParkPeople = pmgBatParkPeopleService.getById(user_id);
                if (pmgBatParkPeople.getId() != null) {
                    // 发送到stomp
                    pmgLogPeopleCount.setName(pmgBatParkPeople.getName());
                    pmgLogPeopleCount.setTemperature(temperature);
                    pmgLogPeopleCount.setFacePhotoSrc(facePhoto_src);
                    pmgLogPeopleCount.setTimeStamp(time);
                    pmgLogPeopleCount.setScore(score);
                    pmgLogPeopleCount.setPersonId(user_id);
                    pmgLogPeopleCount.setSex(pmgBatParkPeople.getSex());
                    pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
                    // 20210729 修改
                    pmgLogPeopleCount.setBuildingId(pmgBatParkPeople.getBuildingId());
                    pmgLogPeopleCount.setFloorId(pmgBatParkPeople.getFloorId());
                    pmgLogPeopleCount.setRoomId(pmgBatParkPeople.getRoomId());

                    pmgLogPeopleCount.setImgSrc(pmgBatParkPeople.getImgsrc());
                    pmgLogPeopleCount.setCompany(pmgBatParkPeople.getCompany());
                    pmgLogPeopleCount.setType(pmgBatParkPeople.getType());
                    try {
                        File file = Base64Img.base64ToFile(facePhoto_src, "2.png");
                        R<FaceUploadResponse> faceUploadResponseR = faceHandler.uploadFile(file);
                        if (faceUploadResponseR.isSuccess()) {
                            pmgLogPeopleCount.setFacePhotoSrc(faceUploadResponseR.getData().getFaceImageUrl());
                            log.info(faceUploadResponseR.getData().getFaceImageUrl());
                        }

                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                    pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogPeopleCount)));
                    // 保存到mongo
                    videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)), pmgLogPeopleCount.getParkId().toString());
                    iPeopleCountService.save(pmgLogPeopleCount);

                } else if (pmgBatParkPeople.getId() == null) {
                    strangerHandler(pmgLogPeopleCount, faceInfo);
                }
            } else if ("2".equals(l)) {
                PmgBatParkVisitor pmgBatParkVisitor = pmgBatParkVisitorService.getById(Long.valueOf(user_id));
                if (pmgBatParkVisitor.getId() != null) {
                    // 发送到stomp
                    pmgLogPeopleCount.setName(pmgBatParkVisitor.getName());
                    pmgLogPeopleCount.setTemperature(temperature);
                    pmgLogPeopleCount.setFacePhotoSrc(facePhoto_src);
                    pmgLogPeopleCount.setTimeStamp(time);
                    pmgLogPeopleCount.setScore(score);
                    pmgLogPeopleCount.setPersonId(user_id);
                    pmgLogPeopleCount.setSex("1");
                    // 20210729 修改
                    pmgLogPeopleCount.setBuildingId(pmgBatParkVisitor.getBuildingId());
                    pmgLogPeopleCount.setFloorId(pmgBatParkVisitor.getFloorId());
//                    pmgLogPeopleCount.setRoomId(pmgBatParkVisitor.getRoomId());

//                    pmgLogPeopleCount.setCode(pmgBatParkVisitor.getCode());
                    pmgLogPeopleCount.setImgSrc(pmgBatParkVisitor.getFaceImg());
//                    pmgLogPeopleCount.setCompany(pmgBatParkVisitor.getCompany());
//                    pmgLogPeopleCount.setType(pmgBatParkVisitor.getType());
                    pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                    pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                    pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                    pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                    pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
                    pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                    pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
//                    // 发送到stomp
//                    JSONObject jsonObject1  = new JSONObject();
//                    jsonObject1.put("name",pmgBatParkVisitor.getName());
//                    jsonObject1.put("temperature",temperature);
//                    jsonObject1.put("face_photo_src",facePhoto_src);
//                    jsonObject1.put("timeStamp",String.valueOf(time));
//                    jsonObject1.put("score",score);
//                    jsonObject1.put("peopleId",user_id);
//                    jsonObject1.put("sex",pmgBatParkVisitor.getSex());
//                    jsonObject1.put("building",pmgBatParkVisitor.getBuilding());
//                    jsonObject1.put("buildingName",pmgBatParkVisitor.getBuildingName());
//                    jsonObject1.put("imgSrc",pmgBatParkVisitor.getImgsrc());
//                    jsonObject1.put("company",pmgBatParkVisitor.getCompany());
//                    jsonObject1.put("code",pmgBatParkVisitor.getCode());
//                    jsonObject1.put("type",pmgBatParkVisitor.getType());


//                    pmgLogPeopleCount.setTemperature(temperature);
//                    pmgLogPeopleCount.setImgSrc(pmgBatParkVisitor.getImgsrc());
//                    pmgLogPeopleCount.setScore(score);
//                    pmgLogPeopleCount.setName(pmgBatParkVisitor.getName());
//                    pmgLogPeopleCount.setSex(pmgBatParkVisitor.getSex());
//                    pmgLogPeopleCount.setCode(pmgBatParkVisitor.getCode());
//                    pmgLogPeopleCount.setType(pmgBatParkVisitor.getType());
//                    pmgLogPeopleCount.setBuilding(pmgBatParkVisitor.getBuilding());
//                    pmgLogPeopleCount.setBuildingName(pmgBatParkVisitor.getBuildingName());
//                    pmgLogPeopleCount.setPersonId(user_id);
//                    pmgLogPeopleCount.setTimeStamp(time);
                    try {
                        File file = Base64Img.base64ToFile(facePhoto_src, "2.png");
                        R<FaceUploadResponse> faceUploadResponseR = faceHandler.uploadFile(file);
                        if (faceUploadResponseR.isSuccess()) {
                            pmgLogPeopleCount.setFacePhotoSrc(faceUploadResponseR.getData().getFaceImageUrl());
                            log.info(faceUploadResponseR.getData().getFaceImageUrl());
                        }

                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                    pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogPeopleCount)));
                    // 保存到mongo
                    videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                            pmgLogPeopleCount.getParkId().toString());
                    iPeopleCountService.save(pmgLogPeopleCount);

                } else if (pmgBatParkVisitor.getId() == null) {
                    strangerHandler(pmgLogPeopleCount, faceInfo);
                }
            } else {
                strangerHandler(pmgLogPeopleCount, faceInfo);
            }
            // 推送人员统计数据
            stompClient.pushPersonData(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 入园小时分布
            stompClient.pushEnterParkPeopleCount(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 已处理未处理 、 总异常事件
            stompClient.getWarningHandlerResult(String.valueOf(pmgBatSmartEquipment.getParkId()));
        }
    }

    /**
     * 通过identity 获取 code 和 type
     *
     * @param identity
     * @return
     */
    public VideoEventsCodeTypeVo getCodeTypeByIdentify(String identity) {
        VideoEventsCodeTypeVo videoEventsCodeTypeVo = new VideoEventsCodeTypeVo();
        if (StringUtils.isNotBlank(identity)) {
            if ("leave_area".equals(identity)) { // 离开区域事件
                videoEventsCodeTypeVo.setCode("131587");
                videoEventsCodeTypeVo.setType("离开区域事件");
            } else if ("people_gathering".equals(identity)) { // 人员聚集事件
                videoEventsCodeTypeVo.setCode("131593");
                videoEventsCodeTypeVo.setType("人员聚集事件");
            } else if ("move_quickly".equals(identity)) { // 快速移动事件
                videoEventsCodeTypeVo.setCode("131592");
                videoEventsCodeTypeVo.setType("快速移动事件");
            } else if ("cross_border_detection".equals(identity)) { // 越界侦测事件
                videoEventsCodeTypeVo.setCode("131585");
                videoEventsCodeTypeVo.setType("越界侦测事件");
            } else if ("wandering_detection".equals(identity)) { // 徘徊侦测事件
                videoEventsCodeTypeVo.setCode("131590");
                videoEventsCodeTypeVo.setType("徘徊侦测事件");
            } else if ("strenuous_exercise".equals(identity)) { // 剧烈运动事件
                videoEventsCodeTypeVo.setCode("131596");
                videoEventsCodeTypeVo.setType("剧烈运动事件");
            } else if ("abnormal_spacing".equals(identity)) { // 间距运动事件
                videoEventsCodeTypeVo.setCode("131665");
                videoEventsCodeTypeVo.setType("间距运动事件");
            } else if ("fall_to_the_ground".equals(identity)) { // 倒地事件
                videoEventsCodeTypeVo.setCode("131605");
                videoEventsCodeTypeVo.setType("倒地事件");
            } else if ("leaving".equals(identity)) { // 离岗事件
                videoEventsCodeTypeVo.setCode("131603");
                videoEventsCodeTypeVo.setType("离岗事件");
            }
        }
        return videoEventsCodeTypeVo;
    }

    /**
     * 人员
     *
     * @param pmgBatSmartEquipment
     * @param ioTEvent
     */
    @SneakyThrows
    @Override
    public void FaceRecognition(PmgBatSmartEquipment pmgBatSmartEquipment, IoTEvent ioTEvent) {
        if (ioTEvent.getIdentifier().equals("user_face")) { // 园内人员
            PmgLogPeopleCount pmgLogPeopleCount = new PmgLogPeopleCount();
            String face_name = (String) ioTEvent.getOut().get("face_name");
            String face_code = (String) ioTEvent.getOut().get("face_code");
            String index_code = (String) ioTEvent.getOut().get("index_code");
            // String capture_face_url = (String) ioTEvent.getOut().get("capture_face_url");
            // 20210123 update url
            String capture_face_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_face_url")), "UTF-8");
            // String capture_bkg_url = (String) ioTEvent.getOut().get("capture_bkg_url");
            // 20210123 update url
            String capture_bkg_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_bkg_url")), "UTF-8");
            String time = ioTEvent.getDeviceTime() + "000";
            Map<String, String> faceR = new HashMap<>();
            faceR.put("face_name", face_name);
            faceR.put("face_code", face_code);
            faceR.put("index_code", index_code);
            faceR.put("capture_face_url", capture_face_url);
            faceR.put("captureBkgUrl", capture_bkg_url);
            faceR.put("time", time);
            faceR.put("equipmentName", pmgBatSmartEquipment.getName());
            faceR.put("parkId", String.valueOf(pmgBatSmartEquipment.getParkId()));
            faceR.put("setPosition", pmgBatSmartEquipment.getSetPosition());
            faceR.put("tenantId", pmgBatSmartEquipment.getTenantId());
            faceR.put("createDept", String.valueOf(pmgBatSmartEquipment.getCreateDept()));
            String l = null;
            if (StringUtil.isNotBlank(face_name)) {
                l = face_name.substring(face_name.length() - 1);
            }
            if ("1".equals(l)) {
                PmgBatParkPeople pmgBatParkPeople = pmgBatParkPeopleService.getById(face_name);
                if (pmgBatParkPeople.getId() != null) {
                    // 发送到stomp
                    pmgLogPeopleCount.setName(pmgBatParkPeople.getName());
                    pmgLogPeopleCount.setTemperature(null);
                    pmgLogPeopleCount.setFacePhotoSrc(capture_face_url);
                    pmgLogPeopleCount.setTimeStamp(Long.valueOf(time));
                    pmgLogPeopleCount.setScore(null);
                    pmgLogPeopleCount.setPersonId(face_name);
                    pmgLogPeopleCount.setSex(pmgBatParkPeople.getSex());
                    // 20210729 修改
                    pmgLogPeopleCount.setBuildingId(pmgBatParkPeople.getBuildingId());
                    pmgLogPeopleCount.setFloorId(pmgBatParkPeople.getFloorId());
                    pmgLogPeopleCount.setRoomId(pmgBatParkPeople.getRoomId());

                    pmgLogPeopleCount.setImgSrc(pmgBatParkPeople.getImgsrc());
                    pmgLogPeopleCount.setCompany(pmgBatParkPeople.getCompany());
                    pmgLogPeopleCount.setType(pmgBatParkPeople.getType());
                    pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                    pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                    pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                    pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                    pmgLogPeopleCount.setCaptureBkgUrl(capture_bkg_url);
                    pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                    pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
                    pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
                    pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogPeopleCount)));
                    // 保存到mongo
                    videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                            pmgLogPeopleCount.getParkId().toString());

                    iPeopleCountService.save(pmgLogPeopleCount);
                } else if (pmgBatParkPeople.getId() == null) {
                    strangerRHandler(pmgLogPeopleCount, faceR);
                }
            } else if ("2".equals(l)) {

                PmgBatParkVisitor pmgBatParkVisitor = pmgBatParkVisitorService.getById(Long.valueOf(face_name));
                if (pmgBatParkVisitor.getId() != null) {

                    pmgLogPeopleCount.setName(pmgBatParkVisitor.getName());
                    pmgLogPeopleCount.setTemperature(null);
                    pmgLogPeopleCount.setFacePhotoSrc(capture_face_url);
                    pmgLogPeopleCount.setTimeStamp(Long.valueOf(time));
                    pmgLogPeopleCount.setScore(null);
                    pmgLogPeopleCount.setPersonId(face_name);
                    pmgLogPeopleCount.setSex("1");
                    // 20210729 修改
                    pmgLogPeopleCount.setBuildingId(pmgBatParkVisitor.getBuildingId());
                    pmgLogPeopleCount.setFloorId(pmgBatParkVisitor.getFloorId());
//                    pmgLogPeopleCount.setRoomId(pmgBatParkVisitor.getRoomId());

//                    pmgLogPeopleCount.setCode(pmgBatParkVisitor.getCode());
                    pmgLogPeopleCount.setImgSrc(pmgBatParkVisitor.getFaceImg());
//                    pmgLogPeopleCount.setCompany(pmgBatParkVisitor.getCompany());
//                    pmgLogPeopleCount.setType(pmgBatParkVisitor.getType());
                    pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
                    pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
                    pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
                    pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
                    pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
                    pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
                    pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
                    // 发送到stomp
                    pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()),
                            JSON.toJSONString(pmgLogPeopleCount)));
                    // 保存到mongo
                    videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                            pmgLogPeopleCount.getParkId().toString());
                    iPeopleCountService.save(pmgLogPeopleCount);
                } else if (pmgBatParkVisitor.getId() == null) {
                    strangerRHandler(pmgLogPeopleCount, faceR);
                }
            }
            // 推送人员统计数据
            stompClient.pushPersonData(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 入园小时分布
            stompClient.pushEnterParkPeopleCount(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 已处理未处理 、 总异常事件
            stompClient.getWarningHandlerResult(String.valueOf(pmgBatSmartEquipment.getParkId()));
        } else if (ioTEvent.getIdentifier().equals("stranger_face")) {
            // --------------陌生人识别事件--------------
            PmgLogPeopleCount pmgLogPeopleCount = new PmgLogPeopleCount();
            // 20210123 update url
            // String capture_face_url = (String) ioTEvent.getOut().get("capture_face_url");
            String capture_face_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_face_url")), "UTF-8");
            // String capture_bkg_url = (String) ioTEvent.getOut().get("capture_bkg_url");
            String capture_bkg_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_bkg_url")), "UTF-8");
            String time = ioTEvent.getDeviceTime() + "000";
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("temperature", null);
            jsonObject1.put("facePhotoSrc", capture_face_url);
            jsonObject1.put("captureBkgUrl", capture_bkg_url);
            jsonObject1.put("timeStamp", Long.valueOf(time));
            jsonObject1.put("score", "");
            jsonObject1.put("peopleId", "");
            jsonObject1.put("sex", "");
            jsonObject1.put("building", "");
            jsonObject1.put("buildingName", "");
            jsonObject1.put("imgSrc", null);
            jsonObject1.put("company", null);
            jsonObject1.put("code", "10003");
            jsonObject1.put("type", "陌生人");
            jsonObject1.put("name", "");
            jsonObject1.put("equipmentName", pmgBatSmartEquipment.getName());
            jsonObject1.put("setPosition", pmgBatSmartEquipment.getSetPosition());
            // 20210729修改
            jsonObject1.put("buildingId", pmgBatSmartEquipment.getBuildingId());
            jsonObject1.put("floorId", pmgBatSmartEquipment.getFloorId());
            jsonObject1.put("roomId", pmgBatSmartEquipment.getRoomId());


            pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(jsonObject1)));

            pmgLogPeopleCount.setFacePhotoSrc(capture_face_url);
            pmgLogPeopleCount.setName("陌生人");
            pmgLogPeopleCount.setCode("10003");
            pmgLogPeopleCount.setType("陌生人");
            pmgLogPeopleCount.setPersonId("");
            pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            pmgLogPeopleCount.setTimeStamp(Long.valueOf(time));
            pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            // 20210729修改
            pmgLogPeopleCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
            pmgLogPeopleCount.setFloorId(pmgBatSmartEquipment.getFloorId());
            pmgLogPeopleCount.setRoomId(pmgBatSmartEquipment.getRoomId());

            pmgLogPeopleCount.setCaptureBkgUrl(capture_bkg_url);
            pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());

            // 保存到mongo
            videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                    pmgLogPeopleCount.getParkId().toString());

            iPeopleCountService.save(pmgLogPeopleCount);

            // 入园小时分布
            stompClient.pushEnterParkPeopleCount(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 已处理未处理 、 总异常事件
            stompClient.getWarningHandlerResult(String.valueOf(pmgBatSmartEquipment.getParkId()));
        } else if (ioTEvent.getIdentifier().equals("leave_area")
                || ioTEvent.getIdentifier().equals("move_quickly")
                || ioTEvent.getIdentifier().equals("cross_border_detection")
                || ioTEvent.getIdentifier().equals("wandering_detection")) {
            // --------------离开区域事件--------------
            // --------------快速移动事件--------------
            // --------------越界侦测事件--------------
            // --------------徘徊侦测事件--------------
            // 背景图URL
            String imageUrl = (String) ioTEvent.getOut().get("image_url");
            if (StringUtils.isNotBlank(imageUrl)) {
                imageUrl = imageUrl.replace(hikConfig.getOrigin(), hikConfig.getDestination());
            }
            // 触发时间
            String dateTime = (String) ioTEvent.getOut().get("date_time") + "000";

            // 封装 - 调用接口保存
            PmgLogVideoEventsCount pmgLogVideoEventsCount = new PmgLogVideoEventsCount();
            pmgLogVideoEventsCount.setCaptureBkgUrl(imageUrl);
            pmgLogVideoEventsCount.setTimeStamp(Long.valueOf(dateTime));
            VideoEventsCodeTypeVo codeTypeVo = getCodeTypeByIdentify(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setCode(codeTypeVo.getCode());
            pmgLogVideoEventsCount.setType(codeTypeVo.getType());
            pmgLogVideoEventsCount.setIdentity(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogVideoEventsCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogVideoEventsCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogVideoEventsCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            pmgLogVideoEventsCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogVideoEventsCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            pmgLogVideoEventsCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());

            // 20210729修改
            pmgLogVideoEventsCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
            pmgLogVideoEventsCount.setFloorId(pmgBatSmartEquipment.getFloorId());
            pmgLogVideoEventsCount.setRoomId(pmgBatSmartEquipment.getRoomId());

            pmgLogVideoEventsCount.setCameraIndexCode(String.valueOf(ioTEvent.getOut().get("camera_index_code")));
            pmgLogVideoEventsCount.setChannelName(String.valueOf(ioTEvent.getOut().get("channel_name")));
            // 前端推送
            pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogVideoEventsCount)));
            // 保存到mongo
            videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogVideoEventsCount)),
                    pmgLogVideoEventsCount.getParkId().toString());
            // 保存到数据库
            pmgLogVideoEventsCountService.save(pmgLogVideoEventsCount);
        } else if (ioTEvent.getIdentifier().equals("people_gathering")) {
            // --------------人员聚集事件--------------
            // 背景图URL
            String imageUrl = (String) ioTEvent.getOut().get("image_url");
            if (StringUtils.isNotBlank(imageUrl)) {
                imageUrl = imageUrl.replace(hikConfig.getOrigin(), hikConfig.getDestination());
            }
            // 触发时间
            String dateTime = (String) ioTEvent.getOut().get("date_time") + "000";
            // 密度比例
            Float density = (Float) ioTEvent.getOut().get("density");

            // 封装 - 调用接口保存
            PmgLogVideoEventsCount pmgLogVideoEventsCount = new PmgLogVideoEventsCount();
            pmgLogVideoEventsCount.setCaptureBkgUrl(imageUrl);
            pmgLogVideoEventsCount.setTimeStamp(Long.valueOf(dateTime));
            pmgLogVideoEventsCount.setDensity(density);
            VideoEventsCodeTypeVo codeTypeVo = getCodeTypeByIdentify(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setCode(codeTypeVo.getCode());
            pmgLogVideoEventsCount.setType(codeTypeVo.getType());
            pmgLogVideoEventsCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogVideoEventsCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogVideoEventsCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogVideoEventsCount.setIdentity(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogVideoEventsCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            pmgLogVideoEventsCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            pmgLogVideoEventsCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
            // 20210729修改
            pmgLogVideoEventsCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
            pmgLogVideoEventsCount.setFloorId(pmgBatSmartEquipment.getFloorId());
            pmgLogVideoEventsCount.setRoomId(pmgBatSmartEquipment.getRoomId());

            pmgLogVideoEventsCount.setCameraIndexCode(String.valueOf(ioTEvent.getOut().get("camera_index_code")));
            pmgLogVideoEventsCount.setChannelName(String.valueOf(ioTEvent.getOut().get("channel_name")));
            // 推送到前端
            pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogVideoEventsCount)));
            // 保存到mongo
            videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogVideoEventsCount)),
                    pmgLogVideoEventsCount.getParkId().toString());
            // 保存到数据库
            pmgLogVideoEventsCountService.save(pmgLogVideoEventsCount);
        } else if (ioTEvent.getIdentifier().equals("strenuous_exercise")
                || ioTEvent.getIdentifier().equals("abnormal_spacing")
                || ioTEvent.getIdentifier().equals("fall_to_the_ground")
                || ioTEvent.getIdentifier().equals("leaving")) {
            // --------------剧烈运动事件--------------
            // --------------间距运动事件--------------
            // --------------倒地事件--------------
            // --------------离岗事件--------------
            // 背景图URL
            String imageUrl = (String) ioTEvent.getOut().get("image_url");
            if (StringUtils.isNotBlank(imageUrl)) {
                imageUrl = imageUrl.replace(hikConfig.getOrigin(), hikConfig.getDestination());
            }
            // 触发时间
            String dateTime = (String) ioTEvent.getOut().get("date_time") + "000";
            // 封装 - 调用接口保存
            PmgLogVideoEventsCount pmgLogVideoEventsCount = new PmgLogVideoEventsCount();
            pmgLogVideoEventsCount.setCaptureBkgUrl(imageUrl);
            pmgLogVideoEventsCount.setTimeStamp(Long.valueOf(dateTime));
            VideoEventsCodeTypeVo codeTypeVo = getCodeTypeByIdentify(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setCode(codeTypeVo.getCode());
            pmgLogVideoEventsCount.setType(codeTypeVo.getType());
            pmgLogVideoEventsCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogVideoEventsCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogVideoEventsCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogVideoEventsCount.setIdentity(ioTEvent.getIdentifier());
            pmgLogVideoEventsCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogVideoEventsCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            pmgLogVideoEventsCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            pmgLogVideoEventsCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());
            // 20210729修改
            pmgLogVideoEventsCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
            pmgLogVideoEventsCount.setFloorId(pmgBatSmartEquipment.getFloorId());
            pmgLogVideoEventsCount.setRoomId(pmgBatSmartEquipment.getRoomId());

            pmgLogVideoEventsCount.setCameraIndexCode(String.valueOf(ioTEvent.getOut().get("camera_index_code")));
            pmgLogVideoEventsCount.setChannelName(String.valueOf(ioTEvent.getOut().get("channel_name")));
            // 推送到前端
            pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(pmgLogVideoEventsCount)));
            // 保存到mongo
            videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogVideoEventsCount)),
                    pmgBatSmartEquipment.getParkId().toString());
            // 保存到数据库
            pmgLogVideoEventsCountService.save(pmgLogVideoEventsCount);
        } else if (ioTEvent.getIdentifier().equals("high_frequency_personnel_identify")) { // 高频人员
            // --------------高频人员事件--------------
            PmgLogPeopleCount pmgLogPeopleCount = new PmgLogPeopleCount();
            // 20210123 update url
            // String capture_face_url = (String) ioTEvent.getOut().get("capture_face_url");
            String capture_face_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_face_url")), "UTF-8");
            // String capture_bkg_url = (String) ioTEvent.getOut().get("capture_bkg_url");
            String capture_bkg_url = URLDecoder.decode(String.valueOf(ioTEvent.getOut().get("capture_bkg_url")), "UTF-8");
            String time = ioTEvent.getDeviceTime() + "000";
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("temperature", null);
            jsonObject1.put("facePhotoSrc", capture_face_url);
            jsonObject1.put("timeStamp", Long.valueOf(time));
            jsonObject1.put("captureBkgUrl", capture_bkg_url);
            jsonObject1.put("score", "");
            jsonObject1.put("peopleId", "");
            jsonObject1.put("sex", "");
            jsonObject1.put("building", "");
            jsonObject1.put("buildingName", "");
            jsonObject1.put("imgSrc", null);
            jsonObject1.put("company", null);
            jsonObject1.put("code", "10004");
            jsonObject1.put("type", "高频人员");
            jsonObject1.put("equipmentName", pmgBatSmartEquipment.getName());
            jsonObject1.put("name", "");
            jsonObject1.put("parkId", pmgBatSmartEquipment.getParkId());
            jsonObject1.put("setPosition", pmgBatSmartEquipment.getSetPosition());
            // 20210729修改
            jsonObject1.put("buildingId", pmgBatSmartEquipment.getBuildingId());
            jsonObject1.put("floorId", pmgBatSmartEquipment.getFloorId());
            jsonObject1.put("roomId", pmgBatSmartEquipment.getRoomId());

            pushService.postFace(NoticeVo.getByParkIdAndData(String.valueOf(pmgBatSmartEquipment.getParkId()), JSON.toJSONString(jsonObject1)));
            // 保存到mongo
            videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                    pmgLogPeopleCount.getParkId().toString());

            pmgLogPeopleCount.setFacePhotoSrc(capture_face_url);
            pmgLogPeopleCount.setName("高频人员");
            pmgLogPeopleCount.setCode("10004");
            pmgLogPeopleCount.setType("高频人员");
            pmgLogPeopleCount.setPersonId("");
            pmgLogPeopleCount.setTimeStamp(Long.valueOf(time));

            pmgLogPeopleCount.setMask(String.valueOf(ioTEvent.getOut().get("mask"))); // 是否带口罩
            pmgLogPeopleCount.setGlass(String.valueOf(ioTEvent.getOut().get("glass"))); // 是否带眼睛
            pmgLogPeopleCount.setCaptureBkgUrl(capture_bkg_url);
            pmgLogPeopleCount.setProductKey(pmgBatSmartEquipment.getProductKey());
            pmgLogPeopleCount.setDeviceName(pmgBatSmartEquipment.getDeviceName());
            pmgLogPeopleCount.setEquipmentName(pmgBatSmartEquipment.getName());
            pmgLogPeopleCount.setParkId(pmgBatSmartEquipment.getParkId());
            pmgLogPeopleCount.setTenantId(pmgBatSmartEquipment.getTenantId());
            pmgLogPeopleCount.setSetPosition(pmgBatSmartEquipment.getSetPosition());
            // 20210729修改
            pmgLogPeopleCount.setBuildingId(pmgBatSmartEquipment.getBuildingId());
            pmgLogPeopleCount.setFloorId(pmgBatSmartEquipment.getFloorId());
            pmgLogPeopleCount.setRoomId(pmgBatSmartEquipment.getRoomId());
            pmgLogPeopleCount.setCreateDept(pmgBatSmartEquipment.getCreateDept());

            String indexCode = String.valueOf(ioTEvent.getOut().get("index_code"));
            pmgLogPeopleCount.setCameraIndexCode(indexCode);
            /********** 通过监控点获取监控点详情开始 ********/
            /*String cameraName = "";
            log.info("indexCode:" + indexCode);
            // 定义请求集合
            Map<String, Object> reqMap = new HashMap<String, Object>();
            // 封装获取监控点状态api请求参数
            reqMap.put("requestApi", HikApiUtil.GET_CAMERAS);
            // 定义请求体
            Map<String, Object> body = new HashMap<String, Object>();
            reqMap.put("body", body);
            log.info("reqMap:" + reqMap);
            R<Map<String, Object>> cameraData = commonHikClient.commonHikMethod(reqMap);
            // 判断返回参数是否不为空，并且返回编码为0(成功)
            if(Func.isNotEmpty(cameraData) && (0 == cameraData.getCode())) {
                Map<String, Object> cameraInfo = cameraData.getData();
                System.out.println("cameraName:" + cameraInfo.get("cameraName"));
                cameraName = String.valueOf(cameraInfo.get("cameraName"));
            }
            pmgLogPeopleCount.setCameraName(cameraName);*/
            /********** 通过监控点获取监控点详情结束********/
            iPeopleCountService.save(pmgLogPeopleCount);
            // 入园小时分布
            stompClient.pushEnterParkPeopleCount(String.valueOf(pmgBatSmartEquipment.getParkId()));
            // 已处理未处理 、 总异常事件
            stompClient.getWarningHandlerResult(String.valueOf(pmgBatSmartEquipment.getParkId()));
        }
    }

    private void strangerHandler(PmgLogPeopleCount pmgLogPeopleCount, Map<String, String> map) {

        // 发送到stomp
        pmgLogPeopleCount.setName("陌生人");
        pmgLogPeopleCount.setTemperature(Float.valueOf(map.get("temperature")));
        pmgLogPeopleCount.setFacePhotoSrc(map.get("facePhotoSrc"));
        pmgLogPeopleCount.setTimeStamp(Long.valueOf(map.get("time")));
        pmgLogPeopleCount.setScore(Integer.valueOf(map.get("score")));
        pmgLogPeopleCount.setPersonId("");
        pmgLogPeopleCount.setSex("");
        // 20210729修改
        pmgLogPeopleCount.setBuildingId(Long.valueOf(map.get("buildingId")));
        pmgLogPeopleCount.setFloorId(Long.valueOf(map.get("floorId")));
        pmgLogPeopleCount.setRoomId(Long.valueOf(map.get("roomId")));

        pmgLogPeopleCount.setCode("10003");
        pmgLogPeopleCount.setImgSrc(null);
        pmgLogPeopleCount.setCompany(Long.valueOf(0));
        pmgLogPeopleCount.setType("陌生人");
        pmgLogPeopleCount.setTenantId(map.get("tenantId"));
        pmgLogPeopleCount.setEquipmentName(map.get("equipmentName"));
        pmgLogPeopleCount.setParkId(Long.valueOf(map.get("parkId")));
        pmgLogPeopleCount.setSetPosition(map.get("setPosition"));
        pmgLogPeopleCount.setCreateDept(Long.valueOf(map.get("createDept")));
//        JSONObject jsonObject1  = new JSONObject();
//        jsonObject1.put("temperature",map.get("temperature"));
//        jsonObject1.put("facePhotoSrc",map.get("facePhotoSrc"));
//        jsonObject1.put("timeStamp",String.valueOf(map.get("time")));
//        jsonObject1.put("score",map.get("score"));
//        jsonObject1.put("peopleId",map.get("peopleId"));
//        jsonObject1.put("sex","");
//        jsonObject1.put("building","");
//        jsonObject1.put("buildingName","");
//        jsonObject1.put("imgSrc","");
//        jsonObject1.put("company","");
//        jsonObject1.put("code","10003");
//        jsonObject1.put("type","陌生人");
//        jsonObject1.put("name","");

        try {
            File file = Base64Img.base64ToFile(map.get("facePhotoSrc"), "2.png");
            R<FaceUploadResponse> faceUploadResponseR = faceHandler.uploadFile(file);
            if (faceUploadResponseR.isSuccess()) {
                pmgLogPeopleCount.setFacePhotoSrc(faceUploadResponseR.getData().getFaceImageUrl());
                log.info(faceUploadResponseR.getData().getFaceImageUrl());
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        pushService.postFace(NoticeVo.getByParkIdAndData(map.get("parkId"), JSON.toJSONString(pmgLogPeopleCount)));
        // 保存到mongo
        videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                pmgLogPeopleCount.getParkId().toString());
        iPeopleCountService.save(pmgLogPeopleCount);
    }

    private void strangerRHandler(PmgLogPeopleCount pmgLogPeopleCount, Map<String, String> map) {
        // 发送到stomp
        pmgLogPeopleCount.setName("陌生人");
        pmgLogPeopleCount.setTemperature(null);
        pmgLogPeopleCount.setFacePhotoSrc(map.get("capture_face_url"));
        pmgLogPeopleCount.setTimeStamp(Long.valueOf(map.get("time")));
        pmgLogPeopleCount.setScore(null);
        pmgLogPeopleCount.setPersonId("");
        pmgLogPeopleCount.setSex("");
        // 20210729修改
        pmgLogPeopleCount.setBuildingId(Long.valueOf(map.get("buildingId")));
        pmgLogPeopleCount.setFloorId(Long.valueOf(map.get("floorId")));
        pmgLogPeopleCount.setRoomId(Long.valueOf(map.get("roomId")));

        pmgLogPeopleCount.setCode("10003");
        pmgLogPeopleCount.setImgSrc(null);
        pmgLogPeopleCount.setCompany(Long.valueOf(0));
        pmgLogPeopleCount.setType("陌生人");
        pmgLogPeopleCount.setEquipmentName(map.get("equipmentName"));
        pmgLogPeopleCount.setTenantId(map.get("tenantId"));
        pmgLogPeopleCount.setSetPosition(map.get("setPosition"));
        pmgLogPeopleCount.setParkId(Long.valueOf(map.get("parkId")));
        pushService.postFace(NoticeVo.getByParkIdAndData(map.get("parkId"), JSON.toJSONString(pmgLogPeopleCount)));
        // 保存到mongo
        videoEventsUtils.savePersonVideoEvents(JSON.parseObject(JSON.toJSONString(pmgLogPeopleCount)),
                pmgLogPeopleCount.getParkId().toString());
        iPeopleCountService.save(pmgLogPeopleCount);
    }
}
