diff --git a/ruoyi-modules/ruoyi-oa/src/main/java/org/dromara/oa/base/service/impl/TemplateVariableAssignServiceImpl.java b/ruoyi-modules/ruoyi-oa/src/main/java/org/dromara/oa/base/service/impl/TemplateVariableAssignServiceImpl.java index f87701de..73134016 100644 --- a/ruoyi-modules/ruoyi-oa/src/main/java/org/dromara/oa/base/service/impl/TemplateVariableAssignServiceImpl.java +++ b/ruoyi-modules/ruoyi-oa/src/main/java/org/dromara/oa/base/service/impl/TemplateVariableAssignServiceImpl.java @@ -1,6 +1,8 @@ package org.dromara.oa.base.service.impl; +import org.dromara.common.core.enums.FormatsType; import org.dromara.common.core.exception.ServiceException; +import org.dromara.common.core.utils.DateUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.oa.base.domain.bo.BaseTemplateVariableBo; import org.dromara.oa.base.domain.bo.TemplateVariableAssignRequest; @@ -18,8 +20,11 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.lang.reflect.Field; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -143,8 +148,7 @@ public class TemplateVariableAssignServiceImpl implements ITemplateVariableAssig } // 根据变量类型决定返回类型(4表示数组类型) - boolean shouldReturnArray = "4".equals(variable.getVarType()); - Object finalValue = convertValueToTargetType(value, shouldReturnArray); + Object finalValue = convertValueToTargetType(value, variable); // 构建返回对象 TemplateVariableAssignVo assignVo = new TemplateVariableAssignVo(); @@ -348,10 +352,11 @@ public class TemplateVariableAssignServiceImpl implements ITemplateVariableAssig * 将值转换为目标类型 * * @param value 原始值 - * @param shouldReturnArray 是否应该返回数组类型 * @return 转换后的值 */ - private Object convertValueToTargetType(Object value, boolean shouldReturnArray) { + private Object convertValueToTargetType(Object value, BaseTemplateVariableVo variable) { + String varType = variable != null ? variable.getVarType() : null; + boolean shouldReturnArray = "4".equals(varType); if (shouldReturnArray) { // 转换为数组类型 if (value instanceof List) { @@ -366,28 +371,77 @@ public class TemplateVariableAssignServiceImpl implements ITemplateVariableAssig } else { return new ArrayList<>(); } + } + + if ("3".equals(varType)) { + return formatDateValue(value); + } + + return convertScalarValueToString(value); + } + + /** + * 将标量值转换为字符串 + * + * @param value 原始值 + * @return 字符串表示 + */ + private String convertScalarValueToString(Object value) { + if (value instanceof Collection) { + Collection collection = (Collection) value; + if (!collection.isEmpty()) { + Object first = collection.iterator().next(); + return first != null ? first.toString() : ""; + } + return ""; + } else if (value != null) { + return value.toString(); } else { - // 转换为字符串类型 - if (value instanceof List) { - List list = (List) value; - if (!list.isEmpty()) { - return list.get(0).toString(); - } else { - return ""; - } - } else if (value instanceof Collection) { - Collection collection = (Collection) value; - if (!collection.isEmpty()) { - return collection.iterator().next().toString(); - } else { - return ""; - } - } else if (value != null) { - return value.toString(); - } else { + return ""; + } + } + + /** + * 时间类型变量统一格式化 + * + * @param value 原始值 + * @return 格式化后的时间字符串 + */ + private String formatDateValue(Object value) { + if (value == null) { + return ""; + } + + if (value instanceof Collection) { + Collection collection = (Collection) value; + if (collection.isEmpty()) { return ""; } + return formatDateValue(collection.iterator().next()); } + + Date date = null; + if (value instanceof Date) { + date = (Date) value; + } else if (value instanceof LocalDateTime localDateTime) { + date = DateUtils.toDate(localDateTime); + } else if (value instanceof LocalDate localDate) { + date = DateUtils.toDate(localDate); + } else if (value instanceof Number number) { + long time = number.longValue(); + if (String.valueOf(time).length() == 10) { + time *= 1000; + } + date = new Date(time); + } else { + date = DateUtils.parseDate(value); + } + + if (date != null) { + return DateUtils.parseDateToStr(FormatsType.YYYY_MM_DD_DOT, date); + } + + return value.toString(); } /**