template sort

This commit is contained in:
2025-07-27 11:06:33 +08:00
parent 74a8953c8f
commit 4d2a962bc6
4 changed files with 44 additions and 2 deletions

View File

@@ -28,4 +28,6 @@ public interface TemplateService {
void saveConfig(Long configId, TemplateConfigEntity config);
ApiResponse<Boolean> sortTemplate(Long templateId, Long afterTemplateId);
ApiResponse<Boolean> updateSort(Long templateId, Integer sort);
}

View File

@@ -52,13 +52,28 @@ public class TemplateServiceImpl implements TemplateService {
@Override
public ApiResponse<Boolean> add(TemplateEntity template) {
template.setId(SnowFlakeUtil.getLongId());
// 如果sort为空,设置默认值
if (template.getSort() == null) {
// 获取当前景区下模板的最大sort值,然后+1
List<TemplateRespVO> existingTemplates = templateRepository.getAllTemplateListByScenicId(template.getScenicId());
int maxSort = existingTemplates.stream()
.mapToInt(t -> t.getSort() != null ? t.getSort() : 0)
.max()
.orElse(0);
template.setSort(maxSort + 1);
}
int i = templateMapper.add(template);
if (template.getChildren() != null) {
AtomicInteger childSort = new AtomicInteger(1);
template.getChildren().forEach(item -> {
item.setId(SnowFlakeUtil.getLongId());
item.setPid(template.getId());
item.setScenicId(template.getScenicId());
item.setStatus(1);
// 为子模板设置sort值
if (item.getSort() == null) {
item.setSort(childSort.getAndIncrement());
}
templateMapper.add(item);
});
}
@@ -87,11 +102,16 @@ public class TemplateServiceImpl implements TemplateService {
int i = templateMapper.update(template);
if (template.getChildren() != null) {
templateMapper.deleteByPid(template.getId());
AtomicInteger childSort = new AtomicInteger(1);
template.getChildren().forEach(item -> {
item.setId(SnowFlakeUtil.getLongId());
item.setPid(template.getId());
item.setScenicId(template.getScenicId());
item.setStatus(1);
// 为子模板设置sort值
if (item.getSort() == null) {
item.setSort(childSort.getAndIncrement());
}
templateMapper.add(item);
});
}
@@ -156,4 +176,15 @@ public class TemplateServiceImpl implements TemplateService {
});
return ApiResponse.success(true);
}
@Override
public ApiResponse<Boolean> updateSort(Long templateId, Integer sort) {
int i = templateMapper.updateSort(templateId, sort);
templateRepository.clearTemplateCache(templateId);
if (i > 0) {
return ApiResponse.success(true);
} else {
return ApiResponse.fail("更新模版排序失败");
}
}
}