125 lines
4.1 KiB
Java
125 lines
4.1 KiB
Java
package com.pmu.betengine.service;
|
|
|
|
import com.pmu.betengine.model.Agent;
|
|
import com.pmu.betengine.model.TPE;
|
|
import com.pmu.betengine.model.statut.StatutTPE;
|
|
import com.pmu.betengine.repository.AgentRepository;
|
|
import com.pmu.betengine.repository.TPERepository;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.List;
|
|
|
|
@Service
|
|
@Transactional
|
|
public class TPEService {
|
|
|
|
private final TPERepository tpeRepository;
|
|
private final AgentRepository agentRepository;
|
|
|
|
public TPEService(TPERepository tpeRepository, AgentRepository agentRepository) {
|
|
this.tpeRepository = tpeRepository;
|
|
this.agentRepository = agentRepository;
|
|
}
|
|
|
|
public List<TPE> getAllTPEs() {
|
|
return tpeRepository.findAll();
|
|
}
|
|
|
|
public TPE getTPEById(Long id) {
|
|
return tpeRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
|
}
|
|
|
|
public TPE createTPE(TPE tpe) {
|
|
if (tpeRepository.existsByImei(tpe.getImei())) {
|
|
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + tpe.getImei());
|
|
}
|
|
if (tpeRepository.existsBySerial(tpe.getSerial())) {
|
|
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + tpe.getSerial());
|
|
}
|
|
return tpeRepository.save(tpe);
|
|
}
|
|
|
|
public TPE updateTPE(Long id, TPE tpeDetails) {
|
|
TPE existing = tpeRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("TPE non trouvé avec l'id: " + id));
|
|
|
|
if (!existing.getImei().equals(tpeDetails.getImei()) &&
|
|
tpeRepository.existsByImei(tpeDetails.getImei())) {
|
|
throw new RuntimeException("Un TPE avec cet IMEI existe déjà: " + tpeDetails.getImei());
|
|
}
|
|
|
|
if (!existing.getSerial().equals(tpeDetails.getSerial()) &&
|
|
tpeRepository.existsBySerial(tpeDetails.getSerial())) {
|
|
throw new RuntimeException("Un TPE avec ce numéro de série existe déjà: " + tpeDetails.getSerial());
|
|
}
|
|
|
|
// Update fields
|
|
existing.setImei(tpeDetails.getImei());
|
|
existing.setSerial(tpeDetails.getSerial());
|
|
existing.setType(tpeDetails.getType());
|
|
existing.setMarque(tpeDetails.getMarque());
|
|
existing.setModele(tpeDetails.getModele());
|
|
existing.setStatut(tpeDetails.getStatut());
|
|
|
|
return 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 TPE updateStatut(Long id, StatutTPE statut) {
|
|
TPE tpe = getTPEById(id);
|
|
tpe.setStatut(statut);
|
|
tpe.setAssigne(statut == StatutTPE.AFFECTE);
|
|
if (statut == StatutTPE.DISPONIBLE) tpe.setAssigne(false);
|
|
return tpeRepository.save(tpe);
|
|
}
|
|
|
|
public TPE assignerTPE(Long tpeId, Long agentId) {
|
|
TPE tpe = getTPEById(tpeId);
|
|
Agent agent = agentRepository.findById(agentId)
|
|
.orElseThrow(() -> new RuntimeException("Agent not found"));
|
|
tpe.setAgent(agent);
|
|
tpe.setAssigne(true);
|
|
return tpeRepository.save(tpe);
|
|
}
|
|
|
|
public TPE libererTPE(Long id) {
|
|
TPE tpe = getTPEById(id);
|
|
if (!tpe.isAssigne()) {
|
|
throw new RuntimeException("Le TPE n'est pas assigné");
|
|
}
|
|
tpe.setAssigne(false);
|
|
tpe.setStatut(StatutTPE.DISPONIBLE);
|
|
tpe.setAgent(null);
|
|
return tpeRepository.save(tpe);
|
|
}
|
|
|
|
public List<TPE> getTPEsByStatut(StatutTPE statut) {
|
|
return tpeRepository.findByStatut(statut);
|
|
}
|
|
|
|
public List<TPE> getTPEsDisponibles() {
|
|
return tpeRepository.findAvailableTPE();
|
|
}
|
|
|
|
public List<TPE> searchTPEs(String searchTerm) {
|
|
return tpeRepository.searchByTerm(searchTerm);
|
|
}
|
|
|
|
public long countTPEsByStatut(StatutTPE statut) {
|
|
return tpeRepository.countByStatut(statut);
|
|
}
|
|
|
|
public long countTPEsAssignes() {
|
|
return tpeRepository.countByAssigne(true);
|
|
}
|
|
|
|
}
|