From f537abe9f3646c739beaf15076246a2f71a347e9 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期四, 20 二月 2025 16:57:55 +0800 Subject: [PATCH] IOS广告增加区域屏蔽 --- src/main/java/com/yeshi/buwan/service/imp/SearchService.java | 1197 +++++++++++++++++++++++++++++++++++------------------------ 1 files changed, 715 insertions(+), 482 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/service/imp/SearchService.java b/src/main/java/com/yeshi/buwan/service/imp/SearchService.java index c146fa9..c76f4de 100644 --- a/src/main/java/com/yeshi/buwan/service/imp/SearchService.java +++ b/src/main/java/com/yeshi/buwan/service/imp/SearchService.java @@ -1,572 +1,805 @@ package com.yeshi.buwan.service.imp; -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import javax.annotation.Resource; - +import com.yeshi.buwan.dao.*; +import com.yeshi.buwan.dao.system.DetailSystemDao; +import com.yeshi.buwan.domain.*; +import com.yeshi.buwan.domain.solr.SolrAlbumVideo; +import com.yeshi.buwan.domain.system.DetailSystem; +import com.yeshi.buwan.domain.video.InternetSearchVideo; +import com.yeshi.buwan.domain.web.DetailSystemSelect; +import com.yeshi.buwan.domain.web.HotSearchAdmin; +import com.yeshi.buwan.dto.search.SolrResultDTO; +import com.yeshi.buwan.dto.search.SolrVideoSearchFilter; +import com.yeshi.buwan.videos.pptv.PPTVUtil; +import com.yeshi.buwan.service.inter.video.VideoInfoExtraService; +import com.yeshi.buwan.service.manager.search.SolrAlbumVideoDataManager; +import com.yeshi.buwan.service.manager.search.SolrInternetSearchVideoDataManager; +import com.yeshi.buwan.util.*; +import com.yeshi.buwan.util.factory.VideoInfoFactory; +import com.yeshi.buwan.util.video.VideoConstant; +import com.yeshi.buwan.vo.video.VideoListResultVO; import org.hibernate.HibernateException; import org.hibernate.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.Cacheable; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; -import com.yeshi.buwan.dao.DetailSystemDao; -import com.yeshi.buwan.dao.HotSearchDao; -import com.yeshi.buwan.dao.SearchDao; -import com.yeshi.buwan.dao.SuperHotSearchDao; -import com.yeshi.buwan.dao.VideoBanQuanVideoDao; -import com.yeshi.buwan.dao.VideoInfoDao; -import com.yeshi.buwan.domain.DetailSystem; -import com.yeshi.buwan.domain.HotSearch; -import com.yeshi.buwan.domain.SearchHistory; -import com.yeshi.buwan.domain.SuperHotSearch; -import com.yeshi.buwan.domain.UserInfo; -import com.yeshi.buwan.domain.VideoInfo; -import com.yeshi.buwan.domain.web.DetailSystemSelect; -import com.yeshi.buwan.domain.web.HotSearchAdmin; -import com.yeshi.buwan.util.BaiduVideoUtil; -import com.yeshi.buwan.util.BanQuanUtil; -import com.yeshi.buwan.util.Constant; -import com.yeshi.buwan.util.SolrUtil; -import com.yeshi.buwan.util.StringUtil; +import javax.annotation.Resource; +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.util.*; @Service public class SearchService { - @Resource - private SearchDao searchDao; - @Resource - private ClassService classService; - @Resource - private VideoInfoDao videoInfoDao; - @Resource - private HotSearchDao hotSearchDao; - @Resource - private SuperHotSearchDao superHotSearchDao; - @Resource - private ConfigService configService; - @Resource - private VideoBanQuanVideoDao videoBanQuanVideoDao; - @Resource - private DetailSystemDao detailSystemDao; + private Logger logger = LoggerFactory.getLogger(SearchService.class); + private Logger debugLogger = LoggerFactory.getLogger("debug"); - public VideoBanQuanVideoDao getVideoBanQuanVideoDao() { - return videoBanQuanVideoDao; - } + private final String ALBUM_SEARCH_FUZZY_KEYS = "銆�,銆�,銆�"; - public void setVideoBanQuanVideoDao(VideoBanQuanVideoDao videoBanQuanVideoDao) { - this.videoBanQuanVideoDao = videoBanQuanVideoDao; - } - public ConfigService getConfigService() { - return configService; - } + @Resource + private SearchDao searchDao; + @Resource + private ClassService classService; + @Resource + private VideoInfoDao videoInfoDao; + @Resource + private HotSearchDao hotSearchDao; + @Resource + private SuperHotSearchDao superHotSearchDao; + @Resource + private VideoBanQuanVideoDao videoBanQuanVideoDao; + @Resource + private DetailSystemDao detailSystemDao; + @Resource + private SolrAlbumVideoDataManager solrAlbumDataManager; - public void setConfigService(ConfigService configService) { - this.configService = configService; - } + @Resource + private SolrInternetSearchVideoDataManager solrInternetSearchVideoDataManager; - public SuperHotSearchDao getSuperHotSearchDao() { - return superHotSearchDao; - } + @Resource + private VideoInfoExtraService videoInfoExtraService; - public void setSuperHotSearchDao(SuperHotSearchDao superHotSearchDao) { - this.superHotSearchDao = superHotSearchDao; - } + @SuppressWarnings("rawtypes") + @Cacheable(value = "userCache", key = "'suggestSearch'+'-'+#key+'-'+#system") + public List<String> suggestSearch(String key, String system) { + if (StringUtil.isNullOrEmpty(key) || key.startsWith("%")) + return new ArrayList<>(); - public HotSearchDao getHotSearchDao() { - return hotSearchDao; - } + List<String> list; + Session session = null; + if (key != null && key.length() > 10) + key = key.substring(0, 10); + String sql = ""; + if (system.equalsIgnoreCase("1")) { + sql = "select name as result from wk_video_video where name like ?"; + } - public void setHotSearchDao(HotSearchDao hotSearchDao) { - this.hotSearchDao = hotSearchDao; - } + list = new ArrayList<>(); - public VideoInfoDao getVideoInfoDao() { - return videoInfoDao; - } + List li = null; + try { + li = videoInfoDao.sqlList(sql, + new String[]{(new StringBuilder(String.valueOf(key))).append("%").toString()}); + } catch (Exception e) { - public void setVideoInfoDao(VideoInfoDao videoInfoDao) { - this.videoInfoDao = videoInfoDao; - } + } + if (li != null) { + for (int i = 0; i < li.size(); i++) { + Object objs = (Object) li.get(i); + list.add(objs.toString()); + } - public ClassService getClassService() { - return classService; - } + } - public void setClassService(ClassService classService) { - this.classService = classService; - } + Set<String> set = new TreeSet<String>(); + if (list != null) { + String st; + for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); set.add(st)) + st = (String) iterator.next(); - public SearchDao getSearchDao() { - return searchDao; - } + } - public void setSearchDao(SearchDao searchDao) { - this.searchDao = searchDao; - } + Iterator<String> it = set.iterator(); + list = new ArrayList<>(); + while (it.hasNext()) { + list.add(it.next()); + if (list.size() > 10) + break; + } - @SuppressWarnings("rawtypes") - @Cacheable(value = "userCache", key = "'suggestSearch'+'-'+#key+'-'+#system") - public List<String> suggestSearch(String key, String system) { - if (StringUtil.isNullOrEmpty(key) || key.startsWith("%")) - return new ArrayList<String>(); + return list; + } - List<String> list; - Session session = null; - if (key != null && key.length() > 10) - key = key.substring(0, 10); - String sql = ""; - if (system.equalsIgnoreCase("1")) { - sql = "select name as result from wk_video_video where name like ?"; - } + @SuppressWarnings("unchecked") + public List<Serializable> search(String ip, String uid, String key, int page, int type, int videoType, + String system) { - list = new ArrayList<String>(); + SearchHistory sh = new SearchHistory(); + sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString()); + sh.setIp(ip); + sh.setKey(key); + UserInfo user = new UserInfo(); + user.setId(uid); + sh.setUser(user); + searchDao.create(sh); + // 鏌ヨ鏁版嵁搴� + String videoTypeList[] = classService.getChildrenType(videoType); + String typewhere = ""; + if (system.equalsIgnoreCase("2")) { + typewhere = " v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108 "; + } else if (videoType > 0) { + for (int i = 0; i < videoTypeList.length; i++) + typewhere = (new StringBuilder(String.valueOf(typewhere))).append(" or v.videoType.id=") + .append(videoTypeList[i]).toString(); - List li = null; - try { - li = videoInfoDao.sqlList(sql, - new String[] { (new StringBuilder(String.valueOf(key))).append("%").toString() }); - } catch (Exception e) { + if (typewhere.startsWith(" or")) + typewhere = typewhere.substring(3, typewhere.length()); + } - } - if (li != null) { - for (int i = 0; i < li.size(); i++) { - Object objs = (Object) li.get(i); - list.add(objs.toString()); - } + String sql; + if (!StringUtil.isNullOrEmpty(typewhere))// 鏀逛负sql + sql = (new StringBuilder("from VideoInfo v where v.show='1' and (v.name like ?) and (")).append(typewhere) + .append(") order by REPLACE(v.name,?,'') ,v.watchCount desc").toString(); + else + sql = "from VideoInfo v where v.show='1' and (v.name like ?) order by REPLACE(v.name,?,''),v.watchCount desc";// 鏀逛负sql + List<Serializable> list = new ArrayList<>(); + BaiduVideoUtil util = new BaiduVideoUtil(); + List<VideoInfo> localList = videoInfoDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, + new String[]{(new StringBuilder("")).append(key).append("%").toString(), key}); + if (localList != null && localList.size() > 0) { + for (VideoInfo info : localList) { + list.add(info); + } + } - } + // 鏌ヨ鏁版嵁搴撶粨鏉� - Set<String> set = new TreeSet<String>(); - if (list != null) { - String st; - for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); set.add(st)) - st = (String) iterator.next(); + if (system.equalsIgnoreCase("1")) + if (page == 1) + try { + List<VideoInfo> fromNet = util.searchVideo(key); + if (fromNet != null && fromNet.size() > 0) { + for (int i = 0; i < fromNet.size(); i++) + if (videoType == 0) + list.add((VideoInfo) fromNet.get(i)); + else if (((VideoInfo) fromNet.get(i)).getVideoType().getId() == (long) videoType) + list.add((VideoInfo) fromNet.get(i)); - } + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } - Iterator<String> it = set.iterator(); - list = new ArrayList<String>(); - while (it.hasNext()) { - list.add(it.next()); - if (list.size() > 10) - break; - } + final List<Serializable> newList = list; + list = (List<Serializable>) searchDao.excute(new HibernateCallback<List<Serializable>>() { + public List<Serializable> doInHibernate(Session session) throws HibernateException { + return BanQuanUtil.filterShowVideo(newList, session); + } + }); + return list; + } - return list; - } + @Cacheable(value = "userCache", key = "'search'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#contentType+'-'+#system+'-'+#cacheMD5") + public List<VideoInfo> search(String detailSystem, String ip, String uid, String key, int page, int contentType, + String system, List<Long> resourceList, String cacheMD5) { + String resourceWhere = ""; + for (Long re : resourceList) { + resourceWhere += " rv.resourceid=" + re + " or"; + } - @SuppressWarnings("unchecked") - public List<Serializable> search(String ip, String uid, String key, int page, int type, int videoType, - String system) { + if (resourceWhere.endsWith("or")) + resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); - SearchHistory sh = new SearchHistory(); - sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString()); - sh.setIp(ip); - sh.setKey(key); - UserInfo user = new UserInfo(); - user.setId(uid); - sh.setUser(user); - searchDao.create(sh); - // 鏌ヨ鏁版嵁搴� - String videoTypeList[] = classService.getChildrenType(videoType); - String typewhere = ""; - if (system.equalsIgnoreCase("2")) { - typewhere = " v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108 "; - } else if (videoType > 0) { - for (int i = 0; i < videoTypeList.length; i++) - typewhere = (new StringBuilder(String.valueOf(typewhere))).append(" or v.videoType.id=") - .append(videoTypeList[i]).toString(); + SearchHistory sh = new SearchHistory(); + sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString()); + sh.setIp(ip); + if (!StringUtil.isNullOrEmpty(key) && key.length() > 30) + key = key.substring(0, 30); + sh.setKey(key); + UserInfo user = new UserInfo(); + user.setId(uid); + sh.setUser(user); - if (typewhere.startsWith(" or")) - typewhere = typewhere.substring(3, typewhere.length()); - } + searchDao.create(sh); + // 鏌ヨ鏁版嵁搴� - String sql; - if (!StringUtil.isNullOrEmpty(typewhere))// 鏀逛负sql - sql = (new StringBuilder("from VideoInfo v where v.show='1' and (v.name like ?) and (")).append(typewhere) - .append(") order by REPLACE(v.name,?,'') ,v.watchCount desc").toString(); - else - sql = "from VideoInfo v where v.show='1' and (v.name like ?) order by REPLACE(v.name,?,''),v.watchCount desc";// 鏀逛负sql - List<Serializable> list = new ArrayList<Serializable>(); - BaiduVideoUtil util = new BaiduVideoUtil(); - List<VideoInfo> localList = videoInfoDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, - new String[] { (new StringBuilder("")).append(key).append("%").toString(), key }); - if (localList != null && localList.size() > 0) { - for (VideoInfo info : localList) { - list.add(info); - } - } + List<VideoInfo> list = new ArrayList<>(); + List<VideoInfo> localList = null; + try { + long startt = System.currentTimeMillis(); + localList = null; + if (contentType == 0) + localList = SolrUtil.search(key, page); + else if (contentType == 1) + localList = SolrUtil.searchZhengPian(key, page); + else + localList = SolrUtil.searchZhuanJi(key, page); + System.out.println("鎼滅储鑰楁椂:" + (System.currentTimeMillis() - startt)); - // 鏌ヨ鏁版嵁搴撶粨鏉� + String sql = ""; + startt = System.currentTimeMillis(); + for (int i = 0; i < localList.size(); i++) { + sql += " select count(*) from wk_resource_video rv left join wk_video_video v on rv.videoid=v.id where v.id is not null and v.show=1 and rv.videoid=" + + localList.get(i).getId() + " and (" + resourceWhere + ") union all"; + } - if (system.equalsIgnoreCase("1")) - if (page == 1) - try { - List<VideoInfo> fromNet = util.searchVideo(key); - if (fromNet != null && fromNet.size() > 0) { - for (int i = 0; i < fromNet.size(); i++) - if (videoType == 0) - list.add((VideoInfo) fromNet.get(i)); - else if (((VideoInfo) fromNet.get(i)).getVideoType().getId() == (long) videoType) - list.add((VideoInfo) fromNet.get(i)); + if (sql.endsWith("union all")) + sql = sql.substring(0, sql.length() - 9); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + List rlist = videoInfoDao.sqlList(sql); + List<Integer> pList = new ArrayList<>(); + for (int i = 0; i < localList.size(); i++) { + if (Integer.parseInt(rlist.get(i) + "") < 1) { + pList.add(i); + // localList.remove(i); + // i--; + } + } - final List<Serializable> newList = list; - list = (List<Serializable>) searchDao.excute(new HibernateCallback<List<Serializable>>() { - public List<Serializable> doInHibernate(Session session) throws HibernateException { - return BanQuanUtil.filterShowVideo(newList, session); - } - }); - return list; - } + Collections.sort(pList); + for (int i = pList.size() - 1; i >= 0; i--) { + localList.remove((int) pList.get(i)); + } + + System.out.println("鎺掗櫎鑰楁椂:" + (System.currentTimeMillis() - startt)); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + } + if (localList != null && localList.size() > 0) { + for (VideoInfo info : localList) { + list.add(info); + } + } + return list; + } + + + private VideoInfo loadAlbumData(VideoInfo video) { + video.setShowType(1); + if (!StringUtil.isNullOrEmpty(video.getMainActor())) { + video.setMainActor("涓绘紨锛�" + video.getMainActor()); + } + + //璁剧疆tag + String tag = ""; + if (!StringUtil.isNullOrEmpty(video.getYear())) { + tag += video.getYear() + "/"; + } + + if (video.getVideoType() != null) { + String vt = VideoConstant.getMainCategoryName(video.getVideoType().getId()); + if (!StringUtil.isNullOrEmpty(vt)) { + tag += vt + "/"; + } + } + + if (!StringUtil.isNullOrEmpty(video.getArea())) { + tag += video.getArea() + "/"; + } + + if (tag.endsWith("/")) + tag = tag.substring(0, tag.length() - 1); + video.setTag(tag); + + return video; + + } + + + public SearchResult searchAlbum(int videoType, String key, List<Long> resourceIds, int page, int pageSize, boolean fuzzy) { + List<VideoInfo> localList = new ArrayList<>(); + //涓撹緫瑙嗛闆嗗悎 + Set<String> solrAlbumVids = new HashSet<>(); + + SolrVideoSearchFilter filter = new SolrVideoSearchFilter(); + filter.setFuzzy(fuzzy); + filter.setKey(key); + if (videoType == Constant.SEARCH_RESULT_TYPE_HIGH_DEFINITION) { + filter.setResourceIds(Arrays.asList(new String[]{PPTVUtil.RESOURCE_ID + ""})); + logger.info("楂樻竻鎼滅储# key锛歿}", key); + } else { + List<String> rids = new ArrayList<>(); + for (Long rid : resourceIds) { + rids.add(rid + ""); + } + filter.setResourceIds(rids); + filter.setVideoType(videoType == 0 ? null : videoType); + } + filter.setContentType(1); + + SolrResultDTO solrResultDTO = solrAlbumDataManager.find(filter, page, pageSize); + Set<String> albumSet = new HashSet<>(); + int albumCount = 0; + if (solrResultDTO != null) { + albumCount = solrResultDTO.getTotalCount(); + for (SolrAlbumVideo sv : (List<SolrAlbumVideo>) solrResultDTO.getVideoList()) { + VideoInfo video = VideoInfoFactory.create(sv); + albumSet.add(video.getName() + "#" + video.getVideoType().getId() + "#" + video.getYear()); + video = loadAlbumData(video); + localList.add(video); + solrAlbumVids.add(video.getId()); + } + } + + //绗竴椤�,涓嶄负楂樻竻鏃舵悳绱� + if (page == 1 && videoType != Constant.SEARCH_RESULT_TYPE_HIGH_DEFINITION) { + //page涓�1鏃� 鍏ㄧ綉鎼� + SolrResultDTO internetSearchResultDTO = solrInternetSearchVideoDataManager.find(filter, 1, 20); + if (internetSearchResultDTO != null && internetSearchResultDTO.getVideoList().size() > 0) { + List<InternetSearchVideo> iList = internetSearchResultDTO.getVideoList(); + List<InternetSearchVideo> isvList = new ArrayList<>(); + if (iList != null && iList.size() > 0) + isvList.addAll(iList); + //鍒犻櫎鍓嶉潰鏈夌殑鏁版嵁锛屾牴鎹畆ootType涓庡悕绉扮瓫閫� + for (int i = 0; i < isvList.size(); i++) { + String uniqueId = isvList.get(i).getName() + "#" + isvList.get(i).getRootType() + "#" + isvList.get(i).getYear(); + if (albumSet.contains(uniqueId)) { + isvList.remove(i); + i--; + } + } + + //绮剧‘鍖归厤锛屼笉鍒嗚瘝 + if (!fuzzy) { + for (InternetSearchVideo sv : isvList) { + VideoInfo video = VideoInfoFactory.create(sv); + video = loadAlbumData(video); + //绗竴涓笓杈戞暟閲� + if (albumCount < 3) { + localList.add(video); + } else { + if (video.getName().equalsIgnoreCase(key)) + localList.add(video); + } + } + } else { + for (InternetSearchVideo sv : isvList) { + VideoInfo video = VideoInfoFactory.create(sv); + video = loadAlbumData(video); + localList.add(video); + } + } + } + //鎸夊叧閿瘝鐨勫尮閰嶅害鎺掑簭 + Comparator<VideoInfo> cm = (VideoInfo o1, VideoInfo o2) -> { + if (o1.getDefinition() - o2.getDefinition() == 0) { + float s1 = CompareStrSimUtil.getSimilarityRatio(o1.getName(), key, true); + float s2 = CompareStrSimUtil.getSimilarityRatio(o2.getName(), key, true); +// if (s1 != s2)//鐩镐技搴﹁秺楂樿秺闈犲墠 + try { + return s1 != s2 ? (s2 > s1 ? 1 : -1) : (Integer.parseInt(o2.getYear()) - Integer.parseInt(o1.getYear())); + } catch (Exception e) { + return s2 > s1 ? 1 : -1; + } +// else +// return o1.getDefinition() - o2.getDefinition();//楂樻竻鎺掑湪鍓� + } else { + return o2.getDefinition() - o1.getDefinition();//楂樻竻鎺掑湪鍓� + } + + }; + + Collections.sort(localList, cm); + } + //鏈悳绱㈠埌鍐呭 + if (localList == null || localList.size() == 0) { + logger.warn("涓撹緫鎼滅储鏃犵粨鏋�#{}", key); + } + return new SearchResult(albumCount, localList); + } + + private void filterSearchResult(List<VideoInfo> localList, List<Long> resourceList) { + String sql = ""; + String resourceWhere = ""; + for (Long re : resourceList) { + resourceWhere += " rv.resourceid=" + re + " or"; + } + + if (resourceWhere.endsWith("or")) + resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); + + + List<String> filterVideoIdList = new ArrayList<>(); + + for (int i = 0; i < localList.size(); i++) { + if (NumberUtil.isNumeric(localList.get(i).getId())) { + filterVideoIdList.add(localList.get(i).getId()); + sql += " select count(*) from wk_resource_video rv left join wk_video_video v on rv.videoid=v.id where v.id is not null and v.show=1 and rv.videoid=" + + localList.get(i).getId() + " and (" + resourceWhere + ") union all"; + } + } + + if (sql.endsWith("union all")) + sql = sql.substring(0, sql.length() - 9); + + List rlist = StringUtil.isNullOrEmpty(sql) ? new ArrayList() : videoInfoDao.sqlList(sql); + Set<String> deleteVids = new HashSet<>(); + for (int i = 0; i < filterVideoIdList.size(); i++) { + if (Integer.parseInt(rlist.get(i) + "") < 1) { + deleteVids.add(filterVideoIdList.get(i)); + } + } + + for (int i = 0; i < localList.size(); i++) { + if (deleteVids.contains(localList.get(i).getId())) { + localList.remove(i); + i--; + } + } + } + + + @Cacheable(value = "userCache", key = "'searchNew'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#videoType+'-'+#system+'-'+#cacheMD5") + public VideoListResultVO searchNew(String detailSystem, String ip, String uid, String key, int page, int videoType, + String system, List<Long> resourceList, String cacheMD5) { + SearchHistory sh = new SearchHistory(); + sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString()); + sh.setIp(ip); + if (!StringUtil.isNullOrEmpty(key) && key.length() > 30) + key = key.substring(0, 30); + sh.setKey(key); + if (uid != null) { + UserInfo user = new UserInfo(); + user.setId(uid); + sh.setUser(user); + } + +// searchDao.create(sh); + // 鏌ヨ鏁版嵁搴� + + //涓撹緫鏁伴噺 + long albumCount = 0L; + List<VideoInfo> list = new ArrayList<>(); + List<VideoInfo> localList = null; + long startt = System.currentTimeMillis(); + try { + + localList = null; + + localList = new ArrayList<>(); + //鍏堟悳绱笓杈� + int pageSize = 20; + SearchResult searchResult = searchAlbum(videoType, key, resourceList, page, pageSize, false); + + if (page == 1 && (searchResult == null || searchResult.videoInfoList == null || searchResult.videoInfoList.size() == 0)) { + searchResult = searchAlbum(videoType, key, resourceList, page, pageSize, true); + } + + debugLogger.info(String.format("鎼滅储-solr涓撹緫鎼滅储鑰楁椂锛氾紙%s锛�-%s",key,(System.currentTimeMillis() - startt))); + startt = System.currentTimeMillis(); + if (searchResult != null && searchResult.videoInfoList != null) { + localList.addAll(searchResult.videoInfoList); + } + + Set<String> solrAlbumVids = new HashSet<>(); + for (VideoInfo videoInfo : localList) { + solrAlbumVids.add(videoInfo.getId()); + } + + + int commonSolrPage = page - searchResult.getTotalCount() / pageSize; - @Cacheable(value = "userCache", key = "'search'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#contentType+'-'+#system+'-'+#cacheMD5") - public List<VideoInfo> search(String detailSystem, String ip, String uid, String key, int page, int contentType, - String system, List<Long> resourceList, String cacheMD5) { - String resourceWhere = ""; - for (Long re : resourceList) { - resourceWhere += " rv.resourceid=" + re + " or"; - } + if (videoType == 0 && commonSolrPage > 0) { + //鎼滅储鍘熷鐨� + try { + List<VideoInfo> solrList = SolrUtil.search(key, commonSolrPage); + for (VideoInfo vi : solrList) { + if (!solrAlbumVids.contains(vi.getId())) { + localList.add(vi); + } + } + } catch (Exception e) { + debugLogger.error("甯歌鎼滅储閿欒锛歿}","",e); + }finally { + debugLogger.info(String.format("鎼滅储-solr甯歌鎼滅储鑰楁椂锛氾紙%s锛�-%s",key,(System.currentTimeMillis() - startt))); + startt = System.currentTimeMillis(); + } - if (resourceWhere.endsWith("or")) - resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); - SearchHistory sh = new SearchHistory(); - sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString()); - sh.setIp(ip); - if (!StringUtil.isNullOrEmpty(key) && key.length() > 30) - key = key.substring(0, 30); - sh.setKey(key); - UserInfo user = new UserInfo(); - user.setId(uid); - sh.setUser(user); + } - searchDao.create(sh); - // 鏌ヨ鏁版嵁搴� + startt = System.currentTimeMillis(); + filterSearchResult(localList, resourceList); + debugLogger.info(String.format("鎼滅储-杩囨护鑰楁椂锛氾紙%s锛�-%s",key,(System.currentTimeMillis() - startt))); + } catch (Exception e) { + e.printStackTrace(); + } finally { + } + if (localList != null && localList.size() > 0) { + list.addAll(localList); + } + startt = System.currentTimeMillis(); + //鑾峰彇闄勫姞淇℃伅 + list = videoInfoExtraService.batchExtra(list, resourceList); + debugLogger.info(String.format("鎼滅储-闄勫姞淇℃伅鑰楁椂锛氾紙%s锛�-%s",key,(System.currentTimeMillis() - startt))); + return new VideoListResultVO(list, videoType == 0 ? albumCount : 1000L); + } - List<VideoInfo> list = new ArrayList<VideoInfo>(); - List<VideoInfo> localList = null; - try { - // localList = session0 - // .createQuery(sql) - // .setParameter( - // 0, - // (new StringBuilder("")).append(key).append("%") - // .toString()).setParameter(1, key) - // .setFirstResult((page - 1) * Constant.pageCount) - // .setMaxResults(Constant.pageCount).list(); - long startt = System.currentTimeMillis(); - localList = null; + public long searchCount(String key, String system) { + String sql = ""; + if (system.equalsIgnoreCase("2")) { + sql = "select count(*) from VideoInfo v where v.show='1' and (v.name like ?) and (v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108))"; + } else { + sql = "select count(*) from VideoInfo v where v.show='1' and (v.name like ?)"; + } - if (contentType == 0) - localList = SolrUtil.search(key, page); - else if (contentType == 1) - localList = SolrUtil.searchZhengPian(key, page); - else - localList = SolrUtil.searchZhuanJi(key, page); - System.out.println("鎼滅储鑰楁椂:" + (System.currentTimeMillis() - startt)); + long count = videoInfoDao.getCount(sql, + new String[]{(new StringBuilder("")).append(key).append("%").toString()}); + return count; + } - String sql = ""; - startt = System.currentTimeMillis(); - for (int i = 0; i < localList.size(); i++) { - sql += " select count(*) from wk_resource_video rv left join wk_video_video v on rv.videoid=v.id where v.id is not null and v.show=1 and rv.videoid=" - + localList.get(i).getId() + " and (" + resourceWhere + ") union all"; - } + @SuppressWarnings({"rawtypes"}) + @Cacheable(value = "userCache", key = "'getHotSearchList'+'-'+#system") + public List<String> getHotSearchList(String system) { + List<String> list = new ArrayList<>(); + try { + List li = searchDao.sqlList( + "SELECT h.`name` FROM wk_video_super_hotsearch sh LEFT JOIN wk_video_hotsearch h ON sh.`hotsearchid`=h.`id` WHERE sh.`detailsystem`=? ORDER BY h.`orderby` DESC", + 0, Constant.HOT_SEARCH_COUNT, new Serializable[]{system}); + for (int i = 0; i < li.size(); i++) { + list.add(li.get(i) + ""); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { - if (sql.endsWith("union all")) - sql = sql.substring(0, sql.length() - 9); + } + return list; + } - List rlist = videoInfoDao.sqlList(sql); - List<Integer> pList = new ArrayList<Integer>(); - for (int i = 0; i < localList.size(); i++) { - if (Integer.parseInt(rlist.get(i) + "") < 1) { - pList.add(i); - // localList.remove(i); - // i--; - } - } + public List<HotSearch> getHotSearchListByDS(String dsid) { + return hotSearchDao.list("from HotSearch hs where hs.system.id=" + dsid + " order by hs.orderby desc"); + } - Collections.sort(pList); - for (int i = pList.size() - 1; i >= 0; i--) { - localList.remove((int) pList.get(i)); - } + public List<HotSearch> getHotSearchList() { + return hotSearchDao.list("from HotSearch"); + } - System.out.println("鎺掗櫎鑰楁椂:" + (System.currentTimeMillis() - startt)); + /** + * 鐑棬鎼滅储 + */ - } catch (Exception e) { - e.printStackTrace(); - } finally { - } - if (localList != null && localList.size() > 0) { - for (VideoInfo info : localList) { - list.add(info); - } - } + public void addSuperHotSearch(SuperHotSearch sv) { + List<SuperHotSearch> list = superHotSearchDao.list("from SuperHotSearch sv where sv.hotSearch.id=" + + sv.getHotSearch().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId()); + if (list != null && list.size() > 0) + return; + superHotSearchDao.create(sv); + } - // 鏌ヨ鏁版嵁搴撶粨鏉� - // //Session session = HibernateSessionFactory.getSession(); - // try { - // - // // 鏆傛椂涓嶇敤鐗堟潈鏁版嵁搴� - // // list = BanQuanUtil.filterShowVideo(detailSystem, list, session); - // - // - // - // - // } catch (Exception e) { - // e.printStackTrace(); - // } finally { - // // session.close(); - // } - return list; - } + public void updateSuperHotSearch(SuperHotSearch hotSearch) { + superHotSearchDao.update(hotSearch); + } - public long searchCount(String key, String system) { - String sql = ""; - if (system.equalsIgnoreCase("2")) { - sql = "select count(*) from VideoInfo v where v.show='1' and (v.name like ?) and (v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108))"; - } else { - sql = "select count(*) from VideoInfo v where v.show='1' and (v.name like ?)"; - } + public void deleteSuperHotSearch(SuperHotSearch hotSearch) { + superHotSearchDao.delete(hotSearch); + } - long count = videoInfoDao.getCount(sql, - new String[] { (new StringBuilder("")).append(key).append("%").toString() }); - return count; - } + @SuppressWarnings("unchecked") + public void updateSuperHotSearchList(final String detailSystemId, final List<SuperHotSearch> typeList) { + searchDao.excute(new HibernateCallback() { + public Object doInHibernate(Session session) throws HibernateException { + try { + List<SuperHotSearch> list = session + .createQuery("from SuperHotSearch sh where sh.detailSystem.id=" + detailSystemId).list(); + session.getTransaction().begin(); + for (SuperHotSearch ad : list) { + session.delete(ad); + } - @SuppressWarnings({ "rawtypes" }) - @Cacheable(value = "userCache", key = "'getHotSearchList'+'-'+#system") - public List<String> getHotSearchList(String system) { - List<String> list = new ArrayList<String>(); - try { - List li = searchDao.sqlList( - "SELECT h.`name` FROM wk_video_super_hotsearch sh LEFT JOIN wk_video_hotsearch h ON sh.`hotsearchid`=h.`id` WHERE sh.`detailsystem`=? ORDER BY h.`orderby` DESC", - 0, Constant.HOT_SEARCH_COUNT, new Serializable[] { system }); - for (int i = 0; i < li.size(); i++) { - list.add(li.get(i) + ""); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { + for (SuperHotSearch videoType : typeList) { + session.persist(videoType); + } + session.flush(); + session.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + session.getTransaction().rollback(); + } - } - return list; - } + return null; + } + }); - public List<HotSearch> getHotSearchListByDS(String dsid) { - return hotSearchDao.list("from HotSearch hs where hs.system.id=" + dsid + " order by hs.orderby desc"); - } + } - public List<HotSearch> getHotSearchList() { - return hotSearchDao.list("from HotSearch"); - } + @SuppressWarnings("unchecked") + public List<HotSearchAdmin> getHotSearchAdmin(String key, String systemId, int detailSystem, int page) { + List<HotSearchAdmin> zhiBoClassList = new ArrayList<>(); + try { + List<DetailSystem> detailSystemList = detailSystemDao.list("from DetailSystem ds where ds.system.id=" + systemId); + String sql = ""; + if (detailSystem > 0) + sql = "select sh.hotSearch from SuperHotSearch sh where sh.hotSearch.name like ? and sh.detailSystem.id=" + + detailSystem + " order by sh.createtime desc"; + else + sql = "from HotSearch zb where zb.name like ? and zb.system.id=" + systemId + " order by zb.createtime desc"; - /** - * 鐑棬鎼滅储 - */ + List<HotSearch> list = hotSearchDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, + new Serializable[]{"%" + key + "%"}); + for (HotSearch vb : list) { + List<DetailSystem> detailSystemS = detailSystemDao + .list("select vb.detailSystem from SuperHotSearch vb where vb.hotSearch.id=" + vb.getId()); - public void addSuperHotSearch(SuperHotSearch sv) { - List<SuperHotSearch> list = superHotSearchDao.list("from SuperHotSearch sv where sv.hotSearch.id=" - + sv.getHotSearch().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId()); - if (list != null && list.size() > 0) - return; - superHotSearchDao.create(sv); - } + List<DetailSystemSelect> dssList = new ArrayList<>(); - public void updateSuperHotSearch(SuperHotSearch hotSearch) { - superHotSearchDao.update(hotSearch); - } + for (DetailSystem ds : detailSystemList) { + DetailSystemSelect dss = new DetailSystemSelect(); + dss.setDetailSystem(ds); + dss.setSelected(false); + dssList.add(dss); + } - public void deleteSuperHotSearch(SuperHotSearch hotSearch) { - superHotSearchDao.delete(hotSearch); - } + // 璁剧疆宸茬粡瀛樺湪鐨� + for (DetailSystem ds : detailSystemS) { + for (DetailSystemSelect dss : dssList) { + if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) { + dss.setSelected(true); + break; + } + } + } + SuperHotSearch sz = new SuperHotSearch(); + sz.setDetailSystem(null); + sz.setHotSearch(vb); + zhiBoClassList.add(new HotSearchAdmin(sz, dssList)); + } - @SuppressWarnings("unchecked") - public void updateSuperHotSearchList(final String detailSystemId, final List<SuperHotSearch> typeList) { - searchDao.excute(new HibernateCallback() { - public Object doInHibernate(Session session) throws HibernateException { - try { - List<SuperHotSearch> list = session - .createQuery("from SuperHotSearch sh where sh.detailSystem.id=" + detailSystemId).list(); - session.getTransaction().begin(); - for (SuperHotSearch ad : list) { - session.delete(ad); - } + } catch (Exception e) { + e.printStackTrace(); + } + return zhiBoClassList; + } - for (SuperHotSearch videoType : typeList) { - session.persist(videoType); - } - session.flush(); - session.getTransaction().commit(); - } catch (Exception e) { - e.printStackTrace(); - session.getTransaction().rollback(); - } + public long getHotSearchAdminCount(String key, String systemId, int detailSystem) { + String sql = ""; + if (detailSystem > 0) + sql = "select count(*) from (select count(*) from wk_video_super_hotsearch zb left join wk_video_hotsearch c on c.id=zb.hotsearchid where zb.detailsystem=" + + detailSystem + " and c.name like '%" + key + "%' group by zb.hotsearchid) s"; + else + sql = "select count(*) from wk_video_hotsearch h where h.name like '%" + key + "%' and h.system=" + systemId; - return null; - } - }); + return videoBanQuanVideoDao.getCountSQL(sql); + } - } + /** + * 娣诲姞鐑棬鎼滅储 + * + * @param search + */ + public void addHotSearch(HotSearch search) { + hotSearchDao.create(search); + } - @SuppressWarnings("unchecked") - public List<HotSearchAdmin> getHotSearchAdmin(String key, int detailSystem, int page) { - List<HotSearchAdmin> zhiBoClassList = new ArrayList<HotSearchAdmin>(); - try { - List<DetailSystem> detailSystemList = detailSystemDao.list("from DetailSystem"); - String sql = ""; - if (detailSystem > 0) - sql = "select sh.hotSearch from SuperHotSearch sh where sh.hotSearch.name like ? and sh.detailSystem.id=" - + detailSystem + " order by sh.createtime desc"; - else - sql = "from HotSearch zb where zb.name like ? order by zb.createtime desc"; + /** + * 淇敼鐑棬鎼滅储 + * + * @param search + */ + public void updateHotSearch(HotSearch search) { - List<HotSearch> list = hotSearchDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, - new Serializable[] { "%" + key + "%" }); - for (HotSearch vb : list) { - List<DetailSystem> detailSystemS = detailSystemDao - .list("select vb.detailSystem from SuperHotSearch vb where vb.hotSearch.id=" + vb.getId()); + hotSearchDao.update(search); + } - List<DetailSystemSelect> dssList = new ArrayList<DetailSystemSelect>(); + /** + * 鍒犻櫎鐑棬鎼滅储 + * + * @param search + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public void deleteHotSearch(final HotSearch search) { + searchDao.excute(new HibernateCallback() { + public Object doInHibernate(Session session) throws HibernateException { + try { + session.getTransaction().begin(); + List<SuperHotSearch> list = session.createQuery("from SuperHotSearch sv where sv.hotSearch.id=?") + .setParameter(0, search.getId()).list(); + for (SuperHotSearch sv : list) + session.delete(sv); + session.createSQLQuery("delete from wk_video_hotsearch where id=" + search.getId()) + .executeUpdate(); + session.flush(); + session.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + session.getTransaction().rollback(); + } + return null; + } + }); - for (DetailSystem ds : detailSystemList) { - DetailSystemSelect dss = new DetailSystemSelect(); - dss.setDetailSystem(ds); - dss.setSelected(false); - dssList.add(dss); - } + } - // 璁剧疆宸茬粡瀛樺湪鐨� - for (DetailSystem ds : detailSystemS) { - for (DetailSystemSelect dss : dssList) { - if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) { - dss.setSelected(true); - break; - } - } - } - SuperHotSearch sz = new SuperHotSearch(); - sz.setDetailSystem(null); - sz.setHotSearch(vb); - zhiBoClassList.add(new HotSearchAdmin(sz, dssList)); - } + /** + * 鑾峰彇鐑棬鎼滅储 + * + * @param id + * @return + */ + public HotSearch getHotSearch(String id) { + List<HotSearch> list = hotSearchDao.getSession().createQuery("from HotSearch where id=?").setParameter(0, id) + .list(); + if (list != null && list.size() > 0) { + return list.get(0); + } + return null; + } - } catch (Exception e) { - e.printStackTrace(); - } - return zhiBoClassList; - } + @SuppressWarnings("unchecked") + public void deleteHotSearchAdmin(final String classId, final String detailSystemId) { + searchDao.excute(new HibernateCallback() { + public Object doInHibernate(Session session) throws HibernateException { + try { - public long getHotSearchAdminCount(String key, int detailSystem) { - String sql = ""; - if (detailSystem > 0) - sql = "select count(*) from (select count(*) from wk_video_super_hotsearch zb left join wk_video_hotsearch c on c.id=zb.hotsearchid where zb.detailsystem=" - + detailSystem + " and c.name like '%" + key + "%' group by zb.hotsearchid) s"; - else - sql = "select count(*) from wk_video_hotsearch h where h.name like '%"+key+"%'"; + List<SuperHotSearch> list = session + .createQuery("from SuperHotSearch vb where vb.hotSearch.id=? and vb.detailSystem.id=?") + .setParameter(0, classId).setParameter(1, detailSystemId).list(); + if (list != null && list.size() > 0) { + session.getTransaction().begin(); + for (SuperHotSearch vb : list) + session.delete(vb); + session.flush(); + session.getTransaction().commit(); + } - return videoBanQuanVideoDao.getCountSQL(sql); - } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + }); - /** - * 娣诲姞鐑棬鎼滅储 - * - * @param search - */ - public void addHotSearch(HotSearch search) { - hotSearchDao.create(search); - } + } - /** - * 淇敼鐑棬鎼滅储 - * - * @param search - */ - public void updateHotSearch(HotSearch search) { - hotSearchDao.update(search); - } + /** + * 灏嗘悳绱㈢粨鏋滃璞¤浆涓鸿棰戝璞� + * + * @param solrAlbumVideoList + * @param resourceList + * @return + */ + public List<VideoInfo> convertSolrAlbumResultToVideo(List<SolrAlbumVideo> solrAlbumVideoList, List<Long> resourceList) { + List<VideoInfo> videoInfoList = new ArrayList<>(); + for (SolrAlbumVideo sv : solrAlbumVideoList) { + VideoInfo video = VideoInfoFactory.create(sv); + videoInfoList.add(video); + } + //鑾峰彇闄勫姞淇℃伅 + videoInfoList = videoInfoExtraService.batchExtra(videoInfoList, resourceList); + return videoInfoList; + } - /** - * 鍒犻櫎鐑棬鎼滅储 - * - * @param search - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void deleteHotSearch(final HotSearch search) { - searchDao.excute(new HibernateCallback() { - public Object doInHibernate(Session session) throws HibernateException { - try { - session.getTransaction().begin(); - List<SuperHotSearch> list = session.createQuery("from SuperHotSearch sv where sv.hotSearch.id=?") - .setParameter(0, search.getId()).list(); - for (SuperHotSearch sv : list) - session.delete(sv); - session.createSQLQuery("delete from wk_video_hotsearch where id=" + search.getId()) - .executeUpdate(); - session.flush(); - session.getTransaction().commit(); - } catch (Exception e) { - e.printStackTrace(); - session.getTransaction().rollback(); - } - return null; - } - }); - } + public static class SearchResult { + private int totalCount; + private List<VideoInfo> videoInfoList; - /** - * 鑾峰彇鐑棬鎼滅储 - * - * @param id - * @return - */ - public HotSearch getHotSearch(String id) { - List<HotSearch> list = hotSearchDao.getSession().createQuery("from HotSearch where id=?").setParameter(0, id) - .list(); - if (list != null && list.size() > 0) { - return list.get(0); - } - return null; - } + public SearchResult(int totalCount, List<VideoInfo> videoInfoList) { + this.totalCount = totalCount; + this.videoInfoList = videoInfoList; + } - @SuppressWarnings("unchecked") - public void deleteHotSearchAdmin(final String classId, final String detailSystemId) { - searchDao.excute(new HibernateCallback() { - public Object doInHibernate(Session session) throws HibernateException { - try { + public int getTotalCount() { + return totalCount; + } - List<SuperHotSearch> list = session - .createQuery("from SuperHotSearch vb where vb.hotSearch.id=? and vb.detailSystem.id=?") - .setParameter(0, classId).setParameter(1, detailSystemId).list(); - if (list != null && list.size() > 0) { - session.getTransaction().begin(); - for (SuperHotSearch vb : list) - session.delete(vb); - session.flush(); - session.getTransaction().commit(); - } + public void setTotalCount(int totalCount) { + this.totalCount = totalCount; + } - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - }); + public List<VideoInfo> getVideoInfoList() { + return videoInfoList; + } - } + public void setVideoInfoList(List<VideoInfo> videoInfoList) { + this.videoInfoList = videoInfoList; + } + } } -- Gitblit v1.8.0