1
0

fix: 优化重排序操作

This commit is contained in:
2025-09-28 23:05:05 +08:00
parent 0a93e1d7ad
commit 026c72c4de
11 changed files with 94 additions and 17 deletions

12
.idea/dataSources.xml generated
View File

@@ -13,5 +13,17 @@
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="bookstore@mysql.lanyuanxiaoyao.com" uuid="7f28761d-62cd-4d02-9327-17e107b432ac">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://mysql.lanyuanxiaoyao.com:43780/bookstore</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

6
.idea/data_source_mapping.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourcePerFileMappings">
<file url="file://$APPLICATION_CONFIG_DIR$/consoles/db/7f28761d-62cd-4d02-9327-17e107b432ac/console.sql" value="7f28761d-62cd-4d02-9327-17e107b432ac" />
</component>
</project>

View File

@@ -62,7 +62,7 @@ function Chapter() {
actionType: 'ajax',
tooltip: '序号重排',
tooltipPlacement: 'top',
api: `get:${commonInfo.baseUrl}/line/generate_sequence`
api: `get:${commonInfo.baseUrl}/line/generate_sequence/${id}`
}
]
),

View File

@@ -1,10 +1,23 @@
package com.lanyuanxiaoyao.bookstore;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.blinkfox.fenix.EnableFenix;
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
import com.lanyuanxiaoyao.bookstore.entity.Line;
import com.lanyuanxiaoyao.bookstore.service.BookService;
import com.lanyuanxiaoyao.bookstore.service.ChapterService;
import jakarta.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.stream.Collectors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.transaction.annotation.Transactional;
/**
* 启动类
@@ -20,4 +33,44 @@ public class BookStoreApplication {
public static void main(String[] args) {
SpringApplication.run(BookStoreApplication.class, args);
}
@Resource
private BookService bookService;
@Resource
private ChapterService chapterService;
@Transactional(rollbackFor = Throwable.class)
// @EventListener(ApplicationReadyEvent.class)
public void loadOldData() throws FileNotFoundException {
var reader = CsvUtil.getReader(new FileReader("C:\\Users\\lanyuanxiaoyao\\Result_6.csv"));
var rows = reader.stream()
.map(row -> new Row(Long.parseLong(row.get(0)), row.get(1), Long.parseLong(row.get(2)), row.get(3)))
.toList();
var book = bookService.detailOrThrow(3602572744994816L);
rows.stream()
.map(row -> new Tuple(row.chapterSequence(), row.chapterTitle()))
.distinct()
.forEach(tuple -> {
var chapter = new Chapter();
chapter.setSequence(NumberUtil.toDouble(tuple.get(0)));
chapter.setName(tuple.get(1));
chapter.setBook(book);
var lines = rows.stream()
.filter(row -> ObjectUtil.equals(row.chapterSequence(), tuple.get(0)) && ObjectUtil.equals(row.chapterTitle(), tuple.get(1)))
.map(row -> {
var line = new Line();
line.setSequence(NumberUtil.toDouble(row.lineSequence()));
line.setText(row.lineText());
line.setChapter(chapter);
return line;
})
.collect(Collectors.toSet());
chapter.setContent(lines);
chapterService.save(chapter);
});
}
public record Row(long chapterSequence, String chapterTitle, long lineSequence, String lineText) {
}
}

View File

@@ -129,7 +129,7 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
public record DetailItem(
Long id,
Long sequence,
Double sequence,
String name,
String description,
LocalDateTime createdTime,

View File

@@ -7,6 +7,7 @@ import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -29,9 +30,9 @@ public class LineController extends SimpleControllerSupport<Line, LineController
return GlobalResponse.responseSuccess();
}
@GetMapping("generate_sequence")
public GlobalResponse<Object> generateSequence() {
lineService.generateSequence();
@GetMapping("generate_sequence/{chapter_id}")
public GlobalResponse<Object> generateSequence(@PathVariable("chapter_id") Long chapterId) {
lineService.generateSequence(chapterId);
return GlobalResponse.responseSuccess();
}
@@ -77,7 +78,7 @@ public class LineController extends SimpleControllerSupport<Line, LineController
public record DetailItem(
Long id,
Long sequence,
Double sequence,
String text
) {
}

View File

@@ -39,7 +39,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Table(name = Constants.DATABASE_PREFIX + "chapter")
public class Chapter extends SimpleEntity {
@Column(nullable = false)
private Long sequence;
private Double sequence;
@Column(nullable = false)
private String name;
private String description;

View File

@@ -39,7 +39,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Table(name = Constants.DATABASE_PREFIX + "line")
public class Line extends SimpleEntity {
@Column(nullable = false)
private Long sequence;
private Double sequence;
@Lob
@Basic(fetch = FetchType.LAZY)
@ToString.Exclude

View File

@@ -9,6 +9,6 @@ import org.springframework.stereotype.Repository;
@Repository
public interface ChapterRepository extends SimpleRepository<Chapter> {
@Query("select max(chapter.sequence) from Chapter chapter")
Optional<Long> findMaxSequence(Long bookId);
Optional<Double> findMaxSequence(Long bookId);
}

View File

@@ -1,5 +1,6 @@
package com.lanyuanxiaoyao.bookstore.service;
import cn.hutool.core.util.NumberUtil;
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
import com.lanyuanxiaoyao.bookstore.entity.vo.Option;
import com.lanyuanxiaoyao.bookstore.repository.ChapterRepository;
@@ -26,14 +27,14 @@ public class ChapterService extends SimpleServiceSupport<Chapter> {
.toList();
}
public Long latestSequence(Long bookId) {
return chapterRepository.findMaxSequence(bookId).orElse(0L);
public Double latestSequence(Long bookId) {
return chapterRepository.findMaxSequence(bookId).orElse(0.0);
}
public void generateSequence() {
var chapters = chapterRepository.findAll(Sort.by(Sort.Direction.ASC, Chapter.Fields.sequence));
for (int index = 0; index < chapters.size(); index++) {
chapters.get(index).setSequence((long) index);
chapters.get(index).setSequence(NumberUtil.toDouble(index));
}
chapterRepository.saveAll(chapters);
}

View File

@@ -1,14 +1,15 @@
package com.lanyuanxiaoyao.bookstore.service;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.bookstore.entity.Line;
import com.lanyuanxiaoyao.bookstore.entity.QLine;
import com.lanyuanxiaoyao.bookstore.repository.LineRepository;
import com.lanyuanxiaoyao.service.template.entity.IdOnlyEntity;
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -34,7 +35,7 @@ public class LineService extends SimpleServiceSupport<Line> {
.toList();
for (int index = 0; index < lines.size(); index++) {
var line = new Line();
line.setSequence((long) index);
line.setSequence(NumberUtil.toDouble(index));
line.setText(lines.get(index));
line.setChapter(chapter);
lineRepository.save(line);
@@ -47,10 +48,13 @@ public class LineService extends SimpleServiceSupport<Line> {
}
@Transactional(rollbackFor = Throwable.class)
public void generateSequence() {
var lines = lineRepository.findAll(Sort.by(Sort.Direction.ASC, Line.Fields.sequence));
public void generateSequence(Long chapterId) {
var lines = lineRepository.findAll(
QLine.line.chapter().id.eq(chapterId),
QLine.line.sequence.asc()
);
for (int index = 0; index < lines.size(); index++) {
lines.get(index).setSequence((long) index);
lines.get(index).setSequence(NumberUtil.toDouble(index));
}
lineRepository.saveAll(lines);
}