admin
2022-08-25 264b5dea5b74c4b5ba54a90caba7e709858a037e
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
package com.yeshi.buwan.service.imp;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.CategoryContryDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.VideoTypeDao;
import com.yeshi.buwan.domain.CategoryContry;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.util.SolrUtil;
import com.yeshi.buwan.util.StringUtil;
 
//推荐服务
@Service
public class RecommendService {
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private BanQuanService banQuanService;
    @Resource
    private VideoTypeDao videoTypeDao;
    @Resource
    private CategoryContryDao categoryContryDao;
 
    @Cacheable(value = "userCache", key = "'guessLike'+'-'+#videoId+'-'+#detailSystem+'-'+#cachemd5")
    public List<VideoInfo> guessLike(String videoId, String detailSystem, List<Long> resourceList, String cachemd5) {
        String resourceWhere = "";
        for (Long rid : resourceList)
            resourceWhere += String.format(" rv.`resourceid`=%d  or", rid);
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
        videoId = StringUtil.isNullOrEmpty(videoId) ? "" : videoId;
        List<VideoInfo> list = new ArrayList<>();
        int start = (int) (Math.random() * 100);
 
        String sql = String.format(
                "SELECT DISTINCT v.`id` FROM wk_category_video cv LEFT JOIN wk_resource_video rv ON cv.`videoid`=rv.`videoid` AND (%s) LEFT JOIN wk_video_video v ON cv.`videoid`=v.`id`  "
                        + (StringUtil.isNullOrEmpty(videoId) ? "" : "where cv.videoid!=" + videoId),
                resourceWhere, videoId);
        System.out.println(sql);
        List idList = videoInfoDao.sqlList(sql, start, 3, null);
        for (int i = 0; i < idList.size(); i++) {
            list.add((VideoInfo) videoInfoDao.find(VideoInfo.class, idList.get(i) + ""));
        }
        List<VideoInfo> videoList = new ArrayList<>();
 
        for (VideoInfo v : list) {
            boolean can = true;
            for (int i = 0; i < videoList.size(); i++) {
                if (v.getId().equalsIgnoreCase(videoList.get(i).getId())) {
                    can = false;
                    break;
                }
            }
            if (can)
                videoList.add(v);
            if (videoList.size() >= 3)
                break;
        }
        return videoList;
    }
 
    public List<VideoInfo> testIOSGuessLike(String videoId) {
        return videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=129", (int) (Math.random() * 100),
                3, new String[] {});
    }
 
    // 大区推荐视频
    public List<VideoInfo> getCategoryRecommendVideoList(long videoTypeId) {
        return videoInfoDao.list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id="
                + videoTypeId + " order by cv.orderby desc");
    }
 
    // 大区推荐视频
    @Cacheable(value = "classCache", key = "'getCategoryRecommendVideoList'+'-'+#videoTypeId+'-'+#count")
    @SuppressWarnings("unchecked")
    public List<VideoInfo> getCategoryRecommendVideoList(final long videoTypeId, final int count) {
 
        return (List<VideoInfo>) videoInfoDao.excute(new HibernateCallback<List<VideoInfo>>() {
 
            @Override
            public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
                SQLQuery query = session.createSQLQuery(
                        "SELECT vv.* FROM wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id WHERE rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid = vv.id  GROUP BY cc.id ORDER BY cc.orderby desc");
                query.addEntity(VideoInfo.class);
                query.setParameter(0, videoTypeId);
                query.setFirstResult(0);
                query.setMaxResults(count);
                return query.list();
            }
        });
 
        // return videoInfoDao
        // .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where
        // cv.videoInfo is not null and cv.videoInfo.show=1 and
        // cv.videoType.id="
        // + videoTypeId + " order by cv.orderby desc", 0, count, null);
    }
 
    // 相关视频
    @Cacheable(value = "userCache", key = "'getRelativeVideoList'+'-'+#number+'-'+#detailSystemId+'-'+#vid+'-'+#cacheMD5")
    public List<VideoInfo> getRelativeVideoList(String detailSystemId, int number, String vid, List<Long> resourceList,
            String cachemd5) {
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
        VideoInfo vi = videoInfoDao.find(VideoInfo.class, vid);
        List<VideoInfo> infoList = SolrUtil.search(vi.getName(), 1);
        for (VideoInfo v : infoList)
            if (v.getId().equalsIgnoreCase(vi.getId())) {
                infoList.remove(v);
                break;
            }
 
        String cacheMD5 = "";
        if (infoList != null) {
            for (VideoInfo info : infoList) {
                cacheMD5 += info.getId() + "#";
            }
        }
 
        infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5);
        if (infoList.size() > number)
            infoList = infoList.subList(0, number);
 
        String sql = "";
        for (int i = 0; i < infoList.size(); i++) {
            sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId() + " and ("
                    + resourceWhere + ") union all";
        }
 
        if (sql.endsWith("union all"))
            sql = sql.substring(0, sql.length() - 9);
 
        List rlist = null;
        if (!StringUtil.isNullOrEmpty(sql))
            rlist = videoInfoDao.sqlList(sql);
        for (int i = 0; i < infoList.size(); i++) {
            if (rlist != null)
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    infoList.remove(i);
                    rlist.remove(i);
                    i--;
                }
        }
        return infoList;
    }
 
    // 无视频的猜你喜欢
    @Cacheable(value = "userCache", key = "'guessLikeList'+'-'+#number+'-'+#detailSystemId+'-'+#cachemd5")
    public List<VideoInfo> guessLikeList(String detailSystemId, int number, List<Long> resourceList, String cachemd5) {
 
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
        try {
            long count = videoInfoDao.getCount("select count(*) from CategoryRecommendCacheVideo cv ");
            List<VideoInfo> infoList = videoInfoDao.list(
                    "select cv.videoInfo from CategoryRecommendCacheVideo cv  order by cv.orderby desc",
                    (int) Math.random() * 20, 100, null);
            String cacheMD5 = "";
            if (infoList != null) {
                for (VideoInfo info : infoList) {
                    cacheMD5 += info.getId() + "#";
                }
            }
 
            infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5);
            String sql = "";
            for (int i = 0; i < infoList.size(); i++) {
                sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId()
                        + " and (" + resourceWhere + ") union all";
            }
 
            if (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
 
            List rlist = videoInfoDao.sqlList(sql);
            for (int i = 0; i < infoList.size(); i++) {
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    infoList.remove(i);
                    rlist.remove(i);
                    i--;
                }
            }
 
            if (infoList.size() > number)
                return infoList.subList(0, number + 0);
            else
                return infoList;
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
 
    }
 
    // 猜你喜欢
 
    @SuppressWarnings("unchecked")
    @Cacheable(value = "userCache", key = "'guessLikeList'+'-'+#number+'-'+#detailSystemId+'-'+#vid+'-'+#cachemd5")
    public List<VideoInfo> guessLikeList(String detailSystemId, int number, String vid, List<Long> resourceList,
            String cachemd5) {
 
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
        try {
            VideoType videoType = null;
            List<VideoType> list = videoTypeDao
                    .list("select cv.videoType from CategoryVideo cv  where cv.video.id=" + vid);
            if (list != null && list.size() > 0)
                videoType = list.get(0);
            else
                videoType = (VideoType) videoTypeDao.find(VideoType.class, 150L);
            long typeid = 0;
            if (videoType.getParent() != null)
                typeid = videoType.getParent().getId();
            else
                typeid = videoType.getId();
 
            long count = videoInfoDao
                    .getCount("select count(*) from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid);
 
            List<VideoInfo> infoList = videoInfoDao
                    .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid
                            + " order by cv.orderby desc", (int) (Math.random() * 20), 100, null);
            String cacheMD5 = "";
            if (infoList != null) {
                for (VideoInfo info : infoList) {
                    cacheMD5 += info.getId() + "#";
                }
            }
 
            infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5);
            String sql = "";
            for (int i = 0; i < infoList.size(); i++) {
                sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId()
                        + " and (" + resourceWhere + ") union all";
            }
 
            if (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
 
            List rlist = videoInfoDao.sqlList(sql);
            for (int i = 0; i < infoList.size(); i++) {
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    infoList.remove(i);
                    i--;
                }
            }
 
            if (infoList.size() > number)
                return infoList.subList(0, number);
            else
                return infoList;
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
 
    }
 
    // 大家都在看
 
    @SuppressWarnings("unchecked")
    @Cacheable(value = "userCache", key = "'peopleSee'+'-'+#number+'-'+#detailSystem+'-'+#vid+'-'+#cachemd5")
    public List<VideoInfo> peopleSee(String detailSystem, int number, String vid, List<Long> resourceList,
            String cachemd5) {
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
        try {
            VideoType videoType = null;
            List<VideoType> list = videoTypeDao
                    .list("select cv.videoType from CategoryVideo cv  where cv.video.id=" + vid);
            if (list != null && list.size() > 0)
                videoType = list.get(0);
            else
                videoType = (VideoType) videoTypeDao.find(VideoType.class, 150);
            long typeid = 0;
            if (videoType.getParent() != null)
                typeid = videoType.getParent().getId();
            else
                typeid = videoType.getId();
            List<VideoInfo> infoList = null;
            VideoInfo vi = (VideoInfo) videoInfoDao.find(VideoInfo.class, vid);
            if (typeid < 154 && !StringUtil.isNullOrEmpty(vi.getArea()))// 从地区视频缓存中
            {
                List<CategoryContry> thirdList = categoryContryDao
                        .list("from CategoryContry cc where cc.parent.parent.cid=" + typeid);
 
                long areaId = 0;
                for (CategoryContry cc : thirdList) {
                    if (vi.getArea().contains(cc.getName())) {
                        areaId = cc.getParent().getId();
                        break;
                    }
                }
                if (areaId > 0) {
                    infoList = videoInfoDao
                            .list("select cc.videoInfo from AreaVideoCache cc where cc.categoryContry.id=" + areaId);
                }
 
            } else {//
                infoList = videoInfoDao
                        .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id="
                                + videoType.getId() + " order by cv.orderby desc", 0, 100, null);
 
            }
 
            if (infoList == null || infoList.size() == 0) {
                infoList = videoInfoDao
                        .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid
                                + " order by cv.orderby desc", 0, 100, null);
            }
 
            String cacheMD5 = "";
            if (infoList != null) {
                for (VideoInfo info : infoList) {
                    cacheMD5 += info.getId() + "#";
                }
            }
            infoList = banQuanService.getBanQuanVideo(infoList, detailSystem, cacheMD5);
 
            String sql = "";
            for (int i = 0; i < infoList.size(); i++) {
                sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId()
                        + " and (" + resourceWhere + ") union all";
            }
 
            if (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
 
            List rlist = videoInfoDao.sqlList(sql);
            for (int i = 0; i < infoList.size(); i++) {
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    infoList.remove(i);
                    rlist.remove(i);
                    i--;
                }
            }
 
            int start = 0;
            start = (int) (infoList.size() * Math.random());
 
            if (infoList.size() > number)
                return infoList.subList(0, number);
            else
                return infoList;
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return new ArrayList<>();
    }
 
}