admin
2018-12-25 4cb15e222cd7d099d533ccbeb7f9a8cd99bf180c
fanli/src/main/java/com/yeshi/fanli/service/impl/config/HotSearchServiceImpl.java
@@ -8,6 +8,7 @@
import javax.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
@@ -23,6 +24,7 @@
import com.yeshi.fanli.service.inter.config.SuperHotSearchService;
import com.yeshi.fanli.service.inter.config.SystemService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.Utils;
@Service
@@ -202,4 +204,130 @@
      });
   }
   @Override
   @SuppressWarnings("unchecked")
   public List<HotSearch> listQuery(int start, int count, String key, Long systemId) throws Exception {
      List<HotSearch> listObj = null;
      if (systemId == null) {
         String hql = "from HotSearch hs where 1=1 ";
         if (!StringUtil.isNullOrEmpty(key)) {
            hql += " hs.name like ?";
         }
         final String queryHQL = hql;
         listObj = (List<HotSearch>) hotSearchDao.excute(new HibernateCallback<List<HotSearch>>() {
            public List<HotSearch> doInHibernate(Session session) throws HibernateException {
               Query query = session.createQuery(queryHQL);
               query.setFirstResult(start);
               query.setMaxResults(count);
               if (!StringUtil.isNullOrEmpty(key)) {
                  query.setParameter(0, "%" + key + "%");
               }
               return query.list();
            }
         });
      } else {
         List<SuperHotSearch> superHotSearchList = null;
         if(StringUtil.isNullOrEmpty(key)){
            superHotSearchList = superHotSearchService.getSuperHotSearchBySystemId(systemId,start, count);
         }else{
            String likekey = "%"+key+"%";
            superHotSearchList = superHotSearchService.getSuperHotSearchBySystemId(systemId, start, count, likekey);
         }
         if (superHotSearchList != null && superHotSearchList.size() > 0) {
            listObj = new ArrayList<HotSearch>();
            for (SuperHotSearch superHotSearch: superHotSearchList) {
               HotSearch hotSearch = superHotSearch.getHotSearch();
               listObj.add(hotSearch);
            }
         }
      }
      if (listObj == null || listObj.size() == 0) {
         return null;
      }
      List<Long> listId = new ArrayList<>();
      for (HotSearch hotSearch : listObj) {
         listId.add(hotSearch.getId());
      }
      List<System> systemList = systemService.getSystems();
      List<SuperHotSearch> listSuper = superHotSearchService.listSuperHotSearch(listId);
      if (listSuper == null || listSuper.size() == 0) {
         for (HotSearch hotSearch : listObj) {
            hotSearch.setSystemList(systemList);
         }
      } else {
         for (HotSearch hotSearch : listObj) {
            Long id = hotSearch.getId();
            List<System> newList = new ArrayList<System>();
            // 是否有关联系统选项
            for (System dsystem : systemList) {
               System newsystem = new System();
               try {
                  PropertyUtils.copyProperties(newsystem, dsystem);
               } catch (Exception e) {
                  e.printStackTrace();
               }
               if (listSuper != null && listSuper.size() > 0) {
                  Long superSystemId = newsystem.getId();
                  for (SuperHotSearch superHotSearch : listSuper) {
                     HotSearch search = superHotSearch.getHotSearch();
                     System system = superHotSearch.getSystem();
                     // 当前专题 、当前系统
                     if (search != null && system != null && id == search.getId() && superSystemId == system.getId()) {
                        newsystem.setCheck(1);
                        break;
                     }
                  }
               }
               if (newsystem.getCheck() != 1) {
                  newsystem.setCheck(0);
               }
               newList.add(newsystem);
            }
            hotSearch.setSystemList(newList);
         }
      }
      return listObj;
   }
   @Override
   public int countList(String key, Long systemId) {
      return (Integer) hotSearchDao.excute(new HibernateCallback<Integer>() {
         public Integer doInHibernate(Session session)
               throws HibernateException {
            Query query = null;
            if (StringUtil.isNullOrEmpty(key)) {
               query = session.createQuery("select count(hs.id) from HotSearch hs");
            } else {
               query = session.createQuery("select count(hs.id) from HotSearch hs where hs.name like ?");
               query.setParameter(0, "%"+key+"%");
            }
            Long result = (Long) query.uniqueResult();
            return result.intValue();
         }
      });
   }
}