|
|
|
@ -2,7 +2,7 @@ package com.hnac.hzims.safeproduct.utils;
|
|
|
|
|
|
|
|
|
|
import cn.afterturn.easypoi.word.WordExportUtil; |
|
|
|
|
import com.hnac.hzims.common.logs.utils.StringUtils; |
|
|
|
|
import com.hnac.hzims.ticket.annotation.DateTimeToMap; |
|
|
|
|
import com.hnac.hzims.safeproduct.constants.SafeProductConstant; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
|
@ -13,10 +13,12 @@ import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.*; |
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
import java.net.URL; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 公用工具类 |
|
|
|
@ -156,8 +158,8 @@ public class BaseUtil {
|
|
|
|
|
* @return map参数 |
|
|
|
|
*/ |
|
|
|
|
public static Map<String, Object> obj2Map(Object obj) { |
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
List<Field> fieldList = new ArrayList<>(); |
|
|
|
|
Class<?> clazz = obj.getClass(); |
|
|
|
|
while (clazz != null) { |
|
|
|
@ -172,59 +174,81 @@ public class BaseUtil {
|
|
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
|
throw new ServiceException("获取属性性出错"); |
|
|
|
|
} |
|
|
|
|
if (value instanceof List) { |
|
|
|
|
List<Map<String, Object>> list = ((List<?>) value).stream().map(BaseUtil::obj2Map).collect(Collectors.toList()); |
|
|
|
|
result.put(field.getName(),list); |
|
|
|
|
} |
|
|
|
|
else if (value instanceof LocalDateTime) { |
|
|
|
|
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); |
|
|
|
|
LocalDateTime time = (LocalDateTime) value; |
|
|
|
|
if(ObjectUtil.isEmpty(dateTimeToMap)) { |
|
|
|
|
result.put(field.getName(),time.format(DateUtil.TIME_FORMATTER)); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
result.putAll(LocalDateConvertMap(time,dateTimeToMap.prefix(),dateTimeToMap.split())); |
|
|
|
|
} |
|
|
|
|
if (value instanceof LocalDateTime) { |
|
|
|
|
value = DateUtil.format((LocalDateTime) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE); |
|
|
|
|
} |
|
|
|
|
else if (value instanceof Date) { |
|
|
|
|
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); |
|
|
|
|
Date time = (Date) value; |
|
|
|
|
if (ObjectUtil.isEmpty(dateTimeToMap)) { |
|
|
|
|
result.put(field.getName(),DateUtil.format(time,DateUtil.PATTERN_DATETIME)); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"year", time.getYear()); |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"month", time.getMonth()); |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"day", time.getDay()); |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"hour", time.getHours()); |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"minuter", time.getMinutes()); |
|
|
|
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"second", time.getSeconds()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
result.put(field.getName(),value); |
|
|
|
|
value = DateUtil.format((Date) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE); |
|
|
|
|
} |
|
|
|
|
result.put(field.getName(),value); |
|
|
|
|
}); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 图片转字节 |
|
|
|
|
* @param tempImgPath 图片路径 |
|
|
|
|
* @return 图片字节 |
|
|
|
|
*/ |
|
|
|
|
public static byte[] imgToByte(String tempImgPath) { |
|
|
|
|
File file = new File(tempImgPath); |
|
|
|
|
byte[] buffer = null; |
|
|
|
|
try { |
|
|
|
|
FileInputStream fis = new FileInputStream(file); |
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); |
|
|
|
|
byte[] b = new byte[1000]; |
|
|
|
|
int n; |
|
|
|
|
while ((n = fis.read(b)) != -1) { |
|
|
|
|
bos.write(b, 0, n); |
|
|
|
|
} |
|
|
|
|
fis.close(); |
|
|
|
|
bos.close(); |
|
|
|
|
buffer = bos.toByteArray(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error(e.getMessage()); |
|
|
|
|
} |
|
|
|
|
//删除临时文件
|
|
|
|
|
file.delete(); |
|
|
|
|
return buffer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 将时间按格式转化为map对象 |
|
|
|
|
* @param dateTime 待转换时间 |
|
|
|
|
* @param prefix 转换格式Key前缀 |
|
|
|
|
* @param split 转换格式Key分隔符 |
|
|
|
|
* @return map对象 |
|
|
|
|
* 下载文件 |
|
|
|
|
* @param fileUrl 文件路径 |
|
|
|
|
* @param downloadFileDir 下载路径 |
|
|
|
|
* @return 下载文件路径 |
|
|
|
|
*/ |
|
|
|
|
public static Map<String, Object> LocalDateConvertMap(LocalDateTime dateTime,String prefix,String split) { |
|
|
|
|
return new HashMap<String, Object>() {{ |
|
|
|
|
put(prefix + split + "year", dateTime.getYear()); |
|
|
|
|
put(prefix + split + "month", dateTime.getMonthValue()); |
|
|
|
|
put(prefix + split + "day", dateTime.getDayOfMonth()); |
|
|
|
|
put(prefix + split + "hour", dateTime.getHour()); |
|
|
|
|
put(prefix + split + "minuter", dateTime.getMinute()); |
|
|
|
|
put(prefix + split + "second", dateTime.getSecond()); |
|
|
|
|
}}; |
|
|
|
|
public static String downloadFileByUrl(String fileUrl, String downloadFileDir){ |
|
|
|
|
URL url; |
|
|
|
|
String fileName = null; |
|
|
|
|
try { |
|
|
|
|
url = new URL(fileUrl); |
|
|
|
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); |
|
|
|
|
connection.setConnectTimeout(5000); |
|
|
|
|
connection.setReadTimeout(5000); |
|
|
|
|
connection.connect(); |
|
|
|
|
int responseCode = connection.getResponseCode(); |
|
|
|
|
if (responseCode == 200) { |
|
|
|
|
InputStream inputStream = connection.getInputStream(); |
|
|
|
|
int lastSlashIndex = fileUrl.lastIndexOf("/"); |
|
|
|
|
if (lastSlashIndex > 0) { |
|
|
|
|
fileName = fileUrl.substring(lastSlashIndex+1); |
|
|
|
|
String filePath = downloadFileDir + fileName; |
|
|
|
|
File file = new File(filePath); |
|
|
|
|
if (file.exists()){ |
|
|
|
|
file.delete(); |
|
|
|
|
} |
|
|
|
|
OutputStream outputStream = Files.newOutputStream(file.toPath()); |
|
|
|
|
// 将文件流拷贝到本地处理
|
|
|
|
|
IOUtils.copy(inputStream, outputStream); |
|
|
|
|
} else { |
|
|
|
|
throw new ServiceException("下载文件路径异常:" + downloadFileDir); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new ServiceException("文件图片下载失败!"); |
|
|
|
|
} |
|
|
|
|
return fileName; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|