package com.ruoyi.ccdi.project.controller;

import com.ruoyi.ccdi.project.domain.dto.CcdiProjectAbnormalAccountQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectEmployeeCreditNegativeQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExternalPersonQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectExternalRiskModelPeopleQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectPersonAnalysisDetailQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectRiskModelPeopleQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectRiskPeopleQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectRiskExclusionSaveDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectSuspiciousTransactionQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectUnsubmittedAccountQueryDTO;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectExternalPersonWarningExcel;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectRiskModelPeopleExcel;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectRiskPeopleOverviewExcel;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectSuspiciousTransactionExcel;
import com.ruoyi.ccdi.project.domain.excel.CcdiProjectUnsubmittedAccountFlowExcel;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectAbnormalAccountPageVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectEmployeeCreditNegativePageVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExternalRiskSummaryVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectExternalPersonWarningVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectPersonAnalysisDetailVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectOverviewDashboardVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectRiskModelCardsVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectRiskModelPeopleVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectRiskPeopleOverviewVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectSuspiciousTransactionPageVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectTopRiskPeopleVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectUnsubmittedAccountFlowVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectUnsubmittedAccountVO;
import com.ruoyi.ccdi.project.service.CcdiProjectAccessService;
import com.ruoyi.ccdi.project.service.ICcdiProjectOverviewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 结果总览控制器
 */
@RestController
@RequestMapping("/ccdi/project/overview")
@Tag(name = "项目结果总览")
public class CcdiProjectOverviewController extends BaseController {

    @Resource
    private ICcdiProjectOverviewService overviewService;

    @Resource
    private CcdiProjectAccessService projectAccessService;

    /**
     * 查询风险仪表盘
     */
    @GetMapping("/dashboard")
    @Operation(summary = "查询风险仪表盘")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getDashboard(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiProjectOverviewDashboardVO dashboard = overviewService.getDashboard(projectId);
        return AjaxResult.success(dashboard);
    }

    /**
     * 排除单条可疑预警
     */
    @PostMapping("/risk-exclusions")
    @Operation(summary = "排除单条可疑预警")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult excludeRisk(@Validated @RequestBody CcdiProjectRiskExclusionSaveDTO dto) {
        projectAccessService.assertCanOperate(dto.getProjectId());
        overviewService.excludeRisk(dto);
        return AjaxResult.success("排除成功");
    }

    /**
     * 查询风险人员总览
     */
    @GetMapping("/risk-people")
    @Operation(summary = "查询风险人员总览")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getRiskPeople(CcdiProjectRiskPeopleQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectRiskPeopleOverviewVO overview = overviewService.getRiskPeopleOverview(queryDTO);
        return AjaxResult.success(overview);
    }

    /**
     * 查询外部人员预警
     */
    @GetMapping("/external-persons")
    @Operation(summary = "查询外部人员预警")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getExternalPersons(CcdiProjectExternalPersonQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectExternalPersonWarningVO warnings = overviewService.getExternalPersonWarnings(queryDTO);
        return AjaxResult.success(warnings);
    }

    /**
     * 查询外部人员风险汇总
     */
    @GetMapping("/external-persons/summary")
    @Operation(summary = "查询外部人员风险汇总")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getExternalRiskSummary(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiProjectExternalRiskSummaryVO summary = overviewService.getExternalRiskSummary(projectId);
        return AjaxResult.success(summary);
    }

    /**
     * 查询中高风险人员TOP10
     */
    @GetMapping("/top-risk-people")
    @Operation(summary = "查询中高风险人员TOP10")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getTopRiskPeople(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiProjectTopRiskPeopleVO topRiskPeople = overviewService.getTopRiskPeople(projectId);
        return AjaxResult.success(topRiskPeople);
    }

    /**
     * 查询风险模型卡片
     */
    @GetMapping("/risk-models/cards")
    @Operation(summary = "查询风险模型卡片")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getRiskModelCards(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiProjectRiskModelCardsVO cards = overviewService.getRiskModelCards(projectId);
        return AjaxResult.success(cards);
    }

    /**
     * 查询风险模型命中人员
     */
    @GetMapping("/risk-models/people")
    @Operation(summary = "查询风险模型命中人员")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getRiskModelPeople(CcdiProjectRiskModelPeopleQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectRiskModelPeopleVO people = overviewService.getRiskModelPeople(queryDTO);
        return AjaxResult.success(people);
    }

    /**
     * 查询外部人员风险模型卡片
     */
    @GetMapping("/external-risk-models/cards")
    @Operation(summary = "查询外部人员风险模型卡片")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getExternalRiskModelCards(Long projectId) {
        projectAccessService.assertCanRead(projectId);
        CcdiProjectRiskModelCardsVO cards = overviewService.getExternalRiskModelCards(projectId);
        return AjaxResult.success(cards);
    }

    /**
     * 查询外部人员风险模型命中人员
     */
    @GetMapping("/external-risk-models/people")
    @Operation(summary = "查询外部人员风险模型命中人员")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getExternalRiskModelPeople(CcdiProjectExternalRiskModelPeopleQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectRiskModelPeopleVO people = overviewService.getExternalRiskModelPeople(queryDTO);
        return AjaxResult.success(people);
    }

    /**
     * 查询项目分析详情
     */
    @GetMapping("/person-analysis/detail")
    @Operation(summary = "查询项目分析详情")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getPersonAnalysisDetail(CcdiProjectPersonAnalysisDetailQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectPersonAnalysisDetailVO detail = overviewService.getPersonAnalysisDetail(queryDTO);
        return AjaxResult.success(detail);
    }

    /**
     * 查询涉疑交易明细
     */
    @GetMapping("/suspicious-transactions")
    @Operation(summary = "查询涉疑交易明细")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getSuspiciousTransactions(CcdiProjectSuspiciousTransactionQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectSuspiciousTransactionPageVO pageVO = overviewService.getSuspiciousTransactions(queryDTO);
        return AjaxResult.success(pageVO);
    }

    /**
     * 查询项目员工负面征信
     */
    @GetMapping("/employee-credit-negative")
    @Operation(summary = "查询项目员工负面征信")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getEmployeeCreditNegative(CcdiProjectEmployeeCreditNegativeQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectEmployeeCreditNegativePageVO pageVO = overviewService.getEmployeeCreditNegative(queryDTO);
        return AjaxResult.success(pageVO);
    }

    /**
     * 查询异常账户人员信息
     */
    @GetMapping("/abnormal-account-people")
    @Operation(summary = "查询异常账户人员信息")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getAbnormalAccountPeople(CcdiProjectAbnormalAccountQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectAbnormalAccountPageVO pageVO = overviewService.getAbnormalAccountPeople(queryDTO);
        return AjaxResult.success(pageVO);
    }

    /**
     * 查询未提交账户
     */
    @GetMapping("/unsubmitted-accounts")
    @Operation(summary = "查询未提交账户")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getUnsubmittedAccounts(CcdiProjectUnsubmittedAccountQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        CcdiProjectUnsubmittedAccountVO result = overviewService.getUnsubmittedAccounts(queryDTO);
        return AjaxResult.success(result);
    }

    /**
     * 查询未提交账户流水
     */
    @GetMapping("/unsubmitted-accounts/flows")
    @Operation(summary = "查询未提交账户流水")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult getUnsubmittedAccountFlows(CcdiProjectUnsubmittedAccountQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiProjectUnsubmittedAccountFlowVO> rows = overviewService.getUnsubmittedAccountFlows(queryDTO);
        return AjaxResult.success(rows);
    }

    /**
     * 导出涉疑交易明细
     */
    @PostMapping("/suspicious-transactions/export")
    @Operation(summary = "导出涉疑交易明细")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportSuspiciousTransactions(
        HttpServletResponse response,
        CcdiProjectSuspiciousTransactionQueryDTO queryDTO
    ) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiProjectSuspiciousTransactionExcel> rows = overviewService.exportSuspiciousTransactions(queryDTO);
        ExcelUtil<CcdiProjectSuspiciousTransactionExcel> util =
            new ExcelUtil<>(CcdiProjectSuspiciousTransactionExcel.class);
        util.exportExcel(response, rows, "涉疑交易明细");
    }

    /**
     * 导出风险人员总览
     */
    @PostMapping("/risk-people/export")
    @Operation(summary = "导出风险人员总览")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportRiskPeople(HttpServletResponse response, Long projectId) {
        projectAccessService.assertCanRead(projectId);
        List<CcdiProjectRiskPeopleOverviewExcel> rows = overviewService.exportRiskPeopleOverview(projectId);
        ExcelUtil<CcdiProjectRiskPeopleOverviewExcel> util =
            new ExcelUtil<>(CcdiProjectRiskPeopleOverviewExcel.class);
        util.exportExcel(response, rows, "风险人员总览");
    }

    /**
     * 导出外部人员预警
     */
    @PostMapping("/external-persons/export")
    @Operation(summary = "导出外部人员预警")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportExternalPersons(HttpServletResponse response, Long projectId) {
        projectAccessService.assertCanRead(projectId);
        List<CcdiProjectExternalPersonWarningExcel> rows = overviewService.exportExternalPersonWarnings(projectId);
        ExcelUtil<CcdiProjectExternalPersonWarningExcel> util =
            new ExcelUtil<>(CcdiProjectExternalPersonWarningExcel.class);
        util.exportExcel(response, rows, "外部人员预警");
    }

    /**
     * 导出风险模型命中人员
     */
    @PostMapping("/risk-models/people/export")
    @Operation(summary = "导出风险模型命中人员")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportRiskModelPeople(HttpServletResponse response, CcdiProjectRiskModelPeopleQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiProjectRiskModelPeopleExcel> rows = overviewService.exportRiskModelPeople(queryDTO);
        ExcelUtil<CcdiProjectRiskModelPeopleExcel> util =
            new ExcelUtil<>(CcdiProjectRiskModelPeopleExcel.class);
        util.exportExcel(response, rows, "风险模型命中人员");
    }

    /**
     * 导出外部人员风险模型命中人员
     */
    @PostMapping("/external-risk-models/people/export")
    @Operation(summary = "导出外部人员风险模型命中人员")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportExternalRiskModelPeople(
        HttpServletResponse response,
        CcdiProjectExternalRiskModelPeopleQueryDTO queryDTO
    ) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiProjectRiskModelPeopleExcel> rows = overviewService.exportExternalRiskModelPeople(queryDTO);
        ExcelUtil<CcdiProjectRiskModelPeopleExcel> util =
            new ExcelUtil<>(CcdiProjectRiskModelPeopleExcel.class);
        util.exportExcel(response, rows, "外部人员风险模型命中人员");
    }

    /**
     * 导出未提交账户流水
     */
    @PostMapping("/unsubmitted-accounts/export")
    @Operation(summary = "导出未提交账户流水")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportUnsubmittedAccountFlows(
        HttpServletResponse response,
        CcdiProjectUnsubmittedAccountQueryDTO queryDTO
    ) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiProjectUnsubmittedAccountFlowExcel> rows = overviewService.exportUnsubmittedAccountFlows(queryDTO);
        ExcelUtil<CcdiProjectUnsubmittedAccountFlowExcel> util =
            new ExcelUtil<>(CcdiProjectUnsubmittedAccountFlowExcel.class);
        util.exportExcel(response, rows, "未提交账户流水");
    }

    /**
     * 导出风险明细
     */
    @PostMapping("/risk-details/export")
    @Operation(summary = "导出风险明细")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportRiskDetails(
        HttpServletResponse response,
        CcdiProjectSuspiciousTransactionQueryDTO queryDTO
    ) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        overviewService.exportRiskDetails(response, queryDTO);
    }

    /**
     * 导出结果总览报告
     */
    @RequestMapping(value = "/report/export", method = { RequestMethod.GET, RequestMethod.POST })
    @Operation(summary = "导出结果总览报告")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public void exportOverviewReport(HttpServletResponse response, Long projectId) {
        projectAccessService.assertCanRead(projectId);
        overviewService.exportOverviewReport(response, projectId);
    }
}
