Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当字典的值比较多时,使用HashMap方式效率明显高于字符串数组 #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jeecgframework.dict.service;


import java.util.HashMap;

/**
* 描述:
* @author:scott
* @since:2024-09-09
* @version:1.0
*/
public interface AutoPoiDictMapServiceI {
/**
* 方法描述: 查询数据字典优化
* 作 者: TestNet
* @param dicTable
* @param dicCode
* @param dicText
* @return
* 返回类型: HashMap<key,value>
*/
public HashMap<String,String> queryDict(String dicTable, String dicCode, String dicText, boolean isKeyValue);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jeecgframework.poi.excel.entity.params;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -79,6 +80,17 @@ public class ExcelBaseEntity {

private List<Method> methods;

private HashMap<String,String> replaceMap;


public HashMap<String, String> getReplaceMap() {
return replaceMap;
}

public void setReplaceMap(HashMap<String, String> replaceMap) {
this.replaceMap = replaceMap;
}

public String getDatabaseFormat() {
return databaseFormat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.dict.service.AutoPoiDictMapServiceI;
import org.jeecgframework.dict.service.AutoPoiDictServiceI;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;
Expand Down Expand Up @@ -227,16 +228,16 @@ public Object getCellValue(ExcelExportEntity entity, Object obj) throws Exceptio
if (StringUtils.isNotEmpty(entity.getFormat())) {
value = formatValue(value, entity);
}
if (entity.getReplace() != null && entity.getReplace().length > 0) {
if (entity.getReplaceMap() != null && !entity.getReplaceMap().isEmpty()) {
//update-begin-author:taoyan date:20180731 for:TASK #3038 【bug】Excel 导出多个值(逗号隔开的情况下,导出字典值是ID值)
if(value == null){
value = "";//String.valueOf(value) 如果value为null 则返回"null"
}
String oldVal=value.toString();
if(entity.isMultiReplace()){
value = multiReplaceValue(entity.getReplace(), String.valueOf(value));
value = multiReplaceValueByHashMap(entity.getReplaceMap(), String.valueOf(value));
}else{
value = replaceValue(entity.getReplace(), String.valueOf(value));
value = replaceValueByHashMap(entity.getReplaceMap(), String.valueOf(value));
}
//update-end-author:taoyan date:20180731 for:TASK #3038 【bug】Excel 导出多个值(逗号隔开的情况下,导出字典值是ID值)

Expand Down Expand Up @@ -300,15 +301,15 @@ private void getExcelField(String targetId, Field field, ExcelExportEntity excel
excelEntity.setReplace(excel.replace());
excelEntity.setHyperlink(excel.isHyperlink());
if(StringUtils.isNotEmpty(excel.dicCode())){
AutoPoiDictServiceI jeecgDictService = null;
AutoPoiDictMapServiceI jeecgDictService = null;
try {
jeecgDictService = ApplicationContextUtil.getContext().getBean(AutoPoiDictServiceI.class);
jeecgDictService = ApplicationContextUtil.getContext().getBean(AutoPoiDictMapServiceI.class);
} catch (Exception e) {
}
if(jeecgDictService!=null){
String[] dictReplace = jeecgDictService.queryDict(excel.dictTable(), excel.dicCode(), excel.dicText());
if(excelEntity.getReplace()!=null && dictReplace!=null && dictReplace.length!=0){
excelEntity.setReplace(dictReplace);
HashMap<String,String> dictMap = jeecgDictService.queryDict(excel.dictTable(), excel.dicCode(), excel.dicText(),true);
if( dictMap!=null && !dictMap.isEmpty()){
excelEntity.setReplaceMap(dictMap);
}
}
}
Expand Down Expand Up @@ -432,7 +433,40 @@ private Object replaceValue(String[] replace, String value) {
}
return value;
}


/**
* 当字典的值比较多时,使用HashMap方式相比使用字符串分割方式更快
* @author TestNet
* @since 2025年1月01日
*/
private Object replaceValueByHashMap(HashMap<String,String> replace, String key) {
return replace.get(key);
}

/**
* 当字典的值比较多时,使用HashMap方式相比使用字符串分割方式更快
* 如果需要被替换的值是多选项,则每一项之间有逗号隔开,走以下方法
* @author TestNet
* @since 2025年1月01日
*/
private Object multiReplaceValueByHashMap(HashMap<String,String> replace, String value) {
if(value.indexOf(",")>0){
String[] radioVals = value.split(",");
String[] temp;
String result = "";
for (String radioVal : radioVals) {
result = result.concat(replace.get(radioVal)) + ",";
}
if(result.equals("")){
result = value;
}else{
result = result.substring(0, result.length()-1);
}
return result;
}else{
return replaceValueByHashMap(replace, value);
}
}
//update-begin-author:taoyan date:20180731 for:TASK #3038 【bug】Excel 导出多个值(逗号隔开的情况下,导出字典值是ID值)
/**
* 如果需要被替换的值是多选项,则每一项之间有逗号隔开,走以下方法
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.dict.service.AutoPoiDictMapServiceI;
import org.jeecgframework.dict.service.AutoPoiDictServiceI;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;
Expand Down Expand Up @@ -81,16 +82,16 @@ public void addEntityToMap(String targetId, Field field, ExcelImportEntity excel
//update-begin-author:taoYan date:20180202 for:TASK #2067 【bug excel 问题】excel导入字典文本翻译问题
excelEntity.setMultiReplace(excel.multiReplace());
if(StringUtils.isNotEmpty(excel.dicCode())){
AutoPoiDictServiceI jeecgDictService = null;
AutoPoiDictMapServiceI jeecgDictService = null;
try {
jeecgDictService = ApplicationContextUtil.getContext().getBean(AutoPoiDictServiceI.class);
jeecgDictService = ApplicationContextUtil.getContext().getBean(AutoPoiDictMapServiceI.class);
} catch (Exception e) {
}
if(jeecgDictService!=null){
String[] dictReplace = jeecgDictService.queryDict(excel.dictTable(), excel.dicCode(), excel.dicText());
if(excelEntity.getReplace()!=null && dictReplace!=null && dictReplace.length!=0){
excelEntity.setReplace(dictReplace);
}
HashMap<String,String> dictReplace = jeecgDictService.queryDict(excel.dictTable(), excel.dicCode(), excel.dicText(),false);
if(dictReplace!=null && !dictReplace.isEmpty()){
excelEntity.setReplaceMap(dictReplace);
}
}
}
//update-end-author:taoYan date:20180202 for:TASK #2067 【bug excel 问题】excel导入字典文本翻译问题
Expand Down