package com.newvideo.service.imp; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.cache.annotation.Cacheable; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.newvideo.dao.VideoBanQuanDao; import com.newvideo.dao.VideoBanQuanVideoDao; import com.newvideo.dao.WebVideoDao; import com.newvideo.domain.DetailSystem; import com.newvideo.domain.HomeVideo; import com.newvideo.domain.VideoBanQuan; import com.newvideo.domain.VideoBanQuanVideo; import com.newvideo.domain.VideoInfo; import com.newvideo.domain.VideoResource; import com.newvideo.domain.web.BanquanKeyAdmin; import com.newvideo.domain.web.BanquanVideoAdmin; import com.newvideo.domain.web.DetailSystemSelect; import com.newvideo.util.Constant; import com.newvideo.util.StringUtil; @Service public class BanQuanService { @Resource private VideoBanQuanDao videoBanQuanDao; @Resource private VideoBanQuanVideoDao videoBanQuanVideoDao; @Resource private WebVideoDao webVideoDao; public VideoBanQuanVideoDao getVideoBanQuanVideoDao() { return videoBanQuanVideoDao; } public void setVideoBanQuanVideoDao(VideoBanQuanVideoDao videoBanQuanVideoDao) { this.videoBanQuanVideoDao = videoBanQuanVideoDao; } public VideoBanQuanDao getVideoBanQuanDao() { return videoBanQuanDao; } public void setVideoBanQuanDao(VideoBanQuanDao videoBanQuanDao) { this.videoBanQuanDao = videoBanQuanDao; } @SuppressWarnings("unchecked") public void addBanQuanVideo(final List list) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); for (int i = 0; i < list.size(); i++) { List bl = session .createQuery("from VideoBanQuanVideo v where v.detailSystem.id=" + list.get(i).getDetailSystem().getId() + " and v.info.id=" + list.get(i).getInfo().getId()) .list(); if (bl == null || bl.size() == 0) { session.persist(list.get(i)); } } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } @SuppressWarnings("unchecked") public void addBanQuanKey(final List list) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); for (int i = 0; i < list.size(); i++) { List bl = session .createQuery("from VideoBanQuan v where v.detailSystem.id=" + list.get(i).getDetailSystem().getId() + " and v.name=?") .setParameter(0, list.get(i).getName()).list(); if (bl == null || bl.size() == 0) { session.persist(list.get(i)); } } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public VideoBanQuanVideo getBanQuanVideo(String id) { return videoBanQuanVideoDao.find(VideoBanQuanVideo.class, id); } public VideoBanQuan getBanQuanKey(String id) { return videoBanQuanDao.find(VideoBanQuan.class, id); } public void updateVideoBanQuanVideo(VideoBanQuanVideo video) { videoBanQuanVideoDao.update(video); } public void updateVideoBanQuan(VideoBanQuan video) { videoBanQuanDao.update(video); } public void deleteBanQuanVideo(final List list) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); for (int i = 0; i < list.size(); i++) { session.delete(list.get(i)); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public void deleteBanQuanKey(final List list) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); for (int i = 0; i < list.size(); i++) { session.delete(list.get(i)); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public void deleteBanQuanKeyByKey(final String key) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); session.createQuery("delete from VideoBanQuan vb where vb.name=?").setParameter(0, key) .executeUpdate(); session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } return null; } }); } public void deleteBanQuanVideoByVID(final String videoid) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); session.createQuery("delete from VideoBanQuanVideo vb where vb.info.id=?").setParameter(0, videoid) .executeUpdate(); session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } return null; } }); } public List getBanQuanVideoList(int page, String key, int detailSystemId) { String sql = ""; if (detailSystemId == 0) sql = "from VideoBanQuanVideo vb where vb.name like ?"; else sql = "from VideoBanQuanVideo vb where vb.name like ? and vb.detailSystem.id=" + detailSystemId; return videoBanQuanVideoDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, new String[] { "%" + key + "%" }); } public long getBanQuanVideoPage(String key, int detailSystemId) { String sql = ""; if (detailSystemId == 0) { sql = "select count(*) from VideoBanQuanVideo v where v.name like ?"; } else { sql = "select count(*) from VideoBanQuanVideo v where v.name like ? and v.detailSystem.id=" + detailSystemId; } long count = videoBanQuanVideoDao.getCount(sql, new String[] { "%" + key + "%" }); return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1; } public List getBanQuanKeyList(int page, String key, int detailSystemId) { String sql = ""; if (detailSystemId == 0) sql = "from VideoBanQuan vb where vb.name like ?"; else sql = "from VideoBanQuan vb where vb.name like ? and vb.detailSystem.id=" + detailSystemId; return videoBanQuanDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount, new String[] { "%" + key + "%" }); } public long getBanQuanKeyPage(String key, int detailSystemId) { String sql = ""; if (detailSystemId == 0) { sql = "select count(*) from VideoBanQuan v where v.name like ?"; } else { sql = "select count(*) from VideoBanQuan v where v.name like ? and v.detailSystem.id=" + detailSystemId; } long count = videoBanQuanVideoDao.getCount(sql, new String[] { "%" + key + "%" }); return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1; } // 过滤版权视频 @SuppressWarnings({ "rawtypes", "unchecked" }) @Cacheable(value = "userCache", key = "'getBanQuanVideo'+'-'+#cacheMD5+'-'+#detailSystemId") public List getBanQuanVideo(final List list, final String detailSystemId, String cacheMD5) { if (list == null || list.size() == 0) return list; List newList = (List) videoBanQuanDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { String sql = ""; for (VideoInfo vi : list) { sql += " (SELECT v.`id`,bv.`videoid`,bv.`detailsystemid`,bv.`show` FROM wk_video_video v LEFT JOIN wk_video_banquan_video bv ON bv.`videoid`=v.`id` WHERE v.`id`=" + vi.getId() + ") UNION ALL"; } if (sql.endsWith("UNION ALL")) sql = sql.substring(0, sql.length() - 9); try { List rlist = session.createSQLQuery(sql).list(); for (int i = 0; i < rlist.size(); i++) { Object[] objs = (Object[]) rlist.get(i); if (!StringUtil.isNullOrEmpty(objs[1] + "") && "1".equalsIgnoreCase(objs[3] + "") && detailSystemId.equalsIgnoreCase(objs[2] + "")) { // 是版权视频 if (list.size() > i) list.get(i).setShow("0"); } else { if (list.size() > i) list.get(i).setShow("1"); } } } catch (Exception e) { e.printStackTrace(); } return list; } }); for (int i = 0; i < newList.size(); i++) { if (newList.get(i).getShow().equalsIgnoreCase("0")) { newList.remove(i); i--; } } return newList; } @SuppressWarnings("rawtypes") public List getBanQuanHomeVideo(final List list, final String detailSystemId) { videoBanQuanDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { String sql = ""; for (HomeVideo hv : list) { sql += " (SELECT v.`id`,bv.`videoid`,bv.`detailsystemid`,bv.`show` FROM wk_video_video v LEFT JOIN wk_video_banquan_video bv ON bv.`videoid`=v.`id` WHERE v.`id`=" + hv.getVideo().getId() + ") UNION ALL"; } if (sql.endsWith("UNION ALL")) sql = sql.substring(0, sql.length() - 9); try { List rlist = session.createSQLQuery(sql).list(); for (int i = 0; i < rlist.size(); i++) { Object[] objs = (Object[]) rlist.get(i); if (!StringUtil.isNullOrEmpty(objs[1] + "") && "1".equalsIgnoreCase(objs[2] + "") && detailSystemId.equalsIgnoreCase(objs[3] + "")) { // 是版权视频 list.get(i).getVideo().setShow("0"); } else list.get(i).getVideo().setShow("1"); } } catch (Exception e) { e.printStackTrace(); } return list; } }); for (int i = 0; i < list.size(); i++) { if (list.get(i).getVideo().getShow().equalsIgnoreCase("0")) { list.remove(i); i--; } } return list; } /** * 管理页面操作 */ @SuppressWarnings("unchecked") public List getBanquanKeyAdmin(final String key, final int detailSystem, final int page) { return (List) videoBanQuanDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { List banquanList = new ArrayList(); try { List detailSystemList = session.createQuery("from DetailSystem").list(); String where = ""; if (detailSystem > 0) where = "where vb.detailSystem.id=" + detailSystem + " and"; else where = "where"; List list = session .createQuery("from VideoBanQuan vb " + where + " vb.name like ? group by vb.name order by vb.createtime desc") .setParameter(0, "%" + key + "%").setFirstResult((page - 1) * Constant.pageCount) .setMaxResults(Constant.pageCount).list(); for (VideoBanQuan vb : list) { List detailSystemS = session .createQuery("select vb.detailSystem from VideoBanQuan vb where vb.name=?") .setParameter(0, vb.getName()).list(); List dssList = new ArrayList(); 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; } } } banquanList.add(new BanquanKeyAdmin(vb, dssList)); } } catch (Exception e) { e.printStackTrace(); } return banquanList; } }); } public long getBanquanKeyAdminCount(String key, int detailSystem) { String where = ""; if (detailSystem > 0) where = "where vb.detailsystem=" + detailSystem + " and"; else where = "where"; return videoBanQuanVideoDao.getCountSQL("select count(*) from (select count(*) from wk_video_banquan vb " + where + " vb.name like '%" + key + "%' group by vb.name ) s"); } @SuppressWarnings("rawtypes") public void addBanQuanKeyAdmin(final String key, final String detailSystemId) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session.createQuery("from VideoBanQuan vb where vb.name=? and vb.detailSystem.id=?") .setParameter(0, key).setParameter(0, detailSystemId).list(); session.getTransaction().begin(); if (list == null || list.size() == 0) { VideoBanQuan vb = new VideoBanQuan(); vb.setCreatetime(System.currentTimeMillis() + ""); vb.setDetailSystem(new DetailSystem(detailSystemId)); vb.setName(key); vb.setShow(true); vb.setVideoType(1); vb.setVideoResource(new VideoResource("6")); session.persist(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } @SuppressWarnings({ "unchecked", "rawtypes" }) public void deleteBanQuanKeyAdmin(final String key, final String detailSystemId) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); List list = session .createQuery("from VideoBanQuan vb where vb.name=? and vb.detailSystem.id=?") .setParameter(0, key).setParameter(1, detailSystemId).list(); if (list != null && list.size() > 0) { for (VideoBanQuan vb : list) session.delete(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } @SuppressWarnings("unchecked") public List getBanquanVideoAdmin(final String key, final int detailSystem, final int page) { return (List) videoBanQuanDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { List banquanList = new ArrayList(); try { List detailSystemList = session.createQuery("from DetailSystem").list(); String where = ""; if (detailSystem > 0) where += " where vb.detailSystem.id= " + detailSystem + " and "; else where += " where "; List list = session .createQuery("from VideoBanQuanVideo vb " + where + " vb.info.name like ? group by vb.info.id order by vb.createtime desc") .setParameter(0, "%" + key + "%").setFirstResult((page - 1) * Constant.pageCount) .setMaxResults(Constant.pageCount).list(); for (VideoBanQuanVideo vb : list) { List detailSystemS = session .createQuery("select vb.detailSystem from VideoBanQuanVideo vb where vb.info.id=?") .setParameter(0, vb.getInfo().getId()).list(); List dssList = new ArrayList(); 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; } } } banquanList.add(new BanquanVideoAdmin(vb, dssList)); } } catch (Exception e) { e.printStackTrace(); } return banquanList; } }); } public long getBanquanVideoAdminCount(String key, int detailSystem) { String where = ""; if (detailSystem > 0) where += " where bv.detailsystemid= " + detailSystem + " and "; else where += " where "; return videoBanQuanVideoDao.getCountSQL( "select count(*) from (select count(*) from wk_video_banquan_video bv left join wk_video_video v on v.id=bv.videoid " + where + " v.name like '%" + key + "%' group by bv.videoid) s"); } @SuppressWarnings("rawtypes") public void addBanQuanVideoAdmin(final String videoid, final String detailSystemId) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery("from VideoBanQuanVideo vb where vb.info.id=? and vb.detailSystem.id=?") .setParameter(0, videoid).setParameter(1, detailSystemId).list(); session.getTransaction().begin(); if (list == null || list.size() == 0) { VideoBanQuanVideo vb = new VideoBanQuanVideo(); vb.setCreatetime(System.currentTimeMillis() + ""); vb.setDetailSystem(new DetailSystem(detailSystemId)); vb.setInfo(new VideoInfo(videoid)); vb.setName(((VideoInfo) session.get(VideoInfo.class, videoid)).getName()); vb.setShow(true); session.persist(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } return null; } }); } @SuppressWarnings("unchecked") public void deleteBanQuanVideoAdmin(final String videoid, final String detailSystemId) { videoBanQuanDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); List list = session .createQuery("from VideoBanQuanVideo vb where vb.info.id=? and vb.detailSystem.id=?") .setParameter(0, videoid).setParameter(1, detailSystemId).list(); if (list != null && list.size() > 0) { for (VideoBanQuanVideo vb : list) session.delete(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public boolean isNeedWebPlay(String detailSystemId, String videoid) { return webVideoDao.getCount("select count(*) from WebVideo web where web.detailSystem.id=? and web.info.id=?", new Serializable[] { detailSystemId, videoid }) > 0; } }