|
@@ -16,7 +16,6 @@ type ReportFreeLayout struct {
|
|
|
IsChapter int `gorm:"column:is_chapter"` // 是否多章节
|
|
|
Content string `gorm:"column:content;size:255"` // 内容
|
|
|
ContentStruct string `gorm:"column:content_struct;size:255"` // 内容
|
|
|
- IsDeleted int `gorm:"column:is_deleted"` //是否删除
|
|
|
CreateTime time.Time `gorm:"column:create_time"` // 创建时间
|
|
|
ModifyTime time.Time `gorm:"column:modify_time"` // 修改时间
|
|
|
}
|
|
@@ -29,6 +28,7 @@ const (
|
|
|
)
|
|
|
|
|
|
type ContentPage struct {
|
|
|
+ Id int `json:"Id"`
|
|
|
Page int `json:"Page"`
|
|
|
Content string `json:"Content"`
|
|
|
ContentStruct string `json:"ContentStruct"`
|
|
@@ -87,32 +87,104 @@ func (*ReportFreeLayout) TableName() string {
|
|
|
return "report_free_layout"
|
|
|
}
|
|
|
|
|
|
-func BatchInsertOrUpdatePages(tx *gorm.DB, list []*ReportFreeLayout, isChapter bool, reportId, chapterId int) (err error) {
|
|
|
+func SortPage(reportId int, tx *gorm.DB) (err error) {
|
|
|
+ if tx == nil {
|
|
|
+ tx = global.DbMap[utils.DbNameReport].Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ _ = tx.Commit()
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ sql := `select * from report_free_layout where report_id = ? and is_chapter=1 order by page asc`
|
|
|
+ var ormList []*ReportFreeLayout
|
|
|
+ err = tx.Raw(sql, reportId).Find(&ormList).Error
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(ormList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ chapterPages := make(map[int][]*ReportFreeLayout)
|
|
|
+ for _, v := range ormList {
|
|
|
+ chapterPages[v.ReportChapterId] = append(chapterPages[v.ReportChapterId], v)
|
|
|
+ }
|
|
|
|
|
|
+ chapterSql := `select report_chapter_id from report_chapter where report_id =? order by sort asc`
|
|
|
+ var chapterIds []int
|
|
|
+ err = tx.Raw(chapterSql, reportId).Scan(&chapterIds).Error
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ initPage := 1
|
|
|
+ for _, chapter := range chapterIds {
|
|
|
+ chapterList := chapterPages[chapter]
|
|
|
+ for _, v := range chapterList {
|
|
|
+ v.Page = initPage
|
|
|
+ initPage++
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ var updateList []*ReportFreeLayout
|
|
|
+ for _, chapterList := range chapterPages {
|
|
|
+ updateList = append(updateList, chapterList...)
|
|
|
+ }
|
|
|
+ err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
|
|
|
+ Columns: []clause.Column{{Name: "Id"}},
|
|
|
+ DoUpdates: clause.AssignmentColumns([]string{"Page"}),
|
|
|
+ }).CreateInBatches(updateList, len(updateList)).Error
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func DeleteChapters(reportId int, chapterId int) (err error) {
|
|
|
+ tx := global.DbMap[utils.DbNameReport].Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ _ = tx.Commit()
|
|
|
+ }()
|
|
|
+ err = tx.Exec("delete from report_free_layout where report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Error
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = SortPage(reportId, tx)
|
|
|
+ return
|
|
|
+}
|
|
|
+func BatchInsertOrUpdatePages(tx *gorm.DB, list []*ReportFreeLayout, isChapter bool, reportId, chapterId int) (err error) {
|
|
|
if isChapter {
|
|
|
- err = tx.Model(&ReportFreeLayout{}).Where("report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Update("is_deleted", 1).Error
|
|
|
+ err = tx.Exec("delete from report_free_layout where report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- } else {
|
|
|
- err = tx.Model(&ReportFreeLayout{}).Where("report_id = ? and report_chapter_id=? and is_chapter=0", reportId, chapterId).Update("is_deleted", 1).Error
|
|
|
+ err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
|
|
|
+ Columns: []clause.Column{{Name: "id"}},
|
|
|
+ DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "modify_time"}),
|
|
|
+ }).CreateInBatches(list, len(list)).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
+ err = SortPage(reportId, tx)
|
|
|
return
|
|
|
+ } else {
|
|
|
+ err = tx.Exec("delete from report_free_layout where report_id = ? and is_chapter=0", reportId).Error
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
|
|
|
+ Columns: []clause.Column{{Name: "id"}},
|
|
|
+ DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "page", "modify_time"}),
|
|
|
+ }).CreateInBatches(list, len(list)).Error
|
|
|
}
|
|
|
- err = tx.Model(&ReportFreeLayout{}).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"}),
|
|
|
- }).CreateInBatches(list, len(list)).Error
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func GetFreeLayoutChapterPagesByReportId(reportId 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 rfl.report_id =? and is_deleted=0 order by rc.sort,rfl.page asc`
|
|
|
+ sql := `select rfl.*,rc.sort from report_free_layout rfl LEFT JOIN report_chapter rc on rc.report_id=rfl.report_id where rfl.report_id =? order by rc.sort,rfl.page asc`
|
|
|
err = global.DbMap[utils.DbNameReport].Raw(sql, reportId).Find(&ormList).Error
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -122,8 +194,8 @@ func GetFreeLayoutChapterPagesByReportId(reportId int) (list []*ContentPage, err
|
|
|
}
|
|
|
func GetSingleFreeLayoutChapterPagesByReportId(reportId, chapterId int) (list []*ContentPage, err error) {
|
|
|
var ormList []*ReportFreeLayout
|
|
|
- sql := `select * from report_free_layout where report_id =? and report_chapter_id=? and is_deleted=0 order by page asc`
|
|
|
- err = global.DbMap[utils.DbNameReport].Raw(sql, reportId,chapterId).Find(&ormList).Error
|
|
|
+ sql := `select * from report_free_layout where report_id =? and report_chapter_id=? order by page asc`
|
|
|
+ err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, chapterId).Find(&ormList).Error
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
@@ -132,7 +204,7 @@ func GetSingleFreeLayoutChapterPagesByReportId(reportId, chapterId int) (list []
|
|
|
}
|
|
|
func GetFreeLayoutPagesByReportId(id int) (list []*ContentPage, err error) {
|
|
|
var ormList []*ReportFreeLayout
|
|
|
- sql := `select * from report_free_layout where report_id =? and is_deleted=0 and is_chapter=0 order by page asc`
|
|
|
+ sql := `select * from report_free_layout where report_id =? and is_chapter=0 order by page asc`
|
|
|
err = global.DbMap[utils.DbNameReport].Raw(sql, id).Find(&ormList).Error
|
|
|
if err != nil {
|
|
|
return nil, err
|