fix: 优化逻辑

This commit is contained in:
tangwei 2026-06-25 17:10:55 +08:00
parent c24c4e8645
commit a3eac3a4ad
5 changed files with 143 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package com.yfd.platform.qgc_base.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.common.DataSourceRequest;
import com.yfd.platform.config.ResponseResult;
@ -24,6 +25,9 @@ import jakarta.annotation.Resource;
@Tag(name = "电站管理")
public class SdEngInfoBHController {
@Resource
private ObjectMapper objectMapper;
@Resource
private ISdEngInfoBHService engInfoBHService;
@ -93,14 +97,17 @@ public class SdEngInfoBHController {
@PostMapping("/add")
@Operation(summary = "新增电站")
public ResponseResult add(@RequestBody SdEngInfoBHOperateRequest request) {
boolean result = engInfoBHService.addEngInfo(request.getEngInfo(), request.getSource());
SdEngInfoBH engInfo = request == null || request.getEngInfo() == null
? null
: objectMapper.convertValue(request.getEngInfo(), SdEngInfoBH.class);
boolean result = engInfoBHService.addEngInfo(engInfo, request.getSource());
return result ? ResponseResult.success("新增成功") : ResponseResult.error("新增失败");
}
@Log(module = "电站管理", value = "修改电站")
@PostMapping("/update")
@Operation(summary = "修改电站")
public ResponseResult update(@RequestBody SdEngInfoBHOperateRequest request) {
public ResponseResult update(@RequestBody SdEngInfoBHOperateRequest request) throws Exception {
boolean result = engInfoBHService.updateEngInfo(request.getEngInfo(), request.getSource());
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
}

View File

@ -2,10 +2,12 @@ package com.yfd.platform.qgc_base.domain;
import lombok.Data;
import java.util.Map;
@Data
public class SdEngInfoBHOperateRequest {
private SdEngInfoBH engInfo;
private Map<String, Object> engInfo;
private String source;
}

View File

@ -12,6 +12,7 @@ import com.yfd.platform.qgc_base.domain.vo.EngStInfoResultVo;
import com.yfd.platform.qgc_base.domain.vo.EngVmsstbprptVo;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@ -62,6 +63,8 @@ public interface ISdEngInfoBHService extends IService<SdEngInfoBH> {
boolean updateEngInfo(SdEngInfoBH engInfo, String source);
boolean updateEngInfo(Map<String, Object> engInfoPatch, String source) throws Exception;
/**
* 删除电站
*/

View File

@ -1,7 +1,5 @@
package com.yfd.platform.qgc_base.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
@ -308,12 +306,10 @@ public class MsOperationLogServiceImpl implements IMsOperationLogService {
}
@Override
public void recordEngUpdateLog(SdEngInfoBH before, SdEngInfoBH changeSet, String source) {
if (before == null || changeSet == null) {
public void recordEngUpdateLog(SdEngInfoBH before, SdEngInfoBH after, String source) {
if (before == null || after == null) {
return;
}
SdEngInfoBH after = BeanUtil.copyProperties(before, SdEngInfoBH.class);
BeanUtil.copyProperties(changeSet, after, CopyOptions.create().ignoreNullValue());
MsOperationLog mainLog = buildMainLog("修改电站", source);
msOperationLogMapper.insert(mainLog);

View File

@ -4,9 +4,15 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.common.DataSourceLoadOptionsBase;
import com.yfd.platform.common.DataSourceRequest;
@ -33,6 +39,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -51,6 +58,19 @@ import java.util.stream.Collectors;
@Service
public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEngInfoBH> implements ISdEngInfoBHService {
private static final Map<String, String> ENG_CODE_NAME_FIELD_MAP = new LinkedHashMap<>();
static {
ENG_CODE_NAME_FIELD_MAP.put("baseId", "baseName");
ENG_CODE_NAME_FIELD_MAP.put("hbrvcd", "hbrvcdName");
ENG_CODE_NAME_FIELD_MAP.put("rvcd", "rvcdName");
ENG_CODE_NAME_FIELD_MAP.put("addvcd", "addvcdName");
ENG_CODE_NAME_FIELD_MAP.put("country", "countryName");
ENG_CODE_NAME_FIELD_MAP.put("hycd", "hynm");
ENG_CODE_NAME_FIELD_MAP.put("topHycd", "topHynm");
ENG_CODE_NAME_FIELD_MAP.put("reachcd", "reachcdName");
}
@Resource
private SdEngInfoBHMapper engInfoBHMapper;
@ -63,6 +83,9 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
@Resource
private IMsOperationLogService msOperationLogService;
@Resource
private ObjectMapper objectMapper;
@Override
public Page<SdEngInfoBH> queryPageList(Page<SdEngInfoBH> page, String ennm, String rvcd, String baseId, String hycd) {
LambdaQueryWrapper<SdEngInfoBH> wrapper = new LambdaQueryWrapper<>();
@ -232,6 +255,60 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
return result;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateEngInfo(Map<String, Object> engInfoPatch, String source) throws JsonMappingException {
if (engInfoPatch == null || engInfoPatch.isEmpty()) {
return false;
}
String stcd = toTextValue(engInfoPatch.get("stcd"));
if (!StringUtils.hasText(stcd)) {
return false;
}
SdEngInfoBH before = this.getById(stcd);
if (before == null) {
return false;
}
SdEngInfoBH after = BeanUtil.copyProperties(before, SdEngInfoBH.class);
objectMapper.updateValue(after, engInfoPatch);
fillRelatedNameFields(after);
boolean result = updateEngInfoByPatch(stcd, after, engInfoPatch);
if (result) {
msOperationLogService.recordEngUpdateLog(before, after, source);
}
return result;
}
private boolean updateEngInfoByPatch(String stcd, SdEngInfoBH after, Map<String, Object> engInfoPatch) {
UpdateWrapper<SdEngInfoBH> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("STCD", stcd);
boolean hasUpdateField = false;
for (Field field : SdEngInfoBH.class.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
if ("serialVersionUID".equals(fieldName) || "stcd".equals(fieldName)) {
continue;
}
boolean explicitPatched = engInfoPatch.containsKey(fieldName);
boolean linkedNamePatched = isLinkedNameFieldPatched(fieldName, engInfoPatch);
if (!explicitPatched && !linkedNamePatched) {
continue;
}
String resolveColumnName = resolveColumnName(field);
Object fieldValue = getFieldValue(field, after);
if (fieldValue == null) {
updateWrapper.setSql(resolveColumnName + " = NULL");
} else {
updateWrapper.set(resolveColumnName, fieldValue);
}
hasUpdateField = true;
}
if (!hasUpdateField) {
return true;
}
return this.update(null, updateWrapper);
}
private void fillRelatedNameFields(SdEngInfoBH engInfo) {
if (engInfo == null) {
return;
@ -354,6 +431,55 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
return value == null ? null : String.valueOf(value);
}
private boolean isLinkedNameFieldPatched(String fieldName, Map<String, Object> engInfoPatch) {
for (Map.Entry<String, String> entry : ENG_CODE_NAME_FIELD_MAP.entrySet()) {
if (entry.getValue().equals(fieldName) && engInfoPatch.containsKey(entry.getKey())) {
return true;
}
}
return false;
}
private String resolveColumnName(Field field) {
TableId tableId = field.getAnnotation(TableId.class);
if (tableId != null) {
return camelToUpperUnderline(field.getName());
}
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null && StrUtil.isNotBlank(tableField.value())) {
return tableField.value();
}
return camelToUpperUnderline(field.getName());
}
private Object getFieldValue(Field field, Object target) {
try {
return field.get(target);
} catch (IllegalAccessException e) {
return null;
}
}
private String toTextValue(Object value) {
if (value == null) {
return null;
}
String text = String.valueOf(value);
return text.isBlank() ? null : text;
}
private String camelToUpperUnderline(String text) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (Character.isUpperCase(ch) && i > 0) {
builder.append('_');
}
builder.append(Character.toUpperCase(ch));
}
return builder.toString();
}
@Override
public boolean deleteEngInfo(String stcd) {
return this.update( new LambdaUpdateWrapper<SdEngInfoBH>().eq(SdEngInfoBH::getStcd, stcd).set(SdEngInfoBH::getIsDeleted, 1));