|
@@ -0,0 +1,172 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "eta/eta_api/models"
|
|
|
+ "eta/eta_api/models/rag"
|
|
|
+ "eta/eta_api/utils"
|
|
|
+ "fmt"
|
|
|
+ "golang.org/x/net/html"
|
|
|
+ "golang.org/x/net/html/atom"
|
|
|
+ "regexp"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// ReportAddOrModifyKnowledge
|
|
|
+// @Description: ETA报告加入/修改到知识库
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2025-04-07 14:41:45
|
|
|
+// @param reportId int
|
|
|
+// @param reportChapterId int
|
|
|
+func ReportAddOrModifyKnowledge(reportId, reportChapterId int) {
|
|
|
+ if reportId <= 0 && reportChapterId <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ //fmt.Println("ReportAddOrModifyKnowledge error:", err)
|
|
|
+ utils.FileLog.Error("ReportAddOrModifyKnowledge error:", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ var title, author, htmlContent string
|
|
|
+ var publishTime time.Time
|
|
|
+
|
|
|
+ if reportChapterId > 0 {
|
|
|
+ chapterInfo, tmpErr := models.GetReportChapterInfoById(reportChapterId)
|
|
|
+ if tmpErr != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ title = chapterInfo.Title
|
|
|
+ author = chapterInfo.Author
|
|
|
+ publishTime = chapterInfo.PublishTime
|
|
|
+ htmlContent = chapterInfo.Content
|
|
|
+ } else {
|
|
|
+ reportInfo, tmpErr := models.GetReportByReportId(reportId)
|
|
|
+ if tmpErr != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ title = reportInfo.Title
|
|
|
+ author = reportInfo.Author
|
|
|
+ publishTime = reportInfo.PublishTime
|
|
|
+ htmlContent = reportInfo.Content
|
|
|
+ }
|
|
|
+
|
|
|
+ htmlContent = html.UnescapeString(htmlContent)
|
|
|
+ doc, err := html.Parse(strings.NewReader(htmlContent))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 只获取文本内容
|
|
|
+ content := &strings.Builder{}
|
|
|
+ getArticleContent(content, doc)
|
|
|
+
|
|
|
+ textContent := content.String()
|
|
|
+ textContent = regexp.MustCompile(`\n+`).ReplaceAllString(textContent, "\n")
|
|
|
+ textContent = strings.Trim(textContent, "\n")
|
|
|
+
|
|
|
+ publishTimeStr := `未知`
|
|
|
+ if !publishTime.IsZero() {
|
|
|
+ title = fmt.Sprintf("%s(%s)", title, publishTime.Format(utils.FormatMonthDayUnSpace))
|
|
|
+ publishTimeStr = publishTime.Format(utils.FormatDateTime)
|
|
|
+ }
|
|
|
+
|
|
|
+ textContent = fmt.Sprintf("标题:%s\n发布时间:%s\n%s", title, publishTimeStr, textContent)
|
|
|
+
|
|
|
+ obj := rag.RagEtaReport{}
|
|
|
+ item, err := obj.GetByReportAndChapterId(reportId, reportChapterId)
|
|
|
+ if err != nil && !utils.IsErrNoRow(err) {
|
|
|
+ // 查询异常,且不是没找到数据的报错
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if err == nil {
|
|
|
+ // 标记删除了的话,那就不处理了
|
|
|
+ if item.IsDeleted == 1 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item.Title = title
|
|
|
+ item.Author = author
|
|
|
+ item.TextContent = textContent
|
|
|
+ item.PublishTime = publishTime
|
|
|
+ item.ModifyTime = time.Now()
|
|
|
+ err = item.Update([]string{"author", "text_content", "publish_time", "modify_time"})
|
|
|
+ } else {
|
|
|
+ // 无数据的时候,需要新增
|
|
|
+ err = nil
|
|
|
+ item = &rag.RagEtaReport{
|
|
|
+ RagEtaReportId: 0,
|
|
|
+ ReportId: reportId,
|
|
|
+ ReportChapterId: reportChapterId,
|
|
|
+ Title: title,
|
|
|
+ Author: author,
|
|
|
+ TextContent: textContent,
|
|
|
+ VectorKey: "",
|
|
|
+ IsPublished: 1,
|
|
|
+ IsDeleted: 0,
|
|
|
+ PublishTime: publishTime,
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ }
|
|
|
+ err = item.Create()
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// ReportUnPublishedKnowledge
|
|
|
+// @Description: 知识库取消发布
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2025-04-07 14:58:25
|
|
|
+// @param reportId int
|
|
|
+// @param reportChapterId int
|
|
|
+func ReportUnPublishedKnowledge(reportId, reportChapterId int) {
|
|
|
+ if reportId <= 0 && reportChapterId <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ //fmt.Println("ReportAddOrModifyKnowledge error:", err)
|
|
|
+ utils.FileLog.Error("ReportAddOrModifyKnowledge error:", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ obj := rag.RagEtaReport{}
|
|
|
+ item, err := obj.GetByReportAndChapterId(reportId, reportChapterId)
|
|
|
+ if err != nil && !utils.IsErrNoRow(err) {
|
|
|
+ // 查询异常,且不是没找到数据的报错
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if item.RagEtaReportId > 0 {
|
|
|
+ item.IsPublished = 0
|
|
|
+ item.ModifyTime = time.Now()
|
|
|
+ err = item.Update([]string{"is_published", "modify_time"})
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getArticleContent(content *strings.Builder, htmlContentNode *html.Node) {
|
|
|
+ if htmlContentNode.Type == html.TextNode {
|
|
|
+ cleanData := strings.TrimSpace(htmlContentNode.Data)
|
|
|
+ if cleanData != `` && cleanData != "</p>" {
|
|
|
+ content.WriteString(cleanData)
|
|
|
+ }
|
|
|
+ } else if htmlContentNode.Type == html.ElementNode {
|
|
|
+ switch htmlContentNode.DataAtom {
|
|
|
+ case atom.Ul:
|
|
|
+ content.WriteString("\n")
|
|
|
+ case atom.Br:
|
|
|
+ // 遇到 <br> 标签时添加换行符
|
|
|
+ content.WriteString("\n")
|
|
|
+ case atom.P:
|
|
|
+ content.WriteString("\n")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for c := htmlContentNode.FirstChild; c != nil; c = c.NextSibling {
|
|
|
+ getArticleContent(content, c)
|
|
|
+ }
|
|
|
+}
|