Skip to content

Commit 79a1768

Browse files
Check Id generation.
1 parent e56e16c commit 79a1768

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Diff for: src/main/java/an/kte/repository/OrderRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +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 OrderRepository extends ListCrudRepository<OrderDocument, Long> {
11+
Optional<OrderDocument> findTopByOrderByIdDesc();
912
}

Diff for: src/main/java/an/kte/service/OrderService.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import org.springframework.data.repository.ListCrudRepository;
1313
import org.springframework.stereotype.Service;
1414

15+
import java.util.Date;
1516
import java.util.List;
1617
import java.util.Optional;
1718

1819
@Service
1920
@Scope("singleton")
2021
public class OrderService implements CommonService<OrderDocument> {
22+
private static final String START_CHECK_ID = "00100";
2123
@Autowired
2224
private ProductRepository productRepository;
2325
@Autowired
@@ -26,11 +28,19 @@ public class OrderService implements CommonService<OrderDocument> {
2628
private PositionRepository positionRepository;
2729

2830
public OrderDocument constructOrder(Long clientId, List<ProductIdCount> productCountList) {
29-
//TODO implement
31+
Optional<OrderDocument> lastOrder = orderRepository.findTopByOrderByIdDesc();
32+
String checkId = START_CHECK_ID;
33+
if (lastOrder.isPresent()) {
34+
OrderDocument orderDocument = lastOrder.get();
35+
Date date = orderDocument.getDate();
36+
if (isToday(date)) {
37+
checkId = nextCheck(orderDocument.getCheckId());
38+
}
39+
}
3040
return OrderDocument.builder()
3141
.clientId(clientId)
3242
.price(calculateOrderPrice(clientId, productCountList))
33-
.checkId("00100")
43+
.checkId(checkId)
3444
.build();
3545
}
3646

@@ -71,6 +81,21 @@ private long getProductPrice(Long productId) {
7181
.orElse(0L);
7282
}
7383

84+
private boolean isToday(Date date) {
85+
Date now = new Date();
86+
now.setHours(0);
87+
now.setMinutes(0);
88+
now.setSeconds(0);
89+
long dayStart = now.getTime();
90+
return date.getTime() > dayStart;
91+
}
92+
93+
private String nextCheck(String lastCheck) {
94+
int asInt = Integer.parseInt(lastCheck);
95+
asInt++;
96+
return String.format("%05d", asInt);
97+
}
98+
7499
@Override
75100
public ListCrudRepository<OrderDocument, Long> getRepository() {
76101
return orderRepository;

0 commit comments

Comments
 (0)