Authored by caoyan

搜索增加有货商品

package com.yohoufo.product.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -16,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.tools.docs.ApiOperation;
import com.yohoufo.common.ApiResponse;
... ... @@ -27,7 +22,6 @@ import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.product.helper.SearchHelpService;
import com.yohoufo.product.request.ProductSearchReq;
import com.yohoufo.product.request.SortIdLevel;
import com.yohoufo.product.response.ProductBrandSeriesResp;
import com.yohoufo.product.response.SearchBrandListResp;
import com.yohoufo.product.response.SearchProductListFilterResp;
import com.yohoufo.product.response.SearchProductRecommendResp;
... ... @@ -62,7 +56,8 @@ public class ProductSearchController {
@RequestParam(value = "isSoonSale", required = false) String isSoonSale,
@RequestParam(value = "query", required = false)String query,
@RequestParam(value = "limit", required = false)Integer limit,
@RequestParam(value = "page", required = false)Integer page
@RequestParam(value = "page", required = false)Integer page,
@RequestParam(value = "app_version", required = true)String appVersion
) {
if (type != null) {
Map<Integer,Integer> poolConfig = ufoServiceCaller.call("ufo.resource.goodsPool", Map.class);
... ... @@ -82,6 +77,8 @@ public class ProductSearchController {
ProductSearchReq req = new ProductSearchReq().setOrder(order).setId(id).setPool(productPool).setBrand(brand).setMidSort(sortIdLevel.getMidSortId()).setMaxSort(sortIdLevel.getMaxSortId())
.setSeries(series).setGender(gender).setSize(size).setIsSoonSale(isSoonSale).setViewNum(limit).setPage(page).setIsIdFilter(type).setSearchType(type);
searchHelpService.setQuery(query, req);
//设置是否包含有货商品
searchHelpService.setContainYoho(appVersion, req);
LOG.info("in method=ufo.product.search.list req={}", req.toString());
JSONObject resp = productSearchService.searchProductList(req);
... ...
... ... @@ -89,6 +89,8 @@ public final class SearchConstants {
String START_TIME = "startTime";
String END_TIME = "endTime";
String CONTAIN_YOHO = "contain_yoho";
}
... ...
... ... @@ -37,5 +37,16 @@ public class SearchHelpService {
req.setQuery(query);
}
}
/**
* 封装搜索接口参数为对象形式
* @param contain_yoho
* @return
*/
public void setContainYoho(String appVersion, ProductSearchReq req) {
if(StringUtils.isNotBlank(appVersion) && appVersion.compareTo("6.8.5") >=0){
req.setContainYoho("Y");
}
}
}
... ...
... ... @@ -52,7 +52,8 @@ public class SearchParam {
.setViewNum(req.getViewNum()).setPage(req.getPage())
.setBrandSeries(req.getSeries()).setGender(req.getGender()).setFilterYearMonth(req.getFilterYearMonth())
.setStartTime(req.getStartTime())
.setEndTime(req.getEndTime());
.setEndTime(req.getEndTime())
.setContainYoho(req.getContainYoho());
return this;
}
... ... @@ -250,6 +251,12 @@ public class SearchParam {
return this;
}
public SearchParam setContainYoho(String containYoho) {
if (StringUtils.isNotEmpty(containYoho)) {
param.put(SearchConstants.IndexNameConstant.CONTAIN_YOHO, containYoho);
}
return this;
}
public Map<String, Object> getParam() {
return param;
... ...
... ... @@ -23,7 +23,7 @@ public class ProductSearchReq {
private Integer startTime;
private Integer endTime;
private Integer searchType;
private String containYoho;//是否包含有货商品
@Override
public String toString() {
... ... @@ -46,6 +46,7 @@ public class ProductSearchReq {
.append("filterYearMonth",filterYearMonth)
.append("startTime",startTime)
.append("endTime",endTime)
.append("contain_yoho",containYoho)
.toString();
}
... ... @@ -222,4 +223,17 @@ public class ProductSearchReq {
}
return this;
}
public String getContainYoho() {
return containYoho;
}
public ProductSearchReq setContainYoho(String containYoho) {
this.containYoho = containYoho;
return this;
}
}
... ...
package com.yohoufo.product.service.impl;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
... ... @@ -19,6 +22,8 @@ import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.exception.ServiceNotAvaibleException;
import com.yohoufo.common.cache.Cachable;
... ... @@ -158,6 +163,10 @@ public class ProductSearchServiceImpl implements ProductSearchService {
// 处理图片,封面图设置
String default_images = fillProductImgUrl(MapUtils.getString(product, "default_images", ""));
product.replace("default_images", default_images);
//处理有货价格
if(product.getString("is_yoho").equals("Y")) {
handlerPrice(product);
}
}
}
... ... @@ -463,4 +472,83 @@ public class ProductSearchServiceImpl implements ProductSearchService {
}
return data;
}
public void handlerPrice(JSONObject product) {
product.put("price", getIntCeilPrice(product.getDouble("price")));
JSONArray planList = product.getJSONArray("product_price_plan_list");
if(CollectionUtils.isEmpty(planList)){
return;
}
JSONObject effectPlan = getEffectProductPricePlan(planList);
//没有符合的价格计划,直接返回
if(null==effectPlan){
return;
}
if(null!=effectPlan.get("current_saleprice")){
product.replace("sales_price", getIntCeilPrice(effectPlan.getDouble("current_saleprice")));
}
if(null!=effectPlan.get("vip1_price")){
product.replace("vip1_price", effectPlan.getDouble("vip1_price"));
}
if(null!=effectPlan.get("vip2_price")){
product.replace("vip2_price", effectPlan.getDouble("vip2_price"));
}
if(null!=effectPlan.get("vip3_price")){
product.replace("vip3_price", effectPlan.getDouble("vip3_price"));
}
if(null!=effectPlan.get("vip_discount_type")){
product.replace("vip_discount_type", effectPlan.getInteger("vip_discount_type"));
}
if(null!=effectPlan.get("student_price") && !"Y".equalsIgnoreCase(product.getString("is_student_price"))){
product.replace("student_price", product.getBigDecimal("sales_price").multiply(new BigDecimal("0.9")));
}
}
/**
* 获取一个生效的变价计划
* @param list
* @return
*/
private JSONObject getEffectProductPricePlan(JSONArray planList){
if(CollectionUtils.isEmpty(planList)){
return null;
}
//先计算匹配到的价格计划,然后根据匹配到的价格计划求优先级
int currentTime= DateUtil.getCurrentTimeSecond();
logger.info("currentTime: {}", currentTime);
List<JSONObject> fitProductPricePlanList=Lists.newArrayList();
for (int i=0; i < planList.size(); i++) {
JSONObject item = planList.getJSONObject(i);
//当前时间大于等于生效时间且当前时间小于结束时间或没有结束时间的
if(currentTime>=item.getIntValue("effect_time") && (currentTime<=item.getIntValue("end_time")||item.getIntValue("end_time")==0)){
fitProductPricePlanList.add(item);
}
}
if(!CollectionUtils.isEmpty(fitProductPricePlanList)){
//在多个价格计划都满足的情况下,生效时间越大越靠前
Collections.sort(fitProductPricePlanList, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
if(o2.getInteger("effect_time").compareTo(o1.getInteger("effect_time"))==0
&& null!=o2.getInteger("create_time") && null!=o1.getInteger("create_time")){
return o2.getInteger("create_time").compareTo(o1.getInteger("create_time"));
}
return o2.getInteger("effect_time").compareTo(o1.getInteger("effect_time"));
}
});
return fitProductPricePlanList.get(0);
}
return null;
}
//获取取整的价格
public static Integer getIntCeilPrice(Double price){
if(null == price) {
return 0;
}
return Double.valueOf(Math.ceil(price)).intValue();
}
}
... ...