Authored by caoyan

搜索增加有货商品

1 package com.yohoufo.product.controller; 1 package com.yohoufo.product.controller;
2 2
3 -import java.text.ParseException;  
4 import java.text.SimpleDateFormat; 3 import java.text.SimpleDateFormat;
5 import java.util.Calendar; 4 import java.util.Calendar;
6 -import java.util.Date;  
7 -import java.util.List;  
8 import java.util.Map; 5 import java.util.Map;
9 6
10 import org.apache.commons.lang3.StringUtils; 7 import org.apache.commons.lang3.StringUtils;
11 -import org.apache.commons.lang3.time.DateUtils;  
12 import org.slf4j.Logger; 8 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -16,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestParam; 12 import org.springframework.web.bind.annotation.RequestParam;
17 import org.springframework.web.bind.annotation.RestController; 13 import org.springframework.web.bind.annotation.RestController;
18 14
19 -import com.alibaba.fastjson.JSONArray;  
20 import com.alibaba.fastjson.JSONObject; 15 import com.alibaba.fastjson.JSONObject;
21 import com.yoho.tools.docs.ApiOperation; 16 import com.yoho.tools.docs.ApiOperation;
22 import com.yohoufo.common.ApiResponse; 17 import com.yohoufo.common.ApiResponse;
@@ -27,7 +22,6 @@ import com.yohoufo.common.caller.UfoServiceCaller; @@ -27,7 +22,6 @@ import com.yohoufo.common.caller.UfoServiceCaller;
27 import com.yohoufo.product.helper.SearchHelpService; 22 import com.yohoufo.product.helper.SearchHelpService;
28 import com.yohoufo.product.request.ProductSearchReq; 23 import com.yohoufo.product.request.ProductSearchReq;
29 import com.yohoufo.product.request.SortIdLevel; 24 import com.yohoufo.product.request.SortIdLevel;
30 -import com.yohoufo.product.response.ProductBrandSeriesResp;  
31 import com.yohoufo.product.response.SearchBrandListResp; 25 import com.yohoufo.product.response.SearchBrandListResp;
32 import com.yohoufo.product.response.SearchProductListFilterResp; 26 import com.yohoufo.product.response.SearchProductListFilterResp;
33 import com.yohoufo.product.response.SearchProductRecommendResp; 27 import com.yohoufo.product.response.SearchProductRecommendResp;
@@ -62,7 +56,8 @@ public class ProductSearchController { @@ -62,7 +56,8 @@ public class ProductSearchController {
62 @RequestParam(value = "isSoonSale", required = false) String isSoonSale, 56 @RequestParam(value = "isSoonSale", required = false) String isSoonSale,
63 @RequestParam(value = "query", required = false)String query, 57 @RequestParam(value = "query", required = false)String query,
64 @RequestParam(value = "limit", required = false)Integer limit, 58 @RequestParam(value = "limit", required = false)Integer limit,
65 - @RequestParam(value = "page", required = false)Integer page 59 + @RequestParam(value = "page", required = false)Integer page,
  60 + @RequestParam(value = "app_version", required = true)String appVersion
66 ) { 61 ) {
67 if (type != null) { 62 if (type != null) {
68 Map<Integer,Integer> poolConfig = ufoServiceCaller.call("ufo.resource.goodsPool", Map.class); 63 Map<Integer,Integer> poolConfig = ufoServiceCaller.call("ufo.resource.goodsPool", Map.class);
@@ -82,6 +77,8 @@ public class ProductSearchController { @@ -82,6 +77,8 @@ public class ProductSearchController {
82 ProductSearchReq req = new ProductSearchReq().setOrder(order).setId(id).setPool(productPool).setBrand(brand).setMidSort(sortIdLevel.getMidSortId()).setMaxSort(sortIdLevel.getMaxSortId()) 77 ProductSearchReq req = new ProductSearchReq().setOrder(order).setId(id).setPool(productPool).setBrand(brand).setMidSort(sortIdLevel.getMidSortId()).setMaxSort(sortIdLevel.getMaxSortId())
83 .setSeries(series).setGender(gender).setSize(size).setIsSoonSale(isSoonSale).setViewNum(limit).setPage(page).setIsIdFilter(type).setSearchType(type); 78 .setSeries(series).setGender(gender).setSize(size).setIsSoonSale(isSoonSale).setViewNum(limit).setPage(page).setIsIdFilter(type).setSearchType(type);
84 searchHelpService.setQuery(query, req); 79 searchHelpService.setQuery(query, req);
  80 + //设置是否包含有货商品
  81 + searchHelpService.setContainYoho(appVersion, req);
85 LOG.info("in method=ufo.product.search.list req={}", req.toString()); 82 LOG.info("in method=ufo.product.search.list req={}", req.toString());
86 83
87 JSONObject resp = productSearchService.searchProductList(req); 84 JSONObject resp = productSearchService.searchProductList(req);
@@ -90,6 +90,8 @@ public final class SearchConstants { @@ -90,6 +90,8 @@ public final class SearchConstants {
90 String START_TIME = "startTime"; 90 String START_TIME = "startTime";
91 String END_TIME = "endTime"; 91 String END_TIME = "endTime";
92 92
  93 + String CONTAIN_YOHO = "contain_yoho";
  94 +
93 } 95 }
94 96
95 } 97 }
@@ -38,4 +38,15 @@ public class SearchHelpService { @@ -38,4 +38,15 @@ public class SearchHelpService {
38 } 38 }
39 } 39 }
40 40
  41 + /**
  42 + * 封装搜索接口参数为对象形式
  43 + * @param contain_yoho
  44 + * @return
  45 + */
  46 + public void setContainYoho(String appVersion, ProductSearchReq req) {
  47 + if(StringUtils.isNotBlank(appVersion) && appVersion.compareTo("6.8.5") >=0){
  48 + req.setContainYoho("Y");
  49 + }
  50 + }
  51 +
41 } 52 }
@@ -52,7 +52,8 @@ public class SearchParam { @@ -52,7 +52,8 @@ public class SearchParam {
52 .setViewNum(req.getViewNum()).setPage(req.getPage()) 52 .setViewNum(req.getViewNum()).setPage(req.getPage())
53 .setBrandSeries(req.getSeries()).setGender(req.getGender()).setFilterYearMonth(req.getFilterYearMonth()) 53 .setBrandSeries(req.getSeries()).setGender(req.getGender()).setFilterYearMonth(req.getFilterYearMonth())
54 .setStartTime(req.getStartTime()) 54 .setStartTime(req.getStartTime())
55 - .setEndTime(req.getEndTime()); 55 + .setEndTime(req.getEndTime())
  56 + .setContainYoho(req.getContainYoho());
56 return this; 57 return this;
57 } 58 }
58 59
@@ -250,6 +251,12 @@ public class SearchParam { @@ -250,6 +251,12 @@ public class SearchParam {
250 return this; 251 return this;
251 } 252 }
252 253
  254 + public SearchParam setContainYoho(String containYoho) {
  255 + if (StringUtils.isNotEmpty(containYoho)) {
  256 + param.put(SearchConstants.IndexNameConstant.CONTAIN_YOHO, containYoho);
  257 + }
  258 + return this;
  259 + }
253 260
254 public Map<String, Object> getParam() { 261 public Map<String, Object> getParam() {
255 return param; 262 return param;
@@ -23,7 +23,7 @@ public class ProductSearchReq { @@ -23,7 +23,7 @@ public class ProductSearchReq {
23 private Integer startTime; 23 private Integer startTime;
24 private Integer endTime; 24 private Integer endTime;
25 private Integer searchType; 25 private Integer searchType;
26 - 26 + private String containYoho;//是否包含有货商品
27 27
28 @Override 28 @Override
29 public String toString() { 29 public String toString() {
@@ -46,6 +46,7 @@ public class ProductSearchReq { @@ -46,6 +46,7 @@ public class ProductSearchReq {
46 .append("filterYearMonth",filterYearMonth) 46 .append("filterYearMonth",filterYearMonth)
47 .append("startTime",startTime) 47 .append("startTime",startTime)
48 .append("endTime",endTime) 48 .append("endTime",endTime)
  49 + .append("contain_yoho",containYoho)
49 .toString(); 50 .toString();
50 } 51 }
51 52
@@ -222,4 +223,17 @@ public class ProductSearchReq { @@ -222,4 +223,17 @@ public class ProductSearchReq {
222 } 223 }
223 return this; 224 return this;
224 } 225 }
  226 +
  227 +
  228 + public String getContainYoho() {
  229 + return containYoho;
  230 + }
  231 +
  232 +
  233 + public ProductSearchReq setContainYoho(String containYoho) {
  234 + this.containYoho = containYoho;
  235 + return this;
  236 + }
  237 +
  238 +
225 } 239 }
1 package com.yohoufo.product.service.impl; 1 package com.yohoufo.product.service.impl;
2 2
  3 +import java.math.BigDecimal;
3 import java.text.SimpleDateFormat; 4 import java.text.SimpleDateFormat;
4 import java.util.ArrayList; 5 import java.util.ArrayList;
  6 +import java.util.Collections;
  7 +import java.util.Comparator;
5 import java.util.Date; 8 import java.util.Date;
6 import java.util.List; 9 import java.util.List;
7 import java.util.Map; 10 import java.util.Map;
@@ -19,6 +22,8 @@ import org.springframework.util.CollectionUtils; @@ -19,6 +22,8 @@ import org.springframework.util.CollectionUtils;
19 import com.alibaba.fastjson.JSON; 22 import com.alibaba.fastjson.JSON;
20 import com.alibaba.fastjson.JSONArray; 23 import com.alibaba.fastjson.JSONArray;
21 import com.alibaba.fastjson.JSONObject; 24 import com.alibaba.fastjson.JSONObject;
  25 +import com.google.common.collect.Lists;
  26 +import com.yoho.core.common.utils.DateUtil;
22 import com.yoho.core.rest.client.ServiceCaller; 27 import com.yoho.core.rest.client.ServiceCaller;
23 import com.yoho.core.rest.exception.ServiceNotAvaibleException; 28 import com.yoho.core.rest.exception.ServiceNotAvaibleException;
24 import com.yohoufo.common.cache.Cachable; 29 import com.yohoufo.common.cache.Cachable;
@@ -158,6 +163,10 @@ public class ProductSearchServiceImpl implements ProductSearchService { @@ -158,6 +163,10 @@ public class ProductSearchServiceImpl implements ProductSearchService {
158 // 处理图片,封面图设置 163 // 处理图片,封面图设置
159 String default_images = fillProductImgUrl(MapUtils.getString(product, "default_images", "")); 164 String default_images = fillProductImgUrl(MapUtils.getString(product, "default_images", ""));
160 product.replace("default_images", default_images); 165 product.replace("default_images", default_images);
  166 + //处理有货价格
  167 + if(product.getString("is_yoho").equals("Y")) {
  168 + handlerPrice(product);
  169 + }
161 } 170 }
162 } 171 }
163 172
@@ -463,4 +472,83 @@ public class ProductSearchServiceImpl implements ProductSearchService { @@ -463,4 +472,83 @@ public class ProductSearchServiceImpl implements ProductSearchService {
463 } 472 }
464 return data; 473 return data;
465 } 474 }
  475 +
  476 + public void handlerPrice(JSONObject product) {
  477 + product.put("price", getIntCeilPrice(product.getDouble("price")));
  478 + JSONArray planList = product.getJSONArray("product_price_plan_list");
  479 + if(CollectionUtils.isEmpty(planList)){
  480 + return;
  481 + }
  482 + JSONObject effectPlan = getEffectProductPricePlan(planList);
  483 + //没有符合的价格计划,直接返回
  484 + if(null==effectPlan){
  485 + return;
  486 + }
  487 + if(null!=effectPlan.get("current_saleprice")){
  488 + product.replace("sales_price", getIntCeilPrice(effectPlan.getDouble("current_saleprice")));
  489 + }
  490 + if(null!=effectPlan.get("vip1_price")){
  491 + product.replace("vip1_price", effectPlan.getDouble("vip1_price"));
  492 + }
  493 +
  494 + if(null!=effectPlan.get("vip2_price")){
  495 + product.replace("vip2_price", effectPlan.getDouble("vip2_price"));
  496 + }
  497 +
  498 + if(null!=effectPlan.get("vip3_price")){
  499 + product.replace("vip3_price", effectPlan.getDouble("vip3_price"));
  500 + }
  501 +
  502 + if(null!=effectPlan.get("vip_discount_type")){
  503 + product.replace("vip_discount_type", effectPlan.getInteger("vip_discount_type"));
  504 + }
  505 + if(null!=effectPlan.get("student_price") && !"Y".equalsIgnoreCase(product.getString("is_student_price"))){
  506 + product.replace("student_price", product.getBigDecimal("sales_price").multiply(new BigDecimal("0.9")));
  507 + }
  508 + }
  509 +
  510 + /**
  511 + * 获取一个生效的变价计划
  512 + * @param list
  513 + * @return
  514 + */
  515 + private JSONObject getEffectProductPricePlan(JSONArray planList){
  516 + if(CollectionUtils.isEmpty(planList)){
  517 + return null;
  518 + }
  519 + //先计算匹配到的价格计划,然后根据匹配到的价格计划求优先级
  520 + int currentTime= DateUtil.getCurrentTimeSecond();
  521 + logger.info("currentTime: {}", currentTime);
  522 + List<JSONObject> fitProductPricePlanList=Lists.newArrayList();
  523 + for (int i=0; i < planList.size(); i++) {
  524 + JSONObject item = planList.getJSONObject(i);
  525 + //当前时间大于等于生效时间且当前时间小于结束时间或没有结束时间的
  526 + if(currentTime>=item.getIntValue("effect_time") && (currentTime<=item.getIntValue("end_time")||item.getIntValue("end_time")==0)){
  527 + fitProductPricePlanList.add(item);
  528 + }
  529 + }
  530 + if(!CollectionUtils.isEmpty(fitProductPricePlanList)){
  531 + //在多个价格计划都满足的情况下,生效时间越大越靠前
  532 + Collections.sort(fitProductPricePlanList, new Comparator<JSONObject>() {
  533 + @Override
  534 + public int compare(JSONObject o1, JSONObject o2) {
  535 + if(o2.getInteger("effect_time").compareTo(o1.getInteger("effect_time"))==0
  536 + && null!=o2.getInteger("create_time") && null!=o1.getInteger("create_time")){
  537 + return o2.getInteger("create_time").compareTo(o1.getInteger("create_time"));
  538 + }
  539 + return o2.getInteger("effect_time").compareTo(o1.getInteger("effect_time"));
  540 + }
  541 + });
  542 + return fitProductPricePlanList.get(0);
  543 + }
  544 + return null;
  545 + }
  546 +
  547 + //获取取整的价格
  548 + public static Integer getIntCeilPrice(Double price){
  549 + if(null == price) {
  550 + return 0;
  551 + }
  552 + return Double.valueOf(Math.ceil(price)).intValue();
  553 + }
466 } 554 }