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

import com.jeesharp.business.modules.finance.invoice.application.MemberInvoiceApplyApplicationService;
import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceApplySaveDTO;
import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceAuditDTO;
import com.jeesharp.business.modules.finance.invoice.entity.MemberInvoiceApply;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyInfoVO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyListVO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.utils.WebFrameworkUtils;
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-05-08
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/member/invoiceApply/v1")
@Tag(name = "发票申请管理")
@AllArgsConstructor
public class MemberInvoiceApplyController extends BaseController {

    private final MemberInvoiceApplyApplicationService memberInvoiceApplyApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('member:invoiceApply:info')")
    public AjaxResult<MemberInvoiceApplyInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(memberInvoiceApplyApplicationService.get(id));
    }

    @PostMapping("list")
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('member:invoiceApply:list')")
    public AjaxResult<PageRespVO<MemberInvoiceApplyListVO>> list(@RequestBody MemberInvoiceApplyQueryVO query) {
        return AjaxResult.okData(memberInvoiceApplyApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('member:invoiceApply:save')")
    public AjaxResult<Void> save(@RequestBody @Valid MemberInvoiceApplySaveDTO dto) {
        memberInvoiceApplyApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @GetMapping("delete")
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('member:invoiceApply:delete')")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        memberInvoiceApplyApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @PostMapping("audit")
    @Operation(summary = "审核")
    @PreAuthorize("@ss.hasPermission('member:invoiceApply:audit')")
    public AjaxResult<Void> audit(@RequestBody @Valid MemberInvoiceAuditDTO dto) {
        memberInvoiceApplyApplicationService.audit(dto, WebFrameworkUtils.getLoginUserId());
        return AjaxResult.ok("审核成功");
    }
}
