|
|
@@ -1,163 +0,0 @@
|
|
|
-package com.seecoder.BlueWhale.serviceImpl;
|
|
|
-
|
|
|
-import com.seecoder.BlueWhale.enums.OrderStatusEnum;
|
|
|
-import com.seecoder.BlueWhale.exception.BlueWhaleException;
|
|
|
-import com.seecoder.BlueWhale.po.Order;
|
|
|
-import com.seecoder.BlueWhale.po.Product;
|
|
|
-import com.seecoder.BlueWhale.po.Store;
|
|
|
-import com.seecoder.BlueWhale.po.User;
|
|
|
-import com.seecoder.BlueWhale.repository.OrderRepository;
|
|
|
-import com.seecoder.BlueWhale.repository.ProductRepository;
|
|
|
-import com.seecoder.BlueWhale.repository.StoreRepository;
|
|
|
-import com.seecoder.BlueWhale.service.OrderService;
|
|
|
-import com.seecoder.BlueWhale.util.SecurityUtil;
|
|
|
-import com.seecoder.BlueWhale.vo.OrderVO;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class OrderServiceImpl implements OrderService {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- OrderRepository orderRepository;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- SecurityUtil securityUtil;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- StoreRepository storeRepository;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- ProductRepository productRepository;
|
|
|
-
|
|
|
- @Override
|
|
|
- public OrderVO create(OrderVO orderVO) {
|
|
|
- Order order = orderVO.toPO();
|
|
|
- order.setUserId(securityUtil.getCurrentUser().getId());
|
|
|
- order.setStatus(OrderStatusEnum.UNPAID);
|
|
|
- order.setCreateTime(new Date());
|
|
|
- orderRepository.save(order);
|
|
|
- return wrapWithProductId(order.toVO());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<OrderVO> getAllOrders() {
|
|
|
- User user=securityUtil.getCurrentUser();
|
|
|
- List<Order> orders = null;
|
|
|
- switch (user.getRole()){
|
|
|
- case CUSTOMER:
|
|
|
- orders = orderRepository.findByUserId(user.getId());
|
|
|
- break;
|
|
|
- case STAFF:
|
|
|
- Integer storeId=user.getStoreId();
|
|
|
- orders = orderRepository.findAll().stream()
|
|
|
- .filter(x-> storeId.equals(productRepository.findById(x.getProductId()).get().getStoreId()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- break;
|
|
|
- case MANAGER:
|
|
|
- case CEO:
|
|
|
- orders = orderRepository.findAll();
|
|
|
- break;
|
|
|
- }
|
|
|
- return orders.stream().map(x->wrapWithProductId(x.toVO())).collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public OrderVO getOrder(Integer id) {
|
|
|
- return wrapWithProductId(orderRepository.findById(id).get().toVO());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Boolean pay(Integer orderId) {
|
|
|
- User user=securityUtil.getCurrentUser();
|
|
|
- Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
- if(order == null){
|
|
|
- throw BlueWhaleException.orderNotExists();
|
|
|
- }
|
|
|
- if(order.getStatus() != OrderStatusEnum.UNPAID){
|
|
|
- throw BlueWhaleException.orderStatusError();
|
|
|
- }
|
|
|
- Product product=productRepository.findById(order.getProductId()).get();
|
|
|
- order.setPaid(product.getPrice()*order.getAmount());
|
|
|
- order.setStatus(OrderStatusEnum.UNSEND);
|
|
|
- orderRepository.save(order);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Boolean deliver(Integer orderId) {
|
|
|
- Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
- if(order == null){
|
|
|
- throw BlueWhaleException.orderNotExists();
|
|
|
- }
|
|
|
- if(order.getStatus() != OrderStatusEnum.UNSEND){
|
|
|
- throw BlueWhaleException.orderStatusError();
|
|
|
- }
|
|
|
- Product product = productRepository.findById(order.getProductId()).get();
|
|
|
- if (product.getStock()<order.getAmount()){
|
|
|
- throw BlueWhaleException.stockNotEnough();
|
|
|
- }
|
|
|
- order.setStatus(OrderStatusEnum.UNGET);
|
|
|
- orderRepository.save(order);
|
|
|
- product.setSalesAmount(product.getSalesAmount()+order.getAmount());
|
|
|
- product.setStock(product.getStock()-order.getAmount());
|
|
|
- productRepository.save(product);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Boolean get(Integer orderId) {
|
|
|
- Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
- if(order == null){
|
|
|
- throw BlueWhaleException.orderNotExists();
|
|
|
- }
|
|
|
- if(order.getStatus() != OrderStatusEnum.UNGET){
|
|
|
- throw BlueWhaleException.orderStatusError();
|
|
|
- }
|
|
|
- order.setStatus(OrderStatusEnum.UNCOMMENT);
|
|
|
- orderRepository.save(order);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Boolean comment(Integer orderId, String comment, Integer rating) {
|
|
|
- Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
- if(order == null){
|
|
|
- throw BlueWhaleException.orderNotExists();
|
|
|
- }
|
|
|
- if(order.getStatus() != OrderStatusEnum.UNCOMMENT){
|
|
|
- throw BlueWhaleException.orderStatusError();
|
|
|
- }
|
|
|
- order.setContent(comment);
|
|
|
- order.setRating(rating);
|
|
|
- order.setStatus(OrderStatusEnum.DONE);
|
|
|
- order.setFinishTime(new Date());
|
|
|
- orderRepository.save(order);
|
|
|
-
|
|
|
- Product product = productRepository.findById(order.getProductId()).get();
|
|
|
- Double currentProductRating = product.getRating();
|
|
|
- product.setRating((currentProductRating*product.getNumber()+rating)/(product.getNumber()+1));
|
|
|
- product.setNumber(product.getNumber()+1);
|
|
|
- productRepository.save(product);
|
|
|
-
|
|
|
- Store store = storeRepository.findById(product.getStoreId()).get();
|
|
|
- Double currentStoreRating = store.getRating();
|
|
|
- store.setRating((currentStoreRating*store.getNumber()+rating)/(store.getNumber()+1));
|
|
|
- store.setNumber(store.getNumber()+1);
|
|
|
- storeRepository.save(store);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- private OrderVO wrapWithProductId(OrderVO orderVO){
|
|
|
- Integer productId=orderVO.getProductId();
|
|
|
- Product product=productRepository.findById(productId).get();
|
|
|
- orderVO.setProductName(product.getName());
|
|
|
- orderVO.setPrice(product.getPrice());
|
|
|
- orderVO.setStoreId(product.getStoreId());
|
|
|
- return orderVO;
|
|
|
- }
|
|
|
-}
|