浏览代码

Merge remote-tracking branch 'origin/dm' into debug

Roc 1 月之前
父节点
当前提交
31412639fb
共有 1 个文件被更改,包括 78 次插入0 次删除
  1. 78 0
      models/report_chapter.go

+ 78 - 0
models/report_chapter.go

@@ -5,6 +5,7 @@ import (
 	"eta/eta_api/global"
 	"eta/eta_api/models/report"
 	"eta/eta_api/utils"
+	"gorm.io/gorm"
 	"time"
 )
 
@@ -46,6 +47,25 @@ type ReportChapter struct {
 	VoiceGenerateType   int       `description:"音频生成方式,0:系统生成,1:人工上传"`
 }
 
+// AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
+func (m *ReportChapter) AfterFind(db *gorm.DB) (err error) {
+	m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
+
+	return
+}
+
+// ConvDateTimeStr
+// @Description: ConvDateTimeStr
+// @author: Roc
+// @receiver m
+// @datetime 2025-02-13 09:54:02
+// @return err error
+func (m *ReportChapter) ConvDateTimeStr() {
+	m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
+
+	return
+}
+
 // ReportChapterItem 报告章节详情
 type ReportChapterItem struct {
 	ReportChapterId     int    `gorm:"column:report_chapter_id;primaryKey;autoIncrement" description:"报告章节ID"`
@@ -83,6 +103,33 @@ type ReportChapterItem struct {
 	ReportCreateTime    string `description:"报告时间创建时间"`
 }
 
+// AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
+func (m *ReportChapterItem) AfterFind(db *gorm.DB) (err error) {
+	m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
+	m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
+	m.PublishTime = utils.GormDateStrToDateTimeStr(m.PublishTime)
+	m.ContentModifyTime = utils.GormDateStrToDateTimeStr(m.ContentModifyTime)
+	m.ReportCreateTime = utils.GormDateStrToDateTimeStr(m.ReportCreateTime)
+
+	return
+}
+
+// ConvDateTimeStr
+// @Description: ConvDateTimeStr
+// @author: Roc
+// @receiver m
+// @datetime 2025-02-13 09:54:02
+// @return err error
+func (m *ReportChapterItem) ConvDateTimeStr() {
+	m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
+	m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
+	m.PublishTime = utils.GormDateStrToDateTimeStr(m.PublishTime)
+	m.ContentModifyTime = utils.GormDateStrToDateTimeStr(m.ContentModifyTime)
+	m.ReportCreateTime = utils.GormDateStrToDateTimeStr(m.ReportCreateTime)
+
+	return
+}
+
 // ReportChapterItemResp
 // @Description: 章节详情(带有一些额外的数据)
 type ReportChapterItemResp struct {
@@ -188,6 +235,11 @@ func GetReportChapterInfoById(reportChapterId int) (item *ReportChapter, err err
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter WHERE report_chapter_id = ? `
 	err = o.Raw(sql, reportChapterId).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }
 
@@ -202,6 +254,11 @@ func GetReportChapterItemById(reportChapterId int) (item *ReportChapterItem, err
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter WHERE report_chapter_id = ? `
 	err = o.Raw(sql, reportChapterId).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }
 
@@ -210,6 +267,11 @@ func GetLastPublishedReportChapter(typeId int, reportType string) (item *ReportC
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter WHERE publish_state = 2 AND type_id = ? AND report_type = ? ORDER BY report_chapter_id DESC limit 1 `
 	err = o.Raw(sql, typeId, reportType).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }
 
@@ -493,6 +555,11 @@ func (chapterChapterInfo *ReportChapter) GetReportChapterById(reportChapterId in
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter WHERE report_chapter_id = ?`
 	err = o.Raw(sql, reportChapterId).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }
 
@@ -532,6 +599,11 @@ func (chapterChapterInfo *ReportChapter) GetFirstReportChapterByReportId(reportI
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter WHERE 1 = 1 AND report_id = ? ORDER BY sort ASC, report_chapter_id ASC LIMIT 1`
 	err = o.Raw(sql, reportId).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }
 
@@ -581,6 +653,7 @@ func GetAllReportChapter() (items []*ReportChapter, err error) {
 	o := global.DbMap[utils.DbNameReport]
 	sql := ` SELECT * FROM report_chapter ORDER BY report_chapter_id asc `
 	err = o.Raw(sql).Find(&items).Error
+
 	return
 }
 
@@ -614,5 +687,10 @@ func GetNewestPreReportChapterByClassifyIdAndTypeId(classifyId, typeId int) (ite
 	o := global.DbMap[utils.DbNameReport]
 	sql := `SELECT a.* FROM report_chapter AS a JOIN report AS b ON a.report_id = b.id WHERE a.classify_id_first = ? AND a.type_id = ? AND a.publish_state = 2 AND b.state IN (2,6) ORDER BY a.publish_time DESC LIMIT 1`
 	err = o.Raw(sql, classifyId, typeId).First(&item).Error
+	if err != nil {
+		return
+	}
+	item.ConvDateTimeStr()
+
 	return
 }