luodangjia
2024-12-10 ee7ce5d1cbf80bee0a15c1e5bc5eaa30858d812b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.hollywood.applet.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hollywood.applet.dto.PutVideoDto;
import com.hollywood.applet.dto.ReportDto;
import com.hollywood.applet.service.TVideoReportService;
import com.hollywood.applet.service.TVideoService;
import com.hollywood.applet.utils.LoginInfoUtil;
import com.hollywood.common.basic.ApiResult;
import com.hollywood.common.basic.PageInfo;
import com.hollywood.common.model.TInformation;
import com.hollywood.common.model.TShortPlay;
import com.hollywood.common.model.TVideo;
import com.hollywood.common.model.TVideoReport;
import com.hollywood.common.redis.RedisAutoTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Set;
 
/**
 * <p>
 * 短视频管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/tVideo")
@Api(tags = "短视频")
public class TVideoController {
    @Autowired
    private LoginInfoUtil loginInfoUtil;
    @Autowired
    private RedisAutoTemplate redisAutoTemplate;
    @Autowired
    private TVideoService videoService;
    @Autowired
    private TVideoReportService reportService;
 
    @ApiOperation(value = "观看短视频接口")
    @PutMapping("/watch")
    public ApiResult watch(Long videoId)
    {
        Long userId = loginInfoUtil.getUserId();
//        redisAutoTemplate.addList(userId+":user:history:",videoId);
//        Set<Object> set = redisAutoTemplate.getSet(String.valueOf(userId));
 
        String str = redisAutoTemplate.getStr(videoId + ":views:");
        if (str ==null) {
            redisAutoTemplate.setStr(videoId + ":views:", "1");
        }else {
            String str1 = redisAutoTemplate.getStr(videoId + ":views:");
            redisAutoTemplate.setStr(videoId + ":views:", String.valueOf(Integer.parseInt(str1)+1));
        }
 
//        System.err.println(set);
        return ApiResult.success();
    }
    @ApiOperation(value = "短视频点赞")
    @PutMapping("/good")
    public ApiResult good(Long videoId)
    {
        Long userId = loginInfoUtil.getUserId();
        redisAutoTemplate.addSet(userId+":user:good:",videoId);
        redisAutoTemplate.addSet(videoId+":video:good:",userId);
        Set<Object> set = redisAutoTemplate.getSet(String.valueOf(userId));
        System.err.println(set);
 
        return ApiResult.success();
    }
    @ApiOperation(value = "短视频取消点赞")
    @PutMapping("/cancelgood")
    public ApiResult cancelgood(Long videoId)
    {
        Long userId = loginInfoUtil.getUserId();
//        redisAutoTemplate.removeZSet(userId+":user:good:",videoId);
        redisAutoTemplate.setRemove(userId+":user:good:",videoId);
//        redisAutoTemplate.removeZSet(videoId+":video:good:",userId);
        redisAutoTemplate.setRemove(videoId+":video:good:",userId);
        Set<Object> set = redisAutoTemplate.getSet(String.valueOf(userId));
        System.err.println(set);
 
        return ApiResult.success();
    }
 
 
    @ApiOperation(value = "获取十个推荐视频,刷十次调一次这个接口")
    @PutMapping("/get")
    public ApiResult<List<TVideo>> get()
    {
        Long userId = loginInfoUtil.getUserId();
        Set<Long> userVedio = redisAutoTemplate.getSet(userId+":user:history:");
        QueryWrapper<TVideo> id = new QueryWrapper<TVideo>().eq("status",1).eq("isDelete",0).orderByDesc("createTime").last("limit 10");
        if (!userVedio.isEmpty()){
            id.notIn("id", userVedio);
        }
        List<TVideo> vedios = videoService.list(id);
        if (vedios.size()<10){
            Integer num = 10-vedios.size();
            List<TVideo> list = videoService.list(Wrappers.lambdaQuery(TVideo.class).eq(TVideo::getStatus,1).eq(TVideo::getIsDelete,0).last("ORDER BY RAND() limit " + num));
            vedios.addAll(list);
        }
        //获取用户的点赞记录
        Set<Long> goods = redisAutoTemplate.getSet(userId+":user:good:");
        for (TVideo vedio : vedios) {
            //获取该视频的点赞用户
            Set<Long> goodsUser = redisAutoTemplate.getSet(vedio.getId()+":video:good:");
            vedio.setLikeCount(goodsUser.size());
            //判断当前用户是否点赞该视频
            if (goods!=null&&goods.contains(vedio.getId().intValue())){
                vedio.setIsGood(1);
            }else {
                vedio.setIsGood(0);
            }
            String str = redisAutoTemplate.getStr(vedio.getId() + ":views:");
            if (str != null && str.isEmpty()) {
                vedio.setViews(0);
            }else {
                if (str != null) {
                    vedio.setViews(Integer.valueOf(str));
                }
            }
        }
        return  ApiResult.success(vedios);
    }
 
 
    @ApiOperation(value = "获取已发布视频")
    @PutMapping("/get-has")
    public ApiResult<PageInfo<TVideo>> getHas(@RequestParam int pageNum,
                                          @RequestParam int  pageSize)
    {
        Long userId = loginInfoUtil.getUserId();
        PageInfo<TVideo> pageInfo = new PageInfo<>(pageNum, pageSize);
        List<TVideo> vedios = videoService.list(new QueryWrapper<TVideo>().eq("userId",userId).eq("status",1));
        //获取用户的点赞记录
        Set<Long> goods = redisAutoTemplate.getSet(userId+":user:good:");
        for (TVideo vedio : vedios) {
            //获取该视频的点赞用户
            Set<Integer> goodsUser = redisAutoTemplate.getSet(vedio.getId()+":video:good:");
            vedio.setLikeCount(goodsUser.size());
            //判断当前用户是否点赞该视频
            if (goods!=null&&goods.contains(vedio.getId().intValue())){
                vedio.setIsGood(1);
            }else {
                vedio.setIsGood(0);
            }
            String str = redisAutoTemplate.getStr(vedio.getId() + ":views:");
            if (str != null && str.isEmpty()) {
                vedio.setViews(0);
            }else {
                if (str != null) {
                    vedio.setViews(Integer.valueOf(str));
                }
            }
        }
        pageInfo.setRecords(vedios);
 
        return  ApiResult.success(pageInfo);
    }
 
    @ApiOperation(value = "发布视频")
    @PutMapping("/put")
    public ApiResult<TVideo> put(@RequestBody PutVideoDto title)
    {
        Long userId = loginInfoUtil.getUserId();
        TVideo tVideo = new TVideo();
            tVideo.setVideoTitle(title.getTitle());
            tVideo.setVideoFile(title.getUrl());
            tVideo.setUserId(userId);
        tVideo.setStatus(1);
            videoService.save(tVideo);
        return  ApiResult.success(tVideo);
    }
 
    @ApiOperation(value = "删除视频")
    @PostMapping("/delete/{id}")
    public ApiResult delete(@PathVariable Long id)
    {
        videoService.removeById(id);
        return ApiResult.success();
    }
 
 
    @ApiOperation(value = "举报视频")
    @PostMapping("/report")
    public ApiResult report(@RequestBody ReportDto reportDto)
    {
        Long userId = loginInfoUtil.getUserId();
        TVideoReport tVideoReport = new TVideoReport();
        BeanUtils.copyProperties(reportDto,tVideoReport);
        tVideoReport.setUserId(userId);
 
 
        reportService.save(tVideoReport);
        return ApiResult.success();
 
    }
 
 
 
 
 
}