package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.ComissaoLogDAO;
import com.rjconsultores.ventaboletos.entidad.ComissaoLog;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.vo.comissao.ComissaoLogVO;

@Repository("comissaoLogHibernateDAO")
public class ComissaoLogHibernateDAO extends GenericHibernateDAO<ComissaoLog, Integer> implements ComissaoLogDAO {
	
	@Autowired
	public ComissaoLogHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
		setSessionFactory(factory);
	}	
	
	@Override
	public void limparComissaoLog(Integer empresaId, Integer puntoVentaId, Date dataInicial, Date dataFinal) {
		StringBuilder hql = new StringBuilder();
		hql.append("delete comissao_log ");
		hql.append("where activo = 1 ");
		hql.append("  AND puntoventa_id = :puntoventaId ");
		hql.append("  AND empresa_id = :empresaId ");
		hql.append("  AND feccorte between TO_DATE(:dataInicial,'DD/MM/YYYY hh24:mi') and TO_DATE(:dataFinal,'DD/MM/YYYY hh24:mi') ");

		Query query = getSession().createSQLQuery(hql.toString());
		query.setString("dataInicial", DateUtil.getStringDate(DateUtil.inicioFecha(dataInicial), "dd/MM/yyyy HH:mm"));
		query.setString("dataFinal", DateUtil.getStringDate(DateUtil.inicioFecha(dataFinal), "dd/MM/yyyy HH:mm"));
		query.setInteger("puntoventaId", puntoVentaId);
		query.setInteger("empresaId", empresaId);
		
		query.executeUpdate();
	}
	
	@Override
	public void suscribirTodos(List<ComissaoLogVO> lsComissaoLogVO) {
		for (ComissaoLogVO vo : lsComissaoLogVO) {
			ComissaoLog comissaoLog = new ComissaoLog();
			comissaoLog.setPuntoventaId(vo.getPuntoventaId());
			comissaoLog.setEmpresaId(vo.getEmpresaId());
			comissaoLog.setActivo(true);
			comissaoLog.setFecmodif(new Date());
			comissaoLog.setFeccorte(vo.getFeccorte());
			comissaoLog.setUsuarioId(vo.getUsuarioId());
			comissaoLog.setTurnoId(vo.getTurnoId());
			comissaoLog.setCajaId(vo.getCajaId());
			comissaoLog.setCajadiversosId(vo.getCajadiversosId());
			comissaoLog.setPorcComissao(vo.getPorcComissao());
			comissaoLog.setValorBase(vo.getValorBase());
			comissaoLog.setValorComissao(vo.getValorComissao());
			comissaoLog.setOrigem(vo.getOrigem());
			comissaoLog.setTexto(vo.getTexto());
			
			suscribir(comissaoLog);
		}
	}

}
