admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
src/main/java/com/yeshi/buwan/service/imp/CollectionService.java
@@ -25,187 +25,187 @@
@Service
public class CollectionService {
   @Resource
   private CollectionDao collectionDao;
   @Resource
   private VideoInfoDao videoInfoDao;
    @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;
    @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;
        videoList = new ArrayList<>();
        list = new ArrayList<>();
        List li = null;
      if (!StringUtil.isNullOrEmpty(loginUid)) {
         collectionDao.excute(new HibernateCallback() {
        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 {
                @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()));
      }
        for (Iterator<Collection> iterator = list.iterator(); iterator.hasNext(); ) {
            Collection collection = (Collection) iterator.next();
            videoList.add(videoInfoDao.find(VideoInfo.class, collection.getVideo().getId()));
        }
      return videoList;
   }
        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) {
    @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) {
            e.printStackTrace();
        }
    }
      }
   }
    @Cacheable(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid")
    public long getCollectVideoCount(String uid) {
   @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});
    }
      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();
   @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());
                    }
               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();
                }
               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;
         }
      });
        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;
   @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());
            }
      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());
            }
         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;
    }
         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, 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;
   }
    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;
    }
}