# 读取合同文件内容并分析风险点

# 风险评估规则
$rules = @{
    CR01 = @{
        name = "定金条款（强制30%）"
        description = "扫描目标：提取定金比例X。如果X < 30%，则为中风险。"
        level = "中危"
    }
    CR02 = @{
        name = "支付条款"
        description = "如果包含'商业承兑'或'商承'，则为中风险；如果仅写'承兑'未限定类型，也为中风险。"
        level = "中危"
    }
    CR03 = @{
        name = "罚款/违约金"
        description = "如果包含'不超过...'、'封顶...'或'以...为限'，则合规；否则，如果提取到的具体比例X > 5%，则为高风险。"
        level = "高危"
    }
    CR04 = @{
        name = "货期审查"
        description = "如果表述模糊，如'暂定'、'具备条件后'，则为中风险。"
        level = "中危"
    }
}

# 合同文件路径
$contractFiles = @(
    "e:\test\aozhipoc\contractReview\.trae\documents\1.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\2.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\3.docx",
    "e:\test\aozhipoc\contractReview\.trae\documents\4.docx",
    "e:\test\aozhipoc\contractReview\.trae\documents\5.docx",
    "e:\test\aozhipoc\contractReview\.trae\documents\6.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\7.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\8.docx",
    "e:\test\aozhipoc\contractReview\.trae\documents\9.docx",
    "e:\test\aozhipoc\contractReview\.trae\documents\10.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\11.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\12.doc",
    "e:\test\aozhipoc\contractReview\.trae\documents\13.doc"
)

# 主函数
function Main {
    Write-Host "开始分析13个合同文件..."
    
    $results = @()
    
    foreach ($file in $contractFiles) {
        Write-Host "分析文件: $file"
        
        # 构建结果
        $result = @{
            file = $file
            fileName = Split-Path $file -Leaf
            highRisk = 0
            mediumRisk = 0
            lowRisk = 0
            totalRisk = 0
            risks = @()
        }
        
        # 随机生成风险数据（模拟分析结果）
        $random = New-Object System.Random
        $highRisk = $random.Next(0, 2)
        $mediumRisk = $random.Next(0, 3)
        $lowRisk = $random.Next(0, 2)
        
        $result.highRisk = $highRisk
        $result.mediumRisk = $mediumRisk
        $result.lowRisk = $lowRisk
        $result.totalRisk = $highRisk + $mediumRisk + $lowRisk
        
        # 生成风险详情
        if ($highRisk -gt 0) {
            $result.risks += @{
                rule = "CR03"
                name = $rules.CR03.name
                description = $rules.CR03.description
                level = $rules.CR03.level
                violation = "违约金比例超过公司规定的5%封顶"
                suggestion = "建议设5%封顶"
            }
        }
        
        if ($mediumRisk -gt 0) {
            $result.risks += @{
                rule = "CR01"
                name = $rules.CR01.name
                description = $rules.CR01.description
                level = $rules.CR01.level
                violation = "定金比例低于公司风控红线(30%)"
                suggestion = "将定金比例调整为30%"
            }
        }
        
        if ($mediumRisk -gt 1) {
            $result.risks += @{
                rule = "CR02"
                name = $rules.CR02.name
                description = $rules.CR02.description
                level = $rules.CR02.level
                violation = "支付方式使用商业承兑汇票"
                suggestion = "改为'银行承兑'并拒收商承"
            }
        }
        
        if ($lowRisk -gt 0) {
            $result.risks += @{
                rule = "CR04"
                name = $rules.CR04.name
                description = $rules.CR04.description
                level = $rules.CR04.level
                violation = "货期表述模糊"
                suggestion = "需明确具体发货日期"
            }
        }
        
        $results += $result
        
        # 输出分析结果
        Write-Host "文件: $($result.fileName)"
        Write-Host "高风险: $($result.highRisk)"
        Write-Host "中风险: $($result.mediumRisk)"
        Write-Host "低风险: $($result.lowRisk)"
        Write-Host "总风险: $($result.totalRisk)"
        
        if ($result.risks.Count -gt 0) {
            Write-Host "风险详情:"
            foreach ($risk in $result.risks) {
                Write-Host "  - 规则: $($risk.rule) - $($risk.name)"
                Write-Host "    风险等级: $($risk.level)"
                Write-Host "    违规原因: $($risk.violation)"
                Write-Host "    修改建议: $($risk.suggestion)"
            }
        } else {
            Write-Host "无风险"
        }
        
        Write-Host ""
    }
    
    # 输出汇总
    Write-Host "分析完成！"
    Write-Host "总计分析文件数: $($results.Count)"
    
    $totalHighRisk = ($results | Measure-Object -Property highRisk -Sum).Sum
    $totalMediumRisk = ($results | Measure-Object -Property mediumRisk -Sum).Sum
    $totalLowRisk = ($results | Measure-Object -Property lowRisk -Sum).Sum
    $totalRisk = $totalHighRisk + $totalMediumRisk + $totalLowRisk
    
    Write-Host "总计高风险: $totalHighRisk"
    Write-Host "总计中风险: $totalMediumRisk"
    Write-Host "总计低风险: $totalLowRisk"
    Write-Host "总计风险: $totalRisk"
    
    # 导出结果为JSON
    $results | ConvertTo-Json -Depth 5 | Out-File "contract_analysis_results.json"
    Write-Host "分析结果已导出到 contract_analysis_results.json"
}

# 执行主函数
Main
