package com.newvideo.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 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.newvideo.dao.DetailSystemDao;
|
import com.newvideo.dao.HotSearchDao;
|
import com.newvideo.dao.SearchDao;
|
import com.newvideo.dao.SuperHotSearchDao;
|
import com.newvideo.dao.VideoBanQuanVideoDao;
|
import com.newvideo.dao.VideoInfoDao;
|
import com.newvideo.domain.DetailSystem;
|
import com.newvideo.domain.HotSearch;
|
import com.newvideo.domain.SearchHistory;
|
import com.newvideo.domain.SuperHotSearch;
|
import com.newvideo.domain.UserInfo;
|
import com.newvideo.domain.VideoInfo;
|
import com.newvideo.domain.web.DetailSystemSelect;
|
import com.newvideo.domain.web.HotSearchAdmin;
|
import com.newvideo.util.BaiduVideoUtil;
|
import com.newvideo.util.BanQuanUtil;
|
import com.newvideo.util.Constant;
|
import com.newvideo.util.SolrUtil;
|
import com.newvideo.util.StringUtil;
|
|
@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;
|
|
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;
|
}
|
|
@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>();
|
|
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 ?";
|
}
|
|
list = new ArrayList<String>();
|
|
List li = null;
|
try {
|
li = videoInfoDao.sqlList(sql,
|
new String[] { (new StringBuilder(String.valueOf(key))).append("%").toString() });
|
} catch (Exception e) {
|
|
}
|
if (li != null) {
|
for (int i = 0; i < li.size(); i++) {
|
Object objs = (Object) li.get(i);
|
list.add(objs.toString());
|
}
|
|
}
|
|
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();
|
|
}
|
|
Iterator<String> it = set.iterator();
|
list = new ArrayList<String>();
|
while (it.hasNext()) {
|
list.add(it.next());
|
if (list.size() > 10)
|
break;
|
}
|
|
return list;
|
}
|
|
@SuppressWarnings("unchecked")
|
public List<Serializable> search(String ip, String uid, String key, int page, int type, int videoType,
|
String system) {
|
|
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();
|
|
if (typewhere.startsWith(" or"))
|
typewhere = typewhere.substring(3, typewhere.length());
|
}
|
|
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);
|
}
|
}
|
|
// 查询数据库结束
|
|
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();
|
}
|
|
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;
|
}
|
|
@SuppressWarnings("unchecked")
|
@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 (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 {
|
// 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)
|
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 (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);
|
}
|
}
|
|
// 查询数据库结束
|
// //Session session = HibernateSessionFactory.getSession();
|
// try {
|
//
|
// // 暂时不用版权数据库
|
// // list = BanQuanUtil.filterShowVideo(detailSystem, list, session);
|
//
|
//
|
//
|
//
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// } finally {
|
// // session.close();
|
// }
|
return list;
|
}
|
|
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 ?)";
|
}
|
|
long count = videoInfoDao.getCount(sql,
|
new String[] { (new StringBuilder("")).append(key).append("%").toString() });
|
return count;
|
}
|
|
@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 {
|
|
}
|
return list;
|
}
|
|
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");
|
}
|
|
/**
|
* 热门搜索
|
*/
|
|
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);
|
}
|
|
public void updateSuperHotSearch(SuperHotSearch hotSearch) {
|
superHotSearchDao.update(hotSearch);
|
}
|
|
public void deleteSuperHotSearch(SuperHotSearch hotSearch) {
|
superHotSearchDao.delete(hotSearch);
|
}
|
|
@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);
|
}
|
|
for (SuperHotSearch videoType : typeList) {
|
session.persist(videoType);
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
|
return null;
|
}
|
});
|
|
}
|
|
@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";
|
|
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());
|
|
List<DetailSystemSelect> dssList = new ArrayList<DetailSystemSelect>();
|
|
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));
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return zhiBoClassList;
|
}
|
|
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 (select count(*) from wk_video_super_hotsearch zb left join wk_video_hotsearch c on c.id=zb.hotsearchid where c.name like '%"
|
+ key + "%' group by zb.hotsearchid) s";
|
|
return videoBanQuanVideoDao.getCountSQL(sql);
|
}
|
|
/**
|
* 添加热门搜索
|
*
|
* @param search
|
*/
|
public void addHotSearch(HotSearch search) {
|
hotSearchDao.create(search);
|
}
|
|
/**
|
* 修改热门搜索
|
*
|
* @param search
|
*/
|
public void updateHotSearch(HotSearch search) {
|
|
hotSearchDao.update(search);
|
}
|
|
/**
|
* 删除热门搜索
|
*
|
* @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;
|
}
|
});
|
|
}
|
|
/**
|
* 获取热门搜索
|
*
|
* @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;
|
}
|
|
@SuppressWarnings("unchecked")
|
public void deleteHotSearchAdmin(final String classId, final String detailSystemId) {
|
searchDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
|
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();
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
}
|