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,185 +28,208 @@ import java.util.Map; ...@@ -25,185 +28,208 @@ 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
private LitemallRegionService regionService; @Autowired
private LitemallRegionService regionService;
/**
* 用户收货地址列表 private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(6);
*
* @param userId 用户ID private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
* @return 收货地址列表
*/ private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 6, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
@GetMapping("list")
public Object list(@LoginUser Integer userId) { /**
if (userId == null) { * 用户收货地址列表
return ResponseUtil.unlogin(); *
} * @param userId 用户ID
List<LitemallAddress> addressList = addressService.queryByUid(userId); * @return 收货地址列表
List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size()); */
for (LitemallAddress address : addressList) { @GetMapping("list")
Map<String, Object> addressVo = new HashMap<>(); public Object list(@LoginUser Integer userId) {
addressVo.put("id", address.getId()); if (userId == null) {
addressVo.put("name", address.getName()); return ResponseUtil.unlogin();
addressVo.put("mobile", address.getMobile()); }
addressVo.put("isDefault", address.getIsDefault()); List<LitemallAddress> addressList = addressService.queryByUid(userId);
String province = regionService.findById(address.getProvinceId()).getName(); List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size());
String city = regionService.findById(address.getCityId()).getName(); List<LitemallRegion> regionList = getLitemallRegions();
String area = regionService.findById(address.getAreaId()).getName(); for (LitemallAddress address : addressList) {
String addr = address.getAddress(); Map<String, Object> addressVo = new HashMap<>();
String detailedAddress = province + city + area + " " + addr; addressVo.put("id", address.getId());
addressVo.put("detailedAddress", detailedAddress); addressVo.put("name", address.getName());
addressVo.put("mobile", address.getMobile());
addressVoList.add(addressVo); addressVo.put("isDefault", address.getIsDefault());
} Callable<String> provinceCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName();
return ResponseUtil.ok(addressVoList); 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);
* @param userId 用户ID executorService.submit(cityNameCallableTask);
* @param id 收货地址ID executorService.submit(areaNameCallableTask);
* @return 收货地址详情 String detailedAddress = "";
*/ try {
@GetMapping("detail") String province = provinceNameCallableTask.get();
public Object detail(@LoginUser Integer userId, @NotNull Integer id) { String city = cityNameCallableTask.get();
if (userId == null) { String area = areaNameCallableTask.get();
return ResponseUtil.unlogin(); String addr = address.getAddress();
} detailedAddress = province + city + area + " " + addr;
}
LitemallAddress address = addressService.findById(id); catch (Exception e) {
if (address == null) { e.printStackTrace();
return ResponseUtil.badArgumentValue(); }
} addressVo.put("detailedAddress", detailedAddress);
Map<Object, Object> data = new HashMap<Object, Object>(); addressVoList.add(addressVo);
data.put("id", address.getId()); }
data.put("name", address.getName()); return ResponseUtil.ok(addressVoList);
data.put("provinceId", address.getProvinceId()); }
data.put("cityId", address.getCityId());
data.put("areaId", address.getAreaId()); /**
data.put("mobile", address.getMobile()); * 收货地址详情
data.put("address", address.getAddress()); *
data.put("isDefault", address.getIsDefault()); * @param userId 用户ID
String pname = regionService.findById(address.getProvinceId()).getName(); * @param id 收货地址ID
data.put("provinceName", pname); * @return 收货地址详情
String cname = regionService.findById(address.getCityId()).getName(); */
data.put("cityName", cname); @GetMapping("detail")
String dname = regionService.findById(address.getAreaId()).getName(); public Object detail(@LoginUser Integer userId, @NotNull Integer id) {
data.put("areaName", dname); if (userId == null) {
return ResponseUtil.ok(data); return ResponseUtil.unlogin();
} }
private Object validate(LitemallAddress address) { LitemallAddress address = addressService.findById(id);
String name = address.getName(); if (address == null) {
if (StringUtils.isEmpty(name)) { return ResponseUtil.badArgumentValue();
return ResponseUtil.badArgument(); }
}
Map<Object, Object> data = new HashMap<Object, Object>();
// 测试收货手机号码是否正确 data.put("id", address.getId());
String mobile = address.getMobile(); data.put("name", address.getName());
if (StringUtils.isEmpty(mobile)) { data.put("provinceId", address.getProvinceId());
return ResponseUtil.badArgument(); data.put("cityId", address.getCityId());
} data.put("areaId", address.getAreaId());
if (!RegexUtil.isMobileExact(mobile)) { data.put("mobile", address.getMobile());
return ResponseUtil.badArgument(); data.put("address", address.getAddress());
} data.put("isDefault", address.getIsDefault());
String pname = regionService.findById(address.getProvinceId()).getName();
Integer pid = address.getProvinceId(); data.put("provinceName", pname);
if (pid == null) { String cname = regionService.findById(address.getCityId()).getName();
return ResponseUtil.badArgument(); data.put("cityName", cname);
} String dname = regionService.findById(address.getAreaId()).getName();
if (regionService.findById(pid) == null) { data.put("areaName", dname);
return ResponseUtil.badArgumentValue(); return ResponseUtil.ok(data);
} }
Integer cid = address.getCityId(); private Object validate(LitemallAddress address) {
if (cid == null) { String name = address.getName();
return ResponseUtil.badArgument(); if (StringUtils.isEmpty(name)) {
} return ResponseUtil.badArgument();
if (regionService.findById(cid) == null) { }
return ResponseUtil.badArgumentValue();
} // 测试收货手机号码是否正确
String mobile = address.getMobile();
Integer aid = address.getAreaId(); if (StringUtils.isEmpty(mobile)) {
if (aid == null) { return ResponseUtil.badArgument();
return ResponseUtil.badArgument(); }
} if (!RegexUtil.isMobileExact(mobile)) {
if (regionService.findById(aid) == null) { return ResponseUtil.badArgument();
return ResponseUtil.badArgumentValue(); }
}
Integer pid = address.getProvinceId();
String detailedAddress = address.getAddress(); if (pid == null) {
if (StringUtils.isEmpty(detailedAddress)) { return ResponseUtil.badArgument();
return ResponseUtil.badArgument(); }
} if (regionService.findById(pid) == null) {
return ResponseUtil.badArgumentValue();
Boolean isDefault = address.getIsDefault(); }
if (isDefault == null) {
return ResponseUtil.badArgument(); Integer cid = address.getCityId();
} if (cid == null) {
return null; return ResponseUtil.badArgument();
} }
if (regionService.findById(cid) == null) {
/** return ResponseUtil.badArgumentValue();
* 添加或更新收货地址 }
*
* @param userId 用户ID Integer aid = address.getAreaId();
* @param address 用户收货地址 if (aid == null) {
* @return 添加或更新操作结果 return ResponseUtil.badArgument();
*/ }
@PostMapping("save") if (regionService.findById(aid) == null) {
public Object save(@LoginUser Integer userId, @RequestBody LitemallAddress address) { return ResponseUtil.badArgumentValue();
if (userId == null) { }
return ResponseUtil.unlogin();
} String detailedAddress = address.getAddress();
Object error = validate(address); if (StringUtils.isEmpty(detailedAddress)) {
if (error != null) { return ResponseUtil.badArgument();
return error; }
}
Boolean isDefault = address.getIsDefault();
if (address.getIsDefault()) { if (isDefault == null) {
// 重置其他收获地址的默认选项 return ResponseUtil.badArgument();
addressService.resetDefault(userId); }
} return null;
}
if (address.getId() == null || address.getId().equals(0)) {
address.setId(null); /**
address.setUserId(userId); * 添加或更新收货地址
addressService.add(address); *
} else { * @param userId 用户ID
address.setUserId(userId); * @param address 用户收货地址
if (addressService.update(address) == 0) { * @return 添加或更新操作结果
return ResponseUtil.updatedDataFailed(); */
} @PostMapping("save")
} public Object save(@LoginUser Integer userId, @RequestBody LitemallAddress address) {
return ResponseUtil.ok(address.getId()); if (userId == null) {
} return ResponseUtil.unlogin();
}
/** Object error = validate(address);
* 删除收货地址 if (error != null) {
* return error;
* @param userId 用户ID }
* @param address 用户收货地址,{ id: xxx }
* @return 删除操作结果 if (address.getIsDefault()) {
*/ // 重置其他收获地址的默认选项
@PostMapping("delete") addressService.resetDefault(userId);
public Object delete(@LoginUser Integer userId, @RequestBody LitemallAddress address) { }
if (userId == null) {
return ResponseUtil.unlogin(); if (address.getId() == null || address.getId().equals(0)) {
} address.setId(null);
Integer id = address.getId(); address.setUserId(userId);
if (id == null) { addressService.add(address);
return ResponseUtil.badArgument(); } else {
} address.setUserId(userId);
if (addressService.update(address) == 0) {
addressService.delete(id); return ResponseUtil.updatedDataFailed();
return ResponseUtil.ok(); }
} }
return ResponseUtil.ok(address.getId());
}
/**
* 删除收货地址
*
* @param userId 用户ID
* @param address 用户收货地址,{ id: xxx }
* @return 删除操作结果
*/
@PostMapping("delete")
public Object delete(@LoginUser Integer userId, @RequestBody LitemallAddress address) {
if (userId == null) {
return ResponseUtil.unlogin();
}
Integer id = address.getId();
if (id == null) {
return ResponseUtil.badArgument();
}
addressService.delete(id);
return ResponseUtil.ok();
}
} }
\ No newline at end of file
...@@ -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