#!/bin/bash

echo "=== 最终时间类型兼容性验证 ==="
echo

echo "1. 检查所有可能的类型不匹配问题..."

echo "  ❌ 查找 Date 变量接收 LocalDateTime 的问题："
find src/main/java -name "*.java" -exec grep -Hn "Date.*=.*get.*Time()" {} \; | grep -v "new Date" | grep -v "//"

echo "  ❌ 查找 setXxxTime(DateUtil.xxx) 问题："
find src/main/java -name "*.java" -exec grep -Hn "\.set.*Time(DateUtil\." {} \; | grep -v "//"

echo "  ❌ 查找 LocalDateTime.compareTo(DateUtil.date()) 问题："
find src/main/java -name "*.java" -exec grep -Hn "\.compareTo(DateUtil\.date())" {} \; | grep -v "//"

echo "  ❌ 查找 DateUtil.offsetXxx(...).compareTo(DateUtil.date()) 问题："
find src/main/java -name "*.java" -exec grep -Hn "DateUtil\.offset.*\.compareTo(DateUtil\.date())" {} \; | grep -v "//"

echo
echo "2. 验证已修复的关键文件..."

files=(
    "src/main/java/com/gxwebsoft/oa/service/impl/OaAssetsSslServiceImpl.java:LocalDateTime now"
    "src/main/java/com/gxwebsoft/shop/service/impl/ShopOrderServiceImpl.java:setPayTime(LocalDateTime.now())"
    "src/main/java/com/gxwebsoft/project/service/impl/ProjectServiceImpl.java:final LocalDateTime expirationTime"
    "src/main/java/com/gxwebsoft/project/controller/ProjectRenewController.java:final LocalDateTime expirationTime"
    "src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImpl.java:setExpirationTime(LocalDateTime.now().plusMonths(1))"
    "src/main/java/com/gxwebsoft/shop/controller/ShopOrderController.java:setPayTime(LocalDateTime.now())"
    "src/main/java/com/gxwebsoft/common/system/controller/CompanyController.java:LocalDateTime now"
    "src/main/java/com/gxwebsoft/cms/controller/CmsWebsiteController.java:LocalDateTime now"
    "src/main/java/com/gxwebsoft/bszx/controller/BszxPayController.java:setExpirationTime(LocalDateTime.now().plusYears(10))"
)

for item in "${files[@]}"; do
    file=$(echo "$item" | cut -d':' -f1)
    pattern=$(echo "$item" | cut -d':' -f2)
    
    echo "  检查 $file"
    if grep -q "$pattern" "$file"; then
        echo "    ✅ 已正确修复"
    else
        echo "    ❌ 需要检查: $pattern"
    fi
done

echo
echo "3. 统计修复情况..."

total_java_files=$(find src/main/java -name "*.java" | wc -l)
localdatetime_files=$(find src/main/java -name "*.java" -exec grep -l "LocalDateTime" {} \; | wc -l)
entity_files=$(find src/main/java -path "*/entity/*" -name "*.java" | wc -l)
entity_localdatetime_files=$(find src/main/java -path "*/entity/*" -name "*.java" -exec grep -l "LocalDateTime" {} \; | wc -l)

echo "  总Java文件数: $total_java_files"
echo "  使用LocalDateTime的文件数: $localdatetime_files"
echo "  实体类文件数: $entity_files"
echo "  使用LocalDateTime的实体类数: $entity_localdatetime_files"

if [ "$entity_localdatetime_files" -gt 0 ]; then
    percentage=$((entity_localdatetime_files * 100 / entity_files))
    echo "  实体类LocalDateTime使用率: ${percentage}%"
fi

echo
echo "4. 检查证书服务修复状态..."

if grep -q "convertToLocalDateTime" src/main/java/com/gxwebsoft/common/core/service/CertificateService.java; then
    echo "  ✅ CertificateService.java - 类型转换方法已添加"
else
    echo "  ❌ CertificateService.java - 需要检查"
fi

echo
echo "=== 验证结果 ==="

# 统计可能的问题
type_mismatch_count=$(find src/main/java -name "*.java" -exec grep -c "Date.*=.*get.*Time()" {} \; | awk '{sum += $1} END {print sum+0}')
dateutil_setter_count=$(find src/main/java -name "*.java" -exec grep -c "\.set.*Time(DateUtil\." {} \; | awk '{sum += $1} END {print sum+0}')
compare_issues_count=$(find src/main/java -name "*.java" -exec grep -c "\.compareTo(DateUtil\.date())" {} \; | awk '{sum += $1} END {print sum+0}')

total_issues=$((type_mismatch_count + dateutil_setter_count + compare_issues_count))

if [ "$total_issues" -eq 0 ]; then
    echo "🎉 所有时间类型兼容性问题已修复！"
    echo "✅ 项目已成功统一使用LocalDateTime"
    echo "✅ 可以安全地进行编译和测试"
else
    echo "⚠️  还有 $total_issues 个潜在问题需要检查"
    echo "   - 类型不匹配: $type_mismatch_count"
    echo "   - DateUtil setter调用: $dateutil_setter_count"
    echo "   - 比较问题: $compare_issues_count"
fi

echo
echo "建议："
echo "1. 运行项目编译检查是否有编译错误"
echo "2. 运行单元测试确保功能正常"
echo "3. 特别测试时间相关的功能（过期检查、时间设置等）"
