zhouhaibin
5 months ago
3 changed files with 409 additions and 1 deletions
@ -0,0 +1,391 @@ |
|||||
|
package org.dromara.productManagement.controller; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.CompletableFuture; |
||||
|
import java.util.concurrent.ExecutionException; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
import cn.hutool.core.io.FileUtil; |
||||
|
import cn.hutool.core.util.IdUtil; |
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
|
||||
|
import cn.dev33.satoken.annotation.SaCheckPermission; |
||||
|
import okhttp3.*; |
||||
|
import okhttp3.RequestBody; |
||||
|
import org.dromara.common.core.utils.StringUtils; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
|
||||
|
import org.dromara.common.web.core.BaseController; |
||||
|
import org.dromara.common.core.domain.R; |
||||
|
|
||||
|
import org.dromara.productManagement.service.IPmgSupplierInformationService; |
||||
|
|
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
/** |
||||
|
* 供应商信息管理 |
||||
|
* |
||||
|
* @author Lion Li |
||||
|
* @date 2024-06-28 |
||||
|
*/ |
||||
|
@Validated |
||||
|
@RequiredArgsConstructor |
||||
|
@RestController |
||||
|
@RequestMapping("/productManagement/supplierInformation") |
||||
|
public class Qwen72bController extends BaseController { |
||||
|
|
||||
|
private final OkHttpClient client = new OkHttpClient.Builder() |
||||
|
.connectTimeout(300, TimeUnit.SECONDS)//连接超时(单位:秒)
|
||||
|
.callTimeout(1200, TimeUnit.SECONDS)//整个流程耗费的超时时间(单位:秒)--很少人使用
|
||||
|
.pingInterval(50, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
|
||||
|
.readTimeout(600, TimeUnit.SECONDS)//读取超时(单位:秒)
|
||||
|
.writeTimeout(600, TimeUnit.SECONDS)//写入超时(单位:秒)
|
||||
|
.build(); |
||||
|
@SaCheckPermission("productManagement:supplierInformation:list") |
||||
|
@PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
||||
|
public R<String> list111(@RequestPart("file") MultipartFile file,String content) throws IOException, ExecutionException, InterruptedException { |
||||
|
String destDir = "D:\\python项目39"; |
||||
|
System.out.println(content); |
||||
|
String originalFilename = file.getOriginalFilename(); |
||||
|
if (originalFilename == null) { |
||||
|
throw new IllegalArgumentException("文件名不能为空"); |
||||
|
} |
||||
|
|
||||
|
// 获取文件的后缀
|
||||
|
String suffix = FileUtil.getSuffix(originalFilename); |
||||
|
|
||||
|
// 生成唯一的文件名
|
||||
|
String name = IdUtil.fastSimpleUUID() + "." + suffix; |
||||
|
|
||||
|
// 构建目标文件路径
|
||||
|
String destPath = destDir + File.separator + name; |
||||
|
|
||||
|
// 创建目标文件
|
||||
|
File destFile = new File(destPath); |
||||
|
FileUtil.writeFromStream(file.getInputStream(), destFile); |
||||
|
if(!sendRequest("http://183.136.156.2:50000/upload", destPath)){ |
||||
|
return R.fail("上传失败"); |
||||
|
} |
||||
|
String fileName="uploads/"+name; |
||||
|
// 使用Hutool的FileUtil将MultipartFile保存到目标文件
|
||||
|
//
|
||||
|
// String result = "";
|
||||
|
// String infozong = "";
|
||||
|
|
||||
|
|
||||
|
// CompletableFuture<String> checkPlaceName = fetchDataAsync("http://localhost:5000/checkPlaceName/"+fileName);
|
||||
|
// CompletableFuture<String> checkRepeatText = fetchDataAsync("http://localhost:5000/checkRepeatText/"+fileName);
|
||||
|
// CompletableFuture<String> checkCompanyName = fetchDataAsync("http://localhost:5000/checkCompanyName/"+fileName);
|
||||
|
//
|
||||
|
// CompletableFuture.allOf(checkPlaceName, checkRepeatText, checkCompanyName)
|
||||
|
// .thenAccept(ignore -> {
|
||||
|
// try {
|
||||
|
// String checkPlaceNameData = checkPlaceName.get();
|
||||
|
// String checkRepeatTextData = checkRepeatText.get();
|
||||
|
// String checkCompanyNameData = checkCompanyName.get();
|
||||
|
// List<HashMap> entity =null;
|
||||
|
// if(StringUtils.isNotBlank(checkPlaceNameData)){
|
||||
|
// ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
// entity = objectMapper.readValue(result, new TypeReference<List<HashMap>>() {});
|
||||
|
// }
|
||||
|
// // 在这里处理所有数据
|
||||
|
// System.out.println("Data1: " + checkPlaceNameData);
|
||||
|
// System.out.println("Data2: " + checkRepeatTextData);
|
||||
|
// System.out.println("Data3: " + checkCompanyNameData);
|
||||
|
// } catch (Exception e) {
|
||||
|
// e.printStackTrace();
|
||||
|
// }
|
||||
|
// })
|
||||
|
// .exceptionally(ex -> {
|
||||
|
// ex.printStackTrace();
|
||||
|
// return null;
|
||||
|
// });
|
||||
|
//同时访问三个接口
|
||||
|
// CompletableFuture<String> checkPlaceNameData = fetchDataAsync("http://183.136.156.2:50000/checkPlaceName/"+fileName)
|
||||
|
// .thenApply(data -> {
|
||||
|
// List<HashMap> entity =null;
|
||||
|
// if(StringUtils.isNotBlank(data)){
|
||||
|
// ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
// try {
|
||||
|
// entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {});
|
||||
|
// } catch (JsonProcessingException e) {
|
||||
|
// throw new RuntimeException(e);
|
||||
|
// }
|
||||
|
// }
|
||||
|
// if(entity==null||entity.size()==0){
|
||||
|
// return "未发现发现异常地名";
|
||||
|
// }
|
||||
|
// String info = "发现异常地名:\n";
|
||||
|
// for(HashMap map:entity){
|
||||
|
// String placeName = (String) map.get("placeName");
|
||||
|
// String yuanwen = (String) map.get("yuanwen");
|
||||
|
// info += "原文:"+yuanwen+"\n出现异常地名:"+placeName+"!请注意"+"\n";
|
||||
|
// }
|
||||
|
// // 对第一个接口的数据进行处理
|
||||
|
// return info;
|
||||
|
// });
|
||||
|
////
|
||||
|
//// CompletableFuture<String> checkRepeatTextData = fetchDataAsync("http://183.136.156.2:50000/checkRepeatText/"+fileName)
|
||||
|
//// .thenApply(data -> {
|
||||
|
//// // 对第二个接口的数据进行处理
|
||||
|
//// List<HashMap> entity =null;
|
||||
|
//// if(StringUtils.isNotBlank(data)){
|
||||
|
//// ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
//// try {
|
||||
|
//// entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {});
|
||||
|
//// } catch (JsonProcessingException e) {
|
||||
|
//// throw new RuntimeException(e);
|
||||
|
//// }
|
||||
|
//// }
|
||||
|
//// String info = "发现存在相似地方:\n";
|
||||
|
////
|
||||
|
//// if(entity==null||entity.size()==0){
|
||||
|
//// return "未发现原文存在相似地方";
|
||||
|
//// }else{
|
||||
|
//// for(HashMap map:entity){
|
||||
|
//// String yuanwen1 = (String) map.get("yuanwen1");
|
||||
|
//// String yuanwen2 = (String) map.get("yuanwen2");
|
||||
|
//// info += "原文1:"+yuanwen1+"\n原文2:"+yuanwen2+"\n请注意,存在相似"+"\n\n";
|
||||
|
//// }
|
||||
|
//// return info;
|
||||
|
//// }
|
||||
|
//// });
|
||||
|
////
|
||||
|
// CompletableFuture<String> checkCompanyNameData = fetchDataAsync("http://183.136.156.2:50000/checkCompanyName/"+fileName)
|
||||
|
// .thenApply(data -> {
|
||||
|
// // 对第三个接口的数据进行处理
|
||||
|
// List<HashMap> entity =null;
|
||||
|
// if(StringUtils.isNotBlank(data)){
|
||||
|
// ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
// try {
|
||||
|
// entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {});
|
||||
|
// } catch (JsonProcessingException e) {
|
||||
|
// throw new RuntimeException(e);
|
||||
|
// }
|
||||
|
// }
|
||||
|
// if(entity==null||entity.size()==0){
|
||||
|
// return "未发现异常公司或组织名称";
|
||||
|
// }
|
||||
|
// String info = "发现异常公司或组织名称:\n";
|
||||
|
// for(HashMap map:entity){
|
||||
|
// String placeName = (String) map.get("placeName");
|
||||
|
// String yuanwen = (String) map.get("yuanwen");
|
||||
|
// info += "原文:"+yuanwen+"\n异常公司或组织名称:"+placeName+"!请注意"+"\n";
|
||||
|
// }
|
||||
|
// // 对第一个接口的数据进行处理
|
||||
|
// return info;
|
||||
|
// });
|
||||
|
// CompletableFuture<String> getDocumentErrorData = fetchDataAsync("http://183.136.156.2:50000/getDocumentError/"+fileName)
|
||||
|
// .thenApply(data -> {
|
||||
|
// // 对第三个接口的数据进行处理
|
||||
|
// List<HashMap> entity =null;
|
||||
|
// if(StringUtils.isNotBlank(data)){
|
||||
|
// ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
// try {
|
||||
|
// entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {});
|
||||
|
// } catch (JsonProcessingException e) {
|
||||
|
// throw new RuntimeException(e);
|
||||
|
// }
|
||||
|
// }
|
||||
|
// if(entity==null||entity.size()==0){
|
||||
|
// return "未发现错别字";
|
||||
|
// }
|
||||
|
// String info = "发现错别字:\n";
|
||||
|
// for(HashMap map:entity){
|
||||
|
// String placeName = (String) map.get("placeName");
|
||||
|
// String jianyi = (String) map.get("jianyi");
|
||||
|
// info += "原文:"+placeName+"\n建议:"+jianyi+"\n";
|
||||
|
// }
|
||||
|
// // 对第一个接口的数据进行处理
|
||||
|
// return info;
|
||||
|
// });
|
||||
|
//// CompletableFuture<String> completableFuture = CompletableFuture.allOf(checkPlaceNameData, checkRepeatTextData, checkCompanyNameData,getDocumentErrorData)
|
||||
|
//// .thenApply(ignore -> {
|
||||
|
//// try {
|
||||
|
//// return checkPlaceNameData.get()+"\n"+checkRepeatTextData.get()+"\n"+checkCompanyNameData.get();
|
||||
|
//// } catch (Exception e) {
|
||||
|
//// throw new RuntimeException(e);
|
||||
|
//// }
|
||||
|
//// });
|
||||
|
// CompletableFuture<String> completableFuture = CompletableFuture.allOf(checkPlaceNameData,checkCompanyNameData,getDocumentErrorData)
|
||||
|
// .thenApply(ignore -> {
|
||||
|
// try {
|
||||
|
// return checkPlaceNameData.get()+"\n"+checkCompanyNameData.get()+"\n";
|
||||
|
// } catch (Exception e) {
|
||||
|
// throw new RuntimeException(e);
|
||||
|
// }
|
||||
|
// });
|
||||
|
// String strings = completableFuture.get();
|
||||
|
CompletableFuture<String> checkPlaceNameData = fetchDataAsync("http://183.136.156.2:50000/checkPlaceName?filename=" + fileName) |
||||
|
.thenApply(data -> { |
||||
|
List<HashMap> entity = null; |
||||
|
if (StringUtils.isNotBlank(data)) { |
||||
|
ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
try { |
||||
|
entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
if (entity == null || entity.size() == 0) { |
||||
|
return "未发现发现异常地名"; |
||||
|
} |
||||
|
String info = "发现异常地名:\n"; |
||||
|
for (HashMap map : entity) { |
||||
|
String placeName = (String) map.get("placeName"); |
||||
|
String yuanwen = (String) map.get("yuanwen"); |
||||
|
info += "原文:" + yuanwen + "\n出现异常地名:" + placeName + "!请注意" + "\n"; |
||||
|
} |
||||
|
return info; |
||||
|
}); |
||||
|
|
||||
|
CompletableFuture<String> checkCompanyNameData = checkPlaceNameData |
||||
|
.thenCompose(result -> fetchDataAsync("http://183.136.156.2:50000/checkCompanyName?filename=" + fileName)) |
||||
|
.thenApply(data -> { |
||||
|
List<HashMap> entity = null; |
||||
|
if (StringUtils.isNotBlank(data)) { |
||||
|
ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
try { |
||||
|
entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
if (entity == null || entity.size() == 0) { |
||||
|
return "未发现异常公司或组织名称"; |
||||
|
} |
||||
|
String info = "发现异常公司或组织名称:\n"; |
||||
|
for (HashMap map : entity) { |
||||
|
String companyName = (String) map.get("companyName"); |
||||
|
String yuanwen = (String) map.get("yuanwen"); |
||||
|
info += "原文:" + yuanwen + "\n异常公司或组织名称:" + companyName + "!请注意" + "\n"; |
||||
|
} |
||||
|
return info; |
||||
|
}); |
||||
|
|
||||
|
CompletableFuture<String> getDocumentErrorData = checkCompanyNameData |
||||
|
.thenCompose(result -> fetchDataAsync("http://183.136.156.2:50000/getDocumentError?filename=" + fileName)) |
||||
|
.thenApply(data -> { |
||||
|
List<HashMap> entity = null; |
||||
|
if (StringUtils.isNotBlank(data)) { |
||||
|
ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
try { |
||||
|
entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
if (entity == null || entity.size() == 0) { |
||||
|
return "未发现错别字"; |
||||
|
} |
||||
|
String info = "发现错别字:\n"; |
||||
|
for (HashMap map : entity) { |
||||
|
String placeName = (String) map.get("placeName"); |
||||
|
String jianyi = (String) map.get("jianyi"); |
||||
|
info += "原文:" + placeName + "\n建议:" + jianyi + "\n"; |
||||
|
} |
||||
|
return info; |
||||
|
}); |
||||
|
CompletableFuture<String> checkRepeatTextData = getDocumentErrorData |
||||
|
.thenCompose(result -> fetchDataAsync("http://183.136.156.2:50000/getDocumentError?filename=" + fileName+"§ionName="+content)) |
||||
|
.thenApply(data -> { |
||||
|
// 对第二个接口的数据进行处理
|
||||
|
List<HashMap> entity =null; |
||||
|
if(StringUtils.isNotBlank(data)){ |
||||
|
ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
try { |
||||
|
entity = objectMapper.readValue(data, new TypeReference<List<HashMap>>() {}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
String info = "发现存在相似地方:\n"; |
||||
|
|
||||
|
if(entity==null||entity.size()==0){ |
||||
|
return "未发现原文存在相似地方"; |
||||
|
}else{ |
||||
|
for(HashMap map:entity){ |
||||
|
String yuanwen1 = (String) map.get("yuanwen1"); |
||||
|
String yuanwen2 = (String) map.get("yuanwen2"); |
||||
|
info += "原文1:"+yuanwen1+"\n原文2:"+yuanwen2+"\n请注意,存在相似"+"\n\n"; |
||||
|
} |
||||
|
return info; |
||||
|
} |
||||
|
}); |
||||
|
CompletableFuture<String> completableFuture = checkRepeatTextData |
||||
|
.thenApply(result -> { |
||||
|
StringBuilder combinedResult = new StringBuilder(); |
||||
|
try { |
||||
|
combinedResult.append(checkPlaceNameData.get()).append("\n"); |
||||
|
combinedResult.append(checkCompanyNameData.get()).append("\n"); |
||||
|
combinedResult.append(getDocumentErrorData.get()).append("\n"); |
||||
|
combinedResult.append(checkRepeatTextData.get()).append("\n");; |
||||
|
} catch (Exception e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
return combinedResult.toString(); |
||||
|
}); |
||||
|
|
||||
|
String strings = completableFuture.get(); |
||||
|
return R.ok("ok",strings); |
||||
|
} |
||||
|
//对多个请求进行异步处理,并返回CompletableFuture对象,最后通过thenAccept()方法处理结果。
|
||||
|
public CompletableFuture<String> fetchDataAsync(String url) { |
||||
|
CompletableFuture<String> future = new CompletableFuture<>(); |
||||
|
|
||||
|
Request request = new Request.Builder() |
||||
|
.url(url) |
||||
|
.build(); |
||||
|
|
||||
|
client.newCall(request).enqueue(new Callback() { |
||||
|
@Override |
||||
|
public void onFailure(Call call, IOException e) { |
||||
|
future.completeExceptionally(e); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void onResponse(Call call, Response response) throws IOException { |
||||
|
if (response.isSuccessful()) { |
||||
|
future.complete(response.body().string()); |
||||
|
} else { |
||||
|
future.completeExceptionally(new IOException("Unexpected code " + response)); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
return future; |
||||
|
} |
||||
|
public boolean sendRequest(String url,String filePath) { |
||||
|
// 替换为你要上传的文件路径
|
||||
|
File file = new File(filePath); |
||||
|
|
||||
|
RequestBody requestBody = new MultipartBody.Builder() |
||||
|
.setType(MultipartBody.FORM) |
||||
|
.addFormDataPart("file", file.getName(), |
||||
|
RequestBody.create(okhttp3.MediaType.parse(MediaType.APPLICATION_OCTET_STREAM_VALUE), file)) |
||||
|
.build(); |
||||
|
|
||||
|
Request request = new Request.Builder() |
||||
|
.url(url) // 替换为你的Flask服务器地址
|
||||
|
.post(requestBody) |
||||
|
.build(); |
||||
|
|
||||
|
try (Response response = client.newCall(request).execute()) { |
||||
|
if (!response.isSuccessful()) { |
||||
|
return false; |
||||
|
} |
||||
|
return true; |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue