Commit 12786648 authored by Hong's avatar Hong
Browse files

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

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