Commit 878a921a authored by Junling Bu's avatar Junling Bu
Browse files
parents 50028820 950b176f
...@@ -12,9 +12,17 @@ import java.util.List; ...@@ -12,9 +12,17 @@ import java.util.List;
@Service @Service
public class LitemallRegionService { public class LitemallRegionService {
@Resource @Resource
private LitemallRegionMapper regionMapper; private LitemallRegionMapper regionMapper;
public List<LitemallRegion> getAll(){
LitemallRegionExample example = new LitemallRegionExample();
byte b = 4;
example.or().andTypeNotEqualTo(b);
return regionMapper.selectByExample(example);
}
public List<LitemallRegion> queryByPid(Integer parentId) { public List<LitemallRegion> queryByPid(Integer parentId) {
LitemallRegionExample example = new LitemallRegionExample(); LitemallRegionExample example = new LitemallRegionExample();
example.or().andPidEqualTo(parentId); example.or().andPidEqualTo(parentId);
......
package org.linlinjava.litemall.wx.service;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author zhy
* @date 2019-01-17 23:07
**/
@Component
public class GetRegionService {
@Autowired
private LitemallRegionService regionService;
private static List<LitemallRegion> litemallRegions;
protected List<LitemallRegion> getLitemallRegions() {
if(litemallRegions==null){
createRegion();
}
return litemallRegions;
}
private synchronized void createRegion(){
if (litemallRegions == null) {
litemallRegions = regionService.getAll();
}
}
}
...@@ -5,9 +5,11 @@ import org.apache.commons.logging.LogFactory; ...@@ -5,9 +5,11 @@ import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.RegexUtil; import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.core.util.ResponseUtil; import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallAddress; import org.linlinjava.litemall.db.domain.LitemallAddress;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.service.LitemallAddressService; import org.linlinjava.litemall.db.service.LitemallAddressService;
import org.linlinjava.litemall.db.service.LitemallRegionService; import org.linlinjava.litemall.db.service.LitemallRegionService;
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.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -18,6 +20,7 @@ import java.util.ArrayList; ...@@ -18,6 +20,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.*;
/** /**
* 用户收货地址服务 * 用户收货地址服务
...@@ -25,14 +28,21 @@ import java.util.Map; ...@@ -25,14 +28,21 @@ import java.util.Map;
@RestController @RestController
@RequestMapping("/wx/address") @RequestMapping("/wx/address")
@Validated @Validated
public class WxAddressController { 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;
private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(6);
private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 6, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
/** /**
* 用户收货地址列表 * 用户收货地址列表
* *
...@@ -46,17 +56,33 @@ public class WxAddressController { ...@@ -46,17 +56,33 @@ public class WxAddressController {
} }
List<LitemallAddress> addressList = addressService.queryByUid(userId); List<LitemallAddress> addressList = addressService.queryByUid(userId);
List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size()); List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size());
List<LitemallRegion> regionList = getLitemallRegions();
for (LitemallAddress address : addressList) { for (LitemallAddress address : addressList) {
Map<String, Object> addressVo = new HashMap<>(); Map<String, Object> addressVo = new HashMap<>();
addressVo.put("id", address.getId()); addressVo.put("id", address.getId());
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());
String province = regionService.findById(address.getProvinceId()).getName(); Callable<String> provinceCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName();
String city = regionService.findById(address.getCityId()).getName(); Callable<String> cityCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getCityId())).findAny().orElse(null).getName();
String area = regionService.findById(address.getAreaId()).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 = "";
try {
String province = provinceNameCallableTask.get();
String city = cityNameCallableTask.get();
String area = areaNameCallableTask.get();
String addr = address.getAddress(); String addr = address.getAddress();
String detailedAddress = province + city + area + " " + addr; detailedAddress = province + city + area + " " + addr;
}
catch (Exception e) {
e.printStackTrace();
}
addressVo.put("detailedAddress", detailedAddress); addressVo.put("detailedAddress", detailedAddress);
addressVoList.add(addressVo); addressVoList.add(addressVo);
......
...@@ -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,
......
...@@ -79,6 +79,7 @@ public class WxHomeController { ...@@ -79,6 +79,7 @@ public class WxHomeController {
if (HomeCacheManager.hasData(HomeCacheManager.INDEX)) { if (HomeCacheManager.hasData(HomeCacheManager.INDEX)) {
return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX)); return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX));
} }
ExecutorService executorService = Executors.newFixedThreadPool(10);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
...@@ -137,6 +138,7 @@ public class WxHomeController { ...@@ -137,6 +138,7 @@ public class WxHomeController {
} }
//缓存数据 //缓存数据
HomeCacheManager.loadData(HomeCacheManager.INDEX, data); HomeCacheManager.loadData(HomeCacheManager.INDEX, data);
executorService.shutdown();
return ResponseUtil.ok(data); return ResponseUtil.ok(data);
} }
......
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