Skip to content

Commit

Permalink
fix: Excel PLACEHOLDER
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Jun 7, 2023
1 parent 0dbea65 commit 3cc36a1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.rebuild.core.service.datareport;

import cn.devezhao.commons.CalendarUtils;
import cn.devezhao.commons.ObjectUtils;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.Field;
Expand Down Expand Up @@ -56,9 +57,13 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.rebuild.core.service.datareport.TemplateExtractor.APPROVAL_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.NROW_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTBIZUNIT;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTDATE;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTDATETIME;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTUSER;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__KEEP;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__NUMBER;
Expand All @@ -79,6 +84,7 @@ public class EasyExcelGenerator extends SetUser {

protected boolean hasMain = false;
protected int phNumber = 1;
protected Map<String, Object> phValues = new HashMap<>();

/**
* @param template
Expand Down Expand Up @@ -121,6 +127,11 @@ public File generate() {
// 主记录
if (main != null) {
excelWriter.fill(main, writeSheet);
} else {
// PH 变量
if (!phValues.isEmpty()) {
excelWriter.fill(phValues, writeSheet);
}
}

// 公式生效
Expand Down Expand Up @@ -251,7 +262,6 @@ protected Map<String, Object> buildData(Record record, Map<String, String> varsM

final String invalidFieldTip = Language.L("[无效字段]");
final String unsupportFieldTip = Language.L("[暂不支持]");
final String phCurrentuser = UserHelper.getName(getUser());

final Map<String, Object> data = new HashMap<>();

Expand All @@ -264,17 +274,10 @@ protected Map<String, Object> buildData(Record record, Map<String, String> varsM
varName = varName.substring(1);
}

// {.__KEEP:块}
if (varName.startsWith(PH__KEEP)) {
String phKeep = varName.length() > PH__KEEP.length()
? varName.substring(PH__KEEP.length() + 1) : "";
data.put(varName, phKeep);
} else if (varName.equalsIgnoreCase(PH__CURRENTUSER)) {
data.put(varName, phCurrentuser);
} else if (varName.equalsIgnoreCase(PH__NUMBER)) {
data.put(varName, phNumber);
}
else {
Object phValue = getPhValue(varName);
if (phValue != null) {
data.put(varName, phValue);
} else {
data.put(varName, invalidFieldTip);
}
}
Expand Down Expand Up @@ -400,6 +403,40 @@ private byte[] buildBarcodeData(Field barcodeField, ID recordId) {
return null;
}

/**
* @param phName
* @return
*/
protected Object getPhValue(String phName) {
if (!phName.contains("__")) return null;

// detail.__KEEP > __KEEP
phName = PLACEHOLDER + phName.split("__")[1];

// {.__KEEP:块}
if (phName.startsWith(PH__KEEP)) {
return phName.length() > PH__KEEP.length()
? phName.substring(PH__KEEP.length() + 1) : "";
}
// 列表序号
if (phName.equals(PH__NUMBER)) {
return phNumber;
}
if (phName.equals(PH__CURRENTUSER)) {
return UserHelper.getName(getUser());
}
if (phName.equals(PH__CURRENTBIZUNIT)) {
return Objects.requireNonNull(UserHelper.getDepartment(getUser())).getName();
}
if (phName.equals(PH__CURRENTDATE)) {
return CalendarUtils.getUTCDateFormat().format(CalendarUtils.now());
}
if (phName.equals(PH__CURRENTDATETIME)) {
return CalendarUtils.getUTCDateTimeFormat().format(CalendarUtils.now());
}
return null;
}

// --

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import static com.rebuild.core.service.datareport.TemplateExtractor.APPROVAL_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.NROW_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.PLACEHOLDER;
import static com.rebuild.core.service.datareport.TemplateExtractor33.DETAIL_PREFIX;

/**
Expand All @@ -47,9 +46,10 @@ protected List<Map<String, Object>> buildData() {
final Entity entity = MetadataHelper.getEntity(recordId.getEntityCode());
final Map<String, String> varsMap = new TemplateExtractor33(template).transformVars(entity);

// 变量
Map<String, String> varsMapOfMain = new HashMap<>();
Map<String, Map<String, String>> varsMapOfRefs = new HashMap<>();

// 字段
List<String> fieldsOfMain = new ArrayList<>();
Map<String, List<String>> fieldsOfRefs = new HashMap<>();

Expand Down Expand Up @@ -79,9 +79,7 @@ protected List<Map<String, Object>> buildData() {
}

// 占位字段
if (varName.startsWith(PLACEHOLDER) || varName.startsWith(NROW_PREFIX + PLACEHOLDER)) {
continue;
}
if (TemplateExtractor33.isPlaceholder(varName)) continue;
// 无效字段
if (fieldName == null) {
log.warn("Invalid field `{}` in template : {}", e.getKey(), template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.util.Map;

import static com.rebuild.core.service.datareport.TemplateExtractor.NROW_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTBIZUNIT;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTDATE;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTDATETIME;
import static com.rebuild.core.service.datareport.TemplateExtractor.PH__CURRENTUSER;
import static com.rebuild.core.service.datareport.TemplateExtractor.PLACEHOLDER;

/**
Expand Down Expand Up @@ -85,6 +89,12 @@ protected List<Map<String, Object>> buildData() {
datas.add(buildData(c, varsMap));
phNumber++;
}

if (varsMap.containsKey(PH__CURRENTUSER)) phValues.put(PH__CURRENTUSER, getPhValue(PH__CURRENTUSER));
if (varsMap.containsKey(PH__CURRENTBIZUNIT)) phValues.put(PH__CURRENTBIZUNIT, getPhValue(PH__CURRENTBIZUNIT));
if (varsMap.containsKey(PH__CURRENTDATE)) phValues.put(PH__CURRENTDATE, getPhValue(PH__CURRENTDATE));
if (varsMap.containsKey(PH__CURRENTDATETIME)) phValues.put(PH__CURRENTDATETIME, getPhValue(PH__CURRENTDATETIME));

return datas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ public class TemplateExtractor {
public static final String PLACEHOLDER = "__";
// 空
protected static final String PH__KEEP = PLACEHOLDER + "KEEP";
// 当前用户
protected static final String PH__CURRENTUSER = PLACEHOLDER + "CURRENTUSER";
// 序号
protected static final String PH__NUMBER = PLACEHOLDER + "NUMBER";

// 当前用户
protected static final String PH__CURRENTUSER = PLACEHOLDER + "CURRENTUSER";
// 当前部门
protected static final String PH__CURRENTBIZUNIT = PLACEHOLDER + "CURRENTBIZUNIT";
// 当前日期
protected static final String PH__CURRENTDATE = PLACEHOLDER + "CURRENTDATE";
// 当前日期时间
protected static final String PH__CURRENTDATETIME = PLACEHOLDER + "CURRENTDATETIME";

// v2:{xxx} v1:${xxx}
private static final Pattern PATT_V2 = Pattern.compile("\\{(.*?)}");

Expand Down Expand Up @@ -155,7 +161,7 @@ protected Set<String> extractVars() {
* @return
*/
protected String transformRealField(Entity entity, String fieldPath) {
if (fieldPath.startsWith(PLACEHOLDER)) return null;
if (TemplateExtractor33.isPlaceholder(fieldPath)) return null;

if (fieldPath.contains("$")) {
fieldPath = fieldPath.replace("$", ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public Map<String, String> transformVars(Entity entity) {
for (final String varName : vars) {
// 列表型字段
if (varName.startsWith(NROW_PREFIX)) {
String listField = varName.substring(1);
final String listField = varName.substring(1);

if (isPlaceholder(listField)) {
map.put(varName, null);
}
// 审批流程
if (varName.startsWith(APPROVAL_PREFIX)) {
else if (varName.startsWith(APPROVAL_PREFIX)) {
String stepNodeField = listField.substring(APPROVAL_PREFIX.length());
if (approvalEntity != null && MetadataHelper.getLastJoinField(approvalEntity, stepNodeField) != null) {
map.put(varName, stepNodeField);
Expand All @@ -76,8 +79,8 @@ else if (varName.startsWith(DETAIL_PREFIX)) {
else {
String[] split = listField.split("\\.");
String ref2Field = split[0];
String ref2Entity = split[1];
Field ref2FieldMeta = MetadataHelper.containsField(ref2Entity, ref2Field)
String ref2Entity = split.length > 1 ? split[1] : null;
Field ref2FieldMeta = ref2Entity != null && MetadataHelper.containsField(ref2Entity, ref2Field)
? MetadataHelper.getField(ref2Entity, ref2Field) : null;

if (ref2FieldMeta != null && entity.equals(ref2FieldMeta.getReferenceEntity())) {
Expand All @@ -95,4 +98,14 @@ else if (varName.startsWith(DETAIL_PREFIX)) {
}
return map;
}

/**
* 是否占位符
*
* @param varName
* @return
*/
public static boolean isPlaceholder(String varName) {
return varName.startsWith(PLACEHOLDER) || varName.contains(NROW_PREFIX + PLACEHOLDER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.Set;

import static com.rebuild.core.service.datareport.TemplateExtractor.NROW_PREFIX;
import static com.rebuild.core.service.datareport.TemplateExtractor.PLACEHOLDER;

/**
* Excel 报表
Expand Down Expand Up @@ -105,11 +104,8 @@ public RespBody checkTemplate(@EntityParam Entity entity, HttpServletRequest req

Set<String> invalidVars = new HashSet<>();
for (Map.Entry<String, String> e : vars.entrySet()) {
String varName = e.getKey();
if (e.getValue() == null) {
if (!(varName.startsWith(PLACEHOLDER) || varName.startsWith(NROW_PREFIX + PLACEHOLDER))) {
invalidVars.add(e.getKey());
}
if (e.getValue() == null && !TemplateExtractor33.isPlaceholder(e.getKey())) {
invalidVars.add(e.getKey());
}
}

Expand Down

0 comments on commit 3cc36a1

Please sign in to comment.