Files
inquiry_robot/deploy/_remote_seed_quote_templates.ps1
T

127 lines
5.2 KiB
PowerShell

# Seed quote templates via ledger API (ASCII-only script to avoid encoding parse errors)
$ErrorActionPreference = 'Stop'
$Incoming = 'C:\Users\ytd_extdev_0903\incoming'
$Work = 'E:\wwwroot\inquiry-new\quote-template-seed'
New-Item -ItemType Directory -Force -Path $Work | Out-Null
$zip = Join-Path $Incoming 'quote-templates-seed.zip'
if (-not (Test-Path $zip)) { throw "missing $zip" }
Expand-Archive -Path $zip -DestinationPath $Work -Force
$manifestPath = Join-Path $Work 'manifest.json'
$manifest = Get-Content $manifestPath -Raw -Encoding UTF8 | ConvertFrom-Json
function Invoke-Json($Method, $Url, $BodyObj, $Token) {
$headers = @{ 'Content-Type' = 'application/json;charset=UTF-8' }
if ($Token) { $headers['X-Access-Token'] = $Token }
$body = $null
if ($null -ne $BodyObj) { $body = ($BodyObj | ConvertTo-Json -Compress -Depth 8) }
return Invoke-RestMethod -Method $Method -Uri $Url -Headers $headers -Body $body -TimeoutSec 60
}
Write-Host 'login...'
$login = Invoke-Json 'POST' 'http://127.0.0.1:8180/jeecgboot/sys/login' @{
username = 'admin'
password = '123456'
captcha = ''
checkKey = 'deploy'
}
if (-not $login -or $login.success -ne $true) {
throw ('login failed: ' + ($login | ConvertTo-Json -Compress))
}
$token = $login.result.token
if (-not $token) { throw 'no token' }
Write-Host 'LOGIN_OK'
$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
}
}
$ok = 0
$fail = 0
foreach ($item in $manifest.items) {
$name = [string]$item.fileName
if (-not $byName.ContainsKey($name)) {
Write-Host ("SKIP missing " + $name)
continue
}
$path = $byName[$name].FullName
Write-Host ("UPLOAD " + $name + " mode=" + $item.mode)
try {
$boundary = [guid]::NewGuid().ToString('N')
$LF = "`r`n"
$fileBytes = [IO.File]::ReadAllBytes($path)
$keywordsJson = ($item.keywords | ConvertTo-Json -Compress)
if (-not $keywordsJson.StartsWith('[')) { $keywordsJson = ('["' + $item.keywords + '"]') }
$sb = New-Object System.Text.StringBuilder
$fields = [ordered]@{
name = [IO.Path]::GetFileNameWithoutExtension($name)
bizType = [string]$item.bizType
category = [string]$item.category
keywordsJson = $keywordsJson
mode = [string]$item.mode
isDefault = 'false'
}
foreach ($k in $fields.Keys) {
[void]$sb.Append(('--' + $boundary + $LF))
[void]$sb.Append(('Content-Disposition: form-data; name="' + $k + '"' + $LF + $LF))
[void]$sb.Append(([string]$fields[$k] + $LF))
}
[void]$sb.Append(('--' + $boundary + $LF))
[void]$sb.Append(('Content-Disposition: form-data; name="file"; filename="' + $name + '"' + $LF))
[void]$sb.Append(('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + $LF + $LF))
$prefix = [Text.Encoding]::UTF8.GetBytes($sb.ToString())
$suffix = [Text.Encoding]::UTF8.GetBytes(($LF + '--' + $boundary + '--' + $LF))
$body = New-Object byte[] ($prefix.Length + $fileBytes.Length + $suffix.Length)
[Array]::Copy($prefix, 0, $body, 0, $prefix.Length)
[Array]::Copy($fileBytes, 0, $body, $prefix.Length, $fileBytes.Length)
[Array]::Copy($suffix, 0, $body, $prefix.Length + $fileBytes.Length, $suffix.Length)
$req = [System.Net.HttpWebRequest]::Create('http://127.0.0.1:8180/jeecgboot/inquiry/quoteTemplate/upload')
$req.Method = 'POST'
$req.ContentType = ('multipart/form-data; boundary=' + $boundary)
$req.Headers.Add('X-Access-Token', $token)
$req.Timeout = 180000
$req.ReadWriteTimeout = 180000
$req.ContentLength = $body.Length
$stream = $req.GetRequestStream()
$stream.Write($body, 0, $body.Length)
$stream.Close()
$resp = $req.GetResponse()
$reader = New-Object IO.StreamReader($resp.GetResponseStream())
$text = $reader.ReadToEnd()
$reader.Close(); $resp.Close()
$obj = $text | ConvertFrom-Json
if ($obj.success -ne $true) {
throw ('api fail ' + $text.Substring(0, [Math]::Min(400, $text.Length)))
}
$row = $obj.result
Write-Host (' id=' + $row.id + ' ver=' + $row.versionId + ' parse=' + $row.parseStatus + ' points=' + $row.mappingPointCount)
$canEnable = $true
if (($item.mode -eq 'auto') -and ("$($row.parseStatus)" -like '*incomplete*' -or "$($row.parseStatus)" -like '*未完成*')) {
$canEnable = $false
Write-Host ' skip enable mapping incomplete'
}
if ($canEnable -and $row.id -and $row.versionId) {
$enUrl = 'http://127.0.0.1:8180/jeecgboot/inquiry/quoteTemplate/enable?templateId=' + $row.id + '&versionId=' + $row.versionId
$en = Invoke-Json 'POST' $enUrl $null $token
Write-Host (' enable=' + $en.success + ' msg=' + $en.message)
}
$ok++
} catch {
$fail++
Write-Host ('FAIL ' + $name + ' : ' + $_.Exception.Message)
}
}
Write-Host ('SEED_DONE ok=' + $ok + ' fail=' + $fail)
$listUrl = 'http://127.0.0.1:8180/jeecgboot/inquiry/quoteTemplate/list?pageNo=1&pageSize=50'
$list = Invoke-Json 'GET' $listUrl $null $token
Write-Host ('list_total=' + $list.result.total)
if ($fail -gt 0) { exit 2 }