package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
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.service.DeviceService;
import com.mortals.xhx.module.face.service.FaceGroupPersonService;
import com.mortals.xhx.module.face.service.FaceGroupService;
import com.mortals.xhx.module.face.service.FacePlanService;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.hik.face.service.IHikPlanService;
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;

/**
 * 创建每日预约人群，并添加的预约人脸组中
 * 执行时间需晚于预约人群同步时间
 * @author:
 * @date: 2023/5/9 13:44
 */
@Slf4j
//@Service("CreateAppointPersonToHikBlackPlanByDay")
public class CreateAppointPersonToHikBlackPlanByDayTaskImpl implements ITaskExcuteService {

    @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 void excuteTask(ITask task) throws AppException {

        log.info("创建每日预约人群任务");
        facePlanService.createAppointmentPersonByDay();
        log.info("创建每日预约人群完成");
    }


    private void createAppointmentPersonByDay() {

        facePlanService.createAppointmentPersonByDay();

     /*   PlanRecognListReq planRecognBlackListReq = new PlanRecognListReq();
        Rest<List<PlanRecognInfo>> planRecognBlackRest = hikPlanService.findPlanRecognBlackList(planRecognBlackListReq);
        if (planRecognBlackRest.getCode() == YesNoEnum.YES.getValue()) {
            List<PlanRecognInfo> delPlanList = planRecognBlackRest.getData().stream().map(i -> {
                if (StrUtil.startWith(i.getName(), Constant.APPOINTMENT_PLAN_PREFIX) && !i.getName().equals(Constant.APPOINTMENT_GROUP_PREFIX + DateUtils.getCurrStrDate())) {
                    return i;
                }
                return null;
            }).filter(f -> f != null).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(delPlanList)) {
                //删除计划
                PlanRecognReq planRecognBlackReq = new PlanRecognReq();
                planRecognBlackReq.setRecognitionResourceIndexCodes(delPlanList.stream().map(i -> i.getIndexCode()).collect(Collectors.toList()));
                Rest<Boolean> delRest = hikPlanService.planRecognBlackDel(planRecognBlackReq);
                if (delRest.getCode() == YesNoEnum.YES.getValue() && delRest.getData()) {
                    log.info("预约计划删除成功！");
                }
            }
        }
        log.info("每日创建预约人员监控计划");
        //获取当天预约
        AppointmentPersonQuery appointmentPersonQuery = new AppointmentPersonQuery();
        appointmentPersonQuery.setAppointmentStartTimeStart(DateUtils.getCurrStrDate());
        appointmentPersonQuery.setAppointmentStartTimeEnd(DateUtils.getCurrStrDate());
        List<AppointmentPersonEntity> appointmentPersonEntities = appointmentPersonService.find(appointmentPersonQuery);
        if (!ObjectUtils.isEmpty(appointmentPersonEntities)) {
            //创建人脸分组
            String group = Constant.APPOINTMENT_GROUP_PREFIX + DateUtils.getCurrStrDate();
            FaceGroupEntity faceGroup = faceGroupService.selectOne(new FaceGroupQuery().name(group));
            if (ObjectUtils.isEmpty(faceGroup)) {
                //创建分组
                log.info("创建人脸分组...");
                FaceGroupReq faceGroupReq = new FaceGroupReq();
                faceGroupReq.setName(group);
                faceGroupReq.setDescription(group);
                Rest<FaceGroupEntity> rest = faceGroupService.saveFaceGroupToHik(faceGroupReq);
                if (rest.getCode() == YesNoEnum.YES.getValue()) {
                    FaceGroupEntity newFaceGroupEntity = rest.getData();
                    //创建人脸tohik
                    for (AppointmentPersonEntity appointmentPersonEntity : appointmentPersonEntities) {
                        PersonEntity personEntity = personService.getCache(appointmentPersonEntity.getPersonId().toString());
                        if (ObjectUtils.isEmpty(personEntity)) {
                            log.info("注册人员未空，persionId:{}", appointmentPersonEntity.getPersonId());
                            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(newFaceGroupEntity.getId());
                                    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());
                        }
                    }

                    log.info("添加当天预约监控计划");
                    String appointPlanName = Constant.APPOINTMENT_GROUP_PREFIX + DateUtils.getCurrStrDate();
                    int threshold = GlobalSysInfo.getParamIntValue(ParamKey.PARAM_FACE_THRESHOLD, 80);
                    //初始化新增取号监控识别计划
                    PlanRecognReq planRecognBlackReq = new PlanRecognReq();
                    planRecognBlackReq.setName(appointPlanName);
                    planRecognBlackReq.setDescription(appointPlanName);
                    planRecognBlackReq.setThreshold(threshold);
                    //设置人脸分组
                    List<String> faceGroupIndexCodes = new ArrayList<>();
                    faceGroupIndexCodes.add(newFaceGroupEntity.getIndexCode());
                    planRecognBlackReq.setFaceGroupIndexCodes(faceGroupIndexCodes);
                    //设置监控点
                    List<String> cameraIndexCodes = deviceService.find(new DeviceQuery()).stream().map(DeviceEntity::getDeviceCode).collect(Collectors.toList());
                    planRecognBlackReq.setCameraIndexCodes(cameraIndexCodes);
                    //设置识别方式
                    planRecognBlackReq.setRecognitionResourceType(RecognitionResourceEnum.FACE_RECOGNITION_SERVER.getValue());
                    //设置时间段 默认全天候
                    AppointmentConfigEntity appointmentConfigEntity = appointmentConfigService.selectOne(new AppointmentConfigQuery());
                    if (!ObjectUtils.isEmpty(appointmentConfigEntity)) {
                        List<AppointmentConfigTimesEntity> timesEntities = appointmentConfigTimesService.find(new AppointmentConfigTimesQuery().aotoCheckCfgId(appointmentConfigEntity.getId()));
                        if (!ObjectUtils.isEmpty(timesEntities)) {
                            List<TimeBlockListItem> timeBlockList = new ArrayList<>();

                            TimeBlockListItem timeBlockListItem = new TimeBlockListItem();
                            //周一至周日
                            timeBlockListItem.setDayOfWeek("1-7");
                            List<TimeRangeItem> timeRange = new ArrayList<>();
                            for (AppointmentConfigTimesEntity timesEntity : timesEntities) {
                                TimeRangeItem timeRangeItem = new TimeRangeItem();
                                String startTimeStr = DateUtils.convertTime2Str(timesEntity.getServiceTimeStart().getTime(), "HH:ss");
                                String endTimeStr = DateUtils.convertTime2Str(timesEntity.getServiceTimeEnd().getTime(), "HH:ss");
                                timeRangeItem.setStartTime(startTimeStr);
                                timeRangeItem.setEndTime(endTimeStr);
                                timeRange.add(timeRangeItem);
                            }
                            timeBlockListItem.setTimeRange(timeRange);
                            timeBlockList.add(timeBlockListItem);
                            planRecognBlackReq.setTimeBlockList(timeBlockList);
                        }
                    }

                    Rest<String> planRecognBlackAddRest = hikPlanService.planRecognBlackAdd(planRecognBlackReq);
                    if (planRecognBlackAddRest.getCode() == YesNoEnum.YES.getValue()) {
                        FacePlanEntity planEntity = new FacePlanEntity();
                        planEntity.initAttrValue();
                        planEntity.setIndexCode(planRecognBlackAddRest.getData());
                        planEntity.setName(appointPlanName);
                        planEntity.setDescription(appointPlanName);
                        planEntity.setCreateTime(new Date());
                        planEntity.setCreateUserId(1L);
                        planEntity.setCreateUserName("admin");
                        facePlanService.save(planEntity);
                    }
                } else {
                    log.info("人员分组创建失败=>{}", rest.getMsg());
                }
            } else {
                log.info("当天人员分组以存在，人员分组名称=>{}", group);
            }
        } else {
            log.info("当天无预约人员，不创建监控计划=>{}", DateUtils.getCurrStrDate());
        }*/
    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }


}
