src/main/java/com/yeshi/buwan/service/imp/SearchService.java
@@ -1,32 +1,30 @@
package com.yeshi.buwan.service.imp;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.annotation.Resource;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.dao.*;
import com.yeshi.buwan.dao.system.DetailSystemDao;
import com.yeshi.buwan.domain.*;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.service.manager.SolrAlbumDataManager;
import com.yeshi.buwan.domain.solr.SolrAlbumVideo;
import com.yeshi.buwan.domain.system.DetailSystem;
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.service.inter.video.VideoInfoExtraService;
import com.yeshi.buwan.service.manager.SolrAlbumVideoDataManager;
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.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.web.DetailSystemSelect;
import com.yeshi.buwan.domain.web.HotSearchAdmin;
import javax.annotation.Resource;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.*;
@Service
public class SearchService {
@@ -41,22 +39,20 @@
    @Resource
    private SuperHotSearchDao superHotSearchDao;
    @Resource
    private ConfigService configService;
    @Resource
    private VideoBanQuanVideoDao videoBanQuanVideoDao;
    @Resource
    private DetailSystemDao detailSystemDao;
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
    private SolrAlbumVideoDataManager solrAlbumDataManager;
    @Resource
    private SolrAlbumDataManager solrAlbumDataManager;
    private VideoInfoExtraService videoInfoExtraService;
    @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 new ArrayList<>();
        List<String> list;
        Session session = null;
@@ -67,7 +63,7 @@
            sql = "select name as result from wk_video_video where name like ?";
        }
        list = new ArrayList<String>();
        list = new ArrayList<>();
        List li = null;
        try {
@@ -93,7 +89,7 @@
        }
        Iterator<String> it = set.iterator();
        list = new ArrayList<String>();
        list = new ArrayList<>();
        while (it.hasNext()) {
            list.add(it.next());
            if (list.size() > 10)
@@ -135,7 +131,7 @@
                    .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>();
        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});
@@ -196,7 +192,7 @@
        searchDao.create(sh);
        // 查询数据库
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        List<VideoInfo> list = new ArrayList<>();
        List<VideoInfo> localList = null;
        try {
            long startt = System.currentTimeMillis();
@@ -220,7 +216,7 @@
                sql = sql.substring(0, sql.length() - 9);
            List rlist = videoInfoDao.sqlList(sql);
            List<Integer> pList = new ArrayList<Integer>();
            List<Integer> pList = new ArrayList<>();
            for (int i = 0; i < localList.size(); i++) {
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    pList.add(i);
@@ -250,8 +246,8 @@
    @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) {
    public VideoListResultVO 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";
@@ -273,7 +269,9 @@
        searchDao.create(sh);
        // 查询数据库
        List<VideoInfo> list = new ArrayList<VideoInfo>();
        //专辑数量
        long albumCount = 0L;
        List<VideoInfo> list = new ArrayList<>();
        List<VideoInfo> localList = null;
        try {
            long startt = System.currentTimeMillis();
@@ -281,43 +279,56 @@
            localList = new ArrayList<>();
            //先搜索专辑
            List<SolrVideo> solrVideoList = solrAlbumDataManager.findByKey(key, 1, videoType == 0 ? null : videoType, page);
            int pageSize = 20;
            //专辑视频集合
            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() + "/";
                }
            SolrVideoSearchFilter filter = new SolrVideoSearchFilter();
            filter.setKey(key);
            filter.setVideoType(videoType == 0 ? null : videoType);
            filter.setContentType(1);
                if (video.getVideoType() != null) {
                    String vt = VideoConstant.getMainCategoryName(sv.getRootVideoType());
                    if (!StringUtil.isNullOrEmpty(vt)) {
                        tag += vt + "/";
            SolrResultDTO solrResultDTO = solrAlbumDataManager.find(filter, page, pageSize);
            if (solrResultDTO != null) {
                albumCount = solrResultDTO.getTotalCount();
                for (SolrAlbumVideo sv : (List<SolrAlbumVideo>) solrResultDTO.getVideoList()) {
                    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 (!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());
            }
            int commonSolrPage = page - solrResultDTO.getTotalCount() / pageSize;
            if (videoType == 0) {
            if (videoType == 0 && commonSolrPage > 0) {
                //搜索原始的
                List<VideoInfo> solrList = SolrUtil.search(key, page);
                List<VideoInfo> solrList = SolrUtil.search(key, commonSolrPage);
                for (VideoInfo vi : solrList) {
                    if (!solrAlbumVids.contains(vi.getId())) {
                        localList.add(vi);
@@ -336,8 +347,8 @@
            if (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
            List rlist = videoInfoDao.sqlList(sql);
            List<Integer> pList = new ArrayList<Integer>();
            List rlist = StringUtil.isNullOrEmpty(sql) ? new ArrayList() : 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);
@@ -363,7 +374,10 @@
            }
        }
        return list;
        //获取附加信息
        list = videoInfoExtraService.batchExtra(list, resourceList);
        return new VideoListResultVO(list, videoType == 0 ? albumCount : 1000L);
    }
    public long searchCount(String key, String system) {
@@ -382,7 +396,7 @@
    @SuppressWarnings({"rawtypes"})
    @Cacheable(value = "userCache", key = "'getHotSearchList'+'-'+#system")
    public List<String> getHotSearchList(String system) {
        List<String> list = new ArrayList<String>();
        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",
@@ -455,16 +469,16 @@
    }
    @SuppressWarnings("unchecked")
    public List<HotSearchAdmin> getHotSearchAdmin(String key, int detailSystem, int page) {
        List<HotSearchAdmin> zhiBoClassList = new ArrayList<HotSearchAdmin>();
    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");
            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 ? order by zb.createtime desc";
                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 + "%"});
@@ -472,7 +486,7 @@
                List<DetailSystem> detailSystemS = detailSystemDao
                        .list("select vb.detailSystem from SuperHotSearch vb where vb.hotSearch.id=" + vb.getId());
                List<DetailSystemSelect> dssList = new ArrayList<DetailSystemSelect>();
                List<DetailSystemSelect> dssList = new ArrayList<>();
                for (DetailSystem ds : detailSystemList) {
                    DetailSystemSelect dss = new DetailSystemSelect();
@@ -502,13 +516,13 @@
        return zhiBoClassList;
    }
    public long getHotSearchAdminCount(String key, int detailSystem) {
    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 + "%'";
            sql = "select count(*) from wk_video_hotsearch h where h.name like '%" + key + "%' and h.system=" + systemId;
        return videoBanQuanVideoDao.getCountSQL(sql);
    }
@@ -602,4 +616,23 @@
    }
    /**
     * 将搜索结果对象转为视频对象
     *
     * @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;
    }
}