diff --git a/README.md b/README.md index 406ce61..e4e1b4c 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,7 @@ 17 | 数据库数据操作失败 18 | uuid不存在 19 | Token解析错误 -20 | 获取redis错误 \ No newline at end of file +20 | 获取redis错误 +30 | 撤销 +31 | 撤销延迟操作失败、 +32 | 撤销操作失败 \ No newline at end of file diff --git a/dao/video.go b/dao/video.go index fe3f3b8..f3d972a 100644 --- a/dao/video.go +++ b/dao/video.go @@ -92,3 +92,19 @@ func DelayAllVideo(id, day int) int { } return int(res.RowsAffected) } + +func QuashOneDelay(id, user_id int, day int) int { + res := DB.Debug().Exec("update videos set delete_time = date_sub(delete_time, interval ? day) where id = ? and auth_id = ? and isdelete=0", day, id, user_id) + if res.Error != nil { + return 0 + } + return int(res.RowsAffected) +} + +func QuashAllDelay(user_id int, day int) int { + res := DB.Debug().Exec("update videos set delete_time = date_sub(delete_time, interval ? day) where auth_id = ? and isdelete=0", day, user_id) + if res.Error != nil { + return 0 + } + return int(res.RowsAffected) +} diff --git a/handler/video.go b/handler/video.go index 7726b7b..b3925a6 100644 --- a/handler/video.go +++ b/handler/video.go @@ -1,6 +1,7 @@ package handler import ( + "encoding/json" "fmt" "github.com/gin-gonic/gin" "net/http" @@ -8,6 +9,7 @@ import ( "strconv" "time" "videoplayer/service" + "videoplayer/worker" ) // video获取视频列表请求 @@ -77,6 +79,7 @@ func SetUpVideoGroup(router *gin.Engine) { videoGroup.POST("/delay", DelayVideo) videoGroup.POST("/delete", DeleteVideo) videoGroup.POST("/update", UpdateVideo) + videoGroup.POST("/quash_option", QuashOption) } func GetWillDelVideoList(c *gin.Context) { @@ -163,6 +166,7 @@ func GetVideoList(c *gin.Context) { func DelayVideo(c *gin.Context) { var delay_req delayReq id, _ := c.Get("id") + user, _ := c.Get("username") cnt := 0 if err := c.ShouldBind(&delay_req); err == nil { if delay_req.Day > 30 || delay_req.Day < 1 { @@ -178,6 +182,9 @@ func DelayVideo(c *gin.Context) { if cnt == 0 { c.JSON(http.StatusOK, gin.H{"code": 17, "data": "延迟失败影响数据", "message": "failed,cnt:" + strconv.Itoa(cnt)}) } else { + data := map[string]interface{}{"auth_id": int(id.(float64)), "delay_time": time.Now().Format("2006-01-02 15:04:05"), "delay_day": delay_req.Day, "option": delay_req.Option, "id": delay_req.ID} + str, _ := json.Marshal(data) + worker.PushRedisList(user.(string)+"-"+strconv.Itoa(delay_req.ID), string(str)) c.JSON(http.StatusOK, gin.H{"code": 0, "data": "延迟成功,影响记录:" + strconv.Itoa(cnt), "message": "success cnt:" + strconv.Itoa(cnt)}) } } else { @@ -188,9 +195,41 @@ 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 { - service.DeleteVideo(video_req.ID, int(id.(float64))) - c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"}) + res := service.DeleteVideo(video_req.ID, int(id.(float64))) + 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")} + str, _ := json.Marshal(data) + worker.PushRedisList(user.(string)+"-"+strconv.Itoa(video_req.ID), string(str)) + c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"}) + } else { + c.JSON(http.StatusOK, gin.H{"code": 9, "message": "failed"}) + } + } else { + c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"}) + } +} + +func QuashOption(c *gin.Context) { + var video_req VideoDelReq + id, _ := c.Get("id") + user, _ := c.Get("username") + if err := c.ShouldBind(&video_req); err == nil { + res := worker.PopRedisList(user.(string) + "-" + strconv.Itoa(video_req.ID)) + if res != "" { + var retrievedData map[string]interface{} + err2 := json.Unmarshal([]byte(res), &retrievedData) + if err2 != nil { + code, msg := service.QuashVideo(int(id.(float64)), retrievedData) + c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg}) + } else { + c.JSON(http.StatusOK, gin.H{"code": 9, "message": err2, "data": "2"}) + return + } + } else { + c.JSON(http.StatusOK, gin.H{"code": 9, "message": "redis data not found", "data": "error"}) + } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"}) } diff --git a/service/videoService.go b/service/videoService.go index 3c50e14..09e2a4d 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -65,6 +65,39 @@ func DeleteVideo(id, user int) int { return dao.DeleteVideoByID(id, user) } +func QuashVideo(user int, data map[string]interface{}) (int, string) { + var res int + var msg string + switch data["method"] { + case "delete": + case "delay": + if data["option"] == "all" { + if dao.QuashAllDelay(user, data["day"].(int)) == 0 { + res = 31 + msg = "quash all video delay error" + } else { + res = 0 + msg = "success" + } + } else if data["option"] == "one" { + if dao.QuashOneDelay(user, data["id"].(int), data["day"].(int)) == 0 { + res = 31 + msg = "quash one video error" + } else { + res = 0 + msg = "success" + } + + } else { + res = 32 + msg = "option error,no such delay option" + } + default: + res = 32 + msg = "method error,no such method" + } + return res, msg +} func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human, isDelete int, createTime, endTime string, fileSize int) bool { video := GetVideo(videoID, authID) if video.ID == 0 { diff --git a/worker/redis.go b/worker/redis.go index cf8941a..a6bd4f7 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -154,23 +154,26 @@ func GetRedis(key string) string { return val } -// pop redis list from left -func popRedisList(key string) string { +// pop redis list from right,as stack +func PopRedisList(key string) string { ctx := context.Background() - val, err := redisClient.LPop(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误 + val, err := redisClient.RPop(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误 if err != nil { fmt.Println("Error reading from Redis: %v", err) + return "" } return val } // push redis list from right -func pushRedisList(key string, value string) { +func PushRedisList(key string, value string) bool { ctx := context.Background() err := redisClient.RPush(ctx, key, value).Err() if err != nil { fmt.Println("Error setting key: %v", err) + return false } + return true } // delete redis key