package cn.iocoder.yudao.module.erp.controller.admin.statistics; import cn.hutool.core.date.LocalDateTimeUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; import cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.sale.ErpSaleCustomerSummaryRespVO; import cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.sale.ErpSaleProductSummaryRespVO; import cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.sale.ErpSaleSummaryRespVO; import cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.sale.ErpSaleTimeSummaryRespVO; import cn.iocoder.yudao.module.erp.service.statistics.ErpSaleStatisticsService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import static cn.hutool.core.date.DatePattern.NORM_MONTH_PATTERN; import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @Tag(name = "管理后台 - ERP 销售统计") @RestController @RequestMapping("/erp/sale-statistics") @Validated public class ErpSaleStatisticsController { @Resource private ErpSaleStatisticsService saleStatisticsService; @GetMapping("/summary") @Operation(summary = "获得销售统计") public CommonResult getSaleSummary() { LocalDateTime today = LocalDateTimeUtils.getToday(); LocalDateTime yesterday = LocalDateTimeUtils.getYesterday(); LocalDateTime month = LocalDateTimeUtils.getMonth(); LocalDateTime year = LocalDateTimeUtils.getYear(); ErpSaleSummaryRespVO summary = new ErpSaleSummaryRespVO() .setTodayPrice(saleStatisticsService.getSalePrice(today, null)) .setYesterdayPrice(saleStatisticsService.getSalePrice(yesterday, today)) .setMonthPrice(saleStatisticsService.getSalePrice(month, null)) .setYearPrice(saleStatisticsService.getSalePrice(year, null)); return success(summary); } @GetMapping("/customer-summary") @Operation(summary = "获得客户销售统计(销售额、已收款、未收款、订单数)") @Parameter(name = "customerId", description = "客户编号,为空表示全部客户") @Parameter(name = "beginTime", description = "下单时间开始", example = "2026-01-01 00:00:00") @Parameter(name = "endTime", description = "下单时间结束", example = "2026-12-31 23:59:59") public CommonResult> getCustomerSummary( @RequestParam(value = "customerId", required = false) Long customerId, @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) LocalDateTime beginTime, @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) LocalDateTime endTime) { return success(saleStatisticsService.getCustomerSummary(customerId, beginTime, endTime)); } @GetMapping("/product-summary") @Operation(summary = "获得产品销售统计(销量、销售额、成本、毛利)") @Parameter(name = "productId", description = "产品编号,为空表示全部产品") public CommonResult> getProductSummary( @RequestParam(value = "productId", required = false) Long productId, @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) LocalDateTime beginTime, @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) LocalDateTime endTime) { return success(saleStatisticsService.getProductSummary(productId, beginTime, endTime)); } @GetMapping("/time-summary") @Operation(summary = "获得销售时间段统计") @Parameter(name = "count", description = "时间段数量", example = "6") public CommonResult> getSaleTimeSummary( @RequestParam(value = "count", defaultValue = "6") Integer count) { List summaryList = new ArrayList<>(); for (int i = count - 1; i >= 0; i--) { LocalDateTime startTime = LocalDateTimeUtils.beginOfMonth(LocalDateTime.now().minusMonths(i)); LocalDateTime endTime = LocalDateTimeUtils.endOfMonth(startTime); summaryList.add(new ErpSaleTimeSummaryRespVO() .setTime(LocalDateTimeUtil.format(startTime, NORM_MONTH_PATTERN)) .setPrice(saleStatisticsService.getSalePrice(startTime, endTime))); } return success(summaryList); } }