package br.com.ec.domain.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import org.primefaces.model.charts.ChartData;
import org.primefaces.model.charts.axes.cartesian.CartesianScales;
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes;
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearTicks;
import org.primefaces.model.charts.bar.BarChartDataSet;
import org.primefaces.model.charts.bar.BarChartModel;
import org.primefaces.model.charts.bar.BarChartOptions;
import org.primefaces.model.charts.optionconfig.animation.Animation;
import org.primefaces.model.charts.optionconfig.legend.Legend;
import org.primefaces.model.charts.optionconfig.legend.LegendLabel;
import org.primefaces.model.charts.optionconfig.title.Title;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import br.com.ec.core.generic.AbstractService;
import br.com.ec.core.generic.GenericRepository;
import br.com.ec.core.util.DataUtils;
import br.com.ec.core.validador.Validador;
import br.com.ec.domain.dto.consulta.ParametrosConsultaVendasDTO;
import br.com.ec.domain.dto.grafico.GraficoBarraDTO;
import br.com.ec.domain.dto.grafico.GraficoDTO;
import br.com.ec.domain.service.GraficoService;
import br.com.ec.domain.service.VendaService;
@Service
public class GraficoServiceImpl
extends AbstractService
<GraficoDTO
> implements GraficoService
{
// private DashboardRepository dashboardRepository;
private VendaService vendaService
;
@Autowired
public GraficoServiceImpl
(Validador validador, VendaService vendaService
/*, DashboardRepository dashboardRepository*/) {
super(validador
);
this.
vendaService = vendaService
;
// this.dashboardRepository = dashboardRepository;
}
@
Override
protected GenericRepository
<GraficoDTO
> getRepository
() {
// return dashboardRepository;
return null;
}
@
Override
public GraficoBarraDTO gerarGraficoVendas
(ParametrosConsultaVendasDTO parametrosConsulta
) {
GraficoBarraDTO graficoLinhaDTO =
new GraficoBarraDTO
();
BarChartModel graficoBarra =
new BarChartModel
();
ChartData data =
new ChartData
();
BarChartDataSet barDataSet =
new BarChartDataSet
();
barDataSet.
setLabel("");
List<Number> valores =
new ArrayList<>();
List<String> cores =
new ArrayList<>();
List<String> bordas =
new ArrayList<>();
List<String> marcadores =
new ArrayList<>();
for (int mes =
1; mes
<=
12; mes++
) {
Double valorAcumulado =
new Double(0.0);
Calendar dataInicial = DataUtils.
obterCalendario(DataUtils.
obterData(1,
new Integer(mes
)-
1,
new Integer(parametrosConsulta.
getParametrosPeriodoDTO().
getAno())));
int qntMaximaDias = dataInicial.
getActualMaximum(Calendar.
DAY_OF_MONTH);
Calendar dataFinal = DataUtils.
obterCalendario(DataUtils.
obterData(qntMaximaDias,
new Integer(mes
)-
1,
new Integer(parametrosConsulta.
getParametrosPeriodoDTO().
getAno())));
parametrosConsulta.
getParametrosPeriodoDTO().
setDataInicial(dataInicial.
getTime());
parametrosConsulta.
getParametrosPeriodoDTO().
setDataFinal(dataFinal.
getTime());
valorAcumulado = vendaService.
obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(
parametrosConsulta,
null/* Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO()) */);
valores.
add(valorAcumulado
);
cores.
add("rgba(75, 192, 192, 0.2)");
bordas.
add("rgb(75, 192, 192)");
marcadores.
add("" + mes
);
}
barDataSet.
setData(valores
);
barDataSet.
setBackgroundColor(cores
);
barDataSet.
setBorderColor(bordas
);
barDataSet.
setBorderWidth(1);
data.
addChartDataSet(barDataSet
);
data.
setLabels(marcadores
);
graficoBarra.
setData(data
);
//Options
BarChartOptions options =
new BarChartOptions
();
CartesianScales cScales =
new CartesianScales
();
CartesianLinearAxes linearAxes =
new CartesianLinearAxes
();
linearAxes.
setOffset(true);
CartesianLinearTicks ticks =
new CartesianLinearTicks
();
ticks.
setBeginAtZero(true);
linearAxes.
setTicks(ticks
);
cScales.
setDisplay(true);
cScales.
addYAxesData(linearAxes
);
options.
setScales(cScales
);
Title title =
new Title
();
title.
setText("VENDAS");
options.
setTitle(title
);
Legend legend =
new Legend
();
legend.
setDisplay(true);
legend.
setPosition("top");
LegendLabel legendLabels =
new LegendLabel
();
legendLabels.
setFontStyle("bold");
legendLabels.
setFontColor("#2980B9");
legendLabels.
setFontSize(24);
legend.
setLabels(legendLabels
);
options.
setLegend(legend
);
// disable animation
Animation animation =
new Animation
();
animation.
setDuration(0);
options.
setAnimation(animation
);
graficoBarra.
setOptions(options
);
graficoLinhaDTO.
setGraficoBarra(graficoBarra
);
return graficoLinhaDTO
;
}
}