admin
2021-03-20 ad3ac53da1c3a11a96ae62d790aa61a81b9eab91
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.yeshi.buwan.service.imp;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.AttentionDao;
import com.yeshi.buwan.dao.user.LoginUserDao;
import com.yeshi.buwan.domain.Attention;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.video.VideoDetailUtil;
 
@Service
public class AttentionService {
    @Resource
    private AttentionDao attentionDao;
    @Resource
    private VideoDetailUtil videoDetailUtil;
    @Resource
    private LoginUserDao loginUserDao;
 
    @SuppressWarnings("unchecked")
    @Caching(evict = {
            @CacheEvict(value = "attentionCache", key = "'isAddAttention'+'-'+#attention.loginUser.id+'-'+#attention.videoInfo.id"),
            @CacheEvict(value = "attentionCache", key = "'getAttentionVideoListByLoginUid'+'-'+#attention.loginUser.id+'-1'") })
    public boolean addAttention(final Attention attention) {
        boolean isS = false;
        isS = (Boolean) attentionDao.excute(new HibernateCallback<Boolean>() {
            public Boolean doInHibernate(Session session) throws HibernateException {
                Boolean isS = false;
                try {
                    List<Attention> list = session
                            .createQuery("from Attention a where a.videoInfo.id=? and a.loginUser.id=?")
                            .setParameter(0, attention.getVideoInfo().getId())
                            .setParameter(1, attention.getLoginUser().getId()).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        list.get(0).setShow(true);
                        list.get(0).setCreatetime(System.currentTimeMillis() + "");
                        session.update(list.get(0));
                    } else {
                        session.persist(attention);
                    }
                    session.flush();
                    session.getTransaction().commit();
                    isS = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return isS;
            }
        });
        return isS;
    }
 
//    @Cacheable(value = "attentionCache", key = "'isAddAttention'+'-'+#loginuid+'-'+#videoid")
    public boolean isAddAttention(String loginuid, String videoid) {
        return attentionDao.getCount(
                "select count(a.id) from Attention a where a.videoInfo.id=? and a.loginUser.id=? and a.show=1",
                new Serializable[] { videoid, loginuid }) > 0;
    }
 
    @SuppressWarnings("unchecked")
    @Caching(evict = {
            @CacheEvict(value = "attentionCache", key = "'isAddAttention'+'-'+#attention.loginUser.id+'-'+#videoid"),
            @CacheEvict(value = "attentionCache", key = "'getAttentionVideoListByLoginUid'+'-'+#loginuid+'-1'") })
    public boolean deleteAttention(final long videoid, final long loginuid) {
        boolean isS = false;
        isS = (Boolean) attentionDao.excute(new HibernateCallback<Boolean>() {
            public Boolean doInHibernate(Session session) throws HibernateException {
                boolean isS = false;
                try {
                    List<Attention> list = session
                            .createQuery("from Attention a where a.videoInfo.id=? and a.loginUser.id=?")
                            .setParameter(0, videoid + "").setParameter(1, loginuid + "").list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        list.get(0).setShow(false);
                        session.update(list.get(0));
                    }
                    session.flush();
                    session.getTransaction().commit();
                    isS = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return isS;
            }
        });
        return isS;
    }
 
    @SuppressWarnings("unchecked")
    @Caching(evict = {
            @CacheEvict(value = "attentionCache", key = "'getAttentionVideoListByLoginUid'+'-'+#loginuid+'-1'") })
    public boolean deleteAttentions(final List<Long> videoList, final long loginuid) {
        boolean isS = false;
        isS = (Boolean) attentionDao.excute(new HibernateCallback<Boolean>() {
            public Boolean doInHibernate(Session session) throws HibernateException {
                Boolean isS = false;
                try {
 
                    for (Long videoid : videoList) {
                        List<Attention> list = session
                                .createQuery(
                                        "from Attention a where a.videoInfo.id=? and a.loginUser.id=? and a.show=1")
                                .setParameter(0, videoid + "").setParameter(1, loginuid + "").list();
 
                        if (list != null && list.size() > 0) {
                            session.getTransaction().begin();
                            session.createSQLQuery(
                                    "update wk_video_attention a set a.show=0 where a.id=" + list.get(0).getId())
                                    .executeUpdate();
                            session.flush();
                            session.getTransaction().commit();
                        }
 
                    }
 
                    isS = true;
                } catch (Exception e) {
                    isS = false;
                    e.printStackTrace();
                        session.getTransaction().rollback();
                }
                return isS;
            }
        });
        return isS;
    }
 
    // 按照Uid获取关注列表
 
    public List<Attention> getAttentionListByLoginUid(long loginUid, int pageIndex) {
        return attentionDao.list(
                "from Attention a where a.show=1 and a.videoInfo.show=1 and a.loginUser.id=? order by FROM_UNIXTIME(a.createtime/1000) desc",
                (pageIndex - 1) * Constant.pageCount, Constant.pageCount, new Serializable[] { loginUid + "" });
    }
 
    public List<Attention> getAttentionListByLoginUidOrderbyUpdateTime(long loginUid, int pageIndex) {
        return attentionDao.list(
                "from Attention a where a.show=1 and a.videoInfo.show=1 and a.loginUser.id=? order by FROM_UNIXTIME(a.videoInfo.updatetime/1000) desc",
                (pageIndex - 1) * Constant.pageCount, Constant.pageCount, new Serializable[] { loginUid + "" });
    }
 
    @SuppressWarnings("unchecked")
    // 获取关注视频的动态
    @Cacheable(value = "attentionCache", key = "'getAttentionVideoListByLoginUid'+'-'+#loginUid+'-'+#pageIndex")
    public List<Attention> getAttentionVideoListByLoginUid(final long loginUid, final int pageIndex) {
        final Map<Long, Object> map = new HashMap<Long, Object>();
        final List<Attention> newList = getAttentionListByLoginUidOrderbyUpdateTime(loginUid, pageIndex);
        List<Attention> list = (List<Attention>) attentionDao.excute(new HibernateCallback<List<Attention>>() {
            public List<Attention> doInHibernate(Session session) throws HibernateException {
 
                try {
                    for (Attention at : newList) {
                        List<VideoResource> resourceList = session
                                .createQuery(
                                        "select rv.resource from ResourceVideo rv where rv.video.id=? order by (15- rv.resource.id)")
                                .setParameter(0, at.getVideoInfo().getId()).list();
                        map.put(at.getId(), resourceList.get(0));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                return newList;
            }
        });
 
        for (Attention at : list) {
            if (map.get(at.getId()) != null) {
                VideoResource vr = (VideoResource) map.get(at.getId());
                VideoDetailInfo detail = videoDetailUtil.getLatestVideoDetail(at.getVideoInfo().getId(), vr);
                List<VideoDetailInfo> detailInfos = new ArrayList<>();
                detailInfos.add(detail);
                at.getVideoInfo().setVideoDetailList(detailInfos);
            }
        }
        // Comparator<Attention> cm = new Comparator<Attention>() {
        //
        // public int compare(Attention o1, Attention o2) {// 倒序排列
        // return (int) (Long.parseLong(o2.getVideoInfo().getUpdatetime())
        // - Long.parseLong(o1.getVideoInfo().getUpdatetime()));
        // }
        // };
        // Collections.sort(list, cm);
 
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).getVideoInfo().setAdmin(null);
                list.get(i).getVideoInfo().setIntroduction("");
                list.get(i).getVideoInfo().setUpdatetime(
                        TimeUtil.getCommentTime(Long.parseLong(list.get(i).getVideoInfo().getUpdatetime())));
            }
        }
 
        return list;
    }
 
    // 根据VideoId查找是谁关注的
    public List<LoginUser> getUserListByAttentionVideo(String videoid) {
        return loginUserDao.list("select a.loginUser from Attention a where a.videoInfo.id=" + videoid);
    }
 
}