parent
4421bac11c
commit
5d0d41052d
@ -0,0 +1,91 @@
|
|||||||
|
package cn.jyjz.xiaoyao.ocr.util;
|
||||||
|
|
||||||
|
import cn.jyjz.xiaoyao.ocr.api.entity.PictureSourceParameter;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class RequestParameterFormat {
|
||||||
|
|
||||||
|
public static PictureSourceParameter fromString(String inputString) {
|
||||||
|
if (isJsonString(inputString)) {
|
||||||
|
return fromJsonString(inputString);
|
||||||
|
} else if (isCustomString(inputString)) {
|
||||||
|
try {
|
||||||
|
return fromCustomString(inputString);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new RuntimeException("Failed to parse custom string", e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Input string does not match known formats");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isJsonString(String str) {
|
||||||
|
return str.trim().startsWith("{") && str.trim().endsWith("}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isCustomString(String str) {
|
||||||
|
return str.startsWith("PictureSourceParameter(") && str.endsWith(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PictureSourceParameter fromJsonString(String jsonString) {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(jsonString, PictureSourceParameter.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Failed to parse JSON string", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PictureSourceParameter fromCustomString(String customString) throws ParseException {
|
||||||
|
Pattern pattern = Pattern.compile(
|
||||||
|
"PictureSourceParameter\\(startTime=(.*?), endTime=(.*?), pageSize=(.*?), pageNo=(.*?), projectNo=(.*?), planId=(.*?), accountNo=(.*?), tenantNo=(.*?)\\)");
|
||||||
|
Matcher matcher = pattern.matcher(customString);
|
||||||
|
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new IllegalArgumentException("String does not match the expected format");
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("CST"));
|
||||||
|
|
||||||
|
PictureSourceParameter param = new PictureSourceParameter();
|
||||||
|
param.setStartTime(parseDate(matcher.group(1).trim(), dateFormat));
|
||||||
|
param.setEndTime(parseDate(matcher.group(2).trim(), dateFormat));
|
||||||
|
param.setPageSize(Integer.parseInt(matcher.group(3).trim()));
|
||||||
|
param.setPageNo(Integer.parseInt(matcher.group(4).trim()));
|
||||||
|
param.setProjectNo(parseLong(matcher.group(5).trim()));
|
||||||
|
param.setPlanId(parseLong(matcher.group(6).trim()));
|
||||||
|
param.setAccountNo(parseLong(matcher.group(7).trim()));
|
||||||
|
param.setTenantNo(parseLong(matcher.group(8).trim()));
|
||||||
|
|
||||||
|
return param;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Date parseDate(String dateString, SimpleDateFormat dateFormat) throws ParseException {
|
||||||
|
try {
|
||||||
|
return dateFormat.parse(dateString);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
// Try parsing with alternative formats if necessary
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Long parseLong(String value) {
|
||||||
|
if ("null".equals(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Long.parseLong(value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue