package com.dacrt.SBIABackend.service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.dacrt.SBIABackend.dto.Channelresume2Dto;
import com.dacrt.SBIABackend.dto.ChannelresumeDto;
import com.dacrt.SBIABackend.dto.ContinuitySection2Dto;
import com.dacrt.SBIABackend.dto.LabelDto;
import com.dacrt.SBIABackend.dto.LabelItemDtoReport;
import com.dacrt.SBIABackend.dto.LabelValueDtoReport;
import com.dacrt.SBIABackend.dto.LabelValueStringDto;
import com.dacrt.SBIABackend.dto.ResumenBiaContentSection1;
import com.dacrt.SBIABackend.dto.ResumenBiaContentSection2;
import com.dacrt.SBIABackend.dto.ResumenBiaContentSection3;
import com.dacrt.SBIABackend.dto.ResumenBiaContentSection4;
import com.dacrt.SBIABackend.dto.ResumenBiaSection1;
import com.dacrt.SBIABackend.dto.ResumenBiaSection2;
import com.dacrt.SBIABackend.dto.ResumenBiaSection3;
import com.dacrt.SBIABackend.dto.ResumenBiaSection4;
import com.dacrt.SBIABackend.dto.ResumenBiaTimeTableDto;
import com.dacrt.SBIABackend.dto.StatusDto2;
import com.dacrt.SBIABackend.security.dto.RespuestaDto;

@Service
public class BiaresumeproSection4Service {
	Logger logger = LoggerFactory.getLogger(BiaresumeproSection4Service.class);
	@PersistenceContext
	private EntityManager entityManager;
	
	
	 public ResumenBiaSection4 getResumeBiaSuppliers(int idcampana,int idunidad,int idprocesos) {
			ResumenBiaContentSection4 detalleResumenBiaContentSection4 = new ResumenBiaContentSection4();
			LabelDto detalleLabelDto= new LabelDto();
			LabelDto detalleLabelDto2 = new LabelDto();
			ResumenBiaSection4 detalleResumenBiaSection4 = new ResumenBiaSection4();
			List<LabelDto> listadoLabelDto=new ArrayList();
			RespuestaDto respuesta = new RespuestaDto("", false);
			HttpStatus estatus = HttpStatus.FORBIDDEN;	   			
		 try {	
		 String SentenciaBase = " SELECT "
		 		+ "   s.name,s.id "
		 		+ "FROM "
		 		+ "    main.evalprocesses ep "
		 		+ "    INNER JOIN main.unitprocesses up ON ep.unitprocessid = up.id "
		 		+ "	INNER JOIN main.campaignunits cu on ep.campaignunitid = cu.id "
		 		+ "	INNER JOIN main.evalprocsupliers es on ep.id = es.evalprocessid "
		 		+ "	INNER JOIN main.suppliers s on es.supplierid = s.id and s.status =1 "
		 		+ " WHERE "
				+ "    up.unitid =  " + idunidad 
				+ " AND up.processid =  " + idprocesos 
				+ " AND cu.campaingid = " + idcampana + " and ep.impactst = 1 and suplierst = 1 ";
			

		
			Query query;
			 query = entityManager.createNativeQuery(SentenciaBase);
			 
			   List<Object[]> listacompleta = query.getResultList();
			   int cuantosregistros1 = listacompleta.size();
			   
			   if (cuantosregistros1>0) {
				   for (Object[] fila : listacompleta) {  
					   detalleLabelDto= new LabelDto();
					   detalleLabelDto.setLabel((String) fila[0]);
					   listadoLabelDto.add(detalleLabelDto);
					   
				   }
				   detalleResumenBiaContentSection4.setSuppliers(listadoLabelDto);
				  } else {
					 // detalleResumenBiaContentSection3.setPeriods(new ArrayList<>());
					  detalleResumenBiaContentSection4.setSuppliers(new ArrayList<>());
				  }
			
			
			detalleResumenBiaSection4.setTitle("Proveedores");
			detalleResumenBiaSection4.setContent(detalleResumenBiaContentSection4);
			
										
						
				return detalleResumenBiaSection4;	    	 
	 }catch (Exception e) {
			// Manejo de excepciones
			respuesta = new RespuestaDto("Error interno del servidor", false);
			estatus = HttpStatus.INTERNAL_SERVER_ERROR;
			return detalleResumenBiaSection4;
		} finally {
			if (entityManager != null && entityManager.isOpen()) {
				entityManager.close();
			}
		}		 	    					
					
	 	}

}
