7-шаговый wizard: загрузка ZIP+целевой → поиск по ключевому слову → выбор колонок → сопоставление → превью → применение → скачивание. FastAPI + vanilla JS (Bootstrap 5), порт 8102. Протестировано на реальных данных: 4 ресурсных ведомости → Сводка стоимости затрат труда (19 обновлений + 10 новых разрядов). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
468 lines
19 KiB
HTML
468 lines
19 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Wizard — Агрегатор xlsx</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
body { background: #f5f6fa; }
|
|
.wizard-container { max-width: 900px; margin: 30px auto; }
|
|
.step { display: none; }
|
|
.step.active { display: block; }
|
|
.step-indicators { display: flex; gap: 4px; margin-bottom: 20px; }
|
|
.step-ind { flex: 1; height: 6px; border-radius: 3px; background: #dee2e6; transition: background .3s; }
|
|
.step-ind.done { background: #198754; }
|
|
.step-ind.current { background: #0d6efd; }
|
|
.preview-table { max-height: 350px; overflow-y: auto; font-size: 0.85rem; }
|
|
.preview-table td, .preview-table th { white-space: nowrap; }
|
|
.status-add { color: #198754; font-weight: 600; }
|
|
.status-update { color: #0d6efd; }
|
|
.file-list { font-size: 0.9rem; }
|
|
.spinner-sm { width: 1rem; height: 1rem; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="wizard-container">
|
|
<h4 class="mb-3">Агрегатор xlsx</h4>
|
|
|
|
<!-- Step indicators -->
|
|
<div class="step-indicators" id="stepIndicators"></div>
|
|
|
|
<!-- Step 1: Upload -->
|
|
<div class="step active" id="step1">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 1. Загрузка файлов</h5>
|
|
<div class="mb-3">
|
|
<label class="form-label">ZIP-архив с источниками (.xlsx)</label>
|
|
<input type="file" class="form-control" id="zipFile" accept=".zip">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Целевой файл (.xlsx)</label>
|
|
<input type="file" class="form-control" id="targetFile" accept=".xlsx">
|
|
</div>
|
|
<button class="btn btn-primary" onclick="uploadFiles()">
|
|
<span id="uploadSpinner" class="spinner-border spinner-sm d-none" role="status"></span>
|
|
Загрузить
|
|
</button>
|
|
<div id="uploadError" class="text-danger mt-2"></div>
|
|
<div id="uploadResult" class="mt-3"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 2: Search criterion -->
|
|
<div class="step" id="step2">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 2. Критерий поиска в источниках</h5>
|
|
<div class="row mb-3">
|
|
<div class="col-md-4">
|
|
<label class="form-label">Лист источника</label>
|
|
<select class="form-select" id="sourceSheet"></select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Искать в колонке</label>
|
|
<select class="form-select" id="searchColumn"></select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Ключевое слово</label>
|
|
<input type="text" class="form-control" id="searchKeyword" value="разряд">
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-outline-primary btn-sm" onclick="doAnalyze()">Найти</button>
|
|
<div id="searchPreview" class="mt-3"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 3: Value column -->
|
|
<div class="step" id="step3">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 3. Колонка значений в источниках</h5>
|
|
<div class="mb-3">
|
|
<label class="form-label">Из какой колонки брать числа?</label>
|
|
<select class="form-select" id="valueColumn"></select>
|
|
</div>
|
|
<button class="btn btn-outline-primary btn-sm" onclick="doAnalyzeWithValue()">Показать превью</button>
|
|
<div id="valuePreview" class="mt-3"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 4: Target matching -->
|
|
<div class="step" id="step4">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 4. Сопоставление с целевым</h5>
|
|
<div class="row mb-3">
|
|
<div class="col-md-4">
|
|
<label class="form-label">Лист целевого</label>
|
|
<select class="form-select" id="targetSheet"></select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Колонка сопоставления</label>
|
|
<select class="form-select" id="matchColumn"></select>
|
|
</div>
|
|
</div>
|
|
<p id="matchInfo" class="text-muted"></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 5: Action -->
|
|
<div class="step" id="step5">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 5. Действие</h5>
|
|
<div class="row mb-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label">Операция</label>
|
|
<select class="form-select" id="actionType">
|
|
<option value="sum" selected>Суммировать</option>
|
|
<option value="replace">Заменить</option>
|
|
<option value="max">Максимум</option>
|
|
<option value="min">Минимум</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">Записать в колонку</label>
|
|
<select class="form-select" id="targetWriteColumn"></select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 6: Unmatched -->
|
|
<div class="step" id="step6">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 6. Ненайденные строки</h5>
|
|
<div class="mb-3">
|
|
<label class="form-label">Если в целевом не найден ключ:</label>
|
|
<select class="form-select" id="unmatchedAction">
|
|
<option value="add" selected>Добавить новые строки</option>
|
|
<option value="skip">Пропустить</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Step 7: Preview & Run -->
|
|
<div class="step" id="step7">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Шаг 7. Превью и подтверждение</h5>
|
|
<div id="previewLoading" class="text-center py-3 d-none">
|
|
<div class="spinner-border text-primary" role="status"></div>
|
|
<p class="mt-2">Вычисляю...</p>
|
|
</div>
|
|
<div id="previewResult"></div>
|
|
<div id="previewSummary" class="mt-3"></div>
|
|
<button class="btn btn-success mt-3 d-none" id="runBtn" onclick="runAggregation()">
|
|
<span id="runSpinner" class="spinner-border spinner-sm d-none" role="status"></span>
|
|
Применить и скачать
|
|
</button>
|
|
<div id="runResult" class="mt-3"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Navigation -->
|
|
<div class="d-flex justify-content-between mt-3">
|
|
<button class="btn btn-outline-secondary" id="prevBtn" onclick="prevStep()" disabled>Назад</button>
|
|
<button class="btn btn-primary" id="nextBtn" onclick="nextStep()" disabled>Далее</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const TOTAL_STEPS = 7;
|
|
let currentStep = 1;
|
|
let sessionId = null;
|
|
let sourceData = null;
|
|
let targetData = null;
|
|
|
|
// --- Navigation ---
|
|
function updateStepUI() {
|
|
for (let i = 1; i <= TOTAL_STEPS; i++) {
|
|
document.getElementById('step' + i).classList.toggle('active', i === currentStep);
|
|
}
|
|
document.getElementById('prevBtn').disabled = currentStep === 1;
|
|
document.getElementById('nextBtn').disabled = !canProceed();
|
|
// Indicators
|
|
const ind = document.getElementById('stepIndicators');
|
|
ind.innerHTML = '';
|
|
for (let i = 1; i <= TOTAL_STEPS; i++) {
|
|
const cls = i < currentStep ? 'done' : i === currentStep ? 'current' : '';
|
|
ind.innerHTML += `<div class="step-ind ${cls}"></div>`;
|
|
}
|
|
// Auto-actions on step enter
|
|
if (currentStep === 7) loadPreview();
|
|
}
|
|
|
|
function canProceed() {
|
|
if (currentStep === 1) return !!sessionId;
|
|
if (currentStep === 2) return document.getElementById('searchColumn').value !== '';
|
|
if (currentStep === 3) return document.getElementById('valueColumn').value !== '';
|
|
if (currentStep === 4) return document.getElementById('matchColumn').value !== '';
|
|
if (currentStep === 5) return true;
|
|
if (currentStep === 6) return true;
|
|
return false;
|
|
}
|
|
|
|
function nextStep() {
|
|
if (currentStep < TOTAL_STEPS) { currentStep++; updateStepUI(); }
|
|
}
|
|
function prevStep() {
|
|
if (currentStep > 1) { currentStep--; updateStepUI(); }
|
|
}
|
|
|
|
// --- Step 1: Upload ---
|
|
async function uploadFiles() {
|
|
const zipFile = document.getElementById('zipFile').files[0];
|
|
const targetFile = document.getElementById('targetFile').files[0];
|
|
if (!zipFile || !targetFile) {
|
|
document.getElementById('uploadError').textContent = 'Выберите оба файла';
|
|
return;
|
|
}
|
|
document.getElementById('uploadSpinner').classList.remove('d-none');
|
|
document.getElementById('uploadError').textContent = '';
|
|
|
|
const fd = new FormData();
|
|
fd.append('zip_file', zipFile);
|
|
fd.append('target_file', targetFile);
|
|
|
|
try {
|
|
const resp = await fetch('/api/wizard/upload', { method: 'POST', body: fd });
|
|
if (!resp.ok) {
|
|
const err = await resp.json();
|
|
throw new Error(err.detail || 'Upload failed');
|
|
}
|
|
const data = await resp.json();
|
|
sessionId = data.session_id;
|
|
sourceData = data.sources;
|
|
targetData = data.target;
|
|
|
|
// Show summary
|
|
let html = '<div class="file-list">';
|
|
html += `<p><strong>Источники:</strong> ${sourceData.length} файл(ов)</p><ul>`;
|
|
for (const s of sourceData) {
|
|
html += `<li>${s.filename} — ${s.sheets.map(sh => sh.name + ' (' + sh.row_count + ' строк)').join(', ')}</li>`;
|
|
}
|
|
html += `</ul><p><strong>Целевой:</strong> ${targetData.filename}</p><ul>`;
|
|
for (const sh of targetData.sheets) {
|
|
html += `<li>${sh.name} — ${sh.row_count} строк, ${sh.columns.length} колонок</li>`;
|
|
}
|
|
html += '</ul></div>';
|
|
document.getElementById('uploadResult').innerHTML = html;
|
|
|
|
// Populate step 2 dropdowns
|
|
populateSourceDropdowns();
|
|
populateTargetDropdowns();
|
|
updateStepUI();
|
|
} catch (e) {
|
|
document.getElementById('uploadError').textContent = e.message;
|
|
} finally {
|
|
document.getElementById('uploadSpinner').classList.add('d-none');
|
|
}
|
|
}
|
|
|
|
function populateSourceDropdowns() {
|
|
// Source sheet
|
|
const sheetSel = document.getElementById('sourceSheet');
|
|
sheetSel.innerHTML = '';
|
|
const allSheets = new Set();
|
|
for (const s of sourceData) {
|
|
for (const sh of s.sheets) allSheets.add(sh.name);
|
|
}
|
|
for (const name of allSheets) {
|
|
sheetSel.innerHTML += `<option value="${name}">${name}</option>`;
|
|
}
|
|
|
|
// Source columns (from first source, first sheet)
|
|
const firstSheet = sourceData[0].sheets[0];
|
|
const colSel = document.getElementById('searchColumn');
|
|
const valSel = document.getElementById('valueColumn');
|
|
colSel.innerHTML = '';
|
|
valSel.innerHTML = '';
|
|
for (const c of firstSheet.columns) {
|
|
const label = `${c.letter}: ${c.header} (${c.type})`;
|
|
colSel.innerHTML += `<option value="${c.letter}" ${c.type === 'text' ? 'selected' : ''}>${label}</option>`;
|
|
valSel.innerHTML += `<option value="${c.letter}" ${c.type === 'number' ? 'selected' : ''}>${label}</option>`;
|
|
}
|
|
// Auto-select: prefer columns with meaningful headers
|
|
const textCols = firstSheet.columns.filter(c => c.type === 'text');
|
|
const numCols = firstSheet.columns.filter(c => c.type === 'number');
|
|
// Search column: prefer "Наименование" or first text column
|
|
const nameLike = textCols.find(c => /наименован|название/i.test(c.header));
|
|
if (nameLike) colSel.value = nameLike.letter;
|
|
else if (textCols.length) colSel.value = textCols[0].letter;
|
|
// Value column: prefer "Количество" / "Кол-во" or first number column
|
|
const qtyLike = numCols.find(c => /количеств|кол-во|кол\.во/i.test(c.header));
|
|
if (qtyLike) valSel.value = qtyLike.letter;
|
|
else if (numCols.length) valSel.value = numCols[0].letter;
|
|
}
|
|
|
|
function populateTargetDropdowns() {
|
|
const sheetSel = document.getElementById('targetSheet');
|
|
const matchSel = document.getElementById('matchColumn');
|
|
const writeSel = document.getElementById('targetWriteColumn');
|
|
sheetSel.innerHTML = '';
|
|
matchSel.innerHTML = '';
|
|
writeSel.innerHTML = '';
|
|
|
|
for (const sh of targetData.sheets) {
|
|
sheetSel.innerHTML += `<option value="${sh.name}">${sh.name}</option>`;
|
|
}
|
|
|
|
const firstSheet = targetData.sheets[0];
|
|
for (const c of firstSheet.columns) {
|
|
const label = `${c.letter}: ${c.header} (${c.type})`;
|
|
matchSel.innerHTML += `<option value="${c.letter}">${label}</option>`;
|
|
writeSel.innerHTML += `<option value="${c.letter}">${label}</option>`;
|
|
}
|
|
// Auto-select: prefer "Наименование" for match, "Количество" for write
|
|
const textCols = firstSheet.columns.filter(c => c.type === 'text');
|
|
const numCols = firstSheet.columns.filter(c => c.type === 'number');
|
|
const nameLike = textCols.find(c => /наименован|название/i.test(c.header));
|
|
if (nameLike) matchSel.value = nameLike.letter;
|
|
else if (textCols.length) matchSel.value = textCols[0].letter;
|
|
const qtyLike = numCols.find(c => /количеств|кол-во|кол\.во/i.test(c.header));
|
|
if (qtyLike) writeSel.value = qtyLike.letter;
|
|
else if (numCols.length) writeSel.value = numCols[0].letter;
|
|
}
|
|
|
|
// --- Step 2: Analyze ---
|
|
async function doAnalyze() {
|
|
const resp = await fetch('/api/wizard/analyze', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
session_id: sessionId,
|
|
search_column: document.getElementById('searchColumn').value,
|
|
search_keyword: document.getElementById('searchKeyword').value,
|
|
source_sheet: document.getElementById('sourceSheet').value,
|
|
}),
|
|
});
|
|
const data = await resp.json();
|
|
let html = `<p>Найдено строк: <strong>${data.found_count}</strong>, уникальных ключей: <strong>${data.unique_keys.length}</strong></p>`;
|
|
if (data.sample_rows.length) {
|
|
html += '<div class="preview-table"><table class="table table-sm table-striped"><thead><tr><th>Файл</th><th>Строка</th><th>Текст</th><th>Ключ</th></tr></thead><tbody>';
|
|
for (const r of data.sample_rows.slice(0, 15)) {
|
|
html += `<tr><td>${r.file}</td><td>${r.row}</td><td>${r.search_value}</td><td>${r.key || '-'}</td></tr>`;
|
|
}
|
|
html += '</tbody></table></div>';
|
|
}
|
|
document.getElementById('searchPreview').innerHTML = html;
|
|
updateStepUI();
|
|
}
|
|
|
|
// --- Step 3: Analyze with value column ---
|
|
async function doAnalyzeWithValue() {
|
|
const resp = await fetch('/api/wizard/analyze', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
session_id: sessionId,
|
|
search_column: document.getElementById('searchColumn').value,
|
|
search_keyword: document.getElementById('searchKeyword').value,
|
|
source_sheet: document.getElementById('sourceSheet').value,
|
|
value_column: document.getElementById('valueColumn').value,
|
|
}),
|
|
});
|
|
const data = await resp.json();
|
|
let html = '';
|
|
if (data.sample_rows.length) {
|
|
html += '<div class="preview-table"><table class="table table-sm table-striped"><thead><tr><th>Файл</th><th>Ключ</th><th>Значение</th></tr></thead><tbody>';
|
|
for (const r of data.sample_rows.slice(0, 10)) {
|
|
html += `<tr><td>${r.file}</td><td>${r.key || '-'}</td><td>${r.number_value != null ? r.number_value.toFixed(2) : '-'}</td></tr>`;
|
|
}
|
|
html += '</tbody></table></div>';
|
|
}
|
|
document.getElementById('valuePreview').innerHTML = html;
|
|
}
|
|
|
|
// --- Step 7: Preview ---
|
|
async function loadPreview() {
|
|
document.getElementById('previewLoading').classList.remove('d-none');
|
|
document.getElementById('previewResult').innerHTML = '';
|
|
document.getElementById('previewSummary').innerHTML = '';
|
|
document.getElementById('runBtn').classList.add('d-none');
|
|
|
|
const body = buildConfig();
|
|
try {
|
|
const resp = await fetch('/api/wizard/preview', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
});
|
|
const data = await resp.json();
|
|
let html = '<div class="preview-table"><table class="table table-sm table-striped"><thead><tr><th>Статус</th><th>Строка</th><th>Название</th><th>Было</th><th>Станет</th></tr></thead><tbody>';
|
|
for (const m of data.matches) {
|
|
const cls = m.status === 'add' ? 'status-add' : 'status-update';
|
|
const label = m.status === 'add' ? 'НОВЫЙ' : 'обновл.';
|
|
html += `<tr><td class="${cls}">${label}</td><td>${m.target_row || '—'}</td><td>${m.target_label}</td><td>${m.current_value != null ? m.current_value.toFixed(2) : '—'}</td><td>${m.new_value.toFixed(2)}</td></tr>`;
|
|
}
|
|
html += '</tbody></table></div>';
|
|
document.getElementById('previewResult').innerHTML = html;
|
|
|
|
const s = data.summary;
|
|
document.getElementById('previewSummary').innerHTML =
|
|
`<span class="badge bg-primary">${s.updates} обновлений</span> ` +
|
|
`<span class="badge bg-success">${s.adds} новых</span> ` +
|
|
`<span class="badge bg-secondary">Итого: ${s.total_new_value.toFixed(2)}</span>`;
|
|
|
|
if (data.matches.length > 0) {
|
|
document.getElementById('runBtn').classList.remove('d-none');
|
|
}
|
|
} finally {
|
|
document.getElementById('previewLoading').classList.add('d-none');
|
|
}
|
|
}
|
|
|
|
// --- Run ---
|
|
async function runAggregation() {
|
|
document.getElementById('runSpinner').classList.remove('d-none');
|
|
const body = buildConfig();
|
|
try {
|
|
const resp = await fetch('/api/wizard/run', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
});
|
|
const data = await resp.json();
|
|
if (data.success) {
|
|
document.getElementById('runResult').innerHTML =
|
|
`<div class="alert alert-success">Готово! Обновлено: ${data.stats.updated}, добавлено: ${data.stats.added}` +
|
|
`<br><a href="${data.download_url}" class="btn btn-outline-success btn-sm mt-2">Скачать результат</a></div>`;
|
|
}
|
|
} finally {
|
|
document.getElementById('runSpinner').classList.add('d-none');
|
|
}
|
|
}
|
|
|
|
function buildConfig() {
|
|
return {
|
|
session_id: sessionId,
|
|
search_keyword: document.getElementById('searchKeyword').value,
|
|
search_column: document.getElementById('searchColumn').value,
|
|
value_column: document.getElementById('valueColumn').value,
|
|
source_sheet: document.getElementById('sourceSheet').value || null,
|
|
target_sheet: document.getElementById('targetSheet').value || null,
|
|
match_column: document.getElementById('matchColumn').value,
|
|
target_write_column: document.getElementById('targetWriteColumn').value,
|
|
action: document.getElementById('actionType').value,
|
|
unmatched: document.getElementById('unmatchedAction').value,
|
|
};
|
|
}
|
|
|
|
// Init
|
|
updateStepUI();
|
|
</script>
|
|
</body>
|
|
</html>
|