data_import.html 22.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据导入 - 问界门店运营数据分析平台</title>
    <link rel="stylesheet" href="./static/css/style.css">
    <style>
        .upload-section { margin-bottom: 30px; }
        .upload-card { border: 2px dashed #d9d9d9; border-radius: 8px; padding: 30px; text-align: center; transition: all 0.3s; cursor: pointer; background: #fafafa; }
        .upload-card:hover, .upload-card.dragover { border-color: #1890ff; background: #e6f7ff; }
        .upload-card .icon { font-size: 48px; color: #1890ff; margin-bottom: 12px; }
        .upload-card .text { font-size: 14px; color: #666; }
        .upload-card .hint { font-size: 12px; color: #999; margin-top: 8px; }
        .form-row { display: flex; gap: 16px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 16px; }
        .form-group { display: flex; flex-direction: column; gap: 4px; }
        .form-group label { font-size: 13px; font-weight: 500; color: #333; }
        .form-group select, .form-group input { padding: 8px 12px; border: 1px solid #d9d9d9; border-radius: 4px; font-size: 13px; min-width: 160px; }
        .file-selected { margin-top: 12px; font-size: 13px; color: #52c41a; display: none; }
        .btn-submit { margin-top: 16px; padding: 10px 28px; background: #1890ff; color: #fff; border: none; border-radius: 6px; font-size: 14px; cursor: pointer; }
        .btn-submit:hover { background: #40a9ff; }
        .btn-submit:disabled { background: #ccc; cursor: not-allowed; }
        .tabs { display: flex; border-bottom: 1px solid #e8e8e8; margin-bottom: 20px; }
        .tab-item { padding: 10px 24px; cursor: pointer; font-size: 14px; color: #666; border-bottom: 2px solid transparent; transition: all 0.2s; }
        .tab-item:hover { color: #1890ff; }
        .tab-item.active { color: #1890ff; border-bottom-color: #1890ff; font-weight: 500; }
        .result-box { margin-top: 20px; padding: 16px; border-radius: 6px; font-size: 14px; display: none; }
        .result-box.success { display: block; background: #f6ffed; border: 1px solid #b7eb8f; color: #389e0d; }
        .result-box.error { display: block; background: #fff2f0; border: 1px solid #ffccc7; color: #cf1322; }
        .history-table { width: 100%; border-collapse: collapse; margin-top: 16px; }
        .history-table th { background: #fafafa; padding: 10px 12px; text-align: left; font-weight: 500; font-size: 13px; border-bottom: 1px solid #f0f0f0; }
        .history-table td { padding: 9px 12px; font-size: 13px; border-bottom: 1px solid #f0f0f0; }
        .tag { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 12px; }
        .tag-imported { background: #e6f7ff; color: #1890ff; }
        .tag-overwritten { background: #fff7e6; color: #fa8c16; }
    </style>
</head>
<body>
    <header>
        <div class="logo">问界运营分析平台</div>
        <nav class="nav-links">
            <a class="nav-item" href="./">首页</a>
            <a class="nav-item" href="./ods">ODS 同步任务</a>
            <a class="nav-item" href="./dw">DWS/DWM/DWA 任务</a>
            <a class="nav-item" href="./analysis">问界生态系数分析</a>
            <a class="nav-item" href="./harmony">鸿蒙生态分析</a>
            <a class="nav-item" href="./harmony/sub-brand">鸿蒙子品牌分析</a>
            <a class="nav-item" href="./strategy">策略效果盯防</a>
        </nav>
        <div class="user-info" id="user-info">管理员</div>
    </header>

    <main>
        <section id="content-area">
            <div class="card">
                <h2>数据导入</h2>

                <!-- Tab 切换 -->
                <div class="tabs" style="margin-top:16px">
                    <div class="tab-item active" onclick="switchTab('store')">门店兜底配置</div>
                    <div class="tab-item" onclick="switchTab('oem')">主机厂交付数据</div>
                    <div class="tab-item" onclick="switchTab('history')">上传历史</div>
                </div>

                <!-- 门店兜底配置 Tab -->
                <div id="tab-store" class="upload-section">
                    <div class="form-row">
                        <div class="form-group">
                            <label>品牌</label>
                            <select id="store-brand">
                                <option value="问界">问界</option>
                                <option value="鸿蒙">鸿蒙</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>数据周期开始</label>
                            <input type="date" id="store-period-start" />
                        </div>
                        <div class="form-group">
                            <label>数据周期结束</label>
                            <input type="date" id="store-period-end" />
                        </div>
                    </div>
                    <div class="upload-card" id="store-drop-zone" onclick="document.getElementById('store-file-input').click()">
                        <div class="icon">📁</div>
                        <div class="text">点击选择 或 拖拽上传 门店兜底配置 Excel</div>
                        <div class="hint">问界: 门店数据-问界-模板004.xlsx &nbsp;|&nbsp; 鸿蒙: 门店数据-鸿蒙-模板009.xlsx</div>
                        <div class="file-selected" id="store-file-selected"></div>
                    </div>
                    <input type="file" id="store-file-input" accept=".xlsx,.xls" style="display:none" onchange="onFileSelect(this, 'store-file-selected', 'store-drop-zone')" />
                    <button class="btn-submit" id="store-submit-btn" onclick="uploadStore()">导入门店兜底配置</button>
                    <div class="result-box" id="store-result"></div>
                </div>

                <!-- 主机厂交付数据 Tab -->
                <div id="tab-oem" class="upload-section" style="display:none">
                    <div class="form-row">
                        <div class="form-group">
                            <label>品牌</label>
                            <select id="oem-brand">
                                <option value="问界">问界</option>
                                <option value="鸿蒙">鸿蒙</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>周期类型</label>
                            <select id="oem-period-type">
                                <option value="">-- 请选择 --</option>
                                <option value="ecology">生态月(26日)</option>
                                <option value="nature">自然月(1日)</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>数据周期开始</label>
                            <input type="date" id="oem-period-start" />
                        </div>
                        <div class="form-group">
                            <label>数据周期结束</label>
                            <input type="date" id="oem-period-end" />
                        </div>
                    </div>
                    <div class="upload-card" id="oem-drop-zone" onclick="document.getElementById('oem-file-input').click()">
                        <div class="icon">📁</div>
                        <div class="text">点击选择 或 拖拽上传 主机厂交付数据 Excel</div>
                        <div class="hint">问界: 主机厂数据-问界-模板002.xlsx &nbsp;|&nbsp; 鸿蒙: 主机厂数据-鸿蒙-模板009.xlsx</div>
                        <div class="file-selected" id="oem-file-selected"></div>
                    </div>
                    <input type="file" id="oem-file-input" accept=".xlsx,.xls" style="display:none" onchange="onFileSelect(this, 'oem-file-selected', 'oem-drop-zone')" />
                    <button class="btn-submit" id="oem-submit-btn" onclick="uploadOem()">导入主机厂交付数据</button>
                    <div class="result-box" id="oem-result"></div>
                </div>

                <!-- 上传历史 Tab -->
                <div id="tab-history" style="display:none">
                    <div class="form-row" style="margin-bottom:16px">
                        <div class="form-group">
                            <label>品牌筛选</label>
                            <select id="history-brand" onchange="loadHistory()">
                                <option value="">全部</option>
                                <option value="问界">问界</option>
                                <option value="鸿蒙">鸿蒙</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>类型筛选</label>
                            <select id="history-type" onchange="loadHistory()">
                                <option value="">全部</option>
                                <option value="store_guarantee">门店兜底配置</option>
                                <option value="oem_delivery">主机厂交付数据</option>
                            </select>
                        </div>
                    </div>
                    <table class="history-table">
                        <thead>
                            <tr>
                                <th>批次号</th>
                                <th>品牌</th>
                                <th>数据类型</th>
                                <th>周期类型</th>
                                <th>数据周期</th>
                                <th>文件名</th>
                                <th>行数</th>
                                <th>状态</th>
                                <th>上传时间</th>
                            </tr>
                        </thead>
                        <tbody id="history-tbody">
                            <tr><td colspan="9" style="text-align:center;color:#999;padding:30px">加载中...</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>
    </main>

    <script>var APP_ROOT = '..';</script>
    <script src="./static/js/common.js"></script>
    <script src="./static/js/auth.js"></script>
    <script>
        var currentTab = 'store';
        var storeFile = null, oemFile = null;

        window.switchTab = function(tab) {
            currentTab = tab;
            document.querySelectorAll('.tab-item').forEach((el, i) => {
                el.classList.toggle('active', i === ['store','oem','history'].indexOf(tab));
            });
            document.getElementById('tab-store').style.display = tab === 'store' ? '' : 'none';
            document.getElementById('tab-oem').style.display = tab === 'oem' ? '' : 'none';
            document.getElementById('tab-history').style.display = tab === 'history' ? '' : 'none';
            if (tab === 'history') loadHistory();
        };

        // 文件选择与拖拽
        function onFileSelect(input, infoId, zoneId) {
            if (input.files.length > 0) {
                var f = input.files[0];
                if (infoId === 'store-file-selected') storeFile = f;
                else oemFile = f;
                var el = document.getElementById(infoId);
                el.textContent = '已选择: ' + f.name + ' (' + formatFileSize(f.size) + ')';
                el.style.display = 'block';
            }
        }

        function formatFileSize(bytes) {
            if (bytes < 1024) return bytes + ' B';
            if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB';
            return (bytes / 1048576).toFixed(1) + ' MB';
        }

        // 门店兜底上传
        window.uploadStore = async function() {
            if (!storeFile) { alert('请选择文件'); return; }
            var brand = document.getElementById('store-brand').value;
            var ps = document.getElementById('store-period-start').value;
            var pe = document.getElementById('store-period-end').value;
            if (!ps || !pe) { alert('请选择数据周期'); return; }

            var btn = document.getElementById('store-submit-btn');
            btn.disabled = true; btn.textContent = '导入中...';
            var resultBox = document.getElementById('store-result');
            resultBox.className = 'result-box'; resultBox.style.display = 'none';

            var formData = new FormData();
            formData.append('file', storeFile);
            formData.append('brand', brand);
            formData.append('period_start', ps);
            formData.append('period_end', pe);

            try {
                var res = await fetch(window.API_BASE + '/data-import/upload-store', { method: 'POST', body: formData });
                var json = await res.json();
                resultBox.style.display = 'block';
                if (json.status === 'success') {
                    var d = json.data;
                    resultBox.className = 'result-box success';
                    var matchInfo = (d.merchant_matched != null) ? ', 门店匹配 <b>' + d.merchant_matched + '</b> 行' : '';
                    if (d.is_overwrite) {
                        resultBox.innerHTML = '✅ 已覆盖更新! 批次 <b>' + d.batch_no + '</b>, 周期 ' + d.period + ', 写入 <b>' + d.row_count + '</b> 行' +
                            (d.overwritten > 0 ? ' (替换 ' + d.overwritten + ' 行旧数据)' : '') + matchInfo;
                    } else {
                        resultBox.innerHTML = '✅ 导入成功! 批次号: <b>' + d.batch_no + '</b>, 导入 <b>' + d.row_count + '</b> 行' + matchInfo;
                    }
                } else {
                    resultBox.className = 'result-box error';
                    resultBox.textContent = '❌ ' + (json.detail || '未知错误');
                }
            } catch (err) {
                resultBox.style.display = 'block';
                resultBox.className = 'result-box error';
                resultBox.textContent = '❌ 请求失败: ' + err.message;
            }
            btn.disabled = false; btn.textContent = '导入门店兜底配置';
        };

        // 主机厂上传
        window.uploadOem = async function() {
            if (!oemFile) { alert('请选择文件'); return; }
            var brand = document.getElementById('oem-brand').value;
            var pt = document.getElementById('oem-period-type').value;
            var ps = document.getElementById('oem-period-start').value;
            var pe = document.getElementById('oem-period-end').value;
            if (!ps || !pe) { alert('请选择数据周期'); return; }

            var btn = document.getElementById('oem-submit-btn');
            btn.disabled = true; btn.textContent = '导入中...';
            var resultBox = document.getElementById('oem-result');
            resultBox.className = 'result-box'; resultBox.style.display = 'none';

            var formData = new FormData();
            formData.append('file', oemFile);
            formData.append('brand', brand);
            formData.append('period_start', ps);
            formData.append('period_end', pe);
            if (pt) formData.append('period_type', pt);

            try {
                var res = await fetch(window.API_BASE + '/data-import/upload-oem', { method: 'POST', body: formData });
                var json = await res.json();
                resultBox.style.display = 'block';
                if (json.status === 'success') {
                    var d = json.data;
                    resultBox.className = 'result-box success';
                    var matchInfo = (d.merchant_matched != null) ? ', 门店匹配 <b>' + d.merchant_matched + '</b> 行' : '';
                    if (d.is_overwrite) {
                        resultBox.innerHTML = '✅ 已覆盖更新! 批次 <b>' + d.batch_no + '</b>, 周期 ' + d.period + ', 写入 <b>' + d.row_count + '</b> 行' +
                            (d.overwritten > 0 ? ' (替换 ' + d.overwritten + ' 行旧数据)' : '') + matchInfo;
                    } else {
                        resultBox.innerHTML = '✅ 导入成功! 批次号: <b>' + d.batch_no + '</b>, 导入 <b>' + d.row_count + '</b> 行' + matchInfo;
                    }
                } else {
                    resultBox.className = 'result-box error';
                    resultBox.textContent = '❌ ' + (json.detail || '未知错误');
                }
            } catch (err) {
                resultBox.style.display = 'block';
                resultBox.className = 'result-box error';
                resultBox.textContent = '❌ 请求失败: ' + err.message;
            }
            btn.disabled = false; btn.textContent = '导入主机厂交付数据';
        };

        // 加载历史
        window.loadHistory = async function() {
            var tbody = document.getElementById('history-tbody');
            tbody.innerHTML = '<tr><td colspan="9" style="text-align:center;color:#999;padding:30px">加载中...</td></tr>';
            var brand = document.getElementById('history-brand').value;
            var dtype = document.getElementById('history-type').value;
            var params = new URLSearchParams();
            if (brand) params.append('brand', brand);
            if (dtype) params.append('data_type', dtype);

            try {
                var res = await fetch(window.API_BASE + '/data-import/history?' + params.toString());
                var json = await res.json();
                var rows = json.data || [];
                if (rows.length === 0) {
                    tbody.innerHTML = '<tr><td colspan="9" style="text-align:center;color:#999;padding:30px">暂无导入记录</td></tr>';
                    return;
                }
                var typeLabel = { store_guarantee: '门店兜底配置', oem_delivery: '主机厂交付数据' };
                var periodTypeLabel = { ecology: '生态月(26日)', nature: '自然月(1日)' };
                var statusLabel = { 1: '<span class="tag tag-imported">已导入</span>', 2: '<span class="tag tag-overwritten">已覆盖</span>', 3: '<span class="tag" style="background:#fff1f0;color:#f5222d">已删除</span>' };
                var html = '';
                rows.forEach(function(r) {
                    html += '<tr>' +
                        '<td><code>' + escapeHtml(r.batch_no) + '</code></td>' +
                        '<td>' + escapeHtml(r.brand) + '</td>' +
                        '<td>' + (typeLabel[r.data_type] || r.data_type) + '</td>' +
                        '<td>' + (periodTypeLabel[r.period_type] || '-') + '</td>' +
                        '<td>' + escapeHtml(r.data_period_start) + ' ~ ' + escapeHtml(r.data_period_end) + '</td>' +
                        '<td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="' + escapeHtml(r.file_name || '') + '">' + escapeHtml(r.file_name || '-') + '</td>' +
                        '<td>' + (r.row_count || 0) + '</td>' +
                        '<td>' + (statusLabel[r.status] || r.status) + '</td>' +
                        '<td>' + escapeHtml(r.created_at || '') + '</td>' +
                        '</tr>';
                });
                tbody.innerHTML = html;
            } catch (err) {
                tbody.innerHTML = '<tr><td colspan="9" style="text-align:center;color:#f5222d;padding:30px">加载失败: ' + escapeHtml(err.message) + '</td></tr>';
            }
        };

        // 默认周期: 上月26日 ~ 今天
        function initDefaultPeriod() {
            var now = new Date();
            var today = toDateStr(now);

            // 上月26日
            var prevMonth = new Date(now.getFullYear(), now.getMonth() - 1, 26);
            var from26 = toDateStr(prevMonth);

            // 本月1日
            var month1st = new Date(now.getFullYear(), now.getMonth(), 1);
            var from1st = toDateStr(month1st);

            // 门店兜底:默认上月26日~今天
            document.getElementById('store-period-start').value = from26;
            document.getElementById('store-period-end').value = today;

            // 主机厂:默认本月1日~今天
            document.getElementById('oem-period-start').value = from1st;
            document.getElementById('oem-period-end').value = today;
        }

        function toDateStr(d) {
            var y = d.getFullYear();
            var m = ('0' + (d.getMonth() + 1)).slice(-2);
            var day = ('0' + d.getDate()).slice(-2);
            return y + '-' + m + '-' + day;
        }

        // 拖拽支持
        ['store-drop-zone', 'oem-drop-zone'].forEach(function(zoneId) {
            var zone = document.getElementById(zoneId);
            if (!zone) return;
            zone.addEventListener('dragover', function(e) { e.preventDefault(); zone.classList.add('dragover'); });
            zone.addEventListener('dragleave', function() { zone.classList.remove('dragover'); });
            zone.addEventListener('drop', function(e) {
                e.preventDefault();
                zone.classList.remove('dragover');
                var inputId = zoneId === 'store-drop-zone' ? 'store-file-input' : 'oem-file-input';
                var infoId = zoneId === 'store-drop-zone' ? 'store-file-selected' : 'oem-file-selected';
                var input = document.getElementById(inputId);
                if (e.dataTransfer.files.length > 0) {
                    input.files = e.dataTransfer.files;
                    onFileSelect(input, infoId, zoneId);
                }
            });
        });

        function escapeHtml(s) {
            if (!s) return '';
            return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
        }

        // 初始化历史
        initDefaultPeriod();
        loadHistory();
    </script>
</body>
</html>