java持久数据

kq4fsx7k  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(210)

我想保留从查询中检索到的数据。数据涉及一组代理。每个代理都已经有了信息。在 agent_data table。我举个例子:
如果代理有这些数据

[123, toto, ZER345, '12 / 01/1971 ', M]

此代理的请求数据

[123, prof, '12 / 01/1999 ', '31 / 12/9999', FF00]

[123 ,, sup prof, '12 / 07/2009 ', '31 / 12/9999 ', FF22]

然后代理将有两行,它们将是这样的:

[123, toto, ZER345, '12 / 01/1971 ', M, prof, '12 / 01/1999', '31 / 12/9999 ', FF00,]
[123, toto, ZER345, '12 / 01/1971 ', M, sup prof, '12 / 07/2009', '31 / 12/9999 ', FF22]

这是我的方法

@Override
public List < EmploiAgentEntite > recupererDonneesValidesCongesAgentsPeriodeReference(
    final List < Long > listeNumDossierAgent, final Calendar dateDebutReference, final Calendar dateFinReference) {

    final List < EmploiAgentEntite > donneesTempAgents = new ArrayList <>(); // Liste temporaire
    final List < EmploiAgentEntite > donneesExistantesList = recupererInfosAgentsExistants(listeNumDossierAgent);

    List < EmploiAgentEntite > donneesValidesCongees = new ArrayList <>(); // List finale

    StringBuilder sqlAgentsDonneesValides = new StringBuilder();
    sqlAgentsDonneesValides.append(" select * from ( select nudoss");
    /* -- Alimenter la date de début */
    sqlAgentsDonneesValides.append(" , case when nuligne = 1 and datdeb > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then cast( :dateDebutReference as DATE) end as ZYAGDATDEB ");
    /* -- Alimenter la date de fin */
    sqlAgentsDonneesValides.append(" , case when nuligne = 1 and datdeb > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then datdeb-1 end as ZYAGDATFIN ");
    /* -- Alimenter le motif du congé */
    sqlAgentsDonneesValides.append(" , case when nuligne = 1 and datdeb > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then '' end as ZYAGMOTIFA from  ( " + regrouperCongesAgentsParLigne());

    sqlAgentsDonneesValides.append(" )as agt_conges union select nudoss ");
    /* -- Alimenter la date de début */
    sqlAgentsDonneesValides
        .append(" , case when nuligne = 1 and DATDEB <= :dateDebutReference and DATFIN > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then DATDEB when nuligne <> 1 and DATDEB <> (DATFIN + 1) ");
    sqlAgentsDonneesValides.append(" then DATFIN + 1 end as ZYAGDATDEB ");
    /* -- Alimenter la date de fin */
    sqlAgentsDonneesValides
        .append(" , case when nuligne = 1 and DATDEB <= :dateDebutReference and DATFIN > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then DATFIN when nuligne <> 1 and DATDEB <> (DATFIN + 1) ");
    sqlAgentsDonneesValides.append(" then DATDEB - 1 end as ZYAGDATFIN ");
    /* -- Alimenter le motif du congé */
    sqlAgentsDonneesValides
        .append(" , case when nuligne = 1 and DATDEB <= :dateDebutReference and DATFIN > :dateDebutReference ");
    sqlAgentsDonneesValides.append(" then MOTIFA when nuligne <> 1 and DATDEB <> (DATFIN + 1) ");
    sqlAgentsDonneesValides
        .append(" then '' end as ZYAGMOTIFA from  (" + regrouperCongesAgentsParLigne() + ") as agt_conges");
    sqlAgentsDonneesValides.append(" union ");
    /* -- Alimenter la date de début */
    sqlAgentsDonneesValides.append(" select NUDOSS , case when nbr_ligne = nuligne ");
    sqlAgentsDonneesValides.append(" and ( DATFIN <> '31/12/9999' or DATFIN <> '31/12/2999' or DATFIN is not null or ");

    sqlAgentsDonneesValides.append(" DATFIN <> '01/01/2999' or DATFIN <> :dateFinReference )  ");
    sqlAgentsDonneesValides.append(" then DATFIN + 1 end as ZYAGDATDEB , case when nbr_ligne = nuligne ");
    sqlAgentsDonneesValides.append(" and ( DATFIN <> '31/12/9999' or DATFIN <> '31/12/2999' or DATFIN is not null ");
    sqlAgentsDonneesValides.append(" or DATFIN <> '01/01/0001' or DATFIN <> :dateFinReference )"); // <>
    sqlAgentsDonneesValides.append(" then cast ( :dateFinReference as date) END AS ZYAGDATFIN ");
    sqlAgentsDonneesValides.append(" , case when nbr_ligne = nuligne ");
    sqlAgentsDonneesValides
        .append(" and ( DATFIN <> '31/12/9999' or DATFIN <> '31/12/2999' or DATFIN is not null or DATFIN <> '01/01/2999' )");
    sqlAgentsDonneesValides.append(" then '' end as ZYAGMOTIFA from ( " + regrouperCongesAgentsParLigne());
    sqlAgentsDonneesValides
        .append(" ) as agt_conges union select nudoss, datdeb, datfin, motifa from ( " + regrouperCongesAgentsParLigne());
    sqlAgentsDonneesValides.append(" ) as agt_conges  ) CONG where ZYAGDATDEB is not null ");
    sqlAgentsDonneesValides.append(" and CONG.nudoss in (:listeNumDossierAgent ) ");
    sqlAgentsDonneesValides.append(" order by ZYAGDATDEB ");

    Query requeteListe = entityManager.createNativeQuery(sqlAgentsDonneesValides.toString());

    requeteListe.setParameter(Constantes.DATE_DEBUT_REFERENCE, dateDebutReference);
    requeteListe.setParameter(Constantes.DATE_FIN_REFERENCE, dateFinReference);
    requeteListe.setParameter(Constantes.LISTE_AGENT_PARAMETRE_SQL, listeNumDossierAgent);

    @SuppressWarnings("unchecked")
    List < Object[] > resultats = requeteListe.getResultList();
    if (CollectionUtils.isNotEmpty(resultats)) {
        for (int i = 0; i < resultats.size(); i++) {
            Object[] agent = resultats.get(i);

            // recupererLigneAgentEncours(agentExistant, agent, resultsAgents);

            EmploiAgentEntite agentEncours = recupererAgentEncoursParNudoss((java.math.BigInteger) agent[0]);
            EmploiAgentEntite entite = setResultQueryInDonneesValidesConges(agentEncours, agent);
            donneesValidesCongees.add(entite);

        }
    }

    return donneesValidesCongees;
}

private void recupererLigneAgentEncours(final EmploiAgentEntite agentExistant, final Object[] agent,
    final List < EmploiAgentEntite > resultsAgents) {

    EmploiAgentEntite newEntite = setResultQueryInDonneesValidesConges(agentExistant, agent);
    newEntite.setEtat(EtatEmploiAgent.A_GENERER);
    resultsAgents.add(newEntite);

}

private List < EmploiAgentEntite > recupererInfosAgentsExistants(final List < Long > listeNumDossierAgent) {
    StringBuilder hql = new StringBuilder();
    hql.append("select agent from EmploiAgentEntite as agent where agent.numDossier IN (:listeNumDossierAgent)");
    Query queryHql = getEntityManager().createQuery(hql.toString());
    queryHql.setParameter(Constantes.LISTE_AGENT_PARAMETRE, listeNumDossierAgent);
    return queryHql.getResultList();
}

/* recuperer les informations de l'agent en cours */
private EmploiAgentEntite recupererAgentEncoursParNudoss(final BigInteger agentDossier) {

    Query agentQuery = entityManager
        .createNativeQuery("Select agent from ech.emploiagent as agent where agent.nudoss = :nudoss");
    agentQuery.setParameter(Constantes.LISTE_NUMERODOSSIER_AGENT_PARAMETRE, agentDossier.longValue());
    // agentQuery.setMaxResults(1);

    return (EmploiAgentEntite) agentQuery.getResultList().get(0);

}

private EmploiAgentEntite setResultQueryInDonneesValidesConges(final EmploiAgentEntite agentEncours, final Object[] agent) {

    EmploiAgentEntite entite = new EmploiAgentEntite(agentEncours);

    entite.setNumDossier(((java.math.BigInteger) agent[0]).longValue());
    if (agent[1] != null) {
        Calendar dateDebutConges = DateUtilsCNE.dateToCalendar((Date) agent[1]);
        entite.setDateDebutAbsence(dateDebutConges);
    } else {
        entite.setDateDebutAbsence(null);
    }
    if (agent[2] != null) {
        Calendar dateFinAbsence = DateUtilsCNE.dateToCalendar((Date) agent[2]);
        entite.setDateFinAbsence(dateFinAbsence);
    } else {
        entite.setDateFinAbsence(null);
    }

    entite.setMotifAbsence((String) agent[3]);

    entite.setEtat(EtatEmploiAgent.A_GENERER);

    entite.setEtapeTraitement(EtapeTraitement.SELECT_AGENT_ELIGIBLES_CNE);

    return entite;

}

我需要帮助来定义如何复制这些数据。;
注:对不起,我的英语不好:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题