Files
inquiry_robot/deploy/_remote_sql_seed_quote_templates.ps1
T

84 lines
4.9 KiB
PowerShell

# Seed templates into DB + upload dir without login (ASCII)
$ErrorActionPreference='Stop'
Get-Content 'E:\wwwroot\.env' | ForEach-Object {
$line=$_.Trim(); if(-not $line -or $line.StartsWith('#')){return}
$i=$line.IndexOf('='); if($i -lt 1){return}
Set-Item -Path ("Env:"+$line.Substring(0,$i).Trim()) -Value $line.Substring($i+1).Trim().Trim('"')
}
$mysql='C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe'
$hostName='127.0.0.1'
$Work='E:\wwwroot\inquiry-new\quote-template-seed'
$zip='C:\Users\ytd_extdev_0903\incoming\quote-templates-seed.zip'
if(-not (Test-Path $zip)){ throw "missing $zip" }
if(Test-Path $Work){ Remove-Item $Work -Recurse -Force }
Expand-Archive -Path $zip -DestinationPath $Work -Force
$manifest=Get-Content (Join-Path $Work 'manifest.json') -Raw -Encoding UTF8 | ConvertFrom-Json
$uploadRoot = if($env:JEECG_UPLOAD_PATH){ $env:JEECG_UPLOAD_PATH } else { 'E:\wwwroot\data\upFiles' }
$bizPath = Join-Path $uploadRoot 'inquiry\quote-templates'
New-Item -ItemType Directory -Force -Path $bizPath | Out-Null
$files=Get-ChildItem -Path (Join-Path $Work 'templates') -Recurse -Filter '*.xlsx' | Where-Object { $_.Name -notlike '~$*' }
$byName=@{}
foreach($f in $files){ if(-not $byName.ContainsKey($f.Name) -or $f.FullName.Length -lt $byName[$f.Name].FullName.Length){ $byName[$f.Name]=$f } }
function Sql([string]$q){
& $mysql -h $hostName -P 3306 -u $env:MYSQL_USER "--password=$env:MYSQL_PASSWORD" --default-character-set=utf8mb4 $env:MYSQL_DB -e $q
if($LASTEXITCODE -ne 0){ throw "sql fail $LASTEXITCODE" }
}
$ok=0
foreach($item in $manifest.items){
$name=[string]$item.fileName
if(-not $byName.ContainsKey($name)){ Write-Host ("SKIP "+$name); continue }
$src=$byName[$name].FullName
$id = ([guid]::NewGuid().ToString('N').Substring(0,32))
$vid = ([guid]::NewGuid().ToString('N').Substring(0,32))
$destName = $id + '_' + $name
$dest = Join-Path $bizPath $destName
Copy-Item $src $dest -Force
$objectKey = ('inquiry/quote-templates/' + $destName).Replace('\','/')
$sha = (Get-FileHash $dest -Algorithm SHA256).Hash.ToLower()
$size = (Get-Item $dest).Length
$mode=[string]$item.mode
$biz=[string]$item.bizType
$cat=[string]$item.category
$kw = ($item.keywords | ConvertTo-Json -Compress)
$tplName = [IO.Path]::GetFileNameWithoutExtension($name)
$status = if($mode -eq 'manual'){ 'enabled' } else { 'enabled' }
$mapping = '{"schemaVersion":"quote-template-mapping-v1","fields":{},"note":"seeded_without_parse; re-upload to refresh mapping"}'
$points = 0
if($item.suggestedFields){
# 禁止用 ConvertTo-Json 直接塞库:WinPS 可能产出非法 JSON,导致 previewSample 解析失败
$fieldsObj = @{}
$item.suggestedFields.PSObject.Properties | ForEach-Object { $fieldsObj[$_.Name]=$_.Value }
$fieldsJsonParts = @()
foreach($k in $fieldsObj.Keys){
$fieldsJsonParts += ('"' + ($k -replace '\\','\\' -replace '"','\"') + '":"' + ([string]$fieldsObj[$k] -replace '\\','\\' -replace '"','\"') + '"')
}
$fieldsJson = '{' + ($fieldsJsonParts -join ',') + '}'
$mapping = '{"schemaVersion":"quote-template-mapping-v1","fields":' + $fieldsJson + ',"fixedYellowCells":[],"unmappedRequired":[]}'
$points = @($fieldsObj.Keys).Count
}
$esc = { param($s) if($null -eq $s){return ''}; return ([string]$s).Replace("\","\\").Replace("'","''") }
$sql = @"
INSERT INTO inq_quote_template(id,name,biz_type,category,keywords_json,mode,status,is_default,current_version,use_count,create_by,create_time)
VALUES('$id','$(& $esc $tplName)','$(& $esc $biz)','$(& $esc $cat)','$(& $esc $kw)','$mode','$status',0,1,0,'seed',NOW())
ON DUPLICATE KEY UPDATE update_time=NOW();
INSERT INTO inq_quote_template_version(id,template_id,version_no,file_name,format,object_key,sha256,file_size,parse_status,mapping_point_count,mapping_json,parse_message,enabled,create_by,create_time)
VALUES('$vid','$id',1,'$(& $esc $name)','excel','$(& $esc $objectKey)','$sha',$size,'parsed',$points,'$(& $esc $mapping)','seed import',$points,1,'seed',NOW());
"@
# enabled=1 for version; fix SQL - enabled column is tinyint
$sql = @"
INSERT INTO inq_quote_template(id,name,biz_type,category,keywords_json,mode,status,is_default,current_version,use_count,create_by,create_time)
VALUES('$id','$(& $esc $tplName)','$(& $esc $biz)','$(& $esc $cat)','$(& $esc $kw)','$mode','$status',0,1,0,'seed',NOW());
INSERT INTO inq_quote_template_version(id,template_id,version_no,file_name,format,object_key,sha256,file_size,parse_status,mapping_point_count,mapping_json,parse_message,enabled,create_by,create_time)
VALUES('$vid','$id',1,'$(& $esc $name)','excel','$(& $esc $objectKey)','$sha',$size,'parsed',$points,'$(& $esc $mapping)','seed import',1,'seed',NOW());
"@
Sql $sql
Write-Host ("OK " + $name + " id=" + $id + " points=" + $points)
$ok++
}
Sql "SELECT COUNT(*) AS templates FROM inq_quote_template; SELECT COUNT(*) AS versions FROM inq_quote_template_version;"
Write-Host ("SQL_SEED_DONE ok="+$ok)