admin
2021-03-06 7804263c6061aef813f0db27cb3046f746572606
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.yeshi.buwan.service.imp.juhe;
 
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.juhe.funtv.FunTVAlbum2Dao;
import com.yeshi.buwan.dao.juhe.funtv.FunTVShortVideo2Dao;
import com.yeshi.buwan.dao.juhe.funtv.FunTVVideo2Dao;
import com.yeshi.buwan.dao.juhe.funtv.VideoFunTV2Dao;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.domain.ResourceVideo;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.entity.PlayUrl;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.funtv.FunTVUtil2;
import com.yeshi.buwan.funtv.entity.FunTVAlbum2;
import com.yeshi.buwan.funtv.entity.FunTVShortVideo2;
import com.yeshi.buwan.funtv.entity.FunTVVideo2;
import com.yeshi.buwan.funtv.entity.VideoFunTV2;
import com.yeshi.buwan.service.imp.CategoryVideoService;
import com.yeshi.buwan.service.imp.ResourceVideoService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.imp.VideoResourceService;
import com.yeshi.buwan.service.inter.juhe.FunTV2Service;
import com.yeshi.buwan.util.mq.CMQManager;
import com.yeshi.buwan.util.video.VideoConstant;
import com.yeshi.buwan.vo.AcceptData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
 
@Service
public class FunTV2ServiceImpl implements FunTV2Service {
 
    @Resource
    private FunTVAlbum2Dao funTVAlbum2Dao;
 
    @Resource
    private FunTVVideo2Dao funTVVideo2Dao;
 
    @Resource
    private FunTVShortVideo2Dao funTVShortVideo2Dao;
 
    @Resource
    private VideoFunTV2Dao videoFunTV2Dao;
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private VideoInfoDao videoInfoDao;
 
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
 
    @Resource
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private CategoryVideoService categoryVideoService;
 
    @Resource
    private VideoResourceService videoResourceService;
 
 
    @Override
    public void saveAlbum(FunTVAlbum2 album2) {
        funTVAlbum2Dao.save(album2);
    }
 
    @Override
    public void saveVideo(FunTVVideo2 video) {
        funTVVideo2Dao.save(video);
    }
 
    @Override
    public void saveShortVideo(FunTVShortVideo2 shortVideo2) {
        funTVShortVideo2Dao.save(shortVideo2);
    }
 
    @Override
    public void addToVideoInfo(FunTVAlbum2 album) {
        if (album.getEpisodes() != null && album.getEpisodes().size() <= 0)
            return;
 
        VideoInfo newVideoInfo = FunTVUtil2.convertFunTVAlbumToVideoInfo(album);
        VideoFunTV2 videoFunTV2 = videoFunTV2Dao.selectByMediaId(album.getId());
        if (videoFunTV2 != null) {//渠道视频已经存在
            VideoInfo videoInfo = videoInfoService.getVideoInfo(videoFunTV2.getVideoId() + "");
            if (videoInfo == null)
                return;
            newVideoInfo.setId(videoInfo.getId());
            if (!newVideoInfo.getTag().equalsIgnoreCase(videoInfo.getTag())) {
                newVideoInfo.setCreatetime(videoInfo.getCreatetime());
                //更新信息tag信息
                videoInfo.setVideocount(newVideoInfo.getVideocount());
                videoInfo.setTag(newVideoInfo.getTag());
                videoInfo.setPicture(newVideoInfo.getPicture());
                videoInfo.setHpicture(newVideoInfo.getHpicture());
                videoInfo.setVpicture(newVideoInfo.getVpicture());
                videoInfo.setShow(newVideoInfo.getShow());
                videoInfo.setVideoType(newVideoInfo.getVideoType());
                videoInfoDao.update(videoInfo);
            }
        } else {//视频不存在
            //判断2个视频实体是否为同一视频
            VideoInfo oldVideo = videoInfoService.getExistSameVideoWithTime(newVideoInfo);
            if (oldVideo == null) {
                //添加视频
                Serializable id = videoInfoDao.save(newVideoInfo);
                newVideoInfo.setId(id + "");
            } else {//为同一视频
                //更新tag
                newVideoInfo.setId(oldVideo.getId());
                oldVideo.setVideocount(newVideoInfo.getVideocount());
                oldVideo.setTag(newVideoInfo.getTag());
                oldVideo.setPicture(newVideoInfo.getPicture());
                oldVideo.setHpicture(newVideoInfo.getHpicture());
                oldVideo.setVpicture(newVideoInfo.getVpicture());
                oldVideo.setShow(newVideoInfo.getShow());
                oldVideo.setVideoType(newVideoInfo.getVideoType());
                videoInfoDao.update(oldVideo);
            }
 
            //加入专辑视频映射
            AlbumVideoMap map = new AlbumVideoMap();
            map.setCreateTime(new Date());
            map.setVideoId(newVideoInfo.getId());
            map.setRootVideoType(newVideoInfo.getVideoType().getId());
            map.setVideoCount(newVideoInfo.getVideocount());
            albumVideoMapDao.save(map);
 
            //加入映射
            VideoFunTV2 vi = new VideoFunTV2();
            vi.setMediaId(album.getId());
            vi.setVideoId(Long.parseLong(newVideoInfo.getId()));
            videoFunTV2Dao.save(vi);
        }
 
        //添加视频来源映射
        resourceVideoService.addVideoResource(newVideoInfo.getId(), FunTVUtil2.RESOURCE_ID + "");
        //添加视频分类映射
        categoryVideoService.addCategoryVideo(newVideoInfo.getId(), newVideoInfo.getVideoType().getId());
        CMQManager.getInstance().addSolrMsg(newVideoInfo.getId());
    }
 
    @Override
    public FunTVAlbum2 getAlbumDetail(String id) {
        FunTVAlbum2 album2 = funTVAlbum2Dao.get(id);
        if (album2 == null)
            return null;
        List<FunTVVideo2> video2List = listAllEpisodeList(id);
        album2.setEpisodes(video2List);
        return album2;
    }
 
    private List<FunTVVideo2> getEpisodeList(String mediaId, int page, int pageSize) {
        List<FunTVVideo2> video2List = funTVVideo2Dao.listByMediaId(mediaId, (page - 1) * pageSize, pageSize);
        return video2List;
    }
 
    private List<FunTVVideo2> listAllEpisodeList(String mediaId) {
        int page = 1;
        int pageSize = 100;
        List<FunTVVideo2> video2List = new ArrayList<>();
        List<FunTVVideo2> tempList = new ArrayList<>();
        do {
            tempList = getEpisodeList(mediaId, page, pageSize);
            if (tempList != null)
                video2List.addAll(tempList);
            page++;
        } while (tempList != null && tempList.size() > 0);
        return video2List;
    }
 
 
    @Override
    public List<VideoDetailInfo> getVideoDetailList(String id, int page, int pageSize) {
        VideoFunTV2 videoFunTV2 = videoFunTV2Dao.selectByVideoId(id);
        if (videoFunTV2 == null)
            return null;
        FunTVAlbum2 album2 = funTVAlbum2Dao.get(videoFunTV2.getMediaId());
        if (album2 == null)
            return null;
 
        List<FunTVVideo2> episodeList = getEpisodeList(album2.getId(), page, pageSize);
 
        List<VideoDetailInfo> list = new ArrayList<>();
        if (episodeList != null)
            for (FunTVVideo2 video2 : episodeList)
                list.add(FunTVUtil2.convertFunTVVideoToVideoDetail(video2, album2));
        return list;
    }
 
    @Override
    public VideoDetailInfo getLatestVideoDetail(String id) {
        VideoFunTV2 videoFunTV2 = videoFunTV2Dao.selectByVideoId(id);
        if (videoFunTV2 == null)
            return null;
        FunTVAlbum2 album2 = getAlbumDetail(videoFunTV2.getMediaId());
        if (album2 != null && album2.getEpisodes() != null && album2.getEpisodes().size() > 0)
            return FunTVUtil2.convertFunTVVideoToVideoDetail(album2.getEpisodes().get(album2.getEpisodes().size() - 1), album2);
        return null;
    }
 
 
    @Override
    public int getShowType(String videoid) {
        VideoInfo videoInfo = videoInfoDao.find(VideoInfo.class, videoid);
        if (videoInfo.getVideoType() != null && (videoInfo.getVideoType().getId() == VideoConstant.VIDEO_CATEGORY_DIANSHIJU || videoInfo.getVideoType().getId() == VideoConstant.VIDEO_CATEGORY_DONGMAN))
            return 2;
        else
            return 1;
    }
 
    @Override
    public PlayUrl getPlayUrl(AcceptData acceptData, String detailSystemId, int resourceId, String id, String videoId) {
        FunTVVideo2 video2 = funTVVideo2Dao.get(id);
        VideoResource vr = videoResourceService.getResource(resourceId + "");
        int t = FunTVUtil2.getPlayType(acceptData,video2);
        PlayUrl pu = new PlayUrl();
        pu.setParams("");
        pu.setPlayType(t);
        pu.setResource(vr);
        pu.setAid(video2.getMediaId());
        pu.setVid(video2.getId());
        pu.setCode(video2.getCeCode());
        if (t == FunTVUtil2.PLAY_NONE) {
            pu.setUrl("");
        } else {
            pu.setUrl(video2.getFunH5Url());
        }
        return pu;
    }
 
    /**
     * 处理专辑
     *
     * @param album2
     */
    @Transactional
    public void processAlbum(FunTVAlbum2 album2) {
        if ("1".equalsIgnoreCase(album2.getStatus()))
            addToVideoInfo(album2);
        else
            offLineAlbum(album2.getId());
 
    }
 
    @Transactional
    @Override
    public void offLineAlbum(String mediaId) {
        //查询出对应的视频ID
        VideoFunTV2 videoFunTV2 = videoFunTV2Dao.selectByMediaId(mediaId);
        if (videoFunTV2 == null)
            return;
        videoFunTV2Dao.deleteByVideoId(videoFunTV2.getVideoId());
        resourceVideoService.delete(videoFunTV2.getVideoId()+"",FunTVUtil2.RESOURCE_ID+"");
        //删除媒体与媒体对应的视频
        funTVAlbum2Dao.delete(mediaId);
        List<FunTVVideo2> video2List = listAllEpisodeList(mediaId);
        if (video2List != null)
            for (FunTVVideo2 video2 : video2List)
                funTVVideo2Dao.delete(video2.getId());
    }
 
}