package com.jeesharp.business.modules.finance.merchantreceivable.controller;

import com.jeesharp.business.modules.finance.merchantreceivable.application.MerchantReceivableApplicationService;
import com.jeesharp.business.modules.finance.merchantreceivable.entity.MerchantReceivable;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableExportVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableInfoVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableListVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableQueryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableSettlementQueryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableSettlementSummaryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.PlatformCommissionPeriodSummaryVO;
import com.jeesharp.framework.office.excel.util.ExcelExportUtil;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户应收账管理 REST 接口，提供流水查询、账期汇总与导出。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/finance/merchantReceivable/v1")
@Tag(name = "商户应收账")
@AllArgsConstructor
public class MerchantReceivableController extends BaseController {

    private final MerchantReceivableApplicationService merchantReceivableApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取应收流水详情")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:info')")
    public AjaxResult<MerchantReceivableInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(merchantReceivableApplicationService.get(id));
    }

    @PostMapping("list")
    @Operation(summary = "应收流水分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:list')")
    public AjaxResult<PageRespVO<MerchantReceivableListVO>> list(@RequestBody MerchantReceivableQueryVO query) {
        return AjaxResult.okData(merchantReceivableApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("summary")
    @Operation(summary = "按商户账期汇总")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:summary')")
    public AjaxResult<List<MerchantReceivableSettlementSummaryVO>> summary(
            @RequestBody MerchantReceivableSettlementQueryVO query) {
        return AjaxResult.okData(merchantReceivableApplicationService.summarize(query));
    }

    @PostMapping("summaryMine")
    @Operation(summary = "当前商户本租户账期汇总")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:mySummary')")
    public AjaxResult<MerchantReceivableSettlementSummaryVO> summaryMine(
            @RequestBody MerchantReceivableSettlementQueryVO query) {
        return AjaxResult.okData(merchantReceivableApplicationService.summarizeMine(query));
    }

    @PostMapping("platformCommissionTrend")
    @Operation(summary = "平台佣金按日/月趋势汇总")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:summary')")
    public AjaxResult<List<PlatformCommissionPeriodSummaryVO>> platformCommissionTrend(
            @RequestBody MerchantReceivableSettlementQueryVO query) {
        return AjaxResult.okData(merchantReceivableApplicationService.summarizePlatformCommission(query));
    }

    @PostMapping("export")
    @Operation(summary = "导出应收流水")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantReceivable:export')")
    public void export(@RequestBody MerchantReceivableQueryVO query, HttpServletResponse response) {
        ExcelExportUtil.export(response, MerchantReceivableExportVO.class,
                merchantReceivableApplicationService.findList(query), "商户应收账流水");
    }
}
