package com.newvideo.service.imp.zhibo;
|
|
import java.io.Serializable;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.List;
|
|
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.zhibo.LiveTypeDao;
|
import com.newvideo.dao.zhibo.ZhiBoStatisticsDao;
|
import com.newvideo.dao.zhibo.ZhiBoStatisticsDetailDao;
|
import com.newvideo.util.Constant;
|
import com.newvideo.util.TimeUtil;
|
import com.newvideo.util.zhibo.ZhiBoUtil;
|
import com.newvideo.zhibo.entity.Live;
|
import com.newvideo.zhibo.entity.LiveType;
|
import com.newvideo.zhibo.entity.ZhiBoContent;
|
import com.newvideo.zhibo.entity.ZhiBoStatistics;
|
import com.newvideo.zhibo.entity.ZhiBoStatisticsDetail;
|
import com.newvideo.zhibo.ljf.entity.LJFLiveData;
|
|
import net.sf.ehcache.statistics.LiveCacheStatisticsData;
|
|
@Service
|
public class ZhiBoServcie {
|
@Resource
|
private ZhiBoStatisticsDao zhiBoStatisticsDao;
|
@Resource
|
private ZhiBoStatisticsDetailDao zhiBoStatisticsDetailDao;
|
|
@Resource
|
private LiveTypeDao liveTypeDao;
|
|
@SuppressWarnings("rawtypes")
|
public void addStatistics(final String rid, final int type) {
|
zhiBoStatisticsDetailDao.excute(new HibernateCallback() {
|
@SuppressWarnings("unchecked")
|
public Object doInHibernate(Session session) throws HibernateException {
|
String hour = TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH");
|
List<ZhiBoStatisticsDetail> detailList = session
|
.createQuery("from ZhiBoStatisticsDetail d where d.rid=? and d.type=? and hour=?")
|
.setParameter(0, rid).setParameter(1, type).setParameter(2, hour).list();
|
session.getTransaction().begin();
|
if (detailList != null && detailList.size() > 0) {
|
detailList.get(0).setCount(detailList.get(0).getCount() + 1);
|
session.update(detailList.get(0));
|
} else {
|
session.persist(new ZhiBoStatisticsDetail(rid, type, hour, 1));
|
}
|
|
List<ZhiBoStatistics> list = session.createQuery("from ZhiBoStatistics s where s.rid=? and s.type=?")
|
.setParameter(0, rid).setParameter(1, type).list();
|
if (list != null && list.size() > 0) {
|
list.get(0).setCount(list.get(0).getCount() + 1);
|
session.update(list.get(0));
|
} else {
|
session.persist(new ZhiBoStatistics(rid, type, 1));
|
}
|
|
session.flush();
|
session.getTransaction().commit();
|
|
return null;
|
}
|
});
|
|
}
|
|
@SuppressWarnings("unchecked")
|
@Cacheable(value = "zhiboCache", key = "'getTop10List'")
|
public List<ZhiBoContent> getTop10List() {
|
|
return (List<ZhiBoContent>) zhiBoStatisticsDao.excute(new HibernateCallback<List<ZhiBoContent>>() {
|
@SuppressWarnings("rawtypes")
|
public List<ZhiBoContent> doInHibernate(Session session) throws HibernateException {
|
List<ZhiBoContent> list = new ArrayList<ZhiBoContent>();
|
List ljfList = session
|
.createSQLQuery(
|
"SELECT {l.*},{s.*} FROM wk_zhibo_ljf l LEFT JOIN wk_zhibo_statistics s ON s.`type`=1 AND s.`rid`=l.`rid` ORDER BY CAST(l.`count` AS SIGNED ) DESC")
|
.addEntity("l", LJFLiveData.class).addEntity("s", ZhiBoStatistics.class).setFirstResult(0)
|
.setMaxResults(10).list();
|
for (int i = 0; i < ljfList.size(); i++) {
|
LJFLiveData data = (LJFLiveData) ((Object[]) ljfList.get(i))[0];
|
ZhiBoStatistics zs = (ZhiBoStatistics) ((Object[]) ljfList.get(i))[1];
|
data.setWatchCount(zs.getCount());
|
ZhiBoContent zc = ZhiBoUtil.convertToCommonData(data);
|
list.add(zc);
|
}
|
Comparator<ZhiBoContent> cm = new Comparator<ZhiBoContent>() {
|
|
public int compare(ZhiBoContent o1, ZhiBoContent o2) {
|
return o2.getWatchCount() - o1.getWatchCount();
|
}
|
};
|
// 加入多个来源时排序
|
Collections.sort(list, cm);
|
return list;
|
}
|
});
|
|
}
|
|
@SuppressWarnings("unchecked")
|
@Cacheable(value = "zhiboCache", key = "'getNewList'+#page")
|
public List<ZhiBoContent> getNewList(final int page) {
|
|
return (List<ZhiBoContent>) zhiBoStatisticsDao.excute(new HibernateCallback<List<ZhiBoContent>>() {
|
public List<ZhiBoContent> doInHibernate(Session session) throws HibernateException {
|
List<ZhiBoContent> list = new ArrayList<ZhiBoContent>();
|
|
List<LJFLiveData> ljfList = session.createQuery("from LJFLiveData data order by data.wealthrank*300+data.count desc")
|
.setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount).list();
|
for (int i = 0; i < ljfList.size(); i++) {
|
ZhiBoContent zc = ZhiBoUtil.convertToCommonData(ljfList.get(i));
|
list.add(zc);
|
}
|
return list;
|
}
|
});
|
|
}
|
|
@Cacheable(value = "zhiboCache", key = "'getNewListCount'")
|
public long getNewListCount() {
|
|
return (Long) zhiBoStatisticsDao.excute(new HibernateCallback<Long>() {
|
public Long doInHibernate(Session session) throws HibernateException {
|
Object obj = session.createQuery("select count(*) from LJFLiveData data").uniqueResult();
|
return Long.parseLong(obj + "");
|
}
|
});
|
|
}
|
|
public List<LiveType> getAllLiveType(String id) {
|
|
List<LiveType> list = liveTypeDao.list("from LiveType lt left outer join fetch lt.systems s where s.id = ?",new Serializable[]{id});
|
|
return list;
|
}
|
|
public LiveType getLiveType(String id) {
|
LiveType find = liveTypeDao.find(LiveType.class, Long.valueOf(id));
|
return find;
|
}
|
|
}
|