Skip to content

Commit

Permalink
1일,1주,1달 정산 데이터 조회 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
chobeebee committed Sep 11, 2024
1 parent 2442af4 commit cccb564
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 226 deletions.
82 changes: 33 additions & 49 deletions src/main/java/com/sparta/binplay/controller/PaymentController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.sparta.binplay.controller;

import com.sparta.binplay.dto.DailyTotalAmountDto;
import com.sparta.binplay.dto.MonthlyTotalAmountDto;
import com.sparta.binplay.dto.WeeklyTotalAmountDto;
import com.sparta.binplay.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;

@RestController
Expand All @@ -19,62 +21,44 @@
public class PaymentController {

private final PaymentService paymentService;
/* private final PaymentVideoRepository paymentVideoRepository;
private final PaymentAdRepository paymentAdRepository;

@GetMapping("/video")
public List<PaymentVideo> getDailyVideoPayments(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return paymentVideoRepository.findAllByCreatedAt(date);
}
@GetMapping("/day")
public ResponseEntity<?> getDayPayment(@RequestParam(value = "date", required = false) String today) {
LocalDate date = (today != null) ? LocalDate.parse(today) : LocalDate.now().minusDays(1);

@GetMapping("/ad")
public List<PaymentAd> getDailyAdPayments(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return paymentAdRepository.findAllByCreatedAt(date.atStartOfDay().toLocalDate());
}
DailyTotalAmountDto summary = paymentService.getDailyPaymentSummary(date);

// 모든 비디오의 정산 데이터를 조회하는 API
@GetMapping("/all-videos")
public ResponseEntity<List<PaymentVideoResponseDto>> getAllVideoPayments(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
List<PaymentVideoResponseDto> payments = paymentService.getAllVideoPayments(date);
return ResponseEntity.status(HttpStatus.OK).body(payments);
}
if (summary.getTotalAmount().compareTo(BigDecimal.ZERO) == 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("{\"message\": \"해당 날짜의 데이터가 없습니다.\"}");
}

// 특정 날짜의 비디오 정산 데이터를 계산하는 API
@PostMapping("/calculate-video")
public ResponseEntity<Void> calculateVideoPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
paymentService.calculateVideoPmt(date);
return ResponseEntity.status(HttpStatus.OK).build();
return ResponseEntity.ok(summary);
}

// 특정 날짜의 광고 정산 데이터를 계산하는 API
@PostMapping("/calculate-ad")
public ResponseEntity<Void> calculateAdPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
paymentService.calculateAdPmt(date);
return ResponseEntity.status(HttpStatus.OK).build();
}*/

// 특정 날짜의 총 정산 금액을 조회하는 API
/* @GetMapping("/total-payment")
public ResponseEntity<DailyTotalAmountDto> getDailyTotalPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
DailyTotalAmountDto totalPayment = paymentService.getDailyTotalPayment(date);
return ResponseEntity.status(HttpStatus.OK).body(totalPayment);
}
*/
@GetMapping("/total-payment/day")
public ResponseEntity<DailyTotalAmountDto> getDailyTotalPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
DailyTotalAmountDto totalPayment = paymentService.getDailyTotalPayment(date);
return ResponseEntity.status(HttpStatus.OK).body(totalPayment);
}
@GetMapping("/week")
public ResponseEntity<?> getWeeklyPayment() {

@GetMapping("/total-payment/week")
public ResponseEntity<DailyTotalAmountDto> getWeeklyTotalPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
DailyTotalAmountDto totalPayment = paymentService.getTotalPaymentForWeek(date);
return ResponseEntity.status(HttpStatus.OK).body(totalPayment);
WeeklyTotalAmountDto summary = paymentService.getWeeklyPaymentSummary();

if (summary.getTotalAmountForWeek().compareTo(BigDecimal.ZERO) == 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("{\"message\": \"해당 날짜의 데이터가 없습니다.\"}");
}

return ResponseEntity.ok(summary);
}

@GetMapping("/total-payment/month")
public ResponseEntity<DailyTotalAmountDto> getMonthlyTotalPayment(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
DailyTotalAmountDto totalPayment = paymentService.getTotalPaymentForMonth(date);
return ResponseEntity.status(HttpStatus.OK).body(totalPayment);
@GetMapping("/month")
public ResponseEntity<?> getMonthlyPayment() {

MonthlyTotalAmountDto summary = paymentService.getMonthlyPaymentSummary();

if (summary.getTotalAmountForMonth().compareTo(BigDecimal.ZERO) == 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("{\"message\": \"해당 날짜의 데이터가 없습니다.\"}");
}

return ResponseEntity.ok(summary);
}
}
18 changes: 15 additions & 3 deletions src/main/java/com/sparta/binplay/dto/DailyTotalAmountDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

import lombok.*;

import java.math.BigDecimal;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DailyTotalAmountDto {
private Double totalVideoAmount;
private Double totalAdAmount;
private Double totalAmount;
private BigDecimal totalVideoAmount;
private BigDecimal totalAdAmount;
private BigDecimal totalAmount;
private List<VideoAmountDto> videoBreakdowns;

public DailyTotalAmountDto(BigDecimal totalVideoAmount, BigDecimal totalAdAmount,
List<VideoAmountDto> videoBreakdowns) {
this.totalVideoAmount = totalVideoAmount;
this.totalAdAmount = totalAdAmount;
this.totalAmount = totalVideoAmount.add(totalAdAmount);
this.videoBreakdowns = videoBreakdowns;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/sparta/binplay/dto/MonthlyTotalAmountDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sparta.binplay.dto;

import lombok.*;

import java.math.BigDecimal;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MonthlyTotalAmountDto {
private BigDecimal totalVideoAmountForMonth;
private BigDecimal totalAdAmountForMonth;
private BigDecimal totalAmountForMonth;
private List<VideoAmountDto> videoSummaries;

public MonthlyTotalAmountDto(BigDecimal totalVideoAmountForMonth, BigDecimal totalAdAmountForMonth, List<VideoAmountDto> videoSummaries) {
this.totalVideoAmountForMonth = totalVideoAmountForMonth;
this.totalAdAmountForMonth = totalAdAmountForMonth;
this.totalAmountForMonth = totalVideoAmountForMonth.add(totalAdAmountForMonth);
this.videoSummaries = videoSummaries;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/sparta/binplay/dto/VideoAmountDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.sparta.binplay.dto;

import lombok.*;

import java.math.BigDecimal;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class VideoAmountDto {
private String videoId;
private BigDecimal videoAmount;
private BigDecimal adAmount;

// 비디오 금액을 더하는 메서드
public VideoAmountDto addVideoAmount(BigDecimal amount) {
this.videoAmount = this.videoAmount.add(amount);
return this; // 메서드 체이닝을 위해 반환
}

// 광고 금액을 더하는 메서드
public VideoAmountDto addAdAmount(BigDecimal amount) {
this.adAmount = this.adAmount.add(amount);
return this; // 메서드 체이닝을 위해 반환
}

public BigDecimal getTotalAmount() {
return videoAmount.add(adAmount);
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/sparta/binplay/dto/WeeklyTotalAmountDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sparta.binplay.dto;

import lombok.*;

import java.math.BigDecimal;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class WeeklyTotalAmountDto {
private BigDecimal totalVideoAmountForWeek;
private BigDecimal totalAdAmountForWeek;
private BigDecimal totalAmountForWeek;
private List<VideoAmountDto> videoSummaries;

public WeeklyTotalAmountDto(BigDecimal totalVideoAmountForWeek, BigDecimal totalAdAmountForWeek, List<VideoAmountDto> videoSummaries) {
this.totalVideoAmountForWeek = totalVideoAmountForWeek;
this.totalAdAmountForWeek = totalAdAmountForWeek;
this.totalAmountForWeek = totalVideoAmountForWeek.add(totalAdAmountForWeek);
this.videoSummaries = videoSummaries;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
package com.sparta.binplay.dto.response;

import com.sparta.binplay.batch.domain.payment.PaymentAd;
import com.sparta.binplay.entity.payment.PaymentAd;
import lombok.*;

import java.time.LocalDate;
Expand All @@ -22,4 +21,3 @@ public static PaymentAdResponseDto from(PaymentAd paymentAd) {
.build();
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
package com.sparta.binplay.dto.response;

import com.sparta.binplay.batch.domain.payment.PaymentVideo;
import com.sparta.binplay.entity.payment.PaymentVideo;
import lombok.*;

import java.time.LocalDate;
Expand All @@ -24,4 +23,3 @@ public static PaymentVideoResponseDto from(PaymentVideo paymentVideo) {
.build();
}
}
*/
9 changes: 3 additions & 6 deletions src/main/java/com/sparta/binplay/entity/AdViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import com.sparta.binplay.dto.request.AdViewRequestDto;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import lombok.*;

import java.time.LocalDate;

@Entity
@Getter
@Setter
@Table(name="ad_views")
@NoArgsConstructor
@Builder
Expand All @@ -23,7 +20,7 @@ public class AdViews {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long adViewId;

@CreationTimestamp
//@CreationTimestamp
@Column(name = "created_at", updatable = false, nullable = false)
private LocalDate createdAt;

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/sparta/binplay/entity/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.sparta.binplay.dto.request.StreamRequestDto;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@Table(name = "streams")
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -30,7 +27,7 @@ public class Streams {
@Column(name = "paused_at")
private int pausedAt;

@CreatedDate
//@CreatedDate
@Column(name="created_at", updatable = false)
private LocalDateTime createdAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,22 @@ public class DailyTotalAmount {
private Double totalVideoAmount;
private Double totalAdAmount;
private Double totalAmount;

public DailyTotalAmount(double totalVideoAmount, double totalAdAmount, double totalAmount) {
this.totalVideoAmount = totalVideoAmount;
this.totalAdAmount = totalAdAmount;
this.totalAmount = totalAmount;
}

public double getTotalVideoAmount() {
return totalVideoAmount;
}

public double getTotalAdAmount() {
return totalAdAmount;
}

public double getTotalAmount() {
return totalAmount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@

import com.sparta.binplay.entity.payment.PaymentAd;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public interface PaymentAdRepository extends JpaRepository<PaymentAd, Long> {

//List<PaymentVideo> findAllByCreatedAt(LocalDate date);

//Optional<PaymentAd> findByVideoAdAndCreatedAt(VideoAd videoAd, LocalDate date);

/*@Query("SELECT p FROM PaymentAd p WHERE p.createdAt = :date")
List<PaymentAd> findAllByCreatedAt(@Param("date") LocalDate date);*/

/*@Query("SELECT SUM(p.totalAmount) FROM PaymentAd p WHERE p.createdAt = :date")
Double findTotalAmountByCreatedAt(@Param("date") LocalDate date);*/

@Query("SELECT SUM(p.totalAmount) FROM PaymentAd p WHERE p.createdAt BETWEEN :startDate AND :endDate")
Double findTotalAmountByCreatedAtBetween(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
List<PaymentAd> findByCreatedAt(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@

import com.sparta.binplay.entity.payment.PaymentVideo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

@Repository
public interface PaymentVideoRepository extends JpaRepository<PaymentVideo, Long> {
List<PaymentVideo> findAllByCreatedAtBetween(LocalDateTime startDate, LocalDateTime endDate);

/*Optional<PaymentVideo> findByVideoAndCreatedAt(Videos video, LocalDate date);
*/
/* @Query("SELECT p FROM PaymentVideo p WHERE p.createdAt = :date")
List<PaymentVideo> findAllByCreatedAt(@Param("date") LocalDate date);*/

/*@Query("SELECT SUM(p.totalAmount) FROM PaymentVideo p WHERE p.createdAt = :date")
Double findTotalAmountByCreatedAt(@Param("date") LocalDate date);*/

@Query("SELECT SUM(p.totalAmount) FROM PaymentVideo p WHERE p.createdAt BETWEEN :startDate AND :endDate")
Double findTotalAmountByCreatedAtBetween(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
List<PaymentVideo> findByCreatedAt(LocalDate date);
}
Loading

0 comments on commit cccb564

Please sign in to comment.