admin
2021-04-25 36aafca8d6c1964bb755fe2ae030b163b6d0f92b
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
package com.yeshi.buwan.service.imp.recommend;
 
import com.yeshi.buwan.dao.HomeVideoDao;
import com.yeshi.buwan.domain.HomeVideo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.video.InternetSearchVideo;
import com.yeshi.buwan.service.inter.juhe.InternetSearchVideoService;
import com.yeshi.buwan.service.inter.recommend.HomeVideoService;
import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.NumberUtil;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.factory.VideoInfoFactory;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class HomeVideoServiceImpl implements HomeVideoService {
 
    @Resource
    private HomeVideoDao homeVideoDao;
 
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
 
    @Resource
    private InternetSearchVideoService internetSearchVideoService;
 
    private List<HomeVideo> betchInternetVideos(List<HomeVideo> homeVideoList) {
        for (HomeVideo hv : homeVideoList) {
            if (hv == null || hv.getVideo() == null || !StringUtil.isNullOrEmpty(hv.getVideoId())) {
                continue;
            }
            if (NumberUtil.isNumeric(hv.getVideoId())) {
                continue;
            }
            InternetSearchVideo internetSearchVideo = internetSearchVideoService.selectByPrimaryKeyCache(hv.getVideoId());
            if (internetSearchVideo != null) {
                hv.setVideo(VideoInfoFactory.create(internetSearchVideo));
            }
        }
 
        return homeVideoList;
    }
 
 
    @Override
    public List<HomeVideo> getHomeVideoList(String homeId, String key, int page) {
        List<HomeVideo> homeVideoList = homeVideoDao.list(
                "from HomeVideo h where h.type.id=? and h.video.name like ? order by h.bigPicture desc, h.video.orderby desc,h.video.watchCount desc,h.createtime desc",
                (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{homeId, "%" + key + "%"});
        return betchInternetVideos(homeVideoList);
    }
 
    @Override
    public long getHomeVideoListCount(String homeId, String key) {
        return homeVideoDao.getCount("select count(*)  from HomeVideo h where h.type.id=? and h.video.name like ? ",
                new String[]{homeId, "%" + key + "%"});
    }
 
    @Override
    @Cacheable(value = "homeCache", key = "'getHomeVideoList-'+#homeId+'-'+#resourceKey+'-'+#hasBigPicture+'-'+#page+'-'+#pageSize")
    public List<HomeVideo> getHomeVideoList(String homeId, String resourceKey, List<Long> resourceIds, Boolean hasBigPicture, int page, int pageSize) {
        List<HomeVideo> homeVideos = (List<HomeVideo>) homeVideoDao.excute((Session session) -> {
 
 
            List<String> rids = new ArrayList<>();
            for (Long rid : resourceIds) {
                rids.add("rv.`resourceid`=" + rid);
            }
 
            String sql = String.format("SELECT hv.* FROM wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid` LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid` WHERE v.show=1 and hv.`hometype`=%s AND (%s) ", homeId, org.yeshi.utils.StringUtil.concat(rids, " or "));
 
            sql += " GROUP BY hv.id ORDER BY hv.`orderby` DESC,hv.`createtime` DESC";
 
            List<HomeVideo> homeVideoList = session.createSQLQuery(sql).addEntity(HomeVideo.class).setFirstResult((page - 1) * pageSize + (hasBigPicture != null && hasBigPicture ? 1 : 0)).setMaxResults(pageSize).list();
 
            return homeVideoList;
        });
 
        List<VideoInfo> videoInfoList = new ArrayList<>();
        for (HomeVideo hv : homeVideos) {
            if (hv.getVideo() != null)
                videoInfoList.add(hv.getVideo());
        }
        homeVideos = betchInternetVideos(homeVideos);
        videoInfoExtraService.batchExtra(videoInfoList, resourceIds);
        return betchInternetVideos(homeVideos);
    }
 
    @Override
    @Cacheable(value = "homeCache", key = "'getHomeVideoListCount-'+#homeId+'-'+#resourceKey+'-'+#bigPicture")
    public long getHomeVideoListCount(String homeId, String resourceKey, List<Long> resourceIds, Boolean bigPicture) {
        return (Long) homeVideoDao.excute((Session session) -> {
 
            List<String> rids = new ArrayList<>();
            for (Long rid : resourceIds) {
                rids.add("rv.`resourceid`=" + rid);
            }
 
            String sql = String.format("SELECT  count(distinct(hv.id)) FROM wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid`  LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid`  WHERE v.show=1 and hv.`hometype`=%s AND (%s)", homeId, org.yeshi.utils.StringUtil.concat(rids, " or "));
 
            if (bigPicture != null) {
                sql += " and hv.big_picture=" + (bigPicture ? 1 : 0);
            }
            return Long.parseLong(session.createSQLQuery(sql).uniqueResult() + "");
        });
    }
}