diff --git a/dao/video.go b/dao/video.go index 4124836..87e2bd4 100644 --- a/dao/video.go +++ b/dao/video.go @@ -1,8 +1,8 @@ package dao import ( + "fmt" "gorm.io/gorm" - "time" ) type Video struct { @@ -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 } @@ -37,10 +37,33 @@ 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 +} + +// 回滚视频输出-逻辑删除,若文件已删除则不可回滚 +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 } @@ -71,7 +94,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 341e005..f088f25 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"` @@ -59,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"` @@ -164,7 +166,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"}) @@ -203,34 +205,30 @@ 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))) + 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"}) } } 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 c94046a..f3df113 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 { @@ -16,7 +19,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 { @@ -59,8 +65,16 @@ 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" { + 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 0 } func QuashVideo(user int, data map[string]interface{}) (int, string) { @@ -73,6 +87,14 @@ 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 { + res = 0 + msg = "success" + } case "delay": if data["option"] == "all" { if dao.QuashAllDelay(user, data["delay_day"].(int)) == 0 {