提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.controller.admin; |
D |
2 |
|
|
3 |
import com.alibaba.excel.EasyExcel; |
|
4 |
import com.alibaba.excel.converters.longconverter.LongStringConverter; |
|
5 |
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
|
6 |
import com.alibaba.fastjson.JSON; |
|
7 |
import com.alibaba.fastjson.JSONArray; |
|
8 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
9 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
10 |
import com.iailab.framework.common.pojo.CommonResult; |
|
11 |
import com.iailab.framework.excel.core.handler.SelectSheetWriteHandler; |
|
12 |
import com.iailab.framework.excel.core.util.ExcelUtils; |
|
13 |
import com.iailab.module.model.mpk.common.utils.Readtxt; |
|
14 |
import com.iailab.module.model.mpk.dto.MdkDTO; |
|
15 |
import com.iailab.module.system.enums.common.SexEnum; |
|
16 |
import io.swagger.v3.oas.annotations.Operation; |
|
17 |
import io.swagger.v3.oas.annotations.Parameter; |
|
18 |
import io.swagger.v3.oas.annotations.Parameters; |
|
19 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
20 |
import org.springframework.util.CollectionUtils; |
|
21 |
import org.springframework.web.bind.annotation.*; |
|
22 |
import org.springframework.web.multipart.MultipartFile; |
|
23 |
|
|
24 |
import javax.servlet.http.HttpServletResponse; |
|
25 |
import java.io.IOException; |
|
26 |
import java.lang.reflect.Method; |
|
27 |
import java.net.URLEncoder; |
|
28 |
import java.nio.charset.StandardCharsets; |
|
29 |
import java.util.*; |
|
30 |
|
|
31 |
import static com.iailab.framework.common.pojo.CommonResult.error; |
|
32 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
33 |
|
|
34 |
/** |
|
35 |
* @author PanZhibao |
|
36 |
* @Description |
|
37 |
* @createTime 2024年08月08日 |
|
38 |
*/ |
|
39 |
@RestController |
|
40 |
@RequestMapping("/model/mpk/api") |
|
41 |
public class MdkController { |
|
42 |
@PostMapping("run") |
|
43 |
public CommonResult<String> run(@RequestBody MdkDTO dto) { |
|
44 |
System.out.println("runTime=" + System.currentTimeMillis()); |
|
45 |
try { |
|
46 |
List<String> datas = dto.getDatas(); |
|
47 |
|
|
48 |
int paramLength = dto.getHasModel() ? datas.size() + 2 : datas.size() + 1; |
|
49 |
Object[] paramsValueArray = new Object[paramLength]; |
|
50 |
Class<?>[] paramsArray = new Class[paramLength]; |
|
51 |
|
|
52 |
try { |
|
53 |
for (int i = 0; i < datas.size(); i++) { |
|
54 |
String json = datas.get(i); |
|
55 |
JSONArray jsonArray = JSON.parseArray(json); |
|
56 |
double[][] data = new double[jsonArray.size()][jsonArray.getJSONArray(0).size()]; |
|
57 |
for (int j = 0; j < jsonArray.size(); j++) { |
|
58 |
for (int k = 0; k < jsonArray.getJSONArray(j).size(); k++) { |
|
59 |
data[j][k] = jsonArray.getJSONArray(j).getDoubleValue(k); |
|
60 |
} |
|
61 |
} |
|
62 |
paramsValueArray[i] = data; |
|
63 |
paramsArray[i] = double[][].class; |
|
64 |
} |
|
65 |
} catch (Exception e) { |
|
66 |
e.printStackTrace(); |
|
67 |
return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),"参数错误,请检查!"); |
|
68 |
} |
|
69 |
|
|
70 |
if (dto.getHasModel()) { |
|
71 |
paramsValueArray[datas.size()] = dto.getModel(); |
|
72 |
paramsValueArray[datas.size() + 1] = dto.getModelSettings(); |
|
73 |
paramsArray[datas.size()] = HashMap.class; |
|
74 |
paramsArray[datas.size() + 1] = HashMap.class; |
|
75 |
}else { |
|
76 |
paramsValueArray[datas.size()] = dto.getModelSettings(); |
|
77 |
paramsArray[datas.size()] = HashMap.class; |
|
78 |
} |
|
79 |
|
|
80 |
Class<?> clazz = Class.forName(dto.getClassName()); |
|
81 |
Method method = clazz.getMethod(dto.getMethodName(), paramsArray); |
|
82 |
HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray); |
|
83 |
return success(JSON.toJSONString(result)); |
|
84 |
} catch (Exception ex) { |
|
85 |
ex.printStackTrace(); |
|
86 |
return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常"); |
|
87 |
} finally { |
|
88 |
System.gc(); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
@PostMapping("/import") |
|
93 |
@Operation(summary = "导入参数") |
|
94 |
public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception { |
|
95 |
List<double[][]> datas = Readtxt.readMethodExcel(file); |
|
96 |
List<String> result = new ArrayList<>(); |
|
97 |
if (!CollectionUtils.isEmpty(datas)) { |
|
98 |
for (double[][] data : datas) { |
|
99 |
if (data.length > 0) { |
|
100 |
result.add(JSON.toJSONString(data)); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
return success(result); |
|
105 |
} |
|
106 |
} |