From 16a0b0d75ee868597aea0aa08b0981be36332d08 Mon Sep 17 00:00:00 2001 From: Charles Marcoin <charles.marcoin@gmail.com> Date: Tue, 15 Oct 2024 18:12:18 +0200 Subject: [PATCH] codefi list for former lists --- pkg/core/sql.go | 142 +++++++++++++++++++++++++------------------- pkg/core/summary.go | 4 +- 2 files changed, 82 insertions(+), 64 deletions(-) diff --git a/pkg/core/sql.go b/pkg/core/sql.go index b50038ad..7a989796 100644 --- a/pkg/core/sql.go +++ b/pkg/core/sql.go @@ -41,7 +41,7 @@ func buildSQLCurrentScoreQuery(codefiListOnly bool) string { if codefiListOnly { baseQuery += ` - inner join codefi_entreprises ce on ce.siren = s.siren` + inner join codefi_entreprises ce on ce.siren = s.siren and ce.libelle = $25` } baseQuery += ` @@ -77,7 +77,76 @@ func buildSQLCurrentScoreQuery(codefiListOnly bool) string { return baseQuery } -func (p summaryParams) toSQLCurrentScoreParams() []interface{} { +func buildSQLScoreQuery(codefiListOnly bool) string { + baseQuery := `select + s.siret, s.siren, s.raison_sociale, s.commune, + s.libelle_departement, s.code_departement, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.score end as valeur_score, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.detail end as detail_score, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then coalesce(s.first_list_etablissement = $25 or s.first_red_list_etablissement = $25, false) end as first_alert, + s.chiffre_affaire, s.arrete_bilan, s.exercice_diane, s.variation_ca, s.resultat_expl, s.effectif, s.effectif_entreprise, + s.libelle_n5, s.libelle_n1, s.code_activite, s.last_procol, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.activite_partielle end as activite_partielle, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.apconso_heure_consomme end as apconso_heure_consomme, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.apconso_montant end as apconso_montant, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf then s.hausse_urssaf end as hausse_urssaf, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf then s.dette_urssaf end as dette_urssaf, + case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.alert end, + count(*) over () as nb_total, + count(case when sc.alert='Alerte seuil F1' and (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then 1 end) over () as nb_f1, + count(case when sc.alert='Alerte seuil F2' and (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then 1 end) over () as nb_f2, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).visible, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).in_zone, + f.id is not null as followed_etablissement, + fe.siren is not null as followed_entreprise, + s.siege, s.raison_sociale_groupe, territoire_industrie, + f.comment, f.category, f.since, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score, + (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).bdf, + s.secteur_covid, s.excedent_brut_d_exploitation, s.etat_administratif, s.etat_administratif_entreprise, s.has_delai + from v_summaries s + inner join score0 sc on sc.siret = s.siret and sc.libelle_liste = $25 and sc.alert != 'Pas d''alerte'` + + if codefiListOnly { + baseQuery += ` + inner join codefi_entreprises ce on ce.siren = sc.siren and ce.libelle = sc.libelle_liste` + } + + baseQuery += ` + left join v_naf n on n.code_n5 = s.code_activite + left join etablissement_follow f on f.active and f.siret = s.siret and f.username = $8 + left join v_entreprise_follow fe on fe.siren = s.siren and fe.username = $8 + where + (s.roles && $1 or $6) + and (s.code_departement=any($1) or $7) + and (s.siege or not $9) + and (s.last_procol = any($10) or $10 is null) + and (s.code_departement=any($11) or $11 is null) + and (s.effectif >= $13 or $13 is null) + and (s.effectif <= $14 or $14 is null) + and (s.effectif_entreprise >= $16 or $16 is null) + and (s.effectif_entreprise <= $17 or $17 is null) + and (s.chiffre_affaire >= $18 or $18 is null) + and (s.chiffre_affaire <= $19 or $19 is null) + and (fe.siren is not null = $12 or $12 is null) + and (s.raison_sociale ilike $5 or s.siret ilike $4 or coalesce($4, $5) is null) + and 'score' = any($1) + and (not (s.secteur_covid = any($20)) or $20 is null) + and (n.code_n1 = any($15) or $15 is null) + and (s.etat_administratif = $21 or $21 is null) + and (not (s.has_delai = $23) or $23 is null) + and (s.date_creation_entreprise <= $24 or $24 is null) + and ((s.first_list_etablissement = $25 or s.first_red_list_etablissement = $25) and $22 or $22 is null) + ` + ExcludedNafCodes + ` + order by sc.alert, sc.score desc, s.siret + limit $2 offset $3` + + return baseQuery +} + +func (p summaryParams) toSQLCurrentScoreParams(codefiListOnly bool) []interface{} { var expressionSiret *string var expressionRaisonSociale *string @@ -87,7 +156,8 @@ func (p summaryParams) toSQLCurrentScoreParams() []interface{} { expressionSiret = &eSiret expressionRaisonSociale = &eRaisonSociale } - return []interface{}{ + + params := []interface{}{ p.zoneGeo, p.limit, p.offset, @@ -113,6 +183,13 @@ func (p summaryParams) toSQLCurrentScoreParams() []interface{} { p.hasntDelai, p.creationDateThreshold, // $24 } + + // Only add the 25th parameter if codefiListOnly is true + if codefiListOnly { + params = append(params, p.libelleListe) // $25 + } + + return params } func (p summaryParams) toSQLScoreParams() []interface{} { @@ -154,62 +231,3 @@ func (p summaryParams) toSQLScoreParams() []interface{} { p.libelleListe, // $25 } } - -// sqlScore récupère les scores de la liste passée en paramètre -var sqlScore = `select - s.siret, s.siren, s.raison_sociale, s.commune, - s.libelle_departement, s.code_departement, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.score end as valeur_score, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.detail end as detail_score, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then coalesce(s.first_list_etablissement = $25 or s.first_red_list_etablissement = $25, false) end as first_alert, - s.chiffre_affaire, s.arrete_bilan, s.exercice_diane, s.variation_ca, s.resultat_expl, s.effectif, s.effectif_entreprise, - s.libelle_n5, s.libelle_n1, s.code_activite, s.last_procol, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.activite_partielle end as activite_partielle, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.apconso_heure_consomme end as apconso_heure_consomme, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp then s.apconso_montant end as apconso_montant, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf then s.hausse_urssaf end as hausse_urssaf, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf then s.dette_urssaf end as dette_urssaf, - case when (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then sc.alert end, - count(*) over () as nb_total, - count(case when sc.alert='Alerte seuil F1' and (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then 1 end) over () as nb_f1, - count(case when sc.alert='Alerte seuil F2' and (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score then 1 end) over () as nb_f2, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).visible, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).in_zone, - f.id is not null as followed_etablissement, - fe.siren is not null as followed_entreprise, - s.siege, s.raison_sociale_groupe, territoire_industrie, - f.comment, f.category, f.since, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).urssaf, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).dgefp, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).score, - (permissions($1, s.roles, s.first_list_entreprise, s.code_departement, fe.siren is not null)).bdf, - s.secteur_covid, s.excedent_brut_d_exploitation, s.etat_administratif, s.etat_administratif_entreprise, s.has_delai - from v_summaries s - inner join score0 sc on sc.siret = s.siret and sc.libelle_liste = $25 and sc.alert != 'Pas d''alerte' - left join v_naf n on n.code_n5 = s.code_activite - left join etablissement_follow f on f.active and f.siret = s.siret and f.username = $8 - left join v_entreprise_follow fe on fe.siren = s.siren and fe.username = $8 - where - (s.roles && $1 or $6) - and (s.code_departement=any($1) or $7) - and (s.siege or not $9) - and (s.last_procol = any($10) or $10 is null) - and (s.code_departement=any($11) or $11 is null) - and (s.effectif >= $13 or $13 is null) - and (s.effectif <= $14 or $14 is null) - and (s.effectif_entreprise >= $16 or $16 is null) - and (s.effectif_entreprise <= $17 or $17 is null) - and (s.chiffre_affaire >= $18 or $18 is null) - and (s.chiffre_affaire <= $19 or $19 is null) - and (fe.siren is not null = $12 or $12 is null) - and (s.raison_sociale ilike $5 or s.siret ilike $4 or coalesce($4, $5) is null) - and 'score' = any($1) - and (not (s.secteur_covid = any($20)) or $20 is null) - and (n.code_n1 = any($15) or $15 is null) - and (s.etat_administratif = $21 or $21 is null) - and (not (s.has_delai = $23) or $23 is null) - and (s.date_creation_entreprise <= $24 or $24 is null) - and ((s.first_list_etablissement = $25 or s.first_red_list_etablissement = $25) and $22 or $22 is null) - ` + ExcludedNafCodes + ` - order by sc.alert, sc.score desc, s.siret - limit $2 offset $3` diff --git a/pkg/core/summary.go b/pkg/core/summary.go index b7ce1b15..3eaf0e24 100644 --- a/pkg/core/summary.go +++ b/pkg/core/summary.go @@ -207,11 +207,11 @@ func getSummaries(params summaryParams) (Summaries, error) { if params.orderBy == "score" { if params.currentListe { - sqlParams = params.toSQLCurrentScoreParams() + sqlParams = params.toSQLCurrentScoreParams(params.codefiListOnly != nil && *params.codefiListOnly) sql = buildSQLCurrentScoreQuery(params.codefiListOnly != nil && *params.codefiListOnly) } else { sqlParams = params.toSQLScoreParams() - sql = sqlScore + sql = buildSQLScoreQuery(params.codefiListOnly != nil && *params.codefiListOnly) } } else if params.orderBy == "raison_sociale" { p := params.toSQLParams()