fanli/src/main/java/com/yeshi/fanli/controller/admin/HotSearchAdminController.java
@@ -7,22 +7,26 @@ import javax.annotation.Resource; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; 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.tag.PageEntity; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.Utils; import org.yeshi.utils.JsonUtil; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/v1/search") @@ -110,9 +114,169 @@ } } @RequestMapping(value = "updateHotSearch", method = RequestMethod.POST) public void updateSection(HotSearch hotSearch,PrintWriter out){ hotSearchService.updateHotSearch(hotSearch); out.print(JsonUtil.loadTrueResult("修改成功")); @RequestMapping(value = "updateHotSearch") public void updateSection(String callback, HotSearch hotSearch, PrintWriter out){ try { if (hotSearch.getId() == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("ID不能为空")); return; } if (StringUtil.isNullOrEmpty(hotSearch.getName())) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("热门搜索词不能为空")); return; } hotSearchService.updateHotSearch(hotSearch); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("修改成功")); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("修改失败")); e.printStackTrace(); } } /** * 查询列表- 新后台 * @param callback * @param pageIndex * @param platform * @param packages * @param key * @param out */ @RequestMapping(value = "newHotSearchList") public void newHotSearchList(String callback, Integer pageIndex, String key, Long systemId, PrintWriter out) { if (pageIndex == null || pageIndex < 1) { pageIndex = 1; } int pageSize = Constant.PAGE_SIZE; try { List<HotSearch> list = hotSearchService.listQuery(pageIndex - 1, pageSize, key, systemId); if (list == null || list.size() == 0) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无更多数据")); return; } int count = hotSearchService.countList(key, systemId); int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1; PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage); JSONObject data = new JSONObject(); data.put("pe", pe); data.put("result_list", list); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("查询失败")); e.printStackTrace(); } } /** * 新增-新后台 * @param callback * @param hotSearch * @param out */ @RequestMapping(value = "saveAdd") public void saveAdd(String callback, HotSearch hotSearch, PrintWriter out) { if (hotSearch == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("热门搜索不能为空")); return; } if (StringUtil.isNullOrEmpty(hotSearch.getName())) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("热门搜索词不能为空")); return; } try { hotSearch.setId(null); hotSearchService.addHotSearch(hotSearch); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("添加成功")); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("添加失败")); e.printStackTrace(); } } /** * 批量删除-新后台 * @param callback * @param idArray * @param out */ @RequestMapping(value = "delete") public void delete(String callback, String idArray, PrintWriter out){ if (StringUtil.isNullOrEmpty(idArray)) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择操作的数据")); return; } try { Gson gson = new Gson(); long[] hsids = gson.fromJson(idArray, new TypeToken<long[]>() {}.getType()); if (hsids == null || hsids.length == 0) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("未检测到删除的数据")); return; } Integer type = hotSearchService.deleteHotSearch(hsids); if(type==null){ JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("删除成功")); }else{ JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("删除失败")); } } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("删除失败")); e.printStackTrace(); } } @RequestMapping(value = "setSystem") public void setSystem(String callback, String type, Long id, Long systemId, PrintWriter out) { if (id == null || systemId == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择操作的数据")); return; } try { System system = systemService.getById(systemId); List<SuperHotSearch> list = superHotSearchService.getHotSearchSystem(id, systemId); if (list == null || list.size() == 0) { superHotSearchService.addSuper(systemId, system); JSONObject data = new JSONObject(); data.put("check", 1); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } else { Integer count = superHotSearchService.deleteSuper(id, systemId); if (count > 0) { JSONObject data = new JSONObject(); data.put("check", 0); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } else { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("修改失败")); } } } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("修改异常")); e.printStackTrace(); } } } fanli/src/main/java/com/yeshi/fanli/entity/bus/search/HotSearch.java
@@ -1,6 +1,7 @@ package com.yeshi.fanli.entity.bus.search; import java.io.Serializable; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; @@ -8,39 +9,45 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name = "yeshi_ec_hot_search") public class HotSearch implements Serializable{ public class HotSearch implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; @Column(name="`orderby`") @Column(name = "`orderby`") private Integer orderby; @Column(name="`name`") @Column(name = "`name`") private String name; private Long createtime; @Transient // 系统关联列表 private List<com.yeshi.fanli.entity.system.System> systemList; public HotSearch() { } public HotSearch(Long id) { super(); this.id = id; } public HotSearch(Integer orderby, String name) { super(); this.orderby = orderby; this.name = name; } public Long getCreatetime() { return createtime; } public void setCreatetime(Long createtime) { this.createtime = createtime; } @@ -48,20 +55,33 @@ public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getOrderby() { return orderby; } public void setOrderby(Integer orderby) { this.orderby = orderby; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<com.yeshi.fanli.entity.system.System> getSystemList() { return systemList; } public void setSystemList(List<com.yeshi.fanli.entity.system.System> systemList) { this.systemList = systemList; } } 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(); } }); } } fanli/src/main/java/com/yeshi/fanli/service/impl/config/SuperHotSearchSerivceImpl.java
@@ -20,6 +20,7 @@ import com.yeshi.fanli.entity.system.System; import com.yeshi.fanli.service.inter.config.SuperHotSearchService; import com.yeshi.fanli.service.inter.config.SystemService; import com.yeshi.fanli.util.StringUtil; @Service public class SuperHotSearchSerivceImpl implements SuperHotSearchService { @@ -67,7 +68,23 @@ strat, count, new Serializable[] { id, key }); } @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public List<SuperHotSearch> listSuperHotSearch(List<Long> list) { List<SuperHotSearch> listObj = (List<SuperHotSearch>) superHotSearchDao.excute(new HibernateCallback<List<SuperHotSearch>>() { public List<SuperHotSearch> doInHibernate(Session session) throws HibernateException { Query query = session.createQuery("from SuperHotSearch srs where srs.hotSearch.id in (:array)"); query.setParameterList("array",list); return query.list(); } }); return listObj; } public Integer deleteSuperHotSearch(final long hsid, final String platform, final String packageName) { return (Integer) superHotSearchDao.excute(new HibernateCallback<Integer>() { @@ -101,4 +118,48 @@ return getSuperHotSearchBySystemId(id); } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List<SuperHotSearch> getHotSearchSystem(Long id, Long systemId) { List<SuperHotSearch> listObj = (List<SuperHotSearch>) superHotSearchDao.excute(new HibernateCallback<List<SuperHotSearch>>() { public List<SuperHotSearch> doInHibernate(Session session) throws HibernateException { Query query = session.createQuery("from SuperHotSearch shs " + " where shs.hotSearch.id=? and shs.system.id=?"); query.setLong(0, id); query.setLong(1,systemId); return query.list(); } }); return listObj; } @Override public Integer deleteSuper(Long id, Long systemId) { return (Integer) superHotSearchDao.excute(new HibernateCallback<Integer>() { public Integer doInHibernate(Session session) throws HibernateException { Transaction transaction = session.beginTransaction(); Query query = session .createQuery("delete SuperHotSearch shs " + " where shs.hotSearch.id=? and shs.system.id=?"); query.setLong(0, id); query.setLong(1, systemId); int i = query.executeUpdate(); transaction.commit(); return i; } }); } @Override public void addSuper(Long id, System system) { SuperHotSearch superHotSearch = new SuperHotSearch(); HotSearch hotSearch = new HotSearch(); hotSearch.setId(id); superHotSearch.setHotSearch(hotSearch); superHotSearch.setSystem(system); superHotSearchDao.create(superHotSearch); } } fanli/src/main/java/com/yeshi/fanli/service/impl/config/SystemServiceImpl.java
@@ -56,4 +56,11 @@ public System getSystemCache(String platform, String packages) { return getSystem(platform, packages); } @Override public System getById(long id) { return systemDao.find(System.class, id); } } fanli/src/main/java/com/yeshi/fanli/service/inter/config/HotSearchService.java
@@ -7,8 +7,7 @@ public interface HotSearchService { List<HotSearchAdmin> getHotSearchs(int index, String platform, String packages, String key); List<HotSearchAdmin> getHotSearchs(int index, String platform, String packages, String key); HotSearch getHotSearch(long id); @@ -20,4 +19,8 @@ int getCount(String platform, String packages, String key); public List<HotSearch> listQuery(int start, int count, String key, Long systemId) throws Exception; int countList(String key, Long systemId); } fanli/src/main/java/com/yeshi/fanli/service/inter/config/SuperHotSearchService.java
@@ -2,9 +2,8 @@ import java.util.List; import org.springframework.cache.annotation.Cacheable; import com.yeshi.fanli.entity.bus.su.search.SuperHotSearch; import com.yeshi.fanli.entity.system.System; public interface SuperHotSearchService { @@ -23,4 +22,31 @@ public void addSuperHotSearch(long hsid, String platform, String packageName); public List<SuperHotSearch> listSuperHotSearch(List<Long> list); /** * 查询系统 * @param id * @param systemId * @return */ public List<SuperHotSearch> getHotSearchSystem(Long id, Long systemId); /** * 删除系统 * @param id * @param systemId * @return */ public Integer deleteSuper(Long id, Long systemId); /** * 新增系统 * @param id * @param system */ public void addSuper(Long id, System system); } fanli/src/main/java/com/yeshi/fanli/service/inter/config/SystemService.java
@@ -13,4 +13,11 @@ public List<System> getSystems(); /** * 通过id查询 * @param id * @return */ public System getById(long id); }