# Change @Profile("admin") to @Profile({"admin", "company"}) for non-conflicting controllers
$saasPath = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"

# Get conflicting paths (these should NOT be changed)
$conflictPaths = @(
    "/bill/billLog", "/chat/chatDataset", "/chat/chatDatasetFile", "/chat/chatKeyword",
    "/chat/chatMsg", "/chat/chatMsgLogs", "/chat/chatRole", "/chat/chatSession",
    "/chat/chatUser", "/chat/upload", "/course/courseAnswerLog", "/course/courseFinishTemp",
    "/course/courseFinishTempParent", "/course/courseLink", "/course/courseRedPacketLog",
    "/course/courseTrafficLog", "/course/period", "/course/playSourceConfig",
    "/course/sop", "/course/sopLogs", "/course/trainingCamp", "/course/userCourse",
    "/course/userCourseCategory", "/course/userCourseVideo", "/crm/customer",
    "/crm/customerAssign", "/crm/customerContacts", "/crm/customerExt",
    "/crm/customerHisOrder", "/crm/customerLevel", "/crm/customerLogs",
    "/crm/customerUser", "/crm/customerVisit", "/crm/event", "/crm/msg",
    "/fastGpt/fastgptChatArtificialWords", "/fastGpt/fastgptEventLogTotal",
    "/fastGpt/fastGptKeywordSend", "/fastGpt/fastGptRole", "/fastGpt/fastGptRoleTag",
    "/his/doctor", "/his/follow", "/his/FsFollowReport", "/his/integralGoods",
    "/his/integralOrder", "/his/userOperationLog", "/index/statistics",
    "/live/coupon", "/live/gift", "/live/live", "/live/liveAfterSales",
    "/live/liveAnchor", "/live/liveCart", "/live/liveGoods", "/live/liveLotteryConf",
    "/live/liveLotteryRecord", "/live/liveLotteryRegistration", "/live/liveMsg",
    "/live/liveOrder", "/live/liveOrderItem", "/live/liveOrderLogs",
    "/live/liveRedConf", "/live/liveUserFavorite", "/live/liveUserFollow",
    "/live/liveUserLike", "/live/liveUserLotteryRecord", "/live/liveVideo",
    "/live/record", "/live/task", "/live/words", "/liveData/liveData",
    "/monitor/logininfor", "/monitor/operlog", "/order",
    "/qw/appContactWay", "/qw/externalContact", "/qw/externalContactTransferCompanyAudit",
    "/qw/friendWelcome", "/qw/group_chat_user", "/qw/groupChat", "/qw/qwDept",
    "/qw/QwWorkTaskNew", "/qw/sop", "/qw/sopLogs", "/qw/sopTemp", "/qw/tag",
    "/qw/tagGroup", "/qw/user", "/qwSop/sopUserLogs", "/qwSop/sopUserLogsInfo",
    "/store/material", "/store/materialGroup", "/store/store/statistics",
    "/store/store/storeAfterSales", "/store/store/storeOrder", "/store/store/storeOrderAudit",
    "/store/store/storeOrderOffline", "/store/store/storePayment", "/store/store/storeProduct",
    "/store/store/storeProductCategory", "/store/store/storeProductPackage",
    "/store/userOnlineState", "/system/approval", "/system/config",
    "/system/dict/data", "/system/dict/type"
)

# Scan all controllers with @Profile("admin")
$controllers = Get-ChildItem -Path $saasPath -Recurse -Filter "*Controller.java"
$changedCount = 0
$skippedCount = 0
$errorCount = 0

foreach ($file in $controllers) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    
    # Check if this has @Profile("admin")
    if ($content -notmatch '@Profile\s*\(\s*"admin"\s*\)') { continue }
    
    # Get the RequestMapping path
    $path = ""
    if ($content -match '@RequestMapping\s*\(\s*(?:value\s*=\s*)?"/([^"]+)"') {
        $path = "/" + $Matches[1]
    }
    
    if ($path -eq "") { continue }
    
    # Check if this path conflicts
    if ($conflictPaths -contains $path) {
        $skippedCount++
        continue
    }
    
    # Change @Profile("admin") to @Profile({"admin", "company"})
    # Need to handle import as well
    
    $newContent = $content
    
    # Replace @Profile("admin") with @Profile({"admin", "company"})
    $newContent = $newContent -replace '@Profile\s*\(\s*"admin"\s*\)', '@Profile({"admin", "company"})'
    
    # Add import for org.springframework.context.annotation.Profile if not already present
    # Actually, Profile is already imported since @Profile("admin") exists
    
    if ($newContent -ne $content) {
        # Write back without BOM
        $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
        [System.IO.File]::WriteAllText($file.FullName, $newContent, $utf8NoBom)
        $changedCount++
    }
}

Write-Host "Changed: $changedCount controllers"
Write-Host "Skipped (conflicting): $skippedCount controllers"
Write-Host "Errors: $errorCount"

# Verify a few changes
Write-Host "`n=== Verification ==="
$verifyFiles = Get-ChildItem -Path $saasPath -Recurse -Filter "*Controller.java" | Select-Object -First 5
foreach ($file in $verifyFiles) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    if ($content -match '@Profile\s*\(\s*\{"admin",\s*"company"\}\s*\)') {
        Write-Host "  OK: $($file.Name) has @Profile({admin, company})"
    }
}
