fix: 优化逻辑

This commit is contained in:
tangwei 2026-07-06 17:01:44 +08:00
parent c6dc532040
commit d30d873fa4
2 changed files with 142 additions and 12 deletions

View File

@ -162,18 +162,6 @@ public class MapLegendController {
if (legend.getDataType() == null) { if (legend.getDataType() == null) {
throw new BizException("图例数据类型(dataType: 1=结构,2=数据)不能为空."); throw new BizException("图例数据类型(dataType: 1=结构,2=数据)不能为空.");
} }
if (legend.getDataType() == 2 && StrUtil.isBlank(legend.getLayerCode())) {
throw new BizException("关联的图层编码(layerCode)不能为空.");
}
if (StrUtil.isNotBlank(legend.getName())){
if(StrUtil.isBlank(legend.getGroupName())){
legend.setGroupName(legend.getName());
}
if(StrUtil.isBlank(legend.getParentName())){
legend.setGroupName(legend.getName());
}
}
legend.setRecordUser(SecurityUtils.getUserId());
mapLegendService.saveOrUpdate(legend); mapLegendService.saveOrUpdate(legend);
return ResponseResult.success("保存成功."); return ResponseResult.success("保存成功.");
} }

View File

@ -11,10 +11,13 @@ import com.yfd.platform.qgc_sys.mapLayer.domain.MsMapmoduleB;
import com.yfd.platform.qgc_sys.mapLayer.mapper.MsMaplegendBMapper; import com.yfd.platform.qgc_sys.mapLayer.mapper.MsMaplegendBMapper;
import com.yfd.platform.qgc_sys.mapLayer.mapper.MsMapmoduleBMapper; import com.yfd.platform.qgc_sys.mapLayer.mapper.MsMapmoduleBMapper;
import com.yfd.platform.qgc_sys.mapLayer.service.IMapLegendService; import com.yfd.platform.qgc_sys.mapLayer.service.IMapLegendService;
import com.yfd.platform.utils.SecurityUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* 地图图例基础服务实现 * 地图图例基础服务实现
@ -94,4 +97,143 @@ public class MapLegendServiceImpl extends ServiceImpl<MsMaplegendBMapper, MsMapl
} }
} }
} }
@Override
public boolean saveOrUpdate(MsMaplegendB legend) {
if (hasSameCode(legend)) {
throw new BizException("存在相同编码的数据");
}
if (StrUtil.isNotBlank(legend.getId())) {
updateSetLegend(legend);
super.updateById(legend);
} else {
if (legend.getDataType() == 2 && StrUtil.isBlank(legend.getParentId())) {
throw new BizException("请先创建图例父级结构再添加数据.");
}
if (legend.getDataType() == 2 && StrUtil.isBlank(legend.getLayerCode())) {
throw new BizException("图层编码不能为空.");
}
List<MsMaplegendB> list = getSubLegendList(legend.getParentId());
Integer maxOrderIndex = getMaxOrderIndex(list);
legend.setOrderIndex(maxOrderIndex + 1);
MsMaplegendB parentLegend = null;
if (StrUtil.isNotBlank(legend.getParentId())) {
parentLegend = this.getById(legend.getParentId());
if (parentLegend == null) {
throw new BizException("获取父级图例信息失败.请检查父级图例(parentId)是否存在.");
}
legend.setFullPath(parentLegend.getFullPath() + legend.getId() + ",");
legend.setTreeLevel(parentLegend.getTreeLevel() + 1);
legend.setHasChildren(0);
} else {
legend.setTreeLevel(1);
legend.setFullPath(legend.getId() + ",");
legend.setHasChildren(0);
}
if (legend.getDataType() == 1) {
legend.setGroupName(legend.getName());
legend.setParentName(legend.getName());
} else {
String fullPath = parentLegend.getFullPath();
if (StrUtil.isNotBlank(fullPath)) {
String[] path = fullPath.split(",");
if (path.length == 1) {
legend.setGroupName(parentLegend.getName());
legend.setParentName(parentLegend.getName());
} else if (path.length == 2) {
legend.setGroupName(parentLegend.getName());
MsMaplegendB root = this.getById(path[0]);
if (root == null) {
legend.setParentName(parentLegend.getParentName());
} else {
legend.setParentName(root.getName());
}
}
}
}
if (parentLegend != null && StrUtil.isBlank(legend.getLayerCode())) {
legend.setLayerCode(parentLegend.getLayerCode());
}
legend.setIfShow(0);
legend.setInternal(0);
legend.setEnable(0);
legend.setRecordUser(SecurityUtils.getUserId());
legend.setModifyUser(SecurityUtils.getUserId());
this.save(legend);
if (parentLegend != null) {
MsMaplegendB temp = new MsMaplegendB();
temp.setId(parentLegend.getId());
temp.setHasChildren(1);
this.updateById(temp);
}
}
return true;
}
private void updateSetLegend(MsMaplegendB legend) {
if (StrUtil.isNotBlank(legend.getParentId())) {
MsMaplegendB parent = this.getById(legend.getParentId());
if (parent != null) {
if (legend.getDataType() == 1) {
legend.setParentName(parent.getName());
legend.setFullPath(parent.getFullPath() + legend.getId() + ",");
} else {
legend.setGroupName(parent.getName());
if (StrUtil.isNotBlank(parent.getFullPath())) {
MsMaplegendB root = this.getById(parent.getFullPath().split(",")[0]);
if (root != null) {
legend.setParentName(root.getName());
}
legend.setFullPath(parent.getFullPath() + legend.getId() + ",");
}
}
if (StrUtil.isNotBlank(legend.getFullPath())) {
legend.setTreeLevel(legend.getFullPath().split(",").length);
}
}
if (legend.getDataType() == 1) {
legend.setGroupName(legend.getName());
}
} else {
if (legend.getDataType() == 1) {
legend.setGroupName(legend.getName());
legend.setParentName(legend.getName());
}
legend.setFullPath(legend.getId() + ",");
legend.setTreeLevel(1);
}
}
public List<MsMaplegendB> getSubLegendList(String parentId) {
if (StrUtil.isNotBlank(parentId)) {
return this.lambdaQuery()
.eq(MsMaplegendB::getParentId, parentId)
.orderByAsc(MsMaplegendB::getOrderIndex)
.list();
} else {
return this.lambdaQuery()
.eq(MsMaplegendB::getTreeLevel, 1)
.orderByAsc(MsMaplegendB::getOrderIndex)
.list();
}
}
public Integer getMaxOrderIndex(List<MsMaplegendB> list) {
if (CollectionUtil.isEmpty(list)) {
return 0;
} else {
return list.stream()
.sorted(Comparator.comparing(MsMaplegendB::getOrderIndex).reversed())
.toList()
.getFirst()
.getOrderIndex();
}
}
public boolean hasSameCode(MsMaplegendB legend) {
List<MsMaplegendB> list = this.lambdaQuery()
.eq(MsMaplegendB::getNameEn, legend.getNameEn())
.list();
return !CollectionUtil.isEmpty(list) && !list.get(0).getId().equals(legend.getId());
}
} }