From b5ebed25f9b38e7e457bd88c9f489690fd639a05 Mon Sep 17 00:00:00 2001 From: walace Date: Mon, 13 May 2019 14:42:39 +0000 Subject: [PATCH] fixes bug#14052 qua:renato dev: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Web/trunk/ventaboletos@93150 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../RelatorioEmbarqueLocalidadeBean.java | 83 +++- ...RelatorioEmbarqueLocalidadeController.java | 365 ++++++++++-------- 2 files changed, 282 insertions(+), 166 deletions(-) diff --git a/src/java/com/rjconsultores/ventaboletos/relatorios/utilitarios/RelatorioEmbarqueLocalidadeBean.java b/src/java/com/rjconsultores/ventaboletos/relatorios/utilitarios/RelatorioEmbarqueLocalidadeBean.java index f9748e595..65f21009c 100644 --- a/src/java/com/rjconsultores/ventaboletos/relatorios/utilitarios/RelatorioEmbarqueLocalidadeBean.java +++ b/src/java/com/rjconsultores/ventaboletos/relatorios/utilitarios/RelatorioEmbarqueLocalidadeBean.java @@ -1,33 +1,94 @@ + package com.rjconsultores.ventaboletos.relatorios.utilitarios; public class RelatorioEmbarqueLocalidadeBean { - protected String data; - protected String linha; - protected Long embarques; - + private String data; + private String horario; + private String linha; + private String classe; + private Long embarques; + private Integer indexColun; + private Integer indexLinha; + + public String getHorario() { + return horario; + } + + public void setHorario(String horario) { + this.horario = horario; + } + public String getData() { return data.substring(0, 10); } - - public String getHorario(){ - return data.substring(11, data.length()); - } - + public void setData(String data) { this.data = data; } + public String getLinha() { return linha; } + public void setLinha(String linha) { this.linha = linha; } + public Long getEmbarques() { return embarques; } + public void setEmbarques(Long embarques) { this.embarques = embarques; } - - + + public Integer getIndexColun() { + return indexColun; + } + + public void setIndexColun(Integer indexColun) { + this.indexColun = indexColun; + } + + public Integer getIndexLinha() { + return indexLinha; + } + + public void setIndexLinha(Integer indexLinha) { + this.indexLinha = indexLinha; + } + + public String getClasse() { + return classe; + } + + public void setClasse(String classe) { + this.classe = classe; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((linha == null) ? 0 : linha.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RelatorioEmbarqueLocalidadeBean other = (RelatorioEmbarqueLocalidadeBean) obj; + if (linha == null) { + if (other.linha != null) + return false; + } else if (!linha.equals(other.linha)) + return false; + return true; + } + } diff --git a/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioEmbarqueLocalidadeController.java b/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioEmbarqueLocalidadeController.java index a4bbef920..d49e05adc 100644 --- a/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioEmbarqueLocalidadeController.java +++ b/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioEmbarqueLocalidadeController.java @@ -9,10 +9,12 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; import javax.sql.DataSource; @@ -23,7 +25,10 @@ import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -99,9 +104,10 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp private Ruta ruta; private OrgaoConcedente orgao; private Parada origem; - + private int total; + private Map mapAux = new HashMap(); - private List lsDadosRelatorio; + private List ls; private void executarRelatorio() throws Exception { @@ -119,15 +125,15 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp - String sql = getSql(empresa.getEmpresaId().toString(), - ruta != null ? ruta.getRutaId().toString() : null, + String sql = getSql(empresa, ruta != null ? ruta.getRutaId().toString() : null, orgao != null ? orgao.getOrgaoConcedenteId().toString() : null, origem != null ? origem.getParadaId().toString() : null, horInicio, horFim); + ResultSet rset = null; try{ + NamedParameterStatement stmt = new NamedParameterStatement(dataSourceRead.getConnection(), sql); - ResultSet rset = null; - + stmt.setString(FILED_FEC_INICIO, fecInicio); stmt.setString(FIELD_FEC_FINAL, fecFinal); if (empresa != null) { @@ -152,131 +158,137 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp } rset = stmt.executeQuery(); - lsDadosRelatorio = new ArrayList(); Map>> datas = ResultSetToList(rset); - exportaExcel(datas); } catch (Exception e){ log.error("", e); - } - } - - private void exportaExcel(Map>> datas){ - try{ - HSSFWorkbook wb = new HSSFWorkbook(); - - HSSFCellStyle estiloVerticalMerge = wb.createCellStyle(); - estiloVerticalMerge.setVerticalAlignment(CellStyle.VERTICAL_CENTER); - - HSSFSheet sheet1 = null; - - int linha = NUM_LINHA_INICIO; - int coluna = 1; - HSSFRow row = null; - - for (Entry>> data : datas.entrySet()){ - // entry.getKey() => Data - sheet1 = wb.createSheet(data.getKey().replaceAll("/", "-")); - geraCabecalho(sheet1, data.getKey(), wb); - int totalGeral = 0; - for (Entry> horario : data.getValue().entrySet()){ - row = sheet1.createRow(linha); - HSSFCell cellHorario = row.createCell(0); - cellHorario.setCellValue(horario.getKey()); - cellHorario.setCellStyle(estiloVerticalMerge); - - HSSFCell cellTotal = row.createCell(7); - - List rows = horario.getValue(); - - int rowcount = (int) Math.ceil((double)rows.size() / 3) -1; - - - sheet1.addMergedRegion(new CellRangeAddress( - linha, //first row (0-based) - linha+rowcount, //last row (0-based) - 0, //first column (0-based) - 0 //last column (0-based) - )); - - sheet1.addMergedRegion(new CellRangeAddress( - linha, //first row (0-based) - linha+rowcount, //last row (0-based) - 7, //first column (0-based) - 7 //last column (0-based) - )); - - int beancount = 1; - int embarquesTotais = 0; - for (RelatorioEmbarqueLocalidadeBean bean : rows){ - if (beancount == 4){ - linha++; - row = sheet1.createRow(linha); - coluna = 1; - } - row.createCell(coluna++).setCellValue(bean.getLinha()); - row.createCell(coluna++).setCellValue(bean.getEmbarques()); - embarquesTotais += bean.getEmbarques(); - beancount++; - } - - cellTotal.setCellValue(embarquesTotais); - cellTotal.setCellStyle(estiloVerticalMerge); - - linha++; - coluna = 1; - - totalGeral += embarquesTotais; - } - - row = sheet1.createRow(linha); - HSSFCell cellTotal = row.createCell(0); - cellTotal.setCellValue("TOTAL"); - HSSFCell cellTotalValue = row.createCell(7); - cellTotalValue.setCellValue(totalGeral); - - linha = NUM_LINHA_INICIO; - - sheet1.autoSizeColumn(0); - sheet1.autoSizeColumn(1); - sheet1.autoSizeColumn(2); - sheet1.autoSizeColumn(3); - sheet1.autoSizeColumn(4); - sheet1.autoSizeColumn(5); - sheet1.autoSizeColumn(6); - sheet1.autoSizeColumn(7); - - } - if(datas.isEmpty()){ - Messagebox.show( - Labels.getLabel("relatorio.parametro.msgNoData"), - Labels.getLabel("relatorioEmbarqueLocalidadeController.window.title"), - Messagebox.OK, Messagebox.INFORMATION); - return; - } - - String nomeArquivo = - UsuarioLogado.getUsuarioLogado().getNombusuario() - + "_" + Calendar.getInstance().getTime().getTime() - + "_" + "EmbarqueLocalidade"; - - File fNomeArquivo = File.createTempFile(nomeArquivo, ".tmp"); - - FileOutputStream stream = new FileOutputStream(fNomeArquivo); - stream.flush(); - wb.write(stream); - stream.close(); - - InputStream is = null; - is = new FileInputStream(fNomeArquivo); - Filedownload.save(is, "application/xls", "EmbarqueLocalidade.xls"); - - fNomeArquivo.delete(); - } catch (Exception e){ - log.error("", e); + }finally { + if(rset != null && !rset.isClosed()) { + rset.close(); + } } } + private void exportaExcel(Map>> datas) { + try { + HSSFWorkbook wb = new HSSFWorkbook(); + + HSSFCellStyle estiloVerticalMerge = wb.createCellStyle(); + estiloVerticalMerge.setVerticalAlignment(CellStyle.ALIGN_CENTER); + + HSSFFont font = wb.createFont(); + HSSFCellStyle style = wb.createCellStyle(); + font.setFontName(HSSFFont.FONT_ARIAL); + font.setFontHeightInPoints((short)10); + font.setBoldweight(Font.BOLDWEIGHT_BOLD); + style.setFont(font); + + HSSFSheet sheet1 = null; + + int linha = NUM_LINHA_INICIO; + HSSFRow row = null; + + sheet1 = wb.createSheet("07-08-19"); + geraCabecalho(sheet1, "07-08-19", wb); + + Map linhas = new HashMap(); + int totalGeral = 0; + + int linhaTotalizador = 6; + HSSFCell cellTotal = null ; + for (RelatorioEmbarqueLocalidadeBean bean : ls) { + + int indexColun = mapAux.get(bean.getHorario()); + + if(!linhas.containsKey(bean.getLinha())) { + linhas.put(bean.getLinha(), linha); + row = sheet1.createRow(linha); + linhaTotalizador = linha; + linha++; + + }else { + row = sheet1.getRow(linhas.get(bean.getLinha())); + linhaTotalizador = sheet1.getRow(linhas.get(bean.getLinha())).getRowNum(); + } + + HSSFCell cellHorario = row.createCell(0); + cellHorario.setCellStyle(estiloVerticalMerge); + + cellTotal = row.createCell(mapAux.size() + 3); + + row.createCell(indexColun).setCellValue(bean.getEmbarques()); + row.createCell(0).setCellValue(bean.getLinha()); + row.createCell(1).setCellValue(bean.getClasse()); + + cellTotal.setCellStyle(estiloVerticalMerge); + String colluna; + String celTotal ; + colluna = CellReference.convertNumToColString(mapAux.size()+2); + celTotal = "SUM(D"+(linhaTotalizador+1)+":"+colluna+(linhaTotalizador+1)+")"; + row.createCell(mapAux.size()+3).setCellValue(celTotal); + cellTotal.setCellFormula(celTotal); + cellTotal.setCellStyle(style); + + totalGeral += bean.getEmbarques(); + } + + row = sheet1.createRow(linha); + int qtdLinhas = linhas.size()+5; + + int somatorioColuna = 3; + for (int i = 3; i < mapAux.size()+3; i++) { + + String colluna = CellReference.convertNumToColString(i); + cellTotal = row.createCell(somatorioColuna); + cellTotal.setCellFormula("SUM("+colluna+6+":"+colluna+(qtdLinhas)+")"); + cellTotal.setCellStyle(style); + somatorioColuna++; + + } + + HSSFCell cellTotalValue = row.createCell(total); + cellTotalValue = row.createCell(mapAux.size()+3); + cellTotalValue.setCellValue(totalGeral); + cellTotalValue.setCellStyle(style); + + + linha = NUM_LINHA_INICIO; + + for (int i = 0; i <= total; i++) { + sheet1.autoSizeColumn(i); + } + + if(mapAux.isEmpty()){ + Messagebox.show( + Labels.getLabel("relatorio.parametro.msgNoData"), + Labels.getLabel("relatorioEmbarqueLocalidadeController.window.title"), + Messagebox.OK, Messagebox.INFORMATION); + return; + } + + String nomeArquivo = + UsuarioLogado.getUsuarioLogado().getNombusuario() + + "_" + Calendar.getInstance().getTime().getTime() + + "_" + "EmbarqueLocalidade"; + + File fNomeArquivo = File.createTempFile(nomeArquivo, ".tmp"); + + FileOutputStream stream = new FileOutputStream(fNomeArquivo); + stream.flush(); + wb.write(stream); + stream.close(); + + InputStream is = null; + is = new FileInputStream(fNomeArquivo); + Filedownload.save(is, "application/xls", "EmbarqueLocalidade.xls"); + + fNomeArquivo.delete(); + }catch (Exception e) { + e.printStackTrace(); + } + + } + private void geraCabecalhoTitulo(HSSFSheet sheet1, HSSFWorkbook wb){ HSSFRow titulo = sheet1.createRow(NUM_LINHA_TITULO); sheet1.addMergedRegion(new CellRangeAddress( @@ -322,21 +334,66 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp 1, //first column (0-based) 7 //last column (0-based) )); - + HSSFFont font = wb.createFont(); + HSSFCellStyle style = wb.createCellStyle(); + font.setFontName(HSSFFont.FONT_ARIAL); + font.setFontHeightInPoints((short)10); + font.setBoldweight(Font.BOLDWEIGHT_BOLD); + style.setFont(font); //Montando o Cabeçalho HSSFRow r0 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA); - r0.createCell(0).setCellValue("DATA: " + dataAtual); - r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa()); + if(empresa != null) { + r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa()); + r0.getCell(1).setCellStyle(style); + r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa()); + r0.getCell(1).setCellStyle(style); + font.setColor(HSSFColor.RED.index); + style.setFont(font); + }else { + + } + + + HSSFRow r1 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA+1); - r1.createCell(0).setCellValue("FAIXA HORÁRIA"); - r1.createCell(1).setCellValue("Prefixo / Nome Linha"); - r1.createCell(2).setCellValue("Embarques"); - r1.createCell(3).setCellValue("Prefixo / Nome Linha"); - r1.createCell(4).setCellValue("Embarques"); - r1.createCell(5).setCellValue("Prefixo / Nome Linha"); - r1.createCell(6).setCellValue("Embarques"); - r1.createCell(7).setCellValue("TOTAL"); + List lsAux = new ArrayList(); + + mapAux = new HashMap(); + Map map = new HashMap(); + + int i = 3; + for (RelatorioEmbarqueLocalidadeBean bean : ls) { + + if(map.containsKey(bean.getHorario())) { + Integer indice = map.get(bean.getHorario()); + map.put(bean.getHorario(), indice); + bean.setIndexColun(indice); + }else { + map.put(bean.getHorario(),i); + bean.setIndexColun(i); + i++; + } + lsAux.add(bean); + } + i = 3; + mapAux = new TreeMap(map); + HSSFFont fontHorario = wb.createFont(); + HSSFCellStyle styleHorario = wb.createCellStyle(); + fontHorario.setColor(HSSFColor.BLACK.index); + fontHorario.setBoldweight(Font.BOLDWEIGHT_BOLD); + styleHorario.setFont(fontHorario); + + for (Entry bean : mapAux.entrySet()) { + r1.createCell(bean.getValue()).setCellValue(bean.getKey()); + r1.getCell(bean.getValue()).setCellStyle(styleHorario); + i++; + } + + + r1.createCell(i++).setCellValue("TOTAL"); + r1.setRowStyle(styleHorario); + total = i; } public void onClick$btnExecutarRelatorio(Event ev) throws Exception { @@ -364,16 +421,17 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp cmbOrigem.setModel(new ListModelList(lsOrigens)); } - protected String getSql(String empresa, String ruta, String orgao, String origem, String horInicio, String horFim) { + protected String getSql(Empresa empresa, String ruta, String orgao, String origem, String horInicio, String horFim) { StringBuilder sql = new StringBuilder(); sql.append("select "); - sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'DD/MM/YYYY HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), "); - sql.append(" concat(concat(r.prefixo, ' '), r.descruta), "); + sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), "); + sql.append(" concat(concat(r.prefixo, ' '), r.descruta), cla.descclase, "); sql.append(" count(b.boleto_id) "); sql.append("from "); sql.append(" boleto b "); sql.append(" inner join ruta r on r.ruta_id = b.ruta_id "); + sql.append(" inner join clase_servicio cla on cla.claseservicio_id = r.claseservicio_id "); sql.append(" inner join marca m on m.marca_id = b.marca_id "); sql.append("where "); sql.append(" b.fechorviaje between TO_DATE(:fecInicio, 'DD/MM/YYYY HH24:MI:SS') AND TO_DATE(:fecFinal, 'DD/MM/YYYY HH24:MI:SS') "); @@ -384,7 +442,7 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp if (orgao != null && !orgao.isEmpty()){ sql.append(" and r.orgaoconcedente_id = :orgao "); } - if (!empresa.isEmpty()){ + if (empresa != null){ sql.append(" and m.empresa_id = :empresa "); } if (ruta != null && !ruta.isEmpty() && !ruta.equals("-1")){ @@ -395,37 +453,33 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp } sql.append("group by "); - sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'DD/MM/YYYY HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), "); - sql.append(" concat(concat(r.prefixo, ' '), r.descruta) "); + sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), "); + sql.append(" concat(concat(r.prefixo, ' '), r.descruta), cla.descclase "); sql.append("order by "); sql.append(" 1 "); - + return sql.toString(); } protected Map>> ResultSetToList(ResultSet rset) throws SQLException { Map>> datas = new LinkedHashMap>>(); + ls = new ArrayList(); while (rset.next()) { RelatorioEmbarqueLocalidadeBean bean = new RelatorioEmbarqueLocalidadeBean(); - bean.setData(rset.getString(1)); + bean.setHorario(rset.getString(1)); bean.setLinha(rset.getString(2)); - bean.setEmbarques(rset.getLong(3)); - - if (!datas.containsKey(bean.getData())){ - datas.put(bean.getData(), new LinkedHashMap>()); - } - - if (!datas.get(bean.getData()).containsKey(bean.getHorario())){ - datas.get(bean.getData()).put(bean.getHorario(), new ArrayList()); - } - - datas.get(bean.getData()).get(bean.getHorario()).add(bean); + bean.setClasse(rset.getString(3)); + bean.setEmbarques(rset.getLong(4)); + ls.add(bean);; } + return datas; } - + public void checarLinhasDuplicadas() { + + } public Datebox getDatInicial() { return datInicial; } @@ -523,3 +577,4 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp } } + \ No newline at end of file