Browse Source

自由布局草稿保存

kobe6258 3 weeks ago
parent
commit
5a3581b801
3 changed files with 78 additions and 40 deletions
  1. 17 2
      controllers/report_v2.go
  2. 8 36
      models/report.go
  3. 53 2
      models/report/report_free_layout.go

+ 17 - 2
controllers/report_v2.go

@@ -667,6 +667,7 @@ func (this *ReportController) Detail() {
 		return
 	}
 	chapterList := make([]*models.ReportChapter, 0)
+	pageList := make([]*report.ContentPage, 0)
 	if item.HasChapter == 1 {
 		// 获取章节内容
 		tmpChapterList, err := models.GetPublishedChapterListByReportId(item.Id)
@@ -685,6 +686,19 @@ func (this *ReportController) Detail() {
 			}
 		}
 
+		if item.ReportLayout == 3 {
+			pages, err := report.GetFreeLayoutChapterPagesByReportId(item.Id)
+			if err != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = "获取自由布局内容页失败, Err: " + err.Error()
+				return
+			}
+			for _, page := range pages {
+				page.Content = html.UnescapeString(page.Content)
+				page.ContentStruct = html.UnescapeString(page.ContentStruct)
+				pageList = append(pageList, page)
+			}
+		}
 		//item.Abstract = item.Title
 	}
 	item.Content = html.UnescapeString(item.Content)
@@ -739,8 +753,9 @@ func (this *ReportController) Detail() {
 	}
 
 	resp := &models.ReportDetailView{
-		ReportDetail: item,
-		ChapterList:  chapterList,
+		ReportDetail:           item,
+		ChapterList:            chapterList,
+		FreeLayoutContentPages: pageList,
 	}
 	br.Ret = 200
 	br.Success = true

+ 8 - 36
models/report.go

@@ -9,7 +9,6 @@ import (
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"gorm.io/gorm"
-	"gorm.io/gorm/clause"
 	"strings"
 	"time"
 )
@@ -956,9 +955,10 @@ func (reportInfo *Report) UpdateReport(cols []string) (err error) {
 // @Description: 晨周报详情
 type ReportDetailView struct {
 	*ReportDetail
-	ChapterList    []*ReportChapter
-	GrandAdminList []ReportDetailViewAdmin
-	PermissionList []ReportDetailViewPermission
+	ChapterList            []*ReportChapter
+	GrandAdminList         []ReportDetailViewAdmin
+	PermissionList         []ReportDetailViewPermission
+	FreeLayoutContentPages []*report.ContentPage
 }
 
 // ReportDetailViewAdmin
@@ -1696,7 +1696,7 @@ type ReportShartUrlResp struct {
 	UrlToken string `description:"分享链接token"`
 }
 
-func InsertOrUpdateReportFreeLayoutContentPage(report *Report, ormList []*report.ReportFreeLayout) (err error) {
+func InsertOrUpdateReportFreeLayoutContentPage(reportInfo *Report, ormList []*report.ReportFreeLayout) (err error) {
 	tx := global.DbMap[utils.DbNameReport].Begin()
 
 	defer func() {
@@ -1707,22 +1707,8 @@ func InsertOrUpdateReportFreeLayoutContentPage(report *Report, ormList []*report
 		_ = tx.Commit()
 	}()
 	reportUpdateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
-	err = tx.Model(&report).Select(reportUpdateCols).Updates(report).Error
-
-	tx.Clauses(clause.OnConflict{
-		Columns:   []clause.Column{{Name: "report_id"}, {Name: "report_chapter_id"}, {Name: "page"}},
-		DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "modify_time", "is_deleted"}),
-	})
-	err = tx.Where("report_id = ?", report.Id).Update("is_deleted", 1).Error
-	if err != nil {
-		return
-	}
-	err = tx.CreateInBatches(ormList, len(ormList)).Error
-	if err != nil {
-		return
-	}
-
-	return
+	err = tx.Model(&reportInfo).Select(reportUpdateCols).Updates(reportInfo).Error
+	return report.BatchInsertOrUpdatePages(tx, ormList, false, reportInfo.Id, 0)
 }
 func UpdateChapterFreeLayoutContentPage(reportInfo *Report, chapterInfo *ReportChapter, updateCols []string, tickerList []*ReportChapterTicker, ormList []*report.ReportFreeLayout) (err error) {
 	tx := global.DbMap[utils.DbNameReport].Begin()
@@ -1753,19 +1739,5 @@ func UpdateChapterFreeLayoutContentPage(reportInfo *Report, chapterInfo *ReportC
 			return
 		}
 	}
-	tx.Clauses(clause.OnConflict{
-		Columns:   []clause.Column{{Name: "report_id"}, {Name: "report_chapter_id"}, {Name: "page"}},
-		DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "modify_time", "is_deleted"}),
-	})
-	err = tx.Where("report_id = ? and report_chapter_id=? and is_chapter=1", reportInfo.Id, chapterInfo.ReportChapterId).Update("is_deleted", 1).Error
-	if err != nil {
-		return
-	}
-	err = tx.CreateInBatches(ormList, len(ormList)).Error
-	if err != nil {
-		return
-	}
-	reportUpdateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
-	err = tx.Model(&reportInfo).Select(reportUpdateCols).Updates(reportInfo).Error
-	return
+	return report.BatchInsertOrUpdatePages(tx, ormList, true, reportInfo.Id, chapterInfo.ReportChapterId)
 }

+ 53 - 2
models/report/report_free_layout.go

@@ -1,6 +1,10 @@
 package report
 
 import (
+	"eta/eta_api/global"
+	"eta/eta_api/utils"
+	"gorm.io/gorm"
+	"gorm.io/gorm/clause"
 	"time"
 )
 
@@ -55,7 +59,15 @@ func (cp *ContentPage) ToView(isChapter bool, ReportId int, ReportChapterId int)
 		}
 	}
 }
-
+func (cp *ReportFreeLayout) ToPageView() *ContentPage {
+	return &ContentPage{
+		Page:            cp.Page,
+		Content:         cp.Content,
+		ContentStruct:   cp.ContentStruct,
+		ReportId:        cp.ReportId,
+		ReportChapterId: cp.ReportChapterId,
+	}
+}
 func ToOrmViewList(srcList []ContentPage, isChapter bool, ReportId int, ReportChapterId int) (list []*ReportFreeLayout) {
 	for _, v := range srcList {
 		list = append(list, v.ToView(isChapter, ReportId, ReportChapterId))
@@ -63,9 +75,48 @@ func ToOrmViewList(srcList []ContentPage, isChapter bool, ReportId int, ReportCh
 	return
 }
 
+func ToPageViewList(srcList []*ReportFreeLayout) (list []*ContentPage) {
+	for _, v := range srcList {
+		list = append(list, v.ToPageView())
+	}
+	return
+}
+
 // TableName 设置表名
-func (ReportFreeLayout) TableName() string {
+func (*ReportFreeLayout) TableName() string {
 	return "report_free_layout"
 }
 
+func BatchInsertOrUpdatePages(tx *gorm.DB, list []*ReportFreeLayout, isChapter bool, reportId, chapterId int) (err error) {
+	tx.Clauses(clause.OnConflict{
+		Columns:   []clause.Column{{Name: "report_id"}, {Name: "report_chapter_id"}, {Name: "page"}},
+		DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "modify_time", "is_deleted"}),
+	})
+	if isChapter {
+		err = tx.Where("report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Update("is_deleted", 1).Error
+		if err != nil {
+			return
+		}
+	} else {
+		err = tx.Where("report_id = ? and report_chapter_id=? and is_chapter=0", reportId, chapterId).Update("is_deleted", 1).Error
+		if err != nil {
+			return
+		}
+	}
+	if err != nil {
+		return
+	}
+	err = tx.CreateInBatches(list, len(list)).Error
+	return
+}
 
+func GetFreeLayoutChapterPagesByReportId(id int) (list []*ContentPage, err error) {
+	var ormList []*ReportFreeLayout
+	sql := `select rfl.*,rc.sort from report_free_layout rfl LEFT JOIN report_chapter rc on rc.report_id=rfl.report_id  where is_deleted=0 order by rc.sort,rfl.page asc`
+	err = global.DbMap[utils.DbNameReport].Raw(sql, id).Find(&ormList).Error
+	if err != nil {
+		return nil, err
+	}
+	list = ToPageViewList(ormList)
+	return
+}