Merge branch 'refs/heads/feat-video' into release

This commit is contained in:
junleea 2024-12-17 18:58:24 +08:00
commit be079f9b31
3 changed files with 67 additions and 24 deletions

View File

@ -1,8 +1,8 @@
package dao package dao
import ( import (
"fmt"
"gorm.io/gorm" "gorm.io/gorm"
"time"
) )
type Video struct { type Video struct {
@ -21,7 +21,7 @@ type Video struct {
func FindWillDelVideoList(id int) []Video { func FindWillDelVideoList(id int) []Video {
var videos []Video var videos []Video
DB.Where("auth_id = ?", id).Where("delete_time<=now()").Where("isdelete=0").Find(&videos) DB.Raw("select * from videos where auth_id = ? and delete_time<=now() and isdelete=0", id).Scan(&videos)
return videos return videos
} }
@ -37,10 +37,33 @@ func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete
return video.ID return video.ID
} }
// 文件已删除
func DeleteVideoByID(id, user int) int { func DeleteVideoByID(id, user int) int {
delete_time := time.Now().Format("2006-01-02 15:04:05") res := DB.Exec("update videos set deleted_at = now(),isdelete=1 where id=? and auth_id=?", id, user) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
DB.Where("id = ? and auth_id = ?", id, user).Updates(&Video{DeleteTime: delete_time, IsDelete: 1}) if res.Error != nil {
DB.Where("id = ? and auth_id = ?", id, user).Delete(&Video{}) fmt.Println("dao DeleteVideoByID:", res.Error)
return 0
}
return id
}
// 文件未删除时逻辑删除
func LogicDeleteVideoByID(id, user int) int {
res := DB.Exec("update videos set deleted_at=now(),delete_time=now() where id=? and auth_id=?", id, user) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
if res.Error != nil {
fmt.Println("dao LogicDeleteVideoByID:", res.Error)
return 0
}
return id
}
// 回滚视频输出-逻辑删除,若文件已删除则不可回滚
func RollbackVideoByID(id, user int) int {
res := DB.Exec("update videos set deleted_at=null where id=? and auth_id=? and isdelete=0", id, user) //isdelete=0说明文件未删除
if res.Error != nil {
fmt.Println("dao RollbackVideoByID:", res.Error)
return 0
}
return id return id
} }
@ -71,7 +94,7 @@ func FindVideoListsByAuthID(auth_id int) []Video {
func FindVideoListByTime(auth_id int, startTime, endTime string) []Video { func FindVideoListByTime(auth_id int, startTime, endTime string) []Video {
var videos []Video var videos []Video
DB.Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos) DB.Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ? and deleted_at != null", startTime, endTime).Find(&videos)
return videos return videos
} }

View File

@ -15,6 +15,7 @@ import (
// video获取视频列表请求 // video获取视频列表请求
type gvlReq struct { type gvlReq struct {
ID int `json:"id" form:"id"`
StartTime int64 `json:"begin" form:"begin"` StartTime int64 `json:"begin" form:"begin"`
EndTime int64 `json:"end" form:"end"` EndTime int64 `json:"end" form:"end"`
IP string `json:"ip" form:"ip"` IP string `json:"ip" form:"ip"`
@ -59,12 +60,13 @@ type videoPReq struct {
} }
type VideoDelReq struct { type VideoDelReq struct {
ID int `json:"id"` ID int `json:"id" form:"id"`
Type string `json:"type" form:"type"`
} }
// video延迟视频请求 // video延迟视频请求
type delayReq struct { type delayReq struct {
ID int `form:"id"` ID int `json:"id" form:"id"`
Option string `form:"option"` Option string `form:"option"`
AuthId int `form:"userId"` AuthId int `form:"userId"`
IP string `form:"ip"` IP string `form:"ip"`
@ -164,7 +166,7 @@ func GetVideoList(c *gin.Context) {
tm1 = "" tm1 = ""
tm2 = "" tm2 = ""
} }
videos := service.GetVideoList(int(id.(float64)), tm1, tm2, gvl_req.Hour) videos := service.GetVideoList(int(id.(float64)), gvl_req.ID, tm1, tm2, gvl_req.Hour)
c.JSON(http.StatusOK, gin.H{"data": videos, "code": proto.SuccessCode, "message": "success"}) c.JSON(http.StatusOK, gin.H{"data": videos, "code": proto.SuccessCode, "message": "success"})
} else { } else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
@ -203,34 +205,30 @@ func DelayVideo(c *gin.Context) {
func DeleteVideo(c *gin.Context) { func DeleteVideo(c *gin.Context) {
var video_req VideoDelReq var video_req VideoDelReq
id, _ := c.Get("id") id, _ := c.Get("id")
user, _ := c.Get("username")
if err := c.ShouldBind(&video_req); err == nil { if err := c.ShouldBind(&video_req); err == nil {
res := service.DeleteVideo(video_req.ID, int(id.(float64))) res := service.DeleteVideo(video_req.ID, int(id.(float64)), video_req.Type)
if res != 0 { if res != 0 {
data := map[string]interface{}{"id": video_req.ID, "auth_id": int(id.(float64)), "delete_time": time.Now().Format("2006-01-02 15:04:05"), "method": "delete"} c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": "delete video success"})
str, _ := json.Marshal(data)
worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", string(str))
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
} else { } else {
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "msg": "failed", "data": "delete video failed"})
} }
} else { } else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "msg": err, "data": "failed"})
} }
} }
func QuashOption(c *gin.Context) { func QuashOption(c *gin.Context) {
id, _ := c.Get("id") id, _ := c.Get("id")
user, _ := c.Get("username") id1 := int(id.(float64))
res := worker.PopRedisList(user.(string) + "-" + strconv.Itoa(int(id.(float64))) + "-option") res := worker.PopRedisList("user-" + strconv.Itoa(int(id.(float64))) + "-option")
if res != "" { if res != "" {
var retrievedData map[string]interface{} var retrievedData map[string]interface{}
err2 := json.Unmarshal([]byte(res), &retrievedData) err2 := json.Unmarshal([]byte(res), &retrievedData)
if err2 == nil { if err2 == nil {
code, msg := service.QuashVideo(int(id.(float64)), retrievedData) code, msg := service.QuashVideo(id1, retrievedData)
c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg}) c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg})
} else { } else {
worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", res) //未操作成功重新添加到队列 worker.PushRedisList("user-"+strconv.Itoa(int(id.(float64)))+"-option", res) //未操作成功重新添加到队列
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": err2, "data": "json解析错误"}) c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": err2, "data": "json解析错误"})
return return
} }

View File

@ -1,11 +1,14 @@
package service package service
import ( import (
"encoding/json"
"fmt" "fmt"
"strconv"
"strings" "strings"
"time" "time"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
"videoplayer/worker"
) )
func GetVideo(id, auth_id int) dao.Video { func GetVideo(id, auth_id int) dao.Video {
@ -16,7 +19,10 @@ func GetWillDelVideoList(id int) []dao.Video {
return dao.FindWillDelVideoList(id) return dao.FindWillDelVideoList(id)
} }
func GetVideoList(auth_id int, start, end, hour string) []dao.Video { func GetVideoList(auth_id, id int, start, end, hour string) []dao.Video {
if id > 0 {
return []dao.Video{dao.FindVideoByID(id, auth_id)}
}
if start == "" { if start == "" {
return dao.FindVideoListsByAuthID(auth_id) return dao.FindVideoListsByAuthID(auth_id)
} else { } else {
@ -59,9 +65,17 @@ func GetVideoListByPage(auth_id, page, page_size int) []dao.Video {
return dao.GetVideoListByPage(auth_id, page, page_size) return dao.GetVideoListByPage(auth_id, page, page_size)
} }
func DeleteVideo(id, user int) int { func DeleteVideo(id, user int, tp string) int {
if tp == "del_with_logic" {
data := map[string]interface{}{"id": id, "auth_id": user, "delete_time": time.Now().Format("2006-01-02 15:04:05"), "method": "delete"}
str, _ := json.Marshal(data)
worker.PushRedisList("user-"+strconv.Itoa(user)+"-option", string(str))
return dao.LogicDeleteVideoByID(id, user)
} else if tp == "del_with_real" {
return dao.DeleteVideoByID(id, user) return dao.DeleteVideoByID(id, user)
} }
return 0
}
func QuashVideo(user int, data map[string]interface{}) (int, string) { func QuashVideo(user int, data map[string]interface{}) (int, string) {
var res int var res int
@ -73,6 +87,14 @@ func QuashVideo(user int, data map[string]interface{}) (int, string) {
}() }()
switch data["method"] { switch data["method"] {
case "delete": case "delete":
video_id := int(data["id"].(float64))
res = dao.RollbackVideoByID(video_id, user)
if res == 0 {
msg = strconv.Itoa(video_id) + " rollback video error"
} else {
res = 0
msg = "success"
}
case "delay": case "delay":
if data["option"] == "all" { if data["option"] == "all" {
if dao.QuashAllDelay(user, data["delay_day"].(int)) == 0 { if dao.QuashAllDelay(user, data["delay_day"].(int)) == 0 {