美文网首页
买家商品service

买家商品service

作者: 谢谢水果 | 来源:发表于2018-11-19 04:43 被阅读0次

    买家类目service接口

    main--service--ProductService.java

    package com.tkft.sell.service;
    
    import com.tkft.sell.dataobject.ProductInfo;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import java.util.List;
    
    public interface ProductService {
        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);
    
    }
    
    

    买家类目service实现

    main--service--impl--ProductServiceImpl.java

    package com.tkft.sell.service.impl;
    
    import com.tkft.sell.dataobject.ProductInfo;
    import com.tkft.sell.dto.CartDTO;
    import com.tkft.sell.enums.ProductStatusEnum;
    import com.tkft.sell.enums.ResultEnum;
    import com.tkft.sell.exception.SellException;
    import com.tkft.sell.repository.ProductInfoRepository;
    import com.tkft.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 javax.transaction.Transactional;
    import java.util.List;
    @Service
    public class ProductServiceImpl implements ProductService {
    
        @Autowired
        private ProductInfoRepository repository;
    
        @Override
        public ProductInfo findOne(String productId) {
            return repository.getOne(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){
                ProductInfo productInfo = repository.getOne(cartDTO.getProductId());
                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){
                ProductInfo productInfo = repository.getOne(cartDTO.getProductId());
                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);
            }
        }
    }
    
    

    买家类目service测试

    test--service.impl--ProductServiceImplTest.java

    package com.tkft.sell.service.impl;
    
    import com.tkft.sell.dataobject.ProductInfo;
    import com.tkft.sell.enums.ProductStatusEnum;
    import org.junit.Assert;
    import org.junit.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.test.context.junit4.SpringRunner;
    import java.math.BigDecimal;
    import java.util.List;
    import static org.junit.Assert.*;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ProductServiceImplTest {
        @Autowired
        private ProductServiceImpl productService;
        @Test
        public void findOne() {
            ProductInfo result = productService.findOne("123456");
            Assert.assertEquals("123456", result.getProductId());
        }
    
        @Test
        public void findUpAll() {
            List<ProductInfo> result = productService.findUpAll();
            Assert.assertNotEquals(0, result.size());
        }
    
        @Test
        public void findAll() {
            PageRequest request = new PageRequest(0,2);
            Page<ProductInfo> result = productService.findAll(request);
            Assert.assertNotEquals(0, result.getTotalElements());
        }
    
        @Test
        public void save() {
            ProductInfo productInfo = new ProductInfo();
            productInfo.setProductId("123457");
            productInfo.setCategoryType(53);
            productInfo.setProductDescription("好吃好吃的瓜");
            productInfo.setProductName("西瓜");
            productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
            productInfo.setProductIcon("www.apple.com");
            productInfo.setProductPrice(new BigDecimal(3.5));
            productInfo.setProductStock(100);
            ProductInfo result = productService.save(productInfo);
            Assert.assertNotNull(result);
        }
    }
    

    相关文章

      网友评论

          本文标题:买家商品service

          本文链接:https://www.haomeiwen.com/subject/daonfqtx.html