admin
2020-09-12 2778cf2a16823f9b1153a0549b47f7b503176a17
src/main/java/com/yeshi/buwan/service/imp/SearchService.java
@@ -2,15 +2,17 @@
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 java.util.*;
import javax.annotation.Resource;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.domain.*;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.service.manager.SolrAlbumDataManager;
import com.yeshi.buwan.util.*;
import com.yeshi.buwan.util.factory.VideoInfoFactory;
import com.yeshi.buwan.util.video.VideoConstant;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
@@ -23,19 +25,8 @@
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;
@Service
public class SearchService {
@@ -55,62 +46,11 @@
   private VideoBanQuanVideoDao videoBanQuanVideoDao;
   @Resource
   private DetailSystemDao detailSystemDao;
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
   public VideoBanQuanVideoDao getVideoBanQuanVideoDao() {
      return videoBanQuanVideoDao;
   }
   public void setVideoBanQuanVideoDao(VideoBanQuanVideoDao videoBanQuanVideoDao) {
      this.videoBanQuanVideoDao = videoBanQuanVideoDao;
   }
   public ConfigService getConfigService() {
      return configService;
   }
   public void setConfigService(ConfigService configService) {
      this.configService = configService;
   }
   public SuperHotSearchDao getSuperHotSearchDao() {
      return superHotSearchDao;
   }
   public void setSuperHotSearchDao(SuperHotSearchDao superHotSearchDao) {
      this.superHotSearchDao = superHotSearchDao;
   }
   public HotSearchDao getHotSearchDao() {
      return hotSearchDao;
   }
   public void setHotSearchDao(HotSearchDao hotSearchDao) {
      this.hotSearchDao = hotSearchDao;
   }
   public VideoInfoDao getVideoInfoDao() {
      return videoInfoDao;
   }
   public void setVideoInfoDao(VideoInfoDao videoInfoDao) {
      this.videoInfoDao = videoInfoDao;
   }
   public ClassService getClassService() {
      return classService;
   }
   public void setClassService(ClassService classService) {
      this.classService = classService;
   }
   public SearchDao getSearchDao() {
      return searchDao;
   }
   public void setSearchDao(SearchDao searchDao) {
      this.searchDao = searchDao;
   }
    @Resource
    private SolrAlbumDataManager solrAlbumDataManager;
   @SuppressWarnings("rawtypes")
   @Cacheable(value = "userCache", key = "'suggestSearch'+'-'+#key+'-'+#system")
@@ -259,17 +199,8 @@
      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;
         if (contentType == 0)
            localList = SolrUtil.search(key, page);
         else if (contentType == 1)
@@ -314,22 +245,124 @@
            list.add(info);
         }
      }
        return list;
    }
      // 查询数据库结束
      // //Session session = HibernateSessionFactory.getSession();
      // try {
      //
      // // 暂时不用版权数据库
      // // list = BanQuanUtil.filterShowVideo(detailSystem, list, session);
      //
      //
      //
      //
      // } catch (Exception e) {
      // e.printStackTrace();
      // } finally {
      // // session.close();
      // }
    @Cacheable(value = "userCache", key = "'searchNew'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#videoType+'-'+#system+'-'+#cacheMD5")
    public List<VideoInfo> searchNew(String detailSystem, String ip, String uid, String key, int page, int videoType,
                                     String system, 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);
        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);
        // 查询数据库
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        List<VideoInfo> localList = null;
        try {
            long startt = System.currentTimeMillis();
            localList = null;
            localList = new ArrayList<>();
            //先搜索专辑
            List<SolrVideo> solrVideoList = solrAlbumDataManager.findByKey(key, 1, videoType == 0 ? null : videoType, page);
            Set<String> solrAlbumVids = new HashSet<>();
            for (SolrVideo sv : solrVideoList) {
                VideoInfo video = VideoInfoFactory.create(sv);
                video.setShowType(1);
                if (!StringUtil.isNullOrEmpty(video.getMainActor())) {
                    video.setMainActor("主演:" + video.getMainActor());
                }
                //设置tag
                String tag = "";
                if (!StringUtil.isNullOrEmpty(sv.getYear())) {
                    tag += sv.getYear() + "/";
                }
                if (video.getVideoType() != null) {
                    String vt = VideoConstant.getMainCategoryName(sv.getRootVideoType());
                    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);
                localList.add(video);
                solrAlbumVids.add(video.getId());
            }
            if (videoType == 0) {
                //搜索原始的
                List<VideoInfo> solrList = SolrUtil.search(key, page);
                for (VideoInfo vi : solrList) {
                    if (!solrAlbumVids.contains(vi.getId())) {
                        localList.add(vi);
                    }
                }
            }
            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 (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
            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--;
                }
            }
            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;
   }