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;
|
|
@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;
|
}
|
|
}
|