package com.ruoyi.credit.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.credit.domain.ServicePeriodLoan;
import com.ruoyi.credit.service.IDepartmentDataConsistencyService;

/**
 * 部门数据一致性验证控制器
 * 
 * 提供部门数据一致性验证的REST API接口，支持验证服务周期与合同、
 * 员工与服务周期、债权与合同等关联数据的部门一致性。
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@RestController
@RequestMapping("/credit/department-consistency")
public class DepartmentDataConsistencyController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(DepartmentDataConsistencyController.class);

    @Autowired
    private IDepartmentDataConsistencyService departmentDataConsistencyService;

    /**
     * 验证服务周期与合同的部门一致性
     */
    @GetMapping("/validate/service-period-contract")
    public AjaxResult validateServicePeriodContract(@RequestParam Long contractId, @RequestParam Long servicePeriodId)
    {
        log.info("验证服务周期与合同的部门一致性: contractId={}, servicePeriodId={}", contractId, servicePeriodId);
        
        try {
            boolean isValid = departmentDataConsistencyService.validateServicePeriodContractConsistency(contractId, servicePeriodId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("contractId", contractId);
            result.put("servicePeriodId", servicePeriodId);
            result.put("message", isValid ? "部门一致性验证通过" : "部门一致性验证失败");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("部门一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证服务周期与合同部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 验证员工与服务周期的部门一致性
     */
    @GetMapping("/validate/employee-service-period")
    public AjaxResult validateEmployeeServicePeriod(@RequestParam Long employeeId, @RequestParam Long servicePeriodId)
    {
        log.info("验证员工与服务周期的部门一致性: employeeId={}, servicePeriodId={}", employeeId, servicePeriodId);
        
        try {
            boolean isValid = departmentDataConsistencyService.validateEmployeeServicePeriodConsistency(employeeId, servicePeriodId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("employeeId", employeeId);
            result.put("servicePeriodId", servicePeriodId);
            result.put("message", isValid ? "部门一致性验证通过" : "部门一致性验证失败");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("部门一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证员工与服务周期部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 验证债权与合同的部门一致性
     */
    @GetMapping("/validate/credit-contract")
    public AjaxResult validateCreditContract(@RequestParam Long contractId, @RequestParam Long creditId)
    {
        log.info("验证债权与合同的部门一致性: contractId={}, creditId={}", contractId, creditId);
        
        try {
            boolean isValid = departmentDataConsistencyService.validateCreditContractConsistency(contractId, creditId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("contractId", contractId);
            result.put("creditId", creditId);
            result.put("message", isValid ? "部门一致性验证通过" : "部门一致性验证失败");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("部门一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证债权与合同部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 验证服务周期下所有员工的部门一致性
     */
    @GetMapping("/validate/service-period-employees/{servicePeriodId}")
    public AjaxResult validateServicePeriodEmployees(@PathVariable Long servicePeriodId)
    {
        log.info("验证服务周期下所有员工的部门一致性: servicePeriodId={}", servicePeriodId);
        
        try {
            boolean isValid = departmentDataConsistencyService.validateAllEmployeesInServicePeriod(servicePeriodId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("servicePeriodId", servicePeriodId);
            result.put("message", isValid ? "所有员工部门一致性验证通过" : "存在员工部门一致性问题");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("员工部门一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证服务周期员工部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 验证服务周期贷款的完整部门一致性
     */
    @PostMapping("/validate/service-period-loan")
    public AjaxResult validateServicePeriodLoan(@RequestBody ServicePeriodLoan servicePeriodLoan)
    {
        log.info("验证服务周期贷款的完整部门一致性: loanId={}, servicePeriodId={}", 
                servicePeriodLoan.getLinkId(), servicePeriodLoan.getServicePeriodId());
        
        try {
            boolean isValid = departmentDataConsistencyService.validateServicePeriodLoanConsistency(servicePeriodLoan);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("loanId", servicePeriodLoan.getLinkId());
            result.put("servicePeriodId", servicePeriodLoan.getServicePeriodId());
            result.put("message", isValid ? "贷款相关数据部门一致性验证通过" : "贷款相关数据存在部门一致性问题");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("贷款部门一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证服务周期贷款部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 验证合同相关的所有数据部门一致性
     */
    @GetMapping("/validate/contract-related/{contractId}")
    public AjaxResult validateContractRelatedData(@PathVariable Long contractId)
    {
        log.info("验证合同相关的所有数据部门一致性: contractId={}", contractId);
        
        try {
            boolean isValid = departmentDataConsistencyService.validateContractRelatedDataConsistency(contractId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("contractId", contractId);
            result.put("message", isValid ? "合同相关数据部门一致性验证通过" : "合同相关数据存在部门一致性问题");
            
            return isValid ? AjaxResult.success(result) : AjaxResult.error("合同数据一致性验证失败", result);
            
        } catch (Exception e) {
            log.error("验证合同相关数据部门一致性异常", e);
            return AjaxResult.error("验证异常: " + e.getMessage());
        }
    }

    /**
     * 修复数据部门不一致问题
     */
    @PostMapping("/fix-inconsistency")
    public AjaxResult fixDepartmentInconsistency(@RequestBody Map<String, Object> request)
    {
        String tableName = (String) request.get("tableName");
        Long recordId = Long.valueOf(request.get("recordId").toString());
        Long targetDeptId = Long.valueOf(request.get("targetDeptId").toString());
        
        log.info("修复数据部门不一致问题: tableName={}, recordId={}, targetDeptId={}", 
                tableName, recordId, targetDeptId);
        
        try {
            boolean success = departmentDataConsistencyService.fixDepartmentInconsistency(tableName, recordId, targetDeptId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", success);
            result.put("tableName", tableName);
            result.put("recordId", recordId);
            result.put("targetDeptId", targetDeptId);
            result.put("message", success ? "部门不一致问题修复成功" : "部门不一致问题修复失败");
            
            return success ? AjaxResult.success(result) : AjaxResult.error("修复失败", result);
            
        } catch (Exception e) {
            log.error("修复数据部门不一致问题异常", e);
            return AjaxResult.error("修复异常: " + e.getMessage());
        }
    }
}