admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/config/HotSearchServiceImpl.java
@@ -1,333 +1,138 @@
package com.yeshi.fanli.service.impl.config;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.goods.HotSearchDao;
import com.yeshi.fanli.entity.admin.HotSearchAdmin;
import com.yeshi.fanli.entity.bus.search.HotSearch;
import com.yeshi.fanli.entity.bus.su.search.SuperHotSearch;
import com.yeshi.fanli.entity.system.System;
import com.yeshi.fanli.service.inter.config.HotSearchService;
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
public class HotSearchServiceImpl implements HotSearchService {
   @Resource
   private HotSearchDao hotSearchDao;
   @Resource
   private SystemService systemService;
   @Resource
   private SuperHotSearchService superHotSearchService;
   @SuppressWarnings("unchecked")
   public List<HotSearchAdmin> getHotSearchs(int index, String platform,
         String packages, String key) {
      final int strat = index * Constant.PAGE_SIZE;
      platform = Utils.getMap().get(platform);
      com.yeshi.fanli.entity.system.System system = systemService.getSystem(platform, packages);
      List<HotSearch> hotSearchList=null;
      List<SuperHotSearch> superHotSearchList=null;
      Map<Long,HotSearchAdmin> hotSearchAdminMap=new HashMap<Long, HotSearchAdmin>();
      List<HotSearchAdmin> hotSearchAdmin=new ArrayList<HotSearchAdmin>();
      if(system == null){
         if("".equals(key.trim())){
            hotSearchList = (List<HotSearch>) hotSearchDao.excute(new HibernateCallback<List<HotSearch>>(){
                  public List<HotSearch> doInHibernate(Session session)
                        throws HibernateException {
                     Query query = session.createQuery(" from HotSearch hs");
                     query.setFirstResult(strat);
                     query.setMaxResults(Constant.PAGE_SIZE);
                     return query.list();
                  }
            });
         }else{
            final String likekey = "%"+key+"%";
            hotSearchList= (List<HotSearch>) hotSearchDao.excute(new HibernateCallback<List<HotSearch>>(){
                     public List<HotSearch> doInHibernate(Session session)
                           throws HibernateException {
                        Query query = session.createQuery(" from HotSearch hs where hs.name like ? ");
                        query.setFirstResult(strat);
                        query.setMaxResults(Constant.PAGE_SIZE);
                        query.setParameter(0, likekey);
                        return query.list();
                     }
            });
         }
         List<Long> hsIdList = new ArrayList<Long>();
         for (HotSearch rs : hotSearchList) {
            hsIdList.add(rs.getId());
         }
         superHotSearchList = superHotSearchService.getSuperHotSearchsByHotSearchs(hsIdList);
         Map<Long,List<System>> superMap = new HashMap<Long, List<System>>();
         Long rsId=null;
         List<System> systemList=null;
         for (SuperHotSearch srs : superHotSearchList) {
            rsId = srs.getHotSearch().getId();
            systemList = superMap.get(rsId);
            if(systemList==null){
               systemList=new ArrayList<System>();
            }
            systemList.add(srs.getSystem());
            superMap.put(rsId,systemList);
         }
         HotSearchAdmin rsa=null;
         for (HotSearch HotSearch : hotSearchList) {
            long id = HotSearch.getId();
            rsa = hotSearchAdminMap.get(id);
            if(rsa==null){
               rsa=new HotSearchAdmin();
            }
            List<System> list = superMap.get(id);
            if(list != null){
               rsa.getSystemList().addAll(superMap.get(id));
            }
            rsa.setHotSearch(HotSearch);
            hotSearchAdminMap.put(id, rsa);
         }
      }else if(system != null){
         if("".equals(key.trim())){
            superHotSearchList = superHotSearchService.getSuperHotSearchBySystemId(system.getId(),strat, Constant.PAGE_SIZE);
         }else{
            String likekey = "%"+key+"%";
            superHotSearchList = superHotSearchService.getSuperHotSearchBySystemId(system.getId(),strat, Constant.PAGE_SIZE,likekey);
         }
         List<Long> hsIdList =new ArrayList<Long>();
         for (SuperHotSearch superHotSearch : superHotSearchList) {
            HotSearch HotSearch = superHotSearch.getHotSearch();
            hsIdList.add(HotSearch.getId());
         }
         superHotSearchList = superHotSearchService.getSuperHotSearchsByHotSearchs(hsIdList);
         HotSearchAdmin rsa=null;
         for (SuperHotSearch superHotSearch : superHotSearchList) {
            HotSearch HotSearch = superHotSearch.getHotSearch();
            rsa = hotSearchAdminMap.get(HotSearch.getId());
            if(rsa==null){
               rsa=new HotSearchAdmin(HotSearch);
            }
            rsa.getSystemList().add(superHotSearch.getSystem());
            hotSearchAdminMap.put(HotSearch.getId(), rsa);
         }
      }
      Map map = Utils.orderBy(hotSearchAdminMap);
      Collection<HotSearchAdmin> values = map.values();
      hotSearchAdmin.addAll(values);
      return hotSearchAdmin;
   }
   public HotSearch getHotSearch(long id) {
      return hotSearchDao.find(HotSearch.class, id);
   }
   public void addHotSearch(HotSearch hotSearch) {
      hotSearch.setCreatetime(java.lang.System.currentTimeMillis());
      hotSearchDao.create(hotSearch);
   }
   public Integer deleteHotSearch(final long[] hsids) {
      return (Integer) hotSearchDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session)
               throws HibernateException {
            session.getTransaction().begin();
            try {
               for (long hsid : hsids) {
                  session.delete(new HotSearch(hsid));
                  Query query = session.createQuery("delete from SuperHotSearch shs where shs.hotSearch.id = ?");
                  query.setParameter(0, hsid);
                  query.executeUpdate();
               }
               session.flush();
               session.getTransaction().commit();
            } catch (Exception e) {
               e.printStackTrace();
               session.getTransaction().rollback();
               return 1;
            }
            return null;
         }
      });
   }
   public void updateHotSearch(HotSearch hotSearch) {
      HotSearch updateSearch = new HotSearch(hotSearch.getId());
      updateSearch.setName(hotSearch.getName());
      updateSearch.setOrderby(hotSearch.getOrderby());
      hotSearchDao.update(hotSearch);
   }
   public int getCount(String platform, String packages, final String key) {
      platform = Utils.getMap().get(platform);
      final System system = systemService.getSystem(platform, packages);
      return (Integer) hotSearchDao.excute(new HibernateCallback<Integer>() {
         public Integer doInHibernate(Session session)
               throws HibernateException {
            String k = "%"+key+"%";
            Query query;
            if(system==null){
//               Query query = session.createSQLQuery("select count(a.name) from (select name from yeshi_ec_hot_search where `name` like ?) a");
               query = session.createQuery("select count(hs.id) from HotSearch hs where hs.name like ?");
               query.setParameter(0, k);
            }else{
//               Query query =session.createSQLQuery("select count(a.name) from (select hs.name from yeshi_ec_super_hot_search shs left  join yeshi_ec_hot_search hs on shs.hotSearch_id=hs.id where shs.system_id=? and hs.name like ?) a");
               query = session.createQuery("select count(shs.id) from SuperHotSearch shs where shs.system.id=? and shs.hotSearch.name like ?");
               query.setParameter(0, system.getId());
               query.setParameter(1, k);
            }
               Long result = (Long) query.uniqueResult();
               int intValue = result.intValue();
               return intValue;
         }
      });
   }
   @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();
         }
      });
   }
}
package com.yeshi.fanli.service.impl.config;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.yeshi.fanli.entity.SystemEnum;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.goods.HotSearchMapper;
import com.yeshi.fanli.entity.bus.search.HotSearch;
import com.yeshi.fanli.entity.bus.su.search.SuperHotSearch;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.HotSearchService;
import com.yeshi.fanli.service.inter.config.SuperHotSearchService;
@Service
public class HotSearchServiceImpl implements HotSearchService {
   @Resource
   private HotSearchMapper hotSearchMapper;
   @Resource
   private BusinessSystemService businessSystemService;
   @Resource
   private SuperHotSearchService superHotSearchService;
   public void addHotSearch(HotSearch hotSearch) {
      hotSearch.setCreatetime(java.lang.System.currentTimeMillis());
      hotSearchMapper.insert(hotSearch);
   }
   public void deleteHotSearch(Long[] hsids) {
      for (Long hsid : hsids) {
         // superHotSearchService.deleteSuper(id, systemId)
         hotSearchMapper.deleteByPrimaryKey(hsid);
      }
   }
   public void updateHotSearch(HotSearch hotSearch) {
      HotSearch updateSearch = new HotSearch(hotSearch.getId());
      updateSearch.setName(hotSearch.getName());
      updateSearch.setOrderby(hotSearch.getOrderby());
      updateSearch.setUseType(hotSearch.getUseType());
      hotSearchMapper.updateByPrimaryKeySelective(updateSearch);
   }
   @Override
   public List<HotSearch> listQuery(int start, int count, String key, Long systemId, Integer useType, SystemEnum system) throws Exception {
      List<HotSearch> listObj = hotSearchMapper.listQuery(start, count, key, systemId, useType,system);
      if (listObj == null || listObj.size() == 0) {
         return null;
      }
      List<Long> listId = new ArrayList<>();
      for (HotSearch hotSearch : listObj) {
         listId.add(hotSearch.getId());
      }
      List<BusinessSystem> systemList = businessSystemService.getBusinessSystems();
      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<BusinessSystem> newList = new ArrayList<BusinessSystem>();
            // 是否有关联系统选项
            for (BusinessSystem dsystem : systemList) {
               BusinessSystem newsystem = new BusinessSystem();
               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();
                     BusinessSystem s = superHotSearch.getSystem();
                     // 当前专题 、当前系统
                     if (search != null && system != null && id == search.getId() && superSystemId == s.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, Integer useType, SystemEnum system) {
      return hotSearchMapper.countQuery(key, systemId, useType,system);
   }
   @Override
   @Cacheable(value = "crgCache", key = "'getHotSearchCache-' + #useType+'-'+#system")
   public List<HotSearch> getHotSearchCache(Integer useType, SystemEnum system) {
      return hotSearchMapper.getHotSearchList(useType,system);
   }
}