diff --git a/consts/common.go b/consts/common.go index 75db4de..0de2d73 100644 --- a/consts/common.go +++ b/consts/common.go @@ -16,6 +16,7 @@ const ( ) const ( + VIDEO_STATUS_INIT = -1 VIDEO_STATUS_TO_BE_DOWNLOAD = 0 VIDEO_STATUS_DOWNLOADING = 1 VIDEO_STATUS_DOWNLOAD_DONE = 2 diff --git a/services/account.go b/services/account.go index e76d3d7..b92febf 100644 --- a/services/account.go +++ b/services/account.go @@ -96,10 +96,11 @@ type AccountInfo struct { func AccountList(page, pageSize int) (*[]*AccountInfo, int64) { db := models.GetDB() accountMap := make(map[int]*AccountInfo, 0) + accountMids := make([]int, 0) total := AccountTotal() if total > 0 { var datas []models.BiliAccounts - db.Model(&models.BiliAccounts{}).Limit(pageSize).Offset((page - 1) * pageSize).Find(&datas) + db.Model(&models.BiliAccounts{}).Order("updated_at DESC").Limit(pageSize).Offset((page - 1) * pageSize).Find(&datas) for _, data := range datas { item := AccountInfo{ Mid: data.Mid, @@ -114,10 +115,11 @@ func AccountList(page, pageSize int) (*[]*AccountInfo, int64) { FoldersCount: 0, } accountMap[data.Mid] = &item + accountMids = append(accountMids, data.Mid) } var favourFolderInfos []models.FavourFoldersInfo - db.Where("mid IN (?)", maps.Keys(accountMap)).Find(&favourFolderInfos) + db.Where("mid IN (?)", accountMids).Find(&favourFolderInfos) for _, v := range favourFolderInfos { folders := FavourFolders{ Mlid: v.Mlid, diff --git a/services/favour.go b/services/favour.go index a6e1b82..111bcc9 100644 --- a/services/favour.go +++ b/services/favour.go @@ -132,6 +132,9 @@ func GetAccountFavourInfoByMid(mid int) *[]*FavourFolders { func SetFavourSyncStatus(mid, mlid, status int) { db := models.GetDB() db.Model(&models.FavourFoldersInfo{}).Where("mlid = ?", mlid).Update("sync", status) + if status == consts.FAVOUR_NEED_SYNC { + db.Model(&models.FavourVideos{}).Where("mid = ? AND mlid = ? AND status = ?", mid, mlid, consts.VIDEO_STATUS_INIT).Update("status", consts.VIDEO_STATUS_TO_BE_DOWNLOAD) + } } type FavFile struct { diff --git a/services/favour_video.go b/services/favour_video.go index c98e2c8..42b5586 100644 --- a/services/favour_video.go +++ b/services/favour_video.go @@ -31,7 +31,11 @@ func (f *FavourVideoService) Save() { }).FirstOrInit(&favourVideo) needUpdata := false if favourVideo.ID == 0 { - favourVideo.Status = consts.VIDEO_STATUS_TO_BE_DOWNLOAD + favInfo := GetFavourInfoByMlid(f.V.Mlid) + favourVideo.Status = consts.VIDEO_STATUS_INIT + if favInfo != nil && favInfo.Sync == consts.FAVOUR_NEED_SYNC { + favourVideo.Status = consts.VIDEO_STATUS_TO_BE_DOWNLOAD + } needUpdata = true } if favourVideo.Part != f.V.Part { @@ -147,41 +151,29 @@ type VideoInfo struct { AccountName string `json:"account_name"` } +func handleQueryStatus(status int) []int { + statusList := []int{status} + if status == consts.VIDEO_STATUS_DOWNLOAD_FAIL { + statusList = append(statusList, consts.VIDEO_STATUS_DOWNLOAD_RETRY) + } + return statusList + +} + func GetVideosByStatus(status, page, pageSize int) (*[]*VideoInfo, int64) { result := make([]*VideoInfo, 0) db := models.GetDB() var total int64 - statusList := []int{status} + statusList := handleQueryStatus(status) - var mlids []int - if status == consts.VIDEO_STATUS_TO_BE_DOWNLOAD { - var syncFav []models.FavourFoldersInfo - db.Model(&models.FavourFoldersInfo{}).Where(&models.FavourFoldersInfo{Sync: consts.FAVOUR_NEED_SYNC}).Find(&syncFav) - for _, v := range syncFav { - mlids = append(mlids, v.Mlid) - } - } - - if status == consts.VIDEO_STATUS_DOWNLOAD_FAIL { - statusList = append(statusList, consts.VIDEO_STATUS_DOWNLOAD_RETRY) - } + query := db.Model(&models.FavourVideos{}).Where("status IN (?)", statusList) - if len(mlids) > 0 && status == consts.VIDEO_STATUS_TO_BE_DOWNLOAD { - db.Model(&models.FavourVideos{}).Where("mlid IN (?) AND status = ?", mlids, status).Count(&total) - } else if len(mlids) < 1 && status == consts.VIDEO_STATUS_TO_BE_DOWNLOAD { - return &result, 0 - } else { - db.Model(&models.FavourVideos{}).Where("status IN (?)", statusList).Count(&total) - } + query.Count(&total) if total > 0 { var favourVideos []models.FavourVideos - if status == consts.VIDEO_STATUS_TO_BE_DOWNLOAD { - db.Model(&models.FavourVideos{}).Where("mlid IN (?) AND status = ?", mlids, status).Limit(pageSize).Offset((page - 1) * pageSize).Find(&favourVideos) - } else { - db.Model(&models.FavourVideos{}).Where("status IN (?)", statusList).Limit(pageSize).Offset((page - 1) * pageSize).Find(&favourVideos) - } + query.Order("updated_at DESC").Limit(pageSize).Offset((page - 1) * pageSize).Find(&favourVideos) accountMap := make(map[int]*AccountInfo, 0) favMap := make(map[int]*FavourFolders, 0) for _, v := range favourVideos {