提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.controller.admin; |
D |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
6 |
import com.iailab.framework.common.pojo.CommonResult; |
1fea3e
|
7 |
import com.iailab.module.model.mpk.common.MdkConstant; |
912a1e
|
8 |
import com.iailab.module.model.mpk.common.utils.DllUtils; |
449017
|
9 |
import com.iailab.module.model.mpk.common.utils.Readtxt; |
D |
10 |
import com.iailab.module.model.mpk.dto.MdkDTO; |
|
11 |
import io.swagger.v3.oas.annotations.Operation; |
912a1e
|
12 |
import lombok.extern.slf4j.Slf4j; |
1fea3e
|
13 |
import org.springframework.beans.factory.annotation.Value; |
449017
|
14 |
import org.springframework.util.CollectionUtils; |
D |
15 |
import org.springframework.web.bind.annotation.*; |
|
16 |
import org.springframework.web.multipart.MultipartFile; |
|
17 |
|
1fea3e
|
18 |
import java.io.File; |
D |
19 |
import java.net.URLClassLoader; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.List; |
449017
|
23 |
|
D |
24 |
import static com.iailab.framework.common.pojo.CommonResult.error; |
|
25 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
26 |
|
|
27 |
/** |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2024年08月08日 |
|
31 |
*/ |
|
32 |
@RestController |
912a1e
|
33 |
@Slf4j |
449017
|
34 |
@RequestMapping("/model/mpk/api") |
D |
35 |
public class MdkController { |
1fea3e
|
36 |
@Value("${mpk.bak-file-path}") |
D |
37 |
private String mpkBakFilePath; |
|
38 |
|
|
39 |
|
449017
|
40 |
@PostMapping("run") |
D |
41 |
public CommonResult<String> run(@RequestBody MdkDTO dto) { |
912a1e
|
42 |
Class<?> clazz; |
D |
43 |
URLClassLoader classLoader; |
1fea3e
|
44 |
try { |
D |
45 |
File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar"); |
|
46 |
if (!jarFile.exists()) { |
|
47 |
throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath()); |
|
48 |
} |
|
49 |
File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll"); |
|
50 |
if (!dllFile.exists()) { |
912a1e
|
51 |
throw new RuntimeException("dll文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath()); |
1fea3e
|
52 |
} |
D |
53 |
// 加载jar包 |
912a1e
|
54 |
classLoader = DllUtils.loadJar(jarFile.getAbsolutePath()); |
D |
55 |
// 实现类 |
|
56 |
clazz = classLoader.loadClass(dto.getClassName()); |
|
57 |
// 加载dll到实现类 |
|
58 |
DllUtils.loadDll(clazz,dllFile.getAbsolutePath()); |
1fea3e
|
59 |
} catch (Exception e) { |
912a1e
|
60 |
e.printStackTrace(); |
1fea3e
|
61 |
throw new RuntimeException("加载运行环境失败。"); |
D |
62 |
} |
|
63 |
|
449017
|
64 |
System.out.println("runTime=" + System.currentTimeMillis()); |
D |
65 |
try { |
|
66 |
List<String> datas = dto.getDatas(); |
|
67 |
|
|
68 |
int paramLength = dto.getHasModel() ? datas.size() + 2 : datas.size() + 1; |
|
69 |
Object[] paramsValueArray = new Object[paramLength]; |
|
70 |
Class<?>[] paramsArray = new Class[paramLength]; |
|
71 |
|
|
72 |
try { |
|
73 |
for (int i = 0; i < datas.size(); i++) { |
|
74 |
String json = datas.get(i); |
|
75 |
JSONArray jsonArray = JSON.parseArray(json); |
|
76 |
double[][] data = new double[jsonArray.size()][jsonArray.getJSONArray(0).size()]; |
|
77 |
for (int j = 0; j < jsonArray.size(); j++) { |
|
78 |
for (int k = 0; k < jsonArray.getJSONArray(j).size(); k++) { |
|
79 |
data[j][k] = jsonArray.getJSONArray(j).getDoubleValue(k); |
|
80 |
} |
|
81 |
} |
|
82 |
paramsValueArray[i] = data; |
|
83 |
paramsArray[i] = double[][].class; |
|
84 |
} |
|
85 |
} catch (Exception e) { |
|
86 |
e.printStackTrace(); |
|
87 |
return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),"参数错误,请检查!"); |
|
88 |
} |
|
89 |
|
|
90 |
if (dto.getHasModel()) { |
|
91 |
paramsValueArray[datas.size()] = dto.getModel(); |
|
92 |
paramsValueArray[datas.size() + 1] = dto.getModelSettings(); |
|
93 |
paramsArray[datas.size()] = HashMap.class; |
|
94 |
paramsArray[datas.size() + 1] = HashMap.class; |
|
95 |
}else { |
|
96 |
paramsValueArray[datas.size()] = dto.getModelSettings(); |
|
97 |
paramsArray[datas.size()] = HashMap.class; |
|
98 |
} |
|
99 |
|
912a1e
|
100 |
HashMap result = (HashMap) clazz.getDeclaredMethod(dto.getMethodName(), paramsArray).invoke(clazz.newInstance(), paramsValueArray); |
449017
|
101 |
return success(JSON.toJSONString(result)); |
D |
102 |
} catch (Exception ex) { |
|
103 |
ex.printStackTrace(); |
|
104 |
return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常"); |
|
105 |
} finally { |
912a1e
|
106 |
if (classLoader != null) { |
D |
107 |
DllUtils.uploadDll(classLoader); |
|
108 |
DllUtils.uploadJar(classLoader); |
|
109 |
} |
449017
|
110 |
System.gc(); |
D |
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
@PostMapping("/import") |
|
115 |
@Operation(summary = "导入参数") |
|
116 |
public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception { |
|
117 |
List<double[][]> datas = Readtxt.readMethodExcel(file); |
|
118 |
List<String> result = new ArrayList<>(); |
|
119 |
if (!CollectionUtils.isEmpty(datas)) { |
|
120 |
for (double[][] data : datas) { |
|
121 |
if (data.length > 0) { |
|
122 |
result.add(JSON.toJSONString(data)); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
return success(result); |
1fea3e
|
127 |
} |
449017
|
128 |
} |