|
|
@@ -2,6 +2,7 @@ package com.uas.platform.b2c.common.search.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.platform.b2c.common.search.constant.SearchParam;
|
|
|
import com.uas.platform.b2c.common.account.model.User;
|
|
|
import com.uas.platform.b2c.common.search.dao.SearchHistoryDao;
|
|
|
import com.uas.platform.b2c.common.search.model.SearchHistory;
|
|
|
@@ -27,6 +28,8 @@ import com.uas.platform.b2c.common.search.util.Sort;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.mobile.device.DeviceResolver;
|
|
|
import org.springframework.mobile.device.LiteDeviceResolver;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -48,27 +51,26 @@ import java.util.Map.Entry;
|
|
|
@Service
|
|
|
public class SearcherServiceImpl implements SearcherService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private ComponentInfoDao componentInfoDao;
|
|
|
+ @Autowired
|
|
|
+ private ComponentInfoDao componentInfoDao;
|
|
|
|
|
|
- @Autowired
|
|
|
- private ComponentGoodsDao componentGoodsDao;
|
|
|
+ @Autowired
|
|
|
+ private ComponentGoodsDao componentGoodsDao;
|
|
|
|
|
|
- @Autowired
|
|
|
- private SearchService searchService;
|
|
|
+ @Autowired
|
|
|
+ private SearchService searchService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private BrandDao brandDao;
|
|
|
+ @Autowired
|
|
|
+ private BrandDao brandDao;
|
|
|
|
|
|
- @Autowired
|
|
|
- private GoodsDao goodsDao;
|
|
|
+ @Autowired
|
|
|
+ private GoodsDao goodsDao;
|
|
|
|
|
|
- @Autowired
|
|
|
- private SearchHistoryDao searchHistoryDao;
|
|
|
+ @Autowired
|
|
|
+ private SearchHistoryDao searchHistoryDao;
|
|
|
|
|
|
- private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
-
|
|
|
- private final DeviceResolver deviceResolver = new LiteDeviceResolver();
|
|
|
+ private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+ private final DeviceResolver deviceResolver = new LiteDeviceResolver();
|
|
|
|
|
|
private final int DEFAULT_PAGE = 1;
|
|
|
|
|
|
@@ -109,7 +111,7 @@ public class SearcherServiceImpl implements SearcherService {
|
|
|
@Override
|
|
|
public ModelMap searchBrand(String keyword, String field) {
|
|
|
ModelMap map = new ModelMap();
|
|
|
- SPage<Map<String, List<Integer>>> brands = null;
|
|
|
+ SPage<Map<String, Object>> brands = null;
|
|
|
try {
|
|
|
brands = searchService.getBrands(keyword, DEFAULT_PAGE, DEFAULT_SIZE);
|
|
|
} catch (SearchException e) {
|
|
|
@@ -120,119 +122,134 @@ public class SearcherServiceImpl implements SearcherService {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 智能搜索器件
|
|
|
- *
|
|
|
- * @param keyword
|
|
|
- * @return
|
|
|
- * @throws InterruptedException
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public ModelMap searchComponent(String keyword, PageParams page) {
|
|
|
- ModelMap map = new ModelMap();
|
|
|
- Map<String, Object> results = new HashMap<>();
|
|
|
- try {
|
|
|
- results = searchService.getComponentIds(keyword, convertPageParams(page));
|
|
|
- } catch (SearchException e) {
|
|
|
- throwSystemException(e);
|
|
|
- }
|
|
|
- List<Long> componentIds = (List<Long>) results.get("componentIds");
|
|
|
- if (!CollectionUtils.isEmpty(componentIds)) {
|
|
|
- List<ComponentInfo> components = componentInfoDao
|
|
|
- .findByIdsInOrder(componentIds.toArray(new Long[componentIds.size()]));
|
|
|
- map.put("components", components);// 当前页器件内容
|
|
|
- }
|
|
|
- map.put("total", results.get("total"));// 搜索结果总数
|
|
|
- map.put("page", results.get("page"));// 当前页面
|
|
|
- map.put("size", results.get("size"));// 每页个数
|
|
|
- return map;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 首字母搜索器件
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ * @throws InterruptedException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public Page<ComponentInfo> searchComponentByFirst(String keyword, PageParams page) {
|
|
|
+ Pageable pageable = new PageInfo(page);
|
|
|
+ return componentInfoDao.findByFirst(keyword,pageable);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 智能搜索批次
|
|
|
- *
|
|
|
- * @param keyword
|
|
|
- * @param page
|
|
|
- * @return
|
|
|
- * @throws InterruptedException
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- @Override
|
|
|
- public ModelMap searchComponentGoods(String keyword, PageParams page, HttpServletRequest request) {
|
|
|
- if (this.deviceResolver.resolveDevice(request).isMobile()) {
|
|
|
- SystemSession.setUser((User) request.getSession().getAttribute("user"));
|
|
|
- if (null != SystemSession.getUser() && null != SystemSession.getUser().getUserUU()) {
|
|
|
- SearchHistory searchHistory = null;
|
|
|
- List<SearchHistory> searchHistories = searchHistoryDao.findByKeywordAndUserUUAndUserEnuu(keyword, SystemSession.getUser().getUserUU(), SystemSession.getUser().getEnterprise() != null ? SystemSession.getUser().getEnterprise().getUu() : null);
|
|
|
- if (searchHistories.size() > 0) {
|
|
|
- searchHistory = searchHistories.get(0);
|
|
|
- } else {
|
|
|
- searchHistory = new SearchHistory();
|
|
|
- }
|
|
|
- searchHistory.setKeyword(keyword);
|
|
|
- searchHistory.setSearchTime(new Date(System.currentTimeMillis()));
|
|
|
- searchHistory.setUserUU(SystemSession.getUser().getUserUU());
|
|
|
- searchHistory.setUserEnuu(SystemSession.getUser().getEnterprise() != null ? SystemSession.getUser().getEnterprise().getUu() : null);
|
|
|
- searchHistoryDao.save(searchHistory);
|
|
|
- searchHistories = searchHistoryDao.findByUserUUAndUserEnuuOrderBySearchTimeDesc(SystemSession.getUser().getUserUU(), SystemSession.getUser().getEnterprise() != null ? SystemSession.getUser().getEnterprise().getUu() : null);
|
|
|
- if (searchHistories.size() > 10) {
|
|
|
- searchHistoryDao.delete(searchHistories.subList(10, searchHistories.size()));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- ModelMap map = new ModelMap();
|
|
|
- Map<String, Object> results = null;
|
|
|
- //Map<String, Object> results = new HashMap<>();
|
|
|
- try {
|
|
|
- results = searchService.getGoodsIds(keyword, convertPageParams(page));
|
|
|
- } catch (SearchException e) {
|
|
|
- throwSystemException(e);
|
|
|
- }
|
|
|
- //统计精准匹配到品牌的数量(便于展示唯一的品牌)
|
|
|
- ModelMap brandMap = searchBrand(keyword, null);
|
|
|
- List<Map<String, Object>> brands = (List) brandMap.get("brands");
|
|
|
- if (!CollectionUtils.isEmpty(brands)) {
|
|
|
- for (Map<String, Object> brand : brands) {
|
|
|
- if (keyword.equalsIgnoreCase(brand.get("nameCn").toString()) || keyword.equalsIgnoreCase(brand.get("nameEn").toString())) {
|
|
|
- map.put("brands", brand);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if ((int) results.get("total") == 0) {
|
|
|
- map.put("total", results.get("total"));// 搜索结果总数
|
|
|
- map.put("page", results.get("page"));// 当前页面
|
|
|
- map.put("size", results.get("size"));// 每页个数
|
|
|
- map.put("components", new ArrayList<>());// 当前页器件内容
|
|
|
- return map;
|
|
|
- }
|
|
|
- List<Integer> componentIds = (List<Integer>) results.get("componentIds");
|
|
|
- List<Integer> goodsIds = (List<Integer>) results.get("goodsIds");
|
|
|
- List<ComponentGoods> components = new ArrayList<>();
|
|
|
- if (!CollectionUtils.isEmpty(componentIds)) {
|
|
|
- for (int i = 0; i < componentIds.size(); i++) {
|
|
|
- Long cid = null;
|
|
|
- if (!StringUtils.isEmpty
|
|
|
- (componentIds.get(i))) {
|
|
|
- cid = Long.valueOf(componentIds.get(i).toString());
|
|
|
- }
|
|
|
- Long gid = goodsIds.get(i) == null ? 0L : Long.valueOf(goodsIds.get(i).toString());
|
|
|
- if ((cid == null) && (gid != 0L)) {
|
|
|
- Goods goods = goodsDao.findOne(gid);
|
|
|
- if (goods != null) {
|
|
|
- components.add(new ComponentGoods(goods));
|
|
|
- }
|
|
|
- } else if (cid != null) {
|
|
|
- ComponentGoods componentGoods = componentGoodsDao.findByCmpIdAndGoId(cid, gid);
|
|
|
- if (componentGoods != null) {
|
|
|
- components.add(componentGoods);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- map.put("components", components);// 当前页器件内容
|
|
|
- }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 智能搜索器件
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ * @throws InterruptedException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public ModelMap searchComponent(String keyword, PageParams page) {
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ Map<String, Object> results = new HashMap<>();
|
|
|
+ try {
|
|
|
+ results = searchService.getComponentIds(keyword, convertPageParams(page));
|
|
|
+ } catch (SearchException e) {
|
|
|
+ throwSystemException(e);
|
|
|
+ }
|
|
|
+ List<Long> componentIds = (List<Long>) results.get("componentIds");
|
|
|
+ if (!CollectionUtils.isEmpty(componentIds)) {
|
|
|
+ List<ComponentInfo> components = componentInfoDao
|
|
|
+ .findByIdsInOrder(componentIds.toArray(new Long[componentIds.size()]));
|
|
|
+ map.put("components", components);// 当前页器件内容
|
|
|
+ }
|
|
|
+ map.put("total", results.get("total"));// 搜索结果总数
|
|
|
+ map.put("page", results.get("page"));// 当前页面
|
|
|
+ map.put("size", results.get("size"));// 每页个数
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 智能搜索批次
|
|
|
+ *
|
|
|
+ * @param keyword
|
|
|
+ * @param page
|
|
|
+ * @return
|
|
|
+ * @throws InterruptedException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public ModelMap searchComponentGoods(String keyword, PageParams page , HttpServletRequest request) {
|
|
|
+ if (this.deviceResolver.resolveDevice(request).isMobile()) {
|
|
|
+ SystemSession.setUser((User)request.getSession().getAttribute("user"));
|
|
|
+ if (null != SystemSession.getUser() && null!= SystemSession.getUser().getUserUU()){
|
|
|
+ SearchHistory searchHistory = null;
|
|
|
+ List<SearchHistory> searchHistories = searchHistoryDao.findByKeywordAndUserUUAndUserEnuu(keyword,SystemSession.getUser().getUserUU(),SystemSession.getUser().getEnterprise() != null ?SystemSession.getUser().getEnterprise().getUu():null);
|
|
|
+ if (searchHistories.size()>0){
|
|
|
+ searchHistory = searchHistories.get(0);
|
|
|
+ }else {
|
|
|
+ searchHistory = new SearchHistory();
|
|
|
+ }
|
|
|
+ searchHistory.setKeyword(keyword);
|
|
|
+ searchHistory.setSearchTime(new Date(System.currentTimeMillis()));
|
|
|
+ searchHistory.setUserUU(SystemSession.getUser().getUserUU());
|
|
|
+ searchHistory.setUserEnuu(SystemSession.getUser().getEnterprise() != null ?SystemSession.getUser().getEnterprise().getUu():null);
|
|
|
+ searchHistoryDao.save(searchHistory);
|
|
|
+ searchHistories = searchHistoryDao.findByUserUUAndUserEnuuOrderBySearchTimeDesc(SystemSession.getUser().getUserUU(),SystemSession.getUser().getEnterprise() != null ?SystemSession.getUser().getEnterprise().getUu():null);
|
|
|
+ if (searchHistories.size()>10){
|
|
|
+ searchHistoryDao.delete(searchHistories.subList(10,searchHistories.size()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ Map<String,Object> results = null;
|
|
|
+ //Map<String, Object> results = new HashMap<>();
|
|
|
+ try {
|
|
|
+ results = searchService.getGoodsIds(keyword, convertPageParams(page));
|
|
|
+ } catch (SearchException e) {
|
|
|
+ throwSystemException(e);
|
|
|
+ }
|
|
|
+ //统计精准匹配到品牌的数量(便于展示唯一的品牌)
|
|
|
+ ModelMap brandMap = searchBrand(keyword,null);
|
|
|
+ List<Map<String, Object>> brands = (List)brandMap.get("brands");
|
|
|
+ if (!CollectionUtils.isEmpty(brands)) {
|
|
|
+ for (Map<String, Object> brand : brands){
|
|
|
+ if (keyword.equalsIgnoreCase(brand.get("nameCn").toString()) || keyword.equalsIgnoreCase(brand.get("nameEn").toString())){
|
|
|
+ map.put("brands",brand);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ((int)results.get("total") == 0){
|
|
|
+ map.put("total", results.get("total"));// 搜索结果总数
|
|
|
+ map.put("page", results.get("page"));// 当前页面
|
|
|
+ map.put("size", results.get("size"));// 每页个数
|
|
|
+ map.put("components", new ArrayList<>());// 当前页器件内容
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ List<Integer> componentIds = (List<Integer>)results.get("componentIds");
|
|
|
+ List<Integer> goodsIds = (List<Integer>)results.get("goodsIds");
|
|
|
+ List<ComponentGoods> components = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(componentIds)) {
|
|
|
+ for (int i = 0 ;i < componentIds.size() ; i++){
|
|
|
+ Long cid = null;
|
|
|
+ if(!StringUtils.isEmpty
|
|
|
+ (componentIds.get(i))) {
|
|
|
+ cid = Long.valueOf(componentIds.get(i).toString());
|
|
|
+ }
|
|
|
+ Long gid = goodsIds.get(i) == null ? 0L : Long.valueOf(goodsIds.get(i).toString());
|
|
|
+ if((cid == null) && (gid != 0L)) {
|
|
|
+ Goods goods = goodsDao.findOne(gid);
|
|
|
+ if(goods != null) {
|
|
|
+ components.add(new ComponentGoods(goods));
|
|
|
+ }
|
|
|
+ }else if(cid != null){
|
|
|
+ ComponentGoods componentGoods = componentGoodsDao.findByCmpIdAndGoId(cid, gid);
|
|
|
+ if (componentGoods != null){
|
|
|
+ components.add(componentGoods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ map.put("components", components);// 当前页器件内容
|
|
|
+ }
|
|
|
|
|
|
map.put("total", results.get("total"));// 搜索结果总数
|
|
|
map.put("page", results.get("page"));// 当前页面
|
|
|
@@ -425,14 +442,20 @@ public class SearcherServiceImpl implements SearcherService {
|
|
|
return brands;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 捕获SearchException抛出SystemException
|
|
|
- *
|
|
|
- * @param e
|
|
|
- * @return
|
|
|
- */
|
|
|
- private void throwSystemException(SearchException e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
- throw new SystemException(e.getMessage());
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public int deleteSearchHistoryByuserUUAndenUU() {
|
|
|
+ return searchHistoryDao.deleteByUserUUAndUserEnuu(SystemSession.getUser().getUserUU(),
|
|
|
+ SystemSession.getUser().getEnterprise() == null ? null : SystemSession.getUser().getEnterprise().getUu() );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 捕获SearchException抛出SystemException
|
|
|
+ *
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void throwSystemException(SearchException e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ throw new SystemException(e.getMessage());
|
|
|
+ }
|
|
|
}
|