diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index a4d6df9..9d588ba 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -13,5 +13,17 @@ $ProjectFileDir$ + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://mysql.lanyuanxiaoyao.com:43780/bookstore + + + + + + $ProjectFileDir$ + \ No newline at end of file diff --git a/.idea/data_source_mapping.xml b/.idea/data_source_mapping.xml new file mode 100644 index 0000000..e31d277 --- /dev/null +++ b/.idea/data_source_mapping.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/client/src/pages/book/Chapter.tsx b/client/src/pages/book/Chapter.tsx index 36eb976..60be467 100644 --- a/client/src/pages/book/Chapter.tsx +++ b/client/src/pages/book/Chapter.tsx @@ -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}` } ] ), diff --git a/src/main/java/com/lanyuanxiaoyao/bookstore/BookStoreApplication.java b/src/main/java/com/lanyuanxiaoyao/bookstore/BookStoreApplication.java index 9383c59..df504be 100644 --- a/src/main/java/com/lanyuanxiaoyao/bookstore/BookStoreApplication.java +++ b/src/main/java/com/lanyuanxiaoyao/bookstore/BookStoreApplication.java @@ -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) { + } } diff --git a/src/main/java/com/lanyuanxiaoyao/bookstore/controller/ChapterController.java b/src/main/java/com/lanyuanxiaoyao/bookstore/controller/ChapterController.java index 47ebf6a..25ae242 100644 --- a/src/main/java/com/lanyuanxiaoyao/bookstore/controller/ChapterController.java +++ b/src/main/java/com/lanyuanxiaoyao/bookstore/controller/ChapterController.java @@ -129,7 +129,7 @@ public class ChapterController extends SimpleControllerSupport generateSequence() { - lineService.generateSequence(); + @GetMapping("generate_sequence/{chapter_id}") + public GlobalResponse generateSequence(@PathVariable("chapter_id") Long chapterId) { + lineService.generateSequence(chapterId); return GlobalResponse.responseSuccess(); } @@ -77,7 +78,7 @@ public class LineController extends SimpleControllerSupport { @Query("select max(chapter.sequence) from Chapter chapter") - Optional findMaxSequence(Long bookId); + Optional findMaxSequence(Long bookId); } diff --git a/src/main/java/com/lanyuanxiaoyao/bookstore/service/ChapterService.java b/src/main/java/com/lanyuanxiaoyao/bookstore/service/ChapterService.java index 2782160..3746571 100644 --- a/src/main/java/com/lanyuanxiaoyao/bookstore/service/ChapterService.java +++ b/src/main/java/com/lanyuanxiaoyao/bookstore/service/ChapterService.java @@ -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 { .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); } diff --git a/src/main/java/com/lanyuanxiaoyao/bookstore/service/LineService.java b/src/main/java/com/lanyuanxiaoyao/bookstore/service/LineService.java index 9145c49..0700ce8 100644 --- a/src/main/java/com/lanyuanxiaoyao/bookstore/service/LineService.java +++ b/src/main/java/com/lanyuanxiaoyao/bookstore/service/LineService.java @@ -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 { .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 { } @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); }