/**
 * @author DBU
 * @date 08/07/21
 * @version 1.0.0
 * @project SIEAU
 */

# --- !Ups

insert into mat_equipements_detail
select idequipement, 'central', idcentrale
from mat_equipements
where idcentrale not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'central'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);

insert into mat_equipements_detail
select idequipement, 'sensor', idcapteur
from mat_equipements
where idcapteur not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'sensor'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);

insert into mat_equipements_detail
select idequipement, 'sim', idsim
from mat_equipements
where idsim not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'sim'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);


insert into mat_equipements_detail
select idequipement, 'powerSupply', idalimentation
from mat_equipements
where idalimentation not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'powerSupply'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);

insert into mat_equipements_detail
select idequipement, 'telecom', idtelecom
from mat_equipements
where idtelecom not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'telecom'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);

insert into mat_equipements_detail
select idequipement, 'variousMateriel', iddivers
from mat_equipements
where iddivers not in (select idmateriel
                         from mat_equipements_detail
                         where typemateriel = 'variousMateriel'
                           and mat_equipements.idequipement = mat_equipements_detail.idequipement);

update mat_equipements set idcentrale=null, idcapteur=null, idalimentation=null, idsim=null, idtelecom=null, iddivers=null where idequipement notnull;

# --- !Downs
