gongxiangp2/scripts/update-missing-report-with-external-check.ts

218 lines
6.6 KiB
TypeScript

/**
* 根据外网服务器检查结果更新缺失源文件报告
*/
import { readFile, writeFile } from 'fs/promises'
interface MissingReportRow {
素材ID: string
素材类型: string
产品型号: string
作者名称: string
使用店铺: string
场景名称: string
平台名称: string
产品品类: string
渲染器: string
源文件路径: string
源文件名: string
源文件大小: string
文件是否存在: string
创建时间: string
更新时间: string
}
interface ExternalCheckRow {
素材ID: string
素材类型: string
产品型号: string
作者: string
使用店铺: string
源文件路径: string
外网服务器URL: string
文件是否存在: string
源文件大小: string
创建时间: string
}
async function main() {
console.log('='.repeat(60))
console.log('更新缺失源文件报告(根据外网服务器检查结果)')
console.log('='.repeat(60))
console.log()
// 简单的CSV解析函数
function parseCSVLine(line: string): string[] {
const result: string[] = []
let current = ''
let inQuotes = false
for (let i = 0; i < line.length; i++) {
const char = line[i]
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"'
i++
} else {
inQuotes = !inQuotes
}
} else if (char === ',' && !inQuotes) {
result.push(current)
current = ''
} else {
current += char
}
}
result.push(current)
return result
}
// 1. 读取外网服务器检查报告
console.log('📋 读取外网服务器检查报告...')
let externalCheckData: ExternalCheckRow[] = []
try {
const externalCheckContent = await readFile('老素材外网服务器检查报告.csv', 'utf-8')
const lines = externalCheckContent.split('\n').filter(line => line.trim())
if (lines.length < 2) {
throw new Error('CSV文件格式错误')
}
const headers = parseCSVLine(lines[0]).map(h => h.trim())
for (let i = 1; i < lines.length; i++) {
const values = parseCSVLine(lines[i])
const row: any = {}
headers.forEach((header, index) => {
row[header] = (values[index] || '').trim()
})
externalCheckData.push(row as ExternalCheckRow)
}
console.log(` 读取到 ${externalCheckData.length} 条外网服务器检查记录`)
} catch (error) {
console.error('❌ 无法读取外网服务器检查报告:', error)
console.log(' 请先运行完整检查脚本生成报告')
process.exit(1)
}
// 2. 构建外网服务器存在的素材ID集合
const externalExists = new Set<string>()
const externalMissing = new Set<string>()
for (const row of externalCheckData) {
if (row['文件是否存在'] === '是') {
externalExists.add(row['素材ID'])
} else {
externalMissing.add(row['素材ID'])
}
}
console.log(` 外网服务器存在: ${externalExists.size} 个`)
console.log(` 外网服务器缺失: ${externalMissing.size} 个`)
console.log()
// 3. 读取缺失源文件报告
console.log('📋 读取缺失源文件报告...')
let missingReportData: MissingReportRow[] = []
try {
const missingReportContent = await readFile('缺失源文件报告.csv', 'utf-8')
const lines = missingReportContent.split('\n').filter(line => line.trim())
if (lines.length < 2) {
throw new Error('CSV文件格式错误')
}
const headers = parseCSVLine(lines[0]).map(h => h.trim())
for (let i = 1; i < lines.length; i++) {
const values = parseCSVLine(lines[i])
const row: any = {}
headers.forEach((header, index) => {
row[header] = (values[index] || '').trim()
})
missingReportData.push(row as MissingReportRow)
}
console.log(` 读取到 ${missingReportData.length} 条缺失源文件记录`)
} catch (error) {
console.error('❌ 无法读取缺失源文件报告:', error)
process.exit(1)
}
// 4. 更新报告
console.log('🔄 更新报告...')
let updatedCount = 0
let removedCount = 0
const updatedRows: MissingReportRow[] = []
for (const row of missingReportData) {
const materialId = row['素材ID']
// 如果在外网服务器上存在,则更新"文件是否存在"字段
if (externalExists.has(materialId)) {
row['文件是否存在'] = '是(外网服务器)'
updatedCount++
updatedRows.push(row)
} else if (externalMissing.has(materialId)) {
// 如果在外网服务器上也缺失,保持原样
updatedRows.push(row)
} else {
// 不在外网服务器检查列表中的,保持原样(可能是新素材或其他情况)
updatedRows.push(row)
}
}
console.log(` 更新了 ${updatedCount} 条记录(标记为外网服务器存在)`)
console.log(` 保留 ${updatedRows.length} 条记录`)
console.log()
// 5. 生成更新后的报告
console.log('💾 生成更新后的报告...')
// 生成CSV内容
const csvRows: string[] = []
if (updatedRows.length > 0) {
const headers = Object.keys(updatedRows[0])
csvRows.push(headers.map(h => {
const str = String(h)
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
return `"${str.replace(/"/g, '""')}"`
}
return str
}).join(','))
for (const row of updatedRows) {
const values = headers.map(header => {
const value = row[header as keyof MissingReportRow] || ''
const str = String(value)
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
return `"${str.replace(/"/g, '""')}"`
}
return str
})
csvRows.push(values.join(','))
}
}
const outputDir = process.env.IS_IN_CONTAINER === 'true' ? '/tmp' : '.'
const outputFile = `${outputDir}/缺失源文件报告_已更新.csv`
await writeFile(outputFile, csvRows.join('\n'), 'utf-8')
console.log('✅ 更新完成!')
console.log(` 新报告文件: ${outputFile}`)
console.log()
// 6. 统计
const stillMissing = updatedRows.filter(r => r['文件是否存在'] === '否')
const existsOnExternal = updatedRows.filter(r => r['文件是否存在'] === '是(外网服务器)')
const existsOnLocal = updatedRows.filter(r => r['文件是否存在'] === '是')
console.log('📊 最终统计:')
console.log(` 外网服务器存在: ${existsOnExternal.length} 个`)
console.log(` 本地存在: ${existsOnLocal.length} 个`)
console.log(` 仍然缺失: ${stillMissing.length} 个`)
console.log()
}
main().catch((error) => {
console.error('执行失败:', error)
process.exit(1)
})