12
12
import org .springframework .data .repository .ListCrudRepository ;
13
13
import org .springframework .stereotype .Service ;
14
14
15
+ import java .util .Date ;
15
16
import java .util .List ;
16
17
import java .util .Optional ;
17
18
18
19
@ Service
19
20
@ Scope ("singleton" )
20
21
public class OrderService implements CommonService <OrderDocument > {
22
+ private static final String START_CHECK_ID = "00100" ;
21
23
@ Autowired
22
24
private ProductRepository productRepository ;
23
25
@ Autowired
@@ -26,11 +28,19 @@ public class OrderService implements CommonService<OrderDocument> {
26
28
private PositionRepository positionRepository ;
27
29
28
30
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
+ }
30
40
return OrderDocument .builder ()
31
41
.clientId (clientId )
32
42
.price (calculateOrderPrice (clientId , productCountList ))
33
- .checkId ("00100" )
43
+ .checkId (checkId )
34
44
.build ();
35
45
}
36
46
@@ -71,6 +81,21 @@ private long getProductPrice(Long productId) {
71
81
.orElse (0L );
72
82
}
73
83
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
+
74
99
@ Override
75
100
public ListCrudRepository <OrderDocument , Long > getRepository () {
76
101
return orderRepository ;
0 commit comments