|
|
|
@ -1,133 +0,0 @@
|
|
|
|
|
package com.dsideal.base.Util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.file.FileReader;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dsideal.base.Const.PyEchartsModel;
|
|
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
public class PythonUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:通过SQL获取查询到的数组
|
|
|
|
|
* 作者:黄海
|
|
|
|
|
* 时间:2021-11-18
|
|
|
|
|
*
|
|
|
|
|
* @param sql
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String[] getArrayBySql(String sql) {
|
|
|
|
|
List<Record> list = Db.find(sql);
|
|
|
|
|
String[] a = new String[list.size()];
|
|
|
|
|
//column name
|
|
|
|
|
String column_name = "";
|
|
|
|
|
String[] x = sql.split(" ");
|
|
|
|
|
for (int i = 0; i < x.length; i++) {
|
|
|
|
|
if (x[i].equalsIgnoreCase("select")) {
|
|
|
|
|
column_name = x[i + 1];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (column_name.equals("")) {
|
|
|
|
|
System.out.println("输入的SQL有误,无法找到列名!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < list.size(); i++) a[i] = list.get(i).getStr(column_name);
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//字符串
|
|
|
|
|
public static Integer ConstStr = 1;
|
|
|
|
|
//整数
|
|
|
|
|
public static Integer ConstNum = 2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:将字符串数组拼装成字符串
|
|
|
|
|
* 作者:黄海
|
|
|
|
|
* 时间:2021-11-18
|
|
|
|
|
*
|
|
|
|
|
* @param arr
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String WriteArrayString(String arrName, Integer type, String[] arr) {
|
|
|
|
|
String res = arrName + " = [";
|
|
|
|
|
for (String s : arr) res += type == ConstStr ? "'" + s + "'," : s + ",";
|
|
|
|
|
if (arr.length > 0) res = res.substring(0, res.length() - 1);
|
|
|
|
|
res += "]\n";
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:运行python脚本
|
|
|
|
|
* 作者:黄海
|
|
|
|
|
* 时间:2021-11-18
|
|
|
|
|
*
|
|
|
|
|
* @param target
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String run(String target) {
|
|
|
|
|
String res = "";
|
|
|
|
|
Process proc;
|
|
|
|
|
try {
|
|
|
|
|
proc = Runtime.getRuntime().exec("python " + target);
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.forName("GBK")));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = in.readLine()) != null)
|
|
|
|
|
res += line + "\n";
|
|
|
|
|
in.close();
|
|
|
|
|
proc.waitFor();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:向指定的python脚本中添加数组数据
|
|
|
|
|
* 作者:黄海
|
|
|
|
|
* 时间:2021-11-18
|
|
|
|
|
*
|
|
|
|
|
* @param python_file_name
|
|
|
|
|
* @param arrays
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static String addContentToPython(String python_file_name, List<PyEchartsModel> arrays) throws IOException {
|
|
|
|
|
String pythonTemplatePath = PathKit.getRootClassPath() + "/Py/";
|
|
|
|
|
String source = pythonTemplatePath + python_file_name;
|
|
|
|
|
String workingPath = System.getProperty("java.io.tmpdir");
|
|
|
|
|
String target = workingPath + "/" + UUID.randomUUID().toString().toLowerCase() + ".py";
|
|
|
|
|
FileReader fileReader = new FileReader(source);
|
|
|
|
|
String result = fileReader.readString();
|
|
|
|
|
FileWriter writer = new FileWriter(target);
|
|
|
|
|
for (int i = 0; i < arrays.size(); i++)
|
|
|
|
|
writer.append(WriteArrayString(arrays.get(i).arrayName, arrays.get(i).parameterType, arrays.get(i).content));
|
|
|
|
|
writer.append("\n");
|
|
|
|
|
writer.append(result);
|
|
|
|
|
writer.close();
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取python脚本的执行结果
|
|
|
|
|
* @param python_file_name
|
|
|
|
|
* @param list
|
|
|
|
|
* @return
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject getPythonResult(String python_file_name, List<PyEchartsModel> list) throws IOException {
|
|
|
|
|
//添加到模板中,返回新生成的python脚本路径
|
|
|
|
|
String target = PythonUtil.addContentToPython(python_file_name, list);
|
|
|
|
|
//3、调用Python脚本
|
|
|
|
|
String res = run(target);
|
|
|
|
|
return JSONObject.parseObject(res);
|
|
|
|
|
}
|
|
|
|
|
}
|