src/main/java/com/yeshi/buwan/service/imp/CategoryRecommendCacheVideoService.java
@@ -6,6 +6,9 @@
import java.util.Comparator;
import java.util.List;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
import com.yeshi.buwan.util.JuHe.VideoResourceUtil;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
@@ -22,270 +25,235 @@
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideo;
import com.yeshi.buwan.service.imp.recommend.CategoryRecommendVideoService;
import javax.annotation.Resource;
@Service
public class CategoryRecommendCacheVideoService {
   @Autowired
   private CategoryRecommendCacheVideoDao dao;
    @Autowired
    private CategoryRecommendCacheVideoDao dao;
   @Autowired
   private CategoryRecommendVideoService categoryRecommendVideoService;
    @Autowired
    private CategoryRecommendVideoService categoryRecommendVideoService;
   public List<CategoryRecommendCacheVideo> getList(long videotype, String key, int page, int pageSize) {
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
      List<CategoryRecommendCacheVideo> list = null;
    @Resource
    private VideoResourceUtil videoResourceUtil;
      int start = (page - 1) * pageSize;
    public List<CategoryRecommendCacheVideo> getList(long videotype, String key, int page, int pageSize) {
      if (videotype <= 0) { // 全部
         String hql = "";
         if (key != null && !"".equals(key.trim())) {
            hql = "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc";
            list = dao.list(hql, start, pageSize, new Serializable[] { "%" + key + "%" });
         } else {
            hql = "from CategoryRecommendCacheVideo crcv order by crcv.rank desc , crcv.orderby desc";
            list = dao.list(hql, start, pageSize, new Serializable[] {});
         }
      } else {
         if (key != null && !"".equals(key.trim())) {
            // list = dao.sqlList("select * from wk_recommend_category_cache
            // cc left join wk_video_video vv on vv.id=cc.videoid where
            // cc.videotypeid = ? and vv.name like ? order by cc.rank =
            // 0,cc.rank,cc.orderby desc", start, pageSize, params);
            list = dao.list(
                  "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc",
                  start, pageSize, new Serializable[] { videotype, "%" + key + "%" });
         } else {
            list = dao.list(
                  "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? order by crcv.rank desc , crcv.orderby desc",
                  start, pageSize, new Serializable[] { videotype });
         }
      }
        List<CategoryRecommendCacheVideo> list = null;
      // if(list != null && list.size() > 0){
      // Collections.sort(list, new Comparator<CategoryRecommendCacheVideo>()
      // {
      // @Override
      // public int compare(CategoryRecommendCacheVideo o1,
      // CategoryRecommendCacheVideo o2) {
      // int rank1= o1.getRank();
      // int rank2 = o2.getRank();
      // int b1 =o1.getOrderby();
      // int b2=o2.getOrderby();
      // if(rank2 == 0 && rank1 == 0){
      // return b2-b1;
      // }else if(rank2 == 0 && rank1 !=0){
      // return -1;
      // }else if(rank2 != 0 && rank1 == 0){
      // return 1;
      // }else if(rank2 != 0 && rank1 != 0){
      // int i = rank1 - rank2;
      // if(i==0){
      // return b2-b1;
      // }
      // return i;
      // }
      // return 0;
      // }
      // });
      // }
      return list;
        int start = (page - 1) * pageSize;
   }
        if (videotype <= 0) { // 全部
            String hql = "";
            if (key != null && !"".equals(key.trim())) {
                hql = "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc";
                list = dao.list(hql, start, pageSize, new Serializable[]{"%" + key + "%"});
            } else {
                hql = "from CategoryRecommendCacheVideo crcv order by crcv.rank desc , crcv.orderby desc";
                list = dao.list(hql, start, pageSize, new Serializable[]{});
            }
        } else {
            if (key != null && !"".equals(key.trim())) {
                // list = dao.sqlList("select * from wk_recommend_category_cache
                // cc left join wk_video_video vv on vv.id=cc.videoid where
                // cc.videotypeid = ? and vv.name like ? order by cc.rank =
                // 0,cc.rank,cc.orderby desc", start, pageSize, params);
                list = dao.list(
                        "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc",
                        start, pageSize, new Serializable[]{videotype, "%" + key + "%"});
            } else {
                list = dao.list(
                        "from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? order by crcv.rank desc , crcv.orderby desc",
                        start, pageSize, new Serializable[]{videotype});
            }
        }
   public int getCount(long videotype, String key) {
      if (videotype <= 0) {
         if (key != null && !"".equals(key.trim())) {
            return (int) dao.getCount(
                  "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? ",
                  new Serializable[] { "%" + key + "%" });
         } else {
            return (int) dao.getCount("select count(*) from CategoryRecommendCacheVideo crcv",
                  new Serializable[] {});
         }
      } else {
         if (key != null && !"".equals(key.trim())) {
            return (int) dao.getCount(
                  "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? ",
                  new Serializable[] { videotype, "%" + key + "%" });
         }
         return (int) dao.getCount(
               "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? ",
               new Serializable[] { videotype });
      }
        // if(list != null && list.size() > 0){
        // Collections.sort(list, new Comparator<CategoryRecommendCacheVideo>()
        // {
        // @Override
        // public int compare(CategoryRecommendCacheVideo o1,
        // CategoryRecommendCacheVideo o2) {
        // int rank1= o1.getRank();
        // int rank2 = o2.getRank();
        // int b1 =o1.getOrderby();
        // int b2=o2.getOrderby();
        // if(rank2 == 0 && rank1 == 0){
        // return b2-b1;
        // }else if(rank2 == 0 && rank1 !=0){
        // return -1;
        // }else if(rank2 != 0 && rank1 == 0){
        // return 1;
        // }else if(rank2 != 0 && rank1 != 0){
        // int i = rank1 - rank2;
        // if(i==0){
        // return b2-b1;
        // }
        // return i;
        // }
        // return 0;
        // }
        // });
        // }
        return list;
   }
    }
   public CategoryRecommendCacheVideo getTop(long id) {
      return dao.find(CategoryRecommendCacheVideo.class, id + "");
   }
    public int getCount(long videotype, String key) {
        if (videotype <= 0) {
            if (key != null && !"".equals(key.trim())) {
                return (int) dao.getCount(
                        "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? ",
                        new Serializable[]{"%" + key + "%"});
            } else {
                return (int) dao.getCount("select count(*) from CategoryRecommendCacheVideo crcv",
                        new Serializable[]{});
            }
        } else {
            if (key != null && !"".equals(key.trim())) {
                return (int) dao.getCount(
                        "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? ",
                        new Serializable[]{videotype, "%" + key + "%"});
            }
            return (int) dao.getCount(
                    "select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? ",
                    new Serializable[]{videotype});
        }
   @Transactional
   @CacheEvict(value = "topCache", allEntries = true)
   public void update(CategoryRecommendCacheVideo crcv) {
      dao.update(crcv);
   }
    }
   @Transactional
   public void save(CategoryRecommendCacheVideo crcv) {
      dao.save(crcv);
   }
    public CategoryRecommendCacheVideo getTop(long id) {
        return dao.find(CategoryRecommendCacheVideo.class, id + "");
    }
   @Transactional
   @CacheEvict(value = "topCache", allEntries = true)
   public void delete(long id) {
      CategoryRecommendCacheVideo crcv = new CategoryRecommendCacheVideo();
      crcv.setId(id + "");
      dao.delete(crcv);
   }
    @Transactional
    @CacheEvict(value = "topCache", allEntries = true)
    public void update(CategoryRecommendCacheVideo crcv) {
        dao.update(crcv);
    }
   public CategoryRecommendCacheVideo getTop(long vid, long tid) {
      List<CategoryRecommendCacheVideo> list = dao.list(
            "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? and crcv.videoType.id = ?",
            new Serializable[] { vid + "", tid });
      if (list.size() > 0) {
         return list.get(0);
      }
      return null;
   }
    @Transactional
    public void save(CategoryRecommendCacheVideo crcv) {
        dao.save(crcv);
    }
   // public List<CategoryRecommendCacheVideo> getVideoListByRank(final int
   // type) {
   //
   // return (List<CategoryRecommendCacheVideo>) dao.excute(new
   // HibernateCallback<List<VideoInfo>>() {
   // @Override
   // public List<VideoInfo> doInHibernate(Session session)
   // throws HibernateException {
   // SQLQuery query = session.createSQLQuery("SELECT cc.* FROM
   // wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid
   // = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id WHERE
   // rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND
   // cc.videotypeid=? AND cc.videoid = vv.id AND cc.rank > 0 GROUP BY cc.id
   // ORDER BY cc.orderby desc");
   // query.addEntity(CategoryRecommendCacheVideo.class);
   // query.setParameter(0, type);
   // return query.list();
   // }
   // });
   // }
    @Transactional
    @CacheEvict(value = "topCache", allEntries = true)
    public void delete(long id) {
        CategoryRecommendCacheVideo crcv = new CategoryRecommendCacheVideo();
        crcv.setId(id + "");
        dao.delete(crcv);
    }
   @SuppressWarnings("unchecked")
   @Cacheable(value = "topCache", key = "'getVideoListByRank'+'-'+#videotype+'-'+#detailSystem")
   public List<VideoInfo> getVideoListByRank(final int videotype, final String detailSystem) {
      final int count = categoryRecommendVideoService.getRankVideoNumber(videotype);
      return (List<VideoInfo>) dao.excute(new HibernateCallback<List<VideoInfo>>() {
         @Override
         public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
            SQLQuery query = session.createSQLQuery("SELECT cc.rank,"
                  + "vv.id,vv.picture,vv.name,vv.vpicture,vv.hpicture,vv.latest_vpicture,vv.latest_hpicture,vv.watchcount,vv.commentcount,cc.orderby,vv.tag "
                  + "FROM wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id LEFT JOIN wk_video_banquan_video bv on cc.videoid = bv.videoid WHERE bv.id is null and (bv.detailsystemid=? OR bv.detailsystemid IS null ) and rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid = vv.id AND cc.rank <= ? AND cc.rank >= 0  GROUP BY cc.id ORDER BY cc.rank !=0 DESC ,cc.rank desc,cc.orderby desc");
            query.setParameter(0, detailSystem);
            query.setParameter(1, videotype);
            query.setParameter(2, count);
            query.setFirstResult(0);
            query.setMaxResults(count);
            List<Object[]> qdata = query.list();
            List<VideoInfoRank> rks = new ArrayList<VideoInfoRank>();
            List<VideoInfo> vs = new ArrayList<VideoInfo>();
            VideoInfoRank vr;
            for (Object[] objs : qdata) {
               vr = new VideoInfoRank();
               vr.setRank(Integer.parseInt(objs[0] + ""));
               vr.setId(objs[1] + "");
               vr.setPicture(objs[2] + "");
               vr.setName(objs[3] + "");
               vr.setVpicture(objs[4] + "");
               vr.setHpicture(objs[5] + "");
               vr.setLatestVpicture(objs[6] + "");
               vr.setLatestHpicture(objs[7] + "");
               vr.setWatchCount(objs[8] + "");
               vr.setCommentCount(Integer.parseInt(objs[9] + ""));
               vr.setOrderBy(Integer.parseInt(objs[10] + ""));
               vr.setTag(objs[11] + "");
               if (vr.getRank() > 0) {
                  rks.add(vr);
               } else
                  vs.add(vr);
            }
    public CategoryRecommendCacheVideo getTop(long vid, long tid) {
        List<CategoryRecommendCacheVideo> list = dao.list(
                "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? and crcv.videoType.id = ?",
                new Serializable[]{vid + "", tid});
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
            Comparator<VideoInfoRank> c = new Comparator<VideoInfoRank>() {
               @Override
               public int compare(VideoInfoRank arg0, VideoInfoRank arg1) {
                  return arg0.getRank() - arg1.getRank();
               }
            };
    @SuppressWarnings("unchecked")
    @Cacheable(value = "topCache", key = "'getVideoListByRank'+'-'+#videotype+'-'+#detailSystem+'-'+#platform+'-'+#version")
    public List<VideoInfo> getVideoListByRank(final int videotype, final String detailSystem, String platform, Integer version) {
            Collections.sort(rks, c);
        List<Long> resourceList = videoResourceUtil.getAvailableResourceIds(new DetailSystem(detailSystem), version);
            // 找出rank=0的视频
        final int count = categoryRecommendVideoService.getRankVideoNumber(videotype);
        List<VideoInfo> videoInfoList = (List<VideoInfo>) dao.excute(new HibernateCallback<List<VideoInfo>>() {
            @Override
            public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
                SQLQuery query = session.createSQLQuery("SELECT cc.rank,"
                        + "vv.id,vv.picture,vv.name,vv.vpicture,vv.hpicture,vv.latest_vpicture,vv.latest_hpicture,vv.watchcount,vv.commentcount,cc.orderby,vv.tag "
                        + "FROM wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id LEFT JOIN wk_video_banquan_video bv on cc.videoid = bv.videoid WHERE bv.id is null and (bv.detailsystemid=? OR bv.detailsystemid IS null ) and rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid = vv.id AND cc.rank <= ? AND cc.rank >= 0  GROUP BY cc.id ORDER BY cc.rank !=0 DESC ,cc.rank desc,cc.orderby desc");
                query.setParameter(0, detailSystem);
                query.setParameter(1, videotype);
                query.setParameter(2, count);
                query.setFirstResult(0);
                query.setMaxResults(count);
                List<Object[]> qdata = query.list();
                List<VideoInfoRank> rks = new ArrayList<>();
                List<VideoInfo> vs = new ArrayList<>();
                VideoInfoRank vr;
                for (Object[] objs : qdata) {
                    vr = new VideoInfoRank();
                    vr.setRank(Integer.parseInt(objs[0] + ""));
                    vr.setId(objs[1] + "");
                    vr.setPicture(objs[2] + "");
                    vr.setName(objs[3] + "");
                    vr.setVpicture(objs[4] + "");
                    vr.setHpicture(objs[5] + "");
                    vr.setLatestVpicture(objs[6] + "");
                    vr.setLatestHpicture(objs[7] + "");
                    vr.setWatchCount(objs[8] != null ? objs[8] + "" : "0");
                    vr.setCommentCount(objs[9] != null ? Integer.parseInt(objs[9] + "") : 0);
                    vr.setOrderBy(Integer.parseInt(objs[10] + ""));
                    vr.setTag(objs[11] + "");
                    vr.setVideocount(0);
                    if (vr.getRank() > 0) {
                        rks.add(vr);
                    } else
                        vs.add(vr);
                }
            for (int i = 1; i <= count; i++) {
               boolean exist = false;
               for (VideoInfoRank vir : rks)
                  if (vir.getRank() == i) {
                     exist = true;
                     break;
                  }
               // 需要加入rks
               if (!exist) {
                  if (vs.size() > 0) {
                     VideoInfoRank rr = (VideoInfoRank) vs.get(0);
                     rr.setRank(i);
                     rks.add(rr);
                     vs.remove(0);
                  }
               }
            }
                Comparator<VideoInfoRank> c = new Comparator<VideoInfoRank>() {
            vs.clear();
                    @Override
                    public int compare(VideoInfoRank arg0, VideoInfoRank arg1) {
                        return arg0.getRank() - arg1.getRank();
                    }
                };
            Collections.sort(rks, c);
                Collections.sort(rks, c);
            for (VideoInfoRank videoInfoRank : rks)
               vs.add(videoInfoRank);
                // 找出rank=0的视频
            // for (VideoInfoRank videoInfoRank : rks) {
            // int rank = videoInfoRank.getRank();
            // if (rank <= vs.size()) {
            // vs.remove((VideoInfo) videoInfoRank);
            // vs.add(rank - 1, videoInfoRank);
            // }
            // }
                for (int i = 1; i <= count; i++) {
                    boolean exist = false;
                    for (VideoInfoRank vir : rks)
                        if (vir.getRank() == i) {
                            exist = true;
                            break;
                        }
                    // 需要加入rks
                    if (!exist) {
                        if (vs.size() > 0) {
                            VideoInfoRank rr = (VideoInfoRank) vs.get(0);
                            rr.setRank(i);
                            rks.add(rr);
                            vs.remove(0);
                        }
                    }
                }
            // SQLQuery query2 = session.createSQLQuery("SELECT cc.* FROM
            // wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON
            // cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON
            // rv.videoid=vv.id WHERE rv.resourceid <> 16 AND vv.id IS NOT
            // NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid =
            // vv.id AND cc.rank > 0 GROUP BY cc.id ORDER BY cc.orderby
            // desc");
            // query2.addEntity(CategoryRecommendCacheVideo.class);
            // query2.setParameter(0, videotype);
            // query2.setFirstResult(0);
            // query2.setMaxResults(count);
            // List<CategoryRecommendCacheVideo> cs = query2.list();
            // for (CategoryRecommendCacheVideo crcv : cs) {
            // int rank = crcv.getRank();
            // if(vs.size() > 0 && rank > 0 && rank <= vs.size() ){
            // vs.remove(crcv.getVideoInfo());
            // vs.add(rank-1, crcv.getVideoInfo());
            // }
            // }
            // vs = vs.subList(0, vs.size()>count?count:vs.size());
            return vs;
         }
      });
   }
                vs.clear();
   public CategoryRecommendCacheVideo getByVid(String vid) {
      List<CategoryRecommendCacheVideo> list = dao
            .list("from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? ", new Serializable[] { vid });
      if (list.size() > 0) {
         return list.get(0);
      }
      return null;
   }
                Collections.sort(rks, c);
                for (VideoInfoRank videoInfoRank : rks)
                    vs.add(videoInfoRank);
                return vs;
            }
        });
        return videoInfoExtraService.batchExtra(videoInfoList, resourceList);
    }
    public CategoryRecommendCacheVideo getByVid(String vid) {
        List<CategoryRecommendCacheVideo> list = dao
                .list("from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? ", new Serializable[]{vid});
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}