From b1ee8e40375c9250a1ab698c431d25f0722a4201 Mon Sep 17 00:00:00 2001 From: versatile0010 Date: Sun, 6 Aug 2023 15:45:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20CouponItem=20=EB=A7=8C=EB=A3=8C?= =?UTF-8?q?=EB=90=98=EC=96=B4=EC=95=BC=20=ED=95=98=EB=8A=94=20=EC=A7=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84=20-=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=82=A0=EC=A7=9C=EB=A1=9C=EB=B6=80=ED=84=B0=206=EA=B0=9C?= =?UTF-8?q?=EC=9B=94=EC=9D=B4=20=EC=A7=80=EB=82=9C=20=EC=8B=9C=EC=A0=90?= =?UTF-8?q?=EC=9D=B8=EC=A7=80=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/entity/CouponItem.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/couphoneserver/domain/entity/CouponItem.java b/src/main/java/com/example/couphoneserver/domain/entity/CouponItem.java index a2227ad..67e941b 100644 --- a/src/main/java/com/example/couphoneserver/domain/entity/CouponItem.java +++ b/src/main/java/com/example/couphoneserver/domain/entity/CouponItem.java @@ -4,6 +4,8 @@ import jakarta.persistence.*; import lombok.*; +import java.time.LocalDateTime; + @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -30,9 +32,11 @@ public class CouponItem extends BaseTimeEntity { public void SetBrand(Brand brand) { this.brand = brand; } - public void setStatus(CouponItemStatus status){ + + public void setStatus(CouponItemStatus status) { this.status = status; } + /** * 스탬프 적립 */ @@ -52,16 +56,23 @@ public void collectStamp() { */ public void retrieveStamp() { int currentStampCount = this.stampCount; - if(currentStampCount >= 1){ + if (currentStampCount >= 1) { this.stampCount = currentStampCount - 1; } } @Builder - public CouponItem(Member member, Brand brand){ + public CouponItem(Member member, Brand brand) { this.member = member; this.brand = brand; this.status = CouponItemStatus.INACTIVE; } + + public boolean isExpired() { + LocalDateTime createdDateTime = this.getCreatedDate(); + LocalDateTime expiryDateTime = createdDateTime.plusMonths(6); + // 만료일 이후이면 true (해당 쿠폰은 만료됨 ) + return LocalDateTime.now().isAfter(expiryDateTime); + } } From f68fbba6cd48c9def69f29fd83701cb5b351a5b3 Mon Sep 17 00:00:00 2001 From: versatile0010 Date: Sun, 6 Aug 2023 15:47:24 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20Scheduler=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84=20-=20=EB=A7=A4=EC=9D=BC=20?= =?UTF-8?q?=EC=9E=90=EC=A0=95=EB=A7=88=EB=8B=A4=201=20=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20-=20DB=20=EC=97=90=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=EB=90=9CCouponItem=20=EC=A4=91=20=EB=A7=8C=EB=A3=8C=EB=90=98?= =?UTF-8?q?=EC=96=B4=EC=95=BC=20=ED=95=A0=20=EC=BF=A0=ED=8F=B0=EC=9D=B4=20?= =?UTF-8?q?=EC=9E=88=EC=9C=BC=EB=A9=B4=20STATUS=20=EB=A5=BC=20EXPIRED=20?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SchedulerService.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/com/example/couphoneserver/service/SchedulerService.java diff --git a/src/main/java/com/example/couphoneserver/service/SchedulerService.java b/src/main/java/com/example/couphoneserver/service/SchedulerService.java new file mode 100644 index 0000000..44fa35a --- /dev/null +++ b/src/main/java/com/example/couphoneserver/service/SchedulerService.java @@ -0,0 +1,37 @@ +package com.example.couphoneserver.service; + +import com.example.couphoneserver.domain.entity.CouponItem; +import com.example.couphoneserver.repository.CouponItemRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.List; + +import static com.example.couphoneserver.domain.CouponItemStatus.EXPIRED; + +@Slf4j +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class SchedulerService { + private final CouponItemRepository couponItemRepository; + + @Scheduled(cron = "0 0 0 * * ?") // 매일 자정마다 1회 실행 + @Transactional(readOnly = false) // write DB + public void couponScheduler() { + log.info("[쿠폰 스케쥴러가 실행됩니다. 현재 시간 : " + LocalDateTime.now()); + log.info("[만료 기간이 지난 쿠폰들은 모두 만료 상태로 일괄 변경합니다.]"); + List coupons = couponItemRepository.findAll(); // 쿠폰을 모두 가져와서 + for (CouponItem couponItem : coupons) { + if (couponItem.isExpired() && couponItem.getStatus() != EXPIRED) { // 만료되어야 하면 + String couponId = String.valueOf(couponItem.getId()); + log.info("[ coupon id: " + couponId + " 가 만료되어, Status 를 EXPIRED 으로 변경하였습니다."); + couponItem.setStatus(EXPIRED); // 만료 상태로 변경함 + } + } + } +} From e9158d86a45eca3f0b3cd5a2ae41a0f58f6a8c20 Mon Sep 17 00:00:00 2001 From: versatile0010 Date: Sun, 6 Aug 2023 15:47:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20Scheduler=20=ED=97=88=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/couphoneserver/CouphoneServerApplication.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/couphoneserver/CouphoneServerApplication.java b/src/main/java/com/example/couphoneserver/CouphoneServerApplication.java index c0ed314..8e0ecfd 100644 --- a/src/main/java/com/example/couphoneserver/CouphoneServerApplication.java +++ b/src/main/java/com/example/couphoneserver/CouphoneServerApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableScheduling; -@SpringBootApplication +@EnableScheduling @EnableJpaAuditing +@SpringBootApplication public class CouphoneServerApplication { static { From 029dfbe07355f139dcf8947214f756369e3978ba Mon Sep 17 00:00:00 2001 From: versatile0010 Date: Sun, 6 Aug 2023 15:49:16 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20Scheduler=20=EC=9E=91=EB=8F=99=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=20=EC=9A=A9=20=EB=8D=94=EB=AF=B8=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/import.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/import.sql b/src/main/resources/import.sql index 72deabe..874f6ad 100644 --- a/src/main/resources/import.sql +++ b/src/main/resources/import.sql @@ -23,4 +23,4 @@ INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, me INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (2, 3, 2, '2023-07-30 11:00:00', 1, '2023-07-30 11:00:00', 'EXPIRED'); INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (3, 4, 2, '2023-07-29 11:00:00', 1, '2023-07-30 11:00:00', 'INACTIVE'); INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (4, 1, 2, '2023-07-28 11:00:00', 2, '2023-07-30 11:00:00', 'INACTIVE'); -INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (5, 10, 1, '2023-07-26 11:00:00', 2, '2023-07-30 11:00:00', 'ACTIVE'); +INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (5, 10, 1, '2022-07-26 11:00:00', 2, '2022-07-30 11:00:00', 'ACTIVE');