Authored by Lixiaodi

热销列表、发售日历

... ... @@ -55,6 +55,10 @@ public class ControllerCacheAop implements ApplicationContextAware{
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
public void restControllerPointcut() {
}
@Pointcut("within(@org.springframework.stereotype.Service *)")
public void servicePointcut() {
}
/**
* a pointcut for all class with annotation : {@link Cachable }
... ... @@ -62,14 +66,13 @@ public class ControllerCacheAop implements ApplicationContextAware{
@Pointcut("@annotation(com.yohoufo.common.cache.Cachable)")
public void cacheAnnotationPointCut() {
}
/**
* Operations
* 先从cache中,找不到则调用controller原来的实现(一般是调用服务),如果调用服务有异常 {@link ServiceNotAvaibleException},则从二级缓存中取
*/
@Around("(controllerPointcut()|| restControllerPointcut()) && cacheAnnotationPointCut() ")
@Around("(controllerPointcut()|| restControllerPointcut() || servicePointcut()) && cacheAnnotationPointCut() ")
public Object cacheAop(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
... ...
... ... @@ -7,7 +7,9 @@ import java.util.List;
public interface IUserFavoriteDao {
List<UserFavorite> selectValidFavoriteByUid(@Param("tableIndex")String tableIndex, @Param("uid")int uid );
List<UserFavorite> selectUserFavorite(@Param("tableIndex")String tableIndex, @Param("uid")int uid ,@Param("productIdList") List<Integer> productIdList);
List<UserFavorite> selectValidFavoriteByUid(@Param("tableIndex")String tableIndex, @Param("uid")int uid);
void insert(@Param("tableIndex")String tableIndex, @Param("userFavorite")UserFavorite userFavorite);
... ...
... ... @@ -21,6 +21,15 @@
where uid = #{uid} and valid_status = 1
</select>
<select id="selectUserFavorite" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from favorite_${tableIndex}
where uid = #{uid} and valid_status = 1 and product_id in
<foreach item="item" index="index" collection="productIdList" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<insert id="insert">
INSERT INTO favorite_${tableIndex} (uid, valid_status,product_id, create_time, update_time)
... ...
package com.yohoufo.product.controller;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.JSONObject;
import com.yoho.tools.docs.ApiOperation;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.annotation.IgnoreSession;
import com.yohoufo.common.annotation.IgnoreSignature;
import com.yohoufo.common.cache.Cachable;
import com.yohoufo.common.cache.ControllerCacheAop;
import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.product.helper.SearchHelpService;
import com.yohoufo.product.request.ProductSearchReq;
... ... @@ -12,19 +26,6 @@ import com.yohoufo.product.response.SearchBrandListResp;
import com.yohoufo.product.response.SearchProductListFilterResp;
import com.yohoufo.product.response.SearchProductRecommendResp;
import com.yohoufo.product.service.ProductSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.annotation.IgnoreSession;
import com.yohoufo.common.cache.Cachable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class ProductSearchController {
... ... @@ -37,6 +38,8 @@ public class ProductSearchController {
private SearchHelpService searchHelpService;
@Autowired
private UfoServiceCaller ufoServiceCaller;
@Autowired
private ControllerCacheAop cacheAop;
@ApiOperation(name = "ufo.product.search.list", desc="首页商品推荐")
@RequestMapping(params = "method=ufo.product.search.list")
... ... @@ -147,4 +150,42 @@ public class ProductSearchController {
return new ApiResponse.ApiResponseBuilder().code(200).message("product.search.seriesList").data(resp).build();
}
@IgnoreSignature
@ApiOperation(name = "ufo.product.search.saleCalendar", desc="销售日历列表")
@RequestMapping(params = "method=ufo.product.search.saleCalendar")
@IgnoreSession
public ApiResponse searchSaleCalendar(@RequestParam(value = "uid", required = false)Integer uid,
@RequestParam(value = "page", required = false)Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
if (page == null || page < 1) {
page = 1;
}
if (limit == null || limit < 1) {
limit = 10;
}
LOG.info("in method=ufo.product.search.saleCalendar uid={}, page={}, limit={}", uid, page, limit);
JSONObject productJson = productSearchService.searchSaleCalendar(page, limit);
productSearchService.processUserFavoriteProductList(productJson, uid);
return new ApiResponse.ApiResponseBuilder().code(200).message("product.search.saleCalendar").data(productJson).build();
}
@IgnoreSignature
@ApiOperation(name = "ufo.product.search.hotSale", desc="热销列表")
@RequestMapping(params = "method=ufo.product.search.hotSale")
@IgnoreSession
@Cachable(expire = 180)
public ApiResponse searchHotSale(@RequestParam(value = "page", required = false)Integer page,
@RequestParam(value = "limit", required = false)Integer limit) {
if (page == null || page < 1) {
page = 1;
}
if (limit == null || limit < 1) {
limit = 10;
}
LOG.info("in method=ufo.product.search.hotSale page={}, limit={}", page, limit);
JSONObject resp = productSearchService.searchHotSale(page, limit);
return new ApiResponse.ApiResponseBuilder().code(200).message("product.search.hotSale").data(resp).build();
}
}
\ No newline at end of file
... ...
... ... @@ -3,7 +3,9 @@ package com.yohoufo.product.service;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yohoufo.common.cache.Cachable;
import com.yohoufo.product.request.ProductSearchReq;
import com.yohoufo.product.request.SortIdLevel;
import com.yohoufo.product.response.ProductBrandSeriesResp;
... ... @@ -25,4 +27,10 @@ public interface ProductSearchService {
List<ProductBrandSeriesResp> searchSeriesList();
JSONObject searchSaleCalendar(Integer page, Integer limit);
JSONObject searchHotSale(Integer page, Integer limit);
void processUserFavoriteProductList(JSONObject productJSON, Integer uid);
}
... ...
... ... @@ -9,6 +9,8 @@ import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.yohoufo.common.cache.Cachable;
import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.common.helper.ImageUrlAssist;
import com.yohoufo.common.utils.UfoStringUtils;
import com.yohoufo.dal.product.ProductMapper;
... ... @@ -37,8 +39,10 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.exception.ServiceNotAvaibleException;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.product.service.ProductSearchService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
@Service
public class ProductSearchServiceImpl implements ProductSearchService {
... ... @@ -50,6 +54,9 @@ public class ProductSearchServiceImpl implements ProductSearchService {
@Autowired
private ServiceCaller serviceCaller;
@Autowired
private UfoServiceCaller ufoServiceCaller;
@Autowired
private ProductSortMapper productSortMapper;
... ... @@ -70,6 +77,10 @@ public class ProductSearchServiceImpl implements ProductSearchService {
public static final String PRODUCT_RECOMMEND_LIST_URL = "/yohosearch/ufo/recommendList.json";
public static final String SALE_CALENDAR_LIST_URL = "/yohosearch/ufo/saleCalendarProductList.json";
public static final String HOT_SALE_LIST_URL = "/yohosearch/ufo/hotSaleProductList.json";
private JSONObject search(Map<String, Object> searchParams, String url) {
logger.info("begin invoke search.productList, param is:{}, url is :{}", searchParams, url);
... ... @@ -131,6 +142,47 @@ public class ProductSearchServiceImpl implements ProductSearchService {
product.replace("default_images", default_images);
}
}
@SuppressWarnings("unchecked")
@Override
public void processUserFavoriteProductList(JSONObject productJSON, Integer uid) {
JSONArray productList = productJSON.getJSONArray("product_list");
if (CollectionUtils.isEmpty(productList) || uid == null || uid < 1) {
return;
}
List<Integer> productIdList = new ArrayList<>();
// 遍历商品列表
for (int i = 0; i < productList.size(); i++) {
JSONObject product = productList.getJSONObject(i);
if (null == product) {
continue;
}
productIdList.add(MapUtils.getInteger(product, "id", 0));
}
try {
List<Integer> favorite = ufoServiceCaller.call("ufo.user.selectFavoriteList", List.class, uid,
productIdList);
if(org.apache.commons.collections.CollectionUtils.isEmpty(favorite)) {
return;
}
// 遍历商品列表
for (int i = 0; i < productList.size(); i++) {
JSONObject product = productList.getJSONObject(i);
if (null == product) {
continue;
}
// 处理图片,封面图设置
Integer productId = MapUtils.getInteger(product, "id", 0);
if (productId != 0 && favorite.contains(productId)) {
product.put("isFavorite", 1);
} else {
product.put("isFavorite", 0);
}
}
} catch (Exception e) {
logger.error("销售日历,处理用户收藏出错!", e);
}
}
private static String fillProductImgUrl(String imgUrl) {
if (StringUtils.isBlank(imgUrl)) {
... ... @@ -332,4 +384,29 @@ public class ProductSearchServiceImpl implements ProductSearchService {
}
return respList;
}
@Override
@Cachable(expire=180)
public JSONObject searchSaleCalendar(Integer page, Integer limit) {
ProductSearchReq req = new ProductSearchReq().setViewNum(limit).setPage(page);
SearchParam searchParam = new SearchParam().buildPageSearchParam(req);
JSONObject data = search(searchParam.getParam(), SALE_CALENDAR_LIST_URL);
// 将图片的相对路径转成绝对路径
if (null != data) {
processProductList(data.getJSONArray("product_list"));
}
return data;
}
@Override
public JSONObject searchHotSale(Integer page, Integer limit) {
ProductSearchReq req = new ProductSearchReq().setViewNum(limit).setPage(page);
SearchParam searchParam = new SearchParam().buildPageSearchParam(req);
JSONObject data = search(searchParam.getParam(), HOT_SALE_LIST_URL);
// 将图片的相对路径转成绝对路径
if (null != data) {
processProductList(data.getJSONArray("product_list"));
}
return data;
}
}
... ...
... ... @@ -36,6 +36,13 @@ public class FavoriteController {
@Autowired
private UfoServiceCaller serviceCaller;
@RequestMapping(params = "method=ufo.user.selectFavoriteList")
public List<Integer> userSelectFavorite(Integer uid, List<Integer> productIdList) throws ServiceException {
logger.info("Begin call ufo.user.favoriteList with uid is {}, productIdList is{}", uid, productIdList);
return favoriteService.findFavorite(uid, productIdList);
}
/**
* 用户收藏list
* @param vo
... ...
... ... @@ -14,4 +14,6 @@ public interface IFavoriteService {
boolean isFavorite(FavoriteRequestVO vo);
int numFavorite(FavoriteRequestVO vo);
List<Integer> findFavorite(Integer uid, List<Integer> productIdList);
}
... ...
... ... @@ -16,6 +16,7 @@ import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service("favoriteServiceImpl")
public class FavoriteServiceImpl implements IFavoriteService {
... ... @@ -66,6 +67,11 @@ public class FavoriteServiceImpl implements IFavoriteService {
userFavoriteDao.updateFavoriteInvalid(getTableIndex(vo.getUid()),vo.getUid(), vo.getProductId(),ts);
}
@Override
public List<Integer> findFavorite(Integer uid, List<Integer> productIdList){
List<UserFavorite> favoriteList = userFavoriteDao.selectUserFavorite(getTableIndex(uid), uid, productIdList);
return favoriteList.stream().map(UserFavorite::getProductId).collect(Collectors.toList());
}
public List<String> listFavorite(FavoriteRequestVO vo){
List<String> favoriteProducts = new ArrayList<>();
... ...