Skip to content

Commit

Permalink
codefi list for former lists
Browse files Browse the repository at this point in the history
  • Loading branch information
cmarcoin committed Oct 17, 2024
1 parent 3f6e1a1 commit 16a0b0d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 64 deletions.
142 changes: 80 additions & 62 deletions pkg/core/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 += `
Expand Down Expand Up @@ -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

Expand All @@ -87,7 +156,8 @@ func (p summaryParams) toSQLCurrentScoreParams() []interface{} {
expressionSiret = &eSiret
expressionRaisonSociale = &eRaisonSociale
}
return []interface{}{

params := []interface{}{
p.zoneGeo,
p.limit,
p.offset,
Expand All @@ -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{} {
Expand Down Expand Up @@ -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`
4 changes: 2 additions & 2 deletions pkg/core/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 16a0b0d

Please sign in to comment.