package com.iailab.module.model.mpk.controller.admin; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.converters.longconverter.LongStringConverter; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.iailab.framework.common.enums.CommonStatusEnum; import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.excel.core.handler.SelectSheetWriteHandler; import com.iailab.framework.excel.core.util.ExcelUtils; import com.iailab.module.model.mpk.common.utils.Readtxt; import com.iailab.module.model.mpk.dto.MdkDTO; import com.iailab.module.system.enums.common.SexEnum; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.*; import static com.iailab.framework.common.pojo.CommonResult.error; import static com.iailab.framework.common.pojo.CommonResult.success; /** * @author PanZhibao * @Description * @createTime 2024年08月08日 */ @RestController @RequestMapping("/model/mpk/api") public class MdkController { @PostMapping("run") public CommonResult run(@RequestBody MdkDTO dto) { System.out.println("runTime=" + System.currentTimeMillis()); try { List datas = dto.getDatas(); int paramLength = dto.getHasModel() ? datas.size() + 2 : datas.size() + 1; Object[] paramsValueArray = new Object[paramLength]; Class[] paramsArray = new Class[paramLength]; try { for (int i = 0; i < datas.size(); i++) { String json = datas.get(i); JSONArray jsonArray = JSON.parseArray(json); double[][] data = new double[jsonArray.size()][jsonArray.getJSONArray(0).size()]; for (int j = 0; j < jsonArray.size(); j++) { for (int k = 0; k < jsonArray.getJSONArray(j).size(); k++) { data[j][k] = jsonArray.getJSONArray(j).getDoubleValue(k); } } paramsValueArray[i] = data; paramsArray[i] = double[][].class; } } catch (Exception e) { e.printStackTrace(); return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),"参数错误,请检查!"); } if (dto.getHasModel()) { paramsValueArray[datas.size()] = dto.getModel(); paramsValueArray[datas.size() + 1] = dto.getModelSettings(); paramsArray[datas.size()] = HashMap.class; paramsArray[datas.size() + 1] = HashMap.class; }else { paramsValueArray[datas.size()] = dto.getModelSettings(); paramsArray[datas.size()] = HashMap.class; } Class clazz = Class.forName(dto.getClassName()); Method method = clazz.getMethod(dto.getMethodName(), paramsArray); HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray); return success(JSON.toJSONString(result)); } catch (Exception ex) { ex.printStackTrace(); return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常"); } finally { System.gc(); } } @PostMapping("/import") @Operation(summary = "导入参数") public CommonResult> importExcel(@RequestParam("file") MultipartFile file) throws Exception { List datas = Readtxt.readMethodExcel(file); List result = new ArrayList<>(); if (!CollectionUtils.isEmpty(datas)) { for (double[][] data : datas) { if (data.length > 0) { result.add(JSON.toJSONString(data)); } } } return success(result); } }