wenminjie
2026-08-04 93414e8ee25852aca467213402585d0e65c08a4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.talentcard.server.business.merchant;
 
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
 
import com.talentcard.server.business.PageResult;
import com.talentcard.server.business.merchant.auth.MerchantAuthService;
import com.talentcard.server.business.merchant.auth.MerchantPrincipal;
import com.talentcard.server.common.ApiResponse;
import com.talentcard.server.service.MerchantSettlementStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@Tag(name = "商户端-结算统计", description = "商户端查询当前商户的交易额、政府补贴额、应结算额、首页统计卡片和结算明细导出")
@RestController
@RequestMapping("/api/merchant/settlement-statistics")
public class MerchantSettlementStatisticsController {
    private final MerchantSettlementStatisticsService service;
 
    public MerchantSettlementStatisticsController(MerchantSettlementStatisticsService service) {
        this.service = service;
    }
 
    @Operation(summary = "查询当前商户结算汇总列表", description = "按日或月汇总当前登录商户的已支付且已核销订单")
    @GetMapping("/summaries")
    public ApiResponse<PageResult<Map<String, Object>>> summaries(
            @RequestAttribute(MerchantAuthService.PRINCIPAL_ATTRIBUTE) MerchantPrincipal principal,
            @Parameter(description = "统计周期:DAY日汇总,MONTH月汇总") @RequestParam(defaultValue = "DAY") String periodType,
            @Parameter(description = "开始日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String endDate,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") int size
    ) {
        return ApiResponse.success(service.summaries(principal.merchantId(), null, periodType, startDate, endDate, page, size));
    }
 
    @Operation(summary = "查询当前商户结算总览", description = "汇总当前登录商户指定日期范围的交易额、政府补贴额和应结算额")
    @GetMapping("/overview")
    public ApiResponse<Map<String, Object>> overview(
            @RequestAttribute(MerchantAuthService.PRINCIPAL_ATTRIBUTE) MerchantPrincipal principal,
            @Parameter(description = "开始日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String endDate
    ) {
        return ApiResponse.success(service.overview(principal.merchantId(), null, startDate, endDate));
    }
 
    @Operation(summary = "查询当前商户本月结算总览", description = "用于商户首页展示本月交易额、政府补贴额和应结算额")
    @GetMapping("/current-month")
    public ApiResponse<Map<String, Object>> currentMonth(
            @RequestAttribute(MerchantAuthService.PRINCIPAL_ATTRIBUTE) MerchantPrincipal principal
    ) {
        return ApiResponse.success(service.currentMonthOverview(principal.merchantId()));
    }
 
    @Operation(summary = "查询商户首页统计卡片", description = "返回交易总额、今日核销、本周核销、补贴总额四个卡片数据")
    @GetMapping("/dashboard-cards")
    public ApiResponse<Map<String, Object>> dashboardCards(
            @RequestAttribute(MerchantAuthService.PRINCIPAL_ATTRIBUTE) MerchantPrincipal principal
    ) {
        return ApiResponse.success(service.dashboardCards(principal.merchantId()));
    }
 
    @Operation(summary = "导出结算明细", description = "点击导出按钮生成Excel文件下载;按当前登录商户、时间、状态导出对账明细数据")
    @GetMapping(value = "/export", produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public void export(
            @RequestAttribute(MerchantAuthService.PRINCIPAL_ATTRIBUTE) MerchantPrincipal principal,
            @Parameter(description = "状态:全部、已支付、已核销、未核销、待支付、已取消/已退款") @RequestParam(required = false) String status,
            @Parameter(description = "开始日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期,支持yyyy-MM-dd或yyyy-MM") @RequestParam(required = false) String endDate,
            HttpServletResponse response
    ) throws IOException {
        byte[] data = service.exportSettlementDetailsExcel(principal.merchantId(), status, startDate, endDate);
        String filename = URLEncoder.encode("结算明细.xlsx", StandardCharsets.UTF_8).replace("+", "%20");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").toString());
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + filename);
        response.setContentLength(data.length);
        response.getOutputStream().write(data);
        response.flushBuffer();
    }
}