import kaz
This commit is contained in:
@ -132,13 +132,10 @@ public class AcceptJSON implements ServletContextAware {
|
||||
@RequestParam(required=false,name="file") MultipartFile file,
|
||||
@RequestParam(required=false,name="skip",defaultValue = "0") Boolean skip
|
||||
) {
|
||||
int cnt=0;
|
||||
JSONObject json = new JSONObject();
|
||||
int count=0;
|
||||
|
||||
try{
|
||||
//TODO add user verification: that he is authorized and that he is an administrator!
|
||||
//if(user.id==null || user.id.equals("null") || user.id.isBlank())
|
||||
// return "Please log in!";
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
@ -147,16 +144,43 @@ public class AcceptJSON implements ServletContextAware {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
LocalDate lastDate = null;
|
||||
String sql= """
|
||||
select to_char(max(date), 'YYYY-MM-DD') as date from integration.days_kz
|
||||
""";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
List<String> ret = jdbcTemplate.query(sql, parameters, new DBTools.JsonRowMapper());
|
||||
List<String> data = new ArrayList<>();
|
||||
for (String s : ret) {
|
||||
JSONObject obj = new JSONObject(s);
|
||||
if(!obj.isNull("date")) {
|
||||
lastDate = LocalDate.parse(obj.getString("date"));
|
||||
}
|
||||
}
|
||||
if(lastDate==null)
|
||||
lastDate = LocalDate.parse("2025-07-01");
|
||||
//lastDate = LocalDate.parse("2025-07-16");
|
||||
|
||||
//By number of day downloading data and write result of downloading to database, try or false, after repair need repeat download
|
||||
while(lastDate.isBefore(LocalDate.now().minusDays(3))) {
|
||||
lastDate = lastDate.plusDays(1);
|
||||
|
||||
String dateBegin = lastDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
String dateEnd = dateBegin;
|
||||
|
||||
System.out.println("date = " + dateEnd);
|
||||
|
||||
StringBuilder sb = new StringBuilder(1024);
|
||||
try
|
||||
{
|
||||
String strURL="http://91.185.13.233:98/get_fito";
|
||||
try {
|
||||
//String strURL = "http://91.185.13.233:98/get_fito?start_date="+dateBegin+"&end_date="+dateEnd;
|
||||
String strURL = "http://95.56.225.145:98/get_fito?start_date="+dateBegin+"&end_date="+dateEnd;
|
||||
|
||||
URL url = new URL(strURL);
|
||||
HttpURLConnection hConn = (HttpURLConnection) url.openConnection();
|
||||
hConn.setRequestMethod("GET");
|
||||
hConn.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsInVzZXJuYW1lIjoiQ0NBTE0iLCJyb2xlIjoidXNlciIsImlhdCI6MTcwNjc4NjQ0Nn0.bypDbAZYjbQu8hWrb6MnnAZ80AbO4ZifP3inEnpbabI");
|
||||
|
||||
hConn.setConnectTimeout(60000); // 60 сек
|
||||
hConn.setConnectTimeout(60000); // 60 sec
|
||||
|
||||
hConn.connect();
|
||||
int responseCode = hConn.getResponseCode();
|
||||
@ -170,15 +194,13 @@ public class AcceptJSON implements ServletContextAware {
|
||||
}
|
||||
inputStream.close();
|
||||
}
|
||||
}catch (IOException e)
|
||||
{
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String content = sb.toString();
|
||||
|
||||
|
||||
int year=2025;
|
||||
//int year = 2025;
|
||||
|
||||
|
||||
if (content == null || content.isEmpty()) {
|
||||
@ -186,14 +208,14 @@ public class AcceptJSON implements ServletContextAware {
|
||||
} else {
|
||||
System.out.println("Start");
|
||||
|
||||
String sql="delete from main.frmlocust where country_id=5 and eid=0 and extract(year from date)="+String.valueOf(year);
|
||||
try{
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
int cont = stmt.executeUpdate();
|
||||
System.out.println("cnt: " + String.valueOf(cont));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//String sql = "delete from main.frmlocust where country_id=5 and eid=0 and extract(year from date)=" + String.valueOf(year);
|
||||
//try {
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
// int cont = stmt.executeUpdate();
|
||||
// System.out.println("cnt: " + String.valueOf(cont));
|
||||
//} catch (SQLException e) {
|
||||
// e.printStackTrace();
|
||||
//}
|
||||
|
||||
//System.out.println(content);
|
||||
//Iterate over the elements of the array.
|
||||
@ -202,6 +224,16 @@ public class AcceptJSON implements ServletContextAware {
|
||||
for (Object o : jsonArray) {
|
||||
JSONObject jsonObj = (JSONObject) o;
|
||||
|
||||
FrmLocustModel locust = convertKAZJSONToLocustModel(jsonObj);
|
||||
|
||||
//I check if there is already such a record in the database, if not, I insert it, if there is, I update it
|
||||
UpdateLocust updateLocust = new UpdateLocust(jdbcTemplate);
|
||||
//if(updateLocust.update(locust))
|
||||
// count++;
|
||||
|
||||
System.out.println( "count = " + count );
|
||||
|
||||
/*
|
||||
//I don't miss other dates
|
||||
ZonedDateTime dateTime = ZonedDateTime.parse(jsonObj.getString("examination_date"), DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
if (dateTime.getYear() != year)
|
||||
@ -389,7 +421,8 @@ public class AcceptJSON implements ServletContextAware {
|
||||
boolean swarms = false; // Стаи
|
||||
if (jsonObj.has("development_stage") && !jsonObj.isNull("development_stage")) {
|
||||
if (jsonObj.getString("development_stage").equals("Яйца")) eggs = true;
|
||||
if (jsonObj.getString("development_stage").equals("Личинки") || jsonObj.getString("development_stage").equals("Заселенность личинками") || jsonObj.getString("development_stage").equals("Личинка")) hoppers = true;
|
||||
if (jsonObj.getString("development_stage").equals("Личинки") || jsonObj.getString("development_stage").equals("Заселенность личинками") || jsonObj.getString("development_stage").equals("Личинка"))
|
||||
hoppers = true;
|
||||
if (jsonObj.getString("development_stage").equals("Кулиги")) bands = true;
|
||||
if (jsonObj.getString("development_stage").equals("Имаго")) adults = true;
|
||||
if (jsonObj.getString("development_stage").equals("Стаи")) swarms = true;
|
||||
@ -425,21 +458,283 @@ public class AcceptJSON implements ServletContextAware {
|
||||
model.addAttribute("PreviewTable", "Error set data");
|
||||
return "json";
|
||||
}
|
||||
cnt++;
|
||||
cnt++;*/
|
||||
}
|
||||
System.out.println("Proc count = "+String.valueOf(cnt));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (CustomException e) {
|
||||
if(e.isSaveToLog()) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
logger.error(MarkerFactory.getMarker(uuid), e.getMessage(), e);
|
||||
}
|
||||
json = e.getJson();
|
||||
} catch (Exception ex) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
logger.error(MarkerFactory.getMarker(uuid), ex.getMessage(), ex);
|
||||
json = Tools.createJSONError(10000,"Internal_Server_Error", (String)null, uuid);
|
||||
}
|
||||
return "Proc count = "+String.valueOf(cnt);
|
||||
return json.toString();
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
public FrmLocustModel convertKAZJSONToLocustModel(JSONObject obj) throws CustomException {
|
||||
|
||||
FrmLocustModel result = new FrmLocustModel();
|
||||
|
||||
result.company_uid = "f52a99a6-db5a-4d15-b29c-5a7989d548eb"; //Company: "Kazakhstan Locust"
|
||||
//result.uid = obj.getString("uuid");
|
||||
//result.eid = String.valueOf(obj.getInt("id")); //Internal identifier in the Uzbek database
|
||||
|
||||
//result.seq = obj.getLong("seq")*1000;
|
||||
result.country_id = "5"; //Kazakhstan
|
||||
result.country_uid = "3c7df90e-9632-4e71-8f36-c184751216df"; //Kazakhstan
|
||||
|
||||
/*
|
||||
sql = """
|
||||
insert into main.frmlocust(
|
||||
eid, --1
|
||||
lat_center, --2
|
||||
lon_center, --3
|
||||
lat1, --4
|
||||
lon1, --5
|
||||
date, --6
|
||||
village, --7
|
||||
terrain, --8
|
||||
district, --9
|
||||
country_id, --10
|
||||
region_id, --11
|
||||
locust_type_id, --12
|
||||
bio_hectare, --13
|
||||
locust_populated, --14
|
||||
description, --15
|
||||
eggs, --16
|
||||
hoppers, --17
|
||||
bands, --18
|
||||
adults, --19
|
||||
swarms --20
|
||||
)values(
|
||||
?, --1 eid
|
||||
?, --2 lat_center
|
||||
?, --3 lon_center
|
||||
?, --4 lat1
|
||||
?, --5 lon1
|
||||
TO_TIMESTAMP(?, 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"'), --6 date
|
||||
LEFT(?, 100), --7 village
|
||||
LEFT(?, 100), --8 terrain
|
||||
LEFT(?, 100), --9 district
|
||||
?, --10 country_id
|
||||
?, --11 region_id
|
||||
?, --12 locust_type_id
|
||||
?, --13 bio_hectare
|
||||
?, --14 locust_populated
|
||||
?, --15 description
|
||||
?, --16 eggs CheckBox
|
||||
?, --17 hoppers CheckBox
|
||||
?, --18 bands CheckBox
|
||||
?, --19 adults CheckBox
|
||||
? --20 swarms CheckBox
|
||||
)""";
|
||||
*/
|
||||
|
||||
|
||||
|
||||
result.eid = "0";
|
||||
result.lat_center = String.valueOf(obj.getDouble("latitude"));
|
||||
result.lon_center = String.valueOf(obj.getDouble("longitude"));
|
||||
result.date = obj.getString("examination_date"); //2023-03-20T06:23:20.000Z
|
||||
if (obj.has("village") && !obj.isNull("village"))
|
||||
result.village = obj.getString("village");
|
||||
if (obj.has("farm") && !obj.isNull("farm"))
|
||||
result.terrain = obj.getString("farm");
|
||||
if (obj.has("district") && !obj.isNull("district"))
|
||||
result.district = obj.getString("district");
|
||||
|
||||
if(!obj.isNull("region") && !obj.optString("region","").isBlank()) {
|
||||
switch (obj.optString("region",null).trim()) {
|
||||
case "Туркестанская":
|
||||
result.region_id = "5";
|
||||
//result.region_uid = "8f4f586f-6159-4fbf-a627-2c8d637abb5e";
|
||||
break;
|
||||
case "Жамбылская":
|
||||
result.region_id = "4";
|
||||
//result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9";
|
||||
break;
|
||||
case "Алматинская":
|
||||
result.region_id = "3";
|
||||
//result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9";
|
||||
break;
|
||||
case "Карагандинская":
|
||||
result.region_id = "2";
|
||||
//result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9";
|
||||
break;
|
||||
case "Жетысу":
|
||||
result.region_id = "478";
|
||||
//result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9";
|
||||
break;
|
||||
case "Кызылординская":
|
||||
result.region_id = "478";
|
||||
//result.region_uid = "2a6adb56-19a7-4f28-9a00-87559db91db9";
|
||||
break;
|
||||
default:
|
||||
System.out.println(obj.optString("region",null));
|
||||
throw new CustomException(200, 10000, "Not find region by region: " + obj.optString("region",null), null, true);
|
||||
}
|
||||
}
|
||||
|
||||
if(!obj.isNull("region") && !obj.optString("region","").isBlank()) {
|
||||
switch (obj.optString("region",null).trim()) {
|
||||
case "Туркестанская":
|
||||
result.region_id = "5";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Жамбылская":
|
||||
result.region_id = "4";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Алматинская":
|
||||
result.region_id = "3";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Карагандинская":
|
||||
result.region_id = "2";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Жетысу":
|
||||
result.region_id = "478";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Кызылординская":
|
||||
result.region_id = "6";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Атырауская":
|
||||
result.region_id = "13";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Западно-Казахстанская":
|
||||
result.region_id = "12";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Акмолинская":
|
||||
result.region_id = "10";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Восточно-Казахстанская":
|
||||
result.region_id = "1";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Абай":
|
||||
result.region_id = "477";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Костанайская":
|
||||
result.region_id = "8";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Павлодарская":
|
||||
result.region_id = "11";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Северо-Казахстанская":
|
||||
result.region_id = "9";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Актюбинская":
|
||||
result.region_id = "7";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Мангистауская":
|
||||
result.region_id = "14";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
case "Улытау":
|
||||
result.region_id = "479";
|
||||
//result.region_uid = "";
|
||||
break;
|
||||
default:
|
||||
System.out.println(obj.optString("region",null));
|
||||
throw new CustomException(200, 10000, "Not find region by region: " + obj.optString("region",null), null, true);
|
||||
}
|
||||
}
|
||||
|
||||
//Вид саранчи
|
||||
if(!obj.isNull("region") && !obj.optString("region","").isBlank()) {
|
||||
switch (obj.getString("grasshopper_type")) {
|
||||
case "Мароккская саранча":
|
||||
result.locust_type_id = "2";
|
||||
//result.locust_type_uid = "";
|
||||
break;
|
||||
case "Итальянский прус":
|
||||
result.locust_type_id = "1";
|
||||
//result.locust_type_uid = "";
|
||||
break;
|
||||
case "Азиатская саранча":
|
||||
result.locust_type_id = "3";
|
||||
//result.locust_type_uid = "";
|
||||
break;
|
||||
default:
|
||||
System.out.println(obj.optString("grasshopper_type",null));
|
||||
throw new CustomException(200, 10000, "Not find grasshopper_type: " + obj.optString("grasshopper_type",null), null, true);
|
||||
}
|
||||
}
|
||||
|
||||
//Обследованно
|
||||
if (obj.has("examination_area") && !obj.isNull("examination_area"))
|
||||
stmt.setDouble(13, Double.parseDouble(obj.get("examination_area").toString()));
|
||||
else
|
||||
stmt.setNull(13, Types.DOUBLE);
|
||||
|
||||
//Площадь заселения
|
||||
if (obj.has("settlement_area") && !obj.isNull("settlement_area"))
|
||||
stmt.setDouble(14, Double.parseDouble(obj.get("settlement_area").toString()));
|
||||
else
|
||||
stmt.setNull(14, Types.DOUBLE);
|
||||
|
||||
boolean eggs = false; // Яйца
|
||||
boolean hoppers = false; // Личинки
|
||||
boolean bands = false; // Кулиги
|
||||
boolean adults = false; // Имаго
|
||||
boolean swarms = false; // Стаи
|
||||
if (obj.has("development_stage") && !obj.isNull("development_stage")) {
|
||||
if (obj.getString("development_stage").equals("Яйца")) eggs = true;
|
||||
if (obj.getString("development_stage").equals("Личинки") || obj.getString("development_stage").equals("Заселенность личинками") || obj.getString("development_stage").equals("Личинка"))
|
||||
hoppers = true;
|
||||
if (obj.getString("development_stage").equals("Кулиги")) bands = true;
|
||||
if (obj.getString("development_stage").equals("Имаго")) adults = true;
|
||||
if (obj.getString("development_stage").equals("Стаи")) swarms = true;
|
||||
}
|
||||
if (eggs)
|
||||
result.eggs = "true";
|
||||
//stmt.setBoolean(16, true);
|
||||
|
||||
if (hoppers)
|
||||
result.hoppers = "true";
|
||||
//stmt.setBoolean(17, true);
|
||||
|
||||
if (bands)
|
||||
result.bands = "true";
|
||||
//stmt.setBoolean(18, true);
|
||||
|
||||
if (adults)
|
||||
result.adults = "true";
|
||||
//stmt.setBoolean(19, true);
|
||||
|
||||
if (swarms)
|
||||
result.swarms = "true";
|
||||
//stmt.setBoolean(20, true);
|
||||
|
||||
|
||||
|
||||
result.description = "";
|
||||
result.description += "city: " + obj.getString("city") + "\n";
|
||||
result.description += "message_type: " + obj.getString("message_type") + "\n";
|
||||
if (obj.has("development_stage") && !obj.isNull("development_stage"))
|
||||
result.description += "stage: " + obj.getString("development_stage") + "\n";
|
||||
|
||||
result.update();
|
||||
return result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Пример запроса: http://127.0.0.1:8081/AcceptJSON но в теле нужно поменять строку "Year y = Year.of(2024);" а также номер дня
|
||||
@ -1516,8 +1811,7 @@ public class AcceptJSON implements ServletContextAware {
|
||||
UpdateLocust updateLocust = new UpdateLocust(jdbcTemplate);
|
||||
if(updateLocust.update(locust))
|
||||
count++;
|
||||
if(count==66)
|
||||
System.out.println( "count = " + count );
|
||||
|
||||
System.out.println( "count = " + count );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user