admin
2025-02-20 f537abe9f3646c739beaf15076246a2f71a347e9
src/test/java/com/hxh/spring/test/ClearVideo.java
@@ -2,141 +2,152 @@
import java.util.List;
import com.yeshi.buwan.job.video.VideoUpdateJob;
import com.yeshi.buwan.util.TimeUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.orm.hibernate4.HibernateCallback;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.domain.ResourceVideo;
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum;
import com.yeshi.buwan.iqiyi.entity.IqiyiVideoInfo;
import com.yeshi.buwan.iqiyi.entity.VideoIqiyi;
import com.yeshi.buwan.videos.iqiyi.entity.IqiyiAlbum;
import com.yeshi.buwan.videos.iqiyi.entity.IqiyiVideoInfo;
import com.yeshi.buwan.videos.iqiyi.entity.VideoIqiyi;
import com.yeshi.buwan.service.imp.ClearService;
import com.yeshi.buwan.util.BeanUtil;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
/**
 * 清除时间比较久远的小视频数据
 *
 * @author Administrator
 *
 * @author Administrator
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
@WebAppConfiguration
public class ClearVideo {
   public static void main(String[] args) {
      // ClearService clearService = BeanUtil.getBean(ClearService.class);
      // for (int i = 0; i < 1000; i++) {
      // clearService.clearDependVideo();
      // }
      ClearService clearService = BeanUtil.getBean(ClearService.class);
    @Resource
    public ClearService clearService;
      for (int i = 0; i < 300; i++) {
         long[] typeIds = new long[] { 299,242,216,249,290,219 };
         for (long id : typeIds) {
            clearService.clearVideos(new long[] { id }, "2019-01-20");
         }
      }
      // }
   }
    @Resource
    private VideoUpdateJob videoUpdateJob;
   /**
    * 清除掉优酷,搜狐,PPTV视频
    */
   public static void clearYouKuAndSouHuAndPPTVVideo(final int p) {
      VideoInfoDao videoInfoDao = BeanUtil.getBean(VideoInfoDao.class);
      videoInfoDao.excute(new HibernateCallback<Object>() {
    @Test
    public void main() throws Exception {
        videoUpdateJob.deleteOutOfDateVideo(null);
    }
         @Override
         public Object doInHibernate(Session session) throws HibernateException {
            List videoIdList = session
                  .createSQLQuery(
                        "SELECT  DISTINCT(rv.`videoid`) FROM `wk_resource_video` rv WHERE rv.`resourceid`=15 OR rv.`resourceid`=16 OR rv.`resourceid`= 20 OR rv.`resourceid`=14")
                  .setFirstResult((p - 1) * 20000).setMaxResults(20000).list();
    /**
     * 清除掉优酷,搜狐,PPTV视频
     */
    public static void clearYouKuAndSouHuAndPPTVVideo(final int p) {
        VideoInfoDao videoInfoDao = BeanUtil.getBean(VideoInfoDao.class);
        videoInfoDao.excute(new HibernateCallback<Object>() {
            for (int i = 0; i < videoIdList.size(); i++) {
               session.getTransaction().begin();
               String videoId = videoIdList.get(i).toString();
               List<ResourceVideo> resourceVideoList = session
                     .createQuery("from ResourceVideo rv where rv.video.id=" + videoId).list();
               for (int n = 0; n < resourceVideoList.size(); n++) {
                  long resourceId = Long.parseLong(resourceVideoList.get(n).getResource().getId());
                  if (resourceId == 14L || resourceId == 15L || resourceId == 16L || resourceId == 20L) {
                     session.createSQLQuery(
                           "delete from wk_resource_video where id=" + resourceVideoList.get(n).getId())
                           .executeUpdate();
                     resourceVideoList.remove(n);
                     n--;
                  }
               }
               if (resourceVideoList != null && resourceVideoList.size() <= 0) {
                  session.createSQLQuery("delete from wk_video_video where id=" + videoId).executeUpdate();
               }
               session.flush();
               session.getTransaction().commit();
            }
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                List videoIdList = session
                        .createSQLQuery(
                                "SELECT  DISTINCT(rv.`videoid`) FROM `wk_resource_video` rv WHERE rv.`resourceid`=15 OR rv.`resourceid`=16 OR rv.`resourceid`= 20 OR rv.`resourceid`=14")
                        .setFirstResult((p - 1) * 20000).setMaxResults(20000).list();
            return null;
         }
      });
                for (int i = 0; i < videoIdList.size(); i++) {
                    session.getTransaction().begin();
                    String videoId = videoIdList.get(i).toString();
                    List<ResourceVideo> resourceVideoList = session
                            .createQuery("from ResourceVideo rv where rv.video.id=" + videoId).list();
                    for (int n = 0; n < resourceVideoList.size(); n++) {
                        long resourceId = Long.parseLong(resourceVideoList.get(n).getResource().getId());
                        if (resourceId == 14L || resourceId == 15L || resourceId == 16L || resourceId == 20L) {
                            session.createSQLQuery(
                                    "delete from wk_resource_video where id=" + resourceVideoList.get(n).getId())
                                    .executeUpdate();
                            resourceVideoList.remove(n);
                            n--;
                        }
                    }
                    if (resourceVideoList != null && resourceVideoList.size() <= 0) {
                        session.createSQLQuery("delete from wk_video_video where id=" + videoId).executeUpdate();
                    }
                    session.flush();
                    session.getTransaction().commit();
                }
   }
                return null;
            }
        });
   /**
    * 清除爱奇艺中不能播放的内容
    */
   public static void clearCantPlayIqiyiVideo() {
      VideoInfoDao videoInfoDao = BeanUtil.getBean(VideoInfoDao.class);
      videoInfoDao.excute(new HibernateCallback<Object>() {
    }
         @SuppressWarnings("unchecked")
         @Override
         public Object doInHibernate(Session session) throws HibernateException {
    /**
     * 清除爱奇艺中不能播放的内容
     */
    public static void clearCantPlayIqiyiVideo() {
        VideoInfoDao videoInfoDao = BeanUtil.getBean(VideoInfoDao.class);
        videoInfoDao.excute(new HibernateCallback<Object>() {
            List<IqiyiVideoInfo> list = session
                  .createSQLQuery(
                        "SELECT tv.* FROM `wk_iqiyi_album_tvid` tv   WHERE tv.`playcontrols` NOT LIKE '%15%'")
                  .addEntity(IqiyiVideoInfo.class).setMaxResults(1000).list();
            for (IqiyiVideoInfo iqiyiVideoInfo : list) {
               try {
                  session.getTransaction().begin();
                  session.createSQLQuery("delete from wk_iqiyi_album_tvid where id=" + iqiyiVideoInfo.getId())
                        .executeUpdate();
                  List<IqiyiAlbum> iaList = session
                        .createQuery("from IqiyiAlbum ia where ia.albumId=" + iqiyiVideoInfo.getAlbumId())
                        .list();
                  if (iaList != null)
                     for (IqiyiAlbum ia : iaList) {
                        List<VideoIqiyi> vlist = session
                              .createQuery("from VideoIqiyi vi where vi.album.id=" + ia.getId()).list();
                        for (VideoIqiyi vi : vlist) {
                           session.createSQLQuery("delete from wk_video_iqiyi where id=" + vi.getId())
                                 .executeUpdate();
                           List<ResourceVideo> rvList = session
                                 .createQuery(
                                       "from ResourceVideo rv where rv.video.id=" + vi.getVideo().getId())
                                 .list();
                           for (ResourceVideo rv : rvList) {
                              session.delete(rv);
                           }
                           if (rvList != null && rvList.size() <= 1
                                 && rvList.get(0).getResource().getId().equalsIgnoreCase("13")) {
                              session.delete(rvList.get(0).getVideo());
                           }
                        }
            @SuppressWarnings("unchecked")
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                        session.createSQLQuery("delete from wk_iqiyi_album where id=" + ia.getId())
                              .executeUpdate();
                     }
                  session.flush();
                  session.getTransaction().commit();
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
                List<IqiyiVideoInfo> list = session
                        .createSQLQuery(
                                "SELECT tv.* FROM `wk_iqiyi_album_tvid` tv   WHERE tv.`playcontrols` NOT LIKE '%15%'")
                        .addEntity(IqiyiVideoInfo.class).setMaxResults(1000).list();
                for (IqiyiVideoInfo iqiyiVideoInfo : list) {
                    try {
                        session.getTransaction().begin();
                        session.createSQLQuery("delete from wk_iqiyi_album_tvid where id=" + iqiyiVideoInfo.getId())
                                .executeUpdate();
                        List<IqiyiAlbum> iaList = session
                                .createQuery("from IqiyiAlbum ia where ia.albumId=" + iqiyiVideoInfo.getAlbumId())
                                .list();
                        if (iaList != null)
                            for (IqiyiAlbum ia : iaList) {
                                List<VideoIqiyi> vlist = session
                                        .createQuery("from VideoIqiyi vi where vi.album.id=" + ia.getId()).list();
                                for (VideoIqiyi vi : vlist) {
                                    session.createSQLQuery("delete from wk_video_iqiyi where id=" + vi.getId())
                                            .executeUpdate();
                                    List<ResourceVideo> rvList = session
                                            .createQuery(
                                                    "from ResourceVideo rv where rv.video.id=" + vi.getVideo().getId())
                                            .list();
                                    for (ResourceVideo rv : rvList) {
                                        session.delete(rv);
                                    }
                                    if (rvList != null && rvList.size() <= 1
                                            && rvList.get(0).getResource().getId().equalsIgnoreCase("13")) {
                                        session.delete(rvList.get(0).getVideo());
                                    }
                                }
            return null;
         }
      });
                                session.createSQLQuery("delete from wk_iqiyi_album where id=" + ia.getId())
                                        .executeUpdate();
                            }
                        session.flush();
                        session.getTransaction().commit();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
   }
                return null;
            }
        });
    }
    @Test
    public void clearDepend() {
        clearService.clearDependVideo();
    }
}