admin
2021-08-27 8fee151ffae0c3818694b7318583814bf92663e2
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.yeshi.buwan.service.imp.juhe;
 
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.juhe.iqiyi.IqiyiAlbum2Dao;
import com.yeshi.buwan.dao.juhe.iqiyi.VideoIqiyi2Dao;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.domain.*;
import com.yeshi.buwan.domain.entity.PlayUrl;
import com.yeshi.buwan.domain.solr.SolrAlbumVideo;
import com.yeshi.buwan.domain.system.SystemConfig;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.dto.mq.UpdateResourceVideoMQMsg;
import com.yeshi.buwan.exception.video.IqiyiVideoSolrException;
import com.yeshi.buwan.videos.iqiyi.IqiYiNewAPI;
import com.yeshi.buwan.videos.iqiyi.entity.IqiyiAlbum2;
import com.yeshi.buwan.videos.iqiyi.entity.VideoIqiyi2;
import com.yeshi.buwan.videos.iqiyi.util.IqiyiUtil;
import com.yeshi.buwan.videos.iqiyi.util.IqiyiUtil2;
import com.yeshi.buwan.query.Iqiyi2AlbumQuery;
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.AlbumVideoMapService;
import com.yeshi.buwan.service.inter.juhe.Iqiyi2Service;
import com.yeshi.buwan.service.inter.system.SystemConfigService;
import com.yeshi.buwan.service.manager.search.SolrAlbumVideoDataManager;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.ThreadUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.log.VideoLogFactory;
import com.yeshi.buwan.util.mq.CMQManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
 
@Service
public class Iqiyi2ServiceImpl implements Iqiyi2Service {
 
    private final Logger logger = LoggerFactory.getLogger("videoUpdate");
 
    @Resource
    private VideoIqiyi2Dao videoIqiyi2Dao;
 
    @Resource
    private IqiyiAlbum2Dao iqiyiAlbum2Dao;
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private VideoInfoDao videoInfoDao;
 
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
 
    @Resource
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private CategoryVideoService categoryVideoService;
 
    @Resource
    private VideoResourceService videoResourceService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private AlbumVideoMapService albumVideoMapService;
 
    @Resource
    private SolrAlbumVideoDataManager solrAlbumVideoDataManager;
 
    public List<VideoDetailInfo> getVideoDetailList(String videoId, int page, int pageSize) {
        //查询专辑
        VideoIqiyi2 videoIqiyi2 = videoIqiyi2Dao.selectByVideoId(Long.parseLong(videoId));
        if (videoIqiyi2 == null)
            return null;
 
        final IqiyiAlbum2 album = iqiyiAlbum2Dao.get(videoIqiyi2.getIqiyiId());
//        if (album != null) {
        ThreadUtil.run(new Runnable() {
            @Override
            public void run() {
                CMQManager.getInstance().addUpdateResourceVideoMsg(new UpdateResourceVideoMQMsg(videoIqiyi2.getIqiyiId() + "", IqiyiUtil2.RESOURCE_ID, new Date()));
            }
        });
//        }
 
        List<IqiyiAlbum2> album2List = new ArrayList<>();
        if (album.getFeatureAlbumId() > 0) {//单视频
            album2List.add(album);
        } else {//专辑
            //判断period字段是否为空
            int sort = album.isSourceAlbum() ? IqiyiAlbum2.SORT_PERIOD : IqiyiAlbum2.SORT_ORDER;
            Iqiyi2AlbumQuery query = new Iqiyi2AlbumQuery();
            query.setFeatureAlbumId(album.getId());
            query.setContentType(1);
            List<IqiyiAlbum2> list = iqiyiAlbum2Dao.listByAid(query, sort, (page - 1) * pageSize, pageSize);
            if (list != null)
                album2List.addAll(list);
        }
        List<VideoDetailInfo> detailInfoList = new ArrayList<>();
        for (IqiyiAlbum2 a : album2List)
            detailInfoList.add(IqiyiUtil2.convertAlbumToVideoDetail(a));
        return detailInfoList;
    }
 
    // 获取最新一集的详情
    public VideoDetailInfo getLatestVideoDetail(String videoId) {
        VideoIqiyi2 videoIqiyi2 = videoIqiyi2Dao.selectByVideoId(Long.parseLong(videoId));
        if (videoIqiyi2 == null)
            return null;
        IqiyiAlbum2 album = iqiyiAlbum2Dao.get(videoIqiyi2.getIqiyiId());
        if (album.getFeatureAlbumId() > 0) {//专辑
            if (album.getLatestVideo() != null) {
                album = iqiyiAlbum2Dao.get(album.getLatestVideo().getId());
            } else
                album = null;
        }
        if (album != null)
            return IqiyiUtil2.convertAlbumToVideoDetail(album);
        return null;
    }
 
    @Override
    public void saveIqiyiAlbum(IqiyiAlbum2 album) {
        iqiyiAlbum2Dao.save(album);
    }
 
    @Override
    public void offlineIqiyiAlbum(Long id) {
        //删除专辑
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("id").is(id), Criteria.where("featureAlbumId").is(id)));
        //删除专辑下的视频
        iqiyiAlbum2Dao.delete(query);
    }
 
 
    public boolean isUnNormalUpdateVideoName(String name) {
        SystemConfig config = systemConfigService.getConfigByKeyCache("iqiyi_update_video_name");
        if (config != null) {
            String[] sts = config.getValue().split(",");
            List<String> list = new ArrayList<>();
            for (String st : sts) {
                list.add(st.trim());
            }
            return list.contains(name.trim());
        }
        return false;
    }
 
    @Override
    public void addToVideoInfo(IqiyiAlbum2 album) {
        if (album.getFeatureAlbumId() > 0L && album.getChannelId() != IqiYiNewAPI.TYPE_DIANYING) {
            return;
        }
 
        if (StringUtil.isNullOrEmpty(album.getName())) {
            return;
        }
 
 
        //只缓存有效的
        if (album.getEffect() == 0)
            return;
 
        //只缓存正片
        if (album.getContentType() != 1)
            return;
 
        //综艺必须要有内容
        if (album.getChannelId() == 6) {
            if (album.getTvQipuIds().size() == 0)
                return;
        }
 
 
        if (album.getChannelId() != 1 && !isUnNormalUpdateVideoName(album.getName())) {
            //标题中只能包含中英文与数字
            String regx = "^[(\\u4e00-\\u9fa5)(:)( )a-zA-Z0-9]+$";
            if (!Pattern.matches(regx, album.getName())) {
                return;
            }
        }
 
        //过滤某些短片与影评
        if (album.getName().contains("《") && !isUnNormalUpdateVideoName(album.getName()))
            return;
 
        //空电视剧或者是空动漫则返回
        if (album.getChannelId() == 2 || album.getChannelId() == 4) {
            if (album.getTvQipuIds() == null || album.getTvQipuIds().size() == 0)
                return;
        }
 
        logger.info(VideoLogFactory.createAddToVideoLog(album));
 
        VideoInfo newVideoInfo = convertAlbumToVideoInfo(album);
        VideoIqiyi2 videoIqiyi2 = videoIqiyi2Dao.selectByIqiyiId(album.getId());
        if (videoIqiyi2 != null) {//渠道视频已经存在
            VideoInfo videoInfo = videoInfoService.getVideoInfo(videoIqiyi2.getVideoId() + "");
            if (videoInfo == null)
                return;
            newVideoInfo.setId(videoInfo.getId());
            if (!newVideoInfo.getTag().equalsIgnoreCase(videoInfo.getTag()) || newVideoInfo.getVideocount() != videoInfo.getVideocount()) {
                newVideoInfo.setCreatetime(videoInfo.getCreatetime());
                //更新信息tag信息
                videoInfo.setLatestHpicture(newVideoInfo.getLatestHpicture());
                videoInfo.setLatestVpicture(newVideoInfo.getLatestVpicture());
                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());
                videoInfo.setUpdatetime(newVideoInfo.getUpdatetime());
                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.setLatestHpicture(newVideoInfo.getLatestHpicture());
                oldVideo.setLatestVpicture(newVideoInfo.getLatestVpicture());
                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());
                oldVideo.setUpdatetime(newVideoInfo.getUpdatetime());
                videoInfoDao.update(oldVideo);
            }
 
            //加入专辑视频映射
            AlbumVideoMap map = new AlbumVideoMap();
            map.setId(newVideoInfo.getId());
            map.setCreateTime(new Date());
            map.setVideoId(newVideoInfo.getId());
            map.setRootVideoType(newVideoInfo.getVideoType().getId());
            map.setVideoCount(newVideoInfo.getVideocount());
            albumVideoMapDao.save(map);
 
            //加入映射
            VideoIqiyi2 vi = new VideoIqiyi2();
            vi.setIqiyiId(album.getId());
            vi.setVideoId(Long.parseLong(newVideoInfo.getId()));
            videoIqiyi2Dao.save(vi);
        }
 
        //添加视频来源映射
        resourceVideoService.addVideoResource(newVideoInfo.getId(), IqiyiUtil2.RESOURCE_ID + "");
        //添加视频分类映射
        categoryVideoService.addCategoryVideo(newVideoInfo.getId(), newVideoInfo.getVideoType().getId());
 
        CMQManager.getInstance().addSolrMsg(newVideoInfo.getId());
    }
 
    @Override
    public PlayUrl getPlayUrl(String detailSystemId, int resourceId, String id, String videoId) {
 
        IqiyiAlbum2 album = iqiyiAlbum2Dao.get(Long.parseLong(id));
        VideoResource vr = videoResourceService.getResource(resourceId + "");
        int t = IqiyiUtil2.getPlayType(album);
        PlayUrl pu = new PlayUrl();
        pu.setParams("");
        pu.setPlayType(t);
        pu.setResource(vr);
 
        if (t == IqiyiUtil2.PLAY_NONE) {
            pu.setUrl("");
        } else if (t == IqiyiUtil2.PLAY_HTML) {
            pu.setUrl(album.getH5Url());
        }
 
        //韩剧
        if ("48".equalsIgnoreCase(detailSystemId)) {
            pu.setUrl(pu.getUrl().replace("_bwap", "_hjvap"));
        }
 
        return pu;
    }
 
    @Override
    public int getShowType(String videoid) {
        VideoIqiyi2 iqiyi2 = videoIqiyi2Dao.selectByVideoId(Long.parseLong(videoid));
        if (iqiyi2 != null) {
            IqiyiAlbum2 album2 = iqiyiAlbum2Dao.get(iqiyi2.getIqiyiId());
            if (album2 != null) {
                if (album2.getChannelId() == 4 || album2.getChannelId() == 2) {
                    if (album2.isSourceAlbum()) {
                        return 1;
                    } else
                        return 2;
                }
            }
        }
 
        return 1;
    }
 
    @Override
    public VideoInfo convertAlbumToVideoInfo(IqiyiAlbum2 album) {
        Calendar ca = Calendar.getInstance();
        if (album.getPeriod().length() >= 8)
            ca.setTimeInMillis(TimeUtil.convertGernalTime(album.getPeriod(), "yyyyMMdd"));
        else
            ca.setTimeInMillis(TimeUtil.convertGernalTime(album.getPeriod(), "yyyy"));
        int day = ca.get(Calendar.DAY_OF_MONTH);
        int month = ca.get(Calendar.MONTH) + 1;
        int year = ca.get(Calendar.YEAR);
 
        VideoInfo video = new VideoInfo();
        video.setVideoType(new VideoType(IqiyiUtil.getType(album.getContentType())));
        video.setArea(album.getAreas());
        video.setBaseurl(album.getH5Url());
        video.setCanSave(false);
        video.setCommentCount(0);
        video.setCreatetime(System.currentTimeMillis());
        video.setDay(day + "");
        video.setYear(year + "");
        video.setMonth(month + "");
        video.setDirector(album.getDirectors());
        video.setDuration(album.getDuration() + "");
        video.setFinish("1");
        video.setIntroduction(album.getDesc());
        video.setMainActor(album.getMainCharacters());
        video.setName(album.getName());
        if (album.getLatestVideo() != null)
            video.setNowNumber(album.getLatestVideo().getOrder() + "");
        else
            video.setNowNumber("1");
        video.setOrderby("0" + "");
        video.setPicture(album.getImageUrl());
 
        video.setVpicture(IqiyiUtil.getVPicture(video.getPicture()));
        video.setHpicture(IqiyiUtil.getHPicture(video.getPicture()));
 
        if (album.getStatistics() != null)
            video.setScore(album.getStatistics().getScore() + "");
        video.setShare("0");
        video.setShow("1");
 
        video.setThirdType("1");
        video.setTotalNumber("0");
 
        video.setWatchCount(0 + "");
 
        video.setAdmin(new AdminInfo("1"));
        video.setContentType(album.getContentType());
        video.setCommentCount(0);
 
        String tag = IqiyiUtil2.getAlbumTag(album);
        video.setTag(tag);
 
        if (album.getLatestVideo() != null) {
            //查询最近的图片
            IqiyiAlbum2 album2 = iqiyiAlbum2Dao.get(album.getLatestVideo().getId());
            if (album2 != null) {
                video.setLatestHpicture(IqiyiUtil.getHPicture(album2.getImageUrl()));
                video.setLatestVpicture(IqiyiUtil.getVPicture(album2.getImageUrl()));
            }
        }
        long videoCount = iqiyiAlbum2Dao.countVideoByAid(album.getId());
        if (videoCount == 0L)
            videoCount = 1;
 
        video.setVideocount((int) videoCount);
 
        video.setUpdatetime(System.currentTimeMillis() + "");
 
        video.setVideoType(IqiyiUtil2.getVideoType(album));
        return video;
    }
 
    @Override
    public IqiyiAlbum2 selectAlbumById(Long id) {
        return iqiyiAlbum2Dao.get(id);
    }
 
    @Override
    public long countById(Long id) {
        return iqiyiAlbum2Dao.countById(id);
    }
 
    @Override
    public void deleteByAid(long id) {
        long count = iqiyiAlbum2Dao.countById(id);
        if (count > 0) {
            iqiyiAlbum2Dao.delete(id);
        }
        VideoIqiyi2 bean = videoIqiyi2Dao.selectByIqiyiId(id);
        if (bean != null) {
            videoIqiyi2Dao.deleteById(bean.getVideoId());
            //删除资源
            resourceVideoService.delete(bean.getVideoId() + "", IqiyiUtil2.RESOURCE_ID + "");
            resourceVideoService.delete(bean.getVideoId() + "", IqiyiUtil.RESOURCE_ID + "");
        }
 
    }
 
    @Override
    public List<IqiyiAlbum2> listByIds(List<Long> idsList) {
        return iqiyiAlbum2Dao.listByIds(idsList);
    }
 
    @Override
    public void validAlbumSolrState(Long albumId) throws IqiyiVideoSolrException {
        long count = iqiyiAlbum2Dao.countById(albumId);
        if (count <= 0L) {
            throw new IqiyiVideoSolrException(IqiyiVideoSolrException.CODE_ALBUM_NOT_SAVE, "专辑尚未缓存");
        }
        VideoIqiyi2 videoIqiyi2 = videoIqiyi2Dao.selectByIqiyiId(albumId);
        if (videoIqiyi2 == null) {
            throw new IqiyiVideoSolrException(IqiyiVideoSolrException.CODE_ALBUM_NOT_ADD_TO_VIDEO, "专辑尚未添加到视频总览");
        }
 
 
        AlbumVideoMap albumVideoMap = albumVideoMapService.selectByVideoId(videoIqiyi2.getVideoId() + "");
        if (albumVideoMap == null) {
            throw new IqiyiVideoSolrException(IqiyiVideoSolrException.CODE_VIDEO_NOT_ADD_TO_SOLR_ALBUM, "视频未添加到专辑搜索映射");
        }
 
        SolrAlbumVideo solrAlbumVideo = solrAlbumVideoDataManager.findOne(videoIqiyi2.getVideoId());
        if (solrAlbumVideo == null) {
            throw new IqiyiVideoSolrException(IqiyiVideoSolrException.CODE_VIDEO_NOT_SYNC_TO_SOLR, "视频尚未同步到搜索引擎");
        }
 
 
    }
}