admin
2021-07-30 19533a17aa55fafc70d0a385928e785cb50e1ebc
src/main/java/com/yeshi/buwan/service/imp/recommend/CategoryRecommendVideoService.java
@@ -7,7 +7,6 @@
import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
@@ -16,9 +15,7 @@
import com.yeshi.buwan.dao.recommend.CategoryRecommendCacheVideoNumberDao;
import com.yeshi.buwan.dao.recommend.CategoryRecommendVideoDao;
import com.yeshi.buwan.dao.recommend.SuperCategoryRecommendVideoDao;
import com.yeshi.buwan.domain.DetailSystem;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideo;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideoNumber;
import com.yeshi.buwan.domain.recommend.CategoryRecommendVideo;
import com.yeshi.buwan.domain.recommend.SuperCategoryRecommendVideo;
@@ -28,237 +25,231 @@
@Service
public class CategoryRecommendVideoService {
   @Resource
   private CategoryRecommendVideoDao categoryRecommendVideoDao;
   @Resource
   private SuperCategoryRecommendVideoDao superCategoryRecommendVideoDao;
   @Resource
   private CategoryRecommendCacheVideoNumberDao categoryRecommendCacheVideoNumberDao;
    @Resource
    private CategoryRecommendVideoDao categoryRecommendVideoDao;
    @Resource
    private SuperCategoryRecommendVideoDao superCategoryRecommendVideoDao;
    @Resource
    private CategoryRecommendCacheVideoNumberDao categoryRecommendCacheVideoNumberDao;
   // 获取大区推荐的Banner
   @Cacheable(value = "classCache",key="'getCategoryRecommendVideoBannerList'+'-'+#detailSystemId+'-'+#videoTypeId")
   public List<CategoryRecommendVideo> getCategoryRecommendVideoBannerList(String detailSystemId, long videoTypeId) {
      return categoryRecommendVideoDao.list(
            "select sc.categoryRecommendVideo from SuperCategoryRecommendVideo sc where  sc.categoryRecommendVideo is not null and sc.categoryRecommendVideo.videoInfo is not null and sc.detailSystem.id=? and sc.categoryRecommendVideo.videoType.id=?  order by sc.categoryRecommendVideo.orderby desc",
            new Serializable[] { detailSystemId, videoTypeId });
   }
    // 获取大区推荐的Banner
    @Cacheable(value = "classCache", key = "'getCategoryRecommendVideoBannerList'+'-'+#detailSystemId+'-'+#videoTypeId")
    public List<CategoryRecommendVideo> getCategoryRecommendVideoBannerList(String detailSystemId, long videoTypeId) {
        return categoryRecommendVideoDao.list(
                "select sc.categoryRecommendVideo from SuperCategoryRecommendVideo sc where  sc.categoryRecommendVideo is not null and sc.categoryRecommendVideo.videoInfo is not null and sc.detailSystem.id=? and sc.categoryRecommendVideo.videoType.id=?  order by sc.categoryRecommendVideo.orderby desc",
                new Serializable[]{detailSystemId, videoTypeId});
    }
   public void addSuperCategoryRecommendVideoBanner(SuperCategoryRecommendVideo sc) {
    public void addSuperCategoryRecommendVideoBanner(SuperCategoryRecommendVideo sc) {
      superCategoryRecommendVideoDao.create(sc);
        superCategoryRecommendVideoDao.create(sc);
   }
    }
   public void deleteSuperCategoryRecommendVideo(String id) {
    public void deleteSuperCategoryRecommendVideo(String id) {
      superCategoryRecommendVideoDao.delete(new SuperCategoryRecommendVideo(id));
        superCategoryRecommendVideoDao.delete(new SuperCategoryRecommendVideo(id));
   }
    }
   public void addCategoryRecommendVideo(CategoryRecommendVideo crv) {
      categoryRecommendVideoDao.create(crv);
   }
    public void addCategoryRecommendVideo(CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.create(crv);
    }
   public CategoryRecommendVideo getCategoryRecommendVideoById(String id) {
      return categoryRecommendVideoDao.find(CategoryRecommendVideo.class, id);
   }
    public CategoryRecommendVideo getCategoryRecommendVideoById(String id) {
        return categoryRecommendVideoDao.find(CategoryRecommendVideo.class, id);
    }
   public void updateCategoryRecommendVideo(CategoryRecommendVideo crv) {
      categoryRecommendVideoDao.update(crv);
   }
    public void updateCategoryRecommendVideo(CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.update(crv);
    }
   @SuppressWarnings("rawtypes")
   public void deleteCategoryRecommendVideo(final CategoryRecommendVideo crv) {
      categoryRecommendVideoDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
               session.getTransaction().begin();
               session.createQuery("delete from SuperCategoryRecommendVideo s where s.categoryRecommendVideo.id=?")
                     .setParameter(0, crv.getId()).executeUpdate();
               session.delete(crv);
               session.flush();
               session.getTransaction().commit();
            } catch (Exception e) {
               e.printStackTrace();
               session.getTransaction().rollback();
            }
            return null;
         }
      });
    @SuppressWarnings("rawtypes")
    public void deleteCategoryRecommendVideo(final CategoryRecommendVideo crv) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    session.createQuery("delete from SuperCategoryRecommendVideo s where s.categoryRecommendVideo.id=?")
                            .setParameter(0, crv.getId()).executeUpdate();
                    session.delete(crv);
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
   }
    }
   // 分区榜首数量
   @Cacheable(value = "classCache",key="'getRankVideoNumber'+'-'+#videotype")
   public int getRankVideoNumber(int videotype) {
      List<CategoryRecommendCacheVideoNumber> list = categoryRecommendCacheVideoNumberDao
            .list("from CategoryRecommendCacheVideoNumber cr where  cr.videoType.id=" + videotype);
      if (list != null && list.size() > 0)
         return list.get(0).getNumber();
      else
         return 32;
   }
    // 分区榜首数量
    @Cacheable(value = "classCache", key = "'getRankVideoNumber'+'-'+#videotype")
    public int getRankVideoNumber(int videotype) {
        List<CategoryRecommendCacheVideoNumber> list = categoryRecommendCacheVideoNumberDao
                .list("from CategoryRecommendCacheVideoNumber cr where  cr.videoType.id=" + videotype);
        if (list != null && list.size() > 0)
            return list.get(0).getNumber();
        else
            return 32;
    }
   // 后台操作
   @SuppressWarnings({ "unchecked" })
   public List<CategoryRecommendVideoAdmin> getCategoryRecommendVideoAdmin(final String key, final int videoTypeId,
         final int detailSystem, final int page) {
      return (List<CategoryRecommendVideoAdmin>) categoryRecommendVideoDao
            .excute(new HibernateCallback<List<CategoryRecommendVideoAdmin>>() {
               public List<CategoryRecommendVideoAdmin> doInHibernate(Session session) throws HibernateException {
                  List<CategoryRecommendVideoAdmin> zhiBoClassList = new ArrayList<>();
                  try {
                     List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem").list();
                     String sql = "";
                     if (detailSystem > 0)
                        // on sh.categoryRecommendVideo.videoInfo= vi.id
                        sql = "select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh  where sh.categoryRecommendVideo.videoType.id="
                              + videoTypeId + " and  sh.detailSystem.id=" + detailSystem + " and sh.categoryRecommendVideo.videoInfo.name like '%"+key+"%'"
                              + " order by  sh.categoryRecommendVideo.createtime desc";
                     else
    // 后台操作
    @SuppressWarnings({"unchecked"})
    public List<CategoryRecommendVideoAdmin> getCategoryRecommendVideoAdmin(final String key, final int videoTypeId, final String systemId,
                                                                            final int detailSystem, final int page) {
        return (List<CategoryRecommendVideoAdmin>) categoryRecommendVideoDao
                .excute(new HibernateCallback<List<CategoryRecommendVideoAdmin>>() {
                    public List<CategoryRecommendVideoAdmin> doInHibernate(Session session) throws HibernateException {
                        List<CategoryRecommendVideoAdmin> zhiBoClassList = new ArrayList<>();
                        try {
                            List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem ds where ds.system.id=" + systemId).list();
                            String sql = "";
                            if (detailSystem > 0)
                                // on sh.categoryRecommendVideo.videoInfo= vi.id
                                sql = "select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh  where sh.categoryRecommendVideo.videoType.id="
                                        + videoTypeId + " and  sh.detailSystem.id=" + detailSystem + " and sh.categoryRecommendVideo.videoInfo.name like '%" + key + "%'"
                                        + " order by  sh.categoryRecommendVideo.createtime desc";
                            else
//                        sql = "from CategoryRecommendVideo zb where zb.videoType.id=" + videoTypeId + " and sh.categoryRecommendVideo.videoInfo.name like '%"+key+"%'"
//                              + " order by zb.createtime desc";
                        sql = "from CategoryRecommendVideo zb   where zb.videoType.id=" + videoTypeId + " and zb.videoInfo.name like '%"+key+"%'"
                              + " order by zb.createtime desc";
                                sql = "from CategoryRecommendVideo zb   where zb.videoType.id=" + videoTypeId + " and zb.system.id=" + systemId + " and zb.videoInfo.name like '%" + key + "%'"
                                        + " order by zb.createtime desc";
                     List<CategoryRecommendVideo> list = session.createQuery(sql)
                           .setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount)
                           .list();
                     for (CategoryRecommendVideo vb : list) {
                        List<DetailSystem> detailSystemS = session
                              .createQuery(
                                    "select vb.detailSystem from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=?")
                              .setParameter(0, vb.getId()).list();
                            List<CategoryRecommendVideo> list = session.createQuery(sql)
                                    .setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount)
                                    .list();
                            for (CategoryRecommendVideo vb : list) {
                                List<DetailSystem> detailSystemS = session
                                        .createQuery(
                                                "select vb.detailSystem from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=?")
                                        .setParameter(0, vb.getId()).list();
                        List<DetailSystemSelect> dssList = new ArrayList<>();
                                List<DetailSystemSelect> dssList = new ArrayList<>();
                        for (DetailSystem ds : detailSystemList) {
                           DetailSystemSelect dss = new DetailSystemSelect();
                           dss.setDetailSystem(ds);
                           dss.setSelected(false);
                           dssList.add(dss);
                        }
                                for (DetailSystem ds : detailSystemList) {
                                    DetailSystemSelect dss = new DetailSystemSelect();
                                    dss.setDetailSystem(ds);
                                    dss.setSelected(false);
                                    dssList.add(dss);
                                }
                        // 设置已经存在的
                        for (DetailSystem ds : detailSystemS) {
                           for (DetailSystemSelect dss : dssList) {
                              if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) {
                                 dss.setSelected(true);
                                 break;
                              }
                           }
                        }
                        SuperCategoryRecommendVideo sz = new SuperCategoryRecommendVideo();
                        sz.setDetailSystem(null);
                        sz.setCategoryRecommendVideo(vb);
                        zhiBoClassList.add(new CategoryRecommendVideoAdmin(sz, dssList));
                     }
                                // 设置已经存在的
                                for (DetailSystem ds : detailSystemS) {
                                    for (DetailSystemSelect dss : dssList) {
                                        if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) {
                                            dss.setSelected(true);
                                            break;
                                        }
                                    }
                                }
                                SuperCategoryRecommendVideo sz = new SuperCategoryRecommendVideo();
                                sz.setDetailSystem(null);
                                sz.setCategoryRecommendVideo(vb);
                                zhiBoClassList.add(new CategoryRecommendVideoAdmin(sz, dssList));
                            }
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
                  return zhiBoClassList;
               }
            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return zhiBoClassList;
                    }
                });
   }
    }
   public long getCategoryRecommendVideoAdminCount(String key, int videoTypeId, int detailSystem) {
      String sql = "";
      //sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
      //+ videoTypeId + " and zb.detailsystemid=" + detailSystem + " group by zb.rcvid) s";
      if (detailSystem > 0)
//         sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid left join wk_video_video vi on vi.id=c.videoid  where c.videotypeid="
//               + videoTypeId + " and zb.detailsystemid=" + detailSystem + "  and vi.name like '%"+key+"%' group by zb.rcvid) s";
         sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
         + videoTypeId + " and zb.detailsystemid=" + detailSystem + " group by zb.rcvid) s";
         else
//         sql = "select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid left join wk_video_video vi on vi.id=c.videoid where c.videotypeid="
//               + videoTypeId + " and vi.name like '%"+key+"%'";
         sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
         + videoTypeId + "  group by zb.rcvid) s";
      return categoryRecommendVideoDao.getCountSQL(sql);
   }
    public long getCategoryRecommendVideoAdminCount(String key, int videoTypeId,String systemId, int detailSystem) {
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
                    + videoTypeId + " and zb.detailsystemid=" + detailSystem + " group by zb.rcvid) s";
        else
            sql = "select count(*) from  (select count(*) from wk_video_super_recommend_category_video zb left join wk_recommend_category_video c on c.id=zb.rcvid where c.videotypeid="
                    + videoTypeId + " and c.system="+systemId+" group by zb.rcvid) s";
   @SuppressWarnings("unchecked")
   public void deleteCategoryRecommendVideoAdmin(final String crvid, final String detailSystemId) {
      categoryRecommendVideoDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
        return categoryRecommendVideoDao.getCountSQL(sql);
    }
               List<SuperCategoryRecommendVideo> list = session
                     .createQuery(
                           "from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=? and vb.detailSystem.id=?")
                     .setParameter(0, crvid).setParameter(1, detailSystemId).list();
               session.getTransaction().begin();
               if (list != null && list.size() > 0) {
                  for (SuperCategoryRecommendVideo vb : list)
                     session.delete(vb);
               }
               session.flush();
               session.getTransaction().commit();
    @SuppressWarnings("unchecked")
    public void deleteCategoryRecommendVideoAdmin(final String crvid, final String detailSystemId) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
            } catch (Exception e) {
               e.printStackTrace();
               session.getTransaction().rollback();
            }
            return null;
         }
      });
                    List<SuperCategoryRecommendVideo> list = session
                            .createQuery(
                                    "from SuperCategoryRecommendVideo vb where vb.categoryRecommendVideo.id=? and vb.detailSystem.id=?")
                            .setParameter(0, crvid).setParameter(1, detailSystemId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperCategoryRecommendVideo vb : list)
                            session.delete(vb);
                    }
                    session.flush();
                    session.getTransaction().commit();
   }
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
   public List<CategoryRecommendVideo> getSuperCategoryRecommendVideoList(String detailSystemId) {
      return categoryRecommendVideoDao
            .list("select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh where sh.detailSystem.id="
                  + detailSystemId);
   }
    }
   // 更新广告列表
   @SuppressWarnings({ "unchecked", "rawtypes" })
   public void updateSuperCategoryRecommendVideoList(final String detailSystemId,
         final List<CategoryRecommendVideo> adList) {
      categoryRecommendVideoDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
               List<SuperCategoryRecommendVideo> list = session
                     .createQuery(
                           "from SuperCategoryRecommendVideo sh where sh.detailSystem.id=" + detailSystemId)
                     .list();
               session.getTransaction().begin();
               for (SuperCategoryRecommendVideo ad : list) {
                  session.delete(ad);
               }
    public List<CategoryRecommendVideo> getSuperCategoryRecommendVideoList(String detailSystemId) {
        return categoryRecommendVideoDao
                .list("select sh.categoryRecommendVideo from SuperCategoryRecommendVideo sh where sh.detailSystem.id="
                        + detailSystemId);
    }
               for (CategoryRecommendVideo categoryRecommendVideo : adList) {
                  SuperCategoryRecommendVideo shd = new SuperCategoryRecommendVideo();
                  shd.setDetailSystem(new DetailSystem(detailSystemId));
                  shd.setCategoryRecommendVideo(categoryRecommendVideo);
                  session.persist(shd);
               }
               session.flush();
               session.getTransaction().commit();
            } catch (Exception e) {
               e.printStackTrace();
               session.getTransaction().rollback();
            }
            return null;
         }
      });
    // 更新广告列表
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void updateSuperCategoryRecommendVideoList(final String detailSystemId,
                                                      final List<CategoryRecommendVideo> adList) {
        categoryRecommendVideoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperCategoryRecommendVideo> list = session
                            .createQuery(
                                    "from SuperCategoryRecommendVideo sh where sh.detailSystem.id=" + detailSystemId)
                            .list();
                    session.getTransaction().begin();
                    for (SuperCategoryRecommendVideo ad : list) {
                        session.delete(ad);
                    }
   }
                    for (CategoryRecommendVideo categoryRecommendVideo : adList) {
                        SuperCategoryRecommendVideo shd = new SuperCategoryRecommendVideo();
                        shd.setDetailSystem(new DetailSystem(detailSystemId));
                        shd.setCategoryRecommendVideo(categoryRecommendVideo);
                        session.persist(shd);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
   public void addSuperCategoryRecommendVideo(SuperCategoryRecommendVideo sv) {
      List<SuperCategoryRecommendVideo> list = superCategoryRecommendVideoDao
            .list("from SuperCategoryRecommendVideo sv where sv.categoryRecommendVideo.id="
                  + sv.getCategoryRecommendVideo().getId() + " and sv.detailSystem.id="
                  + sv.getDetailSystem().getId());
      if (list != null && list.size() > 0)
         return;
      superCategoryRecommendVideoDao.create(sv);
   }
    }
    public void addSuperCategoryRecommendVideo(SuperCategoryRecommendVideo sv) {
        List<SuperCategoryRecommendVideo> list = superCategoryRecommendVideoDao
                .list("from SuperCategoryRecommendVideo sv where sv.categoryRecommendVideo.id="
                        + sv.getCategoryRecommendVideo().getId() + " and sv.detailSystem.id="
                        + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superCategoryRecommendVideoDao.create(sv);
    }
}