|
@@ -8,8 +8,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import com.seecoder.BlueWhale.enums.OrderStatusEnum;
|
|
import com.seecoder.BlueWhale.enums.OrderStatusEnum;
|
|
|
|
|
+import com.seecoder.BlueWhale.exception.BlueWhaleException;
|
|
|
import com.seecoder.BlueWhale.po.Order;
|
|
import com.seecoder.BlueWhale.po.Order;
|
|
|
|
|
+import com.seecoder.BlueWhale.po.Product;
|
|
|
|
|
+import com.seecoder.BlueWhale.po.Store;
|
|
|
import com.seecoder.BlueWhale.repository.OrderRepository;
|
|
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.service.OrderService;
|
|
|
import com.seecoder.BlueWhale.util.SecurityUtil;
|
|
import com.seecoder.BlueWhale.util.SecurityUtil;
|
|
|
import com.seecoder.BlueWhale.vo.OrderVO;
|
|
import com.seecoder.BlueWhale.vo.OrderVO;
|
|
@@ -23,6 +28,12 @@ public class OrderServiceImpl implements OrderService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
SecurityUtil securityUtil;
|
|
SecurityUtil securityUtil;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ StoreRepository storeRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ ProductRepository productRepository;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Boolean create(OrderVO orderVO) {
|
|
public Boolean create(OrderVO orderVO) {
|
|
|
Order order = orderVO.toPO();
|
|
Order order = orderVO.toPO();
|
|
@@ -38,5 +49,87 @@ public class OrderServiceImpl implements OrderService {
|
|
|
List<Order> orders = orderRepository.findByUserId(userId);
|
|
List<Order> orders = orderRepository.findByUserId(userId);
|
|
|
return orders.stream().map(Order::toVO).collect(Collectors.toList());
|
|
return orders.stream().map(Order::toVO).collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Boolean pay(Integer orderId) {
|
|
|
|
|
+ Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
|
|
+ if(order == null){
|
|
|
|
|
+ throw BlueWhaleException.orderNotExists();
|
|
|
|
|
+ }
|
|
|
|
|
+ if(order.getStatus() != OrderStatusEnum.UNPAID){
|
|
|
|
|
+ throw BlueWhaleException.orderStatusError();
|
|
|
|
|
+ }
|
|
|
|
|
+ order.setStatus(OrderStatusEnum.UNSEND);
|
|
|
|
|
+ order.setPaid(1);
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+ order.setStatus(OrderStatusEnum.UNGET);
|
|
|
|
|
+ orderRepository.save(order);
|
|
|
|
|
+
|
|
|
|
|
+ Product product = productRepository.findById(order.getProductId()).orElse(null);
|
|
|
|
|
+ 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);
|
|
|
|
|
+ orderRepository.save(order);
|
|
|
|
|
+
|
|
|
|
|
+ Product product = productRepository.findById(order.getProductId()).orElse(null);
|
|
|
|
|
+ Double currentProductRating = product.getRating();
|
|
|
|
|
+ if(currentProductRating == null){
|
|
|
|
|
+ currentProductRating = 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setRating((currentProductRating*product.getNumber()+rating)/(product.getNumber()+1));
|
|
|
|
|
+ product.setNumber(product.getNumber()+1);
|
|
|
|
|
+ productRepository.save(product);
|
|
|
|
|
+
|
|
|
|
|
+ Store store = storeRepository.findById(product.getStoreId()).orElse(null);
|
|
|
|
|
+ Double currentStoreRating = store.getRating();
|
|
|
|
|
+ if(currentStoreRating == null){
|
|
|
|
|
+ currentStoreRating = 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ store.setRating((currentStoreRating*store.getNumber()+rating)/(store.getNumber()+1));
|
|
|
|
|
+ store.setNumber(store.getNumber()+1);
|
|
|
|
|
+ storeRepository.save(store);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|