From 5034ebd1f709fbc57928185a466db0c4b8384007 Mon Sep 17 00:00:00 2001 From: tangwei Date: Tue, 28 Jul 2026 18:56:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/IMsOperationLogService.java | 16 +++++ .../impl/MsOperationLogServiceImpl.java | 59 +++++++++++++++++++ .../service/impl/SdDwBHServiceImpl.java | 2 +- .../service/impl/SdWeBHServiceImpl.java | 4 +- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/IMsOperationLogService.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/IMsOperationLogService.java index f22e59c7..b22736f0 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_base/service/IMsOperationLogService.java +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/IMsOperationLogService.java @@ -6,6 +6,7 @@ import com.yfd.platform.common.DataSourceRequest; import com.yfd.platform.qgc_base.domain.MsOperationLog; import com.yfd.platform.qgc_base.domain.MsOperationLogDetail; import com.yfd.platform.qgc_base.domain.SdEngInfoBH; +import com.yfd.platform.utils.CodeToNameMetadataBo; import java.util.List; @@ -53,6 +54,21 @@ public interface IMsOperationLogService extends IService { */ void recordAddDetailLog(String recordId,String tableName, T entity, String source); + /** + * 通用新增操作详情日志(含字段级详情,支持代码-名称映射) + *

+ * 与 {@link #recordAddDetailLog(String, String, Object, String)} 功能相同, + * 额外支持将实体中 code 字段对应的 name 字段值填充到 newValueName。 + *

+ * @param recordId 记录ID(stcd) + * @param tableName 表名 + * @param entity 新增的实体 + * @param source 操作来源 + * @param codeToNameMetaList 代码-名称映射元数据列表(可为null) + * @param 实体类型 + */ + void recordAddDetailLog(String recordId, String tableName, T entity, String source, List codeToNameMetaList); + /** * 通用删除操作详情日志(含字段级详情) *

diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/MsOperationLogServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/MsOperationLogServiceImpl.java index 3ed63af0..daa36dfb 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/MsOperationLogServiceImpl.java +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/MsOperationLogServiceImpl.java @@ -15,6 +15,7 @@ import com.yfd.platform.qgc_base.domain.SdEngInfoBH; import com.yfd.platform.qgc_base.mapper.MsOperationLogDetailMapper; import com.yfd.platform.qgc_base.mapper.MsOperationLogMapper; import com.yfd.platform.qgc_base.service.IMsOperationLogService; +import com.yfd.platform.utils.CodeToNameMetadataBo; import com.yfd.platform.utils.DataSourceRequestUtil; import com.yfd.platform.utils.SecurityUtils; import jakarta.annotation.Resource; @@ -471,6 +472,64 @@ public class MsOperationLogServiceImpl extends ServiceImpl void recordAddDetailLog(String recordId, String tableName, T entity, String source, List codeToNameMetaList) { + if (entity == null) { + return; + } + // 构建 codeFieldName -> nameFieldName 映射 + Map codeToNameFieldMap = new HashMap<>(); + if (codeToNameMetaList != null) { + for (CodeToNameMetadataBo meta : codeToNameMetaList) { + if (StrUtil.isNotBlank(meta.getCodeProperty()) && StrUtil.isNotBlank(meta.getModifyProperty())) { + codeToNameFieldMap.put(meta.getCodeProperty(), meta.getModifyProperty()); + } + } + } + + MsOperationLog mainLog = buildMainLog(tableName, recordId, "新增", source); + msOperationLogMapper.insert(mainLog); + + List details = new ArrayList<>(); + Set processedColumns = new HashSet<>(); + Class entityClass = entity.getClass(); + + for (Field field : entityClass.getDeclaredFields()) { + if (shouldSkipField(field)) { + continue; + } + field.setAccessible(true); + String columnName = resolveColumnName(field); + if (processedColumns.contains(columnName)) { + continue; + } + String fieldMeaning = resolveFieldChinese(field); + Object newValue = getFieldValue(field, entity); + + MsOperationLogDetail detail = buildGenericDetail(mainLog.getId(), tableName, recordId, columnName, + fieldMeaning, null, newValue, "新增字段值"); + + // 如果当前字段是 code 字段,且存在对应的 name 字段映射,则填充 newValueName + String nameFieldName = codeToNameFieldMap.get(field.getName()); + if (nameFieldName != null) { + try { + Field nameField = entityClass.getDeclaredField(nameFieldName); + nameField.setAccessible(true); + Object nameValue = nameField.get(entity); + if (nameValue != null) { + detail.setNewValueName(formatValue(nameValue)); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + // 忽略找不到 name 字段的情况 + } + } + + details.add(detail); + processedColumns.add(columnName); + } + batchInsertDetails(details); + } + @Override public void recordDeleteDetailLog(String recordId,String tableName, T entity, String source) { if (entity == null) { diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdDwBHServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdDwBHServiceImpl.java index 32bf57be..e52ae145 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdDwBHServiceImpl.java +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdDwBHServiceImpl.java @@ -68,7 +68,7 @@ public class SdDwBHServiceImpl extends ServiceImpl impleme public boolean add(SdDwBH entity, String source) { boolean result = this.save(entity); if (result) { - msOperationLogService.recordAddDetailLog(entity.getStcd(),TABLE_NAME, entity, source); + msOperationLogService.recordAddDetailLog(entity.getStcd(), TABLE_NAME, entity, source, CODE_TO_NAME_META_LIST); } return result; } diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdWeBHServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdWeBHServiceImpl.java index e5936adf..3ed4c0bd 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdWeBHServiceImpl.java +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdWeBHServiceImpl.java @@ -68,7 +68,8 @@ public class SdWeBHServiceImpl extends ServiceImpl impleme public boolean add(SdWeBH entity, String source) { boolean result = this.save(entity); if (result) { - msOperationLogService.recordAddDetailLog(entity.getStcd(),TABLE_NAME, entity, source); } + msOperationLogService.recordAddDetailLog(entity.getStcd(), TABLE_NAME, entity, source, CODE_TO_NAME_META_LIST); + } return result; } @@ -76,6 +77,7 @@ public class SdWeBHServiceImpl extends ServiceImpl impleme @Transactional(rollbackFor = Exception.class) public boolean update(SdWeBH entity, String source) { SdWeBH before = this.getById(entity.getStcd()); + dictCodeToNameConverter.convertCodeToName(before, CODE_TO_NAME_META_LIST); boolean result = this.updateById(entity); if (result && before != null) { msOperationLogService.recordModifyDetailLog(entity.getStcd(),TABLE_NAME, before, entity, source);