Search in sources :

Example 1 with Goods

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();
}
Also used : GoodsProduct(com.wayn.common.core.domain.shop.GoodsProduct) Goods(com.wayn.common.core.domain.shop.Goods) Cart(com.wayn.mobile.api.domain.Cart)

Example 2 with Goods

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);
}
Also used : MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) java.util(java.util) Keyword(com.wayn.common.core.domain.shop.Keyword) SearchHistory(com.wayn.mobile.api.domain.SearchHistory) ElasticDocument(com.wayn.data.elastic.manager.ElasticDocument) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) NotEmpty(javax.validation.constraints.NotEmpty) BaseController(com.wayn.common.base.controller.BaseController) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) GetMapping(org.springframework.web.bind.annotation.GetMapping) IGoodsService(com.wayn.common.core.service.shop.IGoodsService) AsyncManager(com.wayn.mobile.framework.manager.thread.AsyncManager) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) SearchVO(com.wayn.common.core.domain.vo.SearchVO) IKeywordService(com.wayn.common.core.service.shop.IKeywordService) MobileSecurityUtils(com.wayn.mobile.framework.security.util.MobileSecurityUtils) R(com.wayn.common.util.R) IOException(java.io.IOException) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Goods(com.wayn.common.core.domain.shop.Goods) TimeUnit(java.util.concurrent.TimeUnit) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) MatchPhraseQueryBuilder(org.elasticsearch.index.query.MatchPhraseQueryBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) JSONObject(com.alibaba.fastjson.JSONObject) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IPage(com.baomidou.mybatisplus.core.metadata.IPage) ISearchHistoryService(com.wayn.mobile.api.service.ISearchHistoryService) MatchPhraseQueryBuilder(org.elasticsearch.index.query.MatchPhraseQueryBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Goods(com.wayn.common.core.domain.shop.Goods) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchVO(com.wayn.common.core.domain.vo.SearchVO) JSONObject(com.alibaba.fastjson.JSONObject) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) SearchHistory(com.wayn.mobile.api.domain.SearchHistory) TimeValue(org.elasticsearch.common.unit.TimeValue) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Goods

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;
}
Also used : R(com.wayn.common.util.R) Goods(com.wayn.common.core.domain.shop.Goods) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with Goods

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);
}
Also used : Cart(com.wayn.mobile.api.domain.Cart) LocalDateTime(java.time.LocalDateTime) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) JSONArray(com.alibaba.fastjson.JSONArray) IGoodsProductService(com.wayn.common.core.service.shop.IGoodsProductService) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) Map(java.util.Map) IGoodsService(com.wayn.common.core.service.shop.IGoodsService) ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) ICartService(com.wayn.mobile.api.service.ICartService) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) MobileSecurityUtils(com.wayn.mobile.framework.security.util.MobileSecurityUtils) GoodsProduct(com.wayn.common.core.domain.shop.GoodsProduct) R(com.wayn.common.util.R) Collectors(java.util.stream.Collectors) Goods(com.wayn.common.core.domain.shop.Goods) ReturnCodeEnum(com.wayn.common.enums.ReturnCodeEnum) MyBeanUtil(com.wayn.common.util.bean.MyBeanUtil) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) List(java.util.List) JSONObject(com.alibaba.fastjson.JSONObject) BusinessException(com.wayn.common.exception.BusinessException) CartMapper(com.wayn.mobile.api.mapper.CartMapper) AllArgsConstructor(lombok.AllArgsConstructor) IPage(com.baomidou.mybatisplus.core.metadata.IPage) JSONArray(com.alibaba.fastjson.JSONArray) IntrospectionException(java.beans.IntrospectionException) Goods(com.wayn.common.core.domain.shop.Goods) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(com.alibaba.fastjson.JSONObject) Cart(com.wayn.mobile.api.domain.Cart)

Example 5 with Goods

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;
}
Also used : R(com.wayn.common.util.R) Category(com.wayn.common.core.domain.shop.Category) Goods(com.wayn.common.core.domain.shop.Goods) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Goods (com.wayn.common.core.domain.shop.Goods)7 R (com.wayn.common.util.R)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 JSONObject (com.alibaba.fastjson.JSONObject)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)2 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)2 GoodsProduct (com.wayn.common.core.domain.shop.GoodsProduct)2 IGoodsService (com.wayn.common.core.service.shop.IGoodsService)2 Cart (com.wayn.mobile.api.domain.Cart)2 MobileSecurityUtils (com.wayn.mobile.framework.security.util.MobileSecurityUtils)2 IOException (java.io.IOException)2 LocalDateTime (java.time.LocalDateTime)2 Collectors (java.util.stream.Collectors)2 CollectionUtils (org.apache.commons.collections4.CollectionUtils)2 StringUtils (org.apache.commons.lang3.StringUtils)2 JSONArray (com.alibaba.fastjson.JSONArray)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 ServiceImpl (com.baomidou.mybatisplus.extension.service.impl.ServiceImpl)1 BaseController (com.wayn.common.base.controller.BaseController)1