Commit 12786648 authored by Hong's avatar Hong
Browse files

优化地址,商品详情 的查询速度

parent b2295166
......@@ -12,6 +12,7 @@ import java.util.List;
@Service
public class LitemallRegionService {
@Resource
private LitemallRegionMapper regionMapper;
......
......@@ -13,6 +13,7 @@ import java.util.List;
**/
@Component
public class GetRegionService {
@Autowired
private LitemallRegionService regionService;
......
......@@ -28,11 +28,12 @@ import java.util.concurrent.*;
@RestController
@RequestMapping("/wx/address")
@Validated
public class WxAddressController extends GetRegionService{
public class WxAddressController extends GetRegionService {
private final Log logger = LogFactory.getLog(WxAddressController.class);
@Autowired
private LitemallAddressService addressService;
@Autowired
private LitemallRegionService regionService;
......@@ -62,23 +63,24 @@ public class WxAddressController extends GetRegionService{
addressVo.put("name", address.getName());
addressVo.put("mobile", address.getMobile());
addressVo.put("isDefault", address.getIsDefault());
Callable<String> provinceCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName();
Callable<String> cityCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getCityId())).findAny().orElse(null).getName();
Callable<String> areaCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getAreaId())).findAny().orElse(null).getName();
Callable<String> provinceCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName();
Callable<String> cityCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getCityId())).findAny().orElse(null).getName();
Callable<String> areaCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getAreaId())).findAny().orElse(null).getName();
FutureTask<String> provinceNameCallableTask = new FutureTask<>(provinceCallable);
FutureTask<String> cityNameCallableTask = new FutureTask<>(cityCallable);
FutureTask<String> areaNameCallableTask = new FutureTask<>(areaCallable);
executorService.submit(provinceNameCallableTask);
executorService.submit(cityNameCallableTask);
executorService.submit(areaNameCallableTask);
String detailedAddress="";
String detailedAddress = "";
try {
String province=provinceNameCallableTask.get();
String city =cityNameCallableTask.get();
String area =areaNameCallableTask.get();
String province = provinceNameCallableTask.get();
String city = cityNameCallableTask.get();
String area = areaNameCallableTask.get();
String addr = address.getAddress();
detailedAddress = province + city + area + " " + addr;
}catch (Exception e){
}
catch (Exception e) {
e.printStackTrace();
}
addressVo.put("detailedAddress", detailedAddress);
......
......@@ -10,6 +10,7 @@ import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.GetRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -22,6 +23,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/**
* 商品服务
......@@ -34,31 +36,48 @@ public class WxGoodsController {
@Autowired
private LitemallGoodsService goodsService;
@Autowired
private LitemallGoodsProductService productService;
@Autowired
private LitemallIssueService goodsIssueService;
@Autowired
private LitemallGoodsAttributeService goodsAttributeService;
@Autowired
private LitemallBrandService brandService;
@Autowired
private LitemallCommentService commentService;
@Autowired
private LitemallUserService userService;
@Autowired
private LitemallCollectService collectService;
@Autowired
private LitemallFootprintService footprintService;
@Autowired
private LitemallCategoryService categoryService;
@Autowired
private LitemallSearchHistoryService searchHistoryService;
@Autowired
private LitemallGoodsSpecificationService goodsSpecificationService;
@Autowired
private LitemallGrouponRulesService rulesService;
private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);
private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(16, 16, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
/**
* 商品详情
......@@ -76,28 +95,31 @@ public class WxGoodsController {
LitemallGoods info = goodsService.findById(id);
// 商品属性
List<LitemallGoodsAttribute> goodsAttributeList = goodsAttributeService.queryByGid(id);
Callable<List> goodsAttributeListCallable = () -> goodsAttributeService.queryByGid(id);
// 商品规格
// 返回的是定制的GoodsSpecificationVo
Object specificationList = goodsSpecificationService.getSpecificationVoList(id);
// 商品规格 返回的是定制的GoodsSpecificationVo
Callable<Object> objectCallable = () -> goodsSpecificationService.getSpecificationVoList(id);
// 商品规格对应的数量和价格
List<LitemallGoodsProduct> productList = productService.queryByGid(id);
Callable<List> productListCallable = () -> productService.queryByGid(id);
// 商品问题,这里是一些通用问题
List<LitemallIssue> issue = goodsIssueService.query();
Callable<List> issueCallable = () -> goodsIssueService.query();
// 商品品牌商
Callable<LitemallBrand> brandCallable = ()->{
Integer brandId = info.getBrandId();
LitemallBrand brand = null;
LitemallBrand brand;
if (brandId == 0) {
brand = new LitemallBrand();
} else {
brand = brandService.findById(info.getBrandId());
}
return brand;
};
// 评论
Callable<Map> commentsCallable = () -> {
List<LitemallComment> comments = commentService.queryGoodsByGid(id, 0, 2);
List<Map<String, Object>> commentsVo = new ArrayList<>(comments.size());
int commentCount = commentService.countGoodsByGid(id, 0, 2);
......@@ -115,9 +137,11 @@ public class WxGoodsController {
Map<String, Object> commentList = new HashMap<>();
commentList.put("count", commentCount);
commentList.put("data", commentsVo);
return commentList;
};
//团购信息
List<LitemallGrouponRules> rules = rulesService.queryByGoodsId(id);
Callable<List> grouponRulesCallable = () ->rulesService.queryByGoodsId(id);
// 用户收藏
int userHasCollect = 0;
......@@ -125,24 +149,47 @@ public class WxGoodsController {
userHasCollect = collectService.count(userId, id);
}
// 记录用户的足迹
// 记录用户的足迹 异步处理
if (userId != null) {
executorService.execute(()->{
LitemallFootprint footprint = new LitemallFootprint();
footprint.setUserId(userId);
footprint.setGoodsId(id);
footprintService.add(footprint);
});
}
FutureTask<List> goodsAttributeListTask = new FutureTask<>(goodsAttributeListCallable);
FutureTask<Object> objectCallableTask = new FutureTask<>(objectCallable);
FutureTask<List> productListCallableTask = new FutureTask<>(productListCallable);
FutureTask<List> issueCallableTask = new FutureTask<>(issueCallable);
FutureTask<Map> commentsCallableTsk = new FutureTask<>(commentsCallable);
FutureTask<LitemallBrand> brandCallableTask = new FutureTask<>(brandCallable);
FutureTask<List> grouponRulesCallableTask = new FutureTask<>(grouponRulesCallable);
executorService.submit(goodsAttributeListTask);
executorService.submit(objectCallableTask);
executorService.submit(productListCallableTask);
executorService.submit(issueCallableTask);
executorService.submit(commentsCallableTsk);
executorService.submit(brandCallableTask);
executorService.submit(grouponRulesCallableTask);
Map<String, Object> data = new HashMap<>();
try {
data.put("info", info);
data.put("userHasCollect", userHasCollect);
data.put("issue", issue);
data.put("comment", commentList);
data.put("specificationList", specificationList);
data.put("productList", productList);
data.put("attribute", goodsAttributeList);
data.put("brand", brand);
data.put("groupon", rules);
data.put("issue", issueCallableTask.get());
data.put("comment", commentsCallableTsk.get());
data.put("specificationList", objectCallableTask.get());
data.put("productList", productListCallableTask.get());
data.put("attribute", goodsAttributeListTask.get());
data.put("brand", brandCallableTask.get());
data.put("groupon", grouponRulesCallableTask.get());
}
catch (Exception e) {
e.printStackTrace();
}
//商品分享图片地址
data.put("shareImage", info.getShareUrl());
......@@ -195,7 +242,12 @@ public class WxGoodsController {
* @return 根据条件搜素的商品详情
*/
@GetMapping("list")
public Object list(Integer categoryId, Integer brandId, String keyword, Boolean isNew, Boolean isHot,
public Object list(
Integer categoryId,
Integer brandId,
String keyword,
Boolean isNew,
Boolean isHot,
@LoginUser Integer userId,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment