package com.ruoyi.ccdi.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.project.domain.dto.CcdiFundGraphEdgeDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiFundGraphManualEdgeSaveDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiFundGraphQueryDTO;
import com.ruoyi.ccdi.project.domain.vo.CcdiFundGraphEdgeVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiFundGraphNodeVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiFundGraphStatementVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiFundGraphVO;
import com.ruoyi.ccdi.project.service.CcdiProjectAccessService;
import com.ruoyi.ccdi.project.service.ICcdiFundGraphService;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
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 com.ruoyi.common.utils.SecurityUtils;

import java.util.List;

/**
 * 资金流图谱Controller
 */
@RestController
@RequestMapping("/ccdi/project/fund-graph")
@Tag(name = "资金流图谱")
public class CcdiFundGraphController extends BaseController {

    @Resource
    private ICcdiFundGraphService fundGraphService;

    @Resource
    private CcdiProjectAccessService projectAccessService;

    @GetMapping("/search")
    @Operation(summary = "查询资金流图谱主体")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult searchSubjects(CcdiFundGraphQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiFundGraphNodeVO> subjects = fundGraphService.searchSubjects(queryDTO);
        return AjaxResult.success(subjects);
    }

    @GetMapping("/graph")
    @Operation(summary = "查询一层资金流图谱")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getGraph(CcdiFundGraphQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiFundGraphVO graph = fundGraphService.getFundGraph(queryDTO);
        return AjaxResult.success(graph);
    }

    @GetMapping("/edge-detail")
    @Operation(summary = "查询资金边流水明细")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public TableDataInfo getEdgeDetail(CcdiFundGraphEdgeDetailQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiFundGraphStatementVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiFundGraphStatementVO> result = fundGraphService.getEdgeDetails(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    @PostMapping("/manual-edge")
    @Operation(summary = "新增手工资金流向")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult saveManualEdge(@RequestBody CcdiFundGraphManualEdgeSaveDTO saveDTO) {
        try {
            projectAccessService.assertCanOperate(saveDTO == null ? null : saveDTO.getProjectId());
            CcdiFundGraphEdgeVO edge = fundGraphService.saveManualEdge(saveDTO, SecurityUtils.getUsername());
            return AjaxResult.success(edge);
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
