|
| 1 | +package com.carvalhotechsolutions.mundoanimal.controllers.gerenciamento; |
| 2 | + |
| 3 | +import com.carvalhotechsolutions.mundoanimal.model.Agendamento; |
| 4 | +import com.carvalhotechsolutions.mundoanimal.repositories.AgendamentoRepository; |
| 5 | +import javafx.application.Platform; |
| 6 | +import javafx.beans.binding.DoubleBinding; |
| 7 | +import javafx.collections.FXCollections; |
| 8 | +import javafx.collections.ObservableList; |
| 9 | +import javafx.fxml.FXML; |
| 10 | +import javafx.scene.Node; |
| 11 | +import javafx.scene.chart.BarChart; |
| 12 | +import javafx.scene.chart.CategoryAxis; |
| 13 | +import javafx.scene.chart.NumberAxis; |
| 14 | +import javafx.scene.chart.XYChart; |
| 15 | +import javafx.scene.control.Label; |
| 16 | +import javafx.scene.control.TableColumn; |
| 17 | +import javafx.scene.control.TableView; |
| 18 | +import javafx.scene.control.cell.PropertyValueFactory; |
| 19 | +import javafx.scene.text.Text; |
| 20 | +import javafx.util.StringConverter; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +public class InicioController { |
| 28 | + @FXML |
| 29 | + private Label nomeServico1, horarioAgendamento1, nomeCliente1; |
| 30 | + @FXML |
| 31 | + private Label nomeServico2, horarioAgendamento2, nomeCliente2; |
| 32 | + @FXML |
| 33 | + private Label nomeServico3, horarioAgendamento3, nomeCliente3; |
| 34 | + @FXML |
| 35 | + private Label nomeServico4, horarioAgendamento4, nomeCliente4; |
| 36 | + @FXML |
| 37 | + private TableView<Agendamento> tableView; |
| 38 | + @FXML |
| 39 | + private TableColumn<Agendamento, String> clienteColumn, servicoColumn, dataColumn; |
| 40 | + @FXML |
| 41 | + private BarChart barChart; |
| 42 | + @FXML |
| 43 | + private CategoryAxis xAxis; |
| 44 | + @FXML |
| 45 | + private NumberAxis yAxis; |
| 46 | + |
| 47 | + private AgendamentoRepository agendamentoRepository = new AgendamentoRepository(); |
| 48 | + |
| 49 | + @FXML |
| 50 | + public void initialize() { |
| 51 | + tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY_ALL_COLUMNS); |
| 52 | + |
| 53 | + DoubleBinding larguraDisponivel = tableView.widthProperty().subtract(354); |
| 54 | + |
| 55 | + clienteColumn.prefWidthProperty().bind(larguraDisponivel.multiply(0.35)); |
| 56 | + servicoColumn.prefWidthProperty().bind(larguraDisponivel.multiply(0.35)); |
| 57 | + dataColumn.prefWidthProperty().bind(larguraDisponivel.multiply(0.30)); |
| 58 | + |
| 59 | + clienteColumn.setCellValueFactory(new PropertyValueFactory<>("cliente")); |
| 60 | + servicoColumn.setCellValueFactory(new PropertyValueFactory<>("servico")); |
| 61 | + dataColumn.setCellValueFactory(new PropertyValueFactory<>("dataFormatada")); |
| 62 | + |
| 63 | + carregarProximosAgendamentos(); |
| 64 | + atualizarAgendamentosFinalizados(); |
| 65 | + configurarGrafico(); |
| 66 | + atualizarGraficoServicos(); |
| 67 | + } |
| 68 | + |
| 69 | + private void carregarProximosAgendamentos() { |
| 70 | + List<Agendamento> agendamentos = agendamentoRepository.findStatusPendente(); |
| 71 | + |
| 72 | + // Lista de Labels para facilitar a atribuição |
| 73 | + List<Label> servicos = Arrays.asList(nomeServico1, nomeServico2, nomeServico3, nomeServico4); |
| 74 | + List<Label> horarios = Arrays.asList(horarioAgendamento1, horarioAgendamento2, horarioAgendamento3, horarioAgendamento4); |
| 75 | + List<Label> clientes = Arrays.asList(nomeCliente1, nomeCliente2, nomeCliente3, nomeCliente4); |
| 76 | + |
| 77 | + for (int i = 0; i < 4; i++) { |
| 78 | + if (i < agendamentos.size()) { |
| 79 | + Agendamento agendamento = agendamentos.get(i); |
| 80 | + servicos.get(i).setText(agendamento.getServico().getNomeServico()); |
| 81 | + horarios.get(i).setText(agendamento.getDataHoraFormatada()); |
| 82 | + clientes.get(i).setText(agendamento.getCliente().getNome()); |
| 83 | + } else { |
| 84 | + // Definir mensagens padrão para os HBox vazios |
| 85 | + servicos.get(i).setText("Vago"); |
| 86 | + horarios.get(i).setText("-"); |
| 87 | + clientes.get(i).setText("-"); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void atualizarAgendamentosFinalizados() { |
| 93 | + List<Agendamento> ultimosFinalizados = agendamentoRepository.findUltimosFinalizados(5); |
| 94 | + |
| 95 | + // Atualiza os dados da TableView |
| 96 | + tableView.getItems().setAll(ultimosFinalizados); |
| 97 | + } |
| 98 | + |
| 99 | + public void atualizarGraficoServicos() { |
| 100 | + // Executar dentro do thread do JavaFX |
| 101 | + Platform.runLater(() -> { |
| 102 | + List<Agendamento> agendamentos = agendamentoRepository.findFinalizadosUltimaSemana(); |
| 103 | + |
| 104 | + // Contar quantas vezes cada serviço foi realizado |
| 105 | + Map<String, Long> contagemServicos = agendamentos.stream() |
| 106 | + .collect(Collectors.groupingBy(a -> a.getServico().getNomeServico(), Collectors.counting())); |
| 107 | + |
| 108 | + // Encontrar o maior valor |
| 109 | + long maxValue = contagemServicos.values().stream() |
| 110 | + .mapToLong(Long::longValue) |
| 111 | + .max() |
| 112 | + .orElse(0); |
| 113 | + |
| 114 | + // Configurar o eixo Y com base no valor máximo |
| 115 | + yAxis = (NumberAxis) barChart.getYAxis(); |
| 116 | + // Arredondar para cima para o próximo número inteiro |
| 117 | + int upperBound = (int) Math.ceil(maxValue); |
| 118 | + yAxis.setUpperBound(upperBound); |
| 119 | + yAxis.setLowerBound(0); |
| 120 | + // Definir o número de marcações desejado |
| 121 | + int tickUnit = calculateTickUnit(upperBound); |
| 122 | + yAxis.setTickUnit(tickUnit); |
| 123 | + // Forçar apenas números inteiros |
| 124 | + yAxis.setAutoRanging(false); |
| 125 | + |
| 126 | + // Criar nova série de dados |
| 127 | + XYChart.Series<String, Number> series = new XYChart.Series<>(); |
| 128 | + series.setName("Quantidade"); |
| 129 | + |
| 130 | + // Limpar dados existentes |
| 131 | + barChart.getData().clear(); |
| 132 | + |
| 133 | + // Atualizar categorias do eixo X |
| 134 | + CategoryAxis xAxis = (CategoryAxis) barChart.getXAxis(); |
| 135 | + xAxis.getCategories().clear(); |
| 136 | + |
| 137 | + // Adicionar novas categorias |
| 138 | + ObservableList<String> categorias = FXCollections.observableArrayList(contagemServicos.keySet()); |
| 139 | + xAxis.setCategories(categorias); |
| 140 | + |
| 141 | + // Adicionar dados à série |
| 142 | + contagemServicos.forEach((servico, quantidade) -> |
| 143 | + series.getData().add(new XYChart.Data<>(servico, quantidade)) |
| 144 | + ); |
| 145 | + |
| 146 | + // Adicionar série ao gráfico |
| 147 | + barChart.getData().add(series); |
| 148 | + |
| 149 | + |
| 150 | + // Forçar atualização do layout |
| 151 | + barChart.layout(); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + // Método auxiliar para calcular o intervalo de marcação ideal |
| 156 | + private int calculateTickUnit(int maxValue) { |
| 157 | + if (maxValue <= 5) return 1; |
| 158 | + if (maxValue <= 10) return 2; |
| 159 | + if (maxValue <= 20) return 4; |
| 160 | + if (maxValue <= 50) return 5; |
| 161 | + if (maxValue <= 100) return 10; |
| 162 | + return Math.max(1, maxValue / 10); // Para valores maiores, dividir em aproximadamente 10 intervalos |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + // Método auxiliar para configuração inicial do gráfico |
| 167 | + public void configurarGrafico() { |
| 168 | + yAxis.setTickUnit(1); // Incremento de 1 em 1 |
| 169 | + yAxis.setMinorTickVisible(false); |
| 170 | + barChart.setAnimated(false); // Desabilitar animações para evitar problemas de atualização |
| 171 | + barChart.setLegendVisible(false); |
| 172 | + barChart.setStyle("-fx-font-size: 14px;"); |
| 173 | + } |
| 174 | + |
| 175 | + public void atualizarProximosAgendamentos() { |
| 176 | + Platform.runLater(this::carregarProximosAgendamentos); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments