增加版本

增加版本
This commit is contained in:
2025-10-24 09:27:39 +08:00
parent a0e3e9a3f9
commit e3af2107fa

View File

@ -1,7 +1,7 @@
import { bitable, FieldType } from '@lark-base-open/js-sdk'; import { bitable, FieldType } from '@lark-base-open/js-sdk';
import { Button, Typography, List, Card, Space, Divider, Spin, Table, Select, Modal, DatePicker } from '@douyinfe/semi-ui'; import { Button, Typography, List, Card, Space, Divider, Spin, Table, Select, Modal, DatePicker } from '@douyinfe/semi-ui';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { addDays, format, isWeekend } from 'date-fns'; import { addDays, format } from 'date-fns';
import { zhCN } from 'date-fns/locale'; import { zhCN } from 'date-fns/locale';
import { executePricingQuery, executeSecondaryProcessQuery, executePricingDetailsQuery } from './services/apiService'; import { executePricingQuery, executeSecondaryProcessQuery, executePricingDetailsQuery } from './services/apiService';
@ -37,14 +37,23 @@ export default function App() {
// 客户期望日期状态 // 客户期望日期状态
const [expectedDate, setExpectedDate] = useState<Date | null>(null); const [expectedDate, setExpectedDate] = useState<Date | null>(null);
// 预览相关状态 // 预览相关状态(已移除未使用的 previewLoading 状态)
const [previewLoading, setPreviewLoading] = useState(false);
// 时效计算相关状态 // 时效计算相关状态
const [timelineVisible, setTimelineVisible] = useState(false); const [timelineVisible, setTimelineVisible] = useState(false);
const [timelineLoading, setTimelineLoading] = useState(false); const [timelineLoading, setTimelineLoading] = useState(false);
const [timelineResults, setTimelineResults] = useState<any[]>([]); const [timelineResults, setTimelineResults] = useState<any[]>([]);
const [timelineAdjustments, setTimelineAdjustments] = useState<{[key: number]: number}>({}); const [timelineAdjustments, setTimelineAdjustments] = useState<{[key: number]: number}>({});
// 快照回填来源foreign_id、款式、颜色
const [currentForeignId, setCurrentForeignId] = useState<string | null>(null);
const [currentStyleText, setCurrentStyleText] = useState<string>('');
const [currentColorText, setCurrentColorText] = useState<string>('');
// 功能入口模式与调整相关状态
const [mode, setMode] = useState<'generate' | 'adjust' | null>(null);
const [modeSelectionVisible, setModeSelectionVisible] = useState(true);
const [adjustLoading, setAdjustLoading] = useState(false);
// 删除未使用的 deliveryRecords 状态
const [selectedDeliveryRecordId, setSelectedDeliveryRecordId] = useState<string>('');
// 指定的数据表ID和视图ID // 指定的数据表ID和视图ID
const TABLE_ID = 'tblPIJ7unndydSMu'; const TABLE_ID = 'tblPIJ7unndydSMu';
@ -68,6 +77,148 @@ export default function App() {
const TIMELINE_NODE_FIELD_ID = 'fldeIZzokl'; // 时效表中的节点名称字段ID const TIMELINE_NODE_FIELD_ID = 'fldeIZzokl'; // 时效表中的节点名称字段ID
const CALCULATION_METHOD_FIELD_ID = 'fldxfLZNUu'; // 时效计算方式字段ID const CALCULATION_METHOD_FIELD_ID = 'fldxfLZNUu'; // 时效计算方式字段ID
// 已移除:调整模式不再加载货期记录列表
// 入口选择处理
const chooseMode = (m: 'generate' | 'adjust') => {
setMode(m);
setModeSelectionVisible(false);
};
// 根据货期记录ID读取节点详情并还原流程数据
const loadProcessDataFromDeliveryRecord = async (deliveryRecordId: string) => {
if (!deliveryRecordId) {
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'warning', message: '请先选择一条货期记录' });
}
return;
}
setTimelineLoading(true);
try {
const deliveryTable = await bitable.base.getTable(DELIVERY_RECORD_TABLE_ID);
const deliveryRecord = await deliveryTable.getRecordById(deliveryRecordId);
const nodeDetailsVal = deliveryRecord?.fields?.[DELIVERY_NODE_DETAILS_FIELD_ID];
let recordIds: string[] = [];
if (nodeDetailsVal && typeof nodeDetailsVal === 'object' && (nodeDetailsVal as any).recordIds) {
recordIds = (nodeDetailsVal as any).recordIds as string[];
} else if (Array.isArray(nodeDetailsVal)) {
recordIds = nodeDetailsVal.map((item: any) => item?.recordId || item?.id || item).filter(Boolean);
}
if (!recordIds || recordIds.length === 0) {
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'warning', message: '该货期记录未包含节点详情或为空' });
}
setTimelineLoading(false);
return;
}
const processTable = await bitable.base.getTable(PROCESS_DATA_TABLE_ID);
const records = await Promise.all(recordIds.map(id => processTable.getRecordById(id)));
// 优先使用文本2快照一模一样还原
try {
// 在所有记录中查找非空快照
let snapStr: string | null = null;
for (const rec of records) {
const snapVal = rec?.fields?.[PROCESS_SNAPSHOT_JSON_FIELD_ID];
let candidate: string | null = null;
if (typeof snapVal === 'string') {
candidate = snapVal;
} else if (Array.isArray(snapVal)) {
// 文本结构拼接所有text片段
const texts = snapVal
.filter((el: any) => el && el.type === 'text' && typeof el.text === 'string')
.map((el: any) => el.text);
candidate = texts.length > 0 ? texts.join('') : null;
} else if (snapVal && typeof snapVal === 'object') {
// 兼容 {text: '...'} 或 {type:'text', text:'...'}
if ((snapVal as any).text && typeof (snapVal as any).text === 'string') {
candidate = (snapVal as any).text;
} else if ((snapVal as any).type === 'text' && typeof (snapVal as any).text === 'string') {
candidate = (snapVal as any).text;
}
}
if (candidate && candidate.trim() !== '') {
snapStr = candidate;
break;
}
}
if (snapStr && snapStr.trim() !== '') {
const snapshot = JSON.parse(snapStr);
// 恢复页面状态
if (snapshot.selectedLabels) setSelectedLabels(snapshot.selectedLabels);
if (snapshot.mode) setMode(snapshot.mode);
// 快照回填的foreign_id/款式/颜色
if (snapshot.foreignId) setCurrentForeignId(snapshot.foreignId);
if (snapshot.styleText) setCurrentStyleText(snapshot.styleText);
if (snapshot.colorText) setCurrentColorText(snapshot.colorText);
if (snapshot.timelineAdjustments) setTimelineAdjustments(snapshot.timelineAdjustments);
if (snapshot.expectedDateTimestamp) {
setExpectedDate(new Date(snapshot.expectedDateTimestamp));
} else if (snapshot.expectedDateString) {
setExpectedDate(new Date(snapshot.expectedDateString));
}
if (Array.isArray(snapshot.timelineResults)) {
setTimelineResults(snapshot.timelineResults);
setTimelineVisible(true);
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'success', message: '已按快照一模一样还原流程数据' });
}
setTimelineLoading(false);
return; // 快照还原完成,退出函数
}
}
} catch (snapError) {
console.warn('解析快照失败,降级为基于字段的还原:', snapError);
}
const results = records.map((rec: any) => {
const fields = rec?.fields || {};
const processOrder = fields[PROCESS_ORDER_FIELD_ID_DATA];
const nodeName = fields[PROCESS_NAME_FIELD_ID];
const startTs = fields[ESTIMATED_START_DATE_FIELD_ID];
const endTs = fields[ESTIMATED_END_DATE_FIELD_ID];
const startDate = typeof startTs === 'number' ? new Date(startTs) : (startTs ? new Date(startTs) : null);
const endDate = typeof endTs === 'number' ? new Date(endTs) : (endTs ? new Date(endTs) : null);
return {
processOrder: typeof processOrder === 'number' ? processOrder : parseInt(processOrder) || undefined,
nodeName: typeof nodeName === 'string' ? nodeName : (nodeName?.text || ''),
estimatedStart: startDate ? format(startDate, DATE_FORMATS.STORAGE_FORMAT) : '未找到时效数据',
estimatedEnd: endDate ? format(endDate, DATE_FORMATS.STORAGE_FORMAT) : '未找到时效数据',
timelineRecordId: rec?.id || rec?.recordId || null,
timelineValue: undefined,
calculationMethod: undefined,
weekendDaysConfig: [],
matchedLabels: [],
skippedWeekends: 0,
skippedHolidays: 0,
actualDays: undefined,
startDateRule: undefined,
dateAdjustmentRule: undefined,
ruleDescription: undefined,
adjustmentDescription: undefined
};
});
const sorted = results.sort((a, b) => (a.processOrder || 0) - (b.processOrder || 0));
setTimelineResults(sorted);
setTimelineVisible(true);
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'success', message: '已从货期记录还原流程数据' });
}
} catch (error) {
console.error('从货期记录还原流程数据失败:', error);
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'error', message: `还原失败: ${(error as Error).message}` });
}
} finally {
setTimelineLoading(false);
}
};
// 流程数据表相关常量 // 流程数据表相关常量
const PROCESS_DATA_TABLE_ID = 'tblsJzCxXClj5oK5'; // 流程数据表ID const PROCESS_DATA_TABLE_ID = 'tblsJzCxXClj5oK5'; // 流程数据表ID
const FOREIGN_ID_FIELD_ID = 'fld3oxJmpr'; // foreign_id字段 const FOREIGN_ID_FIELD_ID = 'fld3oxJmpr'; // foreign_id字段
@ -75,6 +226,9 @@ export default function App() {
const PROCESS_ORDER_FIELD_ID_DATA = 'fldmND6vjT'; // 流程顺序字段 const PROCESS_ORDER_FIELD_ID_DATA = 'fldmND6vjT'; // 流程顺序字段
const ESTIMATED_START_DATE_FIELD_ID = 'fldlzvHjYP'; // 预计开始日期字段 const ESTIMATED_START_DATE_FIELD_ID = 'fldlzvHjYP'; // 预计开始日期字段
const ESTIMATED_END_DATE_FIELD_ID = 'fldaPtY7Jk'; // 预计完成日期字段 const ESTIMATED_END_DATE_FIELD_ID = 'fldaPtY7Jk'; // 预计完成日期字段
const PROCESS_SNAPSHOT_JSON_FIELD_ID = 'fldSHTxfnC'; // 文本2用于保存计算页面快照(JSON)
const PROCESS_VERSION_FIELD_ID = 'fldwk5X7Yw'; // 版本字段
const PROCESS_TIMELINESS_FIELD_ID = 'fldEYCXnWt'; // 时效字段(天)
// 货期记录表相关常量 // 货期记录表相关常量
const DELIVERY_RECORD_TABLE_ID = 'tblwiA49gksQrnfg'; // 货期记录表ID const DELIVERY_RECORD_TABLE_ID = 'tblwiA49gksQrnfg'; // 货期记录表ID
@ -87,6 +241,7 @@ export default function App() {
const DELIVERY_NODE_DETAILS_FIELD_ID = 'fldu1KL9yC'; // 节点详情字段需要替换为实际字段ID const DELIVERY_NODE_DETAILS_FIELD_ID = 'fldu1KL9yC'; // 节点详情字段需要替换为实际字段ID
const DELIVERY_CUSTOMER_EXPECTED_DATE_FIELD_ID = 'fldYNluU8D'; // 客户期望日期字段 const DELIVERY_CUSTOMER_EXPECTED_DATE_FIELD_ID = 'fldYNluU8D'; // 客户期望日期字段
const DELIVERY_ADJUSTMENT_INFO_FIELD_ID = 'fldNc6nNsz'; // 货期调整信息字段需要替换为实际字段ID const DELIVERY_ADJUSTMENT_INFO_FIELD_ID = 'fldNc6nNsz'; // 货期调整信息字段需要替换为实际字段ID
const DELIVERY_VERSION_FIELD_ID = 'fld5OmvZrn'; // 版本字段(新增)
// 中国法定节假日配置需要手动维护或使用API // 中国法定节假日配置需要手动维护或使用API
const CHINESE_HOLIDAYS = [ const CHINESE_HOLIDAYS = [
@ -100,19 +255,7 @@ export default function App() {
// ... 其他节假日 // ... 其他节假日
]; ];
// 获取节假日数据的函数 // 已移除未使用的 fetchHolidays 函数
const fetchHolidays = async (year: number): Promise<string[]> => {
try {
// 使用免费的节假日API
const response = await fetch(`https://timor.tech/api/holiday/year/${year}`);
const data = await response.json();
return Object.keys(data.holiday || {});
} catch (error) {
console.error('获取节假日数据失败:', error);
return CHINESE_HOLIDAYS; // fallback到本地配置
}
};
// 这个变量声明也不需要了 // 这个变量声明也不需要了
// const nodeNameToOptionId = new Map(); // const nodeNameToOptionId = new Map();
@ -674,8 +817,7 @@ export default function App() {
} }
} }
console.log('客户期望日期:', expectedDate ? format(expectedDate, 'yyyy-MM-dd') : '未选择'); // 移除冗余日志:客户期望日期输出
setTimelineLoading(true); setTimelineLoading(true);
try { try {
// 构建业务选择的所有标签值集合(用于快速查找) // 构建业务选择的所有标签值集合(用于快速查找)
@ -691,9 +833,7 @@ export default function App() {
} }
} }
console.log('业务选择的所有标签值:', Array.from(businessLabelValues)); // 已移除冗余日志:业务选择标签值
// 优化1预先获取时效数据表的字段映射避免重复获取
const timelineTable = await bitable.base.getTable(TIMELINE_TABLE_ID); const timelineTable = await bitable.base.getTable(TIMELINE_TABLE_ID);
const timelineFieldMetaList = await timelineTable.getFieldMetaList(); const timelineFieldMetaList = await timelineTable.getFieldMetaList();
const timelineLabelFields: {[key: string]: string} = {}; const timelineLabelFields: {[key: string]: string} = {};
@ -1446,36 +1586,12 @@ export default function App() {
recalculateTimeline({}); recalculateTimeline({});
}; };
// 获取时效表中标签字段ID的辅助函数 // 已移除未使用的 getTimelineLabelFieldId 辅助函数
const getTimelineLabelFieldId = async (timelineTable: any, labelKey: string) => {
try {
const fieldMetaList = await timelineTable.getFieldMetaList();
for (const fieldMeta of fieldMetaList) {
if (fieldMeta.name === labelKey) {
return fieldMeta.id;
}
}
} catch (error) {
console.warn(`获取时效表${labelKey}字段ID失败:`, error);
}
return null;
};
// 写入货期记录表的函数 // 写入货期记录表的函数
const writeToDeliveryRecordTable = async (timelineResults: any[], processRecordIds: string[], timelineAdjustments: {[key: number]: number} = {}) => { const writeToDeliveryRecordTable = async (timelineResults: any[], processRecordIds: string[], timelineAdjustments: {[key: number]: number} = {}) => {
try { try {
console.log('开始写入货期记录表'); // 写入货期记录表
console.log('货期记录表ID:', DELIVERY_RECORD_TABLE_ID);
console.log('各字段ID:', {
DELIVERY_FOREIGN_ID_FIELD_ID,
DELIVERY_LABELS_FIELD_ID,
DELIVERY_STYLE_FIELD_ID,
DELIVERY_COLOR_FIELD_ID,
DELIVERY_CREATE_TIME_FIELD_ID,
DELIVERY_EXPECTED_DATE_FIELD_ID,
DELIVERY_NODE_DETAILS_FIELD_ID,
DELIVERY_CUSTOMER_EXPECTED_DATE_FIELD_ID
});
// 获取货期记录表 // 获取货期记录表
const deliveryRecordTable = await bitable.base.getTable(DELIVERY_RECORD_TABLE_ID); const deliveryRecordTable = await bitable.base.getTable(DELIVERY_RECORD_TABLE_ID);
@ -1503,8 +1619,9 @@ export default function App() {
const nodeDetailsField = await deliveryRecordTable.getField(DELIVERY_NODE_DETAILS_FIELD_ID); const nodeDetailsField = await deliveryRecordTable.getField(DELIVERY_NODE_DETAILS_FIELD_ID);
const customerExpectedDateField = await deliveryRecordTable.getField(DELIVERY_CUSTOMER_EXPECTED_DATE_FIELD_ID); const customerExpectedDateField = await deliveryRecordTable.getField(DELIVERY_CUSTOMER_EXPECTED_DATE_FIELD_ID);
const adjustmentInfoField = await deliveryRecordTable.getField(DELIVERY_ADJUSTMENT_INFO_FIELD_ID); const adjustmentInfoField = await deliveryRecordTable.getField(DELIVERY_ADJUSTMENT_INFO_FIELD_ID);
const versionField = await deliveryRecordTable.getField(DELIVERY_VERSION_FIELD_ID);
// 获取foreign_id从选择的记录中获取fldpvBfeC0字段 // 获取foreign_id:优先使用选择记录,其次记录详情,最后快照回填
let foreignId = ''; let foreignId = '';
if (selectedRecords.length > 0) { if (selectedRecords.length > 0) {
const table = await bitable.base.getTable(TABLE_ID); const table = await bitable.base.getTable(TABLE_ID);
@ -1524,36 +1641,52 @@ export default function App() {
foreignId = fieldValue.text; foreignId = fieldValue.text;
} }
} }
// 回退:记录详情
// 获取款式从选择记录中获取fld6Uw95kt if (!foreignId && recordDetails.length > 0) {
let style = ''; const first = recordDetails[0];
if (selectedRecords.length > 0) { const val = first.fields['fldpvBfeC0'];
const table = await bitable.base.getTable(TABLE_ID); if (Array.isArray(val) && val.length > 0) {
const firstRecord = await table.getRecordById(selectedRecords[0]); const item = val[0];
const styleValue = firstRecord.fields['fld6Uw95kt']; foreignId = typeof item === 'string' ? item : (item?.text || item?.name || '');
} else if (typeof val === 'string') {
if (Array.isArray(styleValue) && styleValue.length > 0) { foreignId = val;
style = styleValue[0]?.text || styleValue[0] || ''; } else if (val && typeof val === 'object') {
} else if (typeof styleValue === 'string') { foreignId = val.text || val.name || '';
style = styleValue;
} else if (styleValue && styleValue.text) {
style = styleValue.text;
} }
} }
// 回退:快照状态
if (!foreignId && currentForeignId) {
foreignId = currentForeignId;
}
// 获取颜色从选择记录中获取flde85ni4O // 获取款式与颜色:优先使用记录详情或快照回填,避免重复请求
let style = '';
let color = ''; let color = '';
if (selectedRecords.length > 0) { const extractText = (val: any) => {
const table = await bitable.base.getTable(TABLE_ID); if (Array.isArray(val) && val.length > 0) {
const firstRecord = await table.getRecordById(selectedRecords[0]); const item = val[0];
const colorValue = firstRecord.fields['flde85ni4O']; return typeof item === 'string' ? item : (item?.text || item?.name || '');
} else if (typeof val === 'string') {
if (Array.isArray(colorValue) && colorValue.length > 0) { return val;
color = colorValue[0]?.text || colorValue[0] || ''; } else if (val && typeof val === 'object') {
} else if (typeof colorValue === 'string') { return val.text || val.name || '';
color = colorValue; }
} else if (colorValue && colorValue.text) { return '';
color = colorValue.text; };
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']);
} }
} }
@ -1582,28 +1715,38 @@ export default function App() {
// 创建当前时间戳 // 创建当前时间戳
const currentTime = new Date().getTime(); const currentTime = new Date().getTime();
// 格式化货期调整信息 // 计算版本号(数字)并格式化货期调整信息
let adjustmentInfo = ''; let versionNumber = 1;
try {
if (foreignId) {
const existing = await deliveryRecordTable.getRecords({
pageSize: 5000,
filter: {
conjunction: 'and',
conditions: [{
fieldId: DELIVERY_FOREIGN_ID_FIELD_ID,
operator: 'is',
value: [foreignId]
}]
}
});
const count = existing.records?.length || 0;
versionNumber = count + 1;
}
} catch (e) {
console.warn('计算版本号失败:', e);
}
let adjustmentInfo = `版本V${versionNumber}`;
if (Object.keys(timelineAdjustments).length > 0) { if (Object.keys(timelineAdjustments).length > 0) {
const adjustmentTexts = Object.entries(timelineAdjustments).map(([nodeIndex, adjustment]) => { const adjustmentTexts = Object.entries(timelineAdjustments).map(([nodeIndex, adjustment]) => {
const nodeName = timelineResults[parseInt(nodeIndex)]?.nodeName || `节点${parseInt(nodeIndex) + 1}`; const nodeName = timelineResults[parseInt(nodeIndex)]?.nodeName || `节点${parseInt(nodeIndex) + 1}`;
return `${nodeName}: ${adjustment > 0 ? '+' : ''}${adjustment.toFixed(1)}`; return `${nodeName}: ${adjustment > 0 ? '+' : ''}${adjustment.toFixed(1)}`;
}); });
adjustmentInfo = `当前调整:\n${adjustmentTexts.join('\n')}`; adjustmentInfo += `\n当前调整:\n${adjustmentTexts.join('\n')}`;
} }
// 在创建Cell之前添加数据验证 // 在创建Cell之前进行数据校验移除冗余日志
console.log('准备写入的数据:', {
foreignId,
selectedLabelValues,
style,
color,
currentTime,
expectedDeliveryDate,
customerExpectedDate,
processRecordIds,
adjustmentInfo
});
// 使用createCell方法创建各个字段的Cell // 使用createCell方法创建各个字段的Cell
const foreignIdCell = await foreignIdField.createCell(foreignId); const foreignIdCell = await foreignIdField.createCell(foreignId);
@ -1618,9 +1761,11 @@ export default function App() {
await nodeDetailsField.createCell({ recordIds: processRecordIds }) : null; await nodeDetailsField.createCell({ recordIds: processRecordIds }) : null;
// 创建货期调整信息Cell // 创建货期调整信息Cell
const adjustmentInfoCell = adjustmentInfo ? await adjustmentInfoField.createCell(adjustmentInfo) : null; const adjustmentInfoCell = adjustmentInfo ? await adjustmentInfoField.createCell(adjustmentInfo) : null;
// 创建版本号Cell数字
const versionCell = await versionField.createCell(versionNumber);
// 组合所有Cell到一个记录中 // 组合所有Cell到一个记录中
const recordCells = [foreignIdCell, styleCell, colorCell, createTimeCell]; const recordCells = [foreignIdCell, styleCell, colorCell, createTimeCell, versionCell];
// 只有当数据存在时才添加对应的Cell // 只有当数据存在时才添加对应的Cell
if (labelsCell) recordCells.push(labelsCell); if (labelsCell) recordCells.push(labelsCell);
@ -1630,14 +1775,7 @@ export default function App() {
if (adjustmentInfoCell) recordCells.push(adjustmentInfoCell); if (adjustmentInfoCell) recordCells.push(adjustmentInfoCell);
// 添加记录到货期记录表 // 添加记录到货期记录表
console.log('准备添加记录Cell数量:', recordCells.length);
console.log('recordCells详情:', recordCells.map(cell => ({
fieldId: cell.fieldId,
value: cell.value
})));
const addedRecord = await deliveryRecordTable.addRecord(recordCells); const addedRecord = await deliveryRecordTable.addRecord(recordCells);
console.log('成功写入货期记录表记录ID:', addedRecord);
return addedRecord; return addedRecord;
} catch (error) { } catch (error) {
@ -1673,6 +1811,9 @@ export default function App() {
const processOrderField = await processDataTable.getField(PROCESS_ORDER_FIELD_ID_DATA); const processOrderField = await processDataTable.getField(PROCESS_ORDER_FIELD_ID_DATA);
const startDateField = await processDataTable.getField(ESTIMATED_START_DATE_FIELD_ID); const startDateField = await processDataTable.getField(ESTIMATED_START_DATE_FIELD_ID);
const endDateField = await processDataTable.getField(ESTIMATED_END_DATE_FIELD_ID); const endDateField = await processDataTable.getField(ESTIMATED_END_DATE_FIELD_ID);
const snapshotField = await processDataTable.getField(PROCESS_SNAPSHOT_JSON_FIELD_ID);
const versionField = await processDataTable.getField(PROCESS_VERSION_FIELD_ID);
const timelinessField = await processDataTable.getField(PROCESS_TIMELINESS_FIELD_ID);
// 获取foreign_id - 修改这部分逻辑 // 获取foreign_id - 修改这部分逻辑
let foreignId = null; let foreignId = null;
@ -1706,6 +1847,12 @@ export default function App() {
} }
} }
// 快照回填在调整模式通过快照还原时使用当前foreign_id状态
if (!foreignId && currentForeignId) {
foreignId = currentForeignId;
console.log('使用快照恢复的foreign_id:', foreignId);
}
if (!foreignId) { if (!foreignId) {
console.warn('未找到foreign_id跳过写入流程数据表'); console.warn('未找到foreign_id跳过写入流程数据表');
if (bitable.ui.showToast) { if (bitable.ui.showToast) {
@ -1748,6 +1895,85 @@ export default function App() {
// 继续执行,不中断流程 // 继续执行,不中断流程
} }
// 构建页面快照JSON确保可一模一样还原
// 计算版本号:数字。默认按 foreign_id 在货期记录表的记录数量 + 1
let versionNumber = 1;
try {
const deliveryRecordTable = await bitable.base.getTable(DELIVERY_RECORD_TABLE_ID);
if (mode === 'adjust' && selectedDeliveryRecordId) {
// 调整模式:基于选中货期记录版本 +1
const srcRecord = await deliveryRecordTable.getRecordById(selectedDeliveryRecordId);
const vVal = srcRecord?.fields?.[DELIVERY_VERSION_FIELD_ID];
let baseVersion = 0;
if (typeof vVal === 'number') baseVersion = vVal;
else if (typeof vVal === 'string') baseVersion = parseInt(vVal) || 0;
else if (vVal && typeof vVal === 'object' && vVal.value !== undefined) baseVersion = vVal.value || 0;
versionNumber = baseVersion + 1;
} else if (foreignId) {
const existing = await deliveryRecordTable.getRecords({
pageSize: 5000,
filter: {
conjunction: 'and',
conditions: [{
fieldId: DELIVERY_FOREIGN_ID_FIELD_ID,
operator: 'is',
value: [foreignId]
}]
}
});
const count = existing.records?.length || 0;
versionNumber = count + 1;
}
} catch (e) {
console.warn('计算版本号失败:', e);
}
const expectedDateTimestamp = expectedDate ? expectedDate.getTime() : null;
const expectedDateString = expectedDate ? format(expectedDate, DATE_FORMATS.STORAGE_FORMAT) : null;
// 从记录详情提取款式和颜色,用于快照存档(回填写入来源)
let styleText = '';
let colorText = '';
if (recordDetails.length > 0) {
const firstRecord = recordDetails[0];
const styleFieldValue = firstRecord.fields['fld6Uw95kt'];
if (Array.isArray(styleFieldValue) && styleFieldValue.length > 0) {
const firstItem = styleFieldValue[0];
styleText = typeof firstItem === 'string' ? firstItem : (firstItem?.text || firstItem?.name || '');
} else if (typeof styleFieldValue === 'string') {
styleText = styleFieldValue;
} else if (styleFieldValue && typeof styleFieldValue === 'object') {
styleText = (styleFieldValue.text || styleFieldValue.name || '');
}
const colorFieldValue = firstRecord.fields['flde85ni4O'];
if (Array.isArray(colorFieldValue) && colorFieldValue.length > 0) {
const firstItemC = colorFieldValue[0];
colorText = typeof firstItemC === 'string' ? firstItemC : (firstItemC?.text || firstItemC?.name || '');
} else if (typeof colorFieldValue === 'string') {
colorText = colorFieldValue;
} else if (colorFieldValue && typeof colorFieldValue === 'object') {
colorText = (colorFieldValue.text || colorFieldValue.name || '');
}
}
// 快照回填的备用值
if (!styleText && currentStyleText) styleText = currentStyleText;
if (!colorText && currentColorText) colorText = currentColorText;
const pageSnapshot = {
version: versionNumber,
foreignId,
styleText,
colorText,
mode,
selectedLabels,
expectedDateTimestamp,
expectedDateString,
timelineAdjustments,
timelineResults
};
const snapshotJson = JSON.stringify(pageSnapshot);
// 使用createCell方法准备要写入的记录数据 // 使用createCell方法准备要写入的记录数据
const recordCellsToAdd = []; const recordCellsToAdd = [];
@ -1796,15 +2022,23 @@ export default function App() {
const endDateCell = endTimestamp ? await endDateField.createCell(endTimestamp) : null; const endDateCell = endTimestamp ? await endDateField.createCell(endTimestamp) : null;
// 组合所有Cell到一个记录中 // 组合所有Cell到一个记录中
const snapshotCell = await snapshotField.createCell(snapshotJson);
const versionCell = await versionField.createCell(versionNumber);
const timelinessCell = (typeof result.timelineValue === 'number')
? await timelinessField.createCell(result.timelineValue)
: null;
const recordCells = [ const recordCells = [
foreignIdCell, foreignIdCell,
processNameCell, processNameCell,
processOrderCell processOrderCell,
snapshotCell,
versionCell
]; ];
// 只有当时间戳存在时才添加日期Cell // 只有当时间戳存在时才添加日期Cell
if (startDateCell) recordCells.push(startDateCell); if (startDateCell) recordCells.push(startDateCell);
if (endDateCell) recordCells.push(endDateCell); if (endDateCell) recordCells.push(endDateCell);
if (timelinessCell) recordCells.push(timelinessCell);
console.log(`准备写入的Cell记录 - ${result.nodeName}:`, recordCells); console.log(`准备写入的Cell记录 - ${result.nodeName}:`, recordCells);
recordCellsToAdd.push(recordCells); recordCellsToAdd.push(recordCells);
@ -2064,6 +2298,7 @@ export default function App() {
// 获取记录详情的函数 // 获取记录详情的函数
const fetchRecordDetails = async (recordIdList: string[]) => { const fetchRecordDetails = async (recordIdList: string[]) => {
try { try {
const table = await bitable.base.getTable(TABLE_ID); const table = await bitable.base.getTable(TABLE_ID);
@ -2091,6 +2326,7 @@ export default function App() {
// 选择记录 // 选择记录
const handleSelectRecords = async () => { const handleSelectRecords = async () => {
setLoading(true); setLoading(true);
// 清空标签选择 // 清空标签选择
setSelectedLabels({}); setSelectedLabels({});
@ -2374,7 +2610,92 @@ export default function App() {
return ( return (
<div style={{ padding: '20px' }}> <div style={{ padding: '20px' }}>
<Title heading={2}></Title> {/* 入口选择弹窗 */}
<Modal
title="请选择功能入口"
visible={modeSelectionVisible}
footer={null}
onCancel={() => setModeSelectionVisible(false)}
maskClosable={false}
>
<div style={{ display: 'flex', gap: 24, justifyContent: 'center', padding: '12px 8px 8px', flexWrap: 'wrap' }}>
<Card
style={{
width: 280,
cursor: 'pointer',
borderRadius: 12,
boxShadow: '0 8px 24px rgba(0,0,0,0.06)',
border: '1px solid #e5e7eb',
background: 'linear-gradient(180deg, #fff, #f9fbff)'
}}
onClick={() => chooseMode('generate')}
>
<Title heading={3} style={{ marginBottom: 8 }}></Title>
<Text type='tertiary'>线</Text>
<div style={{ marginTop: 16 }}>
<Button type='primary' theme='solid' size='large' style={{ width: '100%' }} onClick={() => chooseMode('generate')}></Button>
</div>
</Card>
<Card
style={{
width: 280,
cursor: 'pointer',
borderRadius: 12,
boxShadow: '0 8px 24px rgba(0,0,0,0.06)',
border: '1px solid #e5e7eb',
background: 'linear-gradient(180deg, #fff, #f9fbff)'
}}
onClick={() => chooseMode('adjust')}
>
<Title heading={3} style={{ marginBottom: 8 }}></Title>
<Text type='tertiary'>线</Text>
<div style={{ marginTop: 16 }}>
<Button type='primary' theme='solid' size='large' style={{ width: '100%' }} onClick={() => chooseMode('adjust')}></Button>
</div>
</Card>
</div>
</Modal>
{mode === 'generate' && <Title heading={2}></Title>}
{/* 功能入口切换与调整入口 */}
{mode !== null && (
<div style={{ margin: '18px 0 14px' }}>
<Space spacing={16} align='center'>
<Select value={mode} onChange={(v) => setMode(v as any)}
optionList={[{ value: 'generate', label: '生成流程日期' }, { value: 'adjust', label: '调整流程日期' }]} />
{mode === 'adjust' && (
<Space spacing={12} align='center'>
<Button type="primary" onClick={async () => {
try {
const selection = await bitable.base.getSelection();
const recordId = selection?.recordId || '';
const tableId = selection?.tableId || '';
if (!recordId) {
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'warning', message: '请先在数据表中选中一条货期记录' });
}
return;
}
if (tableId && tableId !== DELIVERY_RECORD_TABLE_ID) {
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'warning', message: '请在货期记录表中选择记录' });
}
return;
}
setSelectedDeliveryRecordId(recordId);
await loadProcessDataFromDeliveryRecord(recordId);
} catch (e) {
console.error('读取当前选中记录失败:', e);
if (bitable.ui.showToast) {
await bitable.ui.showToast({ toastType: 'error', message: '读取当前选中记录失败' });
}
}
}}></Button>
</Space>
)}
</Space>
</div>
)}
{/* 时效计算结果模态框 */} {/* 时效计算结果模态框 */}
<Modal <Modal
@ -2656,8 +2977,8 @@ export default function App() {
{/* 标签选择部分 */} {/* 标签选择部分,仅在生成模式显示 */}
{labelOptions && Object.keys(labelOptions).length > 0 && ( {mode === 'generate' && labelOptions && Object.keys(labelOptions).length > 0 && (
<Card title="标签选择" style={{ marginBottom: '20px' }}> <Card title="标签选择" style={{ marginBottom: '20px' }}>
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: '16px' }}> <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: '16px' }}>
{Array.from({ length: 10 }, (_, i) => i + 1).map(num => { {Array.from({ length: 10 }, (_, i) => i + 1).map(num => {
@ -2757,124 +3078,130 @@ export default function App() {
{/* ... existing code ... */} {/* ... existing code ... */}
<main className="main" style={{ padding: '20px' }}> {mode === 'generate' && (
<Card> <main className="main" style={{ padding: '20px' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '20px' }}> <Card>
<div> <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '20px' }}>
<Title heading={3} style={{ margin: 0 }}></Title> <div>
{selectedRecords.length > 0 && ( <Title heading={3} style={{ margin: 0 }}></Title>
<Text type='secondary' style={{ fontSize: '12px', marginTop: '4px' }}> {selectedRecords.length > 0 && (
{selectedRecords.length} <Text type='secondary' style={{ fontSize: '12px', marginTop: '4px' }}>
</Text> {selectedRecords.length}
)} </Text>
</div>
<Space>
<Button
type='primary'
onClick={handleSelectRecords}
loading={loading}
disabled={loading}
>
{selectedRecords.length > 0 ? '重新选择' : '选择记录'}
</Button>
{selectedRecords.length > 0 && (
<Button
type='secondary'
onClick={handleClearRecords}
size='small'
>
</Button>
)}
</Space>
</div>
{/* 已选择记录的详细信息 */}
{selectedRecords.length > 0 && recordDetails.length > 0 && (
<div style={{
padding: '12px',
backgroundColor: '#f8f9fa',
borderRadius: '6px',
border: '1px solid #e9ecef',
marginBottom: '16px'
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
<div>
<Text strong>:</Text>
<Text code style={{ marginLeft: '8px', fontSize: '12px' }}>{recordDetails[0].displayValue || recordDetails[0].id}</Text>
</div>
{recordDetails.length > 1 && (
<div>
<Text type='secondary'>+ {recordDetails.length - 1} </Text>
</div>
)} )}
</div> </div>
<Space>
<Button
type='primary'
onClick={handleSelectRecords}
loading={loading}
disabled={loading}
>
{selectedRecords.length > 0 ? '重新选择' : '选择记录'}
</Button>
{selectedRecords.length > 0 && (
<Button
type='secondary'
onClick={handleClearRecords}
size='small'
>
</Button>
)}
</Space>
</div> </div>
)}
{/* 已选择记录的详细信息 */}
{/* 加载状态 */} {selectedRecords.length > 0 && recordDetails.length > 0 && (
{loading && ( <div style={{
<div style={{ textAlign: 'center', padding: '20px' }}> padding: '12px',
<Spin size="large" /> backgroundColor: '#f8f9fa',
<div style={{ marginTop: '10px' }}> borderRadius: '6px',
<Text>...</Text> border: '1px solid #e9ecef',
marginBottom: '16px'
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
<div>
<Text strong>:</Text>
<Text code style={{ marginLeft: '8px', fontSize: '12px' }}>{recordDetails[0].displayValue || recordDetails[0].id}</Text>
</div>
{recordDetails.length > 1 && (
<div>
<Text type='secondary'>+ {recordDetails.length - 1} </Text>
</div>
)}
</div>
</div> </div>
</div> )}
)}
{/* 加载状态 */}
{/* 空状态提示 */} {loading && (
{selectedRecords.length === 0 && !loading && ( <div style={{ textAlign: 'center', padding: '20px' }}>
<div style={{ <Spin size="large" />
textAlign: 'center', <div style={{ marginTop: '10px' }}>
padding: '20px', <Text>...</Text>
backgroundColor: '#fafafa', </div>
borderRadius: '6px', </div>
border: '1px dashed #d9d9d9' )}
}}>
<Text type="tertiary"></Text> {/* 空状态提示 */}
</div> {selectedRecords.length === 0 && !loading && (
)} <div style={{
</Card> textAlign: 'center',
</main> padding: '20px',
{/* 面料数据查询结果 */} backgroundColor: '#fafafa',
{queryResults.length > 0 && ( borderRadius: '6px',
<> border: '1px dashed #d9d9d9'
<Divider /> }}>
<Title heading={4}> ({queryResults.length} )</Title> <Text type="tertiary"></Text>
<Table </div>
columns={columns} )}
dataSource={queryResults.map((item, index) => ({ ...item, key: index }))} </Card>
pagination={{ pageSize: 10 }} </main>
style={{ marginTop: '10px' }}
/>
</>
)} )}
{/* 二次工艺查询结果 */} {mode === 'generate' && (
{secondaryProcessResults.length > 0 && (
<> <>
<Divider /> {/* 面料数据查询结果 */}
<Title heading={4}> ({secondaryProcessResults.length} )</Title> {queryResults.length > 0 && (
<Table <>
columns={secondaryProcessColumns} <Divider />
dataSource={secondaryProcessResults.map((item, index) => ({ ...item, key: index }))} <Title heading={4}> ({queryResults.length} )</Title>
pagination={{ pageSize: 10 }} <Table
style={{ marginTop: '10px' }} columns={columns}
/> dataSource={queryResults.map((item, index) => ({ ...item, key: index }))}
</> pagination={{ pageSize: 10 }}
)} style={{ marginTop: '10px' }}
{/* 工艺车缝价格查询结果 */} />
{pricingDetailsResults.length > 0 && ( </>
<> )}
<Divider /> {/* 二次工艺查询结果 */}
<Title heading={4}> ({pricingDetailsResults.length} )</Title> {secondaryProcessResults.length > 0 && (
<Table <>
columns={pricingDetailsColumns} <Divider />
dataSource={pricingDetailsResults.map((item, index) => ({ ...item, key: index }))} <Title heading={4}> ({secondaryProcessResults.length} )</Title>
pagination={{ pageSize: 10 }} <Table
style={{ marginTop: '10px' }} columns={secondaryProcessColumns}
/> dataSource={secondaryProcessResults.map((item, index) => ({ ...item, key: index }))}
pagination={{ pageSize: 10 }}
style={{ marginTop: '10px' }}
/>
</>
)}
{/* 工艺价格查询结果 */}
{pricingDetailsResults.length > 0 && (
<>
<Divider />
<Title heading={4}> ({pricingDetailsResults.length} )</Title>
<Table
columns={pricingDetailsColumns}
dataSource={pricingDetailsResults.map((item, index) => ({ ...item, key: index }))}
pagination={{ pageSize: 10 }}
style={{ marginTop: '10px' }}
/>
</>
)}
</> </>
)} )}
</div> </div>