"src/main/webapp/vscode:/vscode.git/clone" did not exist on "49d2a620737be00cb8d40668303c47cee8466618"
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,206 +28,208 @@ import java.util.concurrent.*; ...@@ -28,206 +28,208 @@ 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
private LitemallRegionService regionService; @Autowired
private LitemallRegionService regionService;
private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(6);
private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(6);
private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 6, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 6, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
/**
* 用户收货地址列表 /**
* * 用户收货地址列表
* @param userId 用户ID *
* @return 收货地址列表 * @param userId 用户ID
*/ * @return 收货地址列表
@GetMapping("list") */
public Object list(@LoginUser Integer userId) { @GetMapping("list")
if (userId == null) { public Object list(@LoginUser Integer userId) {
return ResponseUtil.unlogin(); if (userId == null) {
} return ResponseUtil.unlogin();
List<LitemallAddress> addressList = addressService.queryByUid(userId); }
List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size()); List<LitemallAddress> addressList = addressService.queryByUid(userId);
List<LitemallRegion> regionList = getLitemallRegions(); List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size());
for (LitemallAddress address : addressList) { List<LitemallRegion> regionList = getLitemallRegions();
Map<String, Object> addressVo = new HashMap<>(); for (LitemallAddress address : addressList) {
addressVo.put("id", address.getId()); Map<String, Object> addressVo = new HashMap<>();
addressVo.put("name", address.getName()); addressVo.put("id", address.getId());
addressVo.put("mobile", address.getMobile()); addressVo.put("name", address.getName());
addressVo.put("isDefault", address.getIsDefault()); addressVo.put("mobile", address.getMobile());
Callable<String> provinceCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName(); addressVo.put("isDefault", address.getIsDefault());
Callable<String> cityCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getCityId())).findAny().orElse(null).getName(); Callable<String> provinceCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getProvinceId())).findAny().orElse(null).getName();
Callable<String> areaCallable = ()->regionList.stream().filter(region->region.getId().equals(address.getAreaId())).findAny().orElse(null).getName(); Callable<String> cityCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getCityId())).findAny().orElse(null).getName();
FutureTask<String> provinceNameCallableTask = new FutureTask<>(provinceCallable); Callable<String> areaCallable = () -> regionList.stream().filter(region -> region.getId().equals(address.getAreaId())).findAny().orElse(null).getName();
FutureTask<String> cityNameCallableTask = new FutureTask<>(cityCallable); FutureTask<String> provinceNameCallableTask = new FutureTask<>(provinceCallable);
FutureTask<String> areaNameCallableTask = new FutureTask<>(areaCallable); FutureTask<String> cityNameCallableTask = new FutureTask<>(cityCallable);
executorService.submit(provinceNameCallableTask); FutureTask<String> areaNameCallableTask = new FutureTask<>(areaCallable);
executorService.submit(cityNameCallableTask); executorService.submit(provinceNameCallableTask);
executorService.submit(areaNameCallableTask); executorService.submit(cityNameCallableTask);
String detailedAddress=""; executorService.submit(areaNameCallableTask);
try { String detailedAddress = "";
String province=provinceNameCallableTask.get(); try {
String city =cityNameCallableTask.get(); String province = provinceNameCallableTask.get();
String area =areaNameCallableTask.get(); String city = cityNameCallableTask.get();
String addr = address.getAddress(); String area = areaNameCallableTask.get();
detailedAddress = province + city + area + " " + addr; String addr = address.getAddress();
}catch (Exception e){ detailedAddress = province + city + area + " " + addr;
e.printStackTrace(); }
} catch (Exception e) {
addressVo.put("detailedAddress", detailedAddress); e.printStackTrace();
}
addressVoList.add(addressVo); addressVo.put("detailedAddress", detailedAddress);
}
return ResponseUtil.ok(addressVoList); addressVoList.add(addressVo);
} }
return ResponseUtil.ok(addressVoList);
/** }
* 收货地址详情
* /**
* @param userId 用户ID * 收货地址详情
* @param id 收货地址ID *
* @return 收货地址详情 * @param userId 用户ID
*/ * @param id 收货地址ID
@GetMapping("detail") * @return 收货地址详情
public Object detail(@LoginUser Integer userId, @NotNull Integer id) { */
if (userId == null) { @GetMapping("detail")
return ResponseUtil.unlogin(); public Object detail(@LoginUser Integer userId, @NotNull Integer id) {
} if (userId == null) {
return ResponseUtil.unlogin();
LitemallAddress address = addressService.findById(id); }
if (address == null) {
return ResponseUtil.badArgumentValue(); LitemallAddress address = addressService.findById(id);
} if (address == null) {
return ResponseUtil.badArgumentValue();
Map<Object, Object> data = new HashMap<Object, Object>(); }
data.put("id", address.getId());
data.put("name", address.getName()); Map<Object, Object> data = new HashMap<Object, Object>();
data.put("provinceId", address.getProvinceId()); data.put("id", address.getId());
data.put("cityId", address.getCityId()); data.put("name", address.getName());
data.put("areaId", address.getAreaId()); data.put("provinceId", address.getProvinceId());
data.put("mobile", address.getMobile()); data.put("cityId", address.getCityId());
data.put("address", address.getAddress()); data.put("areaId", address.getAreaId());
data.put("isDefault", address.getIsDefault()); data.put("mobile", address.getMobile());
String pname = regionService.findById(address.getProvinceId()).getName(); data.put("address", address.getAddress());
data.put("provinceName", pname); data.put("isDefault", address.getIsDefault());
String cname = regionService.findById(address.getCityId()).getName(); String pname = regionService.findById(address.getProvinceId()).getName();
data.put("cityName", cname); data.put("provinceName", pname);
String dname = regionService.findById(address.getAreaId()).getName(); String cname = regionService.findById(address.getCityId()).getName();
data.put("areaName", dname); data.put("cityName", cname);
return ResponseUtil.ok(data); String dname = regionService.findById(address.getAreaId()).getName();
} data.put("areaName", dname);
return ResponseUtil.ok(data);
private Object validate(LitemallAddress address) { }
String name = address.getName();
if (StringUtils.isEmpty(name)) { private Object validate(LitemallAddress address) {
return ResponseUtil.badArgument(); String name = address.getName();
} if (StringUtils.isEmpty(name)) {
return ResponseUtil.badArgument();
// 测试收货手机号码是否正确 }
String mobile = address.getMobile();
if (StringUtils.isEmpty(mobile)) { // 测试收货手机号码是否正确
return ResponseUtil.badArgument(); String mobile = address.getMobile();
} if (StringUtils.isEmpty(mobile)) {
if (!RegexUtil.isMobileExact(mobile)) { return ResponseUtil.badArgument();
return ResponseUtil.badArgument(); }
} if (!RegexUtil.isMobileExact(mobile)) {
return ResponseUtil.badArgument();
Integer pid = address.getProvinceId(); }
if (pid == null) {
return ResponseUtil.badArgument(); Integer pid = address.getProvinceId();
} if (pid == null) {
if (regionService.findById(pid) == null) { return ResponseUtil.badArgument();
return ResponseUtil.badArgumentValue(); }
} if (regionService.findById(pid) == null) {
return ResponseUtil.badArgumentValue();
Integer cid = address.getCityId(); }
if (cid == null) {
return ResponseUtil.badArgument(); Integer cid = address.getCityId();
} if (cid == null) {
if (regionService.findById(cid) == null) { return ResponseUtil.badArgument();
return ResponseUtil.badArgumentValue(); }
} if (regionService.findById(cid) == null) {
return ResponseUtil.badArgumentValue();
Integer aid = address.getAreaId(); }
if (aid == null) {
return ResponseUtil.badArgument(); Integer aid = address.getAreaId();
} if (aid == null) {
if (regionService.findById(aid) == null) { return ResponseUtil.badArgument();
return ResponseUtil.badArgumentValue(); }
} if (regionService.findById(aid) == null) {
return ResponseUtil.badArgumentValue();
String detailedAddress = address.getAddress(); }
if (StringUtils.isEmpty(detailedAddress)) {
return ResponseUtil.badArgument(); String detailedAddress = address.getAddress();
} if (StringUtils.isEmpty(detailedAddress)) {
return ResponseUtil.badArgument();
Boolean isDefault = address.getIsDefault(); }
if (isDefault == null) {
return ResponseUtil.badArgument(); Boolean isDefault = address.getIsDefault();
} if (isDefault == null) {
return null; return ResponseUtil.badArgument();
} }
return null;
/** }
* 添加或更新收货地址
* /**
* @param userId 用户ID * 添加或更新收货地址
* @param address 用户收货地址 *
* @return 添加或更新操作结果 * @param userId 用户ID
*/ * @param address 用户收货地址
@PostMapping("save") * @return 添加或更新操作结果
public Object save(@LoginUser Integer userId, @RequestBody LitemallAddress address) { */
if (userId == null) { @PostMapping("save")
return ResponseUtil.unlogin(); public Object save(@LoginUser Integer userId, @RequestBody LitemallAddress address) {
} if (userId == null) {
Object error = validate(address); return ResponseUtil.unlogin();
if (error != null) { }
return error; Object error = validate(address);
} if (error != null) {
return error;
if (address.getIsDefault()) { }
// 重置其他收获地址的默认选项
addressService.resetDefault(userId); if (address.getIsDefault()) {
} // 重置其他收获地址的默认选项
addressService.resetDefault(userId);
if (address.getId() == null || address.getId().equals(0)) { }
address.setId(null);
address.setUserId(userId); if (address.getId() == null || address.getId().equals(0)) {
addressService.add(address); address.setId(null);
} else { address.setUserId(userId);
address.setUserId(userId); addressService.add(address);
if (addressService.update(address) == 0) { } else {
return ResponseUtil.updatedDataFailed(); address.setUserId(userId);
} if (addressService.update(address) == 0) {
} return ResponseUtil.updatedDataFailed();
return ResponseUtil.ok(address.getId()); }
} }
return ResponseUtil.ok(address.getId());
/** }
* 删除收货地址
* /**
* @param userId 用户ID * 删除收货地址
* @param address 用户收货地址,{ id: xxx } *
* @return 删除操作结果 * @param userId 用户ID
*/ * @param address 用户收货地址,{ id: xxx }
@PostMapping("delete") * @return 删除操作结果
public Object delete(@LoginUser Integer userId, @RequestBody LitemallAddress address) { */
if (userId == null) { @PostMapping("delete")
return ResponseUtil.unlogin(); public Object delete(@LoginUser Integer userId, @RequestBody LitemallAddress address) {
} if (userId == null) {
Integer id = address.getId(); return ResponseUtil.unlogin();
if (id == null) { }
return ResponseUtil.badArgument(); Integer id = address.getId();
} if (id == null) {
return ResponseUtil.badArgument();
addressService.delete(id); }
return ResponseUtil.ok();
} addressService.delete(id);
return ResponseUtil.ok();
}
} }
\ No newline at end of file
...@@ -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.*;
/** /**
* 商品服务 * 商品服务
...@@ -30,275 +32,325 @@ import java.util.Map; ...@@ -30,275 +32,325 @@ import java.util.Map;
@RequestMapping("/wx/goods") @RequestMapping("/wx/goods")
@Validated @Validated
public class WxGoodsController { public class WxGoodsController {
private final Log logger = LogFactory.getLog(WxGoodsController.class); private final Log logger = LogFactory.getLog(WxGoodsController.class);
@Autowired @Autowired
private LitemallGoodsService goodsService; private LitemallGoodsService goodsService;
@Autowired
private LitemallGoodsProductService productService; @Autowired
@Autowired private LitemallGoodsProductService productService;
private LitemallIssueService goodsIssueService;
@Autowired @Autowired
private LitemallGoodsAttributeService goodsAttributeService; private LitemallIssueService goodsIssueService;
@Autowired
private LitemallBrandService brandService; @Autowired
@Autowired private LitemallGoodsAttributeService goodsAttributeService;
private LitemallCommentService commentService;
@Autowired @Autowired
private LitemallUserService userService; private LitemallBrandService brandService;
@Autowired
private LitemallCollectService collectService; @Autowired
@Autowired private LitemallCommentService commentService;
private LitemallFootprintService footprintService;
@Autowired @Autowired
private LitemallCategoryService categoryService; private LitemallUserService userService;
@Autowired
private LitemallSearchHistoryService searchHistoryService; @Autowired
@Autowired private LitemallCollectService collectService;
private LitemallGoodsSpecificationService goodsSpecificationService;
@Autowired @Autowired
private LitemallGrouponRulesService rulesService; private LitemallFootprintService footprintService;
@Autowired
/** private LitemallCategoryService categoryService;
* 商品详情
* <p> @Autowired
* 用户可以不登录。 private LitemallSearchHistoryService searchHistoryService;
* 如果用户登录,则记录用户足迹以及返回用户收藏信息。
* @Autowired
* @param userId 用户ID private LitemallGoodsSpecificationService goodsSpecificationService;
* @param id 商品ID
* @return 商品详情 @Autowired
*/ private LitemallGrouponRulesService rulesService;
@GetMapping("detail")
public Object detail(@LoginUser Integer userId, @NotNull Integer id) { private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);
// 商品信息
LitemallGoods info = goodsService.findById(id); private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
// 商品属性 private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(16, 16, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
List<LitemallGoodsAttribute> goodsAttributeList = goodsAttributeService.queryByGid(id);
/**
// 商品规格 * 商品详情
// 返回的是定制的GoodsSpecificationVo * <p>
Object specificationList = goodsSpecificationService.getSpecificationVoList(id); * 用户可以不登录。
* 如果用户登录,则记录用户足迹以及返回用户收藏信息。
// 商品规格对应的数量和价格 *
List<LitemallGoodsProduct> productList = productService.queryByGid(id); * @param userId 用户ID
* @param id 商品ID
// 商品问题,这里是一些通用问题 * @return 商品详情
List<LitemallIssue> issue = goodsIssueService.query(); */
@GetMapping("detail")
// 商品品牌商 public Object detail(@LoginUser Integer userId, @NotNull Integer id) {
Integer brandId = info.getBrandId(); // 商品信息
LitemallBrand brand = null; LitemallGoods info = goodsService.findById(id);
if (brandId == 0) {
brand = new LitemallBrand(); // 商品属性
} else { Callable<List> goodsAttributeListCallable = () -> goodsAttributeService.queryByGid(id);
brand = brandService.findById(info.getBrandId());
} // 商品规格 返回的是定制的GoodsSpecificationVo
Callable<Object> objectCallable = () -> goodsSpecificationService.getSpecificationVoList(id);
// 评论
List<LitemallComment> comments = commentService.queryGoodsByGid(id, 0, 2); // 商品规格对应的数量和价格
List<Map<String, Object>> commentsVo = new ArrayList<>(comments.size()); Callable<List> productListCallable = () -> productService.queryByGid(id);
int commentCount = commentService.countGoodsByGid(id, 0, 2);
for (LitemallComment comment : comments) { // 商品问题,这里是一些通用问题
Map<String, Object> c = new HashMap<>(); Callable<List> issueCallable = () -> goodsIssueService.query();
c.put("id", comment.getId());
c.put("addTime", comment.getAddTime()); // 商品品牌商
c.put("content", comment.getContent()); Callable<LitemallBrand> brandCallable = ()->{
LitemallUser user = userService.findById(comment.getUserId()); Integer brandId = info.getBrandId();
c.put("nickname", user.getNickname()); LitemallBrand brand;
c.put("avatar", user.getAvatar()); if (brandId == 0) {
c.put("picList", comment.getPicUrls()); brand = new LitemallBrand();
commentsVo.add(c); } else {
} brand = brandService.findById(info.getBrandId());
Map<String, Object> commentList = new HashMap<>(); }
commentList.put("count", commentCount); return brand;
commentList.put("data", commentsVo); };
//团购信息 // 评论
List<LitemallGrouponRules> rules = rulesService.queryByGoodsId(id); Callable<Map> commentsCallable = () -> {
List<LitemallComment> comments = commentService.queryGoodsByGid(id, 0, 2);
// 用户收藏 List<Map<String, Object>> commentsVo = new ArrayList<>(comments.size());
int userHasCollect = 0; int commentCount = commentService.countGoodsByGid(id, 0, 2);
if (userId != null) { for (LitemallComment comment : comments) {
userHasCollect = collectService.count(userId, id); Map<String, Object> c = new HashMap<>();
} c.put("id", comment.getId());
c.put("addTime", comment.getAddTime());
// 记录用户的足迹 c.put("content", comment.getContent());
if (userId != null) { LitemallUser user = userService.findById(comment.getUserId());
LitemallFootprint footprint = new LitemallFootprint(); c.put("nickname", user.getNickname());
footprint.setUserId(userId); c.put("avatar", user.getAvatar());
footprint.setGoodsId(id); c.put("picList", comment.getPicUrls());
footprintService.add(footprint); commentsVo.add(c);
} }
Map<String, Object> commentList = new HashMap<>();
Map<String, Object> data = new HashMap<>(); commentList.put("count", commentCount);
data.put("info", info); commentList.put("data", commentsVo);
data.put("userHasCollect", userHasCollect); return commentList;
data.put("issue", issue); };
data.put("comment", commentList);
data.put("specificationList", specificationList); //团购信息
data.put("productList", productList); Callable<List> grouponRulesCallable = () ->rulesService.queryByGoodsId(id);
data.put("attribute", goodsAttributeList);
data.put("brand", brand); // 用户收藏
data.put("groupon", rules); int userHasCollect = 0;
if (userId != null) {
//商品分享图片地址 userHasCollect = collectService.count(userId, id);
data.put("shareImage", info.getShareUrl()); }
return ResponseUtil.ok(data);
} // 记录用户的足迹 异步处理
if (userId != null) {
/** executorService.execute(()->{
* 商品分类类目 LitemallFootprint footprint = new LitemallFootprint();
* footprint.setUserId(userId);
* @param id 分类类目ID footprint.setGoodsId(id);
* @return 商品分类类目 footprintService.add(footprint);
*/ });
@GetMapping("category") }
public Object category(@NotNull Integer id) { FutureTask<List> goodsAttributeListTask = new FutureTask<>(goodsAttributeListCallable);
LitemallCategory cur = categoryService.findById(id); FutureTask<Object> objectCallableTask = new FutureTask<>(objectCallable);
LitemallCategory parent = null; FutureTask<List> productListCallableTask = new FutureTask<>(productListCallable);
List<LitemallCategory> children = null; FutureTask<List> issueCallableTask = new FutureTask<>(issueCallable);
FutureTask<Map> commentsCallableTsk = new FutureTask<>(commentsCallable);
if (cur.getPid() == 0) { FutureTask<LitemallBrand> brandCallableTask = new FutureTask<>(brandCallable);
parent = cur; FutureTask<List> grouponRulesCallableTask = new FutureTask<>(grouponRulesCallable);
children = categoryService.queryByPid(cur.getId());
cur = children.size() > 0 ? children.get(0) : cur; executorService.submit(goodsAttributeListTask);
} else { executorService.submit(objectCallableTask);
parent = categoryService.findById(cur.getPid()); executorService.submit(productListCallableTask);
children = categoryService.queryByPid(cur.getPid()); executorService.submit(issueCallableTask);
} executorService.submit(commentsCallableTsk);
Map<String, Object> data = new HashMap<>(); executorService.submit(brandCallableTask);
data.put("currentCategory", cur); executorService.submit(grouponRulesCallableTask);
data.put("parentCategory", parent);
data.put("brotherCategory", children); Map<String, Object> data = new HashMap<>();
return ResponseUtil.ok(data);
} try {
data.put("info", info);
/** data.put("userHasCollect", userHasCollect);
* 根据条件搜素商品 data.put("issue", issueCallableTask.get());
* <p> data.put("comment", commentsCallableTsk.get());
* 1. 这里的前五个参数都是可选的,甚至都是空 data.put("specificationList", objectCallableTask.get());
* 2. 用户是可选登录,如果登录,则记录用户的搜索关键字 data.put("productList", productListCallableTask.get());
* data.put("attribute", goodsAttributeListTask.get());
* @param categoryId 分类类目ID,可选 data.put("brand", brandCallableTask.get());
* @param brandId 品牌商ID,可选 data.put("groupon", grouponRulesCallableTask.get());
* @param keyword 关键字,可选 }
* @param isNew 是否新品,可选 catch (Exception e) {
* @param isHot 是否热买,可选 e.printStackTrace();
* @param userId 用户ID }
* @param page 分页页数
* @param size 分页大小 //商品分享图片地址
* @param sort 排序方式,支持"add_time", "retail_price"或"name" data.put("shareImage", info.getShareUrl());
* @param order 排序类型,顺序或者降序 return ResponseUtil.ok(data);
* @return 根据条件搜素的商品详情 }
*/
@GetMapping("list") /**
public Object list(Integer categoryId, Integer brandId, String keyword, Boolean isNew, Boolean isHot, * 商品分类类目
@LoginUser Integer userId, *
@RequestParam(defaultValue = "1") Integer page, * @param id 分类类目ID
@RequestParam(defaultValue = "10") Integer size, * @return 商品分类类目
@Sort(accepts = {"add_time", "retail_price", "name"}) @RequestParam(defaultValue = "add_time") String sort, */
@Order @RequestParam(defaultValue = "desc") String order) { @GetMapping("category")
public Object category(@NotNull Integer id) {
//添加到搜索历史 LitemallCategory cur = categoryService.findById(id);
if (userId != null && !StringUtils.isNullOrEmpty(keyword)) { LitemallCategory parent = null;
LitemallSearchHistory searchHistoryVo = new LitemallSearchHistory(); List<LitemallCategory> children = null;
searchHistoryVo.setKeyword(keyword);
searchHistoryVo.setUserId(userId); if (cur.getPid() == 0) {
searchHistoryVo.setFrom("wx"); parent = cur;
searchHistoryService.save(searchHistoryVo); children = categoryService.queryByPid(cur.getId());
} cur = children.size() > 0 ? children.get(0) : cur;
} else {
//查询列表数据 parent = categoryService.findById(cur.getPid());
List<LitemallGoods> goodsList = goodsService.querySelective(categoryId, brandId, keyword, isHot, isNew, page, size, sort, order); children = categoryService.queryByPid(cur.getPid());
int total = goodsService.countSelective(categoryId, brandId, keyword, isHot, isNew, page, size, sort, order); }
Map<String, Object> data = new HashMap<>();
// 查询商品所属类目列表。 data.put("currentCategory", cur);
List<Integer> goodsCatIds = goodsService.getCatIds(brandId, keyword, isHot, isNew); data.put("parentCategory", parent);
List<LitemallCategory> categoryList = null; data.put("brotherCategory", children);
if (goodsCatIds.size() != 0) { return ResponseUtil.ok(data);
categoryList = categoryService.queryL2ByIds(goodsCatIds); }
} else {
categoryList = new ArrayList<>(0); /**
} * 根据条件搜素商品
* <p>
Map<String, Object> data = new HashMap<>(); * 1. 这里的前五个参数都是可选的,甚至都是空
data.put("goodsList", goodsList); * 2. 用户是可选登录,如果登录,则记录用户的搜索关键字
data.put("filterCategoryList", categoryList); *
data.put("count", total); * @param categoryId 分类类目ID,可选
return ResponseUtil.ok(data); * @param brandId 品牌商ID,可选
} * @param keyword 关键字,可选
* @param isNew 是否新品,可选
/** * @param isHot 是否热买,可选
* 新品首发页面的横幅 * @param userId 用户ID
* * @param page 分页页数
* @return 新品首发页面的横幅 * @param size 分页大小
*/ * @param sort 排序方式,支持"add_time", "retail_price"或"name"
@GetMapping("new") * @param order 排序类型,顺序或者降序
public Object newGoods() { * @return 根据条件搜素的商品详情
Map<String, String> bannerInfo = new HashMap<>(); */
bannerInfo.put("url", ""); @GetMapping("list")
bannerInfo.put("name", SystemConfig.getNewBannerTitle()); public Object list(
bannerInfo.put("imgUrl", SystemConfig.getNewImageUrl()); Integer categoryId,
Integer brandId,
Map<String, Object> data = new HashMap<>(); String keyword,
data.put("bannerInfo", bannerInfo); Boolean isNew,
return ResponseUtil.ok(data); Boolean isHot,
} @LoginUser Integer userId,
@RequestParam(defaultValue = "1") Integer page,
/** @RequestParam(defaultValue = "10") Integer size,
* 人气推荐页面的横幅 @Sort(accepts = {"add_time", "retail_price", "name"}) @RequestParam(defaultValue = "add_time") String sort,
* @Order @RequestParam(defaultValue = "desc") String order) {
* @return 人气推荐页面的横幅
*/ //添加到搜索历史
@GetMapping("hot") if (userId != null && !StringUtils.isNullOrEmpty(keyword)) {
public Object hotGoods() { LitemallSearchHistory searchHistoryVo = new LitemallSearchHistory();
Map<String, String> bannerInfo = new HashMap<>(); searchHistoryVo.setKeyword(keyword);
bannerInfo.put("url", ""); searchHistoryVo.setUserId(userId);
bannerInfo.put("name", SystemConfig.getHotBannerTitle()); searchHistoryVo.setFrom("wx");
bannerInfo.put("imgUrl", SystemConfig.getHotImageUrl()); searchHistoryService.save(searchHistoryVo);
Map<String, Object> data = new HashMap<>(); }
data.put("bannerInfo", bannerInfo);
return ResponseUtil.ok(data); //查询列表数据
} List<LitemallGoods> goodsList = goodsService.querySelective(categoryId, brandId, keyword, isHot, isNew, page, size, sort, order);
int total = goodsService.countSelective(categoryId, brandId, keyword, isHot, isNew, page, size, sort, order);
/**
* 商品详情页面“大家都在看”推荐商品 // 查询商品所属类目列表。
* List<Integer> goodsCatIds = goodsService.getCatIds(brandId, keyword, isHot, isNew);
* @param id, 商品ID List<LitemallCategory> categoryList = null;
* @return 商品详情页面推荐商品 if (goodsCatIds.size() != 0) {
*/ categoryList = categoryService.queryL2ByIds(goodsCatIds);
@GetMapping("related") } else {
public Object related(@NotNull Integer id) { categoryList = new ArrayList<>(0);
LitemallGoods goods = goodsService.findById(id); }
if (goods == null) {
return ResponseUtil.badArgumentValue(); Map<String, Object> data = new HashMap<>();
} data.put("goodsList", goodsList);
data.put("filterCategoryList", categoryList);
// 目前的商品推荐算法仅仅是推荐同类目的其他商品 data.put("count", total);
int cid = goods.getCategoryId(); return ResponseUtil.ok(data);
}
// 查找六个相关商品
int related = 6; /**
List<LitemallGoods> goodsList = goodsService.queryByCategory(cid, 0, related); * 新品首发页面的横幅
Map<String, Object> data = new HashMap<>(); *
data.put("goodsList", goodsList); * @return 新品首发页面的横幅
return ResponseUtil.ok(data); */
} @GetMapping("new")
public Object newGoods() {
/** Map<String, String> bannerInfo = new HashMap<>();
* 在售的商品总数 bannerInfo.put("url", "");
* bannerInfo.put("name", SystemConfig.getNewBannerTitle());
* @return 在售的商品总数 bannerInfo.put("imgUrl", SystemConfig.getNewImageUrl());
*/
@GetMapping("count") Map<String, Object> data = new HashMap<>();
public Object count() { data.put("bannerInfo", bannerInfo);
Integer goodsCount = goodsService.queryOnSale(); return ResponseUtil.ok(data);
Map<String, Object> data = new HashMap<>(); }
data.put("goodsCount", goodsCount);
return ResponseUtil.ok(data); /**
} * 人气推荐页面的横幅
*
* @return 人气推荐页面的横幅
*/
@GetMapping("hot")
public Object hotGoods() {
Map<String, String> bannerInfo = new HashMap<>();
bannerInfo.put("url", "");
bannerInfo.put("name", SystemConfig.getHotBannerTitle());
bannerInfo.put("imgUrl", SystemConfig.getHotImageUrl());
Map<String, Object> data = new HashMap<>();
data.put("bannerInfo", bannerInfo);
return ResponseUtil.ok(data);
}
/**
* 商品详情页面“大家都在看”推荐商品
*
* @param id, 商品ID
* @return 商品详情页面推荐商品
*/
@GetMapping("related")
public Object related(@NotNull Integer id) {
LitemallGoods goods = goodsService.findById(id);
if (goods == null) {
return ResponseUtil.badArgumentValue();
}
// 目前的商品推荐算法仅仅是推荐同类目的其他商品
int cid = goods.getCategoryId();
// 查找六个相关商品
int related = 6;
List<LitemallGoods> goodsList = goodsService.queryByCategory(cid, 0, related);
Map<String, Object> data = new HashMap<>();
data.put("goodsList", goodsList);
return ResponseUtil.ok(data);
}
/**
* 在售的商品总数
*
* @return 在售的商品总数
*/
@GetMapping("count")
public Object count() {
Integer goodsCount = goodsService.queryOnSale();
Map<String, Object> data = new HashMap<>();
data.put("goodsCount", goodsCount);
return ResponseUtil.ok(data);
}
} }
\ No newline at end of file
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