fix: 优化逻辑
This commit is contained in:
parent
c24c4e8645
commit
a3eac3a4ad
@ -1,6 +1,7 @@
|
|||||||
package com.yfd.platform.qgc_base.controller;
|
package com.yfd.platform.qgc_base.controller;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.yfd.platform.annotation.Log;
|
import com.yfd.platform.annotation.Log;
|
||||||
import com.yfd.platform.common.DataSourceRequest;
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
import com.yfd.platform.config.ResponseResult;
|
import com.yfd.platform.config.ResponseResult;
|
||||||
@ -24,6 +25,9 @@ import jakarta.annotation.Resource;
|
|||||||
@Tag(name = "电站管理")
|
@Tag(name = "电站管理")
|
||||||
public class SdEngInfoBHController {
|
public class SdEngInfoBHController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ISdEngInfoBHService engInfoBHService;
|
private ISdEngInfoBHService engInfoBHService;
|
||||||
|
|
||||||
@ -93,14 +97,17 @@ public class SdEngInfoBHController {
|
|||||||
@PostMapping("/add")
|
@PostMapping("/add")
|
||||||
@Operation(summary = "新增电站")
|
@Operation(summary = "新增电站")
|
||||||
public ResponseResult add(@RequestBody SdEngInfoBHOperateRequest request) {
|
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("新增失败");
|
return result ? ResponseResult.success("新增成功") : ResponseResult.error("新增失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Log(module = "电站管理", value = "修改电站")
|
@Log(module = "电站管理", value = "修改电站")
|
||||||
@PostMapping("/update")
|
@PostMapping("/update")
|
||||||
@Operation(summary = "修改电站")
|
@Operation(summary = "修改电站")
|
||||||
public ResponseResult update(@RequestBody SdEngInfoBHOperateRequest request) {
|
public ResponseResult update(@RequestBody SdEngInfoBHOperateRequest request) throws Exception {
|
||||||
boolean result = engInfoBHService.updateEngInfo(request.getEngInfo(), request.getSource());
|
boolean result = engInfoBHService.updateEngInfo(request.getEngInfo(), request.getSource());
|
||||||
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
|
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,12 @@ package com.yfd.platform.qgc_base.domain;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class SdEngInfoBHOperateRequest {
|
public class SdEngInfoBHOperateRequest {
|
||||||
|
|
||||||
private SdEngInfoBH engInfo;
|
private Map<String, Object> engInfo;
|
||||||
|
|
||||||
private String source;
|
private String source;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import com.yfd.platform.qgc_base.domain.vo.EngStInfoResultVo;
|
|||||||
import com.yfd.platform.qgc_base.domain.vo.EngVmsstbprptVo;
|
import com.yfd.platform.qgc_base.domain.vo.EngVmsstbprptVo;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,6 +63,8 @@ public interface ISdEngInfoBHService extends IService<SdEngInfoBH> {
|
|||||||
|
|
||||||
boolean updateEngInfo(SdEngInfoBH engInfo, String source);
|
boolean updateEngInfo(SdEngInfoBH engInfo, String source);
|
||||||
|
|
||||||
|
boolean updateEngInfo(Map<String, Object> engInfoPatch, String source) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除电站
|
* 删除电站
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
package com.yfd.platform.qgc_base.service.impl;
|
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.date.DateUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
@ -308,12 +306,10 @@ public class MsOperationLogServiceImpl implements IMsOperationLogService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void recordEngUpdateLog(SdEngInfoBH before, SdEngInfoBH changeSet, String source) {
|
public void recordEngUpdateLog(SdEngInfoBH before, SdEngInfoBH after, String source) {
|
||||||
if (before == null || changeSet == null) {
|
if (before == null || after == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SdEngInfoBH after = BeanUtil.copyProperties(before, SdEngInfoBH.class);
|
|
||||||
BeanUtil.copyProperties(changeSet, after, CopyOptions.create().ignoreNullValue());
|
|
||||||
|
|
||||||
MsOperationLog mainLog = buildMainLog("修改电站", source);
|
MsOperationLog mainLog = buildMainLog("修改电站", source);
|
||||||
msOperationLogMapper.insert(mainLog);
|
msOperationLogMapper.insert(mainLog);
|
||||||
|
|||||||
@ -4,9 +4,15 @@ import cn.hutool.core.bean.BeanUtil;
|
|||||||
import cn.hutool.core.bean.copier.CopyOptions;
|
import cn.hutool.core.bean.copier.CopyOptions;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
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.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
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.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.yfd.platform.common.DataSourceLoadOptionsBase;
|
import com.yfd.platform.common.DataSourceLoadOptionsBase;
|
||||||
import com.yfd.platform.common.DataSourceRequest;
|
import com.yfd.platform.common.DataSourceRequest;
|
||||||
@ -33,6 +39,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -51,6 +58,19 @@ import java.util.stream.Collectors;
|
|||||||
@Service
|
@Service
|
||||||
public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEngInfoBH> implements ISdEngInfoBHService {
|
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
|
@Resource
|
||||||
private SdEngInfoBHMapper engInfoBHMapper;
|
private SdEngInfoBHMapper engInfoBHMapper;
|
||||||
|
|
||||||
@ -63,6 +83,9 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
|
|||||||
@Resource
|
@Resource
|
||||||
private IMsOperationLogService msOperationLogService;
|
private IMsOperationLogService msOperationLogService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<SdEngInfoBH> queryPageList(Page<SdEngInfoBH> page, String ennm, String rvcd, String baseId, String hycd) {
|
public Page<SdEngInfoBH> queryPageList(Page<SdEngInfoBH> page, String ennm, String rvcd, String baseId, String hycd) {
|
||||||
LambdaQueryWrapper<SdEngInfoBH> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SdEngInfoBH> wrapper = new LambdaQueryWrapper<>();
|
||||||
@ -232,6 +255,60 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
|
|||||||
return result;
|
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) {
|
private void fillRelatedNameFields(SdEngInfoBH engInfo) {
|
||||||
if (engInfo == null) {
|
if (engInfo == null) {
|
||||||
return;
|
return;
|
||||||
@ -354,6 +431,55 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
|
|||||||
return value == null ? null : String.valueOf(value);
|
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
|
@Override
|
||||||
public boolean deleteEngInfo(String stcd) {
|
public boolean deleteEngInfo(String stcd) {
|
||||||
return this.update( new LambdaUpdateWrapper<SdEngInfoBH>().eq(SdEngInfoBH::getStcd, stcd).set(SdEngInfoBH::getIsDeleted, 1));
|
return this.update( new LambdaUpdateWrapper<SdEngInfoBH>().eq(SdEngInfoBH::getStcd, stcd).set(SdEngInfoBH::getIsDeleted, 1));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user