admin
2021-07-30 19533a17aa55fafc70d0a385928e785cb50e1ebc
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
package com.yeshi.buwan.service.imp;
 
import java.io.Serializable;
import java.util.*;
 
import javax.annotation.Resource;
 
import com.yeshi.buwan.util.HibernateSessionFactory;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.mq.CMQManager;
import com.yeshi.buwan.util.video.VideoConstant;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.buwan.dao.CategoryVideoDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.domain.CategoryVideo;
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.VideoType;
import com.yeshi.buwan.domain.VideoUrl;
import com.yeshi.buwan.domain.entity.PlayUrl;
 
@Service
public class VideoInfoService {
 
    @Autowired
    private VideoInfoDao videoInfoDao;
 
    @Autowired
    private CategoryVideoDao categoryVideoDao;
 
    @Autowired
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private VideoResourceService videoResourceService;
 
    @Resource
    private ClearService clearService;
 
    public VideoInfo getVideoInfo(String vid) {
        return videoInfoDao.find(VideoInfo.class, vid);
    }
 
    @Cacheable(value = "videoCache",key = "'getVideoInfoDetail-'+#vid")
    public VideoInfo getVideoInfoCache(String vid) {
        return videoInfoDao.find(VideoInfo.class, vid);
    }
 
 
    @SuppressWarnings("unchecked")
    public List<VideoInfo> simpleRandomVideoList(final int i) {
        return (List<VideoInfo>) videoInfoDao.excute(new HibernateCallback<List<VideoInfo>>() {
 
            @Override
            public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
                SQLQuery query = session.createSQLQuery(
                        "SELECT v.* FROM wk_video_video v RIGHT JOIN (SELECT * FROM wk_recommend_category_cache WHERE videotypeid = 152 ORDER BY RAND() LIMIT 0,"
                                + i + ")  c ON c.videoid=v.id");
                query.addEntity(VideoInfo.class);
                return query.list();
            }
        });
    }
 
    public VideoInfo getVideoInfo(String name, VideoType videoType) {
        List<VideoInfo> list = videoInfoDao.list(
                "select c.video from CategoryVideo c where c.video.name = ? and c.videoType.id = ?",
                new Serializable[]{name, videoType.getId()});
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @Transactional
    public void save(VideoInfo videoInfo) {
        videoInfoDao.save(videoInfo);
        CategoryVideo ca = new CategoryVideo();
        ca.setVideo(videoInfo);
        ca.setVideoType(videoInfo.getVideoType());
        categoryVideoDao.create(ca);
        List<VideoResource> resourceList = videoInfo.getResourceList();
        ResourceVideo rs = null;
        for (VideoResource videoResource : resourceList) {
            rs = new ResourceVideo();
            rs.setResource(videoResource);
            rs.setVideo(videoInfo);
            resourceVideoService.save(rs);
        }
    }
 
    public List<VideoDetailInfo> getVideoDetailList(String videoid, VideoResource vr, int page, int pageSize) {
        VideoInfo find = videoInfoDao.find(VideoInfo.class, videoid);
        if (find != null) {
            if (vr.getName().equals("AcFun")) {
                List<VideoDetailInfo> detailInfos = new ArrayList<VideoDetailInfo>();
                VideoDetailInfo vd = new VideoDetailInfo();
                vd.setVideo(find);
                vd.setIntroduction(find.getIntroduction());
                vd.setName(find.getName());
                vd.setTag("");
                vd.setType("acFunVideo");
                vd.setId(Long.parseLong(find.getId()));
                vd.setWatchCount(Long.parseLong(find.getWatchCount()));
                VideoUrl vurl = new VideoUrl();
                vurl.setResource(vr);
                vurl.setUrl(find.getBaseurl());
                vurl.setInvalid(0 + "");
                vurl.setBaseUrl(find.getBaseurl());
                List<VideoUrl> urls = new ArrayList<VideoUrl>();
                urls.add(vurl);
                vd.setUrls(urls);
                detailInfos.add(vd);
                return detailInfos;
            }
        }
 
        return null;
    }
 
    public PlayUrl getPlayUrl(String detailSystemId, String id, String type, int resourceid, String videoid) {
        if (type.equals("acFunVideo")) {
            PlayUrl playUrl = new PlayUrl();
            VideoResource videoResource = videoResourceService.getResource(resourceid + "");
            playUrl.setResource(videoResource);
            VideoInfo find = videoInfoDao.find(VideoInfo.class, videoid);
            playUrl.setUrl(find.getBaseurl());
            playUrl.setPlayType(1);
            playUrl.setParams("");
            // 添加播放次数
            find.setWatchCount("" + (Integer.parseInt(find.getWatchCount()) + 1));
            videoInfoDao.update(find);
            return playUrl;
        }
        return null;
    }
 
 
    /**
     * 是否存在相同的视频(根据名称与上映时间)
     *
     * @param newVideoInfo
     * @return
     */
    public VideoInfo getExistSameVideoWithTime(VideoInfo newVideoInfo) {
        int year = Integer.parseInt(newVideoInfo.getYear());
        List<VideoInfo> list = videoInfoDao.listByName(newVideoInfo.getName(), 0, 5);
        if (list != null && list.size() > 0)
            for (int i = 0; i < list.size(); i++) {
                if (Math.abs(Integer.parseInt(list.get(i).getYear()) - year) < 2) {// 年份相差1年以下且属于同一个分类才归为一起
                    // 判断分类
                    if (VideoConstant.isMainCategory(newVideoInfo.getVideoType().getId())) {// 正片分类
                        List<CategoryVideo> cvList = categoryVideoDao.listByVideoId(list.get(i).getId());
                        boolean isS = false;
                        for (CategoryVideo cv : cvList) {
                            if (newVideoInfo.getVideoType().getId() == cv.getVideoType().getId()
                                    || (cv.getVideoType().getParent() != null && newVideoInfo.getVideoType().getId() ==
                                    cv.getVideoType().getParent().getId())) {// 类型相同
                                return list.get(i);
                            }
                        }
                    } else // 不必比较分类--只要名称相同则判断为相同
                    { // 判断上个是否为正片分类
                        List<CategoryVideo> typeList = categoryVideoDao.listByVideoId(list.get(i).getId());
                        boolean isZhengpian = false;
                        if (typeList != null)
                            for (CategoryVideo categoryVideo : typeList) {
                                VideoType ltype = categoryVideo.getVideoType();
                                if (VideoConstant.isMainCategory(ltype.getId())
                                        || (ltype.getParent() != null && VideoConstant.isMainCategory(ltype.getParent().getId()))) {
                                    isZhengpian = true;
                                    break;
                                }
                            }
 
                        if (!isZhengpian)
                            return list.get(i);
                        else
                            return null;
                    }
                }
            }
        return null;
    }
 
 
    /**
     * 根据名称与主演
     *
     * @param newVideoInfo
     * @return
     */
    public VideoInfo getExistSameVideoWithDirector(VideoInfo newVideoInfo) {
        List<VideoInfo> list = videoInfoDao.listByName(newVideoInfo.getName(), 0, 5);
        if (list != null && list.size() > 0) {
            if (VideoConstant.isMainCategory(newVideoInfo.getVideoType().getId())) {
                for (int i = 0; i < list.size(); i++) {
                    VideoInfo vi = list.get(i);
                    if (getSameDirectorOrActorCount(vi.getDirector(), newVideoInfo.getDirector()) > 0 || getSameDirectorOrActorCount(vi.getMainActor(), newVideoInfo.getMainActor()) > 0) {
//                        if (getSameDirectorOrActorCount(vi.getMainActor(), newVideoInfo.getMainActor()) > 0) {
                        //主分类一样
                        if (vi.getVideoType() != null && newVideoInfo.getVideoType() != null && vi.getVideoType().getId() == newVideoInfo.getVideoType().getId())
                            return list.get(i);
//                        }
                    }
                }
            } else {
                return list.get(0);
            }
        }
        return null;
    }
 
    private int getSameDirectorOrActorCount(String d1, String d2) {
        if (!StringUtil.isNullOrEmpty(d1) && !StringUtil.isNullOrEmpty(d2)) {
            List<String> d1List = new ArrayList<>();
            if (d1.contains("/")) {
                d1List.addAll(Arrays.asList(d1.trim().split("/")));
            } else {
                d1List.addAll(Arrays.asList(d1.trim().split(" ")));
            }
 
            List<String> d2List = new ArrayList<>();
            if (d2.contains("/"))
                d2List.addAll(Arrays.asList(d2.trim().split("/")));
            else
                d2List.addAll(Arrays.asList(d2.trim().split(" ")));
 
            d1List.retainAll(d2List);
 
            return d1List.size();
        }
 
        return 0;
    }
 
 
    public List<VideoInfo> listByVideoIds(List<String> videoIds) {
        return videoInfoDao.listByVideoIds(videoIds);
    }
 
    public void hiddenVideo(String id) {
        VideoInfo videoInfo = videoInfoDao.find(VideoInfo.class, id);
        if (videoInfo != null && "1".equalsIgnoreCase(videoInfo.getShow())) {
            videoInfo.setShow("0");
            videoInfoDao.update(videoInfo);
        }
    }
 
 
    //从新统计
    public void statisticVideoExtraInfo(String videoId) {
        videoInfoDao.statisticVideoExtraInfo(videoId);
    }
 
    /**
     * 列表查询
     *
     * @param query
     * @return
     */
    public List<VideoInfo> list(VideoInfoDao.DaoQuery query) {
        return videoInfoDao.list(query);
    }
 
    public long count(VideoInfoDao.DaoQuery query) {
        return videoInfoDao.count(query);
    }
 
 
    public void updateVideoInfo(VideoInfo videoInfo) {
        VideoInfo oldVideo = videoInfoDao.find(VideoInfo.class, videoInfo.getId());
        if (oldVideo == null)
            return;
 
        videoInfoDao.updateSelective(videoInfo);
        //是否需要更新搜索引擎
        boolean needUpdateSolr = false;
        //tag变化
        if (videoInfo.getTag() != null && !videoInfo.getTag().equalsIgnoreCase(oldVideo.getTag())) {
            needUpdateSolr = true;
        }
 
        //名称变化
        if (videoInfo.getName() != null && !videoInfo.getName().equalsIgnoreCase(oldVideo.getName())) {
            needUpdateSolr = true;
        }
 
        //picture变化
        if (videoInfo.getHpicture() != null && !videoInfo.getHpicture().equalsIgnoreCase(oldVideo.getHpicture())) {
            needUpdateSolr = true;
        }
        if (videoInfo.getVpicture() != null && !videoInfo.getVpicture().equalsIgnoreCase(oldVideo.getVpicture())) {
            needUpdateSolr = true;
        }
 
        if (videoInfo.getPicture() != null && !videoInfo.getPicture().equalsIgnoreCase(oldVideo.getPicture())) {
            needUpdateSolr = true;
        }
 
        //视频类型发生变化
        if (videoInfo.getVideoType() != null && (oldVideo.getVideoType() == null || videoInfo.getVideoType().getId() != oldVideo.getVideoType().getId())) {
            needUpdateSolr = true;
        }
 
        //是否显示
        if (videoInfo.getShow() != null && !videoInfo.getShow().equalsIgnoreCase(oldVideo.getShow())) {
            needUpdateSolr = true;
        }
 
        if (needUpdateSolr) {
            CMQManager.getInstance().addSolrMsg(videoInfo.getId());
        }
 
 
    }
 
}