Commit 878a921a authored by Junling Bu's avatar Junling Bu
Browse files
parents 50028820 950b176f
......@@ -12,9 +12,17 @@ import java.util.List;
@Service
public class LitemallRegionService {
@Resource
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) {
LitemallRegionExample example = new LitemallRegionExample();
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;
import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
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.LitemallRegionService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.GetRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
......@@ -18,6 +20,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/**
* 用户收货地址服务
......@@ -25,14 +28,21 @@ import java.util.Map;
@RestController
@RequestMapping("/wx/address")
@Validated
public class WxAddressController {
public class WxAddressController extends GetRegionService {
private final Log logger = LogFactory.getLog(WxAddressController.class);
@Autowired
private LitemallAddressService addressService;
@Autowired
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 {
}
List<LitemallAddress> addressList = addressService.queryByUid(userId);
List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size());
List<LitemallRegion> regionList = getLitemallRegions();
for (LitemallAddress address : addressList) {
Map<String, Object> addressVo = new HashMap<>();
addressVo.put("id", address.getId());
addressVo.put("name", address.getName());
addressVo.put("mobile", address.getMobile());
addressVo.put("isDefault", address.getIsDefault());
String province = regionService.findById(address.getProvinceId()).getName();
String city = regionService.findById(address.getCityId()).getName();
String area = regionService.findById(address.getAreaId()).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 = "";
try {
String province = provinceNameCallableTask.get();
String city = cityNameCallableTask.get();
String area = areaNameCallableTask.get();
String addr = address.getAddress();
String detailedAddress = province + city + area + " " + addr;
detailedAddress = province + city + area + " " + addr;
}
catch (Exception e) {
e.printStackTrace();
}
addressVo.put("detailedAddress", detailedAddress);
addressVoList.add(addressVo);
......
......@@ -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,
......
......@@ -79,6 +79,7 @@ public class WxHomeController {
if (HomeCacheManager.hasData(HomeCacheManager.INDEX)) {
return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX));
}
ExecutorService executorService = Executors.newFixedThreadPool(10);
Map<String, Object> data = new HashMap<>();
......@@ -137,6 +138,7 @@ public class WxHomeController {
}
//缓存数据
HomeCacheManager.loadData(HomeCacheManager.INDEX, data);
executorService.shutdown();
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