xuhy
2024-12-20 cf7740957f2b859e541116f2f4603beff8a0c832
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java
@@ -1,24 +1,39 @@
package com.ruoyi.web.controller.api;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.io.resource.ClassPathResource;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TShop;
import com.ruoyi.system.export.SumGeneratorClient;
import com.ruoyi.system.query.ProfitDetailsQuery;
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.service.TShopService;
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.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
 * <p>
@@ -36,12 +51,14 @@
    private final TOrderMealService orderMealService;
    private final TOrderSaleService orderSaleService;
    private final TokenService tokenService;
    private final TShopService shopService;
    @Autowired
    public TDataStatisticsController(TOrderMealService orderMealService, TOrderSaleService orderSaleService, TokenService tokenService) {
    public TDataStatisticsController(TOrderMealService orderMealService, TOrderSaleService orderSaleService, TokenService tokenService, TShopService shopService) {
        this.orderMealService = orderMealService;
        this.orderSaleService = orderSaleService;
        this.tokenService = tokenService;
        this.shopService = shopService;
    }
    @ApiOperation( value = "销售额统计")
@@ -50,35 +67,37 @@
        Integer roleType = tokenService.getLoginUser().getRoleType();
        Long objectId = tokenService.getLoginUser().getObjectId();
        query.setShopId(objectId);
        if(Objects.isNull(query.getTimeType())){
            query.setTimeType(1);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            if(Objects.isNull(query.getTimeType())){
                query.setTimeType(1);
            }
            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));
        }
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        switch (query.getTimeType()){
            case 1:
                // 今日
                startTime = LocalDateTime.MIN;
                endTime = LocalDateTime.MAX;
                break;
            case 2:
                // 昨日
                startTime = LocalDateTime.now().minusDays(1);
                endTime = LocalDateTime.now().minusDays(1);
                break;
            case 3:
                // 近7天
                startTime = LocalDateTime.now().minusDays(7);
                endTime = LocalDateTime.now();
                break;
            case 4:
                // 近30天
                startTime = LocalDateTime.now().minusDays(30);
                endTime = LocalDateTime.now();
                break;
        }
        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
        SalesVolumeVO salesVolumeVO;
        if(roleType == 2){
            salesVolumeVO = orderMealService.salesVolume(query);
@@ -88,42 +107,156 @@
        return AjaxResult.success(salesVolumeVO);
    }
    @ApiOperation( value = "盈利明细")
    @PostMapping(value = "/profitDetails")
    public AjaxResult<ProfitDetailsVO> profitDetails(@RequestBody ProfitDetailsQuery query) {
        Long objectId = tokenService.getLoginUser().getObjectId();
        Integer roleType = tokenService.getLoginUser().getRoleType();
        query.setShopId(objectId);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            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));
            }
        }
        ProfitDetailsVO profitDetailsVO;
        if(roleType.equals(2)){
            profitDetailsVO = orderMealService.profitDetails(query);
        }else {
            profitDetailsVO = orderSaleService.profitDetails(query);
        }
        return AjaxResult.success(profitDetailsVO);
    }
    @ApiOperation( value = "盈利明细导出")
    @PostMapping(value = "/profitDetailsExport")
    public void profitDetailsExport(@RequestBody ProfitDetailsQuery query) {
        Long objectId = tokenService.getLoginUser().getObjectId();
        Integer roleType = tokenService.getLoginUser().getRoleType();
        query.setShopId(objectId);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            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;
        if(roleType.equals(2)){
            list  = orderMealService.profitDetailsExport(query);
        }else {
            list  = orderSaleService.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);
        if(Objects.isNull(query.getTimeType())){
            query.setTimeType(1);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            if(Objects.isNull(query.getTimeType())){
                query.setTimeType(1);
            }
            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));
        }
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        switch (query.getTimeType()){
            case 1:
                // 今日
                startTime = LocalDateTime.MIN;
                endTime = LocalDateTime.MAX;
                break;
            case 2:
                // 昨日
                startTime = LocalDateTime.now().minusDays(1);
                endTime = LocalDateTime.now().minusDays(1);
                break;
            case 3:
                // 近7天
                startTime = LocalDateTime.now().minusDays(7);
                endTime = LocalDateTime.now();
                break;
            case 4:
                // 近30天
                startTime = LocalDateTime.now().minusDays(30);
                endTime = LocalDateTime.now();
                break;
        }
        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
        List<SalesRankingVO> salesRankingVOS;
        PageInfo<SalesRankingVO> salesRankingVOS;
        if(roleType == 2){
            salesRankingVOS = orderMealService.salesRanking(query);
        }else{
@@ -138,31 +271,31 @@
        Integer roleType = tokenService.getLoginUser().getRoleType();
        Long objectId = tokenService.getLoginUser().getObjectId();
        query.setShopId(objectId);
        if(Objects.isNull(query.getTimeType())){
            query.setTimeType(3);
        }
            if(Objects.isNull(query.getTimeType())){
                query.setTimeType(3);
            }
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        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(DateUtils.localDateTimeToDate(startTime));
@@ -173,44 +306,130 @@
        }else{
            orderTrendsVOS = orderSaleService.orderingTrends(query);
        }
        if (!CollectionUtils.isEmpty(orderTrendsVOS) && Objects.isNull(orderTrendsVOS.get(0).getDayTime())){
            orderTrendsVOS.get(0).setDayTime(LocalDate.now().toString());
        }
        return AjaxResult.success(orderTrendsVOS);
    }
    @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())){
            query.setTimeType(1);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            if(Objects.isNull(query.getTimeType())){
                query.setTimeType(1);
            }
            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));
        }
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        switch (query.getTimeType()){
            case 1:
                // 今日
                startTime = LocalDateTime.MIN;
                endTime = LocalDateTime.MAX;
                break;
            case 2:
                // 昨日
                startTime = LocalDateTime.now().minusDays(1);
                endTime = LocalDateTime.now().minusDays(1);
                break;
            case 3:
                // 近7天
                startTime = LocalDateTime.now().minusDays(7);
                endTime = LocalDateTime.now();
                break;
            case 4:
                // 近30天
                startTime = LocalDateTime.now().minusDays(30);
                endTime = LocalDateTime.now();
                break;
        PersonnelStatisticsAndSumVO personnelStatisticsAndSumVO = new PersonnelStatisticsAndSumVO();
        List<PersonnelStatisticsVO> personnelStatisticsVOS = orderMealService.personnelStatistics(query);
        personnelStatisticsAndSumVO.setPersonnelStatisticsVOS(personnelStatisticsVOS);
        if(!CollectionUtils.isEmpty(personnelStatisticsVOS)){
            int sum = personnelStatisticsVOS.stream().filter(e -> e.getPersonCount() != null).mapToInt(PersonnelStatisticsVO::getPersonCount).sum();
            personnelStatisticsAndSumVO.setPersonCountSum(sum);
            // 计算列表中平均金额乘以人数的总和
            personnelStatisticsAndSumVO.setTotalMoney(personnelStatisticsVOS.stream().filter(e->e.getPersonCount() != null).mapToDouble(personnelStatisticsVO -> (personnelStatisticsVO.getAvgAmount().doubleValue() * personnelStatisticsVO.getPersonCount()) / sum).sum());
        }
        query.setStartTime(DateUtils.localDateTimeToDate(startTime));
        query.setEndTime(DateUtils.localDateTimeToDate(endTime));
        return AjaxResult.success(orderMealService.personnelStatistics(query));
        return AjaxResult.success(personnelStatisticsAndSumVO);
    }
    @ApiOperation( value = "汇总导出")
    @PostMapping(value = "/sumExport")
    public void sumExport(@RequestBody ProfitDetailsQuery query) {
        Long objectId = tokenService.getLoginUser().getObjectId();
        Map<String, Object> result = new HashMap<>();
        SumGeneratorClient sumGeneratorClient = new SumGeneratorClient();
        query.setShopId(objectId);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            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));
            }
        }
        sumGeneratorClient.setGeneratorTime(DateUtils.stringToLocalDate(query.getStartTime()) + " ~ " + DateUtils.stringToLocalDate(query.getEndTime()));
        TShop shop = shopService.getById(objectId);
        if(Objects.nonNull(shop)){
            sumGeneratorClient.setShopName(shop.getShopName());
        }
        sumGeneratorClient = orderSaleService.sumExport(query);
        result.put("sumGeneratorClient",sumGeneratorClient);
        //1.获取excel模板
        ClassPathResource classPathResource = new ClassPathResource("template/营业统计报表.xls");
        TemplateExportParams params = new TemplateExportParams(classPathResource.getPath());
        Workbook workbook = ExcelExportUtil.exportExcel(params, result);
        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-dispodition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }