admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
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
package com.yeshi.buwan.service.imp.zhibo;
 
import com.yeshi.buwan.dao.zhibo.LiveTypeDao;
import com.yeshi.buwan.dao.zhibo.ZhiBoStatisticsDao;
import com.yeshi.buwan.dao.zhibo.ZhiBoStatisticsDetailDao;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.zhibo.entity.LiveType;
import com.yeshi.buwan.zhibo.entity.ZhiBoStatistics;
import com.yeshi.buwan.zhibo.entity.ZhiBoStatisticsDetail;
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 javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
 
@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;
            }
        });
 
    }
 
 
    @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;
    }
 
}