master
wangbing 5 years ago
parent e0f06eb14f
commit 356331b1fc

@ -309,7 +309,7 @@ public class GlobalController implements ErrorController {
return baseResponse;
} catch (InvocationTargetException e) {
BaseResponse baseResponse = new BaseResponse();
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法执行错误!");
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法执行错误[" + e.getTargetException().getMessage() + "]");
return baseResponse;
}
}

@ -11,12 +11,14 @@ import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.ResponseUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.Dict;
import ${basePackage}.module.system.mgr.DictManager;
import ${basePackage}.module.system.req.*;
import ${basePackage}.module.system.rsp.*;
import java.io.IOException;
import java.util.List;
public class DictAjax {
@ -69,10 +71,16 @@ public class DictAjax {
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Dict> sheet = new WSheet<>(file.getBytes(), Dict.class);
WSheet<Dict> sheet = new WSheet<>(file.getBytes(), Dict.class, new WSheet.Validator<Dict>() {
@Override
public List<String> validate(Dict o) {
DictCreateRequest request = MapperUtil.map(o, DictCreateRequest.class);
return ValidationUtil.validate(request);
}
});
if (sheet.hasError()) {
return ResponseUtil.apply(sheet.getBytes(), sheet.getName() + "-err.xlsx");
return ResponseUtil.apply(sheet.getBytes(true), sheet.getName() + "-err.xlsx");
} else {
return baseResponse;
}

@ -11,9 +11,13 @@ import java.util.Date;
* @version 0.0.1
* @since 2017-01-01
*/
public class BaseEntity implements Serializable {
/**
*
*/
private long id;
/**
*
*/
@ -48,14 +52,6 @@ public class BaseEntity implements Serializable {
@JsonIgnore
private boolean isDeleted;
public boolean getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}
public long getRowVersion() {
return rowVersion;
}
@ -64,20 +60,20 @@ public class BaseEntity implements Serializable {
this.rowVersion = rowVersion;
}
public long getCreateBy() {
return createBy;
public long getId() {
return id;
}
public void setCreateBy(long createBy) {
this.createBy = createBy;
public void setId(long id) {
this.id = id;
}
public long getLastUpdateBy() {
return lastUpdateBy;
public long getCreateBy() {
return createBy;
}
public void setLastUpdateBy(long lastUpdateBy) {
this.lastUpdateBy = lastUpdateBy;
public void setCreateBy(long createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
@ -88,6 +84,14 @@ public class BaseEntity implements Serializable {
this.createTime = createTime;
}
public long getLastUpdateBy() {
return lastUpdateBy;
}
public void setLastUpdateBy(long lastUpdateBy) {
this.lastUpdateBy = lastUpdateBy;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
@ -95,4 +99,12 @@ public class BaseEntity implements Serializable {
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
}

@ -6,6 +6,7 @@ import ${basePackage}.frame.utils.StringUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* WColumn - Excel()
@ -36,7 +37,14 @@ public class WColumn implements Serializable {
*/
private Converter converter;
/**
* Field
*/
private Field field;
/**
* set
*/
private Method setMethod;
private int cellType = 1;
@ -112,4 +120,12 @@ public class WColumn implements Serializable {
public void setCellType(int cellType) {
this.cellType = cellType;
}
public Method getSetMethod() {
return setMethod;
}
public void setSetMethod(Method setMethod) {
this.setMethod = setMethod;
}
}

@ -1,12 +1,13 @@
package ${basePackage}.frame.excel;
import ${basePackage}.frame.excel.converter.Converter;
import ${basePackage}.frame.excel.exception.ValueConverterException;
import ${basePackage}.frame.excel.style.DataCellStyle;
import ${basePackage}.frame.excel.style.ErrorCellStyle;
import ${basePackage}.frame.excel.style.HeadCellStyle;
import ${basePackage}.frame.excel.style.RedFont;
import ${basePackage}.frame.utils.ClassUtil;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.utils.StringUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.frame.excel.annotation.*;
import ${basePackage}.frame.excel.converter.*;
@ -64,7 +65,7 @@ public class WSheet<T> implements Serializable, Cloneable {
/**
* DataTable
*
* @return @NameSet
* @return WColumn
*/
private List<WColumn> initColumns(Class<?> clazz) {
//获取工作簿名称,没有则以类名为默认工作簿名称
@ -87,6 +88,9 @@ public class WSheet<T> implements Serializable, Cloneable {
WColumn WColumn = new WColumn();
WColumn.setField(field);
Method set = ClassUtil.setMethod(field.getName(), clazz, field.getType());
WColumn.setSetMethod(set);
//获取列名称
if (!field.isAnnotationPresent(ColumnName.class)) {
WColumn.setName(field.getName());
@ -110,7 +114,7 @@ public class WSheet<T> implements Serializable, Cloneable {
${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class);
Class target = converter.target();
try {
WColumn.setConverter((${basePackage}.frame.excel.converter.Converter) target.newInstance());
WColumn.setConverter((Converter) target.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
@ -162,9 +166,6 @@ public class WSheet<T> implements Serializable, Cloneable {
*
*
* @param list
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public WSheet(List<T> list) {
if (list == null || list.size() == 0) {
@ -180,19 +181,7 @@ public class WSheet<T> implements Serializable, Cloneable {
continue;
}
Field field = column.getField();
String att = StringUtil.upperFirstWord(field.getName());
Method method = null;
try {
// 尝试获取get方法
method = t.getClass().getMethod("get" + att);
} catch (NoSuchMethodException e) {
// 尝试获取is方法工具生成布尔值可能是is而不是get
try {
method = t.getClass().getMethod("is" + att);
} catch (NoSuchMethodException e1) {
LogUtil.w(field.getName()+"can not find get method");
}
}
Method method = ClassUtil.setMethod(field.getName(), t.getClass(), field.getType());
try {
Object value = method.invoke(t);
if (null == value) {
@ -202,7 +191,7 @@ public class WSheet<T> implements Serializable, Cloneable {
row.put(column.getName(), new WCell(string));
}
} catch (IllegalAccessException e) {
LogUtil.w( "can not invoke get method!");
LogUtil.w("can not invoke get method!");
} catch (InvocationTargetException e) {
LogUtil.w(method.getName() + " unexpected exception!");
}
@ -315,17 +304,17 @@ public class WSheet<T> implements Serializable, Cloneable {
}
try {
T t = transferOneObject(clazz, i);
List<String> validate = ValidationUtil.validate(t);
validate.addAll(validator != null ? validator.validate(t) : null);
if (validate.size() > 0) {
for (String s : validate) {
T t = clazz.newInstance();
List<String> errs = transferMap(row, t);
errs.addAll(ValidationUtil.validate(t));
errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList());
if (errs.size() > 0) {
for (String s : errs) {
row.addError(s);
}
}
} catch (Exception e) {
e.printStackTrace();
row.addError("数据检查错误");
} catch (InstantiationException | IllegalAccessException e) {
row.addError("模板对象默认构造函数错误");
}
}
}
@ -333,67 +322,38 @@ public class WSheet<T> implements Serializable, Cloneable {
/**
*
*
* @param clazz
* @param rowIndex
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @param row
* @param target
* @return
*/
public T transferOneObject(Class<T> clazz, int rowIndex) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
T object = clazz.newInstance();
ParentFirst parentFirstAnnotation = clazz.getAnnotation(ParentFirst.class);
boolean parentFirst = parentFirstAnnotation != null && parentFirstAnnotation.value();
Field[] fields = ClassUtil.getFields(clazz, parentFirst);
Set<Field> set = new HashSet<>();
for (Field field : fields) {
set.add(field);
}
for (int j = 0; j < this.columnList.size(); j++) {
String key = this.columnList.get(j).getName();
public List<String> transferMap(WRow row, T target) {
List<String> err = new ArrayList<>();
for (String key : row.keySet()) {
for (WColumn column : columnList) {
Field field = column.getField();
ColumnName fieldColumnName = field.getAnnotation(ColumnName.class);
if (key.equals(fieldColumnName.value())) {
String name = column.getName();
Method setMethod = column.getSetMethod();
String att = StringUtil.upperFirstWord(field.getName());
WCell WCell = this.rowList.get(rowIndex).get(column.getName());
if (key.equals(name)) {
WCell WCell = row.get(column.getName());
if (null != WCell) {
String value = WCell.getValue();
Method method = clazz.getMethod("set" + att, field.getType());
//获取转换器
${basePackage}.frame.excel.converter.Converter converter = column.getConverter();
method.invoke(object, converter.convert(value));
Converter converter = column.getConverter();
try {
setMethod.invoke(target, converter.convert(value));
} catch (ValueConverterException e) {
e.printStackTrace();
err.add(e.getMessage());
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
err.add("[" + setMethod.getName() + "]执行错误");
}
}
break;
}
}
}
return object;
}
/**
* DataTableTList<br/><br/>
* ,@Name<br/>
* DataTable<br/>
*
*
* @return T
* @see WColumn
*/
public List<T> transferList(Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
List<T> list = new ArrayList<>();
for (int i = 0; i < this.rowList.size(); i++) {
T object = transferOneObject(clazz, i);
list.add(object);
}
return list;
return err;
}
/**
@ -508,7 +468,6 @@ public class WSheet<T> implements Serializable, Cloneable {
for (int j = 0; j < this.columnList.size(); j++) {
WColumn column = this.columnList.get(j);
Field field = column.getField();
Cell firstCell = firstRow.createCell(j + offset);
String columnName = column.getName();
XSSFRichTextString textString;
@ -522,13 +481,6 @@ public class WSheet<T> implements Serializable, Cloneable {
}
StringBuilder sb = new StringBuilder();
sb.append(column.getDescription()).append("\n");
// 添加数据说明信息。
if (field.isAnnotationPresent(ColumnDescription.class)) {
// 获取声明字段上的Description信息。
ColumnDescription columnDescription = field.getAnnotation(ColumnDescription.class);
sb.append(columnDescription.value());
sb.append("\n");
}
// 如果填写了注释信息
if (sb.length() > 1) {

@ -1,5 +1,7 @@
package ${basePackage}.frame.excel.converter;
import ${basePackage}.frame.excel.exception.ValueConverterException;
/**
* BooleanConverter - BooleanStringString
*
@ -10,12 +12,28 @@ package ${basePackage}.frame.excel.converter;
public class BooleanConverter implements Converter<Boolean> {
@Override
public Boolean convert(String var) {
try {
return Boolean.parseBoolean(var);
} catch (Exception e) {
public Boolean convert(String var) throws ValueConverterException {
if (null == var || "".equals(var)) {
return false;
}
String lowerCase = var.toLowerCase();
if (lowerCase.matches("y|n")) {
return "y".equals(lowerCase);
} else if (lowerCase.matches("yes|no")) {
return "yes".equals(lowerCase);
} else if (lowerCase.matches("true|false")) {
return "true".equals(lowerCase);
} else if (lowerCase.matches("是|否")) {
return "是".equals(lowerCase);
} else if (lowerCase.matches("有|无")) {
return "有".equals(lowerCase);
} else if (lowerCase.matches("是|不是")) {
return "是".equals(lowerCase);
} else {
throw new ValueConverterException("[" + var + "] can not convert to Boolean");
}
}
@Override
@ -23,6 +41,6 @@ public class BooleanConverter implements Converter<Boolean> {
if (var == null) {
return "";
}
return String.valueOf(var);
return var ? "Y" : "N";
}
}
}

@ -1,5 +1,7 @@
package ${basePackage}.frame.excel.converter;
import ${basePackage}.frame.excel.exception.ValueConverterException;
/**
* Converter -
*
@ -9,7 +11,7 @@ package ${basePackage}.frame.excel.converter;
*/
public interface Converter<T> {
T convert(String var);
T convert(String var) throws ValueConverterException;
String string(T var);
}

@ -0,0 +1,15 @@
package xyz.wbsite.frame.excel.exception;
/**
*
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class ValueConverterException extends Exception {
public ValueConverterException(String s) {
super(s);
}
}

@ -1,8 +1,8 @@
package ${basePackage}.frame.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* ClassUtil
@ -30,25 +30,6 @@ public class ClassUtil {
return fields;
}
/**
* class
*
* @param clazz
* @return
*/
public static Field[] getClassFields(Class<?> clazz) {
List<Field> list = new ArrayList<Field>();
Field[] fields;
do {
fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
list.add(fields[i]);
}
clazz = clazz.getSuperclass();
} while (clazz != Object.class && clazz != null);
return list.toArray(fields);
}
/**
*
*
@ -62,40 +43,38 @@ public class ClassUtil {
/**
* GET
*
* @param name
* @param pojoClass
* @return
* @throws Exception
* @param name
* @param pojoClass POJO
* @return
*/
public static Method getMethod(String name, Class<?> pojoClass) throws Exception {
StringBuffer getMethodName = new StringBuffer("get");
getMethodName.append(name.substring(0, 1).toUpperCase());
getMethodName.append(name.substring(1));
Method method = null;
public static Method getMethod(String name, Class<?> pojoClass) throws RuntimeException {
String getMethodName = "get" + StringUtil.upperFirstWord(name);
try {
method = pojoClass.getMethod(getMethodName.toString());
return pojoClass.getMethod(getMethodName);
} catch (Exception e) {
method = pojoClass.getMethod(
getMethodName.toString().replace("get", "is")
);
getMethodName = "is" + StringUtil.upperFirstWord(name);
try {
return pojoClass.getMethod(getMethodName);
} catch (NoSuchMethodException e1) {
throw new RuntimeException("can not find[" + name + "]method");
}
}
return method;
}
/**
* SET
*
* @param name
* @param pojoClass
* @param type
* @return
* @throws Exception
* @param name
* @param pojoClass POJO
* @return
*/
public static Method getMethod(String name, Class<?> pojoClass, Class<?> type) throws Exception {
StringBuffer getMethodName = new StringBuffer("set");
getMethodName.append(name.substring(0, 1).toUpperCase());
getMethodName.append(name.substring(1));
return pojoClass.getMethod(getMethodName.toString(), type);
public static Method setMethod(String name, Class<?> pojoClass, Class<?> type) {
String setMethodName = "set" + StringUtil.upperFirstWord(name);
try {
return pojoClass.getMethod(setMethodName, type);
} catch (Exception e) {
return null;
}
}
public static boolean isJavaClass(Field field) {

@ -15,7 +15,7 @@ import ${basePackage}.frame.base.BaseEntity;
public class ${table.getCName()} extends BaseEntity {
<#list table.fields as field>
<#if !field.isSystem || field.fieldName == 'ID'>
<#if !field.isSystem>
/**
* ${field.fieldName} - ${field.fieldComment?default("")}
*/
@ -23,7 +23,7 @@ public class ${table.getCName()} extends BaseEntity {
</#if>
</#list>
<#list table.fields as field>
<#if !field.isSystem || field.fieldName == 'ID' >
<#if !field.isSystem>
public ${field.fieldType.javaType()} ${field.getterName()}() {
return this.${field.getFName()};

@ -1,6 +1,9 @@
package ${basePackage}.module.system.ent;
import ${basePackage}.frame.base.BaseEntity;
import ${basePackage}.frame.excel.annotation.ColumnDescription;
import ${basePackage}.frame.excel.annotation.ColumnName;
import ${basePackage}.frame.excel.annotation.SheetName;
/**
* DICT -
@ -9,37 +12,34 @@ import ${basePackage}.frame.base.BaseEntity;
* @version 0.0.1
* @since 2019-07-20
*/
@SheetName("字典")
public class Dict extends BaseEntity {
/**
* ID -
*/
private Long id;
/**
* DICT_NAME -
*/
@ColumnName("字典名称")
@ColumnDescription("1-50长度的字典名称")
private String dictName;
/**
* DICT_CODE -
*/
@ColumnName("字典代码")
@ColumnDescription("1-50长度的字典代码")
private String dictCode;
/**
* VERSION -
*/
@ColumnName("字典版本号")
@ColumnDescription("字典版本号.例如:yyyy-MM-dd HH:mm:ss")
private String version;
/**
* VALID -
*/
@ColumnName("是否有效")
@ColumnDescription("true/false")
private Boolean valid;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getDictName() {
return this.dictName;
}

@ -11,10 +11,6 @@ import ${basePackage}.frame.base.BaseEntity;
*/
public class DictItem extends BaseEntity {
/**
* ID -
*/
private Long id;
/**
* DICT_ID - ID
*/
@ -36,14 +32,6 @@ public class DictItem extends BaseEntity {
*/
private Boolean valid;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Long getDictId() {
return this.dictId;
}

@ -2,9 +2,6 @@ package ${basePackage}.module.system.req;
import org.hibernate.validator.constraints.Length;
import ${basePackage}.frame.base.BaseRequest;
import ${basePackage}.frame.excel.annotation.ColumnDescription;
import ${basePackage}.frame.excel.annotation.ColumnName;
import ${basePackage}.frame.excel.annotation.SheetName;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@ -15,14 +12,11 @@ import javax.validation.constraints.NotNull;
* @version 0.0.1
* @since 2019-07-20
*/
@SheetName("字典")
public class DictCreateRequest extends BaseRequest {
/**
*
*/
@ColumnName("字典名称")
@ColumnDescription("1-50长度的字典名称")
@NotEmpty(message = "字典名称不能为空")
@Length(min = 1, max = 50, message = "字典名称长度不合法(1-50)")
private String dictName;
@ -30,8 +24,6 @@ public class DictCreateRequest extends BaseRequest {
/**
*
*/
@ColumnName("字典代码")
@ColumnDescription("1-50长度的字典代码")
@NotEmpty(message = "字典代码不能为空")
@Length(min = 1, max = 50, message = "字典代码长度不合法(1-50)")
private String dictCode;
@ -39,8 +31,6 @@ public class DictCreateRequest extends BaseRequest {
/**
*
*/
@ColumnName("字典版本号")
@ColumnDescription("字典版本号.例如:yyyy-MM-dd HH:mm:ss")
@NotEmpty(message = "字典版本号不能为空")
@Length(min = 1, max = 50, message = "字典版本号长度不合法(1-50)")
private String version;
@ -48,8 +38,6 @@ public class DictCreateRequest extends BaseRequest {
/**
*
*/
@ColumnName("是否有效")
@ColumnDescription("true/false")
@NotNull(message = "是否有效不能为NULL")
private Boolean valid;

@ -413,6 +413,7 @@
onImport: function (item) {
const file = item.target.files[0];
this.$ajax.imports(this.module, this.target, file);
this.$refs['excel'].value = ''
},
onExport: function () {
this.$ajax.exports(this.module, this.target, this.vm);

Loading…
Cancel
Save

Powered by TurnKey Linux.