admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
src/main/java/com/yeshi/buwan/service/imp/AttentionService.java
@@ -28,212 +28,202 @@
@Service
public class AttentionService {
   @Resource
   private AttentionDao attentionDao;
   @Resource
   private VideoDetailUtil videoDetailUtil;
   @Resource
   private LoginUserDao loginUserDao;
    @Resource
    private AttentionDao attentionDao;
    @Resource
    private VideoDetailUtil videoDetailUtil;
    @Resource
    private LoginUserDao loginUserDao;
   public VideoDetailUtil getVideoDetailUtil() {
      return videoDetailUtil;
   }
    @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;
    }
   public void setVideoDetailUtil(VideoDetailUtil videoDetailUtil) {
      this.videoDetailUtil = videoDetailUtil;
   }
    //   @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;
    }
   public AttentionDao getAttentionDao() {
      return attentionDao;
   }
    @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;
    }
   public void setAttentionDao(AttentionDao attentionDao) {
      this.attentionDao = attentionDao;
   }
    @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 {
   @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;
   }
                    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();
//   @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;
   }
                        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();
                        }
   @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 {
                    isS = true;
                } catch (Exception e) {
                    isS = false;
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return isS;
            }
        });
        return isS;
    }
               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();
    // 按照Uid获取关注列表
                  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();
                  }
    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 + ""});
    }
               }
               isS = true;
            } catch (Exception e) {
               isS = false;
               e.printStackTrace();
               if (session.getTransaction().isActive())
                  session.getTransaction().rollback();
            }
            return isS;
         }
      });
      return isS;
   }
    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 + ""});
    }
   // 按照Uid获取关注列表
    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 + ""});
    }
   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 + "" });
   }
    @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 {
   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 + "" });
   }
                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();
                }
   @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 {
                return newList;
            }
        });
            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();
            }
        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);
            return newList;
         }
      });
        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())));
            }
        }
      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);
        return list;
    }
      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);
   }
    // 根据VideoId查找是谁关注的
    public List<LoginUser> getUserListByAttentionVideo(String videoid) {
        return loginUserDao.list("select a.loginUser from Attention a where a.videoInfo.id=" + videoid);
    }
}