package com.jt.cart.service;
import java.util.List;
import com.jt.common.po.Cart;
public interface CartService {
List<Cart> findCartByUserId(Long userId);
void saveCart(Cart cart);
}
package com.jt.cart.service;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jt.cart.mapper.CartMapper;
import com.jt.common.po.Cart;
@Service
public class CartServiceImpl implements CartService {
@Autowired
private CartMapper cartMapper;
@Override
public List<Cart> findCartByUserId(Long userId) {
Cart cart=new Cart();
cart.setUserId(userId);
return cartMapper.select(cart);
}
/**
* 根据userId和itemId判断数据库中是否有该购物车信息
* 有
* 数据库num+新num做更新操作
* 没有数据
* 应该插入数据库
*/
/* (non-Javadoc)
* @see com.jt.cart.service.CartService#saveCart(com.jt.common.po.Cart)
*/
@Override
public void saveCart(Cart cart) {
Cart cartDB=cartMapper.findCartByUI(cart);
if(cartDB==null){
System.out.println("insert cart");
cart.setCreated(new Date());
cart.setUpdated(cart.getCreated());
cartMapper.insert(cart);
}else{
System.out.println("insert cart2");
int num=cartDB.getNum()+cart.getNum();
cartDB.setNum(num);
cartDB.setUpdated(new Date());
cartMapper.updateByPrimaryKeySelective(cartDB);
}
}
} |