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

import com.jeesharp.business.modules.finance.merchantsettlement.application.MerchantSettlementBillApplicationService;
import com.jeesharp.business.modules.finance.merchantsettlement.dto.MerchantSettlementBillGenerateDTO;
import com.jeesharp.business.modules.finance.merchantsettlement.dto.MerchantSettlementPayoutCreateDTO;
import com.jeesharp.business.modules.finance.merchantsettlement.entity.MerchantSettlementBill;
import com.jeesharp.business.modules.finance.merchantsettlement.entity.MerchantSettlementBillLine;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillInfoVO;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillLineListVO;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillLineQueryVO;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillListVO;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillQueryVO;
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.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商户结算单管理 REST 接口，提供结算单查询、生成、打款登记与商户确认。
 *
 * @author jeesharp
 * @since 2026-06-29
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/finance/merchantSettlementBill/v1")
@Tag(name = "商户结算单")
@AllArgsConstructor
public class MerchantSettlementBillController extends BaseController {

    private final MerchantSettlementBillApplicationService merchantSettlementBillApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取结算单详情")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:info')")
    public AjaxResult<MerchantSettlementBillInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(merchantSettlementBillApplicationService.get(id));
    }

    @PostMapping("list")
    @Operation(summary = "结算单分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:list')")
    public AjaxResult<PageRespVO<MerchantSettlementBillListVO>> list(
            @RequestBody MerchantSettlementBillQueryVO query) {
        return AjaxResult.okData(
                merchantSettlementBillApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("line/list")
    @Operation(summary = "结算单构成明细分页")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:lineList') or @ss.hasPermission('manager:business:finance:merchantSettlementBill:info')")
    public AjaxResult<PageRespVO<MerchantSettlementBillLineListVO>> lineList(
            @RequestBody MerchantSettlementBillLineQueryVO query) {
        return AjaxResult.okData(
                merchantSettlementBillApplicationService.findLinePage(new Page<>(query), query));
    }

    @PostMapping("generate")
    @Operation(summary = "按账期生成结算单")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:generate')")
    public AjaxResult<String> generate(@RequestBody @Valid MerchantSettlementBillGenerateDTO dto) {
        return AjaxResult.okData(merchantSettlementBillApplicationService.generate(dto));
    }

    @PostMapping("recordPayout")
    @Operation(summary = "登记结算打款")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:recordPayout')")
    public AjaxResult<Void> recordPayout(@RequestBody @Valid MerchantSettlementPayoutCreateDTO dto) {
        merchantSettlementBillApplicationService.recordPayout(dto);
        return AjaxResult.ok("打款登记成功");
    }

    @PostMapping("merchantConfirm")
    @Operation(summary = "商户确认收款")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:merchantConfirm')")
    public AjaxResult<Void> merchantConfirm(@RequestParam("id") String id) {
        merchantSettlementBillApplicationService.merchantConfirmReceipt(id);
        return AjaxResult.ok("确认收款成功");
    }

    @GetMapping("cancel")
    @Operation(summary = "作废结算单")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:merchantSettlementBill:cancel')")
    public AjaxResult<Void> cancel(@RequestParam("id") String id) {
        merchantSettlementBillApplicationService.cancel(id);
        return AjaxResult.ok("作废成功");
    }
}
