package com.ruoyi.credit.domain;

import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 数据一致性检查结果模型
 * 
 * @author ruoyi
 * @date 2025-12-25
 */
public class ConsistencyCheckResult
{
    /** 是否一致 */
    private boolean isConsistent;
    
    /** 总银行数量 */
    private int totalBanks;
    
    /** 不一致银行数量 */
    private int inconsistentBanks;
    
    /** 差异详情列表 */
    private List<DataDifference> differences;
    
    /** 检查时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date checkTime;
    
    /** 检查耗时（毫秒） */
    private long checkDurationMs;
    
    /** 检查状态 */
    private String checkStatus;
    
    /** 错误信息 */
    private String errorMessage;

    public ConsistencyCheckResult() {
        this.checkTime = new Date();
        this.checkStatus = "SUCCESS";
    }

    public ConsistencyCheckResult(boolean isConsistent, int totalBanks, int inconsistentBanks, 
                                List<DataDifference> differences) {
        this();
        this.isConsistent = isConsistent;
        this.totalBanks = totalBanks;
        this.inconsistentBanks = inconsistentBanks;
        this.differences = differences;
    }

    /**
     * 创建检查失败的结果
     */
    public static ConsistencyCheckResult createFailedResult(String errorMessage) {
        ConsistencyCheckResult result = new ConsistencyCheckResult();
        result.setConsistent(false);
        result.setCheckStatus("FAILED");
        result.setErrorMessage(errorMessage);
        return result;
    }

    /**
     * 创建成功的一致性结果
     */
    public static ConsistencyCheckResult createConsistentResult(int totalBanks) {
        ConsistencyCheckResult result = new ConsistencyCheckResult();
        result.setConsistent(true);
        result.setTotalBanks(totalBanks);
        result.setInconsistentBanks(0);
        return result;
    }

    /**
     * 获取一致性百分比
     */
    public double getConsistencyPercentage() {
        if (totalBanks == 0) {
            return 100.0;
        }
        return ((double) (totalBanks - inconsistentBanks) / totalBanks) * 100.0;
    }

    /**
     * 是否有差异
     */
    public boolean hasDifferences() {
        return differences != null && !differences.isEmpty();
    }

    // Getters and Setters
    public boolean isConsistent() {
        return isConsistent;
    }

    public void setConsistent(boolean consistent) {
        isConsistent = consistent;
    }

    public int getTotalBanks() {
        return totalBanks;
    }

    public void setTotalBanks(int totalBanks) {
        this.totalBanks = totalBanks;
    }

    public int getInconsistentBanks() {
        return inconsistentBanks;
    }

    public void setInconsistentBanks(int inconsistentBanks) {
        this.inconsistentBanks = inconsistentBanks;
    }

    public List<DataDifference> getDifferences() {
        return differences;
    }

    public void setDifferences(List<DataDifference> differences) {
        this.differences = differences;
    }

    public Date getCheckTime() {
        return checkTime;
    }

    public void setCheckTime(Date checkTime) {
        this.checkTime = checkTime;
    }

    public long getCheckDurationMs() {
        return checkDurationMs;
    }

    public void setCheckDurationMs(long checkDurationMs) {
        this.checkDurationMs = checkDurationMs;
    }

    public String getCheckStatus() {
        return checkStatus;
    }

    public void setCheckStatus(String checkStatus) {
        this.checkStatus = checkStatus;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("isConsistent", isConsistent())
            .append("totalBanks", getTotalBanks())
            .append("inconsistentBanks", getInconsistentBanks())
            .append("differences", getDifferences())
            .append("checkTime", getCheckTime())
            .append("checkDurationMs", getCheckDurationMs())
            .append("checkStatus", getCheckStatus())
            .append("errorMessage", getErrorMessage())
            .toString();
    }
}