Excel 导入导出 带字典翻译

v1.4.1
Parker 4 years ago
parent a8ae3e0b1d
commit 43243e24d3

@ -46,35 +46,40 @@ public abstract class ApiWrapper implements Serializable {
/** ID */ /** ID */
@TableId @TableId
@ApiModelProperty(value = "ID") @ExcelIgnore
@ExcelProperty(value = "ID", order = 1000) //@ApiModelProperty(value = "ID")
//@ExcelProperty(value = "ID", order = 1000)
@CellStyleFormat @CellStyleFormat
private String id; private String id;
/** 创建人 */ /** 创建人 */
@ApiModelProperty(value = "创建人") @ExcelIgnore
@ExcelProperty(value = "创建人", order = 1001) //@ApiModelProperty(value = "创建人")
//@ExcelProperty(value = "创建人", order = 1001)
@CellStyleFormat @CellStyleFormat
private String createBy; private String createBy;
/** 创建时间 */ /** 创建时间 */
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@ExcelProperty(value = "创建时间", order = 1002) @ExcelIgnore
@CellStyleFormat //@ExcelProperty(value = "创建时间", order = 1002)
//@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
/** 更新人 */ /** 更新人 */
@ApiModelProperty(value = "修改人") @ApiModelProperty(value = "修改人")
@ExcelProperty(value = "修改人", order = 1003) @ExcelIgnore
@CellStyleFormat //@ExcelProperty(value = "修改人", order = 1003)
//@CellStyleFormat
private String updateBy; private String updateBy;
/** 更新时间 */ /** 更新时间 */
@ApiModelProperty(value = "修改时间") @ApiModelProperty(value = "修改时间")
@ExcelProperty(value = "修改时间", order = 1004) @ExcelIgnore
@CellStyleFormat //@ExcelProperty(value = "修改时间", order = 1004)
//@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime; private Date updateTime;

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.api.base.warpper.ApiWrapper; import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.DictType;
import org.opsli.plugins.excel.annotation.CellStyleFormat; import org.opsli.plugins.excel.annotation.CellStyleFormat;
/** /**
@ -15,6 +16,9 @@ import org.opsli.plugins.excel.annotation.CellStyleFormat;
* @Author: Parker * @Author: Parker
* @CreateTime: 2020-09-16 17:33 * @CreateTime: 2020-09-16 17:33
* @Description: * @Description:
*
*
*
*/ */
@ApiModel(value="测试接口返回Model",description="测试接口返回Model") @ApiModel(value="测试接口返回Model",description="测试接口返回Model")
@Data @Data
@ -25,8 +29,14 @@ public class TestModel extends ApiWrapper {
@CellStyleFormat @CellStyleFormat
private String name; private String name;
@ApiModelProperty(value = "分类")
@DictType("testType")
@ExcelProperty(value = "分类", order = 2)
@CellStyleFormat
private String type;
@ApiModelProperty(value = "备注") @ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注", order = 2) @ExcelProperty(value = "备注", order = 3)
@CellStyleFormat @CellStyleFormat
private String remark; private String remark;

@ -0,0 +1,23 @@
package org.opsli.common.annotation;
import java.lang.annotation.*;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.annotation
* @Author: Parker
* @CreateTime: 2020-09-16 16:36
* @Description:
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface DictType {
/** 字典类型 code */
String value();
}

@ -0,0 +1,25 @@
package org.opsli.common.enums;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.enums
* @Author: Parker
* @CreateTime: 2020-09-17 23:40
* @Description: Excel
*/
public enum ExcelOperate {
/**
*
*/
READ,
/**
*
*/
WRITE;
;
}

@ -21,7 +21,7 @@ import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.base.service.interfaces.CrudServiceInterface; import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg; import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.excel.ExcelUtil; import org.opsli.core.utils.ExcelUtil;
import org.opsli.plugins.excel.exception.ExcelPluginException; import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.opsli.plugins.redis.RedisLockPlugins; import org.opsli.plugins.redis.RedisLockPlugins;
import org.opsli.plugins.redis.lock.RedisLock; import org.opsli.plugins.redis.lock.RedisLock;
@ -50,24 +50,26 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@RestController @RestController
public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEntity, S extends CrudServiceInterface<E,T>>{ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWrapper, S extends CrudServiceInterface<T,E>>{
/** 开启热点数据状态 */ /** 开启热点数据状态 */
protected boolean hotDataFlag = false; protected boolean hotDataFlag = false;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 0;
/** Entity Clazz 类 */ /** Entity Clazz 类 */
protected Class<T> entityClazz; protected Class<T> entityClazz;
/** Entity 泛型游标 */ /** Entity 泛型游标 */
private static final int entityIndex = 1; private static final int entityIndex = 0;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 1;
@Autowired(required = false) @Autowired(required = false)
protected S IService; protected S IService;
@Autowired @Autowired
private RedisLockPlugins redisLockPlugins; private RedisLockPlugins redisLockPlugins;
@Autowired
private ExcelUtil excelUtil;
/** /**
* *
@ -159,8 +161,10 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
return ResultVo.error(CoreMsg.EXCEL_FILE_NULL.getCode(), return ResultVo.error(CoreMsg.EXCEL_FILE_NULL.getCode(),
CoreMsg.EXCEL_FILE_NULL.getMessage()); CoreMsg.EXCEL_FILE_NULL.getMessage());
} }
ResultVo<?> resultVo;
String msgInfo = "";
try { try {
List<E> modelList = ExcelUtil.readExcel(files.get(0), modelClazz); List<E> modelList = excelUtil.readExcel(files.get(0), modelClazz);
boolean ret = IService.insertBatch(modelList); boolean ret = IService.insertBatch(modelList);
if(!ret){ if(!ret){
throw new ExcelPluginException(CoreMsg.EXCEL_IMPORT_NO); throw new ExcelPluginException(CoreMsg.EXCEL_IMPORT_NO);
@ -168,18 +172,21 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
// 花费毫秒数 // 花费毫秒数
long timerCount = timer.interval(); long timerCount = timer.interval();
// 提示信息 // 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), modelList.size(), timerCount); msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), timerCount);
// 导入成功 // 导出成功
return ResultVo.success(msg); resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_IMPORT_SUCCESS.getCode());
} catch (ExcelPluginException e) { } catch (ExcelPluginException e) {
// 花费毫秒数 // 花费毫秒数
long timerCount = timer.interval(); long timerCount = timer.interval();
log.error(e.getMessage(),e);
// 提示信息 // 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), e.getMessage(), timerCount); msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), timerCount, e.getMessage());
// 导入失败 // 导入失败
return ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msg); resultVo = ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msgInfo);
} }
// 记录导出日志
log.info(msgInfo);
return resultVo;
} }
/** /**
@ -198,6 +205,10 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
* @param response * @param response
*/ */
protected ResultVo<?> excelExport(String fileName, QueryWrapper<T> queryWrapper, HttpServletResponse response){ protected ResultVo<?> excelExport(String fileName, QueryWrapper<T> queryWrapper, HttpServletResponse response){
// 计时器
TimeInterval timer = DateUtil.timer();
String msgInfo = "";
ResultVo<?> resultVo;
try { try {
List<E> modelList = Lists.newArrayList(); List<E> modelList = Lists.newArrayList();
if(queryWrapper != null){ if(queryWrapper != null){
@ -206,16 +217,25 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
modelList = WrapperUtil.transformInstance(entityList, modelClazz); modelList = WrapperUtil.transformInstance(entityList, modelClazz);
} }
// 导出Excel // 导出Excel
ExcelUtil.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX); excelUtil.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage(), modelList.size(), timerCount);
// 导出成功 // 导出成功
return ResultVo.success(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage()); resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_EXPORT_SUCCESS.getCode());
} catch (ExcelPluginException e) { } catch (ExcelPluginException e) {
log.error(e.getMessage(),e); // 花费毫秒数
long timerCount = timer.interval();
// 提示信息 // 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), e.getMessage()); msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), timerCount, e.getMessage());
// 导出失败 // 导出失败
return ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(),msg); resultVo = ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(), msgInfo);
} }
// 记录导出日志
log.info(msgInfo);
return resultVo;
} }
// ================================================= // =================================================

@ -43,19 +43,18 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@Transactional(readOnly = true) @Transactional(readOnly = true)
public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrapper, T extends BaseEntity> public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, E extends ApiWrapper>
extends BaseService<M, T> implements CrudServiceInterface<E,T> { extends BaseService<M, T> implements CrudServiceInterface<T,E> {
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 1;
/** Entity Clazz 类 */ /** Entity Clazz 类 */
protected Class<T> entityClazz; protected Class<T> entityClazz;
/** Entity 泛型游标 */ /** Entity 泛型游标 */
private static final int entityIndex = 2; private static final int entityIndex = 1;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 2;
@Override @Override
public E get(String id) { public E get(String id) {
@ -154,7 +153,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
} }
@Override @Override
public Page<E,T> findPage(Page<E,T> page) { public Page<T,E> findPage(Page<T,E> page) {
page.pageHelperBegin(); page.pageHelperBegin();
try{ try{
List<T> list = this.findList(page.getQueryWrapper()); List<T> list = this.findList(page.getQueryWrapper());
@ -217,8 +216,8 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
@PostConstruct @PostConstruct
public void init(){ public void init(){
try { try {
this.modelClazz = this.getModelClass(); this.modelClazz = this.getModelClazz();
this.entityClazz = this.getEntityClass(); this.entityClazz = this.getEntityClazz();
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
@ -229,7 +228,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
* Clazz * Clazz
* @return * @return
*/ */
private Class<E> getModelClass(){ private Class<E> getModelClazz(){
Class<E> tClass = null; Class<E> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), modelIndex); Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), modelIndex);
if(typeArgument != null){ if(typeArgument != null){
@ -242,7 +241,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
* Clazz * Clazz
* @return * @return
*/ */
private Class<T> getEntityClass(){ private Class<T> getEntityClazz(){
Class<T> tClass = null; Class<T> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), entityIndex); Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), entityIndex);
if(typeArgument != null){ if(typeArgument != null){

@ -18,10 +18,10 @@ import java.util.List;
* *
* *
* *
* @param <E>
* @param <T> * @param <T>
* @param <E>
*/ */
public interface CrudServiceInterface<E,T> extends BaseServiceInterface<T> { public interface CrudServiceInterface<T,E> extends BaseServiceInterface<T> {
/** /**
@ -158,7 +158,7 @@ public interface CrudServiceInterface<E,T> extends BaseServiceInterface<T> {
* *
* @return Page<T> * @return Page<T>
*/ */
Page<E,T> findPage(Page<E,T> page); Page<T,E> findPage(Page<T,E> page);
} }

@ -28,10 +28,10 @@ public enum CoreMsg implements BaseMsg {
/** /**
* Excel * Excel
*/ */
EXCEL_EXPORT_SUCCESS(10300,"Excel 导出成功!"), EXCEL_EXPORT_SUCCESS(10300,"Excel 导出成功! - 数据行数:{} - 耗时:{}毫秒"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败!{}"), EXCEL_EXPORT_ERROR(10301,"Excel 导出失败! - 耗时:{}毫秒 - 失败信息:{}"),
EXCEL_IMPORT_SUCCESS(10302,"EXCEL 导入成功!数据行数:{} 耗时(秒){}"), EXCEL_IMPORT_SUCCESS(10302,"EXCEL 导入成功! - 耗时:{}毫秒"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败{} 耗时(秒){}"), EXCEL_IMPORT_ERROR(10303,"Excel导入失败! - 耗时:{}毫秒 - 失败信息{}"),
EXCEL_IMPORT_NO(10304,""), EXCEL_IMPORT_NO(10304,""),
EXCEL_FILE_NULL(10305,"请选择文件"), EXCEL_FILE_NULL(10305,"请选择文件"),

@ -8,7 +8,7 @@ import com.github.pagehelper.PageSerializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class Page<E,T> extends PageSerializable<E>{ public class Page<T,E> extends PageSerializable<E>{
private int pageNo = 1; private int pageNo = 1;

@ -1,5 +1,6 @@
package org.opsli.core.utils; package org.opsli.core.utils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -7,6 +8,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.dict.DictDetailApi; import org.opsli.api.web.system.dict.DictDetailApi;
import org.opsli.api.wrapper.system.dict.DictModel; import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.api.wrapper.system.dict.SysDictDetailModel; import org.opsli.api.wrapper.system.dict.SysDictDetailModel;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.constants.DictConstants; import org.opsli.common.constants.DictConstants;
import org.opsli.core.cache.local.CacheUtil; import org.opsli.core.cache.local.CacheUtil;
import org.opsli.plugins.redis.RedisLockPlugins; import org.opsli.plugins.redis.RedisLockPlugins;
@ -195,11 +197,16 @@ public class DictUtil {
public static List<DictModel> getDictList(String typeCode){ public static List<DictModel> getDictList(String typeCode){
List<DictModel> dictModels = Lists.newArrayList(); List<DictModel> dictModels = Lists.newArrayList();
try { try {
Map<Object, Object> dictMap = redisPlugin.hGetAll(DictConstants.CACHE_PREFIX_VALUE + typeCode); String key = CacheUtil.handleKey(CacheConstants.EDEN_HASH_DATA, DictConstants.CACHE_PREFIX_VALUE + typeCode);
Map<Object, Object> dictMap = redisPlugin.hGetAll(key);
Set<Map.Entry<Object, Object>> entries = dictMap.entrySet(); Set<Map.Entry<Object, Object>> entries = dictMap.entrySet();
for (Map.Entry<Object, Object> entry : entries) { for (Map.Entry<Object, Object> entry : entries) {
// 赋值 // 赋值
SysDictDetailModel model = (SysDictDetailModel) entry.getValue(); JSONObject jsonObject = (JSONObject) entry.getValue();
if(jsonObject == null){
continue;
}
SysDictDetailModel model = jsonObject.toJavaObject(SysDictDetailModel.class);
DictModel dictModel = new DictModel(); DictModel dictModel = new DictModel();
dictModel.setTypeCode(typeCode); dictModel.setTypeCode(typeCode);
dictModel.setDictName(model.getDictName()); dictModel.setDictName(model.getDictName());

@ -0,0 +1,155 @@
package org.opsli.core.utils;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.common.annotation.DictType;
import org.opsli.common.enums.ExcelOperate;
import org.opsli.plugins.excel.ExcelPlugin;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.utils
* @Author: Parker
* @CreateTime: 2020-09-23 21:53
* @Description: ExcelUtil
*/
@Slf4j
@Component
public class ExcelUtil extends ExcelPlugin {
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName, headLineNum);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
// 处理数据
List<T> ts = this.handleDict(list, classType, ExcelOperate.WRITE);
super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
}
/**
*
* @param datas
* @param typeClazz
* @param operate
* @param <T>
* @return
*/
private <T> List<T> handleDict(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
// 空处理
if(datas == null || datas.size() == 0){
return datas;
}
// 字段名 - 字典code
Map<String,String> fieldAndTypeCode = Maps.newHashMap();
// 字典code - 字典值
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
Field[] fields = ReflectUtil.getFields(typeClazz);
for (Field field : fields) {
DictType dictType = field.getAnnotation(DictType.class);
if(dictType != null){
String typeCode = dictType.value();
fieldAndTypeCode.put(field.getName(), typeCode);
}
}
// 如果没有字典 则直接返回
if(fieldAndTypeCode.size() == 0){
return datas;
}
// 取Redis 值
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
String typeCode = entry.getValue();
List<DictModel> dictList = DictUtil.getDictList(typeCode);
// 如果字典 List 为空 则走下一个
if(dictList == null || dictList.size() == 0) continue;
typeCodeAndValue.put(typeCode, dictList);
}
// 数据字典赋值
for (T data : datas) {
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
try {
String fieldName = entry.getKey();
String typeCode = entry.getValue();
String fieldValue = (String) ReflectUtil.getFieldValue(data, fieldName);
List<DictModel> dictModels = typeCodeAndValue.get(typeCode);
// 匹配字典
String dictVal = this.matchingDict(dictModels, fieldValue, operate);
if(StringUtils.isEmpty(dictVal)){
continue;
}
// 赋值
ReflectUtil.setFieldValue(data, fieldName, dictVal);
}catch (Exception e){
log.error(e.getMessage(), e);
}
}
}
return datas;
}
/**
*
* @param dictModels
* @param fieldValue
* @param operate
* @return
*/
private String matchingDict(List<DictModel> dictModels, String fieldValue, ExcelOperate operate){
String val = "";
for (DictModel dictModel : dictModels) {
// 读操作
if(ExcelOperate.READ == operate){
// 判断 Excel 读入 字典名称是否与 当前字典匹配
if(dictModel.getDictName().equals(fieldValue)){
val = dictModel.getDictValue();
break;
}
}
// 写操作
else if(ExcelOperate.WRITE == operate){
// 判断 Excel 写出 字典值是否与 当前字典匹配
if(dictModel.getDictValue().equals(fieldValue)){
val = dictModel.getDictName();
break;
}
}
}
return val;
}
}

@ -33,8 +33,9 @@
( (
a.dict_name = #{dictName} a.dict_name = #{dictName}
or or
a.type_code = #{typeCode} a.dict_value = #{dictValue}
) )
and a.type_code = #{typeCode}
and a.deleted = 0 and a.deleted = 0
<if test="id != null and id != ''"> <if test="id != null and id != ''">
and a.id != #{id} and a.id != #{id}

@ -13,7 +13,7 @@ import java.util.List;
* @CreateTime: 2020-09-17 13:07 * @CreateTime: 2020-09-17 13:07
* @Description: * @Description:
*/ */
public interface IDictDetailService extends CrudServiceInterface<SysDictDetailModel, SysDictDetail> { public interface IDictDetailService extends CrudServiceInterface<SysDictDetail, SysDictDetailModel> {
/** /**
* ID * ID

@ -12,7 +12,7 @@ import org.opsli.modulars.system.dict.entity.SysDict;
* @CreateTime: 2020-09-17 13:07 * @CreateTime: 2020-09-17 13:07
* @Description: * @Description:
*/ */
public interface IDictService extends CrudServiceInterface<SysDictModel, SysDict> { public interface IDictService extends CrudServiceInterface<SysDict, SysDictModel> {
} }

@ -39,7 +39,7 @@ import java.util.List;
* @Description: * @Description:
*/ */
@Service @Service
public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, SysDictDetailModel, SysDictDetail> implements IDictDetailService { public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, SysDictDetail, SysDictDetailModel> implements IDictDetailService {
@Autowired(required = false) @Autowired(required = false)
private DictDetailMapper mapper; private DictDetailMapper mapper;
@ -62,7 +62,7 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
Integer count = mapper.uniqueVerificationByNameOrValue(entity); Integer count = mapper.uniqueVerificationByNameOrValue(entity);
if(count != null && count > 0){ if(count != null && count > 0){
// 重复 // 重复
throw new ServiceException(SystemMsg.EXCEL_DICT_UNIQUE); throw new ServiceException(SystemMsg.EXCEL_DICT_DETAIL_UNIQUE);
} }
SysDictDetailModel ret = super.insert(model); SysDictDetailModel ret = super.insert(model);

@ -27,7 +27,7 @@ import java.util.List;
* @Description: * @Description:
*/ */
@Service @Service
public class DictServiceImpl extends CrudServiceImpl<DictMapper, SysDictModel, SysDict> implements IDictService { public class DictServiceImpl extends CrudServiceImpl<DictMapper, SysDict, SysDictModel> implements IDictService {
@Autowired(required = false) @Autowired(required = false)
private DictMapper mapper; private DictMapper mapper;

@ -29,7 +29,7 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@ApiRestController("/dict/detail") @ApiRestController("/dict/detail")
public class DictDetailRestController extends BaseRestController<SysDictDetailModel, SysDictDetail, IDictDetailService> public class DictDetailRestController extends BaseRestController<SysDictDetail, SysDictDetailModel, IDictDetailService>
implements DictDetailApi { implements DictDetailApi {
@ -56,7 +56,7 @@ public class DictDetailRestController extends BaseRestController<SysDictDetailMo
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) { public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<SysDictDetail> queryBuilder = new WebQueryBuilder<>(SysDictDetail.class, request.getParameterMap()); QueryBuilder<SysDictDetail> queryBuilder = new WebQueryBuilder<>(SysDictDetail.class, request.getParameterMap());
Page<SysDictDetailModel, SysDictDetail> page = new Page<>(pageNo, pageSize); Page<SysDictDetail, SysDictDetailModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build()); page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page); page = IService.findPage(page);

@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse;
*/ */
@Slf4j @Slf4j
@ApiRestController("/dict") @ApiRestController("/dict")
public class DictRestController extends BaseRestController<SysDictModel, SysDict, IDictService> public class DictRestController extends BaseRestController<SysDict, SysDictModel, IDictService>
implements DictApi { implements DictApi {
@ -55,7 +55,7 @@ public class DictRestController extends BaseRestController<SysDictModel, SysDict
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) { public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<SysDict> queryBuilder = new WebQueryBuilder<>(SysDict.class, request.getParameterMap()); QueryBuilder<SysDict> queryBuilder = new WebQueryBuilder<>(SysDict.class, request.getParameterMap());
Page<SysDictModel, SysDict> page = new Page<>(pageNo, pageSize); Page<SysDict, SysDictModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build()); page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page); page = IService.findPage(page);

@ -18,6 +18,9 @@ public class TestEntity extends BaseEntity {
/** 名称 */ /** 名称 */
private String name; private String name;
/** 分类 */
private String type;
/** 备注 */ /** 备注 */
private String remark; private String remark;

@ -6,6 +6,7 @@
<sql id="TestEntityColumns"> <sql id="TestEntityColumns">
a.id as id, a.id as id,
a.name as name, a.name as name,
a.type as type,
a.remark as remark, a.remark as remark,
a.create_by as createBy, a.create_by as createBy,
a.create_time as createTime, a.create_time as createTime,

@ -11,7 +11,7 @@ import org.opsli.modulars.test.entity.TestEntity;
* @CreateTime: 2020-09-17 13:07 * @CreateTime: 2020-09-17 13:07
* @Description: * @Description:
*/ */
public interface ITestService extends CrudServiceInterface<TestModel,TestEntity> { public interface ITestService extends CrudServiceInterface<TestEntity,TestModel> {
/** /**
* *

@ -31,7 +31,7 @@ import java.util.Collection;
@Service @Service
// 开启热数据标示 不加不生效 // 开启热数据标示 不加不生效
@EnableHotData @EnableHotData
public class TestServiceImpl extends CrudServiceImpl<TestMapper, TestModel, TestEntity> implements ITestService { public class TestServiceImpl extends CrudServiceImpl<TestMapper, TestEntity,TestModel > implements ITestService {
@Autowired(required = false) @Autowired(required = false)
private TestMapper mapper; private TestMapper mapper;

@ -1,6 +1,7 @@
package org.opsli.modulars.test.web; package org.opsli.modulars.test.web;
import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -49,7 +50,7 @@ import java.util.concurrent.TimeUnit;
*/ */
@Slf4j @Slf4j
@ApiRestController("/test") @ApiRestController("/test")
public class TestRestRestController extends BaseRestController<TestModel, TestEntity, ITestService> public class TestRestRestController extends BaseRestController<TestEntity, TestModel, ITestService>
implements TestApi { implements TestApi {
@ -267,7 +268,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) { public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<TestEntity> queryBuilder = new WebQueryBuilder<>(TestEntity.class, request.getParameterMap()); QueryBuilder<TestEntity> queryBuilder = new WebQueryBuilder<>(TestEntity.class, request.getParameterMap());
Page<TestModel, TestEntity> page = new Page<>(pageNo, pageSize); Page<TestEntity,TestModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build()); page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page); page = IService.findPage(page);
@ -294,6 +295,31 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
} }
/**
*
* @return
*/
@ApiOperation(value = "批量插入1000个随机新增数据", notes = "批量插入1000个随机新增数据")
@GetMapping("/insertAll")
public ResultVo<Boolean> insertAll(){
List<DictModel> testType = DictUtil.getDictList("testType");
List<TestModel> datas = new ArrayList<>();
// 转化对象 处理 ApiModel 与 本地对象
for (int i = 0; i < 1000; i++) {
int randomNum = RandomUtil.randomInt(0, testType.size());
TestModel model = new TestModel();
model.setName("测试名称"+random.nextInt());
model.setType(testType.get(randomNum).getDictValue());
model.setRemark("测试备注"+random.nextInt());
datas.add(model);
}
// 调用新增方法
boolean b = IService.insertBatch(datas);
return ResultVo.success("新增成功",b);
}
@ApiOperation(value = "获得字典 - By Name", notes = "获得字典 - By Name") @ApiOperation(value = "获得字典 - By Name", notes = "获得字典 - By Name")
@GetMapping("/getDictByName") @GetMapping("/getDictByName")

@ -30,11 +30,7 @@ import java.util.List;
* @author parker * @author parker
*/ */
@Slf4j @Slf4j
public final class ExcelUtil { public class ExcelPlugin {
/**
*
*/
private ExcelUtil(){}
/** /**
* Excel( sheet) * Excel( sheet)
@ -45,7 +41,7 @@ public final class ExcelUtil {
* @param rowModel BaseRowModel * @param rowModel BaseRowModel
* @return Excel list * @return Excel list
*/ */
public static <T> List<T> readExcel(MultipartFile excel,Class<T> rowModel) public <T> List<T> readExcel(MultipartFile excel,Class<T> rowModel)
throws ExcelPluginException { throws ExcelPluginException {
return readExcel(excel, rowModel, null, 1); return readExcel(excel, rowModel, null, 1);
} }
@ -57,7 +53,7 @@ public final class ExcelUtil {
* @param sheetName sheet 1 * @param sheetName sheet 1
* @return Excel list * @return Excel list
*/ */
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName)
throws ExcelPluginException{ throws ExcelPluginException{
return readExcel(excel, rowModel, sheetName, 1); return readExcel(excel, rowModel, sheetName, 1);
} }
@ -70,7 +66,7 @@ public final class ExcelUtil {
* @param headLineNum 1 * @param headLineNum 1
* @return Excel list * @return Excel list
*/ */
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName,
int headLineNum) throws ExcelPluginException { int headLineNum) throws ExcelPluginException {
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
InputStream inputStream = null; InputStream inputStream = null;
@ -111,7 +107,7 @@ public final class ExcelUtil {
* @param fileName * @param fileName
* @param sheetName sheet * @param sheetName sheet
*/ */
public static <T> void writeExcel(HttpServletResponse response, List<T> list, public <T> void writeExcel(HttpServletResponse response, List<T> list,
String fileName, String sheetName, String fileName, String sheetName,
Class<T> classType, ExcelTypeEnum excelTypeEnum) Class<T> classType, ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{ throws ExcelPluginException{
@ -141,7 +137,7 @@ public final class ExcelUtil {
/** /**
* WriterOutputStream * WriterOutputStream
*/ */
private static OutputStream getOutputStream(String fileName, HttpServletResponse response, private OutputStream getOutputStream(String fileName, HttpServletResponse response,
ExcelTypeEnum excelTypeEnum) ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{ throws ExcelPluginException{
//创建本地文件 //创建本地文件
@ -161,7 +157,7 @@ public final class ExcelUtil {
* @param excel Excel * @param excel Excel
* @param excelListener new ExcelListener() * @param excelListener new ExcelListener()
*/ */
private static ExcelReader getReader(MultipartFile excel, private ExcelReader getReader(MultipartFile excel,
ExcelListener excelListener) throws ExcelPluginException{ ExcelListener excelListener) throws ExcelPluginException{
String fileName = excel.getOriginalFilename(); String fileName = excel.getOriginalFilename();
if (fileName == null ) { if (fileName == null ) {
@ -187,7 +183,7 @@ public final class ExcelUtil {
/** /**
* BeanCopylist * BeanCopylist
*/ */
public static <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){ public <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){
return WrapperUtil.cloneTransformInstance(list,typeClazz); return WrapperUtil.cloneTransformInstance(list,typeClazz);
} }
} }
Loading…
Cancel
Save