admin
2021-02-06 cad915058c3c53bf328a8ae9ca9bc7de099caba7
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
223
224
225
226
227
package com.yeshi.buwan.service.imp;
 
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
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.CollectionDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.domain.Collection;
import com.yeshi.buwan.domain.UserInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.StringUtil;
 
@Service
public class CollectionService {
    @Resource
    private CollectionDao collectionDao;
    @Resource
    private VideoInfoDao videoInfoDao;
 
    public VideoInfoDao getVideoInfoDao() {
        return videoInfoDao;
    }
 
    public void setVideoInfoDao(VideoInfoDao videoInfoDao) {
        this.videoInfoDao = videoInfoDao;
    }
 
    public CollectionDao getCollectionDao() {
        return collectionDao;
    }
 
    public void setCollectionDao(CollectionDao collectionDao) {
        this.collectionDao = collectionDao;
    }
 
    @SuppressWarnings("rawtypes")
    @Cacheable(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-'+#page")
    public List<VideoInfo> getCollectVideo(String uid, String loginUid, int page) {
        List<VideoInfo> videoList;
        List<Collection> list;
 
        videoList = new ArrayList<>();
        list = new ArrayList<>();
        List li = null;
 
        if (!StringUtil.isNullOrEmpty(loginUid)) {
            collectionDao.excute(new HibernateCallback() {
 
                @Override
                public Object doInHibernate(Session session) throws HibernateException {
                    session.createSQLQuery("UPDATE wk_video_collection SET loginuid=? WHERE uid=?")
                            .setParameter(0, loginUid).setParameter(1, uid).executeUpdate();
                    return null;
                }
            });
            li = collectionDao.sqlList((new StringBuilder(
                    "select uid,videoid,thirdtype from wk_video_collection where loginuid=? order by createtime desc limit "))
                            .append(Constant.pageCount * (page - 1)).append(",").append(Constant.pageCount).toString(),
                    new String[] { loginUid });
        } else {
            li = collectionDao.sqlList((new StringBuilder(
                    "select uid,videoid,thirdtype from wk_video_collection where uid=? order by createtime desc limit "))
                            .append(Constant.pageCount * (page - 1)).append(",").append(Constant.pageCount).toString(),
                    new String[] { uid });
        }
        try {
            for (int i = 0; i < li.size(); i++) {
                Collection c = new Collection();
                Object obs[] = (Object[]) li.get(i);
                c.setUser(new UserInfo(obs[0].toString()));
                c.setVideo(new VideoInfo(obs[1].toString()));
                c.setThirdType(obs[2].toString());
                list.add(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
 
        }
 
        for (Iterator<Collection> iterator = list.iterator(); iterator.hasNext();) {
            Collection collection = (Collection) iterator.next();
            videoList.add(videoInfoDao.find(VideoInfo.class, collection.getVideo().getId()));
        }
 
        return videoList;
    }
 
    @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#c.user.id+'-1'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#c.user.id+'-2'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#c.user.id") })
    public void saveCollection(Collection c) {
        try {
            collectionDao.save(c);
        } catch (Exception e) {
 
        }
    }
 
    @Cacheable(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid")
    public long getCollectVideoCount(String uid) {
 
        return collectionDao.getCount("select count(*)  from Collection c where c.user.id=?", new String[] { uid });
    }
 
    @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-1'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-2'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid") })
    @SuppressWarnings("rawtypes")
    public boolean cancelCollect(final String uid, final String videoId, final String thirdType) {
        boolean s = false;
        s = (Boolean) collectionDao.excute(new HibernateCallback<Boolean>() {
            public Boolean doInHibernate(Session session) throws HibernateException {
                boolean s = false;
                try {
                    session.getTransaction().begin();
                    List<String> stList = new ArrayList<>();
                    List list = session
                            .createSQLQuery((new StringBuilder("select id from wk_video_collection c where c.uid="))
                                    .append(uid).append(" and c.videoid=").append(videoId).append(" and c.thirdtype=")
                                    .append(thirdType).toString())
                            .list();
 
                    for (int i = 0; i < list.size(); i++) {
                        BigInteger bi = (BigInteger) list.get(i);
                        if (bi != null)
                            stList.add(bi.toString());
                    }
 
                    if ((stList != null) & (stList.size() > 0)) {
                        Collection c = new Collection();
                        c.setId((new StringBuilder(String.valueOf((String) stList.get(0)))).toString());
                        session.delete(c);
                        s = true;
                    } else {
                        s = false;
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
 
                return s;
            }
        });
 
        return s;
    }
 
    @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-1'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-2'"),
            @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid") })
    public boolean cancelCollect(String uid, String loginUid, String[] videoIds, String[] thirdTypes) {
        String where = "(";
        for (int i = 0; i < videoIds.length; i++)
            where += " (c.videoid=" + videoIds[i] + " and c.thirdtype=" + thirdTypes[i] + ") or";
        if (where.endsWith("or"))
            where = where.substring(0, where.length() - 2);
        where += ")";
        boolean s = false;
 
        try {
            List<String> stList = new ArrayList<>();
            List list;
            if (StringUtil.isNullOrEmpty(loginUid)) {
                list = collectionDao.sqlList((new StringBuilder("select id from wk_video_collection c where c.uid="))
                        .append(uid).append(" and ").append(where).toString());
            } else {
                list = collectionDao
                        .sqlList((new StringBuilder("select id from wk_video_collection c where c.loginuid="))
                                .append(loginUid).append(" and ").append(where).toString());
            }
 
            for (int i = 0; i < list.size(); i++) {
                BigInteger bi = (BigInteger) list.get(i);
                if (bi != null)
                    stList.add(bi.toString());
            }
 
            if ((stList != null) & (stList.size() > 0)) {
                for (String ss : stList) {
                    Collection c = new Collection();
                    c.setId((new StringBuilder(String.valueOf(ss))).toString());
                    collectionDao.delete(c);
                }
                s = true;
            } else {
                s = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return s;
    }
 
    public boolean isCollect(String uid, String videoId, String thirdType) {
        long count = collectionDao
                .getCountSQL((new StringBuilder("select count(*) from wk_video_collection c where c.uid=")).append(uid)
                        .append(" and c.videoid=").append(videoId).append(" and c.thirdtype=").append(thirdType)
                        .toString());
        return count > 0L;
    }
 
    public boolean isCollect(String uid, String videoId) {
        long count = collectionDao
                .getCountSQL((new StringBuilder("select count(*) from wk_video_collection c where c.uid=")).append(uid)
                        .append(" and c.videoid=").append(videoId).toString());
        return count > 0L;
    }
 
}