From d779aae3a7d0e0bf23a4cafe61acea6dd04126d0 Mon Sep 17 00:00:00 2001 From: mairuiming Date: Wed, 29 Oct 2025 19:44:45 +0800 Subject: [PATCH] 3 3 --- src/App.tsx | 229 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 159 insertions(+), 70 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 5dd8ad7..090d47d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -46,10 +46,11 @@ export default function App() { const [timelineLoading, setTimelineLoading] = useState(false); const [timelineResults, setTimelineResults] = useState([]); const [timelineAdjustments, setTimelineAdjustments] = useState<{[key: number]: number}>({}); - // 快照回填来源(foreign_id、款式、颜色) + // 快照回填来源(foreign_id、款式、颜色、文本2) const [currentForeignId, setCurrentForeignId] = useState(null); const [currentStyleText, setCurrentStyleText] = useState(''); const [currentColorText, setCurrentColorText] = useState(''); + const [currentText2, setCurrentText2] = useState(''); const [currentVersionNumber, setCurrentVersionNumber] = useState(null); // 功能入口模式与调整相关状态 const [mode, setMode] = useState<'generate' | 'adjust' | null>(null); @@ -175,6 +176,7 @@ export default function App() { if (snapshot.foreignId) setCurrentForeignId(snapshot.foreignId); if (snapshot.styleText) setCurrentStyleText(snapshot.styleText); if (snapshot.colorText) setCurrentColorText(snapshot.colorText); + if (snapshot.text2) setCurrentText2(snapshot.text2); if (snapshot.version !== undefined) { let vNum: number | null = null; if (typeof snapshot.version === 'number') { @@ -328,11 +330,8 @@ export default function App() { const DELIVERY_VERSION_FIELD_ID = 'fld5OmvZrn'; // 版本字段(新增) // 起始时间字段(货期记录表新增) const DELIVERY_START_TIME_FIELD_ID = 'fld727qCAv'; - // 文本2字段(货期记录表) + // 文本2字段(货期记录表新增) const DELIVERY_TEXT2_FIELD_ID = 'fldG6LZnmU'; - - // 待批量生成表的文本2字段 - const BATCH_TEXT2_FIELD_ID = 'fldlaYgpYO'; // 已移除中国法定节假日相关常量和配置 @@ -1824,6 +1823,7 @@ export default function App() { const labelsField = await deliveryRecordTable.getField(DELIVERY_LABELS_FIELD_ID); const styleField = await deliveryRecordTable.getField(DELIVERY_STYLE_FIELD_ID); const colorField = await deliveryRecordTable.getField(DELIVERY_COLOR_FIELD_ID); + const text2Field = await deliveryRecordTable.getField(DELIVERY_TEXT2_FIELD_ID); const createTimeField = await deliveryRecordTable.getField(DELIVERY_CREATE_TIME_FIELD_ID); const expectedDateField = await deliveryRecordTable.getField(DELIVERY_EXPECTED_DATE_FIELD_ID); const nodeDetailsField = await deliveryRecordTable.getField(DELIVERY_NODE_DETAILS_FIELD_ID); @@ -1840,9 +1840,14 @@ export default function App() { property: labelsField.property }); - // 获取foreign_id:优先使用选择记录,其次记录详情,最后快照回填 + // 获取foreign_id:调整模式严格使用快照数据,生成模式优先使用选择记录 let foreignId = ''; - if (selectedRecords.length > 0) { + if (mode === 'adjust') { + // 调整模式:严格使用快照回填的foreign_id,即使为空也不回退 + foreignId = currentForeignId; + console.log('调整模式:严格使用快照恢复的foreign_id:', foreignId); + } else if (selectedRecords.length > 0) { + // 生成模式:从选择记录获取 const table = await bitable.base.getTable(TABLE_ID); const firstRecord = await table.getRecordById(selectedRecords[0]); const fieldValue = firstRecord.fields['fldpvBfeC0']; @@ -1860,8 +1865,9 @@ export default function App() { foreignId = fieldValue.text; } } - // 回退:记录详情 - if (!foreignId && recordDetails.length > 0) { + + // 生成模式的回退逻辑:记录详情 + if (mode !== 'adjust' && !foreignId && recordDetails.length > 0) { const first = recordDetails[0]; const val = first.fields['fldpvBfeC0']; if (Array.isArray(val) && val.length > 0) { @@ -1873,12 +1879,12 @@ export default function App() { foreignId = val.text || val.name || ''; } } - // 回退:快照状态 - if (!foreignId && currentForeignId) { + // 生成模式的最后回退:快照状态 + if (mode !== 'adjust' && !foreignId && currentForeignId) { foreignId = currentForeignId; } - // 获取款式与颜色:优先使用记录详情或快照回填,避免重复请求 + // 获取款式与颜色:调整模式优先使用快照数据,生成模式优先使用记录详情 let style = ''; let color = ''; const extractText = (val: any) => { @@ -1892,23 +1898,44 @@ export default function App() { } return ''; }; - if (recordDetails.length > 0) { - const first = recordDetails[0]; - style = extractText(first.fields['fld6Uw95kt']) || currentStyleText || ''; - color = extractText(first.fields['flde85ni4O']) || currentColorText || ''; + + if (mode === 'adjust') { + // 调整模式:严格使用快照回填的数据,即使为空也不回退 + style = currentStyleText; + color = currentColorText; + console.log('调整模式:严格使用快照恢复的款式:', style, '颜色:', color); } else { - // 回退:使用快照回填的状态 - style = currentStyleText || ''; - color = currentColorText || ''; - // 若仍为空且有选择记录,仅做一次读取 - if ((!style || !color) && selectedRecords.length > 0) { - const table = await bitable.base.getTable(TABLE_ID); - const firstRecord = await table.getRecordById(selectedRecords[0]); - style = style || extractText(firstRecord.fields['fld6Uw95kt']); - color = color || extractText(firstRecord.fields['flde85ni4O']); + // 生成模式:优先使用记录详情 + if (recordDetails.length > 0) { + const first = recordDetails[0]; + style = extractText(first.fields['fld6Uw95kt']) || currentStyleText || ''; + color = extractText(first.fields['flde85ni4O']) || currentColorText || ''; + } else { + // 回退:使用快照回填的状态 + style = currentStyleText || ''; + color = currentColorText || ''; + // 若仍为空且有选择记录,仅做一次读取 + if ((!style || !color) && selectedRecords.length > 0) { + const table = await bitable.base.getTable(TABLE_ID); + const firstRecord = await table.getRecordById(selectedRecords[0]); + style = style || extractText(firstRecord.fields['fld6Uw95kt']); + color = color || extractText(firstRecord.fields['flde85ni4O']); + } } } + // 获取文本2:调整模式优先使用快照数据,生成模式不填写 + let text2 = ''; + if (mode === 'adjust') { + // 调整模式:严格使用快照回填的数据,即使为空也不回退 + text2 = currentText2; // 直接使用快照值,不使用 || '' 的回退逻辑 + console.log('调整模式:严格使用快照恢复的文本2:', text2); + } else { + // 生成模式:不填写文本2字段,保持为空 + text2 = ''; + console.log('生成模式:文本2字段保持为空'); + } + // 获取标签汇总(从业务选择的标签中获取) const selectedLabelValues = Object.values(selectedLabels).flat().filter(Boolean); @@ -1975,6 +2002,7 @@ export default function App() { const labelsCell = selectedLabelValues.length > 0 ? await labelsField.createCell(selectedLabelValues) : null; const styleCell = await styleField.createCell(style); const colorCell = await colorField.createCell(color); + const text2Cell = await text2Field.createCell(text2); const createTimeCell = await createTimeField.createCell(currentTime); const startTimestamp = startTime ? startTime.getTime() : currentTime; const startTimeCell = await startTimeField.createCell(startTimestamp); @@ -1989,7 +2017,7 @@ export default function App() { const versionCell = await versionField.createCell(versionNumber); // 组合所有Cell到一个记录中 - const recordCells = [foreignIdCell, styleCell, colorCell, createTimeCell, startTimeCell, versionCell]; + const recordCells = [foreignIdCell, styleCell, colorCell, text2Cell, createTimeCell, startTimeCell, versionCell]; // 只有当数据存在时才添加对应的Cell if (labelsCell) recordCells.push(labelsCell); @@ -1997,7 +2025,6 @@ export default function App() { if (customerExpectedDateCell) recordCells.push(customerExpectedDateCell); if (nodeDetailsCell) recordCells.push(nodeDetailsCell); if (adjustmentInfoCell) recordCells.push(adjustmentInfoCell); - if (text2Cell) recordCells.push(text2Cell); // 添加记录到货期记录表 const addedRecord = await deliveryRecordTable.addRecord(recordCells); @@ -2008,7 +2035,7 @@ export default function App() { error: error, message: error.message, stack: error.stack, - recordCells: recordCells.length + recordCellsLength: typeof recordCells !== 'undefined' ? recordCells.length : 'recordCells未定义' }); if (bitable.ui.showToast) { await bitable.ui.showToast({ @@ -2179,11 +2206,27 @@ export default function App() { if (!styleText && currentStyleText) styleText = currentStyleText; if (!colorText && currentColorText) colorText = currentColorText; + // 获取text2用于快照存档 + let text2 = currentText2 || ''; + if (!text2 && recordDetails.length > 0) { + const firstRecord = recordDetails[0]; + const text2FieldValue = firstRecord.fields['fldG6LZnmU']; + if (Array.isArray(text2FieldValue) && text2FieldValue.length > 0) { + const firstItem = text2FieldValue[0]; + text2 = typeof firstItem === 'string' ? firstItem : (firstItem?.text || firstItem?.name || ''); + } else if (typeof text2FieldValue === 'string') { + text2 = text2FieldValue; + } else if (text2FieldValue && typeof text2FieldValue === 'object') { + text2 = (text2FieldValue.text || text2FieldValue.name || ''); + } + } + const pageSnapshot = { version: versionNumber, foreignId, styleText, colorText, + text2, mode, selectedLabels, expectedDateTimestamp, @@ -2416,11 +2459,19 @@ export default function App() { } else if (fieldMeta.id === 'fldkKZecSv') { // foreign_id字段映射 fieldMapping['foreign_id'] = fieldMeta.id; + } else if (fieldMeta.name === '文本 2' || fieldMeta.name === '文本2' || fieldMeta.id === 'fldlaYgpYO') { + // 文本2字段映射 - 支持有空格和无空格的字段名称,以及直接ID匹配 + fieldMapping['文本2'] = fieldMeta.id; } } console.log('字段映射:', fieldMapping); console.log('选项映射:', optionMapping); + console.log('所有字段元数据:', fieldMetaList.map(f => ({ id: f.id, name: f.name }))); + + // 特别检查文本2字段 + const text2Fields = fieldMetaList.filter(f => f.name.includes('文本') || f.id === 'fldlaYgpYO'); + console.log('包含"文本"的字段或fldlaYgpYO:', text2Fields.map(f => ({ id: f.id, name: f.name }))); let successCount = 0; let errorCount = 0; @@ -2562,11 +2613,10 @@ export default function App() { } } - // 提取款式、颜色、foreign_id、文本2字段 + // 提取款式、颜色、foreign_id字段 let extractedStyle: any = null; let extractedColor: any = null; let extractedForeignId: any = null; - let extractedText2: string | null = null; // 提取款式字段 const styleFieldId = fieldMapping['款式']; @@ -2624,23 +2674,44 @@ export default function App() { extractedForeignId = [{ type: "text", text: foreignIdValue.text }]; } } - + // 提取文本2字段 - const text2FieldId = BATCH_TEXT2_FIELD_ID; + let extractedText2: any = null; + const text2FieldId = fieldMapping['文本2']; + console.log('文本2字段映射:', { fieldName: '文本2', fieldId: text2FieldId }); + console.log('记录字段keys:', Object.keys(recordFields)); + console.log('fldlaYgpYO字段值:', recordFields['fldlaYgpYO']); + if (text2FieldId && recordFields[text2FieldId]) { const text2Value = recordFields[text2FieldId]; - if (typeof text2Value === 'string') { + console.log('原始text2Value:', text2Value); + + if (Array.isArray(text2Value) && text2Value.length > 0) { + // 如果已经是正确的格式,直接使用 + if (text2Value[0] && text2Value[0].type === 'text' && text2Value[0].text) { + extractedText2 = text2Value[0].text; + } else { + // 处理其他数组格式 + const firstValue = text2Value[0]; + if (typeof firstValue === 'string') { + extractedText2 = firstValue; + } else if (firstValue && firstValue.text) { + extractedText2 = firstValue.text; + } + } + } else if (typeof text2Value === 'string') { extractedText2 = text2Value; } else if (text2Value && text2Value.text) { extractedText2 = text2Value.text; - } else if (Array.isArray(text2Value) && text2Value.length > 0) { - const firstValue = text2Value[0]; - if (typeof firstValue === 'string') { - extractedText2 = firstValue; - } else if (firstValue && firstValue.text) { - extractedText2 = firstValue.text; - } } + + console.log('提取后的extractedText2:', extractedText2); + } else { + console.log('文本2字段未找到或无数据:', { + fieldId: text2FieldId, + hasField: !!recordFields[text2FieldId], + fieldValue: recordFields[text2FieldId] + }); } console.log(`记录 ${i + 1} 提取的数据:`, { @@ -2716,7 +2787,7 @@ export default function App() { style?: any, color?: any, foreignId?: any, - text2Value?: string + text2?: any ) => { try { // 1. 获取流程配置表数据 @@ -2941,10 +3012,10 @@ export default function App() { // 6. 写入数据到货期记录表和流程数据表 if (results.length > 0) { // 写入流程数据表 - const processRecordIds = await writeToProcessDataTableForBatch(results, sourceRecordId, labels, style, color, foreignId); + const processRecordIds = await writeToProcessDataTableForBatch(results, sourceRecordId, labels, expectedDate, style, color, foreignId, text2); // 写入货期记录表 - await writeToDeliveryRecordTableForBatch(results, processRecordIds, {}, sourceRecordId, labels, startTime, expectedDate, style, color, foreignId, text2Value); + await writeToDeliveryRecordTableForBatch(results, processRecordIds, {}, sourceRecordId, labels, startTime, expectedDate, style, color, foreignId, text2); } } catch (error) { @@ -2958,9 +3029,11 @@ export default function App() { timelineResults: any[], sourceRecordId: string, labels: {[key: string]: string | string[]}, + expectedDate: Date | null, style?: any, color?: any, - foreignId?: any + foreignId?: any, + text2?: any ): Promise => { try { // 获取流程数据表 @@ -3005,11 +3078,32 @@ export default function App() { // 构建页面快照JSON const versionNumber = 1; // 批量处理默认版本号为1 + // 提取实际的文本值 + const extractTextValue = (value: any) => { + if (!value) return ''; + if (typeof value === 'string') return value; + if (Array.isArray(value) && value.length > 0) { + const firstItem = value[0]; + if (typeof firstItem === 'string') return firstItem; + if (firstItem && firstItem.text) return firstItem.text; + } + if (value.text) return value.text; + return ''; + }; + + const expectedDateTimestamp = expectedDate ? expectedDate.getTime() : null; + const expectedDateString = expectedDate ? format(expectedDate, DATE_FORMATS.STORAGE_FORMAT) : null; + const pageSnapshot = { version: versionNumber, - foreignId, + foreignId: extractTextValue(foreignId), + styleText: extractTextValue(style), + colorText: extractTextValue(color), + text2: extractTextValue(text2), mode: 'batch', selectedLabels: labels, + expectedDateTimestamp, + expectedDateString, timelineResults }; const snapshotJson = JSON.stringify(pageSnapshot); @@ -3117,7 +3211,7 @@ export default function App() { style?: any, color?: any, foreignId?: any, - text2Value?: string + text2?: any ) => { try { // 获取货期记录表 @@ -3128,6 +3222,7 @@ export default function App() { const labelsField = await deliveryRecordTable.getField(DELIVERY_LABELS_FIELD_ID); const styleField = await deliveryRecordTable.getField(DELIVERY_STYLE_FIELD_ID); const colorField = await deliveryRecordTable.getField(DELIVERY_COLOR_FIELD_ID); + const text2Field = await deliveryRecordTable.getField(DELIVERY_TEXT2_FIELD_ID); const createTimeField = await deliveryRecordTable.getField(DELIVERY_CREATE_TIME_FIELD_ID); const expectedDateField = await deliveryRecordTable.getField(DELIVERY_EXPECTED_DATE_FIELD_ID); const nodeDetailsField = await deliveryRecordTable.getField(DELIVERY_NODE_DETAILS_FIELD_ID); @@ -3135,7 +3230,6 @@ export default function App() { const adjustmentInfoField = await deliveryRecordTable.getField(DELIVERY_ADJUSTMENT_INFO_FIELD_ID); const versionField = await deliveryRecordTable.getField(DELIVERY_VERSION_FIELD_ID); const startTimeField = await deliveryRecordTable.getField(DELIVERY_START_TIME_FIELD_ID); - const text2Field = await deliveryRecordTable.getField(DELIVERY_TEXT2_FIELD_ID); // 使用传入的foreignId参数,如果没有则使用sourceRecordId作为foreign_id const actualForeignId = foreignId || sourceRecordId; @@ -3223,10 +3317,24 @@ export default function App() { adjustmentInfo += `\n当前调整:\n${adjustmentTexts.join('\n')}`; } + // 提取实际的文本值 + const extractTextValue = (value: any) => { + if (!value) return ''; + if (typeof value === 'string') return value; + if (Array.isArray(value) && value.length > 0) { + const firstItem = value[0]; + if (typeof firstItem === 'string') return firstItem; + if (firstItem && firstItem.text) return firstItem.text; + } + if (value.text) return value.text; + return ''; + }; + // 创建所有必要的Cell - const foreignIdCell = await foreignIdField.createCell(actualForeignId); - const styleCell = await styleField.createCell(style || ''); - const colorCell = await colorField.createCell(color || ''); + const foreignIdCell = await foreignIdField.createCell(extractTextValue(foreignId) || actualForeignId); + const styleCell = await styleField.createCell(extractTextValue(style)); + const colorCell = await colorField.createCell(extractTextValue(color)); + const text2Cell = await text2Field.createCell(extractTextValue(text2)); const createTimeCell = await createTimeField.createCell(currentTime); const startTimeCell = await startTimeField.createCell(startTime ? startTime.getTime() : currentTime); const versionCell = await versionField.createCell(versionNumber); @@ -3236,10 +3344,6 @@ export default function App() { const customerExpectedDateCell = customerExpectedDate ? await customerExpectedDateField.createCell(customerExpectedDate) : null; const nodeDetailsCell = processRecordIds.length > 0 ? await nodeDetailsField.createCell({ recordIds: processRecordIds }) : null; const adjustmentInfoCell = adjustmentInfo ? await adjustmentInfoField.createCell(adjustmentInfo) : null; - const text2Cell = text2Value && text2Value.trim() ? await text2Field.createCell(text2Value) : null; - - console.log('文本2字段值:', text2Value); - console.log('文本2字段Cell创建结果:', !!text2Cell); // 只有当数据存在时才添加对应的Cell console.log('uniqueLabelValues长度:', uniqueLabelValues.length); @@ -3255,7 +3359,7 @@ export default function App() { } // 组合所有Cell到一个记录中 - const recordCells = [foreignIdCell, styleCell, colorCell, createTimeCell, startTimeCell, versionCell]; + const recordCells = [foreignIdCell, styleCell, colorCell, text2Cell, createTimeCell, startTimeCell, versionCell]; // 只有当数据存在时才添加对应的Cell if (labelsCell) recordCells.push(labelsCell); @@ -3263,11 +3367,9 @@ export default function App() { if (customerExpectedDateCell) recordCells.push(customerExpectedDateCell); if (nodeDetailsCell) recordCells.push(nodeDetailsCell); if (adjustmentInfoCell) recordCells.push(adjustmentInfoCell); - if (text2Cell) recordCells.push(text2Cell); console.log('准备写入的RecordCells数量:', recordCells.length); console.log('标签汇总字段Cell是否存在:', !!labelsCell); - console.log('文本2字段Cell是否存在:', !!text2Cell); // 使用Cell数组格式添加记录(与正常模式相同) console.log('即将调用 deliveryRecordTable.addRecord...'); @@ -3281,7 +3383,6 @@ export default function App() { const writtenRecord = await deliveryRecordTable.getRecordById(addedRecord); console.log('写入后的记录验证:', writtenRecord); console.log('写入后的标签汇总字段:', writtenRecord.fields[DELIVERY_LABELS_FIELD_ID]); - console.log('写入后的文本2字段:', writtenRecord.fields[DELIVERY_TEXT2_FIELD_ID]); // 检查标签汇总字段是否为空 const labelsFieldValue = writtenRecord.fields[DELIVERY_LABELS_FIELD_ID]; @@ -3292,18 +3393,6 @@ export default function App() { } else { console.log('标签汇总字段写入成功,包含', Array.isArray(labelsFieldValue) ? labelsFieldValue.length : 1, '个值'); } - - // 检查文本2字段是否正确写入 - const text2FieldValue = writtenRecord.fields[DELIVERY_TEXT2_FIELD_ID]; - if (text2Value && text2Value.trim()) { - if (!text2FieldValue) { - console.error('警告:文本2字段写入后为空!'); - console.error('原始数据:', text2Value); - console.error('字段ID:', DELIVERY_TEXT2_FIELD_ID); - } else { - console.log('文本2字段写入成功,值为:', text2FieldValue); - } - } } catch (verifyError) { console.error('验证写入记录失败:', verifyError); }