Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 스케쥴러 서비스 구현 #24

Merged
merged 4 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;


@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -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;
}

/**
* 스탬프 적립
*/
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<CouponItem> 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); // 만료 상태로 변경함
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Loading