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.HomeAdDao; import com.newvideo.dao.SuperHomeAdDao; import com.newvideo.domain.DetailSystem; import com.newvideo.domain.HomeAd; import com.newvideo.domain.SuperHomeAd; import com.newvideo.domain.SystemInfo; import com.newvideo.domain.VideoInfo; import com.newvideo.domain.web.DetailSystemSelect; import com.newvideo.domain.web.HomeAdAdmin; import com.newvideo.util.Constant; @Service public class HomeAdService { @Resource private HomeAdDao homeAdDao; @Resource private SuperHomeAdDao superHomeAdDao; public SuperHomeAdDao getSuperHomeAdDao() { return superHomeAdDao; } public void setSuperHomeAdDao(SuperHomeAdDao superHomeAdDao) { this.superHomeAdDao = superHomeAdDao; } public HomeAdDao getHomeAdDao() { return homeAdDao; } public void setHomeAdDao(HomeAdDao homeAdDao) { this.homeAdDao = homeAdDao; } public List getHomeAdList(SystemInfo system) { List list = homeAdDao .list("from HomeAd h where h.system.id=" + system.getId() + " order by h.orderby desc"); return list; } @Cacheable(value = "homeCache", key = "'getHomeAdList'+'-'+#detailSystem.id") public List getHomeAdList(DetailSystem detailSystem) { List list = homeAdDao.list("select h.homeAd from SuperHomeAd h where h.detailSystem.id=" + detailSystem.getId() + " order by h.homeAd.orderby desc"); return list; } public static List getIOSTestHomeAdList() { List list = new ArrayList(); HomeAd ad = new HomeAd(); ad.setPicture("http://img.zcool.cn/community/01a5a7562499216ac7254878995802.jpg"); VideoInfo info = new VideoInfo(); info.setId("161850"); info.setName("【摔杯为号】赵家班父子摔杯演闹剧"); info.setPicture("http://img2.funshion.com/pictures/poseidon01/418/985/4189850_v_still.jpg"); info.setThirdType("0"); info.setShare("0"); ad.setVideo(info); list.add(ad); ad = new HomeAd(); ad.setPicture("http://img.zcool.cn/community/01ac60563acca86ac7259e0fe92cae.jpg"); info = new VideoInfo(); info.setId("161334"); info.setName("【冯巩】冯巩,牛莉《夫妻日记》"); info.setPicture("http://img.zcool.cn/community/01ac60563acca86ac7259e0fe92cae.jpg"); info.setThirdType("0"); info.setShare("0"); ad.setVideo(info); list.add(ad); ad = new HomeAd(); ad.setPicture("http://img.zcool.cn/community/01ae2b563accb032f87512f63f63ad.jpg"); info = new VideoInfo(); info.setId("163707"); info.setName("【郭德纲于谦】《谦哥带儿子见世面》"); info.setPicture("http://img.zcool.cn/community/01cdfb563acca06ac7259e0fb69676.jpg"); info.setThirdType("0"); info.setShare("0"); ad.setVideo(info); list.add(ad); ad = new HomeAd(); ad.setPicture("http://img.zcool.cn/community/01ae2b563accb032f87512f63f63ad.jpg"); info = new VideoInfo(); info.setId("161339"); info.setName("【欢乐喜剧人】开心麻花《感染者》"); info.setPicture("http://img2.funshion.com/pictures/poseidon01/418/985/4189850_v_still.jpg"); info.setThirdType("0"); info.setShare("0"); ad.setVideo(info); list.add(ad); return list; } public List getHomeAdList() { List list = homeAdDao.list("from HomeAd ha order by ha.orderby desc"); return list; } @SuppressWarnings("unchecked") public void deleteHomeAd(final String id) { homeAdDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session.createQuery("from SuperHomeAd sh where sh.homeAd.id=?") .setParameter(0, id).list(); session.getTransaction().begin(); for (SuperHomeAd shd : list) session.delete(shd); session.createQuery("delete from HomeAd ha where ha.id=?").setParameter(0, id).executeUpdate(); session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public Serializable addHomeAd(HomeAd ad) { return homeAdDao.save(ad); } public HomeAd getHomeAdById(String id) { return homeAdDao.find(HomeAd.class, id); } public void updateHomeAd(HomeAd ad) { homeAdDao.update(ad); } // 后台操作 @SuppressWarnings("unchecked") public List getHomeAdAdmin(final String key, final int detailSystem, final int page) { return (List) homeAdDao.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.homeAd from SuperHomeAd sh where sh.detailSystem.id=" + detailSystem + " order by sh.homeAd.createtime desc"; else sql = "from HomeAd zb order by zb.createtime desc"; List list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount) .setMaxResults(Constant.pageCount).list(); for (HomeAd vb : list) { List detailSystemS = session .createQuery("select vb.detailSystem from SuperHomeAd vb where vb.homeAd.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; } } } SuperHomeAd sz = new SuperHomeAd(); sz.setDetailSystem(null); sz.setHomeAd(vb); zhiBoClassList.add(new HomeAdAdmin(sz, dssList)); } } catch (Exception e) { e.printStackTrace(); } return zhiBoClassList; } }); } public long getHomeAdAdminCount(String key, int detailSystem) { String sql = ""; if (detailSystem > 0) sql = "select count(*) from (select count(*) from wk_video_super_homead zb left join wk_video_homead c on c.id=zb.homeadid where zb.detailsystemid=" + detailSystem + " group by zb.homeadid) s"; else sql = "select count(*) from wk_video_homead"; return homeAdDao.getCountSQL(sql); } @SuppressWarnings("unchecked") public void deleteHomeAdAdmin(final String classId, final String detailSystemId) { homeAdDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery("from SuperHomeAd vb where vb.homeAd.id=? and vb.detailSystem.id=?") .setParameter(0, classId).setParameter(1, detailSystemId).list(); session.getTransaction().begin(); if (list != null && list.size() > 0) { for (SuperHomeAd vb : list) session.delete(vb); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public List getSuperHomeAdList(String detailSystemId) { return homeAdDao.list("select sh.homeAd from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId); } // 更新广告列表 @SuppressWarnings("unchecked") public void updateSuperHomeAdList(final String detailSystemId, final List adList) { homeAdDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery("from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId).list(); session.getTransaction().begin(); for (SuperHomeAd ad : list) { session.delete(ad); } for (HomeAd homeAd : adList) { SuperHomeAd shd = new SuperHomeAd(); shd.setCreatetime(System.currentTimeMillis() + ""); shd.setDetailSystem(new DetailSystem(detailSystemId)); shd.setHomeAd(homeAd); session.persist(shd); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public void addSuperHomeAd(SuperHomeAd sv) { List list = superHomeAdDao.list("from SuperHomeAd sv where sv.homeAd.id=" + sv.getHomeAd().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId()); if (list != null && list.size() > 0) return; superHomeAdDao.create(sv); } }