ProductInfo (商品类)
package com.imooc.sell.dataobject;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
@Entity
@DynamicUpdate
@Data
public class ProductInfo {
@Id
private String productId;
//名字
private String productName;
//单价
private BigDecimal productPrice;
//库存
private Integer productStock;
//描述
private String productDescription;
//小图
private String productIcon;
//状态
private Integer productStatus;
//类型编号
private Integer categoryType;
}
ProductStatusEnum (设置商品状态类)
package com.imooc.sell.enums;
import lombok.Getter;
//商品状态
@Getter
public enum ProductStatusEnum {
UP(0,"在架商品"),
DOWN(1,"下架")
;
private Integer code;
private String message;
ProductStatusEnum(Integer code,String message) {
this.code = code;
this.message=message;
}
}
dao层(repository层)
package com.imooc.sell.dao;
import com.imooc.sell.dataobject.ProductInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ProductInfoRepository extends JpaRepository<ProductInfo,String> {
List<ProductInfo> findByProductStatus(Integer productStatus);
}
ProductService
package com.imooc.sell.service;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.dto.CartDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;
//商品
public interface ProductService {
//查询
Optional<ProductInfo> findOne(String productId);
//查询所有在架商品
List<ProductInfo> findUpAll();
//管理员查询所有商品
Page<ProductInfo> findAll(Pageable pageable);
//保存更新
ProductInfo save(ProductInfo productInfo);
//加库存
void increaseStock(List<CartDTO> cartDTOList);
//减库存
void decreaseStock(List<CartDTO> cartDTOList);
}
ProductServiceImpl
package com.imooc.sell.service.impl;
import com.imooc.sell.dao.ProductInfoRepository;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.dto.CartDTO;
import com.imooc.sell.enums.ProductStatusEnum;
import com.imooc.sell.enums.ResultEnum;
import com.imooc.sell.exception.SellException;
import com.imooc.sell.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductInfoRepository repository;
@Override
public Optional<ProductInfo> findOne(String productId) {
return repository.findById(productId);
}
@Override
public List<ProductInfo> findUpAll() {
return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
}
@Override
public Page<ProductInfo> findAll(Pageable pageable) {
return repository.findAll(pageable);
}
@Override
public ProductInfo save(ProductInfo productInfo) {
return repository.save(productInfo);
}
@Override
@Transactional
public void increaseStock(List<CartDTO> cartDTOList) {
for (CartDTO cartDTO : cartDTOList){
// Optional<ProductInfo> productInfo = repository.findById(cartDTO.getProductId());
ProductInfo productInfo = repository.findById(cartDTO.getProductId()).orElse(null);
if (productInfo==null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
Integer result=productInfo.getProductStock() + cartDTO.getProductQuantity();
productInfo.setProductStock(result);
repository.save(productInfo);
}
}
@Override
@Transactional
public void decreaseStock(List<CartDTO> cartDTOList) {
for (CartDTO cartDTO : cartDTOList){
// Optional<ProductInfo> productInfo = repository.findById(cartDTO.getProductId());
ProductInfo productInfo = repository.findById(cartDTO.getProductId()).orElse(null);
if (productInfo==null){
throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
}
Integer result=productInfo.getProductStock() - cartDTO.getProductQuantity();
if (result < 0){
throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
}
productInfo.setProductStock(result);
repository.save(productInfo);
}
}
}
ProductServiceImplTest (测试类)
package com.imooc.sell.service.impl;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.enums.ProductStatusEnum;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import sun.dc.pr.PRError;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest
class ProductServiceImplTest {
@Autowired
private ProductServiceImpl productService;
@Test
void findOne() {
Optional<ProductInfo> productInfo = productService.findOne("123456");
Assert.assertEquals("123456",productInfo.get().getProductId());
}
@Test
void findUpAll() {
List<ProductInfo> productInfoList = productService.findUpAll();
Assert.assertNotEquals(0,productInfoList.size());
}
@Test
void findAll() {
PageRequest request =PageRequest.of(0,4);
Page<ProductInfo> productInfoPage = productService.findAll(request);
// System.out.println( productInfoPage.getTotalElements());
Assert.assertNotEquals(0,productInfoPage.getTotalElements());
}
@Test
void save() {
ProductInfo productInfo =new ProductInfo();
productInfo.setProductId("123457");
productInfo.setProductName("皮皮虾");
productInfo.setProductPrice(new BigDecimal(0.01));
productInfo.setProductStock(100);
productInfo.setProductDescription("很好吃的虾");
productInfo.setProductIcon("http://xxxx.jpg");
productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
productInfo.setCategoryType(7);
ProductInfo result = productService.save(productInfo);
Assert.assertNotNull(result);
}
}