CouponService.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.izouma.dingdong.service;
  2. import com.izouma.dingdong.domain.Coupon;
  3. import com.izouma.dingdong.dto.CouponDTO;
  4. import com.izouma.dingdong.repo.CouponRepo;
  5. import com.izouma.dingdong.repo.merchant.MerchantRepo;
  6. import com.izouma.dingdong.service.merchant.MerchantService;
  7. import lombok.AllArgsConstructor;
  8. import org.springframework.stereotype.Service;
  9. import java.time.LocalDate;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12. @Service
  13. @AllArgsConstructor
  14. public class CouponService {
  15. private CouponRepo couponRepo;
  16. private MerchantService merchantService;
  17. private MerchantRepo merchantRepo;
  18. public List<Coupon> my(Long userId, Boolean isAll) {
  19. Long merchantId = merchantService.findMerchantId(userId);
  20. List<Coupon> coupons = couponRepo.findAllByMerchantIdAndEnabledTrue(merchantId);
  21. if (isAll) {
  22. return coupons.stream()
  23. .filter(c -> !c.getStartDate().isBefore(LocalDate.now()))
  24. .collect(Collectors.toList());
  25. }
  26. return coupons.stream().filter(c -> !c.getEndDate().isBefore(LocalDate.now())).collect(Collectors.toList());
  27. }
  28. public CouponDTO toDTO(Coupon coupon) {
  29. CouponDTO dto = CouponDTO.builder()
  30. .id(coupon.getId())
  31. .amount(coupon.getAmount())
  32. .endDate(coupon.getEndDate())
  33. .fullAmount(coupon.getFullAmount())
  34. .name(coupon.getName())
  35. .startDate(coupon.getStartDate())
  36. .build();
  37. if (coupon.getFullAmount() == null) {
  38. dto.setDescription("无使用限制");
  39. } else {
  40. dto.setDescription("满" + coupon.getFullAmount() + "减" + coupon.getAmount());
  41. }
  42. merchantRepo.findById(coupon.getMerchantId()).ifPresent(m -> dto.setMerchantName(m.getShowName()));
  43. return dto;
  44. }
  45. }