admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
    }
 
}