package service
import (
"errors"
"main_program/common"
"main_program/config"
"main_program/entity"
"gorm.io/gorm"
)
type srtFileService struct { }
var SrtFileService srtFileService
func ( srt * srtFileService ) QueryOneNotScanVideoId ( ) ( videoId string , err error ) {
var srtFile entity . Srtfile
result := common . MysqlDB . Where ( & entity . Srtfile { IsScan : 0 } ) . First ( & srtFile )
if errors . Is ( result . Error , gorm . ErrRecordNotFound ) {
return "" , errors . New ( "no srtFile" )
} else {
return srtFile . VideoId , nil
}
}
func ( srt * srtFileService ) QuerySrtFilesByVideoId ( videoId string ) ( srtFiles [ ] entity . Srtfile , err error ) {
var srtFileEntitys [ ] entity . Srtfile
result := common . MysqlDB . Where ( & entity . Srtfile { VideoId : videoId } ) . Find ( & srtFileEntitys )
if result . Error != nil {
return nil , errors . New ( "query srtFiles failed" )
} else {
return srtFileEntitys , nil
}
}
func ( srt * srtFileService ) QueryOneNotScanVideoIdByRegion ( region string ) ( videoId string , err error ) {
var srtFile entity . Srtfile
sqlStr := "SELECT Srtfile.* FROM Srtfile JOIN Videos ON Srtfile.videoId = Videos.videoId JOIN Channel ON Videos.channelId = Channel.channelId WHERE Channel.region = ? and Srtfile.isScan = 0;"
result := common . MysqlDB . Raw ( sqlStr , region ) . First ( & srtFile )
if result . Error != nil {
return "" , errors . New ( "no srtFile" )
} else {
return srtFile . VideoId , nil
}
}
func ( srt * srtFileService ) UpdateIsScanById ( id int , isScan int ) ( err error ) {
result := common . MysqlDB . Model ( & entity . Srtfile { Id : uint ( id ) } ) . Update ( "isScan" , isScan )
if result . Error != nil {
config . Logger . Error ( result . Error )
return errors . New ( "update srtFile failed" )
} else {
return nil
}
}