|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
package br.com.ae3tecnologia.ms.tangerino.controller;
|
|
|
|
|
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.dto.ResultDTO;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.dto.ResultEventDTO;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.service.PtoAjusteService;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.service.PtoEquipamentoService;
|
|
|
|
|
import br.com.ae3tecnologia.ms.tangerino.service.PtoEventoService;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
@ -9,15 +11,21 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
|
|
import static br.com.ae3tecnologia.ms.tangerino.utils.Constants.API_ROUTE;
|
|
|
|
@ -25,48 +33,76 @@ import static br.com.ae3tecnologia.ms.tangerino.utils.Constants.TANGERINO_API_PU
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@CrossOrigin(maxAge = 3600, origins = "*")
|
|
|
|
|
@RequestMapping(API_ROUTE+"/${app.version}/run")
|
|
|
|
|
@RequestMapping(API_ROUTE + "/${app.version}/run")
|
|
|
|
|
public class PtoEventoController {
|
|
|
|
|
|
|
|
|
|
@Value("${app.token}")
|
|
|
|
|
private String token;
|
|
|
|
|
@Value("${app.url.apis}")
|
|
|
|
|
private String urlApis;
|
|
|
|
|
@Value("${app.url.employer}")
|
|
|
|
|
private String urlEmployer;
|
|
|
|
|
@Value("${server.port}")
|
|
|
|
|
private String serverPort;
|
|
|
|
|
|
|
|
|
|
private PtoEventoService eventoService;
|
|
|
|
|
private PtoEquipamentoService equipamentoService;
|
|
|
|
|
private final PtoEventoService eventoService;
|
|
|
|
|
private final PtoAjusteService ajusteService;
|
|
|
|
|
private final PtoEquipamentoService equipamentoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PtoEventoController(PtoEquipamentoService equipamentoService, PtoEventoService eventoService){
|
|
|
|
|
private PtoEventoController(PtoEquipamentoService equipamentoService, PtoEventoService eventoService, PtoAjusteService ajusteService) {
|
|
|
|
|
this.eventoService = eventoService;
|
|
|
|
|
this.equipamentoService = equipamentoService;
|
|
|
|
|
this.ajusteService = ajusteService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping(
|
|
|
|
|
produces = MediaType.APPLICATION_JSON_VALUE
|
|
|
|
|
)
|
|
|
|
|
public void extrairPontos() throws URISyntaxException, JsonProcessingException {
|
|
|
|
|
public void extrairPontos() {
|
|
|
|
|
|
|
|
|
|
List<PtoEquipamentoModel> equipamentoList = obterRelogios(false);
|
|
|
|
|
equipamentoList.forEach(eq -> {
|
|
|
|
|
// percorre cada relogio, passando o ultimo nsr do relogio ou a ulima data/hora
|
|
|
|
|
AtomicReference<Integer> lastNsr = new AtomicReference<>(0);
|
|
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
|
|
|
map.put("serialNumber", eq.getCodigo());
|
|
|
|
|
map.put("nsrType", 3);
|
|
|
|
|
map.put("nsr", eq.getUltimoNsr()); //precisa pegar o ultimo NSR do relogio para passar como parametro
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// requisita da API do tangerino os pontos disponiveis, salvando eles na tabela ptoEvento
|
|
|
|
|
List<PtoEventoModel> ptoEventoModels = obterPontosPorRelogio(map);
|
|
|
|
|
lastNsr.set(extrairEventos(lastNsr.get(), ptoEventoModels, eq));
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
// ao final, atualiza a ptoEquipamento com o ultimoNSR salvo
|
|
|
|
|
eq.setUltimoNsr(lastNsr.get());
|
|
|
|
|
equipamentoService.saveOrUpdate(eq);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// pega todos os relogios ativos de PtoEquipamentoModel
|
|
|
|
|
// percorre cada relogio, passando o ultimo nsr do relogio ou a ulima data/hora, tratando para nao duplicar os nsr
|
|
|
|
|
AtomicReference<Integer> lastNsr = new AtomicReference<>(0); //precisa pegar o ultimo NSR do relogio para passar como parametro
|
|
|
|
|
HashMap<String, String> map = new HashMap<>();
|
|
|
|
|
map.put("employerId", String.valueOf(2775694));
|
|
|
|
|
map.put("offset", String.valueOf(1037355));
|
|
|
|
|
List<PtoEventoModel> ptoEventoModels = obterPontosPorRelogio(map);
|
|
|
|
|
ptoEventoModels.stream().sorted(Comparator.comparing(PtoEventoModel::getNsr)).forEach(x-> {
|
|
|
|
|
// requisita da API do tangerino os pontos disponiveis, salvando eles na tabela ptoEvento
|
|
|
|
|
lastNsr.set(x.getNsr());
|
|
|
|
|
eventoService.save(x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer extrairEventos(Integer lastNsr, List<PtoEventoModel> ptoEventoModels, PtoEquipamentoModel equipamentoModel) {
|
|
|
|
|
AtomicReference<Integer> nsr = new AtomicReference<>(0);
|
|
|
|
|
AtomicInteger nsrForEach = new AtomicInteger(lastNsr);
|
|
|
|
|
ptoEventoModels.stream().sorted(Comparator.comparing(PtoEventoModel::getNsr)).forEach(x -> {
|
|
|
|
|
if (x.getNsr() > lastNsr) { // tratando para nao duplicar os nsr
|
|
|
|
|
eventoService.saveOrUpdate(x);
|
|
|
|
|
nsr.set(x.getNsr());
|
|
|
|
|
if(x.getNsr() - nsrForEach.get() > 1){ //se a diferença entre o ultimo nsr e o atual for maior que um, chamará o Ajustes
|
|
|
|
|
PtoAjusteController ajusteController = new PtoAjusteController(equipamentoService, ajusteService);
|
|
|
|
|
ajusteController.extrair(equipamentoModel, nsrForEach.get());
|
|
|
|
|
}
|
|
|
|
|
nsrForEach.set(x.getNsr());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// ao final, atualiza a ptoEquipamento com o ultimoNSR salvo
|
|
|
|
|
return nsr.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PtoEquipamentoModel> obterRelogios(Boolean inativo) {
|
|
|
|
|
// pega todos os relogios ativos de PtoEquipamentoModel
|
|
|
|
|
return equipamentoService.findByInativo(inativo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PtoEventoModel> obterPontosPorRelogio(HashMap<String, String> filtros) throws URISyntaxException, JsonProcessingException {
|
|
|
|
|
public List<PtoEventoModel> obterPontosPorRelogio(HashMap<String, Object> filtros) throws JsonProcessingException {
|
|
|
|
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
|
@ -74,27 +110,27 @@ public class PtoEventoController {
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
headers.setBasicAuth(token);
|
|
|
|
|
|
|
|
|
|
String url = urlApis+TANGERINO_API_PUNCH;
|
|
|
|
|
String url = urlApis + TANGERINO_API_PUNCH;
|
|
|
|
|
final String[] filter = {""};
|
|
|
|
|
|
|
|
|
|
filtros.forEach((k,v) ->{
|
|
|
|
|
if(filter[0].isEmpty()){
|
|
|
|
|
filter[0] = filter[0].concat(k).concat("=").concat(v);
|
|
|
|
|
filtros.forEach((k, v) -> {
|
|
|
|
|
if (filter[0].isEmpty()) {
|
|
|
|
|
filter[0] = filter[0].concat(k).concat("=").concat(v.toString());
|
|
|
|
|
} else {
|
|
|
|
|
filter[0] = filter[0].concat("&").concat(k).concat("=").concat(v);
|
|
|
|
|
filter[0] = filter[0].concat("&").concat(k).concat("=").concat(v.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
if(!filter[0].isEmpty()){
|
|
|
|
|
if (!filter[0].isEmpty()) {
|
|
|
|
|
url = url.concat("?").concat(filter[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String jsonStr = restTemplate.exchange(url, HttpMethod.GET,new HttpEntity<>(headers), String.class).getBody();
|
|
|
|
|
String jsonStr = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), String.class).getBody();
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
|
|
|
|
|
ResultDTO forObject = objectMapper.readValue(jsonStr , ResultDTO.class);
|
|
|
|
|
ResultEventDTO forObject = objectMapper.readValue(jsonStr, ResultEventDTO.class);
|
|
|
|
|
List<PtoEventoModel> eventos = new ArrayList<>();
|
|
|
|
|
forObject.getContent().stream().forEach(x-> eventos.add(new PtoEventoModel(x)));
|
|
|
|
|
forObject.getContent().forEach(x -> eventos.add(new PtoEventoModel(x)));
|
|
|
|
|
return eventos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|