Search in sources :

Example 1 with GoodsProduct

use of com.wayn.common.core.domain.shop.GoodsProduct 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 GoodsProduct

use of com.wayn.common.core.domain.shop.GoodsProduct in project waynboot-mall by wayn111.

the class CartServiceImpl method addDefaultGoodsProduct.

@Override
public R addDefaultGoodsProduct(Cart cart) {
    Long goodsId = cart.getGoodsId();
    List<GoodsProduct> products = iGoodsProductService.list(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId));
    List<GoodsProduct> goodsProducts = products.stream().filter(GoodsProduct::getDefaultSelected).collect(Collectors.toList());
    GoodsProduct defaultProduct;
    // 如果默认选中货品不为空则取默认选中货品,否则取第一个货品
    if (CollectionUtils.isNotEmpty(goodsProducts)) {
        defaultProduct = goodsProducts.get(0);
    } else {
        defaultProduct = products.get(0);
    }
    cart.setProductId(defaultProduct.getId());
    return this.add(cart);
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) GoodsProduct(com.wayn.common.core.domain.shop.GoodsProduct)

Example 3 with GoodsProduct

use of com.wayn.common.core.domain.shop.GoodsProduct in project waynboot-mall by wayn111.

the class CartServiceImpl method changeNum.

@Override
public R changeNum(Long cartId, Integer number) {
    Cart cart = getById(cartId);
    Long productId = cart.getProductId();
    GoodsProduct goodsProduct = iGoodsProductService.getById(productId);
    Integer productNumber = goodsProduct.getNumber();
    if (number > productNumber) {
        throw new BusinessException(String.format("库存不足,该商品只剩%d件了", productNumber));
    }
    boolean update = lambdaUpdate().setSql("number = " + number).eq(Cart::getId, cartId).update();
    return R.result(update);
}
Also used : BusinessException(com.wayn.common.exception.BusinessException) GoodsProduct(com.wayn.common.core.domain.shop.GoodsProduct) Cart(com.wayn.mobile.api.domain.Cart)

Aggregations

GoodsProduct (com.wayn.common.core.domain.shop.GoodsProduct)3 Cart (com.wayn.mobile.api.domain.Cart)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 Goods (com.wayn.common.core.domain.shop.Goods)1 BusinessException (com.wayn.common.exception.BusinessException)1