feat: 新增预测模型接口
This commit is contained in:
@@ -9,5 +9,5 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class PathProperty {
|
public class PathProperty {
|
||||||
private String historyFile;
|
private String historyFile;
|
||||||
|
private String predictFileDir;
|
||||||
}
|
}
|
||||||
|
@@ -166,5 +166,8 @@ public class MaterialController {
|
|||||||
return FuHttpResponse.Builder().dataResponse(result).build();
|
return FuHttpResponse.Builder().dataResponse(result).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo-1 预测模型接口
|
||||||
|
// 遍历目录下的所有json文件 解析入库
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,88 @@
|
|||||||
|
package mjkf.xinke.main.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.file.FileReader;
|
||||||
|
import cn.hutool.json.JSONException;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.jgy.xxs.core.http.resp.HttpResponse;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.Data;
|
||||||
|
import mjkf.xinke.main.common.http.FuHttpResponse;
|
||||||
|
import mjkf.xinke.main.common.property.PathProperty;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/predict")
|
||||||
|
public class PredictController {
|
||||||
|
@Resource
|
||||||
|
PathProperty pathProperty;
|
||||||
|
|
||||||
|
@ApiOperation("获取预测数据")
|
||||||
|
@GetMapping("/")
|
||||||
|
public HttpResponse get(
|
||||||
|
) throws Exception {
|
||||||
|
// 指定目录路径
|
||||||
|
String directoryPath = pathProperty.getPredictFileDir();
|
||||||
|
List<File> files = FileUtil.loopFiles(directoryPath, file -> file.getName().endsWith(".json"));
|
||||||
|
List result = new ArrayList<DataPredict>();
|
||||||
|
for (File file : files) {
|
||||||
|
FileReader fileReader = new FileReader(file);
|
||||||
|
String jsonContent = fileReader.readString();
|
||||||
|
JSONObject jsonObject;
|
||||||
|
try {
|
||||||
|
jsonObject = JSONUtil.parseObj(jsonContent);
|
||||||
|
result = parseJson(jsonObject);
|
||||||
|
System.out.println("File: " + file.getName());
|
||||||
|
System.out.println("JSONObject: " + jsonObject);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
System.out.println("JSONException: " + file.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return FuHttpResponse.Builder().dataResponse(result).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo-3 遍历目录下的所有json文件 解析入库
|
||||||
|
|
||||||
|
private List<DataPredict> parseJson(JSONObject jsonObject) {
|
||||||
|
var month = jsonObject.getStr("预测月份");
|
||||||
|
var data = jsonObject.getJSONArray("数据");
|
||||||
|
var result = new ArrayList<DataPredict>();
|
||||||
|
for (var item : data) {
|
||||||
|
var dataPredict = new DataPredict((JSONObject) item, month);
|
||||||
|
result.add(dataPredict);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
class DataPredict {
|
||||||
|
private Integer year;
|
||||||
|
private Integer month;
|
||||||
|
private Double price;
|
||||||
|
private String materialId;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public DataPredict() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPredict(JSONObject item, String month) {
|
||||||
|
this.materialId = item.getStr("材料编号");
|
||||||
|
this.name = item.getStr("材料名称");
|
||||||
|
this.price = item.getDouble("预测价格");
|
||||||
|
// 解析字符串日期为 Date 对象
|
||||||
|
var date = DateUtil.parse(month, "yyyyMM");
|
||||||
|
// 获取年份和月份
|
||||||
|
this.year = DateUtil.year(date);
|
||||||
|
this.month = DateUtil.month(date) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -22,4 +22,7 @@ main:
|
|||||||
|
|
||||||
api:
|
api:
|
||||||
host:
|
host:
|
||||||
data-tool: http://localhost:7778
|
data-tool: http://localhost:7778
|
||||||
|
|
||||||
|
path:
|
||||||
|
predict-file-dir: C:\Users\Administrator\Documents\WeChat Files\wslshanlin\FileStorage\File\2024-07
|
@@ -19,4 +19,7 @@ main:
|
|||||||
|
|
||||||
api:
|
api:
|
||||||
host:
|
host:
|
||||||
data-tool: http://192.168.1.3:50000
|
data-tool: http://192.168.1.3:50000
|
||||||
|
|
||||||
|
path:
|
||||||
|
predict-file-dir: /predict
|
65
src/test/java/mjkf/xinke/GetPredictDataTest.java
Normal file
65
src/test/java/mjkf/xinke/GetPredictDataTest.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package mjkf.xinke;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.file.FileReader;
|
||||||
|
import cn.hutool.json.JSONException;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GetPredictDataTest {
|
||||||
|
|
||||||
|
@org.junit.Test
|
||||||
|
public void test() throws Exception {
|
||||||
|
// 指定目录路径
|
||||||
|
String directoryPath = "C:\\Users\\Administrator\\Documents\\WeChat Files\\wslshanlin\\FileStorage\\File\\2024-07";
|
||||||
|
// 获取目录下的所有文件
|
||||||
|
List<File> files = FileUtil.loopFiles(directoryPath, file -> file.getName().endsWith(".json"));
|
||||||
|
for (File file : files) {
|
||||||
|
// 读取文件内容
|
||||||
|
FileReader fileReader = new FileReader(file);
|
||||||
|
String jsonContent = fileReader.readString();
|
||||||
|
// 解析 JSON 内容为 JSONObject
|
||||||
|
JSONObject jsonObject;
|
||||||
|
try {
|
||||||
|
jsonObject = JSONUtil.parseObj(jsonContent);
|
||||||
|
parseJson(jsonObject);
|
||||||
|
// 打印解析后的 JSONObject
|
||||||
|
System.out.println("File: " + file.getName());
|
||||||
|
System.out.println("JSONObject: " + jsonObject);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
System.out.println("JSONException: " + file.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<DataPredict> parseJson(JSONObject jsonObject) {
|
||||||
|
var month = jsonObject.getStr("预测月份");
|
||||||
|
var data = jsonObject.getJSONArray("数据");
|
||||||
|
var result = new ArrayList<DataPredict>();
|
||||||
|
for (var item: data) {
|
||||||
|
var dataPredict = new DataPredict((JSONObject)item);
|
||||||
|
result.add(dataPredict);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
class DataPredict {
|
||||||
|
private Integer year;
|
||||||
|
private Integer month;
|
||||||
|
private Double price;
|
||||||
|
private String materialId;
|
||||||
|
private String name;
|
||||||
|
public DataPredict () {}
|
||||||
|
public DataPredict (JSONObject item) {
|
||||||
|
this.materialId = item.getStr("材料编号");
|
||||||
|
this.name = item.getStr("材料名称");
|
||||||
|
this.price = item.getDouble("预测价格");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user