mirror of
https://gitee.com/freshday/radar.git
synced 2025-12-26 07:16:26 +08:00
commit
2df2a8eda4
@ -44,6 +44,11 @@
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
|
||||
<dependency>
|
||||
|
||||
@ -1,19 +1,30 @@
|
||||
package com.pgmmers.radar.controller;
|
||||
|
||||
|
||||
import com.pgmmers.radar.dal.bean.DataListQuery;
|
||||
import com.pgmmers.radar.service.common.CommonResult;
|
||||
import com.pgmmers.radar.service.model.DataListsService;
|
||||
import com.pgmmers.radar.util.ExcelImportUtil;
|
||||
import com.pgmmers.radar.util.ExportExcelInfo;
|
||||
import com.pgmmers.radar.vo.model.DataListsVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/services/v1/datalist")
|
||||
@Api(value = "DataListsApi", description = "黑白名单列表接口相关操作", tags = {"数据列表API"})
|
||||
public class DataListApiController {
|
||||
|
||||
public static Logger logger = LoggerFactory.getLogger(DataListApiController.class);
|
||||
|
||||
@Autowired
|
||||
private DataListsService dataListsService;
|
||||
|
||||
@ -49,4 +60,47 @@ public class DataListApiController {
|
||||
return dataListsService.delete(id);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 批量导入黑/白名单管理
|
||||
* @param file 文件
|
||||
* @param modelId 模型ID
|
||||
* @return
|
||||
* @author xushuai
|
||||
*/
|
||||
@PostMapping(value = "/batchImportData")
|
||||
public CommonResult batchImportData(@ApiParam(value = "file detail") @RequestPart("file") MultipartFile file, @RequestParam(value = "modelId", required = true)Long modelId) {
|
||||
CommonResult result = new CommonResult();
|
||||
result.setSuccess(false);
|
||||
String fileName = file.getOriginalFilename();
|
||||
if (fileName != null && !(fileName.contains(".xls") || fileName.contains(".xlsx"))) {
|
||||
result.setMsg("传入的件格式有误!");
|
||||
return result;
|
||||
}
|
||||
ExportExcelInfo<DataListsVO> info = getImportMeta();
|
||||
List<Map<String, Object>> listError = new ArrayList<>();
|
||||
List<DataListsVO> list = null;
|
||||
try {
|
||||
list = ExcelImportUtil.excelToList(file.getInputStream(), info, listError, DataListsVO.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("导入Excel失败:" + e.getMessage());
|
||||
}
|
||||
if (list == null || list.size() == 0) {
|
||||
result.setMsg("无导入数据!");
|
||||
return result;
|
||||
}
|
||||
if (list.size() > 1000) {
|
||||
result.setMsg("最大导入不能超过" + 1000 + "条");
|
||||
return result;
|
||||
}
|
||||
return dataListsService.batchImportData(list, modelId);
|
||||
}
|
||||
|
||||
private ExportExcelInfo<DataListsVO> getImportMeta() {
|
||||
ExportExcelInfo<DataListsVO> info = new ExportExcelInfo<DataListsVO>(null);
|
||||
info.addExcelColumn("备注", "comment");
|
||||
info.addExcelColumn("列表名", "label");
|
||||
info.addExcelColumn("名单类型", "listType");
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 优化参数名称
|
||||
* @author xushuai
|
||||
*/
|
||||
public class CamelUtil {
|
||||
public static final char UNDERLINE = '_';
|
||||
|
||||
public static String camelToUnderline(String param) {
|
||||
if (param == null || "".equals(param.trim())) {
|
||||
return "";
|
||||
}
|
||||
int len = param.length();
|
||||
StringBuilder sb = new StringBuilder(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = param.charAt(i);
|
||||
if (Character.isUpperCase(c)) {
|
||||
sb.append(UNDERLINE);
|
||||
sb.append(Character.toLowerCase(c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String underlineToCamel(String param) {
|
||||
if (param == null || "".equals(param.trim())) {
|
||||
return "";
|
||||
}
|
||||
int len = param.length();
|
||||
StringBuilder sb = new StringBuilder(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = param.charAt(i);
|
||||
if (c == UNDERLINE) {
|
||||
if (++i < len) {
|
||||
sb.append(Character.toUpperCase(param.charAt(i)));
|
||||
}
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String underlineToCamel2(String param) {
|
||||
if (param == null || "".equals(param.trim())) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(param);
|
||||
Matcher mc = Pattern.compile("_").matcher(param);
|
||||
int i = 0;
|
||||
while (mc.find()) {
|
||||
int position = mc.end() - (i++);
|
||||
//String.valueOf(Character.toUpperCase(sb.charAt(position)));
|
||||
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
/**
|
||||
* Excel字段类型
|
||||
* @author xushuai
|
||||
*/
|
||||
public enum EnumExcelColumnType {
|
||||
ColumnType_Double(0),
|
||||
ColumnType_Date(1),
|
||||
ColumnType_Calendar(2),
|
||||
ColumnType_String(3),
|
||||
ColumnType__BOOLEAN(4),
|
||||
ColumnType_ERROR(5);
|
||||
private int RowId;
|
||||
private EnumExcelColumnType(int id) {
|
||||
RowId = id;
|
||||
}
|
||||
public int GetValue() {
|
||||
return RowId;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,128 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* 列信息整理
|
||||
* @author xushuai
|
||||
*/
|
||||
public class ExcelColumn<T> {
|
||||
String text;
|
||||
String tableName;
|
||||
String columnName;
|
||||
String camelColumnName;
|
||||
EnumExcelColumnType columnType;
|
||||
boolean isNull=true;
|
||||
int orderIndex;
|
||||
FunctionFormatter<Object, T, Integer, Object> formatter;
|
||||
double columnWidth;
|
||||
public ExcelColumn()
|
||||
{
|
||||
|
||||
}
|
||||
public ExcelColumn(String columnName, String tableName, String text)
|
||||
{
|
||||
this.columnName=columnName;
|
||||
this.camelColumnName= CamelUtil.underlineToCamel(this.columnName);
|
||||
this.tableName=tableName;
|
||||
this.text=text;
|
||||
}
|
||||
public ExcelColumn(String columnName, String tableName, String text, boolean isNull) {
|
||||
this(columnName, tableName, text);
|
||||
this.isNull = isNull;
|
||||
}
|
||||
public ExcelColumn(String columnName, int orderIndex, String tableName, String text)
|
||||
{
|
||||
this.columnName=columnName;
|
||||
this.camelColumnName= CamelUtil.underlineToCamel(this.columnName);
|
||||
this.orderIndex=orderIndex;
|
||||
this.tableName=tableName;
|
||||
this.text=text;
|
||||
}
|
||||
public ExcelColumn(String columnName, int orderIndex, String tableName, String text, boolean isNull)
|
||||
{
|
||||
this(columnName,orderIndex,tableName,text);
|
||||
this.isNull=isNull;
|
||||
}
|
||||
public ExcelColumn(String columnName, int orderIndex, String tableName, String text, EnumExcelColumnType columnType, boolean isNull)
|
||||
{
|
||||
this(columnName,orderIndex,tableName,text);
|
||||
this.columnType=columnType;
|
||||
this.isNull=isNull;
|
||||
}
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
private String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public void setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
}
|
||||
|
||||
public String getCamelColumnName() {
|
||||
if(StringUtils.isEmpty(camelColumnName))
|
||||
{
|
||||
return this.getColumnName();
|
||||
}
|
||||
return camelColumnName;
|
||||
}
|
||||
|
||||
public void setCamelColumnName(String camelColumnName) {
|
||||
this.camelColumnName = camelColumnName;
|
||||
}
|
||||
|
||||
public EnumExcelColumnType getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
|
||||
public void setColumnType(EnumExcelColumnType columnType) {
|
||||
this.columnType = columnType;
|
||||
}
|
||||
|
||||
public boolean isNull() {
|
||||
return isNull;
|
||||
}
|
||||
|
||||
public void setNull(boolean aNull) {
|
||||
isNull = aNull;
|
||||
}
|
||||
|
||||
public int getOrderIndex() {
|
||||
return orderIndex;
|
||||
}
|
||||
|
||||
public void setOrderIndex(int orderIndex) {
|
||||
this.orderIndex = orderIndex;
|
||||
}
|
||||
|
||||
public FunctionFormatter<Object, T, Integer, Object> getFormatter() {
|
||||
return formatter;
|
||||
}
|
||||
|
||||
public void setFormatter(FunctionFormatter<Object, T, Integer, Object> formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public double getColumnWidth() {
|
||||
return columnWidth;
|
||||
}
|
||||
|
||||
public void setColumnWidth(double columnWidth) {
|
||||
this.columnWidth = columnWidth;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,198 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 将导入的文件列信息转换为model
|
||||
* @author xushuai
|
||||
*/
|
||||
public class ExcelImportUtil {
|
||||
|
||||
public static <TModel> List<TModel> excelToList(
|
||||
HttpServletRequest request,
|
||||
ExportExcelInfo<TModel> info,List<Map<String, Object>> listErrorMap,Class<TModel> entityClass
|
||||
) throws Exception {
|
||||
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
|
||||
String fileName= multiRequest.getFileNames().next();
|
||||
|
||||
MultipartFile multipartFile=multiRequest.getFile(fileName);
|
||||
// MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
|
||||
// MultipartFile File= multiRequest.getMultiFileMap().;
|
||||
// request.get
|
||||
//
|
||||
// multiRequest.getMultiFileMap().f
|
||||
//取得上传文件流
|
||||
InputStream in =multipartFile.getInputStream() ;
|
||||
return excelToList(in, info, listErrorMap, entityClass);
|
||||
}
|
||||
|
||||
public static <TModel> List<TModel> excelToList(
|
||||
InputStream in,
|
||||
ExportExcelInfo<TModel> info,List<Map<String, Object>> listErrorMap,Class<TModel> entityClass
|
||||
) throws Exception {
|
||||
//定义要返回的list
|
||||
List<TModel> resultList = new ArrayList<TModel>();
|
||||
String sheetName = info.getSheet();
|
||||
//根据Excel数据源创建WorkBook
|
||||
Workbook wb = WorkbookFactory.create(in);
|
||||
// HSSFWorkbook wb = new HSSFWorkbook(in);
|
||||
//获取工作表
|
||||
Sheet sheet;
|
||||
if (wb.getNumberOfSheets() > 1 && !sheetName.equals("")) {
|
||||
sheet = wb.getSheet(sheetName);
|
||||
} else {
|
||||
sheet = wb.getSheetAt(0);
|
||||
}
|
||||
importSheet(sheet, info, resultList, listErrorMap, entityClass);
|
||||
return resultList;
|
||||
}
|
||||
public static <TModel> void importSheet(Sheet productSheet, ExportExcelInfo<TModel> info, List<TModel> listTModel, List<Map<String, Object>> listErrorMap, Class<TModel> entityClass) throws Exception {
|
||||
importSheet(productSheet, info, listTModel, listErrorMap, entityClass,0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param productSheet listTModel
|
||||
* @param listTModel 导入集合
|
||||
* @param listErrorMap 导入错误集合
|
||||
* @throws Exception
|
||||
*/
|
||||
public static <TModel> void importSheet(Sheet productSheet, ExportExcelInfo<TModel> info, List<TModel> listTModel, List<Map<String, Object>> listErrorMap, Class<TModel> entityClass, int columnRowIndex) throws Exception {
|
||||
int LastCellNum = productSheet.getRow(0).getLastCellNum();//列数量
|
||||
Row rowColumn = productSheet.getRow(columnRowIndex);//excel列所在行
|
||||
Map<String, Integer> mapExcelColumn = new HashMap<>();//excel 列名字和列索引对应 key:excel列名字 value:excel列索引
|
||||
for (int i = 0; i < LastCellNum; i++) {
|
||||
mapExcelColumn.put(ExcelUtils.getString(rowColumn, i), i);
|
||||
}
|
||||
Map<String, Field> mapFiled = getMapFiled(entityClass);
|
||||
//TModel 所有属性字段Field key: 字段名
|
||||
int LastRowNum = productSheet.getLastRowNum();//excel最后一行
|
||||
TModel model;
|
||||
String errorMsg=null;
|
||||
for (int i = columnRowIndex + 1; i <= LastRowNum; i++) {
|
||||
Row row = productSheet.getRow(i);//获取行
|
||||
|
||||
model = entityClass.newInstance();
|
||||
if(row!=null) {
|
||||
errorMsg = rowToModel(mapExcelColumn, info, row, model, mapFiled);//行转model
|
||||
} else {errorMsg=null;}
|
||||
if (!StringUtils.isEmpty(errorMsg)) {//转换失败 保存错误行
|
||||
Map<String, Object> errorMap = getErrorMap(row, mapExcelColumn);//row转map
|
||||
errorMap.put("errorMsg", errorMsg);
|
||||
listErrorMap.add(errorMap);//加入转换错误行集合
|
||||
} else {//转换成功
|
||||
listTModel.add(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static <TModel> Map<String, Field> getMapFiled(Class<TModel> entityClass) {
|
||||
Map<String, Field> mapFiled = new HashMap<>();
|
||||
List<Field> listField = ReflectUtil.getListField(entityClass);
|
||||
for (Field field : listField) {
|
||||
mapFiled.put(field.getName(), field);
|
||||
}
|
||||
return mapFiled;
|
||||
}
|
||||
/**
|
||||
* 行转model
|
||||
*/
|
||||
static <TModel> String rowToModel(Map<String, Integer> mapExcelColumn, ExportExcelInfo<TModel> info, Row row, TModel model, Map<String, Field> mapFiled) throws Exception {
|
||||
String errorMsg = "";
|
||||
boolean hasValue = false;
|
||||
for (ExcelColumn column : info.getListColumn()) {
|
||||
if (mapExcelColumn.containsKey(column.getText())) {
|
||||
if (!mapFiled.containsKey(column.getCamelColumnName())) {
|
||||
throw new Exception(model.getClass().getName() + "不存在字段" + column.getCamelColumnName());
|
||||
}
|
||||
Field field = mapFiled.get(column.getCamelColumnName());
|
||||
String content = getString(row.getCell(mapExcelColumn.get(column.getText())));
|
||||
if (!column.isNull()) {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
errorMsg += column.getCamelColumnName() + "不能为空";
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!StringUtils.isEmpty(content)) {
|
||||
hasValue = true;
|
||||
ReflectUtil.setFieldValueByName(field, content.trim(), model);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorMsg += column.getCamelColumnName() + ":" + e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!hasValue){
|
||||
errorMsg +="该表格中有空行";
|
||||
}
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
static Map<String, Object> getErrorMap(Row row, Map<String, Integer> mapExcelColumn) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (String key : mapExcelColumn.keySet()) {
|
||||
Cell cell = row.getCell(mapExcelColumn.get(key));
|
||||
map.put(key, getString(cell));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
static String getString(Cell cell) {
|
||||
String result = "";
|
||||
if (cell == null) return result;
|
||||
switch (cell.getCellType()) {
|
||||
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
|
||||
//1、判断是否是数值格式
|
||||
if (HSSFDateUtil.isCellDateFormatted(cell)) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
double value = cell.getNumericCellValue();
|
||||
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
|
||||
result = sdf.format(date);
|
||||
} else {
|
||||
Double value = cell.getNumericCellValue();
|
||||
DecimalFormat format = new DecimalFormat();
|
||||
format.applyPattern("###################.###################");
|
||||
result = format.format(value);
|
||||
}
|
||||
break;
|
||||
case HSSFCell.CELL_TYPE_STRING:// String类型
|
||||
result = cell.getRichStringCellValue().toString();
|
||||
break;
|
||||
case HSSFCell.CELL_TYPE_FORMULA://公式型
|
||||
//读公式计算值
|
||||
try {
|
||||
|
||||
double value = cell.getNumericCellValue();
|
||||
if (Double.isNaN(value)) {//如果获取的数据值为非法值,则转换为获取字符串 //result.equals("NaN")
|
||||
result = cell.getRichStringCellValue().toString();
|
||||
} else {
|
||||
DecimalFormat format = new DecimalFormat();
|
||||
format.applyPattern("###################.###################");
|
||||
result = format.format(value);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
result = cell.getRichStringCellValue().toString();
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
case HSSFCell.CELL_TYPE_BLANK:
|
||||
result = "";
|
||||
break;
|
||||
default:
|
||||
result = "";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,113 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 导入Excel列信息
|
||||
* @author xushuai
|
||||
*/
|
||||
public class ExportExcelInfo<TRow> {
|
||||
|
||||
public ExportExcelInfo(List<TRow> dataSource) {
|
||||
this.setDataSource(dataSource);
|
||||
}
|
||||
|
||||
List<ExcelColumn<TRow>> listColumn = new ArrayList<ExcelColumn<TRow>>();
|
||||
String fileName;
|
||||
List<TRow> dataSource;
|
||||
String Sheet = "sheet1";
|
||||
|
||||
int columnType;//列的类型 0:text(文本) 1:columnName
|
||||
|
||||
public int getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
|
||||
public void setColumnType(int columnType) {
|
||||
this.columnType = columnType;
|
||||
}
|
||||
|
||||
boolean IsDisplayColumnName = false;
|
||||
|
||||
|
||||
public boolean isDisplayColumnName() {
|
||||
return IsDisplayColumnName;
|
||||
}
|
||||
|
||||
public void setDisplayColumnName(boolean displayColumnName) {
|
||||
IsDisplayColumnName = displayColumnName;
|
||||
}
|
||||
|
||||
public String getSheet() {
|
||||
return Sheet;
|
||||
}
|
||||
|
||||
public void setSheet(String sheet) {
|
||||
Sheet = sheet;
|
||||
}
|
||||
|
||||
public List<TRow> getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(List<TRow> dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public List<ExcelColumn<TRow>> getListColumn() {
|
||||
return listColumn;
|
||||
}
|
||||
|
||||
public void setListColumn(List<ExcelColumn<TRow>> listColumn) {
|
||||
this.listColumn = listColumn;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public void addExcelColumn(ExcelColumn column) {
|
||||
listColumn.add(column);
|
||||
}
|
||||
|
||||
public ExcelColumn<TRow> addExcelColumn(String text, String columnName) {
|
||||
ExcelColumn<TRow> column = new ExcelColumn<TRow>();
|
||||
column.setText(text);
|
||||
column.setColumnName(columnName);
|
||||
column.setColumnType(EnumExcelColumnType.ColumnType_String);
|
||||
this.getListColumn().add(column);
|
||||
return column;
|
||||
}
|
||||
|
||||
public ExcelColumn addExcelColumn(String text, String columnName, EnumExcelColumnType columnType) {
|
||||
ExcelColumn<TRow> column = addExcelColumn(text, columnName);
|
||||
column.setColumnType(columnType);
|
||||
return column;
|
||||
}
|
||||
|
||||
public ExcelColumn<TRow> addExcelColumn(String text, String columnName, FunctionFormatter<Object, TRow, Integer, Object> formatter) {
|
||||
ExcelColumn<TRow> column = addExcelColumn(text, columnName);
|
||||
column.setFormatter(formatter);
|
||||
return column;
|
||||
}
|
||||
|
||||
public ExcelColumn<TRow> addExcelColumn(String text, String columnName, FunctionFormatter<Object, TRow, Integer, Object> formatter, EnumExcelColumnType columnType) {
|
||||
ExcelColumn column = addExcelColumn(text, columnName, formatter);
|
||||
column.setColumnType(columnType);
|
||||
return column;
|
||||
}
|
||||
|
||||
public ExcelColumn<TRow> getErrorColumn() {
|
||||
ExcelColumn<TRow> column = new ExcelColumn<TRow>();
|
||||
column.setText("异常信息");
|
||||
column.setColumnName("errorMsg");//errorMsg
|
||||
column.setColumnType(EnumExcelColumnType.ColumnType_String);
|
||||
column.setCamelColumnName("errorMsg");
|
||||
return column;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
/**
|
||||
* @author xushuai
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface FunctionFormatter<TValue,TRow,TIndex,R> {
|
||||
R apply(TValue value, TRow row, TIndex index);
|
||||
}
|
||||
@ -0,0 +1,155 @@
|
||||
package com.pgmmers.radar.util;
|
||||
|
||||
import com.pgmmers.radar.dal.util.JsonUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 根据字段名获取字段
|
||||
* @author xushuai
|
||||
*/
|
||||
public class ReflectUtil {
|
||||
|
||||
public static void setFieldValueByName(Field field, Object fieldValue, Object o) throws Exception {
|
||||
field.setAccessible(true);
|
||||
//获取字段类型
|
||||
Class<?> fieldType = field.getType();
|
||||
//根据字段类型给字段赋值
|
||||
if (String.class == fieldType) {
|
||||
field.set(o, String.valueOf(fieldValue));
|
||||
} else if ((Integer.TYPE == fieldType)
|
||||
|| (Integer.class == fieldType)) {
|
||||
field.set(o, Integer.parseInt(fieldValue.toString()));
|
||||
} else if ((Long.TYPE == fieldType)
|
||||
|| (Long.class == fieldType)) {
|
||||
field.set(o, Long.valueOf(fieldValue.toString()));
|
||||
} else if ((Float.TYPE == fieldType)
|
||||
|| (Float.class == fieldType)) {
|
||||
field.set(o, Float.valueOf(fieldValue.toString()));
|
||||
} else if ((Short.TYPE == fieldType)
|
||||
|| (Short.class == fieldType)) {
|
||||
field.set(o, Short.valueOf(fieldValue.toString()));
|
||||
} else if ((Byte.TYPE == fieldType)
|
||||
|| (Byte.class == fieldType)) {
|
||||
field.set(o, Byte.valueOf(fieldValue.toString()));
|
||||
}
|
||||
else if ((Double.TYPE == fieldType)
|
||||
|| (Double.class == fieldType)) {
|
||||
field.set(o, Double.valueOf(fieldValue.toString()));
|
||||
} else if (Character.TYPE == fieldType) {
|
||||
if ((fieldValue != null) && (fieldValue.toString().length() > 0)) {
|
||||
field.set(o, fieldValue.toString().charAt(0));
|
||||
}
|
||||
} else if (BigDecimal.class == fieldType) {
|
||||
Long v1 = Long.valueOf(fieldValue.toString());
|
||||
field.set(o, BigDecimal.valueOf(v1));
|
||||
} else if (Date.class == fieldType) {
|
||||
field.set(o, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fieldValue.toString()));
|
||||
} else if (boolean.class == fieldType) {
|
||||
if ("1".equals(fieldValue)) {
|
||||
field.set(o, true);
|
||||
}
|
||||
if ("0".equals(fieldValue)) {
|
||||
field.set(o, false);
|
||||
}
|
||||
field.set(o, Boolean.valueOf(fieldValue.toString()));
|
||||
} else if (String[].class == fieldType) {
|
||||
String[] arr = JsonUtils.fromJson(fieldValue.toString(), String[].class);
|
||||
field.set(o, arr);
|
||||
} else {
|
||||
field.set(o, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Field> getListField(Class<?> clazz) {
|
||||
Field[] selfFields = clazz.getDeclaredFields();
|
||||
List<Field> list = new ArrayList<>(Arrays.asList(selfFields));
|
||||
Class<?> superClazz = clazz.getSuperclass();
|
||||
if (superClazz != null && superClazz != Object.class) {
|
||||
List<Field> listSuperField = getListField(superClazz);
|
||||
for (Field field : listSuperField) {
|
||||
list.add(field);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段名获取字段
|
||||
*
|
||||
* @param fieldName 字段名
|
||||
* @param clazz 包含该字段的类
|
||||
* @return 字段
|
||||
*/
|
||||
public static Field getFieldByName(String fieldName, Class<?> clazz) {
|
||||
//拿到本类的所有字段
|
||||
Field[] selfFields = clazz.getDeclaredFields();
|
||||
//如果本类中存在该字段,则返回
|
||||
for (Field field : selfFields) {
|
||||
if (field.getName().equals(fieldName)) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
//否则,查看父类中是否存在此字段,如果有则返回
|
||||
Class<?> superClazz = clazz.getSuperclass();
|
||||
if (superClazz != null && superClazz != Object.class) {
|
||||
return getFieldByName(fieldName, superClazz);
|
||||
}
|
||||
//如果本类和父类都没有,则返回空
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFieldValueByNameSequence
|
||||
*
|
||||
* @param fieldNameSequence 带路径的属性名或简单属性名
|
||||
* @param o 对象
|
||||
* @return 属性值
|
||||
* @throws Exception 根据带路径或不带路径的属性名获取属性值
|
||||
* 即接受简单属性名,如userName等,又接受带路径的属性名,如student.department.name等
|
||||
*/
|
||||
public static Object getFieldValueByNameSequence(String fieldNameSequence, Object o) throws Exception {
|
||||
Object value;
|
||||
//将fieldNameSequence进行拆分
|
||||
String[] attributes = fieldNameSequence.split("\\.");
|
||||
if (attributes.length == 1) {
|
||||
value = getFieldValueByName(fieldNameSequence, o);
|
||||
} else {
|
||||
//根据属性名获取属性对象
|
||||
Object fieldObj = getFieldValueByName(attributes[0], o);
|
||||
String subFieldNameSequence = fieldNameSequence.substring(fieldNameSequence.indexOf(".") + 1);
|
||||
value = getFieldValueByNameSequence(subFieldNameSequence, fieldObj);
|
||||
}
|
||||
return value;
|
||||
|
||||
}
|
||||
/*<-------------------------辅助的私有方法----------------------------------------------->*/
|
||||
|
||||
/**
|
||||
* 根据字段名获取字段值
|
||||
*
|
||||
* @param fieldName 字段名
|
||||
* @param o 对象
|
||||
* @return 字段值
|
||||
*/
|
||||
public static Object getFieldValueByName(String fieldName, Object o) throws Exception {
|
||||
Object value;
|
||||
Field field = getFieldByName(fieldName, o.getClass());
|
||||
if (field == null) {
|
||||
throw new Exception(o.getClass().getSimpleName() + "类不存在字段名 " + fieldName);
|
||||
}
|
||||
value = getValueByField(fieldName, o, field);
|
||||
return value;
|
||||
}
|
||||
|
||||
private static Object getValueByField(String fieldName, Object o, Field field) throws IllegalAccessException {
|
||||
field.setAccessible(true);
|
||||
return field.get(o);
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,8 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -112,4 +114,45 @@ public class ExcelUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 row 的第 index 列,获取字符串值
|
||||
*
|
||||
* @param row 目标行
|
||||
* @param index 目标列
|
||||
* @return 字符串值
|
||||
* @author xushuai
|
||||
*/
|
||||
public static String getString(Row row, int index) {
|
||||
return getString(row, index, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 row 的第 index 列,获取字符串值
|
||||
*
|
||||
* @param row 目标行
|
||||
* @param index 目标列
|
||||
* @param numberFormat 对原值为数字时的,数字格式化格式
|
||||
* @return 字符串值
|
||||
* @author xushuai
|
||||
*/
|
||||
public static String getString(Row row, int index, String numberFormat) {
|
||||
Cell cell = row.getCell(index);
|
||||
|
||||
if (cell == null) return null;
|
||||
|
||||
switch (cell.getCellType()) {
|
||||
case Cell.CELL_TYPE_NUMERIC:
|
||||
double val = cell.getNumericCellValue();
|
||||
if (numberFormat == null)
|
||||
return String.valueOf(val);
|
||||
NumberFormat formatter = new DecimalFormat(numberFormat);
|
||||
return formatter.format(val);
|
||||
case Cell.CELL_TYPE_STRING:
|
||||
return cell.getStringCellValue();
|
||||
case Cell.CELL_TYPE_FORMULA:
|
||||
return cell.getStringCellValue();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,62 @@
|
||||
package com.pgmmers.radar.dal.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonParser.Feature;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* json对象转换
|
||||
* @author xushuai
|
||||
*/
|
||||
public class JsonUtils {
|
||||
public JsonUtils() {
|
||||
}
|
||||
|
||||
public static String toJson(Object object) {
|
||||
try {
|
||||
return (new ObjectMapper()).setSerializationInclusion(Include.NON_NULL).writeValueAsString(object);
|
||||
} catch (JsonProcessingException var2) {
|
||||
throw new RuntimeException(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public static <E> E fromJson(String json, Class<E> type) {
|
||||
try {
|
||||
return (new ObjectMapper()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(json, type);
|
||||
} catch (IOException var3) {
|
||||
throw new RuntimeException(var3);
|
||||
}
|
||||
}
|
||||
|
||||
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
|
||||
return (new ObjectMapper()).getTypeFactory().constructParametricType(collectionClass, elementClasses);
|
||||
}
|
||||
|
||||
public static String toJsonString(Object object) {
|
||||
try {
|
||||
return (new ObjectMapper()).writeValueAsString(object);
|
||||
} catch (JsonProcessingException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Map writeJsonToMap(String json) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
Map map = null;
|
||||
|
||||
try {
|
||||
map = (Map)mapper.readValue(json, Map.class);
|
||||
return map;
|
||||
} catch (IOException var4) {
|
||||
throw new RuntimeException(var4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -279,4 +279,27 @@ public class DataListsServiceImpl implements DataListsService, SubscribeHandle {
|
||||
Map<String, Object> listMap = dataListRecordCacheMap.get(modelId);
|
||||
return listMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult batchImportData(List<DataListsVO> list, Long modelId) {
|
||||
CommonResult result = new CommonResult();
|
||||
for (DataListsVO data : list) {
|
||||
data.setStatus(1);
|
||||
data.setName("");
|
||||
data.setModelId(modelId);
|
||||
int count = dataListDal.save(data);
|
||||
if (count > 0) {
|
||||
if(StringUtils.isEmpty(data.getName())){
|
||||
data.setName("dataList_"+data.getId());
|
||||
dataListDal.save(data);
|
||||
}
|
||||
// 通知更新
|
||||
data.setOpt("new");
|
||||
cacheService.publishDataList(data);
|
||||
}
|
||||
}
|
||||
result.setSuccess(true);
|
||||
result.setMsg("导入成功");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,4 +48,5 @@ public interface DataListsService {
|
||||
|
||||
Map<String, Object> getDataListMap(Long modelId);
|
||||
|
||||
CommonResult batchImportData(List<DataListsVO> list, Long modelId);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user