mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
terminado margenes presupuesto e incluido en la api
This commit is contained in:
@ -6,12 +6,19 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imprimelibros.erp.configuracion.margenes_presupuestos.MargenPresupuesto;
|
||||
import com.imprimelibros.erp.configuracion.margenes_presupuestos.MargenPresupuestoDao;
|
||||
import com.imprimelibros.erp.presupuesto.Presupuesto.TipoCubierta;
|
||||
import com.imprimelibros.erp.presupuesto.Presupuesto.TipoEncuadernacion;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Service
|
||||
@ -22,13 +29,16 @@ public class skApiClient {
|
||||
|
||||
private final AuthService authService;
|
||||
private final RestTemplate restTemplate;
|
||||
private final MargenPresupuestoDao margenPresupuestoDao;
|
||||
|
||||
public skApiClient(AuthService authService) {
|
||||
public skApiClient(AuthService authService, MargenPresupuestoDao margenPresupuestoDao) {
|
||||
this.authService = authService;
|
||||
this.restTemplate = new RestTemplate();
|
||||
this.margenPresupuestoDao = margenPresupuestoDao;
|
||||
}
|
||||
|
||||
public String getPrice(Map<String, Object> requestBody) {
|
||||
public String getPrice(Map<String, Object> requestBody, TipoEncuadernacion tipoEncuadernacion,
|
||||
TipoCubierta tipoCubierta) {
|
||||
return performWithRetry(() -> {
|
||||
String url = this.skApiUrl + "api/calcular";
|
||||
|
||||
@ -45,14 +55,57 @@ public class skApiClient {
|
||||
String.class);
|
||||
|
||||
try {
|
||||
Map<String, Object> responseBody = new ObjectMapper().readValue(response.getBody(), Map.class);
|
||||
Map<String, Object> responseBody = new ObjectMapper().readValue(
|
||||
response.getBody(),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
if (responseBody.get("error") == null) {
|
||||
return new ObjectMapper().writeValueAsString(
|
||||
Map.of("data", responseBody.get("data")));
|
||||
Object dataObj = responseBody.get("data");
|
||||
|
||||
if (dataObj instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> data = (Map<String, Object>) dataObj;
|
||||
|
||||
List<Integer> tiradas = mapper.convertValue(
|
||||
data.get("tiradas"), new TypeReference<List<Integer>>() {
|
||||
});
|
||||
List<Double> precios = mapper.convertValue(
|
||||
data.get("precios"), new TypeReference<List<Double>>() {
|
||||
});
|
||||
|
||||
for (int i = 0; i < tiradas.size(); i++) {
|
||||
int tirada = tiradas.get(i);
|
||||
|
||||
MargenPresupuesto margen = margenPresupuestoDao.findByTipoAndTirada(
|
||||
tipoEncuadernacion, tipoCubierta, tirada);
|
||||
|
||||
if (margen != null) {
|
||||
double margenValue = calcularMargen(
|
||||
tirada,
|
||||
margen.getTiradaMin(),
|
||||
margen.getTiradaMax(),
|
||||
margen.getMargenMax(),
|
||||
margen.getMargenMin());
|
||||
double nuevoPrecio = precios.get(i) * (1 + margenValue / 100.0);
|
||||
precios.set(i, nuevoPrecio);
|
||||
} else {
|
||||
System.out.println("No se encontró margen para tirada " + tirada);
|
||||
}
|
||||
}
|
||||
|
||||
// <-- Clave: sustituir la lista en el map que se devuelve
|
||||
data.put("precios", precios);
|
||||
// (tiradas no cambia, pero si la modificases: data.put("tiradas", tiradas);)
|
||||
}
|
||||
|
||||
return mapper.writeValueAsString(Map.of("data", responseBody.get("data")));
|
||||
} else {
|
||||
return "{\"error\": 1}";
|
||||
}
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return "{\"error\": 1}";
|
||||
@ -104,7 +157,11 @@ public class skApiClient {
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
// Fallback al 80% del ancho
|
||||
Map<String, Object> tamanio = (Map<String, Object>) requestBody.get("tamanio");
|
||||
Map<String, Object> tamanio = new ObjectMapper().convertValue(
|
||||
requestBody.get("tamanio"),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (tamanio == null || tamanio.get("ancho") == null)
|
||||
throw new RuntimeException("Tamaño no válido en la solicitud: " + requestBody);
|
||||
else {
|
||||
@ -132,7 +189,10 @@ public class skApiClient {
|
||||
String.class);
|
||||
|
||||
try {
|
||||
Map<String, Object> responseBody = new ObjectMapper().readValue(response.getBody(), Map.class);
|
||||
Map<String, Object> responseBody = new ObjectMapper().readValue(
|
||||
response.getBody(),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
return responseBody.get("data").toString();
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
@ -162,4 +222,14 @@ public class skApiClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double calcularMargen(
|
||||
int tirada, int tiradaMin, int tiradaMax,
|
||||
double margenMax, double margenMin) {
|
||||
if (tirada <= tiradaMin)
|
||||
return margenMax;
|
||||
if (tirada >= tiradaMax)
|
||||
return margenMin;
|
||||
return margenMax - ((double) (tirada - tiradaMin) / (tiradaMax - tiradaMin)) * (margenMax - margenMin);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user