Commit 8ac0c58c authored by Hong's avatar Hong
Browse files

增加地址的查询速度

parent f68e674c
......@@ -15,6 +15,13 @@ 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,7 +28,7 @@ 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
......@@ -33,6 +36,12 @@ public class WxAddressController {
@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 +55,32 @@ 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();
String addr = address.getAddress();
String detailedAddress = province + city + area + " " + addr;
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();
detailedAddress = province + city + area + " " + addr;
}catch (Exception e){
e.printStackTrace();
}
addressVo.put("detailedAddress", detailedAddress);
addressVoList.add(addressVo);
......
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