From f0e477803472d8bb3f8d6a83a46bfdbbdc105a51 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Sun, 22 Dec 2024 22:34:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=B9=A6=E7=B1=8D=E7=9A=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/modules.xml | 1 + .idea/modules/bookstore.test.iml | 9 +++ .../lanyuanxiaoyao/bookstore/BookImport.kt | 63 +++++++++++++++++++ .../bookstore/ChapterDescription.kt | 30 +++++++++ 4 files changed, 103 insertions(+) create mode 100644 .idea/modules/bookstore.test.iml create mode 100644 src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt create mode 100644 src/test/kotlin/com/lanyuanxiaoyao/bookstore/ChapterDescription.kt 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