--Récupération de PSFM à partir d'une liste d'ID :
select
  pmfm.id as pmfmId,
  pmfm.parameter.name as parameterName,
  pmfm.unit.symbol as symbol,
  pmfm.matrix.name as matrixName,
  pmfm.method.name as method
from 
  PmfmImpl pmfm
where
  pmfm.id=1


--PSFM lié à l'environnement :
select
  pmfm.id as pmfmId,
  pmfm.parameter.name as parameterName,
  pmfm.unit.symbol as symbol,
  pmfm.matrix.name as matrixName,
  pmfm.method.name as method
from 
  PmfmImpl pmfm
where
  pmfm.parameter.parameterGroup.id=3 


--PSFM applicable à un engin :
select
  pmfm.id as pmfmId,
  pmfm.parameter.name as parameterName,
  pmfm.unit.symbol as symbol,
  pmfm.matrix.id,
  pmfm.matrix.name as matrixName,
  pmfm.method.name as method
from 
  PmfmImpl pmfm
where pmfm.matrix.id=3


--Liste des engins pour les navires de pêche professionnels
select 
  g.id as gearId,
  g.label as gearCode,
  g.name as gearName
from
  GearImpl g
where
  g.gearClassification.id=1 /*Classification FAO*/
  and g.status.code=1

--Liste des engins scientifiques :
select 
  g.id as gearId,
  g.label as gearCode,
  g.name as gearName,
  g.gearClassification.id
from
  GearImpl g
where
  g.gearClassification.id=5 /*Classification engin campagne*/
  and g.status.code=1

--Liste des pays :
select 
  l.id,
  l.label,
  l.name
from
  LocationImpl l
where
  l.locationLevel.id=21 /*=LocationLevelId.PAYS_ISO3*/
  and l.status.code=1


--Liste des lieux (Ports) restreinte sous un lieu parent (Pays=FRA)
select 
  l.id,
  l.label,
  l.name
from
  LocationImpl l,
  LocationHierarchyImpl lh
where
  l.locationLevel.id=6
  and l.id=lh.locationHierarchyPk.location.id
  and lh.locationHierarchyPk.parent.label='FRA'
  and lh.locationHierarchyPk.parent.locationLevel.id=21
  and l.status.code=1
  

--Liste des personnes (restreinte aux observateurs) :
select distinct
  p.id,
  p.firstname,
  p.lastname,
  p.department.code
from
  PersonImpl p
  join p.profils up
where 1=1
  and up.name='SIH_ROBS'
  and p.status in (1,2)


--Valeurs qualitative du PSFM du sexe :
select 
  qv.id,
  case when (qv.name=qv.description) then qv.name
  else concat(qv.name,' - ',qv.description)
  end as name
from
  PmfmImpl pmfm
  join pmfm.qualitativeValues qv
where 1=1
  and pmfm.id=196 -- enumeration à créer
  --and pmfm.parameter.code='SEX'  -- à éviter, au cas où plusieurs PSFM existent un jour pour le paramètre sex
  and qv.status.code in (1,2)


--Valeurs qualitative 'Vrac/HV' :
select 
  qv.id,
  case when (qv.name=qv.description) then qv.name
  else concat(qv.name,' - ',qv.description)
  end as name
from
  PmfmImpl pmfm
  join pmfm.qualitativeValues qv
where 1=1
  --and pmfm.id=196 -- enumeration à créer
  and pmfm.parameter.code='SORTED'  -- ParameterCode.BULK=
  and qv.status.code in (1,2)

--Valeurs qualitatives de catégorie de poids :
select 
  qv.id,
  case when (qv.name=qv.description) then qv.name
  else concat(qv.name,' - ',qv.description)
  end as name
from
  PmfmImpl pmfm
  join pmfm.qualitativeValues qv
where 1=1
  and pmfm.id=198 -- enumeration à créer
  --and pmfm.parameter.code='SIZE_SORTING_CAT'  -- ParameterCode.BULK=
  and qv.status.code in (1,2)


--Psfm à utiliser pour les poids mesuré ou observé : (utile pour le stockage du poids dans QUANTIFICATION_MEASUREMENT)
select 
  pmfm.id,
  pmfm.parameter.code,
  pmfm.matrix.name,
  pmfm.method.name
from
  PmfmImpl pmfm
where 1=1
  --and pmfm.id=??? -- enumeration à créer avec le bon PSFM
  -- sans doute 220 (Mesure par un observateur)
  and pmfm.parameter.code='WEIGHT'  -- à éviter car cela renvoi plusieurs PSFM


--PSFM à utiliser si le poids est calculé : (utile pour le stockage du poids dans QUANTIFICATION_MEASUREMENT)
select 
  pmfm.id,
  pmfm.parameter.code,
  pmfm.matrix.name,
  pmfm.method.name
from
  PmfmImpl pmfm
where 1=1
  --and pmfm.id=??? -- enumeration à créer avec le bon PSFM
  -- sans doute 223 (calcul observateur par RTP) ou 1386 (sommes des poids de référence contextuels des lots fils)
  and pmfm.parameter.code='WEIGHT_CALCULATED'  -- à éviter car cela renvoi plusieurs PSFM


--Récupération des RTP pour une espèce/lieu/date/sexe
select 
  wlc.conversionCoefficientA as coefA,
  wlc.conversionCoefficientB as coefB,
  wlc.startMonth,
  wlc.endMonth,
  wlc.year
from
  WeightLengthConversionImpl wlc,
  LocationHierarchyImpl lh
where 1=1
  and wlc.location.id = lh.locationHierarchyPk.parent.id
  and wlc.startMonth <= 2 -- mois ou l'OP à eu lieu (1=janvier, 12=decembre)
  and 2 <= wlc.endMonth
  and lh.locationHierarchyPk.location.label='61E7' -- calculé à partir de la position Lat/Long WGS84
  and lh.locationHierarchyPk.location.locationLevel.id=113 -- rect stat
  -- TODO : filtrer sur le sex
  and wlc.referenceTaxon.id in (1523)
order by
  wlc.year desc



Benoit LAVENIER
E-IS - Environmental Information Systems - www.e-is.pro
Téléphone : 09 53 24 41 20  /  06 62 86 37 82
Adresse : 10 place de l'Eglise, 53470 MARTIGNE SUR MAYENNE
Email : benoit.lavenier@e-is.pro
Fax : 09 58 55 73 50