1+ package com .backend .global .initdata ;
2+
3+ import com .backend .domain .bid .entity .Bid ;
4+ import com .backend .domain .bid .enums .BidStatus ;
5+ import com .backend .domain .bid .repository .BidRepository ;
6+ import com .backend .domain .member .entity .Member ;
7+ import com .backend .domain .member .repository .MemberRepository ;
8+ import com .backend .domain .notification .service .NotificationQueueService ;
9+ import com .backend .domain .product .entity .Product ;
10+ import com .backend .domain .product .entity .ProductImage ;
11+ import com .backend .domain .product .entity .StandardProduct ;
12+ import com .backend .domain .product .enums .AuctionStatus ;
13+ import com .backend .domain .product .enums .DeliveryMethod ;
14+ import com .backend .domain .product .enums .ProductCategory ;
15+ import com .backend .domain .product .repository .jpa .ProductImageRepository ;
16+ import com .backend .domain .product .repository .jpa .ProductRepository ;
17+ import com .backend .domain .product .service .ProductSyncService ;
18+ import lombok .RequiredArgsConstructor ;
19+ import org .springframework .beans .factory .annotation .Autowired ;
20+ import org .springframework .boot .ApplicationRunner ;
21+ import org .springframework .context .annotation .Bean ;
22+ import org .springframework .context .annotation .Lazy ;
23+ import org .springframework .context .annotation .Profile ;
24+ import org .springframework .security .crypto .password .PasswordEncoder ;
25+ import org .springframework .stereotype .Component ;
26+ import org .springframework .transaction .annotation .Transactional ;
27+
28+ import java .time .LocalDateTime ;
29+ import java .util .ArrayList ;
30+
31+ @ Profile ("prod" )
32+ @ Component
33+ @ RequiredArgsConstructor
34+ public class ProdInitData {
35+
36+ @ Autowired
37+ @ Lazy
38+ private ProdInitData self ;
39+
40+ private final PasswordEncoder passwordEncoder ;
41+ private final MemberRepository memberRepository ;
42+ private final ProductRepository productRepository ;
43+ private final ProductImageRepository productImageRepository ;
44+ private final BidRepository bidRepository ;
45+ private final ProductSyncService productSyncService ;
46+ private final NotificationQueueService notificationQueueService ;
47+
48+ @ Bean
49+ ApplicationRunner prodInitDataApplicationRunner () {
50+ return args -> {
51+ self .work1 ();
52+ productSyncService .indexAllProducts ();
53+ };
54+ }
55+
56+ @ Transactional
57+ public void work1 () {
58+ if (memberRepository .findByEmail ("user5@test.com" ).isPresent ()) {
59+ return ;
60+ }
61+
62+ if (memberRepository .findByEmail ("member2@example.com" ).isEmpty ()) {
63+ return ;
64+ }
65+
66+ Member member1 = memberRepository .findByEmail ("member2@example.com" ).get ();
67+
68+ Member member2 = Member .builder ()
69+ .email ("user5@test.com" )
70+ .password (passwordEncoder .encode ("12341234" ))
71+ .nickname ("입찰자2" )
72+ .phoneNumber ("010-2222-2222" )
73+ .address ("서울특별시 강남구" )
74+ .authority ("ROLE_USER" )
75+ .creditScore (50 )
76+ .products (new ArrayList <>())
77+ .bids (new ArrayList <>())
78+ .notifications (new ArrayList <>())
79+ .boards (new ArrayList <>())
80+ .comments (new ArrayList <>())
81+ .reviews (new ArrayList <>())
82+ .build ();
83+ memberRepository .save (member2 );
84+
85+
86+ Product product = createProduct (member1 , "아이폰 17 pro" , "미개봉 상품입니다!" ,
87+ ProductCategory .DIGITAL_ELECTRONICS , 200000L , LocalDateTime .now ().minusHours (25 ), 24 ,
88+ DeliveryMethod .DELIVERY , "서울 강남구" , "https://i.postimg.cc/85pkBWcS/iphone15-1.jpg" );
89+ product .setStatus (AuctionStatus .SUCCESSFUL .getDisplayName ());
90+ product .setEndTime (LocalDateTime .now ().minusHours (1 ));
91+ product .setCurrentPrice (220000L );
92+
93+
94+ Bid winningBid = bidRepository .save (Bid .builder ()
95+ .product (product )
96+ .member (member2 )
97+ .bidPrice (220000L )
98+ .status (BidStatus .WINNING )
99+ .build ());
100+
101+ product .addBid (winningBid );
102+
103+
104+ // 입찰 성공 알림
105+ notificationQueueService .enqueueNotification (
106+ member2 ,
107+ "'아이폰 17 pro' 상품에 220,000원으로 입찰했습니다." ,
108+ "BID_SUCCESS" ,
109+ product
110+ );
111+
112+ // 낙찰 알림
113+ notificationQueueService .enqueueNotification (
114+ member2 ,
115+ "축하합니다! '아이폰 17 pro' 상품을 낙찰받았습니다!" ,
116+ "AUCTION_WON" ,
117+ product
118+ );
119+
120+ notificationQueueService .enqueueNotification (
121+ member1 ,
122+ "'아이폰 17 pro' 상품이 낙찰되었습니다." ,
123+ "AUCTION_END" ,
124+ product
125+ );
126+ }
127+
128+ private Product createProduct (Member seller , String name , String desc , ProductCategory category ,
129+ Long initialPrice , LocalDateTime startTime , int duration ,
130+ DeliveryMethod deliveryMethod , String location , String imageUrl ) {
131+ Product product = new StandardProduct (name , desc , category , initialPrice ,
132+ startTime , duration , deliveryMethod , location , seller );
133+ product = productRepository .save (product );
134+
135+ // 이미지 추가
136+ ProductImage image = new ProductImage (imageUrl , product );
137+ productImageRepository .save (image );
138+ product .addProductImage (image );
139+
140+ return product ;
141+ }
142+ }
0 commit comments