jiangqs
2023-05-22 65816321c28dab1f43d0563af67dbcbad2c5bdca
ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/order/ShoppingCartServiceImpl.java
@@ -2,21 +2,24 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.order.domain.dto.AppBaseBathDto;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.order.domain.dto.AppShoppingCartAddDto;
import com.ruoyi.order.domain.dto.AppShoppingCartChangeDto;
import com.ruoyi.order.domain.pojo.goods.Goods;
import com.ruoyi.order.domain.pojo.order.ShoppingCart;
import com.ruoyi.order.domain.vo.AppShoppingCartVo;
import com.ruoyi.order.mapper.order.ShoppingCartMapper;
import com.ruoyi.order.service.goods.GoodsService;
import com.ruoyi.order.service.order.ShoppingCartService;
import com.ruoyi.system.api.constant.AppErrorConstant;
import com.ruoyi.system.api.domain.dto.AppBaseBathDto;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * <p>
@@ -41,18 +44,23 @@
     */
    @Override
    public void addShoppingCart(AppShoppingCartAddDto appShoppingCartAddDto){
        Long goodsId = appShoppingCartAddDto.getGoodsId();
        String goodsId = appShoppingCartAddDto.getGoodsId();
        Goods goods = goodsService.getById(goodsId);
        if(goods.getGoodsStatus()!=1){
            throw new ServiceException(AppErrorConstant.GOODS_DOWN);
        }
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShoppingCart::getDelFlag, 0).eq(ShoppingCart::getGoodsId, goodsId);
        ShoppingCart shoppingCart = this.getOne(queryWrapper);
        queryWrapper.eq(ShoppingCart::getDelFlag, 0)
                .eq(ShoppingCart::getGoodsId, goodsId)
                .eq(ShoppingCart::getUserId, appShoppingCartAddDto.getUserId())
                .eq(ShoppingCart::getShopId, appShoppingCartAddDto.getShopId());
        ShoppingCart shoppingCart = this.getOne(queryWrapper,false);
        if(shoppingCart==null){
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(appShoppingCartAddDto.getUserId());
            shoppingCart.setShopId(appShoppingCartAddDto.getShopId());
            shoppingCart.setGoodsId(appShoppingCartAddDto.getGoodsId());
            shoppingCart.setBuyNum(appShoppingCartAddDto.getBuyNum());
            shoppingCart.setDelFlag(0);
            shoppingCart.setCreateTime(new Date());
@@ -70,14 +78,13 @@
     */
    @Override
    public void changeShoppingCart(AppShoppingCartChangeDto appShoppingCartChangeDto){
        Long goodsId = appShoppingCartChangeDto.getGoodsId();
        Long shoppingCartId = appShoppingCartChangeDto.getShoppingCartId();
        ShoppingCart shoppingCart = this.getById(shoppingCartId);
        String goodsId = shoppingCart.getGoodsId();
        Goods goods = goodsService.getById(goodsId);
        if(goods.getGoodsStatus()!=1){
            throw new ServiceException(AppErrorConstant.GOODS_DOWN);
        }
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShoppingCart::getDelFlag, 0).eq(ShoppingCart::getGoodsId, goodsId);
        ShoppingCart shoppingCart = this.getOne(queryWrapper);
        shoppingCart.setBuyNum(appShoppingCartChangeDto.getBuyNum());
        shoppingCart.setUpdateTime(new Date());
        this.saveOrUpdate(shoppingCart);
@@ -89,7 +96,32 @@
     */
    @Override
    public void deleteShoppingCart(AppBaseBathDto appBaseBathDto){
        String ids = appBaseBathDto.getIds();
        shoppingCartMapper.deleteShoppingCartByIds(ids);
        String[] ids = appBaseBathDto.getIds().split(",");
        for(String str : ids){
            shoppingCartMapper.deleteShoppingCartById(Long.valueOf(str));
        }
        /*List<String> list = Arrays.asList(ids);
        shoppingCartMapper.deleteShoppingCartByIds(list);*/
    }
    /**
     *
     * @param userId
     * @param shopId
     * @return
     */
    @Override
    public List<AppShoppingCartVo> listShoppingCartVo(Long userId, Long shopId){
        return shoppingCartMapper.listShoppingCartVo(userId, shopId);
    }
    /**
     * 删除购物车
     * @param
     */
    @Override
    public void deleteByUserIdAndGoodsId(Long userId,String goodsId){
        shoppingCartMapper.deleteByUserIdAndGoodsId(userId,goodsId);
    }
}