diff --git a/.idea/modules.xml b/.idea/modules.xml index 0451ca2..52999ef 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/.idea/modules/bookstore.test.iml b/.idea/modules/bookstore.test.iml new file mode 100644 index 0000000..b12bc7b --- /dev/null +++ b/.idea/modules/bookstore.test.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt b/src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt new file mode 100644 index 0000000..4e27d5c --- /dev/null +++ b/src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt @@ -0,0 +1,63 @@ +package com.lanyuanxiaoyao.bookstore + +import cn.hutool.core.util.IdUtil +import jakarta.annotation.Resource +import java.io.File +import org.junit.jupiter.api.Test +import org.slf4j.LoggerFactory +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ActiveProfiles + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("mysql-local") +class BookImport { + private val log = LoggerFactory.getLogger(javaClass) + + @Resource + private lateinit var bookRepository: BookRepository + + @Resource + private lateinit var chapterRepository: ChapterRepository + + @Resource + private lateinit var lineRepository: LineRepository + + @Test + fun run() { + val book = bookRepository.findById("149e22b4411f4112a209fd7d17725d44").orElseThrow() + + val bookFile = File("\\\\192.168.31.127\\home\\Drive\\books\\online\\H\\best\\超级淫乱系统.txt") + val lines = bookFile.readText().split(Regex("\\n\\s*\\n")) + var chapterIndex = 0 + var chapterName = "" + lines.forEachIndexed { index, line -> + if (index % 2 == 0) { + chapterIndex++ + chapterName = line.trim().split(Regex("\\s+"))[1] + } else { + val chapter = chapterRepository.save( + Chapter( + IdUtil.fastSimpleUUID(), + chapterIndex, + chapterName, + null, + book + ) + ) + val chapterLines = line.split("\n") + .map { it.trim() } + .mapIndexed { lineIndex, text -> + Line( + IdUtil.fastSimpleUUID(), + lineIndex.toLong(), + text, + null, + chapter + ) + } + lineRepository.saveAll(chapterLines) + println("$chapterIndex $chapterName ${chapterLines.size}") + } + } + } +} diff --git a/src/test/kotlin/com/lanyuanxiaoyao/bookstore/ChapterDescription.kt b/src/test/kotlin/com/lanyuanxiaoyao/bookstore/ChapterDescription.kt new file mode 100644 index 0000000..df77ec1 --- /dev/null +++ b/src/test/kotlin/com/lanyuanxiaoyao/bookstore/ChapterDescription.kt @@ -0,0 +1,30 @@ +package com.lanyuanxiaoyao.bookstore + +import jakarta.annotation.Resource +import jakarta.transaction.Transactional +import org.junit.jupiter.api.Test +import org.slf4j.LoggerFactory +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.data.domain.Sort +import org.springframework.test.context.ActiveProfiles + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("mysql-local") +class ChapterDescription { + private val log = LoggerFactory.getLogger(javaClass) + + @Resource + private lateinit var bookRepository: BookRepository + + @Resource + private lateinit var chapterRepository: ChapterRepository + + @Transactional + @Test + fun run() { + val chapters = chapterRepository.findAll(Sort.by(Sort.Direction.ASC, "sequence")) + val chapter = chapters[0] + val lines = chapter.content + lines.forEach { line -> log.info(line.text) } + } +} \ No newline at end of file