xuhy
2025-03-12 da6c062284cbabd038668591758eba10094cc72b
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TDataStatisticsController.java
@@ -3,26 +3,28 @@
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.export.SaleGeneratorListExport;
import com.ruoyi.system.domain.TShop;
import com.ruoyi.system.export.SumGeneratorClient;
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.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.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;
@@ -31,9 +33,7 @@
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
 * <p>
@@ -51,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 = "销售额统计")
@@ -109,6 +111,7 @@
    @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())){
@@ -140,13 +143,20 @@
                query.setEndTime(DateUtils.localDateTimeToDate(endTime));
            }
        }
        return AjaxResult.success(orderMealService.profitDetails(query));
        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())){
@@ -179,7 +189,12 @@
                query.setEndTime(DateUtils.localDateTimeToDate(endTime));
            }
        }
        List<GoodsProfitVO> list  = orderMealService.profitDetailsExport(query);
        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");
@@ -256,38 +271,35 @@
        Integer roleType = tokenService.getLoginUser().getRoleType();
        Long objectId = tokenService.getLoginUser().getObjectId();
        query.setShopId(objectId);
        if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
            if(Objects.isNull(query.getTimeType())){
                query.setTimeType(3);
            }
            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 = 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<OrderTrendsVO> orderTrendsVOS;
        if(roleType == 2){
            orderTrendsVOS = orderMealService.orderingTrends(query);
@@ -342,13 +354,84 @@
        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());
            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());
            personnelStatisticsAndSumVO.setTotalMoney(personnelStatisticsVOS.stream().filter(e->e.getPersonCount() != null).mapToDouble(personnelStatisticsVO -> (personnelStatisticsVO.getAvgAmount().doubleValue() * personnelStatisticsVO.getPersonCount()) / sum).sum());
        }
        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 = orderMealService.sumExport(query);
        sumGeneratorClient.setGeneratorTime(DateUtils.stringToLocalDateTime(query.getStartTime()).toLocalDate() + " ~ " + DateUtils.stringToLocalDateTime(query.getEndTime()).toLocalDate());
        TShop shop = shopService.getById(objectId);
        if(Objects.nonNull(shop)){
            sumGeneratorClient.setShopName(shop.getShopName());
        }
        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();
            }
        }
    }
}