mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-03-01 06:09:13 +00:00
trabajando en leer datos de tipo cubierta
This commit is contained in:
@ -0,0 +1,266 @@
|
||||
package com.imprimelibros.erp.presupuesto.service;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imprimelibros.erp.presupuesto.dto.Presupuesto;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class PresupuestoFormDataMapper {
|
||||
|
||||
public class PresupuestoFormDataDto {
|
||||
|
||||
public DatosGenerales datosGenerales = new DatosGenerales();
|
||||
public Interior interior = new Interior();
|
||||
public Cubierta cubierta = new Cubierta();
|
||||
public Servicios servicios = new Servicios();
|
||||
public Integer selectedTirada;
|
||||
|
||||
// ===== Datos Generales =====
|
||||
public static class DatosGenerales {
|
||||
public String titulo = "";
|
||||
public String autor = "";
|
||||
public String isbn = "";
|
||||
public String tirada1 = "";
|
||||
public String tirada2 = "";
|
||||
public String tirada3 = "";
|
||||
public String tirada4 = "";
|
||||
public Integer ancho;
|
||||
public Integer alto;
|
||||
public boolean formatoPersonalizado = false;
|
||||
public String paginasNegro = "";
|
||||
public String paginasColor = "";
|
||||
public String posicionPaginasColor = "";
|
||||
public String tipoEncuadernacion = "fresado"; // enum name
|
||||
}
|
||||
|
||||
// ===== Interior =====
|
||||
public static class Interior {
|
||||
public String tipoImpresion = "negro"; // enum name
|
||||
public Integer papelInteriorId = 3;
|
||||
public Integer gramajeInterior = 80;
|
||||
}
|
||||
|
||||
// ===== Cubierta =====
|
||||
public static class Cubierta {
|
||||
public String tipoCubierta = "tapaBlanda"; // enum name
|
||||
public int solapasCubierta = 0; // 0/1 para tu UI
|
||||
public String tamanioSolapasCubierta = "80";
|
||||
public Integer cubiertaCaras = 2;
|
||||
|
||||
public Integer papelGuardasId = 3;
|
||||
public Integer gramajeGuardas = 170;
|
||||
public Integer guardasImpresas = 0;
|
||||
public String cabezada = "WHI";
|
||||
|
||||
public Integer papelCubiertaId = 3;
|
||||
public Integer gramajeCubierta = 170;
|
||||
public Integer acabado = 1;
|
||||
|
||||
public SobreCubierta sobrecubierta = new SobreCubierta();
|
||||
public Faja faja = new Faja();
|
||||
|
||||
public static class SobreCubierta {
|
||||
public boolean activo = false;
|
||||
public Integer papelSobrecubiertaId = 2;
|
||||
public Integer gramajeSobrecubierta = 170;
|
||||
public Integer tamanioSolapasSobrecubierta = 80;
|
||||
public Integer acabado = 0;
|
||||
}
|
||||
|
||||
public static class Faja {
|
||||
public boolean activo = false;
|
||||
public Integer papelFajaId = 2;
|
||||
public Integer gramajeFaja = 170;
|
||||
public Integer alto = 50;
|
||||
public Integer tamanioSolapasFaja = 80;
|
||||
public Integer acabado = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Servicios / Extras =====
|
||||
public static class Servicios {
|
||||
public List<String> servicios = List.of();
|
||||
public DatosMarcapaginas datosMarcapaginas = new DatosMarcapaginas();
|
||||
public DatosMaquetacion datosMaquetacion = new DatosMaquetacion();
|
||||
|
||||
public static class DatosMarcapaginas {
|
||||
public Integer marcapaginas_tirada = 100;
|
||||
public String tamanio_marcapaginas = "_50x140_";
|
||||
public String caras_impresion = "una_cara";
|
||||
public String papel_marcapaginas = "cartulina_grafica";
|
||||
public Integer gramaje_marcapaginas = 300;
|
||||
public String acabado_marcapaginas = "ninguno";
|
||||
public Resultado resultado = new Resultado();
|
||||
}
|
||||
|
||||
public static class DatosMaquetacion {
|
||||
public Integer num_caracteres = 200000;
|
||||
public String formato_maquetacion = "A5";
|
||||
public String cuerpo_texto = "medium";
|
||||
public Integer num_columnas = 1;
|
||||
public Integer num_tablas = 0;
|
||||
public Integer num_fotos = 0;
|
||||
public boolean correccion_ortotipografica = false;
|
||||
public boolean texto_mecanografiado = false;
|
||||
public boolean disenio_portada = false;
|
||||
public boolean epub = false;
|
||||
public Resultado resultado = new Resultado();
|
||||
}
|
||||
|
||||
public static class Resultado {
|
||||
public Integer num_paginas_estimadas = 0; // solo para maquetación
|
||||
public Integer precio_pagina_estimado = 0;
|
||||
public Number precio_unitario = 0; // solo para marcapáginas
|
||||
public Number precio = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final ObjectMapper om = new ObjectMapper();
|
||||
|
||||
public PresupuestoFormDataDto toFormData(Presupuesto p) {
|
||||
|
||||
PresupuestoFormDataDto vm = new PresupuestoFormDataDto();
|
||||
|
||||
// ===== Datos Generales
|
||||
vm.datosGenerales.titulo = nz(p.getTitulo(), "");
|
||||
vm.datosGenerales.autor = nz(p.getAutor(), "");
|
||||
vm.datosGenerales.isbn = nz(p.getIsbn(), "");
|
||||
|
||||
vm.datosGenerales.tirada1 = nzStr(p.getTirada1());
|
||||
vm.datosGenerales.tirada2 = nzStr(p.getTirada2());
|
||||
vm.datosGenerales.tirada3 = nzStr(p.getTirada3());
|
||||
vm.datosGenerales.tirada4 = nzStr(p.getTirada4());
|
||||
|
||||
vm.datosGenerales.ancho = p.getAncho();
|
||||
vm.datosGenerales.alto = p.getAlto();
|
||||
vm.datosGenerales.formatoPersonalizado = Boolean.TRUE.equals(p.getFormatoPersonalizado());
|
||||
|
||||
vm.datosGenerales.paginasNegro = nzStr(p.getPaginasNegro());
|
||||
vm.datosGenerales.paginasColor = nzStr(p.getPaginasColor());
|
||||
vm.datosGenerales.posicionPaginasColor = nz(p.getPosicionPaginasColor(), "");
|
||||
|
||||
vm.datosGenerales.tipoEncuadernacion = enumName(p.getTipoEncuadernacion(), "fresado");
|
||||
|
||||
// ===== Interior
|
||||
vm.interior.tipoImpresion = enumName(p.getTipoImpresion(), "negro");
|
||||
vm.interior.papelInteriorId = nz(p.getPapelInteriorId(), 3);
|
||||
vm.interior.gramajeInterior = nz(p.getGramajeInterior(), 80);
|
||||
|
||||
// ===== Cubierta
|
||||
vm.cubierta.tipoCubierta = enumName(p.getTipoCubierta(), "tapaBlanda");
|
||||
vm.cubierta.solapasCubierta = Boolean.TRUE.equals(p.getSolapasCubierta()) ? 1 : 0;
|
||||
vm.cubierta.tamanioSolapasCubierta = String.valueOf(nz(p.getTamanioSolapasCubierta(), 80));
|
||||
vm.cubierta.cubiertaCaras = nz(p.getCubiertaCaras(), 2);
|
||||
|
||||
vm.cubierta.papelGuardasId = nz(p.getPapelGuardasId(), 3);
|
||||
vm.cubierta.gramajeGuardas = nz(p.getGramajeGuardas(), 170);
|
||||
vm.cubierta.guardasImpresas = nz(p.getGuardasImpresas(), 0);
|
||||
vm.cubierta.cabezada = nz(p.getCabezada(), "WHI");
|
||||
|
||||
vm.cubierta.papelCubiertaId = nz(p.getPapelCubiertaId(), 3);
|
||||
vm.cubierta.gramajeCubierta = nz(p.getGramajeCubierta(), 170);
|
||||
vm.cubierta.acabado = nz(p.getAcabado(), 1);
|
||||
|
||||
vm.cubierta.sobrecubierta.activo = Boolean.TRUE.equals(p.getSobrecubierta());
|
||||
vm.cubierta.sobrecubierta.papelSobrecubiertaId = nz(p.getPapelSobrecubiertaId(), 2);
|
||||
vm.cubierta.sobrecubierta.gramajeSobrecubierta = nz(p.getGramajeSobrecubierta(), 170);
|
||||
vm.cubierta.sobrecubierta.tamanioSolapasSobrecubierta = nz(p.getTamanioSolapasSobrecubierta(), 80);
|
||||
vm.cubierta.sobrecubierta.acabado = nz(p.getAcabadoSobrecubierta(), 0);
|
||||
|
||||
vm.cubierta.faja.activo = Boolean.TRUE.equals(p.getFaja());
|
||||
vm.cubierta.faja.papelFajaId = nz(p.getPapelFajaId(), 2);
|
||||
vm.cubierta.faja.gramajeFaja = nz(p.getGramajeFaja(), 170);
|
||||
vm.cubierta.faja.tamanioSolapasFaja = nz(p.getTamanioSolapasFaja(), 80);
|
||||
vm.cubierta.faja.acabado = nz(p.getAcabadoFaja(), 0);
|
||||
vm.cubierta.faja.alto = nz(p.getAltoFaja(), 50);
|
||||
|
||||
// ===== Selected tirada
|
||||
vm.selectedTirada = p.getSelectedTirada();
|
||||
|
||||
// ===== Servicios desde JSONs
|
||||
// servicios_json: acepta ["maquetacion","marcapaginas"] o [{id:...}, ...]
|
||||
vm.servicios.servicios = parseServiciosIds(p.getServiciosJson());
|
||||
|
||||
// datos_maquetacion_json
|
||||
PresupuestoFormDataDto.Servicios.DatosMaquetacion maq = parse(p.getDatosMaquetacionJson(),
|
||||
PresupuestoFormDataDto.Servicios.DatosMaquetacion.class);
|
||||
if (maq != null)
|
||||
vm.servicios.datosMaquetacion = merge(vm.servicios.datosMaquetacion, maq);
|
||||
|
||||
// datos_marcapaginas_json
|
||||
PresupuestoFormDataDto.Servicios.DatosMarcapaginas mp = parse(p.getDatosMarcapaginasJson(),
|
||||
PresupuestoFormDataDto.Servicios.DatosMarcapaginas.class);
|
||||
if (mp != null)
|
||||
vm.servicios.datosMarcapaginas = merge(vm.servicios.datosMarcapaginas, mp);
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
// ===== Helpers =====
|
||||
|
||||
private static String enumName(Enum<?> e, String def) {
|
||||
return e != null ? e.name() : def;
|
||||
}
|
||||
|
||||
private static String nz(String v, String def) {
|
||||
return v != null ? v : def;
|
||||
}
|
||||
|
||||
private static Integer nz(Integer v, Integer def) {
|
||||
return v != null ? v : def;
|
||||
}
|
||||
|
||||
private static String nzStr(Integer v) {
|
||||
return v == null ? "" : String.valueOf(v);
|
||||
}
|
||||
|
||||
private <T> T parse(String json, Class<T> type) {
|
||||
try {
|
||||
if (json == null || json.isBlank())
|
||||
return null;
|
||||
return om.readValue(json, type);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> parseServiciosIds(String json) {
|
||||
if (json == null || json.isBlank())
|
||||
return new ArrayList<>();
|
||||
try {
|
||||
// 1) intentar como lista de strings
|
||||
List<String> ids = om.readValue(json, new TypeReference<List<String>>() {
|
||||
});
|
||||
return ids != null ? ids : new ArrayList<>();
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
try {
|
||||
// 2) intentar como lista de objetos con 'id'
|
||||
List<Map<String, Object>> list = om.readValue(json, new TypeReference<>() {
|
||||
});
|
||||
List<String> ids = new ArrayList<>();
|
||||
for (Map<String, Object> it : list) {
|
||||
Object id = it.get("id");
|
||||
if (id != null)
|
||||
ids.add(String.valueOf(id));
|
||||
}
|
||||
return ids;
|
||||
} catch (
|
||||
|
||||
Exception e) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T merge(T base, T override) {
|
||||
// merge muy simple: si override != null, devuélvelo; si no, base
|
||||
// (si quisieras merge campo a campo, usa BeanUtils o MapStruct)
|
||||
return override != null ? override : base;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user