package com.fp.armas.portal.dao.registro;

import java.util.List;

import javax.persistence.Query;

import com.fp.armas.portal.dao.general.GenericDaoImpl;
import com.fp.armas.portal.model.Tcustpersonaddress;
import com.fp.armas.portal.util.FechaUtil;
import com.fp.armas.portal.util.PortalMessages;
import com.fp.armas.portal.util.RegistroException;

/**
 * Objeto de acceso a datos de la tabla {@link Tcustpersonaddress}
 * @author dcruz
 *
 */
public class DireccionPersonaDao extends GenericDaoImpl<Tcustpersonaddress> {

	public DireccionPersonaDao() {
		super(Tcustpersonaddress.class);
	}

	@SuppressWarnings("unchecked")
	public Tcustpersonaddress buscaDireccionPrincipal(Integer personcode) throws RegistroException{
		Tcustpersonaddress direccioPrincipal = null;
		Query query = this.getEntityManager().createQuery("SELECT o FROM Tcustpersonaddress o WHERE o.id.personcode=:personcode AND  o.id.dateto=:dateto AND o.principal=:principal");
		query.setParameter("personcode", (long)personcode);
		query.setParameter("dateto", FechaUtil.obtenerFecha(2999, 11, 31));
		query.setParameter("principal", PortalMessages.getInstancia().getString("valor.general.si"));
		List<Tcustpersonaddress> listaddress = query.getResultList();
		if(listaddress != null && !listaddress.isEmpty()){
			direccioPrincipal = listaddress.iterator().next();
		}
		return direccioPrincipal;
	}
	
	/**
	 * Busca y retorna la primera direcci&oacute;n encontrada
	 * @param personcode
	 * @param tipodireccion
	 * @return
	 * @throws RegistroException
	 */
	@SuppressWarnings("unchecked")
	public Tcustpersonaddress buscaDireccionPorTipo(Integer personcode, String tipodireccion) throws RegistroException{
		Tcustpersonaddress direccioPrincipal = null;
		Query query = this.getEntityManager().createQuery("SELECT o FROM Tcustpersonaddress o WHERE o.id.personcode=:personcode AND  o.id.dateto=:dateto AND  o.addresstypecatalog=:tipodireccion AND o.addresstypecatalogcode=:tipodireccioncodigo");
		query.setParameter("personcode", (long)personcode);
		query.setParameter("dateto", FechaUtil.obtenerFecha(2999, 11, 31));
		query.setParameter("tipodireccion", tipodireccion);
		query.setParameter("tipodireccioncodigo", PortalMessages.getInstancia().getString("catalogo.codigo.tipo.direccion"));
		List<Tcustpersonaddress> listaddress = query.getResultList();
		if(listaddress != null && !listaddress.isEmpty()){
			direccioPrincipal = listaddress.iterator().next();
		}
		return direccioPrincipal;
	}
	
	/**
	 * Busca y retorna todas las direcciones encontradas
	 * @param personcode
	 * @param tipodireccion
	 * @return
	 * @throws RegistroException
	 */
	@SuppressWarnings("unchecked")
	public List<Tcustpersonaddress> buscaDireccionesPorTipo(Long personcode, String tipodireccion) throws RegistroException{		
		Query query = this.getEntityManager().createQuery("SELECT o FROM Tcustpersonaddress o WHERE o.id.personcode=:personcode AND  o.id.dateto=:dateto AND  o.addresstypecatalog=:tipodireccion AND o.addresstypecatalogcode=:tipodireccioncodigo");
		query.setParameter("personcode", (long)personcode);
		query.setParameter("dateto", FechaUtil.obtenerFecha(2999, 11, 31));
		query.setParameter("tipodireccion", tipodireccion);
		query.setParameter("tipodireccioncodigo", PortalMessages.getInstancia().getString("catalogo.codigo.tipo.direccion"));
		List<Tcustpersonaddress> listaddress = query.getResultList();
		if(listaddress != null && !listaddress.isEmpty()){
			return listaddress;
		}
		return null;
	}
}
