xuhy
2024-12-20 cf7740957f2b859e541116f2f4603beff8a0c832
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 = "销售额统计")
@@ -361,5 +363,75 @@
    }
    @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();
            }
        }
    }
}