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, List<Long> resourceIds) {
|
for (int i = 0; i < homeVideoList.size(); i++) {
|
HomeVideo hv = homeVideoList.get(i);
|
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) {
|
String[] rids = internetSearchVideo.getResourceIds().split(",");
|
List<Long> ridList = new ArrayList<>();
|
for (String rid : rids) {
|
ridList.add(Long.parseLong(rid));
|
}
|
ridList.retainAll(resourceIds);
|
if (ridList.size() > 0)
|
hv.setVideo(VideoInfoFactory.create(internetSearchVideo));
|
else {
|
homeVideoList.remove(i);
|
i--;
|
}
|
}
|
|
}
|
|
return homeVideoList;
|
}
|
|
private List<HomeVideo> betchShortVideos(List<HomeVideo> homeVideoList, List<Long> resourceIds) {
|
for (int i = 0; i < homeVideoList.size(); i++) {
|
HomeVideo hv = homeVideoList.get(i);
|
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) {
|
Long rid = Long.parseLong(shortVideo.getResourceId() + "");
|
if (resourceIds.contains(rid)) {
|
hv.setVideo(VideoInfoFactory.create(shortVideo));
|
} else {
|
homeVideoList.remove(i);
|
i--;
|
}
|
}
|
}
|
|
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, resourceIds);
|
homeVideos = betchShortVideos(homeVideos, resourceIds);
|
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, resourceIds);
|
}
|
|
@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);
|
});
|
}
|
}
|