You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
18 KiB
Java

2 years ago
package weaver.interfaces.workflow.action.javacode;
import cn.wps.yun.StringUtil;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.icbc.api.internal.apache.http.impl.cookie.S;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.general.BaseBean;
import weaver.soa.workflow.request.RequestInfo;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Online custom action interface
*/
public class Action20231123035106 implements Action{
private static final BaseBean bb = new BaseBean();
private static final String postSQl = "";
private static final String receiptSQL = "";
private static final String queryDocIdSql =
"select * from docimagefile docfile " +
"left join docdetail doc on docfile.DOCID = doc.ID " +
"left join imagefile file on docfile.IMAGEFILEID = file.IMAGEFILEID " +
// "left join uf_tssjb tssjb on docfile.DOCID = tssjb.wdid " +
"where (doc.docsubject like '%食品%' or doc.docsubject like '%专项%' ) ";
private static final String dataEqualSql = "and doc.DOCCREATEDATE = ? ";
private static final String dataRangeSql = "and doc.DOCCREATEDATE >= ? and doc.DOCCREATEDATE <= ? ";
public String url = "";
public String queryType = "";
public String queryDate = "";
public String startDate = "";
public String endDate = "";
public String deptIds = "";
// public String userNames = "";
public String dirPath = "/opt/weaver/scjgw";
public String postSeccategory = "";
public String receiptSeccategory = "";
@Override
public String execute(RequestInfo requestInfo) {
writeLog("执行定时任务开始","PushCorn");
writeLog("url",url);
writeLog("queryType",queryType);
writeLog("queryDate",queryDate);
writeLog("startDate",startDate);
writeLog("endDate",endDate);
writeLog("deptIds",deptIds);
// writeLog("userNames",userNames);
writeLog("dirPath",dirPath);
writeLog("postSeccategory",postSeccategory);
writeLog("receiptSeccategory",receiptSeccategory);
try {
System.setOut(new PrintStream(System.out, true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
writeLog("new String(s.getBytes(UTF_8))",new String("通".getBytes(UTF_8)));
writeLog("new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)", new String("通".getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
writeLog("new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)", new String("通".getBytes(StandardCharsets.UTF_8), Charset.forName("GBK")));
// 假设 s 是 UTF-8 编码的字符串
String s1 = "通";
// 将 s 从 UTF-8 转换为字节数组
byte[] utf8Bytes = s1.getBytes(StandardCharsets.UTF_8);
// 将字节数组从 UTF-8 转换为 GBK
String gbkString = new String(utf8Bytes, Charset.forName("GBK"));
// 打印日志
writeLog("new String(s.getBytes(StandardCharsets.UTF_8), Charset.forName(\"GBK\"))", gbkString);
writeLog("new String(s.getBytes(UTF_8))",new String("通报".getBytes(UTF_8)));
writeLog("new String(s.getBytes(UTF_8))",new String("关于2023年天津市食品安全抽检监测任务完成情况的通报第三期.zip".getBytes(UTF_8)));
String x= "关于2023年天津市食品安全抽检监测任务完成情况的通报第三期.zip";
writeLog("utf8Str===>",convertGbkToUtf8(x));
String querySql = queryDocIdSql;
//生成人员map
final HashMap<String, String> useridNamemap = new HashMap<>();
RecordSet recordSet = new RecordSet();
// if (!StringUtil.isEmpty(deptIds)) {
// querySql = querySql + "and doc.docdepartmentid in (" + deptIds + " )";
// // String[] deptids = deptIds.split(",");
// // String[] usernames = deptIds.split(",");
// // for (int i = 0; i < userids.length; i++) {
// // useridNamemap.put(userids[i],usernames[i]);
// // }
// }else {
// return;
// }
// List<String> childNodeIds = getChildNodeIds(postSeccategory);
// childNodeIds.add(postSeccategory);
// List<String> childNodeIds1 = getChildNodeIds(receiptSeccategory);
// childNodeIds1.add(receiptSeccategory);
// childNodeIds.addAll(childNodeIds1);
//添加目录
// querySql = querySql + "and ( seccategory in ( "+ String.join(",",childNodeIds)+" ) )" ;
//查询指定天的
// if ("0".equals(queryType)) {
// writeLog("查询指定天的文档");
// querySql = querySql + dataEqualSql;
// recordSet.executeQuery(querySql, queryDate);
// } else if ("1".equals(queryType)) {
// writeLog("查询指定范围的文档",startDate,endDate);
// querySql = querySql + dataRangeSql;
// recordSet.executeQuery(querySql, startDate, endDate);
// } else if ("2".equals(queryType)) {
// writeLog("查询昨天的文档");
// querySql = querySql + dataEqualSql;
// recordSet.executeQuery(querySql, getYesterdayDateStr());
// }
recordSet.executeQuery(querySql);
writeLog("查询sql",querySql);
writeLog("查询sql",recordSet.getExceptionMsg());
writeLog("查询数量", recordSet.getCounts()+"");
//开始推送数据
//检查文件夹路径
checkAndCreateDir();
HashMap<String, String> docid_docNameMap = new HashMap<>();
HashMap<String, List<String>> docid_imagefilleIdMap = new HashMap<>();
HashMap<String,HashMap<String, String>> fileinfo = new HashMap<>();
while (recordSet.next()){
//文档id
String docid = weaver.general.Util.null2String(recordSet.getString("DOCID"));
//文档标题
String docsubject = weaver.general.Util.null2String(recordSet.getString("docsubject"));
//附件id
String imagefileid = weaver.general.Util.null2String(recordSet.getString("IMAGEFILEID"));
//文档时间
String DOCCREATEDATE = weaver.general.Util.null2String(recordSet.getString("DOCCREATEDATE"));
//目录 seccategory
String seccategory = weaver.general.Util.null2String(recordSet.getString("seccategory"));
String DOCCREATERID = weaver.general.Util.null2String(recordSet.getString("DOCCREATERID"));
HashMap<String, String> map = new HashMap<>();
map.put("DOCCREATEDATE",DOCCREATEDATE);
map.put("seccategory",postSeccategory.equals(seccategory) ? "2" : "1");
map.put("DOCCREATERID",new User(Util.getIntValue(DOCCREATERID)).getLastname() );
docid_docNameMap.put(docid,docsubject);
fileinfo.put(docid,map);
List<String> imagefilleIdList = docid_imagefilleIdMap.get(docid);
if (imagefilleIdList == null){
ArrayList<String> filleIdList = new ArrayList<>();
filleIdList.add(imagefileid);
docid_imagefilleIdMap.put(docid,filleIdList);
}else {
imagefilleIdList.add(imagefileid);
}
}
writeLog("需要推送的文档",docid_docNameMap.toString());
// 开始封装文件
String DateDirFile = "";
if(docid_docNameMap.keySet().size() > 0 ){
// 创建今天的文件夹
DateDirFile = checkAndCreateDir(getTodayDateStr());
}else {
return "";
}
String finalDateDirFile = DateDirFile;
writeLog("今天的文件目录",DateDirFile);
writeLog("docid_imagefilleIdMap",docid_imagefilleIdMap.toString());
docid_imagefilleIdMap.forEach((docid, filleIdList)->{
String FileDir = docid_docNameMap.get(docid);
String FileDir2 = new String(FileDir.getBytes(UTF_8));
writeLog("编码修改前 FileDir",FileDir);
writeLog("编码修改后 FileDir2",FileDir2);
String zipFileName = finalDateDirFile + File.separator + docid +".zip";
//修改下编码
String zipFileName2 = new String(zipFileName.getBytes(StandardCharsets.UTF_8));
String zipFileName3 = null;
try {
// zipFileName.getBytes(UTF_8)
zipFileName3 = new String(zipFileName.getBytes("GBK"), UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
writeLog("编码修改前 zipFileName",zipFileName);
writeLog("编码修改后 zipFileName",zipFileName2);
writeLog("编码修改后 zipFileName3",zipFileName3);
File filezip = new File(new String(zipFileName.getBytes(UTF_8)));
byte[] folderBytes = new byte[0]; // 假设原始编码是GBK
try {
folderBytes = zipFileName.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedFolderName = new String(folderBytes, Charset.defaultCharset());
writeLog("编码修改后 encodedFolderName",encodedFolderName);
ZipArchiveOutputStream zos =null;
writeLog("filezip",zipFileName);
try {
zos = new ZipArchiveOutputStream( new FileOutputStream(filezip));
zos.setEncoding("UTF-8");
for (String filleId : filleIdList) {
ImageFileManager ifm = new ImageFileManager();
ifm.getImageFileInfoById(Integer.parseInt(filleId));
InputStream imagefile = ifm.getInputStream();
// String s1 = ifm.getImageFileName().split(".")[0];
writeLog("压缩文件",ifm.getImageFileName());
addInputStreamToZip(imagefile,zos,FileDir+File.separator+ifm.getImageFileName());
}
} catch (FileNotFoundException e) {
writeLog("压缩文件异常",e.toString());
e.printStackTrace();
} catch (IOException e) {
writeLog("压缩文件异常",e.toString());
e.printStackTrace();
}catch (Exception e){
writeLog("压缩文件异常",e.toString());
e.printStackTrace();
}finally {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
writeLog("压缩完文件名",filezip.getName());
String s = "通.zip";
String convertedString ="";
try {
byte[] bytes = StringUtils.getBytes(s, "UTF-8");
// 使用Guava转换编码
ByteSource byteSource = ByteSource.wrap(s.getBytes(Charsets.UTF_8));
byte[] gbkBytes = byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8).read();
// String gbkString = new String(gbkBytes, "GBK");
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
e.printStackTrace();
}
// filezip.renameTo(new File(convertedString));
// HashMap<String, String> map = fileinfo.get(docid);
//开始推送数据
// try {
// String s = ApiClient.callApi(url, docid, docid_docNameMap.get(docid), map.get("DOCCREATEDATE"),
// map.get("seccategory"), map.get("DOCCREATERID"), filezip);
// writeLog("返回数据",s);
// } catch (IOException e) {
// e.printStackTrace();
// writeLog(e.getMessage());
// writeLog(e.getMessage());
// }
});
return "";
}
private static String getTodayDateStr() {
// 获取当前日期
LocalDate today = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// 将昨天的日期格式化为字符串
return today.format(formatter);
}
private static String getYesterdayDateStr() {
// 获取当前日期
LocalDate today = LocalDate.now();
// 减去一天得到前一天的日期
LocalDate yesterday = today.minusDays(1);
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 将昨天的日期格式化为字符串
return yesterday.format(formatter);
}
private static void writeLog(String... log) {
StringBuilder logStr = new StringBuilder();
for (String s : log) {
logStr.append("|").append(s);
}
bb.writeLog("PushCornTest.class"+"==>"+logStr);
}
private void checkAndCreateDir(){
// 创建File对象
File directory = new File(dirPath);
// 检查路径是否存在
if (!directory.exists()) {
// 不存在,尝试创建多级目录
boolean result = directory.mkdirs();
if (result) {
writeLog("多级目录已创建成功:" + dirPath);
} else {
writeLog("目录创建失败,请检查路径是否正确,或者程序是否有相应的权限。");
}
} else {
writeLog("目录已存在:" + dirPath);
}
}
private String checkAndCreateDir(String date){
// 创建File对象
File directory = new File(dirPath+File.separator+date);
// 检查路径是否存在
if (!directory.exists()) {
// 不存在,尝试创建多级目录
boolean result = directory.mkdirs();
if (result) {
writeLog("多级目录已创建成功:" + dirPath+File.separator+date);
} else {
writeLog(dirPath+File.separator+date,"目录创建失败,请检查路径是否正确,或者程序是否有相应的权限。");
return "-1";
}
} else {
writeLog("目录已存在:" + dirPath+File.separator+date);
}
return dirPath+File.separator+date;
}
public void addInputStreamToZip(InputStream inputStream, ZipArchiveOutputStream zos, String entryName) throws IOException {
writeLog("编码修改后 entryName",entryName);
ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
zos.putArchiveEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
inputStream.close();
zos.closeArchiveEntry();
}
public static List<String> getChildNodeIds(String parentId) {
String sql = "SELECT id FROM docseccategory WHERE parentid = ?";
List<String> childIds = new ArrayList<>();
RecordSet rs = new RecordSet();
rs.executeQuery(sql , parentId);
while (rs.next()) {
String id = rs.getString("id");
childIds.add(id);
childIds.addAll(getChildNodeIds(id)); // 递归调用以获取所有后代节点
}
return childIds;
}
public static String convertGbkToUtf8(String gbkStr) {
// 将GBK字符串转换为Unicode
String unicodeStr = new String(gbkStr.getBytes(Charset.forName("GBK")), Charset.forName("UTF-8"));
// 从Unicode转换为UTF-8
byte[] utf8Bytes = unicodeStr.getBytes(Charset.forName("UTF-8"));
return new String(utf8Bytes, Charset.forName("UTF-8"));
}
public static String toUnicodeString(String str) {
StringBuilder unicode = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
unicode.append(String.format("\\u%04X", (int) str.charAt(i)));
}
return unicode.toString();
}
public static String unicodeToUtf8(String unicodeStr) {
String stringFromUnicode = unicodeStr.replace("\\u", "");
byte[] utf8Bytes = new byte[stringFromUnicode.length() / 2];
for (int i = 0; i < stringFromUnicode.length(); i += 2) {
int value = Integer.parseInt(stringFromUnicode.substring(i, i + 2), 16);
utf8Bytes[i / 2] = (byte) value;
}
return new String(utf8Bytes, StandardCharsets.UTF_8);
}
}