Authored by wangnan9279

图库

package com.yoho.search.restapi.others;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.service.IImageRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @Author: wangnan
... ... @@ -8,4 +19,18 @@ import org.springframework.stereotype.Controller;
*/
@Controller
public class ImageRepertoryController {
@Autowired
private IImageRepertoryService imageRepertoryService;
/**
* 搜索图库列表
*/
@SearchCacheAble(cacheInMinute = 1, cacheName = "IMAGEREPERTORY")
@RequestMapping(method = RequestMethod.GET, value = "/imageRepertory")
@ResponseBody
public SearchApiResult searchImageRepertory(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return imageRepertoryService.searchImageRepertoryList(paramMap);
}
}
... ...
... ... @@ -120,6 +120,18 @@ public class SearchRequestParams {
public static final String SHOPS_PARAM_SHOPSTYPE = "shopsType";
public static final String SHOPS_PARAM_STATUS = "status";
//ImageRepertory index param
public static final String IMAGEREPERTORY_PARAM_ID = "id";
public static final String IMAGEREPERTORY_PARAM_ITEMID = "itemId";
public static final String IMAGEREPERTORY_PARAM_CHANNEL = "channel";
public static final String IMAGEREPERTORY_PARAM_SEASON = "season";
public static final String IMAGEREPERTORY_PARAM_IMAGESIZE = "imageSize";
public static final String IMAGEREPERTORY_PARAM_IMAGENAME = "imageName";
public static final String IMAGEREPERTORY_PARAM_IMAGETYPE = "imageType";
public static final String IMAGEREPERTORY_PARAM_ITEMTYPE = "itemType";
public static final String IMAGEREPERTORY_PARAM_PORT = "port";
public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag";
//Promotion index field
... ...
package com.yoho.search.service.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.service.IImageRepertoryService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
... ... @@ -10,11 +32,107 @@ import java.util.Map;
* @Author: wangnan
* @Date: Created in 2018/4/16
*/
public class ImageRepertoryServiceImpl implements IImageRepertoryService{
@Service
public class ImageRepertoryServiceImpl extends BaseService implements IImageRepertoryService, ApplicationEventPublisherAware {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationEventPublisher publisher;
@Autowired
private SearchCommonService searchCommonService;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@Override
public SearchApiResult searchImageRepertoryList(Map<String, String> paramMap) {
return null;
try {
logger.info("[class=ImageRepertoryServiceImpl][func=searchImageRepertoryList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1.构造查询参数
SearchParam searchParam = new SearchParam();
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0) {
return new SearchApiResult().setCode(400).setMessage("分页参数错误");
}
if (pageSize > 100) {
pageSize = 100;
}
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
//2.配置Filter
BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
searchParam.setFiter(boolFilter);
final String indexName = ISearchConstants.INDEX_NAME_IMAGE_REPERTORY;
//3.查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
SearchApiResult searchApiResult = new SearchApiResult();
if (searchResult == null) {
return searchApiResult.setCode(500);
}
// 8.构造返回结果并加入缓存
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("imageRepertory_list", getImageRepertoryMap(searchResult.getResultList()));
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getEventName(), EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("imageRepertory", paramMap, e);
}
}
private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ID);
boolFilter = boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE);
boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_PORT);
boolFilter = generalHandler(paramMap, boolFilter);
return boolFilter;
}
private List<Map<String, Object>> getImageRepertoryMap(List<Map<String, Object>> resultList) {
List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> map : resultList) {
Map<String, Object> productMap = getImageRepertoryMap(map);
returnList.add(productMap);
}
return returnList;
}
private Map<String, Object> getImageRepertoryMap(Map<String, Object> map) {
Map<String, Object> productMap = new LinkedHashMap<>();
productMap.put("id", map.get("id") == null ? "" : map.get("id"));
productMap.put("itemId", map.get("itemId") == null ? "" : map.get("itemId"));
productMap.put("channel", map.get("channel") == null ? "" : map.get("channel"));
productMap.put("season", map.get("season") == null ? "" : map.get("season"));
productMap.put("imageUrl", map.get("imageUrl") == null ? "" : map.get("imageUrl"));
productMap.put("imageSize", map.get("imageSize") == null ? "" : map.get("imageSize"));
productMap.put("imageName", map.get("imageName") == null ? "" : map.get("imageName"));
productMap.put("imageType", map.get("imageType") == null ? "" : map.get("imageType"));
productMap.put("itemType", map.get("itemType") == null ? "" : map.get("itemType"));
productMap.put("remarks", map.get("remarks") == null ? "" : map.get("remarks"));
productMap.put("port", map.get("port") == null ? "" : map.get("port"));
productMap.put("menu", map.get("menu") == null ? "" : map.get("menu"));
productMap.put("menuName", map.get("menuName") == null ? "" : map.get("menuName"));
return productMap;
}
}
... ...