购物车业务逻辑
用户添加购物车有两种情况
添加购物车流程
封装实体类
package com.jin.mall.pojo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
@Data
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
public class ProductCarInfo {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.id
*
* @mbg.generated
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.product_id
*
* @mbg.generated
*/
private Integer productId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.product_car
*
* @mbg.generated
*/
private Integer productCar;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.quantity
*
* @mbg.generated
*/
private Integer quantity;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.shop_id
*
* @mbg.generated
*/
private Integer shopId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.price
*
* @mbg.generated
*/
private Double price;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ayy_product_car_info.specification
*
* @mbg.generated
*/
private String specification;
}
自定义封装实体类,商家ID,商家名称,用户ID,购物项
package com.jin.mall.pojo;
import lombok.Data;
import java.util.List;
/*
自定义封装购物车实体类
*/
@Data
public class BuyerCart {
private Integer sellerId;
private String sellerName;
private Integer userid;
private List carInfoList;
}
业务层逻辑
微信小程序案例
package com.jin.mall.service;
import com.jin.mall.mapper.LeaseMapper;
import com.jin.mall.mapper.ProductCarInfoMapper;
import com.jin.mall.mapper.UserMerchantMapper;
import com.jin.mall.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Repository
@Service
public class LeaseCatService {
@Autowired
private LeaseMapper leaseMapper;
@Autowired
private ProductCarInfoMapper infoMapper;
@Autowired
private UserMerchantMapper merchantMapper;
/*
添加购物车
*/
public List addLeaseCarList(List cartlist, Integer id, Integer num){
/*未判断登陆状态*/
LeaseWithBLOBs lease = leaseMapper.selectByPrimaryKey(id);
if (lease.getStock()==null&&"".equals(lease.getStock())){
throw new RuntimeException("此商品不存在");
}
if (!"1".equals(lease.getStatus())){
throw new RuntimeException("此商品审核未通过,禁止购买");
}
Integer sellerId = lease.getShopid();
BuyerCart buyerCart = findBuyerCartShopId(cartlist, sellerId);
if (buyerCart==null){
buyerCart = new BuyerCart();
buyerCart.setSellerId(sellerId);
ArrayList list = new ArrayList<>();
ProductCarInfo item = findByOrderItem(lease, num);
list.add(item);
buyerCart.setCarInfoList(list);
cartlist.add(buyerCart);
} else {
List carInfoList = buyerCart.getCarInfoList();
ProductCarInfo item = findByOrderItem(carInfoList, id);
if (item==null){
item=findByOrderItem(item,num);
carInfoList.add(item);
}else {
item.setQuantity(item.getQuantity()+num);
}if (item.getProductCar()<=0){
carInfoList.remove(item);
}if (carInfoList.size()<=0){
cartlist.remove(buyerCart);
}
}
return cartlist;
}
/*
查询此购物车集合有没有这个卖家的购物车对象
*/
private BuyerCart findBuyerCartShopId(List list,Integer shopId){
if (list!=null){
for (BuyerCart buyerCart : list) {
if (buyerCart.getSellerId().equals(shopId)){
return buyerCart;
}
}
}
return null;
}
/*
查询此购物车集合有没有这个商品,存在返回购物项对象
*/
private ProductCarInfo FindBuyerCartLease(List list,Integer leaseId){
if (list!=null){
for (ProductCarInfo carInfo : list) {
if (carInfo.getProductId().equals(leaseId)){
return carInfo;
}
}
}
return null;
}
/*
创建购物项对象
*/
private ProductCarInfo findByOrderItem(Lease lease, Integer num){
if (num<=0){
throw new RuntimeException("购买数量非法");
}
ProductCarInfo carInfo = new ProductCarInfo();
carInfo.setQuantity(num);
carInfo.setProductId(lease.getLeaseId());
LeaseWithBLOBs leaseWithBLOBs = leaseMapper.selectByPrimaryKey(lease.getLeaseId());
carInfo.setShopId(lease.getShopid());
carInfo.setPrice(lease.getPrice());
carInfo.setSpecification(leaseWithBLOBs.getSpecificationId());
carInfo.setQuantity(num);
carInfo.setProductId(lease.getLeaseId());
return carInfo;
}
/*
获取购物车列表对象
*/
public List getLeaseCar(Integer uid) {
List list = (List) infoMapper.selectByPrimaryKey(uid);
if (list.size() <= 0) {
throw new RuntimeException("未添加商品至购物车");
}
return list;
}
}
表现层
package com.jin.mall.control;
import com.jin.mall.pojo.BuyerCart;
import com.jin.mall.pojo.Result;
import com.jin.mall.service.LeaseCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
public class LeaseCatControl {
@Autowired
private LeaseCatService leaseCatService;
/*
添加商品到购物车
*/
@RequestMapping("addGoodsToCartList")
public Result addGoodsToCartList(Integer uid,Integer leaseId,Integer num){
try {
List list = findCartList(uid);
list = leaseCatService.addLeaseCarList(list, leaseId, num);
return new Result(true,"添加成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"添加失败");
}
}
/*
获取购物车数据
*/
@GetMapping("findCartList")
public List findCartList(Integer uid){
// 判断用户是否登陆,未登陆禁止添加访问购物车
// 已登陆,返回购物车列表对象
return leaseCatService.getLeaseCar(uid);
}
}