添加撤销操作功能,未测试

This commit is contained in:
junleea 2024-06-24 17:58:22 +08:00
parent 062fb6f4b6
commit 554c70bec8
5 changed files with 101 additions and 7 deletions

View File

@ -24,3 +24,6 @@
18 | uuid不存在
19 | Token解析错误
20 | 获取redis错误
30 | 撤销
31 | 撤销延迟操作失败、
32 | 撤销操作失败

View File

@ -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)
}

View File

@ -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"})
}

View File

@ -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 {

View File

@ -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