| 1 | package com.tecmilenio.mapsconect.service; |
| 2 | |
| 3 | import com.tecmilenio.mapsconect.dto.EmpresaDTO; |
| 4 | import com.tecmilenio.mapsconect.dto.ExperienciaDTO; |
| 5 | import com.tecmilenio.mapsconect.dto.ResenaDTO; |
| 6 | import com.tecmilenio.mapsconect.dto.ResenaRequestDTO; |
| 7 | import com.tecmilenio.mapsconect.entity.EmpresaVinculada; |
| 8 | import com.tecmilenio.mapsconect.entity.Estudiante; |
| 9 | import com.tecmilenio.mapsconect.entity.ResenaEmpresarial; |
| 10 | import com.tecmilenio.mapsconect.entity.Usuario; |
| 11 | import com.tecmilenio.mapsconect.exception.ResourceNotFoundException; |
| 12 | import com.tecmilenio.mapsconect.repository.EmpresaVinculadaRepository; |
| 13 | import com.tecmilenio.mapsconect.repository.EstudianteRepository; |
| 14 | import com.tecmilenio.mapsconect.repository.ResenaEmpresarialRepository; |
| 15 | import com.tecmilenio.mapsconect.repository.UsuarioRepository; |
| 16 | import org.springframework.beans.factory.annotation.Autowired; |
| 17 | import org.springframework.stereotype.Service; |
| 18 | |
| 19 | import java.util.Comparator; |
| 20 | import java.util.List; |
| 21 | |
| 22 | @Service |
| 23 | public class EmpresarialService { |
| 24 | |
| 25 | @Autowired |
| 26 | private EmpresaVinculadaRepository empresaRepository; |
| 27 | |
| 28 | @Autowired |
| 29 | private ResenaEmpresarialRepository resenaRepository; |
| 30 | |
| 31 | @Autowired |
| 32 | private EstudianteRepository estudianteRepository; |
| 33 | |
| 34 | @Autowired |
| 35 | private UsuarioRepository usuarioRepository; |
| 36 | |
| 37 | public List<EmpresaDTO> listarEmpresas(String busqueda, Double calificacionMinima) { |
| 38 | List<EmpresaVinculada> empresas = (busqueda != null && !busqueda.isBlank()) |
| 39 | ? empresaRepository.findByActivaTrueAndNombreContainingIgnoreCase(busqueda) |
| 40 | : empresaRepository.findByActivaTrue(); |
| 41 | |
| 42 | return empresas.stream() |
| 43 | .map(this::mapearAEmpresaDTO) |
| 44 | .filter(dto -> calificacionMinima == null |
| 45 | || dto.getCalificacion() == null |
| 46 | || dto.getCalificacion() >= calificacionMinima) |
| 47 | .toList(); |
| 48 | } |
| 49 | |
| 50 | public List<ResenaDTO> listarResenasPorEmpresa(Integer idEmpresa) { |
| 51 | if (!empresaRepository.existsById(idEmpresa)) { |
| 52 | throw new ResourceNotFoundException("Empresa no encontrada"); |
| 53 | } |
| 54 | return resenaRepository.findByEmpresa_Id(idEmpresa).stream().map(this::mapearAResenaDTO).toList(); |
| 55 | } |
| 56 | |
| 57 | public ResenaDTO crearResena(String email, ResenaRequestDTO dto) { |
| 58 | Usuario usuario = usuarioRepository.findByEmail(email) |
| 59 | .orElseThrow(() -> new ResourceNotFoundException("Usuario no encontrado")); |
| 60 | |
| 61 | Estudiante estudiante = estudianteRepository.findByUsuario_Id(usuario.getId()) |
| 62 | .orElseThrow(() -> new RuntimeException("Debes completar tu perfil de estudiante antes de dejar una reseña")); |
| 63 | |
| 64 | EmpresaVinculada empresa = empresaRepository.findById(dto.getIdEmpresa()) |
| 65 | .orElseThrow(() -> new ResourceNotFoundException("Empresa no encontrada")); |
| 66 | |
| 67 | ResenaEmpresarial resena = ResenaEmpresarial.builder() |
| 68 | .estudiante(estudiante) |
| 69 | .empresa(empresa) |
| 70 | .calificacion(dto.getCalificacion()) |
| 71 | .proyectoRealizado(dto.getProyectoRealizado()) |
| 72 | .recomendaciones(dto.getRecomendaciones()) |
| 73 | .build(); |
| 74 | |
| 75 | return mapearAResenaDTO(resenaRepository.save(resena)); |
| 76 | } |
| 77 | |
| 78 | private EmpresaDTO mapearAEmpresaDTO(EmpresaVinculada empresa) { |
| 79 | List<ResenaEmpresarial> resenas = resenaRepository.findByEmpresa_Id(empresa.getId()); |
| 80 | |
| 81 | Double promedio = resenas.isEmpty() ? null |
| 82 | : resenas.stream().mapToInt(ResenaEmpresarial::getCalificacion).average().orElse(0); |
| 83 | |
| 84 | ResenaEmpresarial mejorResena = resenas.stream() |
| 85 | .max(Comparator.comparingInt(ResenaEmpresarial::getCalificacion)) |
| 86 | .orElse(null); |
| 87 | |
| 88 | ExperienciaDTO experiencia = null; |
| 89 | if (mejorResena != null) { |
| 90 | Usuario autorUsuario = mejorResena.getEstudiante().getUsuario(); |
| 91 | experiencia = ExperienciaDTO.builder() |
| 92 | .autor(autorUsuario != null ? autorUsuario.getNombre() : "Estudiante") |
| 93 | .semestre(mejorResena.getEstudiante().getSemestreActual()) |
| 94 | .texto(mejorResena.getRecomendaciones() != null |
| 95 | ? mejorResena.getRecomendaciones() |
| 96 | : mejorResena.getProyectoRealizado()) |
| 97 | .build(); |
| 98 | } |
| 99 | |
| 100 | return EmpresaDTO.builder() |
| 101 | .id(empresa.getId()) |
| 102 | .nombre(empresa.getNombre()) |
| 103 | .sector(empresa.getSector()) |
| 104 | .descripcion(empresa.getDescripcion()) |
| 105 | .activa(empresa.getActiva()) |
| 106 | .calificacion(promedio) |
| 107 | .totalResenas((long) resenas.size()) |
| 108 | .experienciaDestacada(experiencia) |
| 109 | .build(); |
| 110 | } |
| 111 | |
| 112 | private ResenaDTO mapearAResenaDTO(ResenaEmpresarial resena) { |
| 113 | Usuario usuario = resena.getEstudiante().getUsuario(); |
| 114 | return ResenaDTO.builder() |
| 115 | .id(resena.getId()) |
| 116 | .idEmpresa(resena.getEmpresa().getId()) |
| 117 | .idEstudiante(resena.getEstudiante().getId()) |
| 118 | .nombreEstudiante(usuario != null ? (usuario.getNombre() + " " + usuario.getApellido()) : null) |
| 119 | .calificacion(resena.getCalificacion()) |
| 120 | .proyectoRealizado(resena.getProyectoRealizado()) |
| 121 | .recomendaciones(resena.getRecomendaciones()) |
| 122 | .fechaResena(resena.getFechaResena()) |
| 123 | .build(); |
| 124 | } |
| 125 | |
| 126 | } |