package com.ruoyi.ccdi.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.project.domain.dto.CcdiBankStatementQueryDTO;
import com.ruoyi.ccdi.project.domain.excel.CcdiBankStatementExcel;
import com.ruoyi.ccdi.project.domain.vo.CcdiBankStatementCounterpartyVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiBankStatementDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiBankStatementFilterOptionsVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiBankStatementListVO;
import com.ruoyi.ccdi.project.service.CcdiProjectAccessService;
import com.ruoyi.ccdi.project.service.ICcdiBankStatementService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流水明细查询Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/ccdi/project/bank-statement")
@Tag(name = "流水明细查询")
public class CcdiBankStatementController extends BaseController {

    @Resource
    private ICcdiBankStatementService bankStatementService;

    @Resource
    private CcdiProjectAccessService projectAccessService;

    /**
     * 分页查询流水明细
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询流水明细")
    public TableDataInfo list(CcdiBankStatementQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiBankStatementListVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiBankStatementListVO> result = bankStatementService.selectStatementPage(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    /**
     * 分页查询对手方聚合列表
     */
    @GetMapping("/counterparty/list")
    @Operation(summary = "分页查询对手方聚合列表")
    public TableDataInfo counterpartyList(CcdiBankStatementQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiBankStatementCounterpartyVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiBankStatementCounterpartyVO> result = bankStatementService.selectCounterpartyPage(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    /**
     * 查询项目级筛选项
     */
    @GetMapping("/options")
    @Operation(summary = "查询项目级筛选项")
    public AjaxResult getOptions(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiBankStatementFilterOptionsVO options = bankStatementService.getFilterOptions(projectId);
        return AjaxResult.success(options);
    }

    /**
     * 查询流水详情
     */
    @GetMapping("/detail/{bankStatementId}")
    @Operation(summary = "查询流水详情")
    public AjaxResult getDetail(@PathVariable Long bankStatementId, String modelCode, String suspiciousType) {
        projectAccessService.assertCanReadByBankStatementId(bankStatementId);
        CcdiBankStatementDetailVO detail = bankStatementService.getStatementDetail(
            bankStatementId,
            modelCode,
            suspiciousType
        );
        return AjaxResult.success(detail);
    }

    /**
     * 导出流水明细
     */
    @PostMapping("/export")
    @Operation(summary = "导出流水明细")
    public void export(HttpServletResponse response, CcdiBankStatementQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiBankStatementExcel> list = bankStatementService.selectStatementListForExport(queryDTO);
        ExcelUtil<CcdiBankStatementExcel> util = new ExcelUtil<>(CcdiBankStatementExcel.class);
        util.exportExcel(response, list, "流水明细");
    }
}
