From 45403c689e2e0d6e69d32e1833faabc116f859e2 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期四, 19 九月 2024 14:58:40 +0800
Subject: [PATCH] 修改接口

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java |  215 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 167 insertions(+), 48 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java
index b736fed..59e98fc 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java
@@ -1,20 +1,35 @@
 package com.ruoyi.web.controller.api;
 
 
+import cn.afterturn.easypoi.excel.ExcelExportUtil;
+import cn.afterturn.easypoi.excel.entity.ExportParams;
+import com.ruoyi.common.basic.PageInfo;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.WebUtils;
 import com.ruoyi.framework.web.service.TokenService;
+import com.ruoyi.system.export.SaleGeneratorListExport;
+import com.ruoyi.system.query.ProfitDetailsQuery;
+import com.ruoyi.system.query.TDataGeneratorQuery;
 import com.ruoyi.system.query.TDataStatisticsQuery;
 import com.ruoyi.system.service.TOrderMealService;
 import com.ruoyi.system.service.TOrderSaleService;
-import com.ruoyi.system.vo.OrderTrendsVO;
-import com.ruoyi.system.vo.PersonnelStatisticsVO;
-import com.ruoyi.system.vo.SalesRankingVO;
-import com.ruoyi.system.vo.SalesVolumeVO;
+import com.ruoyi.system.vo.*;
+import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URLEncoder;
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
@@ -26,6 +41,7 @@
  * @author xiaochen
  * @since 2024-08-27
  */
+@Api(tags = "营业数据统计")
 @RestController
 @RequestMapping("/t-data-statistics")
 public class TDataStatisticsController {
@@ -55,27 +71,27 @@
         switch (query.getTimeType()){
             case 1:
                 // 今日
-                startTime = LocalDateTime.MIN;
-                endTime = LocalDateTime.MAX;
+                startTime = DateUtils.getDayStart(LocalDateTime.now());
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 2:
                 // 昨日
-                startTime = LocalDateTime.now().minusDays(1);
-                endTime = LocalDateTime.now().minusDays(1);
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
                 break;
             case 3:
                 // 近7天
-                startTime = LocalDateTime.now().minusDays(7);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 4:
                 // 近30天
-                startTime = LocalDateTime.now().minusDays(30);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
         }
-        query.setStartTime(startTime);
-        query.setEndTime(endTime);
+        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
         SalesVolumeVO salesVolumeVO;
         if(roleType == 2){
             salesVolumeVO = orderMealService.salesVolume(query);
@@ -85,9 +101,103 @@
         return AjaxResult.success(salesVolumeVO);
     }
 
+    @ApiOperation( value = "盈利明细")
+    @PostMapping(value = "/profitDetails")
+    public AjaxResult<ProfitDetailsVO> profitDetails(@RequestBody ProfitDetailsQuery query) {
+        Long objectId = tokenService.getLoginUser().getObjectId();
+        query.setShopId(objectId);
+        if(Objects.nonNull(query.getTimeType())){
+            LocalDateTime startTime = null;
+            LocalDateTime endTime = null;
+            switch (query.getTimeType()){
+                case 1:
+                    // 今日
+                    startTime = DateUtils.getDayStart(LocalDateTime.now());
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+                case 2:
+                    // 昨日
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
+                    break;
+                case 3:
+                    // 近7天
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+                case 4:
+                    // 近30天
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+            }
+            query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+            query.setEndTime(DateUtils.localDateTimeToDate(endTime));
+        }
+        return AjaxResult.success(orderMealService.profitDetails(query));
+    }
+
+    @ApiOperation( value = "盈利明细导出")
+    @PostMapping(value = "/profitDetailsExport")
+    public void profitDetailsExport(@RequestBody ProfitDetailsQuery query) {
+        Long objectId = tokenService.getLoginUser().getObjectId();
+        query.setShopId(objectId);
+        if(Objects.nonNull(query.getTimeType())){
+            LocalDateTime startTime = null;
+            LocalDateTime endTime = null;
+            switch (query.getTimeType()){
+                case 1:
+                    // 今日
+                    startTime = DateUtils.getDayStart(LocalDateTime.now());
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+                case 2:
+                    // 昨日
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
+                    break;
+                case 3:
+                    // 近7天
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+                case 4:
+                    // 近30天
+                    startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                    endTime = DateUtils.getDayEnd(LocalDateTime.now());
+                    break;
+            }
+            query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+            query.setEndTime(DateUtils.localDateTimeToDate(endTime));
+        }
+        List<GoodsProfitVO> list  = orderMealService.profitDetailsExport(query);
+        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), GoodsProfitVO.class, list);
+        HttpServletResponse response = WebUtils.response();
+        response.setContentType("application/vnd.ms-excel");
+        response.setCharacterEncoding("utf-8");
+        ServletOutputStream outputStream = null;
+        try {
+            String fileName = URLEncoder.encode("盈利明细.xls", "utf-8");
+            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
+            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
+            response.setHeader("Pragma", "no-cache");
+            response.setHeader("Cache-Control", "no-cache");
+            outputStream = response.getOutputStream();
+            workbook.write(outputStream);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                outputStream.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     @ApiOperation( value = "商品分类销售排行")
     @PostMapping(value = "/salesRanking")
-    public AjaxResult<List<SalesRankingVO>> salesRanking(@RequestBody TDataStatisticsQuery query) {
+    public AjaxResult<PageInfo<SalesRankingVO>> salesRanking(@RequestBody TDataStatisticsQuery query) {
         Integer roleType = tokenService.getLoginUser().getRoleType();
         Long objectId = tokenService.getLoginUser().getObjectId();
         query.setShopId(objectId);
@@ -99,28 +209,28 @@
         switch (query.getTimeType()){
             case 1:
                 // 今日
-                startTime = LocalDateTime.MIN;
-                endTime = LocalDateTime.MAX;
+                startTime = DateUtils.getDayStart(LocalDateTime.now());
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 2:
                 // 昨日
-                startTime = LocalDateTime.now().minusDays(1);
-                endTime = LocalDateTime.now().minusDays(1);
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
                 break;
             case 3:
                 // 近7天
-                startTime = LocalDateTime.now().minusDays(7);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 4:
                 // 近30天
-                startTime = LocalDateTime.now().minusDays(30);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
         }
-        query.setStartTime(startTime);
-        query.setEndTime(endTime);
-        List<SalesRankingVO> salesRankingVOS;
+        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
+        PageInfo<SalesRankingVO> salesRankingVOS;
         if(roleType == 2){
             salesRankingVOS = orderMealService.salesRanking(query);
         }else{
@@ -143,27 +253,27 @@
         switch (query.getTimeType()){
             case 1:
                 // 今日
-                startTime = LocalDateTime.MIN;
-                endTime = LocalDateTime.MAX;
+                startTime = DateUtils.getDayStart(LocalDateTime.now());
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 2:
                 // 昨日
-                startTime = LocalDateTime.now().minusDays(1);
-                endTime = LocalDateTime.now().minusDays(1);
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
                 break;
             case 3:
                 // 近7天
-                startTime = LocalDateTime.now().minusDays(7);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 4:
                 // 近30天
-                startTime = LocalDateTime.now().minusDays(30);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
         }
-        query.setStartTime(startTime);
-        query.setEndTime(endTime);
+        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
         List<OrderTrendsVO> orderTrendsVOS;
         if(roleType == 2){
             orderTrendsVOS = orderMealService.orderingTrends(query);
@@ -175,7 +285,7 @@
 
     @ApiOperation( value = "接待人数统计")
     @PostMapping(value = "/personnelStatistics")
-    public AjaxResult<List<PersonnelStatisticsVO>> personnelStatistics(@RequestBody TDataStatisticsQuery query) {
+    public AjaxResult<PersonnelStatisticsAndSumVO> personnelStatistics(@RequestBody TDataStatisticsQuery query) {
         Long objectId = tokenService.getLoginUser().getObjectId();
         query.setShopId(objectId);
         if(Objects.isNull(query.getTimeType())){
@@ -186,28 +296,37 @@
         switch (query.getTimeType()){
             case 1:
                 // 今日
-                startTime = LocalDateTime.MIN;
-                endTime = LocalDateTime.MAX;
+                startTime = DateUtils.getDayStart(LocalDateTime.now());
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 2:
                 // 昨日
-                startTime = LocalDateTime.now().minusDays(1);
-                endTime = LocalDateTime.now().minusDays(1);
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
                 break;
             case 3:
                 // 近7天
-                startTime = LocalDateTime.now().minusDays(7);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
             case 4:
                 // 近30天
-                startTime = LocalDateTime.now().minusDays(30);
-                endTime = LocalDateTime.now();
+                startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
+                endTime = DateUtils.getDayEnd(LocalDateTime.now());
                 break;
         }
-        query.setStartTime(startTime);
-        query.setEndTime(endTime);
-        return AjaxResult.success(orderMealService.personnelStatistics(query));
+        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
+        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
+
+        PersonnelStatisticsAndSumVO personnelStatisticsAndSumVO = new PersonnelStatisticsAndSumVO();
+        List<PersonnelStatisticsVO> personnelStatisticsVOS = orderMealService.personnelStatistics(query);
+        personnelStatisticsAndSumVO.setPersonnelStatisticsVOS(personnelStatisticsVOS);
+        if(!CollectionUtils.isEmpty(personnelStatisticsVOS)){
+            personnelStatisticsAndSumVO.setPersonCountSum(personnelStatisticsVOS.stream().filter(e->e.getPersonCount() != null).mapToInt(PersonnelStatisticsVO::getPersonCount).sum());
+            // 计算列表中平均金额乘以人数的总和
+            personnelStatisticsAndSumVO.setTotalMoney(personnelStatisticsVOS.stream().filter(e->e.getPersonCount() != null).mapToDouble(personnelStatisticsVO -> personnelStatisticsVO.getAvgAmount().doubleValue() * personnelStatisticsVO.getPersonCount()).sum());
+        }
+        return AjaxResult.success(personnelStatisticsAndSumVO);
     }
 
 

--
Gitblit v1.7.1