package io.gmss.fiscad.implementations.synchronisation;

import io.gmss.fiscad.entities.decoupage.Commune;
import io.gmss.fiscad.entities.decoupage.Nationalite;
import io.gmss.fiscad.entities.decoupage.Quartier;
import io.gmss.fiscad.entities.infocad.metier.*;
import io.gmss.fiscad.entities.infocad.parametre.*;
import io.gmss.fiscad.entities.metadata.*;
import io.gmss.fiscad.entities.rfu.metier.*;
import io.gmss.fiscad.entities.rfu.parametre.Caracteristique;
import io.gmss.fiscad.entities.rfu.parametre.TypeCaracteristique;
import io.gmss.fiscad.entities.rfu.parametre.ZoneRfu;
import io.gmss.fiscad.entities.user.User;
import io.gmss.fiscad.enums.*;
import io.gmss.fiscad.interfaces.infocad.metier.ParcelleGeomService;
import io.gmss.fiscad.interfaces.synchronisation.SynchronisationService;
import io.gmss.fiscad.interfaces.user.UserService;
import io.gmss.fiscad.paylaods.request.*;
import io.gmss.fiscad.paylaods.response.*;
import io.gmss.fiscad.repositories.decoupage.*;
import io.gmss.fiscad.repositories.infocad.metier.*;
import io.gmss.fiscad.repositories.infocad.parametre.*;
import io.gmss.fiscad.repositories.metadata.*;
import io.gmss.fiscad.repositories.rfu.metier.*;
import io.gmss.fiscad.repositories.rfu.parametre.CaracteristiqueRepository;
import io.gmss.fiscad.repositories.rfu.parametre.TypeCaracteristiqueRepository;
import io.gmss.fiscad.repositories.rfu.parametre.ZoneRfuRepository;
import io.gmss.fiscad.repositories.user.UserRepository;
import io.gmss.fiscad.service.FileStorageService;
import io.gmss.fiscad.service.worker.*;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.ToDoubleBiFunction;

@Service
@RequiredArgsConstructor
public class SynchronisationServiceImpl implements SynchronisationService {
    private final UserService userService;
    private final DepartementRepository departementRepository;
    private final CommuneRepository communeRepository;
    private final ArrondissementRepository arrondissementRepository;
    private final QuartierRepository quartierRepository;
    private final BlocRepository blocRepository;
    private final ModeAcquisitionRepository modeAcquisitionRepository;
    private final NationaliteRepository nationaliteRepository;
    private final NatureDomaineRepository natureDomaineRepository;
    private final PositionRepresentationRepository positionRepresentationRepository;
    private final ProfessionRepository professionRepository;
    private final SituationMatrimonialeRepository situationMatrimonialeRepository;
    private final SourceDroitRepository sourceDroitRepository;
    private final TypeContestationRepository typeContestationRepository;
    private final TypeDomaineRepository typeDomaineRepository;
    private final TypePersonneRepository typePersonneRepository;
    private final TypePieceRepository typePieceRepository;
    private final TypeRepresentationRepository typeRepresentationRepository;
    private final EnqueteRepository enqueteRepository;
    private final SituationGeographiqueRepository situationGeographiqueRepository;
    private final FileStorageService fileStorageService;
    private final ZoneRfuRepository zoneRfuRepository;
    private final CaracteristiqueRepository caracteristiqueRepository;
    private final TypeCaracteristiqueRepository typeCaracteristiqueRepository;
    private final MobileDataEnqueteBatimentRepository mobileDataEnqueteBatimentRepository;
    private final MobileDataActeurConcerneRepository mobileDataActeurConcerneRepository;
    private final MobileDataPersonneRepository mobileDataPersonneRepository;
    private final MobileDataMembreGroupeRepository mobileDataMembreGroupeRepository;
    private final MobileDataPieceRepository mobileDataPieceRepository;
    private final MobileDataBatimentRepository mobileDataBatimentRepository;
    private final MobileDataUploadRepository mobileDataUploadRepository;
    private final MobileDataEnqueteRepository mobileDataEnqueteRepository;
    private final MobileDataCaracteristiqueParcelleRepository mobileDataCaracteristiqueParcelleRepository;
    private final MobileDataEnqueteUniteLogementRepository mobileDataEnqueteUniteLogementRepository;
    private final MobileDataCaracteristiqueBatimentRepository mobileDataCaracteristiqueBatimentRepository;
    private final MobileDataCaracteristiqueUniteLogementRepository mobileDataCaracteristiqueUniteLogementRepository;
    private final MobileDataUniteLogementRepository mobileDataUniteLogementRepository;
    private final MobileDataParcelleRepository mobileDataParcelleRepository;
    private final PersonneAsyncWorker personneAsyncWorker;
    private final MembreGroupeAsyncWorker membreGroupeAsyncWorker;
    private final ActeurConcerneAsyncWorker acteurConcerneAsyncWorker;
    private final BatimentAsyncWorker batimentAsyncWorker;
    private final CaracteristiqueBatimentAsyncWorker caracteristiqueBatimentAsyncWorker;
    private final CaracteristiqueParcelleAsyncWorker caracteristiqueParcelleAsyncWorker;
    private final CaracteristiqueUniteLogementAsyncWorker caracteristiqueUniteLogementAsyncWorker;
    private final EnqueteAsyncWorker enqueteAsyncWorker;
    private final EnqueteBatimentAsyncWorker enqueteBatimentAsyncWorker;
    private final EnqueteUniteLogementAsyncWorker enqueteUniteLogementAsyncWorker;
    private final ParcelleAsyncWorker parcelleAsyncWorker;
    private final PieceAsyncWorker pieceAsyncWorker;
    private final UniteLogementAsyncWorker uniteLogementAsyncWorker;
    private final UploadAsyncWorker uploadAsyncWorker;
    @Value("${io.gmss.fiscad.profile}")
    private String profile;


    @Override
    public UserDecoupageSyncResponses getDecoupageAdminUserConnecter() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = authentication.getName();

        User user = userService.getUserByUsername(userName);
        List<DepartementSyncResponse> departementSyncResponses =
                departementRepository.getDepartementResponse();

        List<CommuneSyncResponse> communeSyncResponses =
                communeRepository.getCommuneResponse();

        List<ArrondissementSyncResponse> arrondissementSyncResponses =
                arrondissementRepository.getArrondissementResponse(user.getStructure().getId());

        List<QuartierSyncResponse> quartierSyncResponses =
                quartierRepository.getQuartierResponse(user.getStructure().getId());

        List<BlocSyncResponse> blocSyncResponses = new ArrayList<>();
        System.out.println(profile);
        if (profile.equals(ProfileApp.ABOMEY.toString().toLowerCase())) {
            if (user.getStructure() != null) {
                System.out.println(user.getStructure().getId());
                blocSyncResponses =
                        blocRepository.getBlocResponse(user.getStructure().getId());
            }
        }

        if (profile.equals(ProfileApp.DGI.toString().toLowerCase())) {
            System.out.println("DGI");
            if (user.getIdSecteurCourant() != null) {
                blocSyncResponses =
                        blocRepository.getBlocSecteurResponse(user.getStructure().getId(), user.getIdSecteurCourant());
            }
        }

        UserDecoupageSyncResponses userDecoupageSyncResponses = new UserDecoupageSyncResponses();
        userDecoupageSyncResponses.setDepartementSyncResponses(departementSyncResponses);
        userDecoupageSyncResponses.setCommuneSyncResponses(communeSyncResponses);
        userDecoupageSyncResponses.setArrondissementSyncResponses(arrondissementSyncResponses);
        userDecoupageSyncResponses.setQuartierSyncResponses(quartierSyncResponses);
        userDecoupageSyncResponses.setBlocSyncResponses(blocSyncResponses);
        return userDecoupageSyncResponses;
    }

    @Override
    public ReferencesSyncResponses getReferencesSyncResponses() {
        ReferencesSyncResponses referencesSyncResponses = new ReferencesSyncResponses();
        List<TypeRepresentation> typeRepresentationSyncResponses = typeRepresentationRepository.findAll();
        List<TypePiece> typePieceSyncResponses = typePieceRepository.findAll();
        List<TypePersonne> typePersonneSyncResponses = typePersonneRepository.findAll();
        List<TypeDomaine> typeDomaineSyncResponses = typeDomaineRepository.findAll();
        List<TypeContestation> typeContestationSyncResponses = typeContestationRepository.findAll();
        List<SourceDroitsSyncResponse> sourceDroitSyncResponses = sourceDroitRepository.getSourceDroitSyncResponse();
        List<SituationMatrimoniale> situationMatrimonialeSyncResponses = situationMatrimonialeRepository.findAll();
        List<Profession> professionSyncResponses = professionRepository.findAll();
        List<PositionRepresentation> positionRepresentationSyncResponses = positionRepresentationRepository.findAll();
        List<NatureDomaineSyncResponse> natureDomaineSyncResponses = natureDomaineRepository.getNatureDomaineResponse();
        List<Nationalite> nationaliteSyncResponses = nationaliteRepository.findAll();
        List<ModeAcquisition> modeAcquisitionSyncResponses = modeAcquisitionRepository.findAll();
        List<ModesAcquisitionSourceSyncResponse> modeAcquisitionSourceSyncResponses = sourceDroitRepository.getModeAcquitionSourceSyncResponse();
        List<SituationGeographique> situationGeographiques = situationGeographiqueRepository.findAll();
        List<TypePersonneNatureDomaineSyncResponse> typePersonneNatureDomaineSyncResponses = natureDomaineRepository.getTypePersonneNatureDomaineSyncResponse();
        List<ModesAcquisitionTypePersonneSyncResponse> modesAcquisitionTypePersonneSyncResponses = modeAcquisitionRepository.getModeAcquisitionTypePersonne();
        List<Caracteristique> caracteristiquesSyncResponses = caracteristiqueRepository.findAllByActifIsTrue();
        List<TypeCaracteristique> typeCaracteristiquesSyncResponses = typeCaracteristiqueRepository.findAllByActifIsTrue();
        List<ZoneRfuSyncResponse> zoneRfuSyncResponses = zoneRfuRepository.getZoneRfuResponse();


        referencesSyncResponses.setNatureDomaineSyncResponses(natureDomaineSyncResponses);
        referencesSyncResponses.setNationaliteSyncResponses(nationaliteSyncResponses);
        referencesSyncResponses.setModeAcquisitionSyncResponses(modeAcquisitionSyncResponses);
        referencesSyncResponses.setProfessionSyncResponses(professionSyncResponses);
        referencesSyncResponses.setSituationMatrimonialeSyncResponses(situationMatrimonialeSyncResponses);
        referencesSyncResponses.setPositionRepresentationSyncResponses(positionRepresentationSyncResponses);
        referencesSyncResponses.setSourceDroitSyncResponses(sourceDroitSyncResponses);
        //referencesSyncResponses.setStructureSyncResponses(structureSyncResponses);
        referencesSyncResponses.setTypeDomaineSyncResponses(typeDomaineSyncResponses);
        referencesSyncResponses.setTypePersonneSyncResponses(typePersonneSyncResponses);
        referencesSyncResponses.setTypeContestationSyncResponses(typeContestationSyncResponses);
        referencesSyncResponses.setTypePieceSyncResponses(typePieceSyncResponses);
        referencesSyncResponses.setTypeRepresentationSyncResponses(typeRepresentationSyncResponses);
        referencesSyncResponses.setSituationSyncGeographiques(situationGeographiques);
        referencesSyncResponses.setModesAcquisitionSourceSyncResponses(modeAcquisitionSourceSyncResponses);
        referencesSyncResponses.setTypePersonneNatureDomaineSyncResponses(typePersonneNatureDomaineSyncResponses);
        referencesSyncResponses.setModesAcquisitionTypePersonneSyncResponses(modesAcquisitionTypePersonneSyncResponses);

        referencesSyncResponses.setCaracteristiquesSyncResponses(caracteristiquesSyncResponses);
        referencesSyncResponses.setTypeCaracteristiquesSyncResponses(typeCaracteristiquesSyncResponses);
        referencesSyncResponses.setZoneRfuSyncResponses(zoneRfuSyncResponses);

        return referencesSyncResponses;
    }

    @Override
    public SyncEnqueteAllDataResponse syncEnqueteAllData(EnqueteAllDataPayload enqueteAllDataPayload) {
        List<SyncResponse> personnesSyncResp = syncPersonnes(enqueteAllDataPayload.getPersonnes());
        List<SyncResponse> membreGroupesResp = syncMembreGroupe(enqueteAllDataPayload.getMembreGroupes());
        List<SyncResponse> parcelleSyncResp = syncParcelle(enqueteAllDataPayload.getParcelle());
        List<SyncResponse> enqueteSyncResp = syncEnquete(enqueteAllDataPayload.getEnquetes());
        List<SyncResponse> acteurConcernesSyncResp = syncActeurConcerne(enqueteAllDataPayload.getActeurConcernes());
        List<SyncResponse> pieceSyncResp = syncPiece(enqueteAllDataPayload.getPieces());

        SyncEnqueteAllDataResponse syncEnqueteAllDataResponse = new SyncEnqueteAllDataResponse();
        syncEnqueteAllDataResponse.setPersonneSynResponses(personnesSyncResp);
        syncEnqueteAllDataResponse.setMembreGroupeSynResponses(membreGroupesResp);
        syncEnqueteAllDataResponse.setParcelleSynResponses(parcelleSyncResp);
        syncEnqueteAllDataResponse.setEnqueteSynResponses(enqueteSyncResp);
        syncEnqueteAllDataResponse.setActeurConcerneSynResponses(acteurConcernesSyncResp);
        syncEnqueteAllDataResponse.setPieceSynResponses(pieceSyncResp);
        return syncEnqueteAllDataResponse;
    }

    @Override
    public List<SyncResponse> syncPersonnes(List<PersonnePayLoad> personnePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (personnePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune personne à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
          syncResponses = saveMobileData(personnePayLoads, TypeObjet.PERSONNE);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataPersonne> optionalMobileDataPersonne= mobileDataPersonneRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataPersonne.isPresent()){
//                personneAsyncWorker.traitementAsyncPersonne(optionalMobileDataPersonne.get());
//            }
//            //optionalMobileDataPersonne.ifPresent(personneAsyncWorker::traitementAsyncPersonne);
//        });

        return syncResponses;
    }


    @Override
    public List<SyncResponse> syncMembreGroupe(List<MembreGroupePayLoad> membreGroupePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (membreGroupePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucun membre à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(membreGroupePayLoads, TypeObjet.MEMBREGROUPE);

//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataMembreGroupe> optionalMobileDataMembreGroupe= mobileDataMembreGroupeRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataMembreGroupe.isPresent()){
//                membreGroupeAsyncWorker.traitementAsyncMembreGroupe(optionalMobileDataMembreGroupe.get());
//            }
//            //optionalMobileDataMembreGroupe.ifPresent(membreGroupeAsyncWorker::traitementAsyncMembreGroupe);
//        });

        return syncResponses;
    }

    @Override
    @Transactional
    public List<SyncResponse> syncEnquete(List<EnquetePayLoad> enquetePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (enquetePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune enquête à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(enquetePayLoads, TypeObjet.ENQUETE);

//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataEnquete> optionalMobileDataEnquete= mobileDataEnqueteRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataEnquete.isPresent()){
//                enqueteAsyncWorker.traitementAsyncEnquete(optionalMobileDataEnquete.get());
//            }
//
//        });

        return syncResponses;
    }

    @Override
    public List<SyncResponse> syncParcelle(List<ParcellePayLoad> parcellePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (parcellePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune parcelle à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(parcellePayLoads, TypeObjet.PARCELLE);

//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataParcelle> optionalMobileDataParcelle= mobileDataParcelleRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataParcelle.isPresent()){
//                parcelleAsyncWorker.traitementAsyncParcelle(optionalMobileDataParcelle.get());
//            }
//            //optionalMobileDataParcelle.ifPresent(parcelleAsyncWorker::traitementAsyncParcelle);
//        });

        return syncResponses;
    }
    @Override
    public List<SyncResponse> syncPiece(List<PiecePayLoad> piecePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (piecePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune pièce à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(piecePayLoads, TypeObjet.PIECE);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataPiece> optionalMobileDataPiece= mobileDataPieceRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataPiece.isPresent()){
//                pieceAsyncWorker.traitementAsyncPiece(optionalMobileDataPiece.get());
//            }
//            //optionalMobileDataPiece.ifPresent(pieceAsyncWorker::traitementAsyncPiece);
//        });
        return syncResponses;
    }

    @Override
    public List<SyncResponse> syncActeurConcerne(List<ActeurConcernePayLoad> acteurConcernePayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (acteurConcernePayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucun acteur concerné à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(acteurConcernePayLoads, TypeObjet.ACTEURCONCERNER);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataActeurConcerne> optionalMobileDataActeurConcerne= mobileDataActeurConcerneRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataActeurConcerne.isPresent()){
//                acteurConcerneAsyncWorker.traitementAsyncActeurConcerne(optionalMobileDataActeurConcerne.get());
//            }
//            //optionalMobileDataActeurConcerne.ifPresent(acteurConcerneAsyncWorker::traitementAsyncActeurConcerne);
//        });

        return syncResponses;
    }
    @Override
    public SyncResponse syncFiles(MultipartFile file,
                                  Long idBackend,
                                  Long externalKey,
                                  Long pieceId,
                                  Long membreGroupeId,
                                  Long terminalId,
                                  String name,
                                  String filePath,
                                  Long max_numero_piece_id,
                                  Long max_numero_upload_id,
                                  Long max_numero_acteur_concerne_id,
                                  Long enqueteId,
                                  Long enqueteBatimentId,
                                  Long enqueteUniteLogementId,
                                  Long personneId) {
        UploadPayLoad uploadPayLoad = new UploadPayLoad();
        uploadPayLoad.setSynchronise(false);
        //uploadPayLoad.setFile(file);
        uploadPayLoad.setExternalKey(externalKey);
        uploadPayLoad.setPieceId(pieceId);
        uploadPayLoad.setIdBackend(idBackend);
        uploadPayLoad.setMembreGroupeId(membreGroupeId);
        uploadPayLoad.setTerminalId(terminalId);
        uploadPayLoad.setName(name);
        uploadPayLoad.setFilePath(filePath);
        uploadPayLoad.setMax_numero_upload_id(max_numero_upload_id);
        uploadPayLoad.setMax_numero_piece_id(max_numero_piece_id);
        uploadPayLoad.setMax_numero_acteur_concerne_id(max_numero_acteur_concerne_id);
        uploadPayLoad.setEnqueteId(enqueteId);
        uploadPayLoad.setEnqueteBatimentId(enqueteBatimentId);
        uploadPayLoad.setEnqueteUniteLogementId(enqueteUniteLogementId);
        uploadPayLoad.setPersonneId(personneId);
        List<UploadPayLoad> uploadPayLoads = new ArrayList<>();
        uploadPayLoads.add(uploadPayLoad);
        try {
            List<SyncResponse> syncResponses = saveMobileData(uploadPayLoads, TypeObjet.UPLOAD,file);
//            syncResponses.forEach(syncResponse -> {
//                Optional<MobileDataUpload> optionalMobileDataUpload= mobileDataUploadRepository.findById(syncResponse.getIdBackend());
//                if(optionalMobileDataUpload.isPresent()){
//                    uploadAsyncWorker.traitementAsyncUpload(optionalMobileDataUpload.get());
//                }
//
//                //optionalMobileDataUpload.ifPresent(uploadAsyncWorker::traitementAsyncUpload);
//            });
            return syncResponses.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            SyncResponse syncResponse = new SyncResponse(uploadPayLoad.getIdBackend(),
                    uploadPayLoad.getExternalKey(), false, "Erreur inattendue");
            return syncResponse;
        }
    }



    @Override
    public List<SyncResponse> syncBatiment(List<BatimentPaylaod> batimentPaylaods) {

        List<SyncResponse> syncResponses = new ArrayList<>();
        if (batimentPaylaods == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucun batiment à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }

        syncResponses = saveMobileData(batimentPaylaods, TypeObjet.BATIMENT);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataBatiment> optionalMobileDataBatiment = mobileDataBatimentRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataBatiment.isPresent()){
//                batimentAsyncWorker.traitementAsyncBatiment(optionalMobileDataBatiment.get());
//            }
//            //optionalMobileDataBatiment.ifPresent(batimentAsyncWorker::traitementAsyncBatiment);
//        });
        return syncResponses;
    }
    @Override
    public List<SyncResponse> syncUniteLogement(List<UniteLogementPaylaod> uniteLogementPaylaods) {

        List<SyncResponse> syncResponses = new ArrayList<>();
        if (uniteLogementPaylaods == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune unité de logement à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(uniteLogementPaylaods, TypeObjet.UNITELOGEMENT);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataUniteLogement> optionalMobileDataUniteLogement= mobileDataUniteLogementRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataUniteLogement.isPresent()){
//                uniteLogementAsyncWorker.traitementAsyncUniteLogement(optionalMobileDataUniteLogement.get());
//            }
//            //optionalMobileDataUniteLogement.ifPresent(uniteLogementAsyncWorker::traitementAsyncUniteLogement);
//        });
        return syncResponses;
    }

    @Override
    public List<SyncResponse> syncCaracteristiqueBatiment(List<CaracteristiqueBatimentPaylod> caracteristiqueBatimentPaylods) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (caracteristiqueBatimentPaylods == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune caracteristique de bâtiment à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(caracteristiqueBatimentPaylods, TypeObjet.CARACTERISTIQUEBATIMENT);

//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataCaracteristiqueBatiment> optionalMobileDataCaracteristiqueBatiment= mobileDataCaracteristiqueBatimentRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataCaracteristiqueBatiment.isPresent()){
//                caracteristiqueBatimentAsyncWorker.traitementAsyncCaracteristiqueBatiment(optionalMobileDataCaracteristiqueBatiment.get());
//            }
//            //optionalMobileDataCaracteristiqueBatiment.ifPresent(caracteristiqueBatimentAsyncWorker::traitementAsyncCaracteristiqueBatiment);
//        });

        return syncResponses;
    }
    @Override
    public List<SyncResponse> syncCaracteristiqueUniteLogement(List<CaracteristiqueUniteLogementPaylod> caracteristiqueUniteLogementPaylods) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (caracteristiqueUniteLogementPaylods == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune caracteristique d'unité de logement à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(caracteristiqueUniteLogementPaylods, TypeObjet.CARACTERISTIQUEUNITELOGEMENT);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataCaracteristiqueUniteLogement> optionalMobileDataCaracteristiqueUniteLogement= mobileDataCaracteristiqueUniteLogementRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataCaracteristiqueUniteLogement.isPresent()){
//                caracteristiqueUniteLogementAsyncWorker.traitementAsyncCaracteristiqueUniteLogement(optionalMobileDataCaracteristiqueUniteLogement.get());
//            }
//            //optionalMobileDataCaracteristiqueUniteLogement.ifPresent(caracteristiqueUniteLogementAsyncWorker::traitementAsyncCaracteristiqueUniteLogement);
//        });
        return syncResponses;
    }
    @Override
    public List<SyncResponse> syncCaracteristiqueParcelle(List<CaracteristiqueParcellePaylod> caracteristiqueParcellePaylods) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (caracteristiqueParcellePaylods == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune caracteristique de parcelle à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(caracteristiqueParcellePaylods, TypeObjet.CARACTERISTIQUEPARCELLE);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataCaracteristiqueParcelle> optionalMobileDataCaracteristiqueParcelle= mobileDataCaracteristiqueParcelleRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataCaracteristiqueParcelle.isPresent()){
//                caracteristiqueParcelleAsyncWorker.traitementAsyncCaracteristiqueParcelle(optionalMobileDataCaracteristiqueParcelle.get());
//            }
//            //optionalMobileDataCaracteristiqueParcelle.ifPresent(caracteristiqueParcelleAsyncWorker::traitementAsyncCaracteristiqueParcelle);
//        });
        return syncResponses;
    }
    @Override
    public List<SyncResponse> syncEnqueteBatiment(List<EnqueteBatimentPayload> enqueteBatimentPayLoads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (enqueteBatimentPayLoads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune enquête de batiment à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(enqueteBatimentPayLoads, TypeObjet.ENQUETEBATIMENT);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataEnqueteBatiment> optionalMobileDataEnqueteBatiment= mobileDataEnqueteBatimentRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataEnqueteBatiment.isPresent()){
//                enqueteBatimentAsyncWorker.traitementAsyncEnqueteBatiment(optionalMobileDataEnqueteBatiment.get());
//            }
//            //optionalMobileDataEnqueteBatiment.ifPresent(enqueteBatimentAsyncWorker::traitementAsyncEnqueteBatiment);
//        });
        return syncResponses;
    }

    @Override
    public List<SyncResponse> syncEnqueteUniteLogement(List<EnqueteUniteLogementPayload> enqueteUniteLogementPayloads) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (enqueteUniteLogementPayloads == null) {
            SyncResponse syncResponse = new SyncResponse(null,
                    null, false, "Aucune enquête d'unité de logement à synchroniser");
            syncResponses.add(syncResponse);
            return syncResponses;
        }
        syncResponses = saveMobileData(enqueteUniteLogementPayloads, TypeObjet.ENQUETEUNITELOGEMENT);
//        syncResponses.forEach(syncResponse -> {
//            Optional<MobileDataEnqueteUniteLogement> optionalMobileDataEnqueteUniteLogement= mobileDataEnqueteUniteLogementRepository.findById(syncResponse.getIdBackend());
//            if(optionalMobileDataEnqueteUniteLogement.isPresent()){
//                enqueteUniteLogementAsyncWorker.traitementAsyncEnqueteUniteLogement(optionalMobileDataEnqueteUniteLogement.get());
//            }
//            //optionalMobileDataEnqueteUniteLogement.ifPresent(enqueteUniteLogementAsyncWorker::traitementAsyncEnqueteUniteLogement);
//        });
        return syncResponses;
    }

    private <T> List<SyncResponse> saveMobileData(List<T> payloads, TypeObjet typeObjet) {
        return saveMobileData(payloads, typeObjet, null);
    }

    private <T> List<SyncResponse> saveMobileData(List<T> payloads, TypeObjet typeObjet, MultipartFile file) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        System.out.println("==SYNCHRONISATION== : "+typeObjet.toString());
        switch (typeObjet) {
            case PERSONNE -> {
                payloads.forEach(payLoad -> {
                    PersonnePayLoad personnePayLoad = (PersonnePayLoad) payLoad;
                    try {
                        MobileDataPersonne mobileDataPersonne = new MobileDataPersonne();

                        Optional<MobileDataPersonne> optionalMobileDataPersonne = Optional.empty();

                        if (personnePayLoad.getIdBackend() != null) {
                            optionalMobileDataPersonne = mobileDataPersonneRepository.findById(personnePayLoad.getIdBackend());
                            if (optionalMobileDataPersonne.isPresent()) {
                                mobileDataPersonne = optionalMobileDataPersonne.get();
                            }
                        } else {
                            optionalMobileDataPersonne = mobileDataPersonneRepository.findFirstByTerminalIdAndEnqueteExternalKey(personnePayLoad.getTerminalId(), personnePayLoad.getExternalKey());
                            if (optionalMobileDataPersonne.isPresent()) {
                                mobileDataPersonne = optionalMobileDataPersonne.get();
                            }
                        }

                        mobileDataPersonne.setTerminalId(personnePayLoad.getTerminalId());
                        mobileDataPersonne.setExternalKey(personnePayLoad.getExternalKey());
                        mobileDataPersonne.setTraiter(false);
                        mobileDataPersonne.setPersonnePayLoad(personnePayLoad);
                        mobileDataPersonne = mobileDataPersonneRepository.save(mobileDataPersonne);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.PERSONNE.toString());
                        syncResponse.setExternalKey(personnePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataPersonne.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.PERSONNE.toString());
                        syncResponse.setExternalKey(personnePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(personnePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case MEMBREGROUPE -> {
                payloads.forEach(payLoad -> {
                    MembreGroupePayLoad membreGroupePayLoad = (MembreGroupePayLoad) payLoad;
                    try {
                        MobileDataMembreGroupe mobileDataMembreGroupe = new MobileDataMembreGroupe();

                        Optional<MobileDataMembreGroupe> optionalMobileDataMembreGroupe = Optional.empty();

                        if (membreGroupePayLoad.getIdBackend() != null) {
                            optionalMobileDataMembreGroupe = mobileDataMembreGroupeRepository.findById(membreGroupePayLoad.getIdBackend());
                            if (optionalMobileDataMembreGroupe.isPresent()) {
                                mobileDataMembreGroupe = optionalMobileDataMembreGroupe.get();
                            }
                        } else {
                            optionalMobileDataMembreGroupe = mobileDataMembreGroupeRepository.findFirstByTerminalIdAndEnqueteExternalKey(membreGroupePayLoad.getTerminalId(), membreGroupePayLoad.getExternalKey());
                            if (optionalMobileDataMembreGroupe.isPresent()) {
                                mobileDataMembreGroupe = optionalMobileDataMembreGroupe.get();
                            }
                        }
                        mobileDataMembreGroupe.setTerminalId(membreGroupePayLoad.getTerminalId());
                        mobileDataMembreGroupe.setExternalKey(membreGroupePayLoad.getExternalKey());
                        mobileDataMembreGroupe.setTraiter(false);
                        mobileDataMembreGroupe.setMembreGroupePayLoad(membreGroupePayLoad);
                        mobileDataMembreGroupe = mobileDataMembreGroupeRepository.save(mobileDataMembreGroupe);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.MEMBREGROUPE.toString());
                        syncResponse.setExternalKey(membreGroupePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataMembreGroupe.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.MEMBREGROUPE.toString());
                        syncResponse.setExternalKey(membreGroupePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(membreGroupePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case ACTEURCONCERNER -> {
                payloads.forEach(payLoad -> {
                    ActeurConcernePayLoad acteurConcernePayLoad = (ActeurConcernePayLoad) payLoad;
                    try {
                        MobileDataActeurConcerne mobileDataActeurConcerne = new MobileDataActeurConcerne();

                        Optional<MobileDataActeurConcerne> optionalMobileDataActeurConcerne = Optional.empty();

                        if (acteurConcernePayLoad.getIdBackend() != null) {
                            optionalMobileDataActeurConcerne = mobileDataActeurConcerneRepository.findById(acteurConcernePayLoad.getIdBackend());
                            if (optionalMobileDataActeurConcerne.isPresent()) {
                                mobileDataActeurConcerne = optionalMobileDataActeurConcerne.get();
                            }
                        } else {
                            optionalMobileDataActeurConcerne = mobileDataActeurConcerneRepository.findFirstByTerminalIdAndEnqueteExternalKey(acteurConcernePayLoad.getTerminalId(), acteurConcernePayLoad.getExternalKey());
                            if (optionalMobileDataActeurConcerne.isPresent()) {
                                mobileDataActeurConcerne = optionalMobileDataActeurConcerne.get();
                            }
                        }
                        mobileDataActeurConcerne.setTerminalId(acteurConcernePayLoad.getTerminalId());
                        mobileDataActeurConcerne.setExternalKey(acteurConcernePayLoad.getExternalKey());
                        mobileDataActeurConcerne.setTraiter(false);
                        mobileDataActeurConcerne.setActeurConcernePayLoad(acteurConcernePayLoad);
                        mobileDataActeurConcerne = mobileDataActeurConcerneRepository.save(mobileDataActeurConcerne);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.ACTEURCONCERNER.toString());
                        syncResponse.setExternalKey(acteurConcernePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataActeurConcerne.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.ACTEURCONCERNER.toString());
                        syncResponse.setExternalKey(acteurConcernePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(acteurConcernePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case PIECE -> {
                payloads.forEach(payLoad -> {
                    PiecePayLoad piecePayLoad = (PiecePayLoad) payLoad;
                    try {
                        MobileDataPiece mobileDataPiece = new MobileDataPiece();

                        Optional<MobileDataPiece> optionalMobileDataPiece = Optional.empty();

                        if (piecePayLoad.getIdBackend() != null) {
                            optionalMobileDataPiece = mobileDataPieceRepository.findById(piecePayLoad.getIdBackend());
                            if (optionalMobileDataPiece.isPresent()) {
                                mobileDataPiece = optionalMobileDataPiece.get();
                            }
                        } else {
                            optionalMobileDataPiece = mobileDataPieceRepository.findFirstByTerminalIdAndEnqueteExternalKey(piecePayLoad.getTerminalId(), piecePayLoad.getExternalKey());
                            if (optionalMobileDataPiece.isPresent()) {
                                mobileDataPiece = optionalMobileDataPiece.get();
                            }
                        }
                        mobileDataPiece.setTerminalId(piecePayLoad.getTerminalId());
                        mobileDataPiece.setExternalKey(piecePayLoad.getExternalKey());
                        mobileDataPiece.setTraiter(false);
                        mobileDataPiece.setPiecePayLoad(piecePayLoad);
                        mobileDataPiece = mobileDataPieceRepository.save(mobileDataPiece);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.PIECE.toString());
                        syncResponse.setExternalKey(piecePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataPiece.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.PIECE.toString());
                        syncResponse.setExternalKey(piecePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(piecePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case UPLOAD -> {
                payloads.forEach(payLoad -> {
                    UploadPayLoad uploadPayLoad = (UploadPayLoad) payLoad;
                    try {
                        MobileDataUpload mobileDataUpload = new MobileDataUpload();

                        Optional<MobileDataUpload> optionalMobileDataUpload = Optional.empty();

                        if (uploadPayLoad.getIdBackend() != null) {
                            optionalMobileDataUpload = mobileDataUploadRepository.findById(uploadPayLoad.getIdBackend());
                            if (optionalMobileDataUpload.isPresent()) {
                                mobileDataUpload = optionalMobileDataUpload.get();
                            }
                        } else {
                            optionalMobileDataUpload = mobileDataUploadRepository.findFirstByTerminalIdAndEnqueteExternalKey(uploadPayLoad.getTerminalId(), uploadPayLoad.getExternalKey());
                            if (optionalMobileDataUpload.isPresent()) {
                                mobileDataUpload = optionalMobileDataUpload.get();
                            }
                        }

                        mobileDataUpload.setTerminalId(uploadPayLoad.getTerminalId());
                        mobileDataUpload.setExternalKey(uploadPayLoad.getExternalKey());
                        mobileDataUpload.setTraiter(false);
                        mobileDataUpload.setUploadPayLoad(uploadPayLoad);
                        String fileName = fileStorageService.storeFile(file);
                        mobileDataUpload.setFileName(fileName);
                        mobileDataUpload.setMimeType(file.getContentType());
                        mobileDataUpload.setSize(file.getSize());
                        mobileDataUpload.setOriginalFileName(file.getOriginalFilename());
                        mobileDataUpload.setName(uploadPayLoad.getName());
                        mobileDataUpload.setFilePath(uploadPayLoad.getFilePath());
                        mobileDataUpload = mobileDataUploadRepository.save(mobileDataUpload);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.UPLOAD.toString());
                        syncResponse.setExternalKey(uploadPayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataUpload.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.UPLOAD.toString());
                        syncResponse.setExternalKey(uploadPayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(uploadPayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case BATIMENT -> {
                payloads.forEach(payLoad -> {
                    BatimentPaylaod batimentPaylaod = (BatimentPaylaod) payLoad;
                    try {
                        MobileDataBatiment mobileDataBatiment = new MobileDataBatiment();

                        Optional<MobileDataBatiment> optionalMobileDataBatiment = Optional.empty();

                        if (batimentPaylaod.getIdBackend() != null) {
                            optionalMobileDataBatiment = mobileDataBatimentRepository.findById(batimentPaylaod.getIdBackend());
                            if (optionalMobileDataBatiment.isPresent()) {
                                mobileDataBatiment = optionalMobileDataBatiment.get();
                            }
                        } else {
                            optionalMobileDataBatiment = mobileDataBatimentRepository.findFirstByTerminalIdAndEnqueteExternalKey(batimentPaylaod.getTerminalId(), batimentPaylaod.getExternalKey());
                            if (optionalMobileDataBatiment.isPresent()) {
                                mobileDataBatiment = optionalMobileDataBatiment.get();
                            }
                        }
                        mobileDataBatiment.setTerminalId(batimentPaylaod.getTerminalId());
                        mobileDataBatiment.setExternalKey(batimentPaylaod.getExternalKey());
                        mobileDataBatiment.setTraiter(false);
                        mobileDataBatiment.setBatimentPaylaod(batimentPaylaod);
                        mobileDataBatiment = mobileDataBatimentRepository.save(mobileDataBatiment);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.BATIMENT.toString());
                        syncResponse.setExternalKey(batimentPaylaod.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataBatiment.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.PIECE.toString());
                        syncResponse.setExternalKey(batimentPaylaod.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(batimentPaylaod.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case UNITELOGEMENT -> {
                payloads.forEach(payLoad -> {
                    UniteLogementPaylaod uniteLogementPaylaod = (UniteLogementPaylaod) payLoad;
                    try {
                        MobileDataUniteLogement mobileDataUniteLogement = new MobileDataUniteLogement();

                        Optional<MobileDataUniteLogement> optionalMobileDataUniteLogement = Optional.empty();

                        if (uniteLogementPaylaod.getIdBackend() != null) {
                            optionalMobileDataUniteLogement = mobileDataUniteLogementRepository.findById(uniteLogementPaylaod.getIdBackend());
                            if (optionalMobileDataUniteLogement.isPresent()) {
                                mobileDataUniteLogement = optionalMobileDataUniteLogement.get();
                            }
                        } else {
                            optionalMobileDataUniteLogement = mobileDataUniteLogementRepository.findFirstByTerminalIdAndEnqueteExternalKey(uniteLogementPaylaod.getTerminalId(), uniteLogementPaylaod.getExternalKey());
                            if (optionalMobileDataUniteLogement.isPresent()) {
                                mobileDataUniteLogement = optionalMobileDataUniteLogement.get();
                            }
                        }
                        mobileDataUniteLogement.setTerminalId(uniteLogementPaylaod.getTerminalId());
                        mobileDataUniteLogement.setExternalKey(uniteLogementPaylaod.getExternalKey());
                        mobileDataUniteLogement.setTraiter(false);
                        mobileDataUniteLogement.setUniteLogementPaylaod(uniteLogementPaylaod);
                        mobileDataUniteLogement = mobileDataUniteLogementRepository.save(mobileDataUniteLogement);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.UNITELOGEMENT.toString());
                        syncResponse.setExternalKey(uniteLogementPaylaod.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataUniteLogement.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.PIECE.toString());
                        syncResponse.setExternalKey(uniteLogementPaylaod.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(uniteLogementPaylaod.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case ENQUETEBATIMENT -> {
                payloads.forEach(payLoad -> {
                    EnqueteBatimentPayload enqueteBatimentPayload = (EnqueteBatimentPayload) payLoad;
                    try {
                        MobileDataEnqueteBatiment mobileDataEnqueteBatiment = new MobileDataEnqueteBatiment();

                        Optional<MobileDataEnqueteBatiment> optionalMobileDataEnqueteBatiment = Optional.empty();

                        if (enqueteBatimentPayload.getIdBackend() != null) {
                            optionalMobileDataEnqueteBatiment = mobileDataEnqueteBatimentRepository.findById(enqueteBatimentPayload.getIdBackend());
                            if (optionalMobileDataEnqueteBatiment.isPresent()) {
                                mobileDataEnqueteBatiment = optionalMobileDataEnqueteBatiment.get();
                            }
                        } else {
                            optionalMobileDataEnqueteBatiment = mobileDataEnqueteBatimentRepository.findFirstByTerminalIdAndEnqueteExternalKey(enqueteBatimentPayload.getTerminalId(), enqueteBatimentPayload.getExternalKey());
                            if (optionalMobileDataEnqueteBatiment.isPresent()) {
                                mobileDataEnqueteBatiment = optionalMobileDataEnqueteBatiment.get();
                            }
                        }
                        mobileDataEnqueteBatiment.setTerminalId(enqueteBatimentPayload.getTerminalId());
                        mobileDataEnqueteBatiment.setExternalKey(enqueteBatimentPayload.getExternalKey());
                        mobileDataEnqueteBatiment.setTraiter(false);
                        mobileDataEnqueteBatiment.setEnqueteBatimentPayload(enqueteBatimentPayload);
                        mobileDataEnqueteBatiment = mobileDataEnqueteBatimentRepository.save(mobileDataEnqueteBatiment);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.UNITELOGEMENT.toString());
                        syncResponse.setExternalKey(enqueteBatimentPayload.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataEnqueteBatiment.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.ENQUETEBATIMENT.toString());
                        syncResponse.setExternalKey(enqueteBatimentPayload.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(enqueteBatimentPayload.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case ENQUETEUNITELOGEMENT -> {
                payloads.forEach(payLoad -> {
                    EnqueteUniteLogementPayload enqueteUniteLogementPayload = (EnqueteUniteLogementPayload) payLoad;
                    try {
                        MobileDataEnqueteUniteLogement mobileDataEnqueteUniteLogement = new MobileDataEnqueteUniteLogement();

                        Optional<MobileDataEnqueteUniteLogement> optionalMobileDataEnqueteUniteLogement = Optional.empty();

                        if (enqueteUniteLogementPayload.getIdBackend() != null) {
                            optionalMobileDataEnqueteUniteLogement = mobileDataEnqueteUniteLogementRepository.findById(enqueteUniteLogementPayload.getIdBackend());
                            if (optionalMobileDataEnqueteUniteLogement.isPresent()) {
                                mobileDataEnqueteUniteLogement = optionalMobileDataEnqueteUniteLogement.get();
                            }
                        } else {
                            optionalMobileDataEnqueteUniteLogement = mobileDataEnqueteUniteLogementRepository.findFirstByTerminalIdAndEnqueteExternalKey(enqueteUniteLogementPayload.getTerminalId(), enqueteUniteLogementPayload.getExternalKey());
                            if (optionalMobileDataEnqueteUniteLogement.isPresent()) {
                                mobileDataEnqueteUniteLogement = optionalMobileDataEnqueteUniteLogement.get();
                            }
                        }
                        mobileDataEnqueteUniteLogement.setTerminalId(enqueteUniteLogementPayload.getTerminalId());
                        mobileDataEnqueteUniteLogement.setExternalKey(enqueteUniteLogementPayload.getExternalKey());
                        mobileDataEnqueteUniteLogement.setTraiter(false);
                        mobileDataEnqueteUniteLogement.setEnqueteUniteLogementPayload(enqueteUniteLogementPayload);
                        mobileDataEnqueteUniteLogement = mobileDataEnqueteUniteLogementRepository.save(mobileDataEnqueteUniteLogement);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.ENQUETEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(enqueteUniteLogementPayload.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataEnqueteUniteLogement.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.ENQUETEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(enqueteUniteLogementPayload.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(enqueteUniteLogementPayload.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case CARACTERISTIQUEPARCELLE -> {
                payloads.forEach(payLoad -> {
                    CaracteristiqueParcellePaylod caracteristiqueParcellePaylod = (CaracteristiqueParcellePaylod) payLoad;
                    try {
                        MobileDataCaracteristiqueParcelle mobileDataCaracteristiqueParcelle = new MobileDataCaracteristiqueParcelle();

                        Optional<MobileDataCaracteristiqueParcelle> optionalMobileDataCaracteristiqueParcelle = Optional.empty();

                        if (caracteristiqueParcellePaylod.getIdBackend() != null) {
                            optionalMobileDataCaracteristiqueParcelle = mobileDataCaracteristiqueParcelleRepository.findById(caracteristiqueParcellePaylod.getIdBackend());
                            if (optionalMobileDataCaracteristiqueParcelle.isPresent()) {
                                mobileDataCaracteristiqueParcelle = optionalMobileDataCaracteristiqueParcelle.get();
                            }
                        } else {
                            optionalMobileDataCaracteristiqueParcelle = mobileDataCaracteristiqueParcelleRepository.findFirstByTerminalIdAndEnqueteExternalKey(caracteristiqueParcellePaylod.getTerminalId(), caracteristiqueParcellePaylod.getExternalKey());
                            if (optionalMobileDataCaracteristiqueParcelle.isPresent()) {
                                mobileDataCaracteristiqueParcelle = optionalMobileDataCaracteristiqueParcelle.get();
                            }
                        }
                        mobileDataCaracteristiqueParcelle.setTerminalId(caracteristiqueParcellePaylod.getTerminalId());
                        mobileDataCaracteristiqueParcelle.setExternalKey(caracteristiqueParcellePaylod.getExternalKey());
                        mobileDataCaracteristiqueParcelle.setTraiter(false);
                        mobileDataCaracteristiqueParcelle.setCaracteristiqueParcellePaylod(caracteristiqueParcellePaylod);
                        mobileDataCaracteristiqueParcelle = mobileDataCaracteristiqueParcelleRepository.save(mobileDataCaracteristiqueParcelle);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.CARACTERISTIQUEPARCELLE.toString());
                        syncResponse.setExternalKey(caracteristiqueParcellePaylod.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataCaracteristiqueParcelle.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.CARACTERISTIQUEPARCELLE.toString());
                        syncResponse.setExternalKey(caracteristiqueParcellePaylod.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(caracteristiqueParcellePaylod.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }

            case CARACTERISTIQUEBATIMENT -> {
                payloads.forEach(payLoad -> {
                    CaracteristiqueBatimentPaylod caracteristiqueBatimentPaylod = (CaracteristiqueBatimentPaylod) payLoad;
                    try {
                        MobileDataCaracteristiqueBatiment mobileDataCaracteristiqueBatiment = new MobileDataCaracteristiqueBatiment();

                        Optional<MobileDataCaracteristiqueBatiment> optionalMobileDataCaracteristiqueBatiment = Optional.empty();

                        if (caracteristiqueBatimentPaylod.getIdBackend() != null) {
                            optionalMobileDataCaracteristiqueBatiment = mobileDataCaracteristiqueBatimentRepository.findById(caracteristiqueBatimentPaylod.getIdBackend());
                            if (optionalMobileDataCaracteristiqueBatiment.isPresent()) {
                                mobileDataCaracteristiqueBatiment = optionalMobileDataCaracteristiqueBatiment.get();
                            }
                        } else {
                            optionalMobileDataCaracteristiqueBatiment = mobileDataCaracteristiqueBatimentRepository.findFirstByTerminalIdAndEnqueteExternalKey(caracteristiqueBatimentPaylod.getTerminalId(), caracteristiqueBatimentPaylod.getExternalKey());
                            if (optionalMobileDataCaracteristiqueBatiment.isPresent()) {
                                mobileDataCaracteristiqueBatiment = optionalMobileDataCaracteristiqueBatiment.get();
                            }
                        }

                        mobileDataCaracteristiqueBatiment.setTerminalId(caracteristiqueBatimentPaylod.getTerminalId());
                        mobileDataCaracteristiqueBatiment.setExternalKey(caracteristiqueBatimentPaylod.getExternalKey());
                        mobileDataCaracteristiqueBatiment.setTraiter(false);
                        mobileDataCaracteristiqueBatiment.setCaracteristiqueBatimentPaylod(caracteristiqueBatimentPaylod);
                        mobileDataCaracteristiqueBatiment = mobileDataCaracteristiqueBatimentRepository.save(mobileDataCaracteristiqueBatiment);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.CARACTERISTIQUEBATIMENT.toString());
                        syncResponse.setExternalKey(caracteristiqueBatimentPaylod.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataCaracteristiqueBatiment.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.CARACTERISTIQUEBATIMENT.toString());
                        syncResponse.setExternalKey(caracteristiqueBatimentPaylod.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(caracteristiqueBatimentPaylod.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case CARACTERISTIQUEUNITELOGEMENT -> {
                payloads.forEach(payLoad -> {
                    CaracteristiqueUniteLogementPaylod caracteristiqueUniteLogementPaylod = (CaracteristiqueUniteLogementPaylod) payLoad;
                    try {
                        MobileDataCaracteristiqueUniteLogement mobileDataCaracteristiqueUniteLogement = new MobileDataCaracteristiqueUniteLogement();

                        Optional<MobileDataCaracteristiqueUniteLogement> optionalMobileDataCaracteristiqueUniteLogement = Optional.empty();

                        if (caracteristiqueUniteLogementPaylod.getIdBackend() != null) {
                            optionalMobileDataCaracteristiqueUniteLogement = mobileDataCaracteristiqueUniteLogementRepository.findById(caracteristiqueUniteLogementPaylod.getIdBackend());
                            if (optionalMobileDataCaracteristiqueUniteLogement.isPresent()) {
                                mobileDataCaracteristiqueUniteLogement = optionalMobileDataCaracteristiqueUniteLogement.get();
                            }
                        } else {
                            optionalMobileDataCaracteristiqueUniteLogement = mobileDataCaracteristiqueUniteLogementRepository.findFirstByTerminalIdAndEnqueteExternalKey(caracteristiqueUniteLogementPaylod.getTerminalId(), caracteristiqueUniteLogementPaylod.getExternalKey());
                            if (optionalMobileDataCaracteristiqueUniteLogement.isPresent()) {
                                mobileDataCaracteristiqueUniteLogement = optionalMobileDataCaracteristiqueUniteLogement.get();
                            }
                        }
                        mobileDataCaracteristiqueUniteLogement.setTerminalId(caracteristiqueUniteLogementPaylod.getTerminalId());
                        mobileDataCaracteristiqueUniteLogement.setExternalKey(caracteristiqueUniteLogementPaylod.getExternalKey());
                        mobileDataCaracteristiqueUniteLogement.setTraiter(false);
                        mobileDataCaracteristiqueUniteLogement.setCaracteristiqueUniteLogementPaylod(caracteristiqueUniteLogementPaylod);
                        mobileDataCaracteristiqueUniteLogement = mobileDataCaracteristiqueUniteLogementRepository.save(mobileDataCaracteristiqueUniteLogement);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(caracteristiqueUniteLogementPaylod.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataCaracteristiqueUniteLogement.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(caracteristiqueUniteLogementPaylod.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(caracteristiqueUniteLogementPaylod.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case PARCELLE -> {
                payloads.forEach(payLoad -> {
                    ParcellePayLoad parcellePayLoad = (ParcellePayLoad) payLoad;
                    try {
                        MobileDataParcelle mobileDataParcelle = new MobileDataParcelle();

                        Optional<MobileDataParcelle> optionalMobileDataParcelle = Optional.empty();

                        if (parcellePayLoad.getIdBackend() != null) {
                            optionalMobileDataParcelle = mobileDataParcelleRepository.findById(parcellePayLoad.getIdBackend());
                            if (optionalMobileDataParcelle.isPresent()) {
                                mobileDataParcelle = optionalMobileDataParcelle.get();
                            }
                        } else {
                            optionalMobileDataParcelle = mobileDataParcelleRepository.findFirstByTerminalIdAndEnqueteExternalKey(parcellePayLoad.getTerminalId(), parcellePayLoad.getExternalKey());
                            if (optionalMobileDataParcelle.isPresent()) {
                                mobileDataParcelle = optionalMobileDataParcelle.get();
                            }
                        }
                        mobileDataParcelle.setTerminalId(parcellePayLoad.getTerminalId());
                        mobileDataParcelle.setExternalKey(parcellePayLoad.getExternalKey());
                        mobileDataParcelle.setTraiter(false);
                        mobileDataParcelle.setParcellePayLoad(parcellePayLoad);
                        mobileDataParcelle = mobileDataParcelleRepository.save(mobileDataParcelle);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(parcellePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataParcelle.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(parcellePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(parcellePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
            case ENQUETE -> {
                payloads.forEach(payLoad -> {
                    EnquetePayLoad enquetePayLoad = (EnquetePayLoad) payLoad;
                    try {
                        MobileDataEnquete mobileDataEnquete = new MobileDataEnquete();

                        Optional<MobileDataEnquete> optionalMobileDataEnquete = Optional.empty();

                        if (enquetePayLoad.getIdBackend() != null) {
                            optionalMobileDataEnquete = mobileDataEnqueteRepository.findById(enquetePayLoad.getIdBackend());
                            if (optionalMobileDataEnquete.isPresent()) {
                                mobileDataEnquete = optionalMobileDataEnquete.get();
                            }
                        } else {
                            optionalMobileDataEnquete = mobileDataEnqueteRepository.findFirstByTerminalIdAndEnqueteExternalKey(enquetePayLoad.getTerminalId(), enquetePayLoad.getExternalKey());
                            if (optionalMobileDataEnquete.isPresent()) {
                                mobileDataEnquete = optionalMobileDataEnquete.get();
                            }
                        }
                        mobileDataEnquete.setTerminalId(enquetePayLoad.getTerminalId());
                        mobileDataEnquete.setExternalKey(enquetePayLoad.getExternalKey());
                        mobileDataEnquete.setTraiter(false);
                        mobileDataEnquete.setEnquetePayLoad(enquetePayLoad);
                        mobileDataEnquete = mobileDataEnqueteRepository.save(mobileDataEnquete);
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation effectuée avec succès pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(enquetePayLoad.getExternalKey());
                        syncResponse.setSynchronise(true);
                        syncResponse.setIdBackend(mobileDataEnquete.getId());
                        syncResponses.add(syncResponse);
                    } catch (Exception e) {
                        e.printStackTrace();
                        SyncResponse syncResponse = new SyncResponse();
                        syncResponse.setObservation("Synchronisation échouée pour " + TypeObjet.CARACTERISTIQUEUNITELOGEMENT.toString());
                        syncResponse.setExternalKey(enquetePayLoad.getExternalKey());
                        syncResponse.setSynchronise(false);
                        syncResponse.setIdBackend(enquetePayLoad.getIdBackend());
                        syncResponses.add(syncResponse);
                    }
                });
            }
        }
        return syncResponses;
    }

    @Override
    public List<SyncResponse> syncEnqueteFromMobile(List<Long> idEnquete) {
        List<SyncResponse> syncResponses = new ArrayList<>();
        if (idEnquete == null) {
            return null;
        } else {
            idEnquete.forEach(aLong -> {
                Optional<Enquete> optionalEnquete = enqueteRepository.findById(aLong);
                if (optionalEnquete.isPresent()) {
                    optionalEnquete.get().setSynchronise(true);
                    Enquete enquete = enqueteRepository.save(optionalEnquete.get());
                    SyncResponse syncResponse = new SyncResponse();
                    syncResponse.setSynchronise(enquete.isSynchronise());
                    syncResponse.setExternalKey(enquete.getExternalKey());
                    syncResponse.setIdBackend(enquete.getId());
                    syncResponses.add(syncResponse);
                }
            });
            return syncResponses;
        }
    }
    @Async("traitementTableRelationnelleExecutor")
    public void traitementTableRelationnelle(Long terminalId){

        List<MobileDataPersonne> mobileDataPersonnes= mobileDataPersonneRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataPersonnes.forEach(mobileDataPersonne -> {
            personneAsyncWorker.traitementAsyncPersonne(mobileDataPersonne);
        });

        List<MobileDataMembreGroupe> mobileDataMembreGroupes=mobileDataMembreGroupeRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataMembreGroupes.forEach(mobileDataMembreGroupe -> {
            membreGroupeAsyncWorker.traitementAsyncMembreGroupe(mobileDataMembreGroupe);
        });

        List<MobileDataParcelle> mobileDataParcelles= mobileDataParcelleRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataParcelles.forEach(mobileDataParcelle -> {
            parcelleAsyncWorker.traitementAsyncParcelle(mobileDataParcelle);
        });

        List<MobileDataEnquete> mobileDataEnquetes= mobileDataEnqueteRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataEnquetes.forEach(mobileDataEnquete -> {
            enqueteAsyncWorker.traitementAsyncEnquete(mobileDataEnquete);
        });
        List<MobileDataActeurConcerne> mobileDataActeurConcernes= mobileDataActeurConcerneRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataActeurConcernes.forEach(mobileDataActeurConcerne -> {
            acteurConcerneAsyncWorker.traitementAsyncActeurConcerne(mobileDataActeurConcerne);
        });
        List<MobileDataPiece> mobileDataPieces= mobileDataPieceRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataPieces.forEach(mobileDataPiece -> {
            pieceAsyncWorker.traitementAsyncPiece(mobileDataPiece);
        });
        List<MobileDataCaracteristiqueParcelle> mobileDataCaracteristiqueParcelles= mobileDataCaracteristiqueParcelleRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataCaracteristiqueParcelles.forEach(mobileDataCaracteristiqueParcelle -> {
            caracteristiqueParcelleAsyncWorker.traitementAsyncCaracteristiqueParcelle(mobileDataCaracteristiqueParcelle);
        });
        List<MobileDataBatiment> mobileDataBatiments= mobileDataBatimentRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataBatiments.forEach(mobileDataBatiment -> {
            batimentAsyncWorker.traitementAsyncBatiment(mobileDataBatiment);
        });
        List<MobileDataEnqueteBatiment> mobileDataEnqueteBatiments= mobileDataEnqueteBatimentRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataEnqueteBatiments.forEach(mobileDataEnqueteBatiment -> {
            enqueteBatimentAsyncWorker.traitementAsyncEnqueteBatiment(mobileDataEnqueteBatiment);
        });
        List<MobileDataCaracteristiqueBatiment> mobileDataCaracteristiqueBatiments= mobileDataCaracteristiqueBatimentRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataCaracteristiqueBatiments.forEach(mobileDataCaracteristiqueBatiment -> {
            caracteristiqueBatimentAsyncWorker.traitementAsyncCaracteristiqueBatiment(mobileDataCaracteristiqueBatiment);
        });
        List<MobileDataUniteLogement> mobileDataUniteLogements= mobileDataUniteLogementRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataUniteLogements.forEach(mobileDataUniteLogement -> {
            uniteLogementAsyncWorker.traitementAsyncUniteLogement(mobileDataUniteLogement);
        });
        List<MobileDataEnqueteUniteLogement> mobileDataEnqueteUniteLogements= mobileDataEnqueteUniteLogementRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataEnqueteUniteLogements.forEach(mobileDataEnqueteUniteLogement -> {
            enqueteUniteLogementAsyncWorker.traitementAsyncEnqueteUniteLogement(mobileDataEnqueteUniteLogement);
        });
        List<MobileDataCaracteristiqueUniteLogement> mobileDataCaracteristiqueUniteLogements= mobileDataCaracteristiqueUniteLogementRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataCaracteristiqueUniteLogements.forEach(mobileDataCaracteristiqueUniteLogement -> {
            caracteristiqueUniteLogementAsyncWorker.traitementAsyncCaracteristiqueUniteLogement(mobileDataCaracteristiqueUniteLogement);
        });
        List<MobileDataUpload> mobileDataUploads= mobileDataUploadRepository.findAllByTraiterIsFalseAndTerminalId(terminalId);
        mobileDataUploads.forEach(mobileDataUpload -> {
            uploadAsyncWorker.traitementAsyncUpload(mobileDataUpload);
        });
    }

}



