Pre Merge pull request !356 from panxiaoyang/readConverterEnum
This commit is contained in:
commit
006e2f575a
|
|
@ -2,6 +2,7 @@ package com.ruoyi.system.api.domain;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
import com.ruoyi.system.api.emun.EnableStatusEnum;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.core.annotation.Excel;
|
import com.ruoyi.common.core.annotation.Excel;
|
||||||
|
|
@ -11,7 +12,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典数据表 sys_dict_data
|
* 字典数据表 sys_dict_data
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class SysDictData extends BaseEntity
|
public class SysDictData extends BaseEntity
|
||||||
|
|
@ -48,8 +49,11 @@ public class SysDictData extends BaseEntity
|
||||||
@Excel(name = "是否默认", readConverterExp = "Y=是,N=否")
|
@Excel(name = "是否默认", readConverterExp = "Y=是,N=否")
|
||||||
private String isDefault;
|
private String isDefault;
|
||||||
|
|
||||||
/** 状态(0正常 1停用) */
|
/**
|
||||||
@Excel(name = "状态", readConverterExp = "0=正常,1=停用")
|
* 状态(0正常 1停用)
|
||||||
|
* @see EnableStatusEnum
|
||||||
|
**/
|
||||||
|
@Excel(name = "状态", readConverterEnum = EnableStatusEnum.class)
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
public Long getDictCode()
|
public Long getDictCode()
|
||||||
|
|
@ -153,7 +157,7 @@ public class SysDictData extends BaseEntity
|
||||||
{
|
{
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.ruoyi.system.api.domain;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
import com.ruoyi.system.api.emun.EnableStatusEnum;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.core.annotation.Excel;
|
import com.ruoyi.common.core.annotation.Excel;
|
||||||
|
|
@ -11,7 +12,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典类型表 sys_dict_type
|
* 字典类型表 sys_dict_type
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class SysDictType extends BaseEntity
|
public class SysDictType extends BaseEntity
|
||||||
|
|
@ -30,8 +31,11 @@ public class SysDictType extends BaseEntity
|
||||||
@Excel(name = "字典类型")
|
@Excel(name = "字典类型")
|
||||||
private String dictType;
|
private String dictType;
|
||||||
|
|
||||||
/** 状态(0正常 1停用) */
|
/**
|
||||||
@Excel(name = "状态", readConverterExp = "0=正常,1=停用")
|
* 状态(0正常 1停用)
|
||||||
|
* @see EnableStatusEnum
|
||||||
|
**/
|
||||||
|
@Excel(name = "状态", readConverterEnum = EnableStatusEnum.class)
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
public Long getDictId()
|
public Long getDictId()
|
||||||
|
|
@ -78,7 +82,7 @@ public class SysDictType extends BaseEntity
|
||||||
{
|
{
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.util.Set;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
import com.ruoyi.system.api.emun.EnableStatusEnum;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.core.annotation.Excel;
|
import com.ruoyi.common.core.annotation.Excel;
|
||||||
|
|
@ -12,7 +13,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色表 sys_role
|
* 角色表 sys_role
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class SysRole extends BaseEntity
|
public class SysRole extends BaseEntity
|
||||||
|
|
@ -45,8 +46,11 @@ public class SysRole extends BaseEntity
|
||||||
/** 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示 ) */
|
/** 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示 ) */
|
||||||
private boolean deptCheckStrictly;
|
private boolean deptCheckStrictly;
|
||||||
|
|
||||||
/** 角色状态(0正常 1停用) */
|
/**
|
||||||
@Excel(name = "角色状态", readConverterExp = "0=正常,1=停用")
|
* 角色状态(0正常 1停用)
|
||||||
|
* @see EnableStatusEnum
|
||||||
|
**/
|
||||||
|
@Excel(name = "角色状态", readConverterEnum = EnableStatusEnum.class)
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
/** 删除标志(0代表存在 2代表删除) */
|
/** 删除标志(0代表存在 2代表删除) */
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.ruoyi.system.api.domain;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
|
import com.ruoyi.system.api.emun.EnableStatusEnum;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.core.annotation.Excel;
|
import com.ruoyi.common.core.annotation.Excel;
|
||||||
|
|
@ -14,7 +15,7 @@ import com.ruoyi.common.core.xss.Xss;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户对象 sys_user
|
* 用户对象 sys_user
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class SysUser extends BaseEntity
|
public class SysUser extends BaseEntity
|
||||||
|
|
@ -55,8 +56,11 @@ public class SysUser extends BaseEntity
|
||||||
/** 密码 */
|
/** 密码 */
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
/** 帐号状态(0正常 1停用) */
|
/**
|
||||||
@Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
|
* 帐号状态(0正常 1停用)
|
||||||
|
* @see EnableStatusEnum
|
||||||
|
**/
|
||||||
|
@Excel(name = "帐号状态", readConverterEnum = EnableStatusEnum.class)
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
/** 删除标志(0代表存在 2代表删除) */
|
/** 删除标志(0代表存在 2代表删除) */
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.ruoyi.system.api.emun;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.annotation.DictTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ===========================================
|
||||||
|
* Copyright 2024 xiaoyang
|
||||||
|
* All rights reserved
|
||||||
|
* <p>项 目 名 :RuoYi-Cloud</p>
|
||||||
|
* <p>文 件 名 :EnableStatusEnum</p>
|
||||||
|
* <p>描 述 :启用状态枚举</p>
|
||||||
|
*
|
||||||
|
* @author :xiaoyang
|
||||||
|
* @date : 2024/1/12 20:26
|
||||||
|
* ============================================
|
||||||
|
*/
|
||||||
|
public enum EnableStatusEnum implements DictTag
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 正常
|
||||||
|
*/
|
||||||
|
enable("0", "正常"),
|
||||||
|
/**
|
||||||
|
* 停用
|
||||||
|
*/
|
||||||
|
disable("1", "停用");
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
;
|
||||||
|
|
||||||
|
EnableStatusEnum(String code, String name)
|
||||||
|
{
|
||||||
|
this.code = code;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String getKey()
|
||||||
|
{
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取名称
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取编码
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getCode()
|
||||||
|
{
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取键(集成字典类型)
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.ruoyi.common.core.annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ===========================================
|
||||||
|
* Copyright 2024 xiaoyang
|
||||||
|
* All rights reserved
|
||||||
|
* <p>项 目 名 :RuoYi-Cloud</p>
|
||||||
|
* <p>文 件 名 :DictTag</p>
|
||||||
|
* <p>描 述 :字典类型</p>
|
||||||
|
*
|
||||||
|
* @author :xiaoyang
|
||||||
|
* @date : 2024/1/12 20:27
|
||||||
|
* ============================================
|
||||||
|
*/
|
||||||
|
public interface DictTag
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 绑定键
|
||||||
|
*
|
||||||
|
* @return 绑定的键值
|
||||||
|
*/
|
||||||
|
String getKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定名称
|
||||||
|
*
|
||||||
|
* @return 绑定的名称
|
||||||
|
*/
|
||||||
|
String getName();
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ import com.ruoyi.common.core.utils.poi.ExcelHandlerAdapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义导出Excel数据注解
|
* 自定义导出Excel数据注解
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
|
@ -37,6 +37,14 @@ public @interface Excel
|
||||||
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
|
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
|
||||||
*/
|
*/
|
||||||
public String readConverterExp() default "";
|
public String readConverterExp() default "";
|
||||||
|
/**
|
||||||
|
* 字典转换器的枚举,该枚举需要实现DictTag标签,否则不生效
|
||||||
|
* <p>
|
||||||
|
* 枚举字典有值时,优先取枚举字典
|
||||||
|
*
|
||||||
|
* @return 字典转换枚举
|
||||||
|
*/
|
||||||
|
Class<? extends Enum> readConverterEnum() default Enum.class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分隔符,读取字符串组内容
|
* 分隔符,读取字符串组内容
|
||||||
|
|
@ -179,4 +187,4 @@ public @interface Excel
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,10 @@ import java.time.LocalDateTime;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 时间工具类
|
* 时间工具类
|
||||||
|
|
@ -127,6 +129,82 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期型字符串转化为日期 格式
|
||||||
|
*/
|
||||||
|
public static LocalDate parseLocalDate(Object str)
|
||||||
|
{
|
||||||
|
if (str == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return toLocalDate(str.toString(), parsePatterns);
|
||||||
|
}
|
||||||
|
catch (ParseException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 日期型字符串转化为日期 格式
|
||||||
|
*/
|
||||||
|
public static LocalDateTime parseLocalDateTime(Object str)
|
||||||
|
{
|
||||||
|
if (str == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return toLocalDateTime(str.toString(), parsePatterns);
|
||||||
|
}
|
||||||
|
catch (ParseException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static LocalDate toLocalDate(String dateTime, String... formats) throws ParseException{
|
||||||
|
if (StringUtils.isEmpty(dateTime)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (formats == null || formats.length == 0) {
|
||||||
|
formats = new String[]{"yyyy-MM-dd"};
|
||||||
|
}
|
||||||
|
for(String format:formats){
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||||
|
LocalDate ldt = LocalDate.parse(dateTime, df);
|
||||||
|
return ldt;
|
||||||
|
}catch (Exception ignored)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ParseException("转换异常",formats.length);
|
||||||
|
}
|
||||||
|
public static LocalDateTime toLocalDateTime(String dateTime, String... formats) throws ParseException{
|
||||||
|
if (StringUtils.isEmpty(dateTime)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (formats == null || formats.length == 0) {
|
||||||
|
formats = new String[]{"yyyy-MM-dd HH:mm:ss"};
|
||||||
|
}
|
||||||
|
for(String format:formats){
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
||||||
|
LocalDateTime ldt = LocalDateTime.parse(dateTime, df);
|
||||||
|
return ldt;
|
||||||
|
}catch (Exception ignored)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ParseException("转换异常",formats.length);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 获取服务器启动时间
|
* 获取服务器启动时间
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import com.ruoyi.common.core.annotation.DictTag;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.RegExUtils;
|
import org.apache.commons.lang3.RegExUtils;
|
||||||
import org.apache.commons.lang3.reflect.FieldUtils;
|
import org.apache.commons.lang3.reflect.FieldUtils;
|
||||||
|
|
@ -257,7 +258,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对excel表单默认第一个索引名转换成list
|
* 对excel表单默认第一个索引名转换成list
|
||||||
*
|
*
|
||||||
* @param is 输入流
|
* @param is 输入流
|
||||||
* @return 转换后集合
|
* @return 转换后集合
|
||||||
*/
|
*/
|
||||||
|
|
@ -282,7 +283,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对excel表单默认第一个索引名转换成list
|
* 对excel表单默认第一个索引名转换成list
|
||||||
*
|
*
|
||||||
* @param is 输入流
|
* @param is 输入流
|
||||||
* @param titleNum 标题占用行数
|
* @param titleNum 标题占用行数
|
||||||
* @return 转换后集合
|
* @return 转换后集合
|
||||||
|
|
@ -292,9 +293,42 @@ public class ExcelUtil<T>
|
||||||
return importExcel(StringUtils.EMPTY, is, titleNum);
|
return importExcel(StringUtils.EMPTY, is, titleNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始excel值转换为字段的转换器
|
||||||
|
* @return 值和字段的映射表
|
||||||
|
*/
|
||||||
|
public Map<String,String> initValueToFieldConversionMap(Excel attr){
|
||||||
|
HashMap<String, String> conversionMap = new HashMap<>();
|
||||||
|
//如果存在转换exp,提取转换exp
|
||||||
|
if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
||||||
|
{
|
||||||
|
String allList = attr.readConverterExp();
|
||||||
|
String[] split = allList.split(",");
|
||||||
|
for (String dictTagItem : split)
|
||||||
|
{
|
||||||
|
String[] split1 = dictTagItem.split("=");
|
||||||
|
conversionMap.put(split1[1], split1[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//字段转换初始化(通过枚举字段转换)
|
||||||
|
if (!attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||||
|
{
|
||||||
|
Class<? extends Enum> aClass = attr.readConverterEnum();
|
||||||
|
Object[] enumConstants = aClass.getEnumConstants();
|
||||||
|
for (Object enumItem : enumConstants)
|
||||||
|
{
|
||||||
|
if (enumItem instanceof DictTag)
|
||||||
|
{
|
||||||
|
DictTag dictTag = (DictTag) enumItem;
|
||||||
|
conversionMap.put(dictTag.getName(), dictTag.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return conversionMap;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 对excel表单指定表格索引名转换成list
|
* 对excel表单指定表格索引名转换成list
|
||||||
*
|
*
|
||||||
* @param sheetName 表格索引名
|
* @param sheetName 表格索引名
|
||||||
* @param titleNum 标题占用行数
|
* @param titleNum 标题占用行数
|
||||||
* @param is 输入流
|
* @param is 输入流
|
||||||
|
|
@ -336,6 +370,8 @@ public class ExcelUtil<T>
|
||||||
// 有数据时才处理 得到类的所有field.
|
// 有数据时才处理 得到类的所有field.
|
||||||
List<Object[]> fields = this.getFields();
|
List<Object[]> fields = this.getFields();
|
||||||
Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>();
|
Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>();
|
||||||
|
// 定义所有的excel值和java对象的值的转换映射表
|
||||||
|
Map<String,Map<String,String>> allFieldConversionMap=new HashMap<>();
|
||||||
for (Object[] objects : fields)
|
for (Object[] objects : fields)
|
||||||
{
|
{
|
||||||
Excel attr = (Excel) objects[1];
|
Excel attr = (Excel) objects[1];
|
||||||
|
|
@ -343,6 +379,13 @@ public class ExcelUtil<T>
|
||||||
if (column != null)
|
if (column != null)
|
||||||
{
|
{
|
||||||
fieldsMap.put(column, objects);
|
fieldsMap.put(column, objects);
|
||||||
|
//如果存在解析键值映射,则直接初始化
|
||||||
|
if(StringUtils.isNotEmpty(attr.readConverterExp())
|
||||||
|
|| !attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||||
|
{
|
||||||
|
Map<String, String> fieldConversionMap = initValueToFieldConversionMap(attr);
|
||||||
|
allFieldConversionMap.put(attr.name(),fieldConversionMap);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = titleNum + 1; i <= rows; i++)
|
for (int i = titleNum + 1; i <= rows; i++)
|
||||||
|
|
@ -417,6 +460,14 @@ public class ExcelUtil<T>
|
||||||
val = DateUtil.getJavaDate((Double) val);
|
val = DateUtil.getJavaDate((Double) val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (LocalDate.class == fieldType)
|
||||||
|
{
|
||||||
|
val = DateUtils.parseLocalDate(val);
|
||||||
|
}
|
||||||
|
else if (LocalDateTime.class == fieldType)
|
||||||
|
{
|
||||||
|
val = DateUtils.parseLocalDateTime(val);
|
||||||
|
}
|
||||||
else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
|
else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
|
||||||
{
|
{
|
||||||
val = Convert.toBool(val, false);
|
val = Convert.toBool(val, false);
|
||||||
|
|
@ -428,9 +479,9 @@ public class ExcelUtil<T>
|
||||||
{
|
{
|
||||||
propertyName = field.getName() + "." + attr.targetAttr();
|
propertyName = field.getName() + "." + attr.targetAttr();
|
||||||
}
|
}
|
||||||
if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
if (allFieldConversionMap.get(attr.name())!=null)
|
||||||
{
|
{
|
||||||
val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
|
val = getConvertValueByExpMap(Convert.toStr(val), attr.separator(), allFieldConversionMap.get(attr.name()));
|
||||||
}
|
}
|
||||||
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
||||||
{
|
{
|
||||||
|
|
@ -503,7 +554,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对list数据源将其里面的数据导入到excel表单
|
* 对list数据源将其里面的数据导入到excel表单
|
||||||
*
|
*
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public void exportExcel(HttpServletResponse response)
|
public void exportExcel(HttpServletResponse response)
|
||||||
|
|
@ -533,7 +584,8 @@ public class ExcelUtil<T>
|
||||||
for (int index = 0; index < sheetNo; index++)
|
for (int index = 0; index < sheetNo; index++)
|
||||||
{
|
{
|
||||||
createSheet(sheetNo, index);
|
createSheet(sheetNo, index);
|
||||||
|
//字段转换列表<列下标,<待转值,转换值>>
|
||||||
|
Map<Integer, Map<String, String>> fieldConversionMap = new HashMap<>();
|
||||||
// 产生一行
|
// 产生一行
|
||||||
Row row = sheet.createRow(rownum);
|
Row row = sheet.createRow(rownum);
|
||||||
int column = 0;
|
int column = 0;
|
||||||
|
|
@ -547,17 +599,17 @@ public class ExcelUtil<T>
|
||||||
for (Field subField : subFields)
|
for (Field subField : subFields)
|
||||||
{
|
{
|
||||||
Excel subExcel = subField.getAnnotation(Excel.class);
|
Excel subExcel = subField.getAnnotation(Excel.class);
|
||||||
this.createHeadCell(subExcel, row, column++);
|
this.createHeadCellAndInitFieldConversion(subExcel, row, column++,fieldConversionMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.createHeadCell(excel, row, column++);
|
this.createHeadCellAndInitFieldConversion(excel, row, column++,fieldConversionMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Type.EXPORT.equals(type))
|
if (Type.EXPORT.equals(type))
|
||||||
{
|
{
|
||||||
fillExcelData(index, row);
|
fillExcelData(index, row, fieldConversionMap);
|
||||||
addStatisticsRow();
|
addStatisticsRow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -565,12 +617,13 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 填充excel数据
|
* 填充excel数据
|
||||||
*
|
*
|
||||||
* @param index 序号
|
* @param index 序号
|
||||||
* @param row 单元格行
|
* @param row 单元格行
|
||||||
|
* @param fieldConversionMap 字段转换Map
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void fillExcelData(int index, Row row)
|
public void fillExcelData(int index, Row row, Map<Integer, Map<String, String>> fieldConversionMap)
|
||||||
{
|
{
|
||||||
int startNo = index * sheetSize;
|
int startNo = index * sheetSize;
|
||||||
int endNo = Math.min(startNo + sheetSize, list.size());
|
int endNo = Math.min(startNo + sheetSize, list.size());
|
||||||
|
|
@ -618,7 +671,7 @@ public class ExcelUtil<T>
|
||||||
{
|
{
|
||||||
subField.setAccessible(true);
|
subField.setAccessible(true);
|
||||||
Excel attr = subField.getAnnotation(Excel.class);
|
Excel attr = subField.getAnnotation(Excel.class);
|
||||||
this.addCell(attr, row, (T) obj, subField, column + subIndex);
|
this.addCell(attr, row, (T) obj, subField, column + subIndex, fieldConversionMap);
|
||||||
}
|
}
|
||||||
subIndex++;
|
subIndex++;
|
||||||
}
|
}
|
||||||
|
|
@ -628,7 +681,7 @@ public class ExcelUtil<T>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.addCell(excel, row, vo, field, column++);
|
this.addCell(excel, row, vo, field, column++, fieldConversionMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -636,7 +689,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建表格样式
|
* 创建表格样式
|
||||||
*
|
*
|
||||||
* @param wb 工作薄对象
|
* @param wb 工作薄对象
|
||||||
* @return 样式列表
|
* @return 样式列表
|
||||||
*/
|
*/
|
||||||
|
|
@ -689,7 +742,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据Excel注解创建表格头样式
|
* 根据Excel注解创建表格头样式
|
||||||
*
|
*
|
||||||
* @param wb 工作薄对象
|
* @param wb 工作薄对象
|
||||||
* @return 自定义样式列表
|
* @return 自定义样式列表
|
||||||
*/
|
*/
|
||||||
|
|
@ -722,7 +775,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据Excel注解创建表格列样式
|
* 根据Excel注解创建表格列样式
|
||||||
*
|
*
|
||||||
* @param wb 工作薄对象
|
* @param wb 工作薄对象
|
||||||
* @return 自定义样式列表
|
* @return 自定义样式列表
|
||||||
*/
|
*/
|
||||||
|
|
@ -760,9 +813,9 @@ public class ExcelUtil<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建单元格
|
* 创建首行单元格,并且初始化字段的转换
|
||||||
*/
|
*/
|
||||||
public Cell createHeadCell(Excel attr, Row row, int column)
|
public Cell createHeadCellAndInitFieldConversion(Excel attr, Row row, int column, Map<Integer, Map<String, String>> fieldConversionMap)
|
||||||
{
|
{
|
||||||
// 创建列
|
// 创建列
|
||||||
Cell cell = row.createCell(column);
|
Cell cell = row.createCell(column);
|
||||||
|
|
@ -770,6 +823,35 @@ public class ExcelUtil<T>
|
||||||
cell.setCellValue(attr.name());
|
cell.setCellValue(attr.name());
|
||||||
setDataValidation(attr, row, column);
|
setDataValidation(attr, row, column);
|
||||||
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
||||||
|
//字段转换初始化(通过readConverterExp字符转换)
|
||||||
|
if (StringUtils.isNotEmpty(attr.readConverterExp()))
|
||||||
|
{
|
||||||
|
HashMap<String, String> conversionMap = new HashMap<>();
|
||||||
|
String allList = attr.readConverterExp();
|
||||||
|
String[] split = allList.split(",");
|
||||||
|
for (String dictTagItem : split)
|
||||||
|
{
|
||||||
|
String[] split1 = dictTagItem.split("=");
|
||||||
|
conversionMap.put(split1[0], split1[1]);
|
||||||
|
}
|
||||||
|
fieldConversionMap.put(column, conversionMap);
|
||||||
|
}
|
||||||
|
//字段转换初始化(通过枚举字段转换)
|
||||||
|
if (!attr.readConverterEnum().getName().equals(Enum.class.getName()))
|
||||||
|
{
|
||||||
|
HashMap<String, String> conversionMap = new HashMap<>();
|
||||||
|
Class<? extends Enum> aClass = attr.readConverterEnum();
|
||||||
|
Object[] enumConstants = aClass.getEnumConstants();
|
||||||
|
for (Object enumItem : enumConstants)
|
||||||
|
{
|
||||||
|
if (enumItem instanceof DictTag)
|
||||||
|
{
|
||||||
|
DictTag dictTag = (DictTag) enumItem;
|
||||||
|
conversionMap.put(dictTag.getKey(), dictTag.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fieldConversionMap.put(column, conversionMap);
|
||||||
|
}
|
||||||
if (isSubList())
|
if (isSubList())
|
||||||
{
|
{
|
||||||
// 填充默认样式,防止合并单元格样式失效
|
// 填充默认样式,防止合并单元格样式失效
|
||||||
|
|
@ -784,7 +866,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置单元格信息
|
* 设置单元格信息
|
||||||
*
|
*
|
||||||
* @param value 单元格值
|
* @param value 单元格值
|
||||||
* @param attr 注解相关
|
* @param attr 注解相关
|
||||||
* @param cell 单元格信息
|
* @param cell 单元格信息
|
||||||
|
|
@ -885,8 +967,15 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加单元格
|
* 添加单元格
|
||||||
|
* @param attr 当前单元格表注解
|
||||||
|
* @param row 当前单元格归属行
|
||||||
|
* @param vo 单元格内容
|
||||||
|
* @param field 单元格字段
|
||||||
|
* @param column 单元格的列号
|
||||||
|
* @param fieldConversionMap 所有单元格的解析值字典
|
||||||
|
* @return 生成后的单元格
|
||||||
*/
|
*/
|
||||||
public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
|
public Cell addCell(Excel attr, Row row, T vo, Field field, int column,Map<Integer, Map<String, String>> fieldConversionMap)
|
||||||
{
|
{
|
||||||
Cell cell = null;
|
Cell cell = null;
|
||||||
try
|
try
|
||||||
|
|
@ -908,15 +997,14 @@ public class ExcelUtil<T>
|
||||||
// 用于读取对象中的属性
|
// 用于读取对象中的属性
|
||||||
Object value = getTargetValue(vo, field, attr);
|
Object value = getTargetValue(vo, field, attr);
|
||||||
String dateFormat = attr.dateFormat();
|
String dateFormat = attr.dateFormat();
|
||||||
String readConverterExp = attr.readConverterExp();
|
|
||||||
String separator = attr.separator();
|
String separator = attr.separator();
|
||||||
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
|
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
|
||||||
{
|
{
|
||||||
cell.setCellValue(parseDateToStr(dateFormat, value));
|
cell.setCellValue(parseDateToStr(dateFormat, value));
|
||||||
}
|
}
|
||||||
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
|
else if (fieldConversionMap.get(column)!=null && StringUtils.isNotNull(value))
|
||||||
{
|
{
|
||||||
cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
|
cell.setCellValue(getConvertValueByExpMap(Convert.toStr(value), separator, fieldConversionMap.get(column)));
|
||||||
}
|
}
|
||||||
else if (value instanceof BigDecimal && -1 != attr.scale())
|
else if (value instanceof BigDecimal && -1 != attr.scale())
|
||||||
{
|
{
|
||||||
|
|
@ -943,7 +1031,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置 POI XSSFSheet 单元格提示或选择框
|
* 设置 POI XSSFSheet 单元格提示或选择框
|
||||||
*
|
*
|
||||||
* @param sheet 表单
|
* @param sheet 表单
|
||||||
* @param textlist 下拉框显示的内容
|
* @param textlist 下拉框显示的内容
|
||||||
* @param promptContent 提示内容
|
* @param promptContent 提示内容
|
||||||
|
|
@ -980,7 +1068,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框).
|
* 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框).
|
||||||
*
|
*
|
||||||
* @param sheet 要设置的sheet.
|
* @param sheet 要设置的sheet.
|
||||||
* @param textlist 下拉框显示的内容
|
* @param textlist 下拉框显示的内容
|
||||||
* @param promptContent 提示内容
|
* @param promptContent 提示内容
|
||||||
|
|
@ -1031,77 +1119,34 @@ public class ExcelUtil<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析导出值 0=男,1=女,2=未知
|
* 通过解析字典,寻找解析值
|
||||||
*
|
*
|
||||||
* @param propertyValue 参数值
|
* @param propertyValue 解析前的值
|
||||||
* @param converterExp 翻译注解
|
* @param separator 分隔符
|
||||||
* @param separator 分隔符
|
* @param conversionMap 解析字典
|
||||||
* @return 解析后值
|
* @return 解析后值
|
||||||
*/
|
*/
|
||||||
public static String convertByExp(String propertyValue, String converterExp, String separator)
|
public static String getConvertValueByExpMap(String propertyValue,String separator,Map<String,String> conversionMap){
|
||||||
{
|
List<String> afterConversionValueList=new ArrayList<>();
|
||||||
StringBuilder propertyString = new StringBuilder();
|
//如果某个对象值为"1,2,3",则根据转换Map表全都进修转换
|
||||||
String[] convertSource = converterExp.split(",");
|
if (StringUtils.containsAny(propertyValue, separator))
|
||||||
for (String item : convertSource)
|
|
||||||
{
|
{
|
||||||
String[] itemArray = item.split("=");
|
for (String value : propertyValue.split(separator))
|
||||||
if (StringUtils.containsAny(propertyValue, separator))
|
|
||||||
{
|
{
|
||||||
for (String value : propertyValue.split(separator))
|
String conversionValue = conversionMap.get(value);
|
||||||
|
if(StringUtils.isNotEmpty(conversionValue))
|
||||||
{
|
{
|
||||||
if (itemArray[0].equals(value))
|
afterConversionValueList.add(conversionValue);
|
||||||
{
|
|
||||||
propertyString.append(itemArray[1] + separator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
}else
|
||||||
|
{
|
||||||
|
if(StringUtils.isNotEmpty(conversionMap.get(propertyValue)))
|
||||||
{
|
{
|
||||||
if (itemArray[0].equals(propertyValue))
|
afterConversionValueList.add(conversionMap.get(propertyValue));
|
||||||
{
|
|
||||||
return itemArray[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return StringUtils.stripEnd(propertyString.toString(), separator);
|
return String.join(separator,afterConversionValueList);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 反向解析值 男=0,女=1,未知=2
|
|
||||||
*
|
|
||||||
* @param propertyValue 参数值
|
|
||||||
* @param converterExp 翻译注解
|
|
||||||
* @param separator 分隔符
|
|
||||||
* @return 解析后值
|
|
||||||
*/
|
|
||||||
public static String reverseByExp(String propertyValue, String converterExp, String separator)
|
|
||||||
{
|
|
||||||
StringBuilder propertyString = new StringBuilder();
|
|
||||||
String[] convertSource = converterExp.split(",");
|
|
||||||
for (String item : convertSource)
|
|
||||||
{
|
|
||||||
String[] itemArray = item.split("=");
|
|
||||||
if (StringUtils.containsAny(propertyValue, separator))
|
|
||||||
{
|
|
||||||
for (String value : propertyValue.split(separator))
|
|
||||||
{
|
|
||||||
if (itemArray[1].equals(value))
|
|
||||||
{
|
|
||||||
propertyString.append(itemArray[0] + separator);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (itemArray[1].equals(propertyValue))
|
|
||||||
{
|
|
||||||
return itemArray[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringUtils.stripEnd(propertyString.toString(), separator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1311,7 +1356,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建工作表
|
* 创建工作表
|
||||||
*
|
*
|
||||||
* @param sheetNo sheet数量
|
* @param sheetNo sheet数量
|
||||||
* @param index 序号
|
* @param index 序号
|
||||||
*/
|
*/
|
||||||
|
|
@ -1328,7 +1373,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取单元格值
|
* 获取单元格值
|
||||||
*
|
*
|
||||||
* @param row 获取的行
|
* @param row 获取的行
|
||||||
* @param column 获取单元格列号
|
* @param column 获取单元格列号
|
||||||
* @return 单元格值
|
* @return 单元格值
|
||||||
|
|
@ -1388,7 +1433,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断是否是空行
|
* 判断是否是空行
|
||||||
*
|
*
|
||||||
* @param row 判断的行
|
* @param row 判断的行
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
@ -1411,7 +1456,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化不同类型的日期对象
|
* 格式化不同类型的日期对象
|
||||||
*
|
*
|
||||||
* @param dateFormat 日期格式
|
* @param dateFormat 日期格式
|
||||||
* @param val 被格式化的日期对象
|
* @param val 被格式化的日期对象
|
||||||
* @return 格式化后的日期字符
|
* @return 格式化后的日期字符
|
||||||
|
|
@ -1477,7 +1522,7 @@ public class ExcelUtil<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取对象的子列表方法
|
* 获取对象的子列表方法
|
||||||
*
|
*
|
||||||
* @param name 名称
|
* @param name 名称
|
||||||
* @param pojoClass 类对象
|
* @param pojoClass 类对象
|
||||||
* @return 子列表方法
|
* @return 子列表方法
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.ruoyi.system.domain;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
import com.ruoyi.system.api.emun.EnableStatusEnum;
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
import com.ruoyi.common.core.annotation.Excel;
|
import com.ruoyi.common.core.annotation.Excel;
|
||||||
|
|
@ -11,7 +12,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 岗位表 sys_post
|
* 岗位表 sys_post
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class SysPost extends BaseEntity
|
public class SysPost extends BaseEntity
|
||||||
|
|
@ -34,8 +35,11 @@ public class SysPost extends BaseEntity
|
||||||
@Excel(name = "岗位排序")
|
@Excel(name = "岗位排序")
|
||||||
private Integer postSort;
|
private Integer postSort;
|
||||||
|
|
||||||
/** 状态(0正常 1停用) */
|
/**
|
||||||
@Excel(name = "状态", readConverterExp = "0=正常,1=停用")
|
* 状态(0正常 1停用)
|
||||||
|
* @see EnableStatusEnum
|
||||||
|
**/
|
||||||
|
@Excel(name = "状态", readConverterEnum = EnableStatusEnum.class)
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
/** 用户是否存在此岗位标识 默认不存在 */
|
/** 用户是否存在此岗位标识 默认不存在 */
|
||||||
|
|
@ -105,7 +109,7 @@ public class SysPost extends BaseEntity
|
||||||
{
|
{
|
||||||
this.flag = flag;
|
this.flag = flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue