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
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.solr.SolrShortVideo;
import com.yeshi.buwan.domain.video.InternetSearchVideo;
import com.yeshi.buwan.service.imp.VideoInfoService;
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.service.manager.search.SolrShortVideoDataManager;
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 com.yeshi.buwan.util.video.VideoUtil;
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.Date;
import java.util.List;
 
@Service
public class HomeVideoServiceImpl implements HomeVideoService {
 
    @Resource
    private HomeVideoDao homeVideoDao;
 
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
 
    @Resource
    private InternetSearchVideoService internetSearchVideoService;
 
    @Resource
    private SolrShortVideoDataManager solrShortVideoDataManager;
 
    @Resource
    private VideoInfoService videoInfoService;
 
 
    public void deleteHomeVideo(HomeVideo video) {
 
        homeVideoDao.delete(video);
    }
 
    public void deleteHomeTypeVideo(List<HomeVideo> list) {
 
        for (HomeVideo homeVideo : list) {
            deleteHomeVideo(homeVideo);
        }
    }
 
 
    public void updateHomeVideo(HomeVideo homeVideo) {
 
        homeVideoDao.update(homeVideo);
    }
 
    public void addHomeVideo(List<HomeVideo> list) {
        if (list != null)
            for (HomeVideo homeVideo : list) {
                addHomeVideo(homeVideo);
            }
    }
 
    public void addHomeVideo(HomeVideo video) {
 
        if (video.getFromType() == null)
            video.setFromType(HomeVideo.FROM_TYPE_LOCAL);
 
        if (video.getCreatetime() == null)
            video.setCreatetime(System.currentTimeMillis() + "");
 
        if (video.getVideoUpdateTime() == null) {
            video.setVideoUpdateTime(new Date());
        }
 
        if (video.getVideoName() == null) {
 
            switch (video.getFromType()) {
 
                case HomeVideo.FROM_TYPE_LOCAL: {
                    //取视频名称
                    VideoInfo videoInfo = videoInfoService.getVideoInfo(video.getVideoId());
                    if (videoInfo != null) {
                        video.setVideoName(videoInfo.getName());
                    }
                }
                break;
                case HomeVideo.FROM_TYPE_INTERNET: {
                    //取视频名称
                    InternetSearchVideo internetSearchVideo = internetSearchVideoService.selectByPrimaryKey(video.getVideoId());
                    if (internetSearchVideo != null) {
                        video.setVideoName(internetSearchVideo.getName());
                    }
                }
                break;
            }
 
        }
 
 
        long count = homeVideoDao.getCount("select count(*) from HomeVideo h where h.type.id=? and h.videoId=?",
                new String[]{video.getType().getId(), video.getVideoId()});
        if (count <= 0L)
            homeVideoDao.create(video);
    }
 
 
    private List<HomeVideo> betchInternetVideos(List<HomeVideo> homeVideoList) {
        for (HomeVideo hv : homeVideoList) {
            if (hv == null || hv.getVideo() != null || StringUtil.isNullOrEmpty(hv.getVideoId())) {
                continue;
            }
            if (VideoUtil.getVideoFromType(hv.getVideoId()) != HomeVideo.FROM_TYPE_INTERNET) {
                continue;
            }
            InternetSearchVideo internetSearchVideo = internetSearchVideoService.selectByPrimaryKeyCache(hv.getVideoId());
            if (internetSearchVideo != null) {
                hv.setVideo(VideoInfoFactory.create(internetSearchVideo));
            }
        }
 
        return homeVideoList;
    }
 
    private List<HomeVideo> betchShortVideos(List<HomeVideo> homeVideoList) {
        for (HomeVideo hv : homeVideoList) {
            if (hv == null || hv.getVideo() != null || StringUtil.isNullOrEmpty(hv.getVideoId())) {
                continue;
            }
            if (VideoUtil.getVideoFromType(hv.getVideoId()) != HomeVideo.FROM_TYPE_SHORT) {
                continue;
            }
 
            SolrShortVideo shortVideo = solrShortVideoDataManager.findOne(hv.getVideoId());
            if (shortVideo != null) {
                hv.setVideo(VideoInfoFactory.create(shortVideo));
            }
        }
 
        return homeVideoList;
    }
 
 
    @Override
    public List<HomeVideo> getHomeVideoList(String homeId, String key, int page) {
        return getHomeVideoList(homeId, null, null, null, page, Constant.pageCount);
    }
 
    @Override
    public long getHomeVideoListCount(String homeId, String key) {
        return getHomeVideoListCount(homeId, null, null, null);
    }
 
    @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<>();
            if (resourceIds != null)
                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  hv.`hometype`=%s AND ( (v.show=1 AND hv.from_type=0  %s) OR hv.from_type>0 )  ", homeId, rids.size() == 0 ? "" : "AND (" + 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<>();
        homeVideos = betchInternetVideos(homeVideos);
        homeVideos = betchShortVideos(homeVideos);
        for (int i = 0; i < homeVideos.size(); i++) {
            if (homeVideos.get(i).getVideo() == null) {
                homeVideos.remove(i);
                i--;
            }
        }
 
        for (HomeVideo hv : homeVideos) {
            if (hv.getVideo() != null) {
                hv.getVideo().setIntroduction("");
                hv.getVideo().setVideoType(null);
                videoInfoList.add(hv.getVideo());
            }
        }
        videoInfoExtraService.batchExtra(videoInfoList, resourceIds);
        return betchInternetVideos(homeVideos);
    }
 
    @Override
    @Cacheable(value = "homeCache", key = "'getHomeVideoListCount-'+#homeId+'-'+#resourceKey+'-'+#hasBigPicture")
    public long getHomeVideoListCount(String homeId, String resourceKey, List<Long> resourceIds, Boolean hasBigPicture) {
        return (Long) homeVideoDao.excute((Session session) -> {
 
            List<String> rids = new ArrayList<>();
            if (resourceIds != null)
                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  hv.`hometype`=%s AND ( (v.show=1 AND hv.from_type=0 %s) OR hv.from_type>0 ) ", homeId, rids.size() == 0 ? "" : "AND (" + org.yeshi.utils.StringUtil.concat(rids, " or ") + ")");
 
            return Long.parseLong(session.createSQLQuery(sql).uniqueResult() + "") + (hasBigPicture != null && hasBigPicture ? 1 : 0);
        });
    }
}