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();
|
}
|
}
|