From d8967012d344c070e5211f50dba540c9ef893a11 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 17 Dec 2024 17:16:22 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=A0=B9=E6=8D=AEid=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/video.go | 3 ++- service/videoService.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/handler/video.go b/handler/video.go index 341e005..41c0974 100644 --- a/handler/video.go +++ b/handler/video.go @@ -15,6 +15,7 @@ import ( // video获取视频列表请求 type gvlReq struct { + ID int `json:"id" form:"id"` StartTime int64 `json:"begin" form:"begin"` EndTime int64 `json:"end" form:"end"` IP string `json:"ip" form:"ip"` @@ -164,7 +165,7 @@ func GetVideoList(c *gin.Context) { tm1 = "" 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"}) } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) diff --git a/service/videoService.go b/service/videoService.go index c94046a..fd61dba 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -16,7 +16,10 @@ func GetWillDelVideoList(id int) []dao.Video { 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 == "" { return dao.FindVideoListsByAuthID(auth_id) } else { From e9f089c6a2b1add01489881a0e149a6a79dab60d Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 17 Dec 2024 17:57:26 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8F=8A=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/video.go | 23 ++++++++++++++++++----- handler/video.go | 13 +++++++------ service/videoService.go | 9 +++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/dao/video.go b/dao/video.go index 4124836..5cc8dca 100644 --- a/dao/video.go +++ b/dao/video.go @@ -1,8 +1,8 @@ package dao import ( + "fmt" "gorm.io/gorm" - "time" ) type Video struct { @@ -37,10 +37,23 @@ func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete return video.ID } +// 文件已删除 func DeleteVideoByID(id, user int) int { - delete_time := time.Now().Format("2006-01-02 15:04:05") - DB.Where("id = ? and auth_id = ?", id, user).Updates(&Video{DeleteTime: delete_time, IsDelete: 1}) - DB.Where("id = ? and auth_id = ?", id, user).Delete(&Video{}) + 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) + if res.Error != nil { + 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 } @@ -71,7 +84,7 @@ func FindVideoListsByAuthID(auth_id int) []Video { func FindVideoListByTime(auth_id int, startTime, endTime string) []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 } diff --git a/handler/video.go b/handler/video.go index 41c0974..d3dbb7e 100644 --- a/handler/video.go +++ b/handler/video.go @@ -60,12 +60,13 @@ type videoPReq struct { } type VideoDelReq struct { - ID int `json:"id"` + ID int `json:"id" form:"id"` + Type string `json:"type" form:"type"` } // video延迟视频请求 type delayReq struct { - ID int `form:"id"` + ID int `json:"id" form:"id"` Option string `form:"option"` AuthId int `form:"userId"` IP string `form:"ip"` @@ -206,17 +207,17 @@ func DeleteVideo(c *gin.Context) { id, _ := c.Get("id") user, _ := c.Get("username") 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 { 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"} 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"}) + c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": "delete video success"}) } 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 { - 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"}) } } diff --git a/service/videoService.go b/service/videoService.go index fd61dba..2dfe74a 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -62,8 +62,13 @@ func GetVideoListByPage(auth_id, page, page_size int) []dao.Video { return dao.GetVideoListByPage(auth_id, page, page_size) } -func DeleteVideo(id, user int) int { - return dao.DeleteVideoByID(id, user) +func DeleteVideo(id, user int, tp string) int { + if tp == "del_with_logic" { + return dao.LogicDeleteVideoByID(id, user) + } else if tp == "del_with_real" { + return dao.DeleteVideoByID(id, user) + } + return 0 } func QuashVideo(user int, data map[string]interface{}) (int, string) { From fb3d2e8e673165d9489c5345275ff687f6558eba Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 17 Dec 2024 18:05:08 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=85=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E8=A7=86=E9=A2=91=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/video.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/video.go b/dao/video.go index 5cc8dca..ed08de2 100644 --- a/dao/video.go +++ b/dao/video.go @@ -21,7 +21,7 @@ type Video struct { func FindWillDelVideoList(id int) []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 } From a803991a420f1dd8e7509d55ed73c15c57283261 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 17 Dec 2024 18:31:14 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=9B=9E=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/video.go | 10 ++++++++++ handler/video.go | 12 ++++-------- service/videoService.go | 13 +++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/dao/video.go b/dao/video.go index ed08de2..87e2bd4 100644 --- a/dao/video.go +++ b/dao/video.go @@ -57,6 +57,16 @@ func LogicDeleteVideoByID(id, user int) int { 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 +} + func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human, isDelete int, createTime, endTime string, fileSize int) bool { res := DB.Model(&Video{}).Where("id = ? and auth_id = ?", videoID, authID).Updates(Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize}) if res.Error != nil { diff --git a/handler/video.go b/handler/video.go index d3dbb7e..f088f25 100644 --- a/handler/video.go +++ b/handler/video.go @@ -205,13 +205,9 @@ func DelayVideo(c *gin.Context) { func DeleteVideo(c *gin.Context) { var video_req VideoDelReq id, _ := c.Get("id") - user, _ := c.Get("username") if err := c.ShouldBind(&video_req); err == nil { res := service.DeleteVideo(video_req.ID, int(id.(float64)), video_req.Type) 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"} - 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, "msg": "success", "data": "delete video success"}) } else { c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "msg": "failed", "data": "delete video failed"}) @@ -223,16 +219,16 @@ func DeleteVideo(c *gin.Context) { func QuashOption(c *gin.Context) { id, _ := c.Get("id") - user, _ := c.Get("username") - res := worker.PopRedisList(user.(string) + "-" + strconv.Itoa(int(id.(float64))) + "-option") + id1 := int(id.(float64)) + res := worker.PopRedisList("user-" + strconv.Itoa(int(id.(float64))) + "-option") if res != "" { var retrievedData map[string]interface{} err2 := json.Unmarshal([]byte(res), &retrievedData) 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}) } 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解析错误"}) return } diff --git a/service/videoService.go b/service/videoService.go index 2dfe74a..b47119a 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -1,11 +1,14 @@ package service import ( + "encoding/json" "fmt" + "strconv" "strings" "time" "videoplayer/dao" "videoplayer/proto" + "videoplayer/worker" ) func GetVideo(id, auth_id int) dao.Video { @@ -64,6 +67,9 @@ func GetVideoListByPage(auth_id, page, page_size int) []dao.Video { 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) @@ -81,6 +87,13 @@ func QuashVideo(user int, data map[string]interface{}) (int, string) { }() switch data["method"] { 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 { + msg = "success" + } case "delay": if data["option"] == "all" { if dao.QuashAllDelay(user, data["delay_day"].(int)) == 0 { From 31ef5ee93b6200fa5009ead51d75dac2ab74025c Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 17 Dec 2024 18:57:57 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=9B=9E=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/videoService.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/videoService.go b/service/videoService.go index b47119a..f3df113 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -92,6 +92,7 @@ func QuashVideo(user int, data map[string]interface{}) (int, string) { if res == 0 { msg = strconv.Itoa(video_id) + " rollback video error" } else { + res = 0 msg = "success" } case "delay":