Rev 284 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.domain.service.cliente.impl; |
| 2 | |||
| 3 | import java.io.ByteArrayOutputStream; |
||
| 4 | import java.io.FileNotFoundException; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.util.ArrayList; |
||
| 7 | import java.util.HashMap; |
||
| 8 | |||
| 9 | import org.apache.poi.hssf.usermodel.HSSFSheet; |
||
| 10 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
||
| 11 | import org.apache.poi.ss.usermodel.CellStyle; |
||
| 12 | import org.apache.poi.ss.usermodel.Row; |
||
| 13 | import org.primefaces.model.DefaultStreamedContent; |
||
| 14 | import org.primefaces.model.StreamedContent; |
||
| 15 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 16 | import org.springframework.stereotype.Service; |
||
| 17 | |||
| 18 | import br.com.ec.controller.util.ExcelUtil; |
||
| 195 | espaco | 19 | import br.com.ec.core.consulta.ParametrosConsulta; |
| 20 | import br.com.ec.core.exception.NegocioException; |
||
| 21 | import br.com.ec.core.generic.AbstractService; |
||
| 22 | import br.com.ec.core.generic.GenericRepository; |
||
| 23 | import br.com.ec.core.util.ArquivoUtil; |
||
| 24 | import br.com.ec.core.util.DataUtils; |
||
| 25 | import br.com.ec.core.util.TipoExtensao; |
||
| 26 | import br.com.ec.core.util.VerificadorUtil; |
||
| 27 | import br.com.ec.core.validador.Validador; |
||
| 106 | espaco | 28 | import br.com.ec.domain.model.Cliente; |
| 686 | blopes | 29 | import br.com.ec.domain.model.tipos.TipoCashback; |
| 30 | import br.com.ec.domain.service.cashback.CashbackService; |
||
| 106 | espaco | 31 | import br.com.ec.domain.service.cliente.ClienteService; |
| 32 | import br.com.ec.domain.service.pedido.PedidoService; |
||
| 33 | import br.com.ec.infrastructure.repository.ClienteRepository; |
||
| 34 | |||
| 35 | @Service |
||
| 36 | public class ClienteServiceImpl extends AbstractService<Cliente> implements ClienteService { |
||
| 37 | |||
| 38 | private PedidoService pedidoService; |
||
| 686 | blopes | 39 | private CashbackService cashbackService; |
| 106 | espaco | 40 | private ClienteRepository clienteRepository; |
| 41 | |||
| 42 | @Autowired |
||
| 686 | blopes | 43 | public ClienteServiceImpl(Validador validador, PedidoService pedidoService, CashbackService cashbackService, ClienteRepository clienteRepository) { |
| 106 | espaco | 44 | super(validador); |
| 45 | this.pedidoService = pedidoService; |
||
| 686 | blopes | 46 | this.cashbackService = cashbackService; |
| 106 | espaco | 47 | this.clienteRepository = clienteRepository; |
| 48 | } |
||
| 49 | |||
| 50 | @Override |
||
| 51 | protected GenericRepository<Cliente> getRepository() { |
||
| 52 | return clienteRepository; |
||
| 53 | } |
||
| 54 | |||
| 55 | @Override |
||
| 56 | protected void regrasNegocioCadastrar(Cliente cliente) { |
||
| 57 | cliente.setAtivo(true); |
||
| 58 | cliente.setDataCadastro(DataUtils.getDataAtual()); |
||
| 59 | verificarSeCpfCnpjJaExiste(cliente); |
||
| 60 | } |
||
| 61 | |||
| 62 | @Override |
||
| 63 | protected void regrasNegocioAlterar(Cliente cliente) { |
||
| 64 | super.regrasNegocioAlterar(cliente); |
||
| 65 | verificarSeCpfCnpjJaExiste(cliente); |
||
| 66 | } |
||
| 67 | |||
| 68 | private void verificarSeCpfCnpjJaExiste(Cliente cliente) { |
||
| 69 | if (VerificadorUtil.naoEstaNulo(clienteRepository.consultarClientePorCpfCnpj(cliente.getCpfCnpj(), cliente.getSequencial()))) { |
||
| 70 | throw new NegocioException("CPF/CNPJ JÁ EXISTE."); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public Cliente detalharCliente(Cliente cliente) { |
||
| 76 | Cliente clienteDetalhado = clienteRepository.detalharCliente(cliente); |
||
| 77 | clienteDetalhado.setPedidos(pedidoService.consultarPedidosDoCliente(cliente)); |
||
| 686 | blopes | 78 | clienteDetalhado.setListaCashback(cashbackService.consultarCashbackCliente(cliente.getSequencial())); |
| 106 | espaco | 79 | return clienteDetalhado; |
| 80 | } |
||
| 81 | |||
| 82 | @Override |
||
| 83 | public Cliente consultarClientePorCpfCnpj(String cpfCnpj) { |
||
| 686 | blopes | 84 | Cliente cliente = clienteRepository.consultarClientePorCpfCnpj(cpfCnpj, null); |
| 85 | cliente.setValorCashbackDisponivel(cashbackService.consultarSaldoCliente(cliente.getSequencial())); |
||
| 86 | return cliente; |
||
| 106 | espaco | 87 | } |
| 88 | |||
| 89 | @Override |
||
| 90 | public Cliente consultarClientePorCpfCnpjOuContato(String cpfCnpj, String contatoCliente) { |
||
| 91 | return clienteRepository.consultarClientePorCpfCnpjOuContato(cpfCnpj, contatoCliente); |
||
| 92 | } |
||
| 93 | |||
| 94 | @Override |
||
| 95 | protected void regrasNegocioExcluir(Cliente cliente) { |
||
| 96 | if (verificarSeClientePossuiVendas(cliente)) { |
||
| 97 | throw new NegocioException("Não é permitido excluir o cliente que possui vendas"); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | private boolean verificarSeClientePossuiVendas(Cliente cliente) { |
||
| 102 | return clienteRepository.verificarSeClientePossuiVendas(cliente); |
||
| 103 | } |
||
| 104 | |||
| 105 | @Override |
||
| 106 | public StreamedContent gerarArquivoExcel(ParametrosConsulta<Cliente> parametrosConsulta) { |
||
| 107 | HSSFWorkbook wb = new HSSFWorkbook(); |
||
| 108 | HSSFSheet aba = ExcelUtil.criarAbas(wb, "CLIENTES"); |
||
| 109 | |||
| 110 | CellStyle headerStyle = ExcelUtil.configurarCelulaCabecalho(wb); |
||
| 111 | Row linhaHeader = aba.createRow(0); |
||
| 112 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 0, headerStyle, "NOME"); |
||
| 113 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 1, headerStyle, "CPF/CNPJ"); |
||
| 114 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 2, headerStyle, "CONTATO"); |
||
| 115 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 3, headerStyle, "TELEFONE"); |
||
| 116 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 4, headerStyle, "EMAIL"); |
||
| 117 | |||
| 118 | // CONSULTAR |
||
| 119 | ArrayList<Cliente> clientes = (ArrayList<Cliente>) clienteRepository.consultarPassandoParametrosConsulta(parametrosConsulta, 0, |
||
| 120 | clienteRepository.obterQuantidadeDeRegistrosParametrosConsulta(parametrosConsulta), null, null, new HashMap<String, Object>()); |
||
| 121 | |||
| 122 | // PREENCHER |
||
| 123 | Integer numeroLinha = 1; |
||
| 284 | espaco | 124 | CellStyle estilo = ExcelUtil.configurarCelulaTexto(wb); |
| 106 | espaco | 125 | for (Cliente cliente : clientes) { |
| 126 | Row linha = aba.createRow(numeroLinha); |
||
| 284 | espaco | 127 | ExcelUtil.criarCelula(wb, aba, linha, 0, estilo, cliente.getNome()); |
| 128 | ExcelUtil.criarCelula(wb, aba, linha, 1, estilo, cliente.getCpfCnpjFormatado()); |
||
| 129 | ExcelUtil.criarCelula(wb, aba, linha, 2, estilo, cliente.getContato()); |
||
| 130 | ExcelUtil.criarCelula(wb, aba, linha, 3, estilo, cliente.getTelefone()); |
||
| 131 | ExcelUtil.criarCelula(wb, aba, linha, 4, estilo, cliente.getEmail()); |
||
| 106 | espaco | 132 | numeroLinha++; |
| 133 | } |
||
| 134 | |||
| 135 | try { |
||
| 136 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
||
| 137 | wb.write(stream); |
||
| 138 | stream.close(); |
||
| 139 | wb.close(); |
||
| 140 | return new DefaultStreamedContent(ArquivoUtil.gerarInputStreamDeArquivo(stream.toByteArray()), TipoExtensao.EXCEL.getDescricao(), "clientes.xls"); |
||
| 141 | } catch (FileNotFoundException e) { |
||
| 142 | e.printStackTrace(); |
||
| 143 | } catch (IOException e) { |
||
| 144 | e.printStackTrace(); |
||
| 145 | } |
||
| 146 | return null; |
||
| 147 | } |
||
| 148 | |||
| 149 | } |