You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SRuml/CartService.java

90 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.example.service;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Cart;
import com.example.exception.CustomException;
import com.example.mapper.CartMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 购物车业务处理
**/
@Service
public class CartService {
@Resource
private CartMapper cartMapper;
/**
* 新增
*/
public void add(Cart cart) {
// 判断该用户对该商品有没有加入过购物车如果加入过那么只要更新一下该条记录的num+1
Cart dbCart = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
if (ObjectUtil.isNotEmpty(dbCart)) {
dbCart.setNum(dbCart.getNum() + 1);
cartMapper.updateById(dbCart);
} else {
cartMapper.insert(cart);
}
}
/**
* 删除
*/
public void deleteById(Integer id) {
cartMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
cartMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(Cart cart) {
cartMapper.updateById(cart);
}
/**
* 根据ID查询
*/
public Cart selectById(Integer id) {
return cartMapper.selectById(id);
}
/**
* 查询所有
*/
public List<Cart> selectAll(Cart cart) {
return cartMapper.selectAll(cart);
}
/**
* 分页查询
*/
public PageInfo<Cart> selectPage(Cart cart, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
cart.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Cart> list = cartMapper.selectAll(cart);
return PageInfo.of(list);
}
}