src/main/java/com/yeshi/buwan/service/imp/HomeAdService.java
@@ -14,10 +14,10 @@
import com.yeshi.buwan.dao.HomeAdDao;
import com.yeshi.buwan.dao.SuperHomeAdDao;
import com.yeshi.buwan.domain.DetailSystem;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.HomeAd;
import com.yeshi.buwan.domain.SuperHomeAd;
import com.yeshi.buwan.domain.SystemInfo;
import com.yeshi.buwan.domain.system.SystemInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.domain.web.HomeAdAdmin;
@@ -25,262 +25,246 @@
@Service
public class HomeAdService {
   @Resource
   private HomeAdDao homeAdDao;
   @Resource
   private SuperHomeAdDao superHomeAdDao;
    @Resource
    private HomeAdDao homeAdDao;
    @Resource
    private SuperHomeAdDao superHomeAdDao;
   public SuperHomeAdDao getSuperHomeAdDao() {
      return superHomeAdDao;
   }
    public List<HomeAd> getHomeAdList(SystemInfo system) {
        List<HomeAd> list = homeAdDao
                .list("from HomeAd h where h.system.id=" + system.getId() + " order by  h.orderby desc");
        return list;
    }
   public void setSuperHomeAdDao(SuperHomeAdDao superHomeAdDao) {
      this.superHomeAdDao = superHomeAdDao;
   }
    @Cacheable(value = "homeCache", key = "'getHomeAdList'+'-'+#detailSystem.id")
    public List<HomeAd> getHomeAdList(DetailSystem detailSystem) {
        List<HomeAd> list = homeAdDao.list("select h.homeAd from SuperHomeAd h where h.detailSystem.id="
                + detailSystem.getId() + " order by h.homeAd.orderby desc");
        return list;
    }
   public HomeAdDao getHomeAdDao() {
      return homeAdDao;
   }
    public static List<HomeAd> getIOSTestHomeAdList() {
        List<HomeAd> 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);
   public void setHomeAdDao(HomeAdDao homeAdDao) {
      this.homeAdDao = homeAdDao;
   }
        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);
   public List<HomeAd> getHomeAdList(SystemInfo system) {
      List<HomeAd> list = homeAdDao
            .list("from HomeAd h where h.system.id=" + system.getId() + " order by  h.orderby desc");
      return list;
   }
        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);
   @Cacheable(value = "homeCache", key = "'getHomeAdList'+'-'+#detailSystem.id")
   public List<HomeAd> getHomeAdList(DetailSystem detailSystem) {
      List<HomeAd> list = homeAdDao.list("select h.homeAd from SuperHomeAd h where h.detailSystem.id="
            + detailSystem.getId() + " order by h.homeAd.orderby desc");
      return list;
   }
        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);
   public static List<HomeAd> getIOSTestHomeAdList() {
      List<HomeAd> 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);
        return list;
    }
      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);
    public List<HomeAd> getHomeAdList() {
      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);
        List<HomeAd> list = homeAdDao.list("from HomeAd ha order by ha.orderby desc");
        return list;
    }
      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);
    @SuppressWarnings("unchecked")
    public void deleteHomeAd(final String id) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> 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;
            }
        });
      return list;
   }
    }
   public List<HomeAd> getHomeAdList() {
    public Serializable addHomeAd(HomeAd ad) {
        return homeAdDao.save(ad);
    }
      List<HomeAd> list = homeAdDao.list("from HomeAd ha order by ha.orderby desc");
      return list;
   }
    public HomeAd getHomeAdById(String id) {
   @SuppressWarnings("unchecked")
   public void deleteHomeAd(final String id) {
      homeAdDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
               List<SuperHomeAd> 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;
         }
      });
        return homeAdDao.find(HomeAd.class, id);
    }
   }
    public void updateHomeAd(HomeAd ad) {
        homeAdDao.update(ad);
    }
   public Serializable addHomeAd(HomeAd ad) {
      return homeAdDao.save(ad);
   }
    // 后台操作
    @SuppressWarnings("unchecked")
    public List<HomeAdAdmin> getHomeAdAdmin(final String key, final String systemId, final int detailSystem, final int page) {
        return (List<HomeAdAdmin>) homeAdDao.excute(new HibernateCallback<List<HomeAdAdmin>>() {
            public List<HomeAdAdmin> doInHibernate(Session session) throws HibernateException {
                List<HomeAdAdmin> zhiBoClassList = new ArrayList<>();
                try {
                    List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem ds where ds.system.id=" + systemId).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 where zb.system.id=" + systemId + " order by zb.createtime desc";
   public HomeAd getHomeAdById(String id) {
                    List<HomeAd> list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount)
                            .setMaxResults(Constant.pageCount).list();
                    for (HomeAd vb : list) {
                        List<DetailSystem> detailSystemS = session
                                .createQuery("select vb.detailSystem from SuperHomeAd vb where vb.homeAd.id=?")
                                .setParameter(0, vb.getId()).list();
      return homeAdDao.find(HomeAd.class, id);
   }
                        List<DetailSystemSelect> dssList = new ArrayList<>();
   public void updateHomeAd(HomeAd ad) {
      homeAdDao.update(ad);
   }
                        for (DetailSystem ds : detailSystemList) {
                            DetailSystemSelect dss = new DetailSystemSelect();
                            dss.setDetailSystem(ds);
                            dss.setSelected(false);
                            dssList.add(dss);
                        }
   // 后台操作
   @SuppressWarnings("unchecked")
   public List<HomeAdAdmin> getHomeAdAdmin(final String key, final int detailSystem, final int page) {
      return (List<HomeAdAdmin>) homeAdDao.excute(new HibernateCallback<List<HomeAdAdmin>>() {
         public List<HomeAdAdmin> doInHibernate(Session session) throws HibernateException {
            List<HomeAdAdmin> zhiBoClassList = new ArrayList<>();
            try {
               List<DetailSystem> 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";
                        // 设置已经存在的
                        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));
                    }
               List<HomeAd> list = session.createQuery(sql).setFirstResult((page - 1) * Constant.pageCount)
                     .setMaxResults(Constant.pageCount).list();
               for (HomeAd vb : list) {
                  List<DetailSystem> detailSystemS = session
                        .createQuery("select vb.detailSystem from SuperHomeAd vb where vb.homeAd.id=?")
                        .setParameter(0, vb.getId()).list();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return zhiBoClassList;
            }
        });
    }
                  List<DetailSystemSelect> dssList = new ArrayList<>();
    public long getHomeAdAdminCount(String key, String systemId, 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 where system="+systemId;
        return homeAdDao.getCountSQL(sql);
    }
                  for (DetailSystem ds : detailSystemList) {
                     DetailSystemSelect dss = new DetailSystemSelect();
                     dss.setDetailSystem(ds);
                     dss.setSelected(false);
                     dssList.add(dss);
                  }
    @SuppressWarnings("unchecked")
    public void deleteHomeAdAdmin(final String classId, final String detailSystemId) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> 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;
            }
        });
                  // 设置已经存在的
                  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 List<HomeAd> getSuperHomeAdList(String detailSystemId) {
        return homeAdDao.list("select sh.homeAd from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId);
    }
   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 updateSuperHomeAdList(final String detailSystemId, final List<HomeAd> adList) {
        homeAdDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHomeAd> list = session
                            .createQuery("from SuperHomeAd sh where sh.detailSystem.id=" + detailSystemId).list();
                    session.getTransaction().begin();
                    for (SuperHomeAd ad : list) {
                        session.delete(ad);
                    }
   @SuppressWarnings("unchecked")
   public void deleteHomeAdAdmin(final String classId, final String detailSystemId) {
      homeAdDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
               List<SuperHomeAd> 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;
         }
      });
                    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 List<HomeAd> 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<HomeAd> adList) {
      homeAdDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            try {
               List<SuperHomeAd> 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<SuperHomeAd> 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);
   }
    public void addSuperHomeAd(SuperHomeAd sv) {
        List<SuperHomeAd> 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);
    }
}