mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-03-17 12:17:32 +08:00
optimize updates
This commit is contained in:
146
film/server/model/system/CollectRecord.go
Normal file
146
film/server/model/system/CollectRecord.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"server/config"
|
||||
"server/plugin/db"
|
||||
)
|
||||
|
||||
// FailureRecord 失败采集记录信息机构体
|
||||
type FailureRecord struct {
|
||||
gorm.Model
|
||||
OriginId string `json:"originId"` // 采集站唯一ID
|
||||
OriginName string `json:"originName"` // 采集站唯一ID
|
||||
Uri string `json:"uri"` // 采集源链接
|
||||
CollectType ResourceType `json:"collectType"` // 采集类型
|
||||
PageNumber int `json:"pageNumber"` // 页码
|
||||
Hour int `json:"hour"` // 采集参数 h 时长
|
||||
Cause string `json:"cause"` // 失败原因
|
||||
Status int `json:"status"` // 重试状态
|
||||
}
|
||||
|
||||
// TableName 采集失败记录表表名
|
||||
func (fr FailureRecord) TableName() string {
|
||||
return config.FailureRecordTableName
|
||||
}
|
||||
|
||||
// CreateFailureRecordTable 创建失效记录表
|
||||
func CreateFailureRecordTable() {
|
||||
var fl = &FailureRecord{}
|
||||
// 不存在则创建FailureRecord表
|
||||
if !db.Mdb.Migrator().HasTable(fl) {
|
||||
if err := db.Mdb.AutoMigrate(fl); err != nil {
|
||||
log.Println("Create Table failure_record failed:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SaveFailureRecord 添加采集失效记录
|
||||
func SaveFailureRecord(fl FailureRecord) {
|
||||
// 数据量不多但存在并发问题, 开启事务
|
||||
err := db.Mdb.Transaction(func(tx *gorm.DB) error {
|
||||
// 将采集失败信息存储到record表中
|
||||
if err := tx.Create(&fl).Error; err != nil {
|
||||
log.Println("Add failure record failed:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// 如果事务提交失败, 则输出相应信息, (存一份数据到Redis??)
|
||||
if err != nil {
|
||||
log.Println("Save failure record affairs failed:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// FailureRecordList 获取所有的采集失效记录
|
||||
func FailureRecordList(vo RecordRequestVo) []FailureRecord {
|
||||
// 通过RecordRequestVo,生成查询条件
|
||||
qw := db.Mdb.Model(&FailureRecord{})
|
||||
if vo.OriginId != "" {
|
||||
qw.Where("origin_id = ?", vo.OriginId)
|
||||
}
|
||||
if !vo.BeginTime.IsZero() && !vo.EndTime.IsZero() {
|
||||
qw.Where("created_at BETWEEN ? AND ? ", vo.BeginTime, vo.EndTime)
|
||||
}
|
||||
//if vo.CollectType >= 0 {
|
||||
// qw.Where("collect_type = ?", vo.CollectType)
|
||||
//}
|
||||
//if vo.Hour != 0 {
|
||||
// qw.Where("hour = ?", vo.Hour)
|
||||
//}
|
||||
if vo.Status >= 0 {
|
||||
qw.Where("status = ?", vo.Status)
|
||||
}
|
||||
|
||||
// 获取分页数据
|
||||
GetPage(qw, vo.Paging)
|
||||
// 获取分页查询的数据
|
||||
var list []FailureRecord
|
||||
if err := qw.Limit(vo.Paging.PageSize).Offset((vo.Paging.Current - 1) * vo.Paging.PageSize).Order("updated_at DESC").Find(&list).Error; err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// FindRecordById 获取id对应的失效记录
|
||||
func FindRecordById(id uint) *FailureRecord {
|
||||
var fr FailureRecord
|
||||
fr.ID = id
|
||||
// 通过ID查询对应的数据
|
||||
db.Mdb.First(&fr)
|
||||
return &fr
|
||||
}
|
||||
|
||||
// PendingRecord 查询所有待处理的记录信息
|
||||
func PendingRecord() []FailureRecord {
|
||||
var list []FailureRecord
|
||||
// 1. 获取 hour > 4320 || hour < 0 && status = 1 的影片信息
|
||||
db.Mdb.Where("(hour > 4320 OR hour < 0) AND status = 1").Find(&list)
|
||||
// 2. 获取 hour > 0 && hour < 4320 && status = 1 的影片信息(只获取最早的一条记录)
|
||||
var fr FailureRecord
|
||||
db.Mdb.Where("hour > 0 AND hour < 4320 AND status = 1").Order("hour DESC, created_at ASC").First(&fr)
|
||||
// 3. 将 fr 添加到 list中
|
||||
list = append(list, fr)
|
||||
return list
|
||||
}
|
||||
|
||||
// ChangeRecord 修改已完成二次采集的记录状态
|
||||
func ChangeRecord(fr *FailureRecord, status int) {
|
||||
switch {
|
||||
case fr.Hour > 168 && fr.Hour < 360:
|
||||
db.Mdb.Model(&FailureRecord{}).Where("hour > 168 AND hour < 360 AND created_at >= ?", fr.CreatedAt).Update("status", status)
|
||||
case fr.Hour < 0, fr.Hour > 4320:
|
||||
db.Mdb.Model(&FailureRecord{}).Where("id = ?", fr.ID).Update("status", status)
|
||||
default:
|
||||
// 其余范围,暂不处理
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// RetryRecord 修改重试采集成功的记录
|
||||
func RetryRecord(id uint, status int64) error {
|
||||
// 查询id对应的失败记录
|
||||
fr := FindRecordById(id)
|
||||
// 将本次更新成功的记录数据状态修改为成功 0
|
||||
return db.Mdb.Model(&FailureRecord{}).Where("update_at > ?", fr.UpdatedAt).Update("status", 0).Error
|
||||
|
||||
}
|
||||
|
||||
// DelDoneRecord 删除已处理的记录信息 -- 逻辑删除
|
||||
func DelDoneRecord() {
|
||||
if err := db.Mdb.Where("status = ?", 0).Delete(&FailureRecord{}).Error; err != nil {
|
||||
log.Println("Delete failure record failed:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TruncateRecordTable 截断 record table
|
||||
func TruncateRecordTable() {
|
||||
var s FailureRecord
|
||||
err := db.Mdb.Exec(fmt.Sprintf("TRUNCATE Table %s", s.TableName())).Error
|
||||
if err != nil {
|
||||
log.Println("TRUNCATE TABLE Error: ", err)
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ type FilmCollectTask struct {
|
||||
Cid cron.EntryID `json:"cid"` // 定时任务Id
|
||||
Time int `json:"time"` // 采集时长, 最新x小时更新的内容
|
||||
Spec string `json:"spec"` // 执行周期 cron表达式
|
||||
Model int `json:"model"` // 任务类型, 0 - 自动更新已启用站点 || 1 - 更新Ids中的资源站数据
|
||||
Model int `json:"model"` // 任务类型, 0 - 自动更新已启用站点 || 1 - 更新Ids中的资源站数据 || 2 - 定期清理失败采集记录
|
||||
State bool `json:"state"` // 状态 开启 | 禁用
|
||||
Remark string `json:"remark"` // 任务备注信息
|
||||
}
|
||||
|
||||
@@ -73,19 +73,19 @@ func FilmZero() {
|
||||
db.Rdb.Del(db.Cxt, db.Rdb.Keys(db.Cxt, "OriginalResource*").Val()...)
|
||||
db.Rdb.Del(db.Cxt, db.Rdb.Keys(db.Cxt, "Search*").Val()...)
|
||||
// 删除mysql中留存的检索表
|
||||
var s *SearchInfo
|
||||
var s SearchInfo
|
||||
//db.Mdb.Exec(fmt.Sprintf(`drop table if exists %s`, s.TableName()))
|
||||
// 截断数据表 truncate table users
|
||||
if ExistSearchTable() {
|
||||
db.Mdb.Exec(fmt.Sprintf(`TRUNCATE table %s`, s.TableName()))
|
||||
db.Mdb.Exec(fmt.Sprintf("TRUNCATE table %s", s.TableName()))
|
||||
}
|
||||
}
|
||||
|
||||
// ResetSearchTable 重置Search表
|
||||
func ResetSearchTable() {
|
||||
// 删除 Search 表
|
||||
var s *SearchInfo
|
||||
db.Mdb.Exec(fmt.Sprintf(`drop table if exists %s`, s.TableName()))
|
||||
var s SearchInfo
|
||||
db.Mdb.Exec(fmt.Sprintf("drop table if exists %s", s.TableName()))
|
||||
// 重新创建 Search 表
|
||||
CreateSearchTable()
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func SaveSearchTag(search SearchInfo) {
|
||||
// 获取redis中的searchMap
|
||||
key := fmt.Sprintf(config.SearchTitle, search.Pid)
|
||||
searchMap := db.Rdb.HGetAll(db.Cxt, key).Val()
|
||||
// 是否存储对应分类的map, 如果不存在则缓存一份
|
||||
// 是否存在对应分类的map, 如果不存在则缓存一份
|
||||
if len(searchMap) == 0 {
|
||||
searchMap = make(map[string]string)
|
||||
searchMap["Category"] = "类型"
|
||||
@@ -230,6 +230,7 @@ func CreateSearchTable() {
|
||||
}
|
||||
}
|
||||
|
||||
// ExistSearchTable 是否存在Search Table
|
||||
func ExistSearchTable() bool {
|
||||
// 1. 判断表中是否存在当前表
|
||||
return db.Mdb.Migrator().HasTable(&SearchInfo{})
|
||||
@@ -237,7 +238,7 @@ func ExistSearchTable() bool {
|
||||
|
||||
// AddSearchIndex search表中数据保存完毕后 将常用字段添加索引提高查询效率
|
||||
func AddSearchIndex() {
|
||||
var s *SearchInfo
|
||||
var s SearchInfo
|
||||
tableName := s.TableName()
|
||||
// 添加索引
|
||||
db.Mdb.Exec(fmt.Sprintf("CREATE UNIQUE INDEX idx_mid ON %s (mid)", tableName))
|
||||
@@ -331,8 +332,8 @@ func ExistSearchInfo(mid int64) bool {
|
||||
|
||||
// TunCateSearchTable 截断SearchInfo数据表
|
||||
func TunCateSearchTable() {
|
||||
var searchInfo *SearchInfo
|
||||
err := db.Mdb.Exec(fmt.Sprint("TRUNCATE TABLE ", searchInfo.TableName())).Error
|
||||
var searchInfo SearchInfo
|
||||
err := db.Mdb.Exec(fmt.Sprintf("TRUNCATE TABLE %s", searchInfo.TableName())).Error
|
||||
if err != nil {
|
||||
log.Println("TRUNCATE TABLE Error: ", err)
|
||||
}
|
||||
@@ -434,7 +435,7 @@ func GetMovieListByCid(cid int64, page *Page) []MovieBasicInfo {
|
||||
return list
|
||||
}
|
||||
|
||||
// GetHotMovieByPid 获取指定类别的热门影片
|
||||
// GetHotMovieByPid 获取Pid指定类别的热门影片
|
||||
func GetHotMovieByPid(pid int64, page *Page) []SearchInfo {
|
||||
// 返回分页参数
|
||||
//var count int64
|
||||
@@ -452,6 +453,24 @@ func GetHotMovieByPid(pid int64, page *Page) []SearchInfo {
|
||||
return s
|
||||
}
|
||||
|
||||
// GetHotMovieByCid 获取当前分类下的热门影片
|
||||
func GetHotMovieByCid(cid int64, page *Page) []SearchInfo {
|
||||
// 返回分页参数
|
||||
//var count int64
|
||||
//db.Mdb.Model(&SearchInfo{}).Where("pid", pid).Count(&count)
|
||||
//page.Total = int(count)
|
||||
//page.PageCount = int((page.Total + page.PageSize - 1) / page.PageSize)
|
||||
// 进行具体的信息查询
|
||||
var s []SearchInfo
|
||||
// 当前时间偏移一个月
|
||||
t := time.Now().AddDate(0, -1, 0).Unix()
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("cid=? AND update_stamp > ?", cid, t).Order(" year DESC, hits DESC").Find(&s).Error; err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SearchFilmKeyword 通过关键字搜索库存中满足条件的影片名
|
||||
func SearchFilmKeyword(keyword string, page *Page) []SearchInfo {
|
||||
var searchList []SearchInfo
|
||||
|
||||
@@ -36,7 +36,7 @@ func CreateUserTable() {
|
||||
// 如果不存在则创建表 并设置自增ID初始值为10000
|
||||
if !ExistUserTable() {
|
||||
err := db.Mdb.AutoMigrate(u)
|
||||
db.Mdb.Exec(fmt.Sprintf("alter table %s auto_Increment=%d", u.TableName(), config.UserIdInitialVal))
|
||||
db.Mdb.Exec(fmt.Sprintf("alter table %s auto_Increment = %d", u.TableName(), config.UserIdInitialVal))
|
||||
if err != nil {
|
||||
log.Println("Create Table SearchInfo Failed: ", err)
|
||||
}
|
||||
@@ -81,6 +81,7 @@ func GetUserByNameOrEmail(userName string) *User {
|
||||
return u
|
||||
}
|
||||
|
||||
// GetUserById 通过id获取对应的用户信息
|
||||
func GetUserById(id uint) User {
|
||||
var user = User{Model: gorm.Model{ID: id}}
|
||||
db.Mdb.First(&user)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package system
|
||||
|
||||
import "time"
|
||||
|
||||
// SearchTagsVO 搜索标签请求参数
|
||||
type SearchTagsVO struct {
|
||||
Pid int64 `json:"pid"`
|
||||
@@ -34,6 +36,12 @@ type FilmTaskOptions struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Name string `json:"name"`
|
||||
Value any `json:"value"`
|
||||
}
|
||||
type OptionGroup map[string][]Option
|
||||
|
||||
// CollectParams 数据采集所需要的参数
|
||||
type CollectParams struct {
|
||||
Id string `json:"id"` // 资源站id
|
||||
@@ -109,7 +117,18 @@ type PlayLinkVo struct {
|
||||
LinkList []MovieUrlInfo `json:"linkList"`
|
||||
}
|
||||
|
||||
// MovieDetailVo 影片详情数据, 播放源合并版
|
||||
type MovieDetailVo struct {
|
||||
MovieDetail
|
||||
List []PlayLinkVo `json:"list"`
|
||||
}
|
||||
|
||||
type RecordRequestVo struct {
|
||||
OriginId string `json:"originId"` // 源站点ID
|
||||
CollectType int `json:"collectType"` // 采集类型
|
||||
Hour int `json:"hour"` // 采集时长
|
||||
Status int `json:"status"` // 状态
|
||||
BeginTime time.Time `json:"beginTime"` // 起始时间
|
||||
EndTime time.Time `json:"endTime"` // 结束时间
|
||||
Paging *Page `json:"paging"` // 分页参数
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user