This commit is contained in:
sidibe
2025-11-19 12:20:37 +00:00
commit 1972c8ff90
86 changed files with 3373 additions and 0 deletions

View File

@@ -0,0 +1,177 @@
package com.pmu.betengine.service;
import com.pmu.betengine.model.TPE;
import com.pmu.betengine.model.dto.TPEDTO;
import com.pmu.betengine.model.dto.TPERequestDTO;
import com.pmu.betengine.model.statut.StatutTPE;
import com.pmu.betengine.repository.TPERepository;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional
public class TPEService {
private final TPERepository tpeRepository;
private final ModelMapper modelMapper;
public TPEService(TPERepository tpeRepository, ModelMapper modelMapper) {
this.tpeRepository = tpeRepository;
this.modelMapper = modelMapper;
}
public List<TPEDTO> getAllTPEs() {
return tpeRepository.findAll()
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public TPEDTO getTPEById(Long id) {
TPE tpe = tpeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
return convertToDTO(tpe);
}
public TPEDTO createTPE(TPERequestDTO requestDTO) {
// Vérifier l'unicité de l'IMEI
if (tpeRepository.existsByImei(requestDTO.getImei())) {
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + requestDTO.getImei());
}
// Vérifier l'unicité du numéro de série
if (tpeRepository.existsBySerial(requestDTO.getSerial())) {
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + requestDTO.getSerial());
}
TPE tpe = convertToEntity(requestDTO);
TPE saved = tpeRepository.save(tpe);
return convertToDTO(saved);
}
public TPEDTO updateTPE(Long id, TPERequestDTO requestDTO) {
TPE existing = tpeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
// Vérifier l'unicité de l'IMEI si modifié
if (!existing.getImei().equals(requestDTO.getImei()) &&
tpeRepository.existsByImei(requestDTO.getImei())) {
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + requestDTO.getImei());
}
// Vérifier l'unicité du numéro de série si modifié
if (!existing.getSerial().equals(requestDTO.getSerial()) &&
tpeRepository.existsBySerial(requestDTO.getSerial())) {
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + requestDTO.getSerial());
}
// Mise à jour des champs
modelMapper.map(requestDTO, existing);
return convertToDTO(tpeRepository.save(existing));
}
public void deleteTPE(Long id) {
if (!tpeRepository.existsById(id)) {
throw new RuntimeException("TPE non trouvé avec l'id: " + id);
}
tpeRepository.deleteById(id);
}
public TPEDTO updateStatut(Long id, StatutTPE statut) {
TPE tpe = tpeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
tpe.setStatut(statut);
// Si le statut est AFFECTE, mettre à jour le champ assigne
if (statut == StatutTPE.AFFECTE) {
tpe.setAssigne(true);
} else if (statut == StatutTPE.DISPONIBLE) {
tpe.setAssigne(false);
}
return convertToDTO(tpeRepository.save(tpe));
}
public TPEDTO assignerTPE(Long id) {
TPE tpe = tpeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
if (tpe.isAssigne()) {
throw new RuntimeException("Le TPE est déjà assigné");
}
tpe.setAssigne(true);
tpe.setStatut(StatutTPE.AFFECTE);
return convertToDTO(tpeRepository.save(tpe));
}
public TPEDTO libererTPE(Long id) {
TPE tpe = tpeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
if (!tpe.isAssigne()) {
throw new RuntimeException("Le TPE n'est pas assigné");
}
tpe.setAssigne(false);
tpe.setStatut(StatutTPE.DISPONIBLE);
return convertToDTO(tpeRepository.save(tpe));
}
public List<TPEDTO> getTPEsByStatut(StatutTPE statut) {
return tpeRepository.findByStatut(statut)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public List<TPEDTO> getTPEsDisponibles() {
return tpeRepository.findAvailableTPE()
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public List<TPEDTO> searchTPEs(String searchTerm) {
return tpeRepository.searchByTerm(searchTerm)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public long countTPEsByStatut(StatutTPE statut) {
return tpeRepository.countByStatut(statut);
}
public long countTPEsAssignes() {
return tpeRepository.countByAssigne(true);
}
private TPEDTO convertToDTO(TPE tpe) {
TPEDTO dto = modelMapper.map(tpe, TPEDTO.class);
// Formatage des dates
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
if (tpe.getCreatedAt() != null) {
dto.setCreatedAt(tpe.getCreatedAt().format(formatter));
}
if (tpe.getUpdatedAt() != null) {
dto.setUpdatedAt(tpe.getUpdatedAt().format(formatter));
}
return dto;
}
private TPE convertToEntity(TPERequestDTO dto) {
return modelMapper.map(dto, TPE.class);
}
}