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 long getAttentionCountByLoginUid(long loginUid) {
|
return attentionDao.getCount(
|
"select count(*) from Attention a where a.show=1 and a.videoInfo.show=1 and a.loginUser.id=?",
|
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);
|
}
|
|
}
|