From 812dd0b6c53f2d1a7a5a583cb501286b25c67c1c Mon Sep 17 00:00:00 2001 From: rivo Date: Sun, 1 Dec 2024 12:40:52 +0800 Subject: [PATCH] FEAT:fixing --- .../app/controller/AnalyticsController.java | 4 ++ .../impl/AnalyticsControllerImpl.java | 7 +++ .../model/response/ResponseSalesReport.java | 1 - .../repositories/OrderProductRepository.java | 11 +++++ .../com/pos/app/service/AnalyticsService.java | 4 ++ .../service/impl/AnalyticsServiceImpl.java | 43 +++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/pos/app/controller/AnalyticsController.java b/src/main/java/com/pos/app/controller/AnalyticsController.java index 71b426e..677d56b 100644 --- a/src/main/java/com/pos/app/controller/AnalyticsController.java +++ b/src/main/java/com/pos/app/controller/AnalyticsController.java @@ -3,6 +3,7 @@ import com.pos.app.annotations.BaseController; import com.pos.app.model.response.BaseResponse; import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -33,4 +34,7 @@ BaseResponse getReportSales( @RequestParam(value = "start-date", required = false) Date startDate, @RequestParam(value = "end-date", required = false) Date endDate ); + + @GetMapping("v1/download-report") + ResponseEntity downloadReport(); } diff --git a/src/main/java/com/pos/app/controller/impl/AnalyticsControllerImpl.java b/src/main/java/com/pos/app/controller/impl/AnalyticsControllerImpl.java index 516c740..569a2d0 100644 --- a/src/main/java/com/pos/app/controller/impl/AnalyticsControllerImpl.java +++ b/src/main/java/com/pos/app/controller/impl/AnalyticsControllerImpl.java @@ -7,6 +7,7 @@ import com.pos.app.utils.ResponseHelper; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; import java.util.Date; @@ -36,4 +37,10 @@ public BaseResponse getAnalyticsChartRevenue(Date startDate, Date endDate) { public BaseResponse getReportSales(Pageable pageable, Date startDate, Date endDate) { return ResponseHelper.createBaseResponse(analyticsService.getReportSales(pageable, startDate, endDate)); } + + @Override + public ResponseEntity downloadReport() { + + return analyticsService.downloadReport(); + } } diff --git a/src/main/java/com/pos/app/model/response/ResponseSalesReport.java b/src/main/java/com/pos/app/model/response/ResponseSalesReport.java index f4e3b53..74c98fd 100644 --- a/src/main/java/com/pos/app/model/response/ResponseSalesReport.java +++ b/src/main/java/com/pos/app/model/response/ResponseSalesReport.java @@ -18,7 +18,6 @@ @JsonSerialize @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) public class ResponseSalesReport { - private String productName; private String productId; private String orderId; diff --git a/src/main/java/com/pos/app/repositories/OrderProductRepository.java b/src/main/java/com/pos/app/repositories/OrderProductRepository.java index 67d3116..6d18032 100644 --- a/src/main/java/com/pos/app/repositories/OrderProductRepository.java +++ b/src/main/java/com/pos/app/repositories/OrderProductRepository.java @@ -28,4 +28,15 @@ public interface OrderProductRepository extends JpaRepository getSalesReport(Pageable pageable); + + @Query(value = "select p.id, p.name," + + " o.id, op.qty, op.pricePerQty," + + " op.totalPrice, t.totalTransaction, " + + "t.taxPercentage, op.createdDate" + + " from OrderProduct as op " + + "join Product as p on op.product.id = p.id " + + "join Order as o on o.id = op.order.id " + + "join Transaction as t on t.order.id = o.id " + + "order by op.createdDate desc ") + List getSalesReport(); } diff --git a/src/main/java/com/pos/app/service/AnalyticsService.java b/src/main/java/com/pos/app/service/AnalyticsService.java index 0ded6bb..ea3c4fe 100644 --- a/src/main/java/com/pos/app/service/AnalyticsService.java +++ b/src/main/java/com/pos/app/service/AnalyticsService.java @@ -5,6 +5,7 @@ import com.pos.app.model.response.ResponseSalesReport; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; import java.util.Date; import java.util.List; @@ -18,4 +19,7 @@ public interface AnalyticsService { List getAnalyticsChartRevenue(Date startDate, Date endDate); Page getReportSales(Pageable pageable, Date startDate, Date endDate); + + ResponseEntity downloadReport(); + } diff --git a/src/main/java/com/pos/app/service/impl/AnalyticsServiceImpl.java b/src/main/java/com/pos/app/service/impl/AnalyticsServiceImpl.java index 12bbe77..c4f3303 100644 --- a/src/main/java/com/pos/app/service/impl/AnalyticsServiceImpl.java +++ b/src/main/java/com/pos/app/service/impl/AnalyticsServiceImpl.java @@ -13,8 +13,14 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import java.io.ByteArrayOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Date; @@ -111,4 +117,41 @@ public Page getReportSales(Pageable pageable, Date startDate, Date endDate) { throw new SystemErrorException(e); } } + + @Override + public ResponseEntity downloadReport() { + List orderProductPage = orderProductRepository.getSalesReport(); + try { + String[] headers = {"Product Name", "Product ID", "order ID", "qty", "price per qty", "total price", "total transaction", "tax", "date"}; + String[][] data = new String[orderProductPage.size()][headers.length]; + + int index = 0; + for (Object[] obj : orderProductPage) { + String[] objData = new String[obj.length]; + for (int i = 0; i < obj.length; i++) { + objData[i] = obj[i] != null ? obj[i].toString() : ""; + } + data[index] = objData; + index++; + } + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + PrintWriter writer = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream)); + + writer.println(String.join(",", headers)); + + for (String[] row : data) { + writer.println(String.join(",", row)); + } + + writer.flush(); + writer.close(); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Content-Disposition", "attachment; filename=data.csv"); + + return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), httpHeaders, HttpStatus.OK); + } catch (Exception e) { + throw new SystemErrorException(e); + } + } }