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(#151 #160 ): 결제 정보 등록 기능 수정 #190

Merged
merged 7 commits into from
Nov 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CouponDTO toDTO (CouponEntity entity) {
.build();
}

public CouponEntity toEntity (CouponDTO dto, CouponCategory category, Admin admin, Member tutor) {
public CouponEntity toAdminCouponEntity (CouponDTO dto, CouponCategory category, Admin admin) {
return CouponEntity.builder()
.couponCode(dto.getCouponCode())
.couponName(dto.getCouponName())
Expand All @@ -50,6 +50,20 @@ public CouponEntity toEntity (CouponDTO dto, CouponCategory category, Admin admi
.couponExpireDate(dto.getCouponExpireDate())
.couponCategory(category)
.admin(admin)
.build();
}

public CouponEntity toTutorCouponEntity (CouponDTO dto, CouponCategory category, Member tutor) {
return CouponEntity.builder()
.couponCode(dto.getCouponCode())
.couponName(dto.getCouponName())
.couponContents(dto.getCouponContents())
.couponDiscountRate(dto.getCouponDiscountRate())
.createdAt(dto.getCreatedAt())
.updatedAt(dto.getUpdatedAt())
.couponStartDate(dto.getCouponStartDate())
.couponExpireDate(dto.getCouponExpireDate())
.couponCategory(category)
.tutor(tutor)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package intbyte4.learnsmate.issue_coupon.service;

import intbyte4.learnsmate.issue_coupon.domain.IssueCoupon;
import intbyte4.learnsmate.coupon.domain.entity.CouponEntity;
import intbyte4.learnsmate.issue_coupon.domain.dto.IssueCouponDTO;
import intbyte4.learnsmate.member.domain.entity.Member;
import jakarta.transaction.Transactional;
Expand All @@ -20,6 +20,8 @@ public interface IssueCouponService {
@Transactional
Map<String, List<IssueCouponDTO>> findAllStudentCoupons(Long studentCode);

void updateCouponUseStatus(IssueCouponDTO issueCouponDTO, Member member, CouponEntity couponEntity);

// 보유중인 쿠폰 조회
// @Transactional
// List<IssueCouponDTO> findAllStudentCoupons(IssueCouponDTO dto, Long studentCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ public Map<String, List<IssueCouponDTO>> findAllStudentCoupons(Long studentCode)

return result;
}

@Override
public void updateCouponUseStatus(IssueCouponDTO issueCouponDTO, Member member, CouponEntity couponEntity) {
issueCouponDTO.setCouponUseStatus(true);
IssueCoupon issueCoupon = issueCouponMapper.toEntity(issueCouponDTO, member, couponEntity);
issueCouponRepository.save(issueCoupon);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import intbyte4.learnsmate.coupon_category.domain.CouponCategory;
import intbyte4.learnsmate.coupon_category.service.CouponCategoryServiceImpl;
import intbyte4.learnsmate.issue_coupon.domain.dto.IssueCouponDTO;
import intbyte4.learnsmate.issue_coupon.service.IssueCouponService;
import intbyte4.learnsmate.lecture.domain.dto.LectureDTO;
import intbyte4.learnsmate.lecture.domain.dto.LectureDetailDTO;
import intbyte4.learnsmate.lecture.domain.entity.Lecture;
Expand Down Expand Up @@ -60,43 +61,55 @@ public class LectureFacade {
private final CouponCategoryServiceImpl couponCategoryService;
private final AdminService adminService;
private final AdminMapper adminMapper;
private final IssueCouponService issueCouponService;

@Transactional
public List<LectureDTO> discountLecturePrice(List<LectureDTO> lectureDTOList, List<IssueCouponDTO> issueCouponDTOList) {
return lectureDTOList.stream()
.map(lectureDTO -> {
MemberDTO memberDTO = new MemberDTO();
memberDTO.setMemberCode(lectureDTO.getTutorCode());
Member tutor = memberMapper.fromMemberDTOtoMember(memberDTO);
Lecture lecture = lectureMapper.toEntity(lectureDTO, tutor);

IssueCouponDTO matchedCoupon = issueCouponDTOList.stream()
.filter(issueCouponDTO -> {
CouponDTO couponDTO = couponService.findCouponDTOByCouponCode(issueCouponDTO.getCouponCode());
CouponCategory couponCategory = couponCategoryService.findByCouponCategoryCode(couponDTO.getCouponCategoryCode());

AdminDTO adminDTO = adminService.findByAdminCode(couponDTO.getAdminCode());
Admin admin = adminMapper.toEntity(adminDTO);

MemberDTO tutorDTO = memberService.findMemberByMemberCode(couponDTO.getTutorCode(), MemberType.TUTOR);
Member tutorEntity = memberMapper.fromMemberDTOtoMember(tutorDTO);

CouponEntity couponEntity = couponMapper.toEntity(couponDTO, couponCategory, admin, tutorEntity);
CouponByLectureDTO couponByLectureDTO = couponByLectureService.findByCouponAndLecture(lecture, couponEntity);
return couponByLectureDTO != null;
})
.findFirst()
.orElse(null);

if (matchedCoupon != null) {
CouponDTO couponDTO = couponService.findCouponDTOByCouponCode(matchedCoupon.getCouponCode());
lectureDTO.setLecturePrice(lectureDTO.getLecturePrice() * (1 - couponDTO.getCouponDiscountRate() / 100));
}
return lectureDTO;
})
.toList();
public LectureDTO discountLecturePrice(LectureDTO lectureDTO, IssueCouponDTO issueCouponDTO) {
Result result = getResult(lectureDTO, issueCouponDTO);
applyDiscountAndSetStatus(result);
return lectureDTO;
}

private void applyDiscountAndSetStatus(Result result) {
CouponEntity couponEntity;

if (result.adminDTO() == null) {
couponEntity = couponMapper.toTutorCouponEntity(result.couponDTOInfo(), result.couponCategory(), result.tutor());
CouponByLectureDTO couponByLectureDTO = couponByLectureService.findByCouponAndLecture(result.lecture(), couponEntity);

Long couponCode = couponByLectureDTO.getCouponCode();
adaptCoupon(result, couponCode, couponEntity);
} else {
Admin admin = adminMapper.toEntity(result.adminDTO());
couponEntity = couponMapper.toAdminCouponEntity(result.couponDTOInfo(), result.couponCategory(), admin);

Long couponCode = couponEntity.getCouponCode();
adaptCoupon(result, couponCode, couponEntity);
}
}

private void adaptCoupon(Result result, Long couponCode, CouponEntity couponEntity) {
CouponDTO couponDTO = couponService.findCouponDTOByCouponCode(couponCode);
result.lectureDTO().setLecturePrice(result.lectureDTO().getLecturePrice() * (1 - couponDTO.getCouponDiscountRate() / 100));
result.issueCouponDTO().setCouponUseStatus(true);

Member student = memberService.findByStudentCode(result.issueCouponDTO.getStudentCode());
issueCouponService.updateCouponUseStatus(result.issueCouponDTO(), student, couponEntity);
}

private Result getResult(LectureDTO lectureDTO, IssueCouponDTO issueCouponDTO) {
MemberDTO memberDTO = memberService.findMemberByMemberCode(lectureDTO.getTutorCode(), MemberType.TUTOR);
Member tutor = memberMapper.fromMemberDTOtoMember(memberDTO);
Lecture lecture = lectureMapper.toEntity(lectureDTO, tutor);
CouponDTO couponDTOInfo = couponService.findCouponDTOByCouponCode(issueCouponDTO.getCouponCode());
CouponCategory couponCategory = couponCategoryService.findByCouponCategoryCode(couponDTOInfo.getCouponCategoryCode());
AdminDTO adminDTO = adminService.findByAdminCode(couponDTOInfo.getAdminCode());

return new Result(lectureDTO, tutor, lecture, issueCouponDTO, couponDTOInfo, couponCategory, adminDTO);
}

private record Result(LectureDTO lectureDTO, Member tutor, Lecture lecture, IssueCouponDTO issueCouponDTO, CouponDTO couponDTOInfo, CouponCategory couponCategory, AdminDTO adminDTO) {
}

@Transactional
public LectureDTO removeLecture(Long lectureCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,25 @@ public ResponseEntity<ResponseFindPaymentVO> getPaymentDetails(@PathVariable("pa

@Operation(summary = "결제 내역 등록")
@PostMapping("/register")
public ResponseEntity<List<ResponseRegisterPaymentVO>> registerPayment
public ResponseEntity<ResponseRegisterPaymentVO> registerPayment
(@RequestBody RequestRegisterPaymentVO requestRegisterPaymentVO) {
List<IssueCouponDTO> issueCouponDTOList = requestRegisterPaymentVO.getIssueCouponVOList().stream()
.map(issueCouponMapper::fromRequestRegisterIssueCouponPaymentVOToDTO)
.toList();

MemberDTO memberDTO = memberMapper
.fromRequestRegisterMemberPaymentVOToMemberDTO(requestRegisterPaymentVO.getMemberVO());

List<LectureDTO> lectureDTOList = requestRegisterPaymentVO.getLectureVOList().stream()
.map(lectureMapper::fromRequestRegisterLecturePaymentVOToDTO)
.toList();

List<LectureDTO> lectures = lectureFacade.discountLecturePrice(lectureDTOList, issueCouponDTOList);

List<PaymentDTO> payments = paymentService.lecturePayment(memberDTO, lectures, issueCouponDTOList);

List<ResponseRegisterPaymentVO> responseList = payments.stream()
.map(paymentMapper::fromPaymentDTOtoResponseRegisterPaymentVO)
.toList();

return ResponseEntity.ok(responseList);
IssueCouponDTO issueCouponDTO = issueCouponMapper.fromRequestRegisterIssueCouponPaymentVOToDTO(requestRegisterPaymentVO.getIssueCouponVO());
MemberDTO memberDTO = memberMapper.fromRequestRegisterMemberPaymentVOToMemberDTO(requestRegisterPaymentVO.getMemberVO());

LectureDTO lectureDTO = lectureMapper.fromRequestRegisterLecturePaymentVOToDTO(requestRegisterPaymentVO.getLectureVO());

if (issueCouponDTO != null) {
lectureDTO = lectureFacade.discountLecturePrice(lectureDTO, issueCouponDTO);
PaymentDTO payment = paymentService.lectureAdaptedPayment(memberDTO, lectureDTO, issueCouponDTO);
ResponseRegisterPaymentVO response = paymentMapper.fromPaymentDTOtoResponseRegisterPaymentVO(payment);
return ResponseEntity.ok(response);
} else {
PaymentDTO payment = paymentService.lectureUnAdaptedPayment(memberDTO, lectureDTO);
ResponseRegisterPaymentVO response = paymentMapper.fromPaymentDTOtoResponseRegisterPaymentVO(payment);
return ResponseEntity.ok(response);
}
}


@Operation(summary = "필터별 결제 내역 조회")
@GetMapping("/filter")
public ResponseEntity<List<PaymentFilterDTO>> getPaymentsByFilters(@RequestBody PaymentFilterRequestVO request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class RequestRegisterPaymentVO {
private List<RequestRegisterIssueCouponPaymentVO> issueCouponVOList;
private RequestRegisterIssueCouponPaymentVO issueCouponVO;
private RequestRegisterMemberPaymentVO memberVO;
private List<RequestRegisterLecturePaymentVO> LectureVOList;
private RequestRegisterLecturePaymentVO LectureVO;

private Long paymentCode;
private Integer paymentPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class PaymentMapper {

// PaymentDTO -> Payment
public Payment toEntity(PaymentDTO paymentDTO, LectureByStudent lectureByStudent, IssueCoupon issueCoupon) {
public Payment toAdaptEntity(PaymentDTO paymentDTO, LectureByStudent lectureByStudent, IssueCoupon issueCoupon) {
return Payment.builder()
.paymentCode(paymentDTO.getPaymentCode())
.paymentPrice(paymentDTO.getPaymentPrice())
Expand All @@ -23,6 +23,15 @@ public Payment toEntity(PaymentDTO paymentDTO, LectureByStudent lectureByStudent
.build();
}

public Payment toEntity(PaymentDTO paymentDTO, LectureByStudent lectureByStudent) {
return Payment.builder()
.paymentCode(paymentDTO.getPaymentCode())
.paymentPrice(paymentDTO.getPaymentPrice())
.createdAt(paymentDTO.getCreatedAt())
.lectureByStudent(lectureByStudent)
.build();
}

// Payment -> PaymentDTO
public PaymentDTO toDTO(Payment payment) {
return PaymentDTO.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package intbyte4.learnsmate.payment.service;

import intbyte4.learnsmate.coupon.domain.dto.CouponDTO;
import intbyte4.learnsmate.issue_coupon.domain.dto.IssueCouponDTO;
import intbyte4.learnsmate.lecture.domain.dto.LectureDTO;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
import intbyte4.learnsmate.payment.domain.dto.PaymentDTO;
import intbyte4.learnsmate.payment.domain.dto.PaymentDetailDTO;
import intbyte4.learnsmate.payment.domain.dto.PaymentFilterDTO;
import intbyte4.learnsmate.payment.domain.vo.PaymentFilterRequestVO;

Expand All @@ -20,5 +18,7 @@ public interface PaymentService {
// 직원이 특정 결제 내역을 단건 상세 조회
PaymentDTO getPaymentDetails(Long paymentCode);

List<PaymentDTO> lecturePayment(MemberDTO memberDTO, List<LectureDTO> lectureDTOList, List<IssueCouponDTO> issueCouponDTOList);
PaymentDTO lectureAdaptedPayment(MemberDTO memberDTO, LectureDTO lectureDTO, IssueCouponDTO issueCouponDTO);

PaymentDTO lectureUnAdaptedPayment(MemberDTO memberDTO, LectureDTO lectureDTO);
}
Loading