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
package com.newvideo.service.imp.zhibo;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.newvideo.dao.zhibo.LJFLiveDataDao;
import com.newvideo.zhibo.entity.ZhiBoStatistics;
import com.newvideo.zhibo.ljf.LiuJianFangApi;
import com.newvideo.zhibo.ljf.entity.LJFLiveData;
 
//六间房直播
@Service
public class LJFLiveService {
    @Resource
    private LJFLiveDataDao ljfLiveDataDao;
 
    public void addLJFLiveData(LJFLiveData data) {
        ljfLiveDataDao.create(data);
    }
 
    // 删除数据 同时增加数据
 
    @SuppressWarnings("rawtypes")
    public void updateData(final List<LJFLiveData> list) {
        ljfLiveDataDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                session.getTransaction().begin();
                session.createQuery("delete from LJFLiveData").executeUpdate();
                Iterator<LJFLiveData> iterator = list.iterator();
                Set<String> ridSet = new HashSet<String>();
                while (iterator.hasNext()) {
                    LJFLiveData liveData = iterator.next();
                    String rid = liveData.getRid();
                    if(ridSet.contains(rid)){
                        iterator.remove();
                    }else{
                        ridSet.add(rid);
                    }
                }
                for (LJFLiveData data : list) {
                    session.persist(data);
                    Object count = session
                            .createQuery("select count(*) from ZhiBoStatistics s where s.rid=? and s.type=?")
                            .setParameter(0, data.getRid()).setParameter(1, 1).uniqueResult();
                    if (Integer.parseInt(count + "") <= 0)
                        session.persist(new ZhiBoStatistics(data.getRid(), 1, 0));
                }
                session.flush();
                session.getTransaction().commit();
                return null;
            }
        });
    }
 
    public void update() {
        List<LJFLiveData> list = new ArrayList<LJFLiveData>();
        for (int p = 1; p < 100; p++) {
            List<LJFLiveData> li = (LiuJianFangApi.liveList("", p));
            if (li != null && li.size() > 0)
                list.addAll(li);
        }
        updateData(list);
    }
 
}