|
|
@@ -9,9 +9,13 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.seecoder.BlueWhale.enums.OrderStatusEnum;
|
|
|
import com.seecoder.BlueWhale.exception.BlueWhaleException;
|
|
|
+import com.seecoder.BlueWhale.po.Coupon;
|
|
|
+import com.seecoder.BlueWhale.po.CouponGroup;
|
|
|
import com.seecoder.BlueWhale.po.Order;
|
|
|
import com.seecoder.BlueWhale.po.Product;
|
|
|
import com.seecoder.BlueWhale.po.Store;
|
|
|
+import com.seecoder.BlueWhale.repository.CouponGroupRepository;
|
|
|
+import com.seecoder.BlueWhale.repository.CouponRepository;
|
|
|
import com.seecoder.BlueWhale.repository.OrderRepository;
|
|
|
import com.seecoder.BlueWhale.repository.ProductRepository;
|
|
|
import com.seecoder.BlueWhale.repository.StoreRepository;
|
|
|
@@ -34,6 +38,12 @@ public class OrderServiceImpl implements OrderService {
|
|
|
@Autowired
|
|
|
ProductRepository productRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ CouponRepository couponRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ CouponGroupRepository couponGroupRepository;
|
|
|
+
|
|
|
@Override
|
|
|
public OrderVO create(OrderVO orderVO) {
|
|
|
Order order = orderVO.toPO();
|
|
|
@@ -65,11 +75,21 @@ public class OrderServiceImpl implements OrderService {
|
|
|
if(order.getStatus() != OrderStatusEnum.UNPAID){
|
|
|
throw BlueWhaleException.orderStatusError();
|
|
|
}
|
|
|
+
|
|
|
+ Coupon coupon = couponRepository.findById(couponId).orElse(null);
|
|
|
+ CouponGroup couponGroup = couponGroupRepository.findById(coupon.getGroupId()).orElse(null);
|
|
|
+ Product product = productRepository.findById(order.getProductId()).orElse(null);
|
|
|
+ if(coupon == null || coupon.getUserId() != securityUtil.getCurrentUser().getId() || couponGroup == null || couponGroup.getStoreId() != product.getStoreId()){
|
|
|
+ throw BlueWhaleException.couponNotAllowed();
|
|
|
+ }
|
|
|
+
|
|
|
order.setStatus(OrderStatusEnum.UNSEND);
|
|
|
//double price=productRepository.findById(order.getProductId()).get().getPrice();
|
|
|
//order.setPaid(price*order.getAmount());
|
|
|
order.setPaid(calculate(orderId,couponId));
|
|
|
orderRepository.save(order);
|
|
|
+ coupon.setUsed(true);
|
|
|
+ couponRepository.save(coupon);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -138,7 +158,31 @@ public class OrderServiceImpl implements OrderService {
|
|
|
|
|
|
@Override
|
|
|
public Double calculate(Integer orderId, Integer couponId) {
|
|
|
- return 0.0;
|
|
|
+ Order order = orderRepository.findById(orderId).orElse(null);
|
|
|
+ if(order == null){
|
|
|
+ throw BlueWhaleException.orderNotExists();
|
|
|
+ }
|
|
|
+
|
|
|
+ Coupon coupon = couponRepository.findById(couponId).orElse(null);
|
|
|
+ CouponGroup couponGroup = couponGroupRepository.findById(coupon.getGroupId()).orElse(null);
|
|
|
+ Product product = productRepository.findById(order.getProductId()).orElse(null);
|
|
|
+ if(coupon == null || coupon.getUserId() != securityUtil.getCurrentUser().getId() || couponGroup == null || couponGroup.getStoreId() != product.getStoreId()){
|
|
|
+ throw BlueWhaleException.couponNotAllowed();
|
|
|
+ }
|
|
|
+
|
|
|
+ Double originalPrice = product.getPrice()*order.getAmount();
|
|
|
+ if(couponGroup.getSatisfaction()>originalPrice){
|
|
|
+ throw BlueWhaleException.couponNotAllowed();
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (couponGroup.getType()){
|
|
|
+ case FULL_REDUCTION:
|
|
|
+ return originalPrice - couponGroup.getMinus();
|
|
|
+ case SPECIAL:
|
|
|
+ // 待完善
|
|
|
+ return originalPrice;
|
|
|
+ }
|
|
|
+ throw BlueWhaleException.couponNotAllowed();
|
|
|
}
|
|
|
|
|
|
private OrderVO wrapWithProductId(OrderVO orderVO){
|