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

import com.jeesharp.business.modules.finance.reconcile.application.FinanceReconcileApplicationService;
import com.jeesharp.business.modules.finance.reconcile.vo.FinanceReconcileExportVO;
import com.jeesharp.business.modules.finance.reconcile.vo.FinanceReconcileQueryVO;
import com.jeesharp.business.modules.finance.reconcile.vo.FinanceReconcileSummaryVO;
import com.jeesharp.framework.office.excel.util.ExcelExportUtil;
import com.jeesharp.framework.pojo.AjaxResult;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 财务对账 REST 接口，提供汇总预览与 Excel 导出。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/finance/reconcile/v1")
@Tag(name = "财务对账")
@AllArgsConstructor
public class FinanceReconcileController extends BaseController {

    private final FinanceReconcileApplicationService financeReconcileApplicationService;

    @PostMapping("summary")
    @Operation(summary = "对账汇总预览")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:reconcile:summary')")
    public AjaxResult<FinanceReconcileSummaryVO> summary(@RequestBody FinanceReconcileQueryVO query) {
        return AjaxResult.okData(financeReconcileApplicationService.summarize(query));
    }

    @PostMapping("export")
    @Operation(summary = "导出对账明细")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:reconcile:export')")
    public void export(@RequestBody FinanceReconcileQueryVO query, HttpServletResponse response) {
        List<FinanceReconcileExportVO> rows = financeReconcileApplicationService.findExportList(query);
        String day = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        ExcelExportUtil.export(response, FinanceReconcileExportVO.class, rows, "财务对账_" + day);
    }
}
