package com.ruoyi.credit.controller;

import java.util.Map;
import java.util.List;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.credit.service.impl.LoanConsistencyServiceImpl;
import com.ruoyi.credit.mapper.LoanConsistencyMapper;

/**
 * 贷款数据调试控制器
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/credit/debug")
public class LoanDebugController extends BaseController {

    @Autowired
    private LoanConsistencyServiceImpl loanConsistencyService;

    @Autowired
    private LoanConsistencyMapper loanConsistencyMapper;

    /**
     * 调试贷款数据
     */
    @GetMapping("/loan/{loanId}")
    public AjaxResult debugLoan(@PathVariable("loanId") Long loanId) {
        try {
            Map<String, Object> debugInfo = loanConsistencyService.debugLoanData(loanId);
            return success(debugInfo);
        } catch (CannotGetJdbcConnectionException e) {
            logger.error("数据库连接失败: {}", e.getMessage());
            return error("数据库连接失败，请检查数据库服务是否正常运行");
        } catch (DataAccessException e) {
            logger.error("数据访问异常: {}", e.getMessage());
            return error("数据访问异常: " + e.getMessage());
        } catch (Exception e) {
            logger.error("调试失败: {}", e.getMessage(), e);
            return error("调试失败: " + e.getMessage());
        }
    }

    /**
     * 分步调试贷款数据
     */
    @GetMapping("/loan/{loanId}/step-by-step")
    public AjaxResult debugLoanStepByStep(@PathVariable("loanId") Long loanId) {
        try {
            Map<String, Object> stepByStepInfo = loanConsistencyMapper.debugLoanStepByStep(loanId);
            return success(stepByStepInfo);
        } catch (CannotGetJdbcConnectionException e) {
            logger.error("数据库连接失败: {}", e.getMessage());
            return error("数据库连接失败，请检查数据库服务是否正常运行");
        } catch (DataAccessException e) {
            logger.error("数据访问异常: {}", e.getMessage());
            return error("数据访问异常: " + e.getMessage());
        } catch (Exception e) {
            logger.error("分步调试失败: {}", e.getMessage(), e);
            return error("分步调试失败: " + e.getMessage());
        }
    }

    /**
     * 调试基础贷款信息
     */
    @GetMapping("/loan/{loanId}/basic")
    public AjaxResult debugBasicLoanInfo(@PathVariable("loanId") Long loanId) {
        try {
            Map<String, Object> basicInfo = loanConsistencyMapper.debugBasicLoanInfo(loanId);
            return success(basicInfo);
        } catch (Exception e) {
            return error("调试基础贷款信息失败: " + e.getMessage());
        }
    }

    /**
     * 调试服务周期信息
     */
    @GetMapping("/service-period/{servicePeriodId}")
    public AjaxResult debugServicePeriod(@PathVariable("servicePeriodId") Long servicePeriodId) {
        try {
            Map<String, Object> servicePeriodInfo = loanConsistencyMapper.debugServicePeriodInfo(servicePeriodId);
            return success(servicePeriodInfo);
        } catch (Exception e) {
            return error("调试服务周期信息失败: " + e.getMessage());
        }
    }

    /**
     * 调试合同信息
     */
    @GetMapping("/contract/{contractId}")
    public AjaxResult debugContract(@PathVariable("contractId") Long contractId) {
        try {
            Map<String, Object> contractInfo = loanConsistencyMapper.debugContractInfo(contractId);
            return success(contractInfo);
        } catch (Exception e) {
            return error("调试合同信息失败: " + e.getMessage());
        }
    }

    /**
     * 调试债权信息
     */
    @GetMapping("/credit/{contractId}")
    public AjaxResult debugCredit(@PathVariable("contractId") Long contractId) {
        try {
            Map<String, Object> creditInfo = loanConsistencyMapper.debugCreditInfo(contractId);
            return success(creditInfo);
        } catch (Exception e) {
            return error("调试债权信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有需要验证的贷款列表
     */
    @GetMapping("/loans-with-certificates")
    public AjaxResult getLoansWithCertificates() {
        try {
            List<Long> loanIds = loanConsistencyMapper.selectLoansWithCreditCertificates();
            return success(loanIds);
        } catch (Exception e) {
            return error("获取贷款列表失败: " + e.getMessage());
        }
    }

    /**
     * 批量调试多个贷款
     */
    @GetMapping("/batch-debug")
    public AjaxResult batchDebugLoans() {
        try {
            // 获取前5个贷款进行调试
            List<Long> loanIds = Arrays.asList(17L, 18L, 19L, 20L, 21L);
            
            Map<String, Object> batchResult = new java.util.HashMap<>();
            
            for (Long loanId : loanIds) {
                try {
                    Map<String, Object> debugInfo = loanConsistencyService.debugLoanData(loanId);
                    batchResult.put("loan_" + loanId, debugInfo);
                } catch (Exception e) {
                    batchResult.put("loan_" + loanId + "_error", e.getMessage());
                }
            }
            
            return success(batchResult);
        } catch (Exception e) {
            return error("批量调试失败: " + e.getMessage());
        }
    }
}