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.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.newvideo.dao.HomeNoticeDao; import com.newvideo.dao.SuperHomeNoticeDao; import com.newvideo.dao.VideoInfoDao; import com.newvideo.domain.DetailSystem; import com.newvideo.domain.HomeNotice; import com.newvideo.domain.HomeType; import com.newvideo.domain.SuperHomeNotice; import com.newvideo.domain.SystemInfo; import com.newvideo.domain.web.DetailSystemSelect; import com.newvideo.domain.web.HomeNoticeAdmin; import com.newvideo.util.Constant; @Service public class HomeNoticeService { @Resource private HomeNoticeDao homeNoticeDao; @Resource private VideoInfoDao videoInfoDao; @Resource private SuperHomeNoticeDao superHomeNoticeDao; // 首页公告 public List getHomeNoticeList(SystemInfo system) { List list = homeNoticeDao .list("from HomeNotice h where h.show='1' and h.system.id=" + system.getId()); return list; } // 首页公告 public List getHomeNoticeList() { List list = homeNoticeDao.list("from HomeNotice"); return list; } public List getVisiableHomeNoticeList() { List list = homeNoticeDao.list("from HomeNotice h where h.show=1"); return list; } public void addHomeNotice(HomeNotice notice) { homeNoticeDao.create(notice); } public void updateHomeNotice(HomeNotice notice) { homeNoticeDao.update(notice); } public HomeNotice getHomeNotice(Serializable s) { return homeNoticeDao.find(HomeNotice.class, s); } @SuppressWarnings("unchecked") public void deleteHomeNotice(final HomeNotice notice) { homeNoticeDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session.createQuery("from SuperHomeNotice sv where sv.homeNotice.id=?") .setParameter(0, notice.getId()).list(); session.getTransaction().begin(); for (SuperHomeNotice sv : list) session.delete(sv); session.delete(new HomeNotice(notice.getId())); session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public List getIOSTestHomeType() { List list = new ArrayList(); HomeType type = new HomeType(); type.setId("130"); type.setName("萌宠奇趣"); type.setVideoList( videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=130 order by v.createtime desc", 0, 3, new String[] {})); list.add(type); type = new HomeType(); type.setId("129"); type.setName("相声小品"); type.setVideoList( videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=129 order by v.createtime desc", 0, 3, new String[] {})); list.add(type); type = new HomeType(); type.setId("19"); type.setName("纪录片"); type.setVideoList( videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=19 order by v.createtime desc", 0, 3, new String[] {})); list.add(type); type = new HomeType(); type.setId("14"); type.setName("游戏"); type.setVideoList(videoInfoDao.list( "from VideoInfo v where v.show=1 and v.videoType.id=91 OR v.videoType.id=92 OR v.videoType.id=93 OR v.videoType.id=94 order by v.createtime desc", 0, 3, new String[] {})); list.add(type); type = new HomeType(); type.setId("13"); type.setName("体育"); type.setVideoList( videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=13 order by v.createtime desc", 0, 3, new String[] {})); list.add(type); return list; } // 后台操作 @SuppressWarnings("unchecked") public List getHomeNoticeAdmin(final String key, final int detailSystem, final int page) { return (List) homeNoticeDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { List zhiBoClassList = new ArrayList(); try { List detailSystemList = session.createQuery("from DetailSystem").list(); String sql = ""; if (detailSystem > 0) sql = "select sh.homeNotice from SuperHomeNotice sh where sh.detailSystem.id=" + detailSystem + " order by sh.homeNotice.createtime desc"; else sql = "from HomeNotice zb order by zb.createtime desc"; List list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount) .setMaxResults(Constant.pageCount).list(); for (HomeNotice vb : list) { List detailSystemS = session .createQuery("select vb.detailSystem from SuperHomeNotice vb where vb.homeNotice.id=?") .setParameter(0, vb.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; } } } SuperHomeNotice sz = new SuperHomeNotice(); sz.setDetailSystem(null); sz.setHomeNotice(vb); zhiBoClassList.add(new HomeNoticeAdmin(sz, dssList)); } } catch (Exception e) { e.printStackTrace(); } return zhiBoClassList; } }); } public long getHomeNoticeAdminCount(String key, int detailSystem) { String sql = ""; if (detailSystem > 0) sql = "select count(*) from (select count(*) from wk_video_super_homenotice zb left join wk_video_homenotice c on c.id=zb.homenoticeid where zb.detailsystem=" + detailSystem + " group by zb.homenoticeid) s"; else sql = "select count(*) from (select count(*) from wk_video_super_homenotice zb left join wk_video_homenotice c on c.id=zb.homenoticeid group by zb.homenoticeid) s"; return homeNoticeDao.getCountSQL(sql); } @SuppressWarnings("unchecked") public void deleteHomeNoticeAdmin(final String classId, final String detailSystemId) { homeNoticeDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery("from SuperHomeNotice vb where vb.homeNotice.id=? and vb.detailSystem.id=?") .setParameter(0, classId).setParameter(1, detailSystemId).list(); session.getTransaction().begin(); if (list != null && list.size() > 0) { for (SuperHomeNotice vb : list) session.delete(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } /** * 小黄条 */ public List getHomeNoticeList(String detailSystem) { return homeNoticeDao .list("select hs.homeNotice FROM SuperHomeNotice hs where hs.detailSystem.id=" + detailSystem); } public void addSuperHomeNotice(SuperHomeNotice sv) { List list = superHomeNoticeDao.list("from SuperHomeNotice sv where sv.homeNotice.id=" + sv.getHomeNotice().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId()); if (list != null && list.size() > 0) return; superHomeNoticeDao.create(sv); } public void updateSuperHomeNotice(SuperHomeNotice hotSearch) { superHomeNoticeDao.update(hotSearch); } @Transactional public void deleteSuperHomeNotice(final SuperHomeNotice hotSearch) { superHomeNoticeDao.excute(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException { Query query = session.createQuery("delete from SuperHomeNotice shn where shn.homeNotice.id = ? and shn.detailSystem.id = ?"); query.setParameter(0, hotSearch.getHomeNotice().getId()); query.setParameter(1, hotSearch.getDetailSystem().getId()); query.executeUpdate(); return null; } }); } @SuppressWarnings("unchecked") public void updateSuperHomeNoticeList(final String detailSystemId, final List typeList) { homeNoticeDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery("from SuperHomeNotice sh where sh.detailSystem.id=" + detailSystemId).list(); session.getTransaction().begin(); for (SuperHomeNotice ad : list) { session.delete(ad); } for (SuperHomeNotice videoType : typeList) { session.persist(videoType); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } }