一.买家端dao层设计
配置yml文件链接数据库
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://192.168.1.***:3306/sell?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
server:
context-path: /sell
一:类目productCategory设计
1.dao层接口,商品类目productCategoryDao(接口继承springdate-jpa)
public interface ProductCategoryDao extends JpaRepository<ProductCategory, Integer> {
//通过类目编号查
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
2.service层接口
public interface CategoryService {
ProductCategory findOne(Integer id);
List<ProductCategory> findAll();
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
ProductCategory save(ProductCategory productCategory);
}
接口实现
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private ProductCategoryDao productCategoryDao;
@Override
public ProductCategory findOne(Integer id) {
return productCategoryDao.findOne(id);
}
@Override
public List<ProductCategory> findAll() {
return productCategoryDao.findAll();
}
@Override
public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
return productCategoryDao.findByCategoryTypeIn(categoryTypeList);
}
@Override
public ProductCategory save(ProductCategory productCategory) {
return productCategoryDao.save(productCategory);
}
二:商品productInfo设计
1.dao层接口productDao
public interface ProductDao extends JpaRepository<ProductInfo, String> {
//查上架商品
List<ProductInfo> findByProductStatus(Integer productStatus);
}
2.service层接口productService
public interface ProductService {
ProductInfo findOne(String productId);
//查询所有在架商品
List<ProductInfo> findUpAll();
Page<ProductInfo> findAll(Pageable pageable);
ProductInfo save(ProductInfo productInfo);
//加库存
void increase(List<CartDto> cartDtoList);
//减库存
void decrease(List<CartDto> cartDtoList);
}
接口实现
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public ProductInfo findOne(String productId) {
return productDao.findOne(productId);
}
//枚举解决在架商品
@Override
public List<ProductInfo> findUpAll() {
return productDao.findByProductStatus(ProductStatusEnum.UP.getCode());
}
@Override
public Page<ProductInfo> findAll(Pageable pageable) {
return productDao.findAll(pageable);
}
@Override
public ProductInfo save(ProductInfo productInfo) {
return productDao.save(productInfo);
}
@Override
public void increase(List<CartDto> cartDtoList) {
for (CartDto cartDto : cartDtoList) {
ProductInfo productInfo = productDao.findOne(cartDto.getProductId());
if (productInfo == null) {
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
int result = productInfo.getProductStock() + cartDto.getProductQuantity();
productInfo.setProductStock(result);
productDao.save(productInfo);
}
}
//扣库存
@Override
public void decrease(List<CartDto> cartDtoList) {
for (CartDto cartDto : cartDtoList) {
ProductInfo productInfo = productDao.findOne(cartDto.getProductId());
if (productInfo == null) {
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
int result = productInfo.getProductStock() - cartDto.getProductQuantity();
if (result < 0) {
throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
}
productInfo.setProductStock(result);
productDao.save(productInfo);
}
}
}
三:商品状态的枚举productStatusEnum
@Getter
public enum ProductStatusEnum {
UP(0, "在"),
DOWN(1, "下架");
private Integer code;
private String message;
public Integer getCode() {
return code;
}
ProductStatusEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}