package com.mortals.xhx.module.face.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.module.appointment.model.*;
import com.mortals.xhx.module.appointment.service.AppointmentConfigService;
import com.mortals.xhx.module.appointment.service.AppointmentConfigTimesService;
import com.mortals.xhx.module.appointment.service.AppointmentPersonService;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.face.model.*;
import com.mortals.xhx.module.face.service.FaceGroupPersonService;
import com.mortals.xhx.module.face.service.FaceGroupService;
import com.mortals.xhx.module.hik.face.model.req.face.FaceInfo;
import com.mortals.xhx.module.hik.face.model.req.face.FacePic;
import com.mortals.xhx.module.hik.face.model.req.face.FaceReq;
import com.mortals.xhx.module.hik.face.model.req.group.FaceGroupReq;
import com.mortals.xhx.module.hik.face.model.req.plan.PlanRecognListReq;
import com.mortals.xhx.module.hik.face.model.req.plan.PlanRecognReq;
import com.mortals.xhx.module.hik.face.model.req.plan.TimeBlockListItem;
import com.mortals.xhx.module.hik.face.model.req.plan.TimeRangeItem;
import com.mortals.xhx.module.hik.face.model.req.resource.ResourceReq;
import com.mortals.xhx.module.hik.face.model.rsp.face.FaceDataInfo;
import com.mortals.xhx.module.hik.face.model.rsp.plan.PlanRecognInfo;
import com.mortals.xhx.module.hik.face.model.rsp.resource.ResourceDataInfo;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.hik.face.service.IHikPlanService;
import com.mortals.xhx.module.person.model.PersonEntity;
import com.mortals.xhx.module.person.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.face.dao.FacePlanDao;
import com.mortals.xhx.module.face.service.FacePlanService;
import org.springframework.util.ObjectUtils;

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

import static com.mortals.xhx.common.key.Constant.*;
import static com.mortals.xhx.common.key.ParamKey.PARAM_FACE_GROUP_REGISTER;

/**
 * FacePlanService
 * 人员识别计划信息 service实现
 *
 * @author zxfei
 * @date 2023-04-15
 */
@Service("facePlanService")
@Slf4j
public class FacePlanServiceImpl extends AbstractCRUDServiceImpl<FacePlanDao, FacePlanEntity, Long> implements FacePlanService {


    @Autowired
    private PersonService personService;
    @Autowired
    private IHikFaceService hikFaceService;
    @Autowired
    private IHikPlanService hikPlanService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private FacePlanService facePlanService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private FaceGroupService faceGroupService;
    @Autowired
    private FaceGroupPersonService faceGroupPersonService;
    @Autowired
    private AppointmentPersonService appointmentPersonService;
    @Autowired
    private AppointmentConfigService appointmentConfigService;
    @Autowired
    private AppointmentConfigTimesService appointmentConfigTimesService;

    @Override
    public Rest<List<PlanRecognInfo>> createStrangerPlan() {
        //初始化创建陌生人计划，如果已经有了则不添加
        int threshold = GlobalSysInfo.getParamIntValue(ParamKey.PARAM_FACE_THRESHOLD, 80);
        //创建或者更新
        PlanRecognListReq planRecognListReq = new PlanRecognListReq();
        Rest<List<PlanRecognInfo>> planRecognWhiteListRest = hikPlanService.findPlanRecognWhiteList(planRecognListReq);
        if (planRecognWhiteListRest.getCode() == YesNoEnum.YES.getValue()) {
            PlanRecognInfo planRecognWhiteInfo = planRecognWhiteListRest.getData().stream().map(i -> {
                i.getName();
                if (i.getName().equals(Constant.STRANGER_PLAN)) {
                    return i;
                }
                return null;
            }).filter(f -> f != null).findFirst().orElseGet(() -> null);
            //初始化新增取号监控识别计划
            PlanRecognReq planRecognWhiteReq = new PlanRecognReq();
            planRecognWhiteReq.setName(Constant.STRANGER_PLAN);
            planRecognWhiteReq.setDescription(Constant.STRANGER_PLAN_DESC);
            planRecognWhiteReq.setThreshold(threshold);
            //设置人脸分组
            FaceGroupEntity faceGroupEntity = faceGroupService.selectOne(new FaceGroupQuery().name(PARAM_FACE_GROUP_REGISTER));
            if (ObjectUtils.isEmpty(faceGroupEntity)) {
                log.info("人脸组不存在！");
                return Rest.fail();
            }
            List<String> faceGroupIndexCodes = new ArrayList<>();
            faceGroupIndexCodes.add(faceGroupEntity.getIndexCode());
            planRecognWhiteReq.setFaceGroupIndexCodes(faceGroupIndexCodes);
            //设置监控点
            List<String> cameraIndexCodes = deviceService.find(new DeviceQuery()).stream().map(DeviceEntity::getDeviceCode).distinct().collect(Collectors.toList());
            planRecognWhiteReq.setCameraIndexCodes(cameraIndexCodes);
            //设置识别方式
            planRecognWhiteReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());

            ResourceReq resourceReq = new ResourceReq();
            resourceReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());
            Rest<List<ResourceDataInfo>> resourceRecognition = hikFaceService.getResourceRecognition(resourceReq);
            if (resourceRecognition.getCode() == YesNoEnum.YES.getValue() && !ObjectUtils.isEmpty(resourceRecognition.getData())) {
                ArrayList<String> recognitionResourceIndexCodes = new ArrayList<>();
                recognitionResourceIndexCodes.add(resourceRecognition.getData().get(0).getIndexCode());
                planRecognWhiteReq.setRecognitionResourceIndexCodes(recognitionResourceIndexCodes);
            }

            if (ObjectUtils.isEmpty(planRecognWhiteInfo)) {
                Rest<String> planRecognWhiteAddRest = hikPlanService.planRecognWhiteAdd(planRecognWhiteReq);
                if (planRecognWhiteAddRest.getCode() == YesNoEnum.YES.getValue()) {
                    FacePlanEntity planEntity = new FacePlanEntity();
                    planEntity.initAttrValue();
                    planEntity.setIndexCode(planRecognWhiteAddRest.getData());
                    planEntity.setFaceGroupIndexCodes(faceGroupEntity.getIndexCode());
                    planEntity.setCameraIndexCodes(planRecognWhiteReq.getCameraIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceIndexCodes(planRecognWhiteReq.getRecognitionResourceIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getDesc());
                    planEntity.setThreshold(threshold);
                    planEntity.setStatus(PlanStatusEnum.RUNNING.getValue());
                    planEntity.setStartTime(new Date());
                    planEntity.setName(Constant.STRANGER_PLAN);
                    planEntity.setDescription(Constant.STRANGER_PLAN_DESC);
                    planEntity.setCreateTime(new Date());
                    planEntity.setCreateUserId(1L);
                    planEntity.setCreateUserName("admin");
                    facePlanService.save(planEntity);
                }
            } else {
                //判断计划运行状态
             /*   if (PlanStatusEnum.FAIL.getValue().equals(planRecognWhiteInfo.getStatus())) {
                    //计划下发识别，重新下发当前计划
                    // facePlanService.re
                    PlanRecognReq planRecognReq = new PlanRecognReq();
                    planRecognReq.setIndexCode(planRecognWhiteInfo.getIndexCode());
                    Rest<Boolean> booleanRest = hikPlanService.planRecognWhiteReStart(planRecognReq);
                    if (YesNoEnum.YES.getValue() == booleanRest.getCode()) {
                        return Rest.ok(booleanRest.getMsg());
                    } else {
                        return Rest.fail(booleanRest.getMsg());
                    }
                } else if (PlanStatusEnum.RUNNING.getValue().equals(planRecognWhiteInfo.getStatus())) {
                    return Rest.ok(PlanStatusEnum.RUNNING.getDesc());
                }*/
                //更新计划
/*                planRecognWhiteReq.setIndexCode(planRecognWhiteInfo.getIndexCode());
                Rest<Boolean> planRecognWhiteUpdateRest = hikPlanService.planRecognWhiteUpdate(planRecognWhiteReq);
                if (planRecognWhiteUpdateRest.getCode() == YesNoEnum.YES.getValue() && planRecognWhiteUpdateRest.getData()) {
                    FacePlanEntity whitePlan = facePlanService.selectOne(new FacePlanQuery().indexCode(planRecognWhiteInfo.getIndexCode()));
                    if (!ObjectUtils.isEmpty(whitePlan)) {
                        whitePlan.setUpdateTime(new Date());
                        whitePlan.setUpdateUserId(1L);
                        whitePlan.setUpdateUserName("system");
                        facePlanService.update(whitePlan);
                    }

                }*/
            }
        }

        return planRecognWhiteListRest;
    }

    @Override
    public Rest<List<PlanRecognInfo>> createStrangerPlanByDay() {
        //初始化创建陌生人计划，如果已经有了则不添加
        int threshold = GlobalSysInfo.getParamIntValue(ParamKey.PARAM_FACE_THRESHOLD, 80);
        //创建或者更新
        PlanRecognListReq planRecognListReq = new PlanRecognListReq();
        Rest<List<PlanRecognInfo>> planRecognWhiteListRest = hikPlanService.findPlanRecognWhiteList(planRecognListReq);
        if (planRecognWhiteListRest.getCode() == YesNoEnum.YES.getValue()) {
            PlanRecognInfo planRecognWhiteInfo = planRecognWhiteListRest.getData().stream().map(i -> {
                i.getName();
                if (i.getName().equals(Constant.STRANGER_PLAN)) {
                    return i;
                }
                return null;
            }).filter(f -> f != null).findFirst().orElseGet(() -> null);
            //初始化新增取号监控识别计划
            PlanRecognReq planRecognWhiteReq = new PlanRecognReq();
            planRecognWhiteReq.setName(Constant.STRANGER_PLAN);
            planRecognWhiteReq.setDescription(Constant.STRANGER_PLAN_DESC);
            planRecognWhiteReq.setThreshold(threshold);
            //设置人脸分组
            FaceGroupEntity faceGroupEntity = faceGroupService.selectOne(new FaceGroupQuery().name(PARAM_FACE_GROUP_REGISTER));
            if (ObjectUtils.isEmpty(faceGroupEntity)) {
                log.info("人脸组不存在！");
                return Rest.fail();
            }
            List<String> faceGroupIndexCodes = new ArrayList<>();
            faceGroupIndexCodes.add(faceGroupEntity.getIndexCode());
            planRecognWhiteReq.setFaceGroupIndexCodes(faceGroupIndexCodes);
            //设置监控点
            List<String> cameraIndexCodes = deviceService.find(new DeviceQuery()).stream().map(DeviceEntity::getDeviceCode).distinct().collect(Collectors.toList());

//            ArrayList<String> temp = new ArrayList<>();
//            temp.add(cameraIndexCodes.get(2));
            planRecognWhiteReq.setCameraIndexCodes(cameraIndexCodes);
            //planRecognWhiteReq.setCameraIndexCodes(temp);
            //设置识别方式
            planRecognWhiteReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());

            ResourceReq resourceReq = new ResourceReq();
            resourceReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());
            Rest<List<ResourceDataInfo>> resourceRecognition = hikFaceService.getResourceRecognition(resourceReq);
            if (resourceRecognition.getCode() == YesNoEnum.YES.getValue() && !ObjectUtils.isEmpty(resourceRecognition.getData())) {
                ArrayList<String> recognitionResourceIndexCodes = new ArrayList<>();
                recognitionResourceIndexCodes.add(resourceRecognition.getData().get(0).getIndexCode());
                planRecognWhiteReq.setRecognitionResourceIndexCodes(recognitionResourceIndexCodes);
            }

            //设置时间段 默认全天候
/*            List<TimeBlockListItem> timeBlockListItemList=new ArrayList<>();
            TimeBlockListItem timeBlockListItem = new TimeBlockListItem();
            timeBlockListItem.setDayOfWeek("7");
            List<TimeRangeItem> timeRange=new ArrayList<>();
            TimeRangeItem timeRangeItem = new TimeRangeItem();
            timeRangeItem.setStartTime("06:00");
            timeRangeItem.setEndTime("18:00");
            timeRange.add(timeRangeItem);
            timeBlockListItem.setTimeRange(timeRange);
            timeBlockListItemList.add(timeBlockListItem);
            planRecognWhiteReq.setTimeBlockList(timeBlockListItemList);*/

            if (ObjectUtils.isEmpty(planRecognWhiteInfo)) {
                Rest<String> planRecognWhiteAddRest = hikPlanService.planRecognWhiteAdd(planRecognWhiteReq);
                if (planRecognWhiteAddRest.getCode() == YesNoEnum.YES.getValue()) {
                    FacePlanEntity planEntity = new FacePlanEntity();
                    planEntity.initAttrValue();
                    planEntity.setIndexCode(planRecognWhiteAddRest.getData());
                    planEntity.setFaceGroupIndexCodes(faceGroupEntity.getIndexCode());
                    planEntity.setCameraIndexCodes(planRecognWhiteReq.getCameraIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceIndexCodes(planRecognWhiteReq.getRecognitionResourceIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getDesc());
                    planEntity.setThreshold(threshold);
                    planEntity.setStatus(PlanStatusEnum.RUNNING.getValue());
                    planEntity.setStartTime(new Date());
                    planEntity.setName(Constant.STRANGER_PLAN);
                    planEntity.setDescription(Constant.STRANGER_PLAN_DESC);
                    planEntity.setCreateTime(new Date());
                    planEntity.setCreateUserId(1L);
                    planEntity.setCreateUserName("admin");
                    facePlanService.save(planEntity);
                }
            } else {
                //判断计划运行状态
             /*   if (PlanStatusEnum.FAIL.getValue().equals(planRecognWhiteInfo.getStatus())) {
                    //计划下发识别，重新下发当前计划
                    // facePlanService.re
                    PlanRecognReq planRecognReq = new PlanRecognReq();
                    planRecognReq.setIndexCode(planRecognWhiteInfo.getIndexCode());
                    Rest<Boolean> booleanRest = hikPlanService.planRecognWhiteReStart(planRecognReq);
                    if (YesNoEnum.YES.getValue() == booleanRest.getCode()) {
                        return Rest.ok(booleanRest.getMsg());
                    } else {
                        return Rest.fail(booleanRest.getMsg());
                    }
                } else if (PlanStatusEnum.RUNNING.getValue().equals(planRecognWhiteInfo.getStatus())) {
                    return Rest.ok(PlanStatusEnum.RUNNING.getDesc());
                }*/
                //更新计划
/*                planRecognWhiteReq.setIndexCode(planRecognWhiteInfo.getIndexCode());
                Rest<Boolean> planRecognWhiteUpdateRest = hikPlanService.planRecognWhiteUpdate(planRecognWhiteReq);
                if (planRecognWhiteUpdateRest.getCode() == YesNoEnum.YES.getValue() && planRecognWhiteUpdateRest.getData()) {
                    FacePlanEntity whitePlan = facePlanService.selectOne(new FacePlanQuery().indexCode(planRecognWhiteInfo.getIndexCode()));
                    if (!ObjectUtils.isEmpty(whitePlan)) {
                        whitePlan.setUpdateTime(new Date());
                        whitePlan.setUpdateUserId(1L);
                        whitePlan.setUpdateUserName("system");
                        facePlanService.update(whitePlan);
                    }

                }*/
            }
        }

        return planRecognWhiteListRest;

    }

    @Override
    public void createAppointmentPersonByDay() {
        //创建当前的预约人群人像 并同步到海康人脸库
        //获取当天预约
        AppointmentPersonQuery appointmentPersonQuery = new AppointmentPersonQuery();
        appointmentPersonQuery.setAppointmentStartTimeStart(DateUtils.getCurrStrDate());
        appointmentPersonQuery.setAppointmentStartTimeEnd(DateUtils.getCurrStrDate());
        List<AppointmentPersonEntity> appointmentPersonEntities = appointmentPersonService.find(appointmentPersonQuery);
        if (!ObjectUtils.isEmpty(appointmentPersonEntities)) {
            log.info("当天预约人数：{}", appointmentPersonEntities.size());
            //将人脸同步添加到海康人脸库中，如果已经添加过的 则不添加
            FaceGroupEntity faceGroup = faceGroupService.selectOne(new FaceGroupQuery().name(APPOINTMENT_GROUP));
            if (!ObjectUtils.isEmpty(faceGroup)) {
                //删除本地和远端人脸库，重新添加 todo
                List<FaceGroupPersonEntity> delFacePerson = faceGroupPersonService.find(new FaceGroupPersonQuery().faceGroupId(faceGroup.getId()));
                if (!ObjectUtils.isEmpty(delFacePerson)) {
                    FaceReq faceRequest = new FaceReq();
                    faceRequest.setFaceGroupIndexCode(faceGroup.getIndexCode());
                    faceRequest.setIndexCodes(delFacePerson.stream().map(i -> i.getIndexCode()).collect(Collectors.toList()));
                    Rest<Boolean> delRest = hikFaceService.faceSingleDel(faceRequest);
                    if (delRest.getCode() == YesNoEnum.YES.getValue() && delRest.getData()) {
                        faceGroupPersonService.remove(delFacePerson.stream().map(i -> i.getId()).toArray(Long[]::new), null);
                    } else {
                        log.info("人脸删除失败==》{}", JSON.toJSONString(delRest));
                        return;
                    }
                }

                //创建人脸tohik
                for (AppointmentPersonEntity appointmentPersonEntity : appointmentPersonEntities) {
                    PersonEntity personEntity = personService.getCache(appointmentPersonEntity.getIdCard());
                    if (ObjectUtils.isEmpty(personEntity)) {
                        log.info("注册人员不能为空，idcard:{}", appointmentPersonEntity.getIdCard());
                        continue;
                    }
                    //查询图片并转成base64
                    if (!ObjectUtils.isEmpty(personEntity.getPhoto())) {
                        String filePath = uploadService.getFilePath(personEntity.getPhoto());
                        byte[] bytes = FileUtil.readBytes(filePath);
                        if (bytes.length > 0) {
                            String picBase64Data = Base64.encode(bytes);
                            FaceReq faceReq = new FaceReq();
                            FaceInfo faceInfo = new FaceInfo();
                            faceInfo.setName(personEntity.getName());
                            faceInfo.setSex(personEntity.getGender() == GenderEnum.男.getValue() ? "1" : "2");
                            faceInfo.setCertificateType("111");
                            faceInfo.setCertificateNum(personEntity.getIdCard());
                            faceReq.setFaceInfo(faceInfo);
                            FacePic facePic = new FacePic();
                            facePic.setFaceBinaryData(picBase64Data);
                            faceReq.setFacePic(facePic);
                            faceReq.setFaceGroupIndexCode(faceGroup.getIndexCode());
                            Rest<FaceDataInfo> faceDataInfoRest = hikFaceService.faceSingleAdd(faceReq);
                            if (faceDataInfoRest.getCode() == YesNoEnum.YES.getValue()) {
                                //人脸添加成功，保存人脸组
                                FaceGroupPersonEntity faceGroupPersonEntity = new FaceGroupPersonEntity();
                                faceGroupPersonEntity.initAttrValue();
                                faceGroupPersonEntity.setFaceGroupId(faceGroup.getId());
                                faceGroupPersonEntity.setName(faceDataInfoRest.getData().getFaceInfo().getName());
                                faceGroupPersonEntity.setIndexCode(faceDataInfoRest.getData().getIndexCode());
                                faceGroupPersonEntity.setCreateTime(new Date());
                                faceGroupPersonEntity.setCreateUserId(1L);
                                faceGroupPersonService.save(faceGroupPersonEntity);
                            } else {
                                log.info("添加人脸异常=>{}", faceDataInfoRest.getMsg());
                            }
                        } else {
                            log.info("图片数据为空，filePath=>{}", filePath);
                        }
                    } else {
                        log.info("人员图片photo为空，person=>{}", personEntity.getName());
                    }
                }
            }

        }
    }

    @Override
    public Rest<String> createAppointmentPlan() {
        PlanRecognListReq planRecognBlackListReq = new PlanRecognListReq();
        planRecognBlackListReq.setName(APPOINTMENT_PLAN);
        Rest<List<PlanRecognInfo>> planRecognBlackRest = hikPlanService.findPlanRecognBlackList(planRecognBlackListReq);
        if (planRecognBlackRest.getCode() == YesNoEnum.YES.getValue()) {
            if (ObjectUtils.isEmpty(planRecognBlackRest.getData())) {
                //新建
                int threshold = GlobalSysInfo.getParamIntValue(ParamKey.PARAM_FACE_THRESHOLD, 80);

                //初始化新增取号监控识别计划
                PlanRecognReq planRecognBlackReq = new PlanRecognReq();
                planRecognBlackReq.setName(Constant.APPOINTMENT_PLAN);
                planRecognBlackReq.setDescription(Constant.APPOINTMENT_PLAN_DESC);
                planRecognBlackReq.setThreshold(threshold);
                //设置人脸分组
                FaceGroupEntity faceGroupEntity = faceGroupService.selectOne(new FaceGroupQuery().name(APPOINTMENT_GROUP));
                if (ObjectUtils.isEmpty(faceGroupEntity)) {
                    log.info("人脸组不存在！");
                    return Rest.fail();
                }
                List<String> faceGroupIndexCodes = new ArrayList<>();
                faceGroupIndexCodes.add(faceGroupEntity.getIndexCode());
                planRecognBlackReq.setFaceGroupIndexCodes(faceGroupIndexCodes);
                //设置监控点
                List<String> cameraIndexCodes = deviceService.find(new DeviceQuery()).stream().map(DeviceEntity::getDeviceCode).distinct().collect(Collectors.toList());

                planRecognBlackReq.setCameraIndexCodes(cameraIndexCodes);
                //设置识别方式
                planRecognBlackReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());

                ResourceReq resourceReq = new ResourceReq();
                resourceReq.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getValue());
                Rest<List<ResourceDataInfo>> resourceRecognition = hikFaceService.getResourceRecognition(resourceReq);
                if (resourceRecognition.getCode() == YesNoEnum.YES.getValue() && !ObjectUtils.isEmpty(resourceRecognition.getData())) {
                    ArrayList<String> recognitionResourceIndexCodes = new ArrayList<>();
                    recognitionResourceIndexCodes.add(resourceRecognition.getData().get(0).getIndexCode());
                    planRecognBlackReq.setRecognitionResourceIndexCodes(recognitionResourceIndexCodes);
                }


                Rest<String> planRecognBlackAddRest = hikPlanService.planRecognBlackAdd(planRecognBlackReq);
                if (planRecognBlackAddRest.getCode() == YesNoEnum.YES.getValue()) {
                    FacePlanEntity planEntity = new FacePlanEntity();
                    planEntity.initAttrValue();
                    planEntity.setIndexCode(planRecognBlackAddRest.getData());
                    planEntity.setFaceGroupIndexCodes(faceGroupEntity.getIndexCode());
                    planEntity.setCameraIndexCodes(planRecognBlackReq.getCameraIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceIndexCodes(planRecognBlackReq.getRecognitionResourceIndexCodes().stream().collect(Collectors.joining(",")));
                    planEntity.setRecognitionResourceType(RecognitionResourceEnum.SUPER_BRAIN.getDesc());
                    planEntity.setThreshold(threshold);
                    planEntity.setStatus(PlanStatusEnum.RUNNING.getValue());
                    planEntity.setStartTime(new Date());
                    planEntity.setName(APPOINTMENT_PLAN);
                    planEntity.setDescription(APPOINTMENT_PLAN_DESC);
                    planEntity.setCreateTime(new Date());
                    planEntity.setCreateUserId(1L);
                    planEntity.setCreateUserName("admin");

                    facePlanService.save(planEntity);
                } else {
                    log.info("预约监控计划创建失败=>{}", planRecognBlackAddRest.getMsg());
                }


            } else {
                log.info("预约监控计划已经存在=>{}", JSON.toJSONString(planRecognBlackRest.getData()));
                //判断状态 如果失败 则重新下发
            }

        } else {
            log.info("预约查找计划异常=>{}", planRecognBlackRest.getMsg());
        }
        return Rest.ok();
    }
}