Skip to content

Commit

Permalink
Merge pull request #175 from CoolPeace-yanolza/develop
Browse files Browse the repository at this point in the history
1.23 14시 30분 배포
  • Loading branch information
tkddn204 authored Jan 23, 2024
2 parents 565200a + 8b8367f commit 9251cd7
Show file tree
Hide file tree
Showing 27 changed files with 329 additions and 113 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/coolpeace/CoolPeaceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableCaching
public class CoolPeaceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.coolpeace.global.jwt.security.JwtPrincipal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -22,6 +23,7 @@ public class AccomodationService {
private final AccommodationRepository accommodationRepository;
private final RoomRepository roomRepository;

@Cacheable(value = "accommodation", key = "#jwtPrincipal.toString()",cacheManager = "contentCacheManager")
public List<AccommodationResponse> getAccommodations(JwtPrincipal jwtPrincipal) {

Long memberId = Long.parseLong(jwtPrincipal.getMemberId());
Expand All @@ -34,6 +36,7 @@ public List<AccommodationResponse> getAccommodations(JwtPrincipal jwtPrincipal)
.toList();
}

@Cacheable(value = "rooms", key = "#accommodationId",cacheManager = "contentCacheManager")
public List<RoomResponse> getRooms(JwtPrincipal jwtPrincipal, Long accommodationId) {

Long memberId = Long.parseLong(jwtPrincipal.getMemberId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coolpeace.domain.batch.job;

import com.coolpeace.domain.batch.tasklet.CouponStatusTasklet;
import com.coolpeace.domain.batch.tasklet.SettlementTasklet;
import com.coolpeace.domain.statistics.service.DailyStatisticsService;
import com.coolpeace.domain.batch.tasklet.CouponTasklet;
Expand Down Expand Up @@ -33,11 +34,10 @@ public Job dailyStatisticsJob(JobRepository jobRepository,
.start(saleStep(jobRepository, platformTransactionManager))
.next(couponStep(jobRepository, platformTransactionManager))
.next(settlementStep(jobRepository, platformTransactionManager))
.next(couponStatusStep(jobRepository,platformTransactionManager))
.build();
}



@Bean
public Step saleStep(JobRepository jobRepository,
PlatformTransactionManager platformTransactionManager) throws Exception{
Expand Down Expand Up @@ -68,4 +68,14 @@ public Step settlementStep(JobRepository jobRepository,
.build();
}

@Bean
public Step couponStatusStep(JobRepository jobRepository,
PlatformTransactionManager platformTransactionManager) {
log.info("couponStatus step start");
return new StepBuilder("couponStatusStep", jobRepository)
.tasklet(new CouponStatusTasklet(dailyStatisticsService), platformTransactionManager)
.listener(customStepListener)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.coolpeace.domain.batch.tasklet;

import com.coolpeace.domain.statistics.service.DailyStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class CouponStatusTasklet implements Tasklet, StepExecutionListener {
private final DailyStatisticsService dailyStatisticsService;

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
log.info("쿠폰상태 Tasklet 시작");
dailyStatisticsService.updateCouponStatusStartExposure();
dailyStatisticsService.updateCouponStatusEndExposure();

return RepeatStatus.FINISHED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public record CouponRegisterRequest(
@NotNull(message = "할인의 유형을 선택해야 합니다.")
@ValidEnum(enumClass = DiscountType.class, message = "올바르지 않은 할인의 유형입니다.")
String discountType,
@NotNull(message = "할인의 값을 입력해야 합니다.")
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,

// 객실 유형
@NotNull(message = "객실의 유형을 선택해야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public record CouponUpdateRequest(
String customerType,
@ValidEnum(enumClass = DiscountType.class, message = "올바르지 않은 할인의 유형입니다.", required = false)
String discountType,
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,
@ValidEnum(enumClass = CouponRoomType.class, message = "올바르지 않은 객실의 유형입니다.", required = false)
String couponRoomType,
Boolean registerAllRoom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.coolpeace.domain.coupon.dto.request.CouponRegisterRequest;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component
public class CouponRegisterRequestValidator implements Validator {
public class CouponRegisterRequestValidator extends CouponRequestValidator {
@Override
public boolean supports(Class<?> clazz) {
return CouponRegisterRequest.class.equals(clazz);
Expand All @@ -17,12 +15,7 @@ public boolean supports(Class<?> clazz) {
public void validate(Object target, Errors errors) {
CouponRegisterRequest request = (CouponRegisterRequest) target;

// 방 등록
if (!request.registerAllRoom()) {
ValidationUtils.rejectIfEmpty(errors,
"registerRooms",
"registerRooms.empty",
"등록할 객실을 선택해주세요.");
}
validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.coolpeace.domain.coupon.dto.request.validator;

import com.coolpeace.domain.coupon.entity.type.DiscountType;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public abstract class CouponRequestValidator implements Validator {

// 방 등록 유효성 검사
protected static void validateRegisterRooms(Errors errors, boolean registerAllRoom) {
if (!registerAllRoom) {
ValidationUtils.rejectIfEmpty(errors,
"registerRooms",
"registerRooms.empty",
"등록할 객실을 선택해주세요.");
}
}

// 할인 유형 유효성 검사
protected static void validateDiscountValues(Errors errors,
String requestedDiscountTypeStr,
Integer requestedDiscountFlatValue,
Integer requestedDiscountFlatRate) {
if (requestedDiscountTypeStr.equals(DiscountType.FIXED_PRICE.getValue())) {
if (requestedDiscountFlatValue == null || requestedDiscountFlatValue <= 0) {
errors.reject("discountFlatValue.empty", "정액 할인의 경우 할인 금액를 입력해야 합니다.");
}
} else {
if (requestedDiscountFlatRate == null || requestedDiscountFlatRate <= 0) {
errors.reject("discountFlatValue.empty", "정률 할인의 경우 할인 비율을 입력해야 합니다.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
package com.coolpeace.domain.coupon.dto.request.validator;

import com.coolpeace.domain.coupon.dto.request.CouponRegisterRequest;
import com.coolpeace.domain.coupon.dto.request.CouponUpdateRequest;
import com.coolpeace.domain.coupon.entity.type.DiscountType;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class CouponUpdateRequestValidator implements Validator {
public class CouponUpdateRequestValidator extends CouponRequestValidator {
@Override
public boolean supports(Class<?> clazz) {
return CouponUpdateRequest.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
CouponRegisterRequest request = (CouponRegisterRequest) target;

// 할인 유형
if (request.discountType().equals(DiscountType.FIXED_PRICE.getValue())) {
if (request.discountValue() < 100) {
errors.reject("discountValue",
"정액 할인일 경우 값을 100 이상으로 설정해야 합니다.");
}
} else {
if (request.discountValue() > 100) {
errors.reject("discountValue",
"정률 할인일 경우 값을 100 이하로 설정해야 합니다.");
}
}

// 특정 객실 등록
if (!request.registerAllRoom()) {
if (CollectionUtils.isEmpty(request.registerRooms())) {
errors.reject("registerRooms.empty",
"등록할 객실 리스트가 비어 있습니다.");
}
}
CouponUpdateRequest request = (CouponUpdateRequest) target;

validateRegisterRooms(errors, request.registerAllRoom());
validateDiscountValues(errors, request.discountType(), request.discountFlatValue(), request.discountFlatRate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.coupon.entity.Coupon;
import com.coolpeace.domain.coupon.entity.type.DiscountType;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -13,7 +14,9 @@ public record CouponResponse(
String couponStatus,
String couponConcatTitle,
String discountType,
Integer discountValue,
Integer discountFlatValue,
Integer discountFlatRate,
Integer maximumDiscountPrice,
String customerType,
String couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -35,7 +38,9 @@ public static CouponResponse from(Coupon coupon) {
coupon.getCouponStatus().getValue(),
coupon.getCouponTitle(),
coupon.getDiscountType().getValue(),
coupon.getDiscountValue(),
coupon.getDiscountType().equals(DiscountType.FIXED_PRICE) ? coupon.getDiscountValue() : null,
coupon.getDiscountType().equals(DiscountType.FIXED_RATE) ? coupon.getDiscountValue() : null,
coupon.getMaximumDiscountPrice(),
coupon.getCustomerType().getValue(),
coupon.getCouponRoomType().getValue(),
coupon.getMinimumReservationPrice(),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/coolpeace/domain/coupon/entity/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Coupon extends BaseTimeEntity {

private Integer discountValue;

private Integer maximumDiscountPrice;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private CustomerType customerType = CustomerType.ALL_CLIENT;
Expand Down Expand Up @@ -83,6 +85,7 @@ public class Coupon extends BaseTimeEntity {
public Coupon(String title,
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -95,6 +98,7 @@ public Coupon(String title,
this.title = title;
this.discountType = discountType;
this.discountValue = discountValue;
this.maximumDiscountPrice = maximumDiscountPrice;
this.customerType = customerType;
this.couponRoomType = couponRoomType;
this.minimumReservationPrice = minimumReservationPrice;
Expand All @@ -111,6 +115,7 @@ public static Coupon from(
String title,
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -125,6 +130,7 @@ public static Coupon from(
title,
discountType,
discountValue,
maximumDiscountPrice,
customerType,
couponRoomType,
minimumReservationPrice,
Expand Down Expand Up @@ -172,6 +178,7 @@ private void updateCouponStatusByExposureDate() {
public void updateCoupon(
DiscountType discountType,
Integer discountValue,
Integer maximumDiscountPrice,
CustomerType customerType,
CouponRoomType couponRoomType,
Integer minimumReservationPrice,
Expand All @@ -183,6 +190,7 @@ public void updateCoupon(
this.title = Optional.ofNullable(title).orElse(this.title);
this.discountType = Optional.ofNullable(discountType).orElse(this.discountType);
this.discountValue = Optional.ofNullable(discountValue).orElse(this.discountValue);
this.maximumDiscountPrice = Optional.ofNullable(maximumDiscountPrice).orElse(this.maximumDiscountPrice);
this.customerType = Optional.ofNullable(customerType).orElse(this.customerType);
this.couponRoomType = Optional.ofNullable(couponRoomType).orElse(this.couponRoomType);
this.minimumReservationPrice = Optional.ofNullable(minimumReservationPrice).orElse(this.minimumReservationPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public interface CouponRepositoryCustom {

List<Coupon> findAllByExposureDate(Accommodation accommodation, LocalDate localDate);

List<Coupon> startExposureCoupons(LocalDate localDate);

List<Coupon> endExposureCoupons(LocalDate localDate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,20 @@ public List<Coupon> findAllByExposureDate(Accommodation accommodation, LocalDate
.and(coupon.exposureStartDate.before(localDate))
.and(coupon.exposureEndDate.after(localDate))).fetch();
}

@Override
public List<Coupon> endExposureCoupons(LocalDate nowDate) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.couponStatus.ne(CouponStatusType.DELETED)
.and((coupon.exposureEndDate.before(nowDate))
.or(coupon.exposureEndDate.eq(nowDate)))).fetch();
}

@Override
public List<Coupon> startExposureCoupons(LocalDate nowDate) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.couponStatus.eq(CouponStatusType.EXPOSURE_WAIT)
.and((coupon.exposureStartDate.after(nowDate))
.or(coupon.exposureStartDate.eq(LocalDate.now())))).fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.coolpeace.domain.coupon.repository.CouponRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,17 +17,21 @@ public class CouponQueryService {

private final CouponRepository couponRepository;

@Cacheable(value = "dailyReport", key = "#accommodationId",cacheManager = "contentCacheManager")
public CouponDailyResponse dailyReport(String jwtPrincipal, Long accommodationId) {
Long memberId = Long.valueOf(jwtPrincipal);

if (Boolean.TRUE.equals(couponRepository.noRegister(memberId, accommodationId))) {
return CouponDailyResponse.from(1,CouponDailyCondition.NO_REGISTER);
}

List<Coupon> expiration3daysCoupons = couponRepository.expiration3days(memberId,
accommodationId);
if (!expiration3daysCoupons.isEmpty()) {
return CouponDailyResponse.from(3, CouponDailyCondition.EXPIRATION_3DAYS,
expiration3daysCoupons.stream().map(Coupon::getCouponTitle).toList());
}

if (Boolean.TRUE.equals(couponRepository.noExposure(memberId, accommodationId))) {
return CouponDailyResponse.from(2, CouponDailyCondition.NO_EXPOSURE);
}
Expand Down
Loading

0 comments on commit 9251cd7

Please sign in to comment.