use of com.wayn.common.core.domain.shop.Goods in project waynboot-mall by wayn111.
the class CartServiceImpl method add.
@Override
public R add(Cart cart) {
Long goodsId = cart.getGoodsId();
Long productId = cart.getProductId();
Integer number = cart.getNumber();
if (!ObjectUtils.allNotNull(goodsId, productId, number) || number <= 0) {
return R.error(ReturnCodeEnum.PARAMETER_TYPE_ERROR);
}
Goods goods = iGoodsService.getById(goodsId);
if (!goods.getIsOnSale()) {
return R.error(ReturnCodeEnum.GOODS_HAS_OFFSHELF_ERROR);
}
Long userId = MobileSecurityUtils.getLoginUser().getMember().getId();
GoodsProduct product = iGoodsProductService.getById(productId);
Cart existsCart = checkExistsGoods(userId, goodsId, productId);
if (Objects.isNull(existsCart)) {
if (Objects.isNull(product) || product.getNumber() < number) {
return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
}
cart.setGoodsSn(goods.getGoodsSn());
cart.setGoodsName(goods.getName());
if (StringUtils.isEmpty(product.getUrl())) {
cart.setPicUrl(goods.getPicUrl());
} else {
cart.setPicUrl(product.getUrl());
}
cart.setPrice(product.getPrice());
cart.setSpecifications((product.getSpecifications()));
cart.setUserId(Math.toIntExact(userId));
cart.setChecked(true);
cart.setRemark(goods.getBrief());
cart.setCreateTime(LocalDateTime.now());
save(cart);
} else {
int num = existsCart.getNumber() + number;
if (num > product.getNumber()) {
return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
}
existsCart.setNumber(num);
cart.setUpdateTime(LocalDateTime.now());
if (!updateById(existsCart)) {
return R.error();
}
}
return R.success();
}
use of com.wayn.common.core.domain.shop.Goods in project waynboot-mall by wayn111.
the class SearchController method result.
@GetMapping("result")
public R result(SearchVO searchVO) throws IOException {
Long memberId = MobileSecurityUtils.getUserId();
String keyword = searchVO.getKeyword();
Boolean filterNew = searchVO.getFilterNew();
Boolean filterHot = searchVO.getFilterHot();
Boolean isNew = searchVO.getIsNew();
Boolean isHot = searchVO.getIsHot();
Boolean isPrice = searchVO.getIsPrice();
Boolean isSales = searchVO.getIsSales();
String orderBy = searchVO.getOrderBy();
SearchHistory searchHistory = new SearchHistory();
if (memberId != null && StringUtils.isNotEmpty(keyword)) {
searchHistory.setCreateTime(LocalDateTime.now());
searchHistory.setUserId(memberId);
searchHistory.setKeyword(keyword);
}
Page<SearchVO> page = getPage();
// 查询包含关键字、已上架商品
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
MatchQueryBuilder matchFiler = QueryBuilders.matchQuery("isOnSale", true);
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyword);
MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keyword", keyword);
boolQueryBuilder.filter(matchFiler).should(matchQuery).should(matchPhraseQueryBuilder).minimumShouldMatch(1);
searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
// 按是否新品排序
if (isNew) {
searchSourceBuilder.sort(new FieldSortBuilder("isNew").order(SortOrder.DESC));
}
// 按是否热品排序
if (isHot) {
searchSourceBuilder.sort(new FieldSortBuilder("isHot").order(SortOrder.DESC));
}
// 按价格高低排序
if (isPrice) {
searchSourceBuilder.sort(new FieldSortBuilder("retailPrice").order("asc".equals(orderBy) ? SortOrder.ASC : SortOrder.DESC));
}
// 按销量排序
if (isSales) {
searchSourceBuilder.sort(new FieldSortBuilder("sales").order(SortOrder.DESC));
}
// 筛选新品
if (filterNew) {
MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isNew", true);
boolQueryBuilder.filter(filterQuery);
}
// 筛选热品
if (filterHot) {
MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isHot", true);
boolQueryBuilder.filter(filterQuery);
}
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.from((int) (page.getCurrent() - 1) * (int) page.getSize());
searchSourceBuilder.size((int) page.getSize());
List<JSONObject> list = elasticDocument.search("goods", searchSourceBuilder, JSONObject.class);
List<Integer> goodsIdList = list.stream().map(jsonObject -> (Integer) jsonObject.get("id")).collect(Collectors.toList());
if (goodsIdList.size() == 0) {
return R.success().add("goods", Collections.emptyList());
}
// 根据es中返回商品ID查询商品详情并保持es中的排序
List<Goods> goodsList = iGoodsService.searchResult(goodsIdList);
Map<Integer, Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(goods -> Math.toIntExact(goods.getId()), o -> o));
List<Goods> returnGoodsList = new ArrayList<>(goodsList.size());
for (Integer goodsId : goodsIdList) {
returnGoodsList.add(goodsMap.get(goodsId));
}
if (CollectionUtils.isNotEmpty(goodsList)) {
AsyncManager.me().execute(new TimerTask() {
@Override
public void run() {
searchHistory.setHasGoods(true);
iSearchHistoryService.save(searchHistory);
}
});
}
return R.success().add("goods", returnGoodsList);
}
use of com.wayn.common.core.domain.shop.Goods in project waynboot-mall by wayn111.
the class CategoryController method secondCateGoods.
@GetMapping("secondCategoryGoods")
public R secondCateGoods(@RequestParam(defaultValue = "0") Long cateId) {
Page<Goods> page = getPage();
List<Long> cateList = List.of(cateId);
R success = iGoodsService.selectListPageByCateIds(page, cateList);
success.add("category", iCategoryService.getById(cateId));
return success;
}
use of com.wayn.common.core.domain.shop.Goods in project waynboot-mall by wayn111.
the class CartServiceImpl method list.
@Override
public R list(Page<Cart> page, Long userId) {
IPage<Cart> goodsIPage = cartMapper.selectCartPageList(page, userId);
List<Cart> cartList = goodsIPage.getRecords();
List<Long> goodsIdList = cartList.stream().map(Cart::getGoodsId).collect(Collectors.toList());
JSONArray array = new JSONArray();
if (CollectionUtils.isEmpty(goodsIdList)) {
return R.success().add("data", array);
}
Map<Long, Goods> goodsIdMap = iGoodsService.selectGoodsByIds(goodsIdList).stream().collect(Collectors.toMap(Goods::getId, goods -> goods));
for (Cart cart : cartList) {
JSONObject jsonObject = new JSONObject();
try {
MyBeanUtil.copyProperties2Map(cart, jsonObject);
Goods goods = goodsIdMap.get(cart.getGoodsId());
if (goods.getIsNew()) {
jsonObject.put("tag", "新品");
}
if (goods.getIsHot()) {
jsonObject.put("tag", "热品");
}
} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {
log.error(e.getMessage(), e);
}
array.add(jsonObject);
}
return R.success().add("data", array);
}
use of com.wayn.common.core.domain.shop.Goods in project waynboot-mall by wayn111.
the class CategoryController method firstCateGoods.
@GetMapping("firstCategoryGoods")
public R firstCateGoods(@RequestParam(defaultValue = "0") Long cateId) {
Page<Goods> page = getPage();
List<Category> categoryList = iCategoryService.list(new QueryWrapper<Category>().select("id").eq("pid", cateId));
List<Long> cateList = categoryList.stream().map(Category::getId).collect(Collectors.toList());
R success = iGoodsService.selectListPageByCateIds(page, cateList);
success.add("category", iCategoryService.getById(cateId));
return success;
}
Aggregations