|
@@ -167,6 +167,161 @@ public class FlaskApiClient
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传销售数据文件到Flask服务
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file 上传的文件
|
|
|
|
|
+ * @return 响应结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject uploadSalesFile(MultipartFile file) throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/sales/upload";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ // 将MultipartFile转换为临时文件
|
|
|
|
|
+ File tempFile = convertMultipartFileToFile(file);
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ // 创建请求体
|
|
|
|
|
+ MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
+ FileSystemResource resource = new FileSystemResource(tempFile);
|
|
|
|
|
+ body.add("file", resource);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置请求头
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
+
|
|
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
|
|
|
|
+
|
|
|
|
|
+ // 发送请求
|
|
|
|
|
+ log.info("调用Flask API上传销售数据文件: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ // 解析响应
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API上传销售数据文件响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ finally
|
|
|
|
|
+ {
|
|
|
|
|
+ // 删除临时文件
|
|
|
|
|
+ if (tempFile.exists())
|
|
|
|
|
+ {
|
|
|
|
|
+ tempFile.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API上传销售数据文件失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("上传销售数据文件到Flask服务失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分析销售数据
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 分析结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject analyzeSalesFile() throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/sales/analyze";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("调用Flask API分析销售数据: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API分析销售数据响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API分析销售数据失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("分析销售数据失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取销售分析结果
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 分析结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject getSalesResults() throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/sales/results";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("调用Flask API获取销售分析结果: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API获取销售分析结果响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API获取销售分析结果失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("获取销售分析结果失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 预测销售趋势
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param sku SKU编码
|
|
|
|
|
+ * @param period 预测周期
|
|
|
|
|
+ * @return 预测结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject predictSalesTrend(String sku, int period) throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/sales/predict";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ // 创建请求体
|
|
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
|
|
+ requestBody.put("sku", sku);
|
|
|
|
|
+ requestBody.put("period", period);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置请求头
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+
|
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
|
|
|
|
|
+
|
|
|
|
|
+ // 发送请求
|
|
|
|
|
+ log.info("调用Flask API预测销售趋势: {}, sku={}, period={}", url, sku, period);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ // 解析响应
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API预测销售趋势响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API预测销售趋势失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("预测销售趋势失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 将MultipartFile转换为File
|
|
* 将MultipartFile转换为File
|
|
|
*/
|
|
*/
|