Skip to content

Commit 636549c

Browse files
Discount calculation.
1 parent 04bb379 commit 636549c

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

src/main/java/an/kte/repository/DiscountRepository.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.springframework.data.repository.ListCrudRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.Optional;
8+
79
@Repository
810
public interface DiscountRepository extends ListCrudRepository<Discount, Long> {
9-
Discount findTopByOrderByIdDesc();
11+
Optional<Discount> findTopByOrderByIdDesc();
1012
}

src/main/java/an/kte/service/DiscountService.java

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
11
package an.kte.service;
22

3+
import an.kte.model.Client;
34
import an.kte.model.Discount;
5+
import an.kte.model.Product;
6+
import an.kte.repository.ClientRepository;
47
import an.kte.repository.DiscountRepository;
8+
import an.kte.repository.ProductRepository;
59
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Scope;
611
import org.springframework.data.repository.ListCrudRepository;
712
import org.springframework.stereotype.Service;
813

14+
import java.util.Objects;
15+
import java.util.Optional;
16+
917
@Service
10-
public class DiscountService implements CommonService {
18+
@Scope("singleton")
19+
public class DiscountService implements CommonService<Discount> {
20+
private final double MAX_DISCOUNT_PERCENT = .18;
21+
private final long SECOND_DISCOUNT_THRESHOLD = 5;
1122
@Autowired
1223
private DiscountRepository discountRepository;
24+
@Autowired
25+
private ProductRepository productRepository;
26+
@Autowired
27+
private ClientRepository clientRepository;
28+
29+
public long getPositionPrice(Long clientId, Long productId, Long count) {
30+
Optional<Product> product = productRepository.findById(productId);
31+
Optional<Client> client = clientRepository.findById(clientId);
32+
Optional<Discount> actualDiscount = getActualDiscount();
33+
if (product.isPresent()) {
34+
long price = count * product.get().getPrice();
35+
double percent = 0;
36+
if (actualDiscount.isPresent() && Objects.equals(productId, actualDiscount.get().getProductId())) {
37+
percent += actualDiscount.get().getPercent();
38+
}
39+
if (client.isPresent()) {
40+
if (count >= SECOND_DISCOUNT_THRESHOLD && client.get().getDiscount2() != null && client.get().getDiscount2() != 0) {
41+
percent += client.get().getDiscount2();
42+
} else if (client.get().getDiscount1() != null) {
43+
percent += client.get().getDiscount1();
44+
}
45+
}
46+
if (percent > MAX_DISCOUNT_PERCENT) {
47+
percent = MAX_DISCOUNT_PERCENT;
48+
}
49+
return (long) (price * (1 - percent));
50+
} else {
51+
return 0;
52+
}
53+
}
1354

14-
public Discount findTopByOrderByIdDesc() {
55+
public Optional<Discount> getActualDiscount() {
1556
return discountRepository.findTopByOrderByIdDesc();
1657
}
1758

src/main/java/an/kte/service/OrderService.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class OrderService implements CommonService<OrderDocument> {
2626
private OrderRepository orderRepository;
2727
@Autowired
2828
private PositionRepository positionRepository;
29+
@Autowired
30+
private DiscountService discountService;
2931

3032
public OrderDocument constructOrder(Long clientId, List<ProductIdCount> productCountList) {
3133
Optional<OrderDocument> lastOrder = orderRepository.findTopByOrderByIdDesc();
@@ -49,7 +51,10 @@ public OrderDocument saveOrder(Long clientId, List<ProductIdCount> productCountL
4951
final OrderDocument savedOrderDocument = save(orderDocument);
5052
productCountList.stream()
5153
.map(idCount -> {
52-
long price = calculatePositionPrice(idCount.getProductId(), idCount.getCount());
54+
long price = discountService.getPositionPrice(
55+
clientId,
56+
idCount.getProductId(),
57+
idCount.getCount());
5358
long sourcePrice = getProductPrice(idCount.getProductId());
5459
return Position.builder()
5560
.orderId(savedOrderDocument.getId())
@@ -66,13 +71,12 @@ public OrderDocument saveOrder(Long clientId, List<ProductIdCount> productCountL
6671

6772
private long calculateOrderPrice(Long clientId, List<ProductIdCount> productCountList) {
6873
return productCountList.stream()
69-
.map(idCount -> calculatePositionPrice(idCount.getProductId(), idCount.getCount()))
70-
.reduce(Long::sum).orElse(0L);
71-
}
72-
73-
private long calculatePositionPrice(Long productId, Long count) {
74-
Optional<Product> product = productRepository.findById(productId);
75-
return product.map(value -> value.getPrice() * count).orElse(0L);
74+
.map(idCount -> discountService.getPositionPrice(
75+
clientId,
76+
idCount.getProductId(),
77+
idCount.getCount()))
78+
.reduce(Long::sum)
79+
.orElse(0L);
7680
}
7781

7882
private long getProductPrice(Long productId) {

src/main/java/an/kte/service/StatisticService.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import an.kte.repository.ClientStatisticRepository;
66
import an.kte.repository.ProductStatisticRepository;
77
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Scope;
89
import org.springframework.stereotype.Service;
910
import org.springframework.transaction.annotation.Transactional;
1011

@@ -15,6 +16,7 @@
1516
import java.sql.SQLException;
1617

1718
@Service
19+
@Scope("singleton")
1820
public class StatisticService {
1921
@Autowired
2022
private DataSource dataSource;

0 commit comments

Comments
 (0)