
import * as commonUtils from '@/utils/utils';
import * as commonFunc from './commonFunc';

/**
 * 自定义onChange事件处理函数 - 处理表格字段变更时的联动逻辑
 *
 * @param {string} name - 表名（'master'表示主表，其他表示从表如slave,control,materials,process）
 * @param {string} sFieldName - 触发变更的字段名称
 * @param {Object} tableConfig - 表格配置对象，包含gdsconfigformslave配置数组
 * @param {Object} masterData - 主表数据对象
 * @param {Object} tableDataRow - 当前行数据对象（从表时使用）
 * @param {boolean} isWait - 是否等待处理完成
 * @param {Object} dataCollection - 所有表格数据集合
 * @param {Object} dataSelected - 选中的数据对象
 * @param {Object} app - 应用实例对象
 * @returns {Object} 处理后的数据对象
 */
export function getKeyUpEvent(name, sFieldName, tableConfig, masterData, tableDataRow, isWait, dataCollection, dataSelected, app) {
  // 初始化数据对象和拷贝容器
  let tableDataRowNew = {};
  const newCopyTo = {};

  // 根据表名类型初始化数据
  if (name === 'master') {
    tableDataRowNew = masterData; // 主表使用masterData
  } else {
    tableDataRowNew = tableDataRow; // 从表使用当前行数据
    newCopyTo.slave = tableDataRow; // 设置从表数据到拷贝容器
  }

  // 检查表格配置是否存在且有效
  if (commonUtils.isNotEmptyObject(tableConfig) && commonUtils.isNotEmptyArr(tableConfig.gdsconfigformslave)) {
    // 查找对应字段的配置项
    const iConfigIndex = tableConfig.gdsconfigformslave.findIndex(item => item.sName === sFieldName);
    if (iConfigIndex > -1) {
      // 设置主表数据到拷贝容器
      newCopyTo.master = masterData;

      // 获取赋值字段配置和按钮参数
      const sAssignField = tableConfig.gdsconfigformslave[iConfigIndex].sOnChangeAssignField;
      const sButtonParam = tableConfig.gdsconfigformslave[iConfigIndex].sButtonParam; /* 设置多级联动 */


      // 根据赋值字段类型分发处理逻辑
      /* keyUp联动其他表 */
      if (commonUtils.isNotEmptyObject(sAssignField) && (sAssignField.includes('@@@:') || sAssignField.includes(';,'))) {
        // 多表联动处理（包含@@@:或;,分隔符）
        return handleMultiTableLinkage(name, sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app);
      } else if (commonUtils.isNotEmptyObject(sAssignField) && sAssignField.includes('_')) {
        // 跨表联动处理（包含下划线_all汇总, _filter过滤）
        return handleCrossTableLinkage(name, sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app);
      } else if (commonUtils.isNotEmptyObject(sAssignField)) {
        // 单表keyUp事件处理
        return handleSingleTableEvent(name, sAssignField, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataSelected, isWait);
      } else {
        // 无有效赋值字段，返回空对象
        return {};
      }
    }
  }

  // 未找到对应配置或配置无效，返回原始数据
  return tableDataRowNew;
}

// 辅助函数：处理多表联动
function handleMultiTableLinkage(name, sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app) {
  const sAssignFieldObj = sAssignField.split(';,');
  console.log('=====sAssignFieldObj', sAssignFieldObj);
  let addState = {};

  if (name === 'master') {
    sAssignFieldObj.forEach((sAssignFieldItem) => {
      if (sAssignFieldItem?.indexOf('@@@:') !== -1) {
        const sFormula = sAssignFieldItem.split('@@@:')[1];
        const sAssignFieldV2 = commonFunc.getAssignFieldV2(sFormula, tableDataRowNew);
        const result = commonFunc.getAssignFieldValue(sAssignFieldV2, tableDataRowNew, newCopyTo);
        addState = { ...addState, ...result };
      } else if (sAssignFieldItem?.includes('_all')) {
        addState = { ...addState, ...handleAllTableLinkage(sAssignFieldItem, newCopyTo, masterData, dataCollection) };
      } else {
        addState = {
          ...addState,
          ...commonFunc.getAssignFieldValue(sAssignFieldItem, tableDataRowNew, newCopyTo),
        };
      }
    });
  } else {
    sAssignFieldObj.forEach((sAssignFieldItem) => {
      const tableConfigCopy = cloneDeep(tableConfig);
      tableConfigCopy.gdsconfigformslave[iConfigIndex].sOnChangeAssignField = sAssignFieldItem;
      const returnResult = getKeyUpEvent(name, sFieldName, tableConfigCopy, masterData, { ...tableDataRow, ...addState }, isWait, dataCollection, dataSelected, app);
      addState = { ...addState, ...returnResult };
    });
  }

  console.log('=====KeyUpResult', addState);
  return { ...tableDataRowNew, ...addState };
}

// 辅助函数：处理_all类型的表联动
function handleAllTableLinkage(sAssignFieldItem, newCopyTo, masterData, dataCollection) {
  const addState = {};
  const leftFiled = sAssignFieldItem; /* 每一个赋值字段 */
  if (commonUtils.isNotEmptyObject(leftFiled)) {
    const tbName = leftFiled.substring(0, leftFiled.indexOf('_'));
    const tableData = dataCollection[tbName];
    if (commonUtils.isNotEmptyArr(tableData)) {
      const sOnChangeAssignFieldNew = leftFiled.replace(`${tbName}_all.`, '').trim();
      tableData.forEach((item, index) => {
        newCopyTo[tbName] = item;
        const tableRow = { ...item, ...commonFunc.getAssignFieldValue(sOnChangeAssignFieldNew, item, newCopyTo) }; // 取赋值字段
        tableRow.handleType = commonUtils.isEmpty(tableRow.handleType) ? 'update' : tableRow.handleType;
        tableData[index] = tableRow;
      });
      addState[tbName] = tableData;
      masterData.handleType = commonUtils.isEmpty(masterData.handleType) ? 'update' : masterData.handleType;
      addState.master = masterData;
      addState.bMultiTable = true;
    }
  }
  return addState;
}

// 辅助函数：处理跨表联动
function handleCrossTableLinkage(name, sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app) {
  const addState = {};
  /* 主表联动其他表格 */
  if (name === 'master' && (!sAssignField.includes('filter') && !sAssignField.includes('sum') )) {
    const sAssignFieldObj = sAssignField.split(',');
    if (commonUtils.isNotEmptyArr(sAssignFieldObj)) {
      sAssignFieldObj.forEach((eachFiled) => {
        if (eachFiled.includes('_')) {
          const tbName = eachFiled.substring(0, eachFiled.indexOf('_'));
          const tableData = dataCollection[tbName];
          if (commonUtils.isNotEmptyArr(tableData)) {
            const sOnChangeAssignFieldNew = eachFiled.replace(`${tbName}_all.`, '').trim();
            console.log('333', sOnChangeAssignFieldNew);
            tableData.forEach((item, index) => {
              newCopyTo[tbName] = item;
              newCopyTo.master = masterData;
              let tableRow = { ...item, ...commonFunc.getAssignFieldValue(sOnChangeAssignFieldNew, item, newCopyTo, true, true) };
              if (commonUtils.isNotEmptyObject(sButtonParam)) {
                let sLinkFieldName = '';
                if (commonUtils.isJSON(sButtonParam)) {
                  const linkObj = JSON.parse(sButtonParam);
                  let models = 'Product';
                  if (linkObj.linkMaterials) {
                    sLinkFieldName = linkObj.linkMaterials;
                    models = 'Materials';
                  } else if (linkObj.linkProduct) {
                    sLinkFieldName = linkObj.linkProduct;
                    models = 'Product';
                  }
                  console.log('sLinkFieldName', sLinkFieldName, models);
                  tableRow = this.getCalculateAllMoney(app, models, sLinkFieldName, masterData, tableRow);
                }
              }
              tableRow.handleType = commonUtils.isEmpty(tableRow.handleType) ? 'update' : tableRow.handleType;
              tableData[index] = tableRow;
            });
            addState[tbName] = tableData;
            masterData.handleType = commonUtils.isEmpty(masterData.handleType) ? 'update' : masterData.handleType;
            addState.master = commonUtils.isNotEmptyObject(addState.master) ? { ...addState.master, ...masterData } : masterData;
            addState.bMultiTable = true;
          }
        } else {
          if (commonUtils.isNotEmptyArr(dataCollection)) {
            if (eachFiled.includes('master.')) {
              newCopyTo.master = dataCollection.master;
            } else if (eachFiled.includes('control.')) {
              // const tbName = 'control';
            }
          }
          tableDataRowNew = { ...tableDataRowNew, ...commonFunc.getAssignFieldValue(eachFiled, tableDataRowNew, newCopyTo) };
          let { handleType } = masterData;
          if (name !== 'master') {
            // eslint-disable-next-line prefer-destructuring
            handleType = tableDataRow.handleType;
          }
          addState.bMultiTable = true;
          tableDataRowNew.handleType = commonUtils.isEmpty(handleType) ? 'update' : handleType;
          addState.master = tableDataRowNew;
        }
      });
    }
    return addState;
  } else {
    const sAssignFieldObj = sAssignField.split(',');
    if (commonUtils.isNotEmptyArr(sAssignFieldObj)) {
      const leftFiled = sAssignFieldObj[0];
      if (commonUtils.isNotEmptyObject(leftFiled)) {
        const tbName = leftFiled.substring(0, leftFiled.indexOf('_'));
        const tableData = dataCollection[name];
        if (commonUtils.isNotEmptyArr(tableData)) {
          if (sAssignField.indexOf('filter') > -1 && sAssignField.indexOf('sum') > -1) {
            return handleFilterAndSum(sAssignField, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app);
          } else if (sAssignField.indexOf('sum') > -1) {
            return handleSumOnly(tbName, sAssignField, tableData, tableDataRowNew, newCopyTo, masterData, tableDataRow, app);
          } else if (sAssignField.indexOf('filter') > -1) {
            return handleFilterOnly(sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, app);
          }
        }
      }
    }
    return tableDataRowNew;
  }
}

// 辅助函数：处理过滤并汇总
function handleFilterAndSum(sAssignField, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, dataSelected, app) {
  console.log('赋值字段', sAssignField);
  let sOnChangeAssignFieldNew = sAssignField;
  const indexOf = sAssignField.indexOf('_');
  const targetSumName = sAssignField.substring(0, indexOf);
  let targetTbName = '';

  const fStartIndexOf = sAssignField.indexOf(':');
  const fEndIndexOf = sAssignField.lastIndexOf('_filter');
  console.log('拿目标要过滤的表名', targetSumName, fStartIndexOf, fEndIndexOf);
  if (fStartIndexOf > -1 && fEndIndexOf > -1) {
    targetTbName = sAssignField.substring(fStartIndexOf + 1, fEndIndexOf);
  }
  const targetTableData = dataCollection[targetTbName];
  const targetData = dataCollection[targetSumName];
  const targetRow = dataSelected[`${targetSumName}Row`];
  const targetFieldName = sAssignField.substring(sAssignField.indexOf('.') + 1, sAssignField.indexOf(':'));
  const baseFieldName = sAssignField.substring(sAssignField.lastIndexOf('.') + 1, sAssignField.length);
  let filterTableData = [];
  const leftIndexOf = sAssignField.indexOf('{');
  const rightIndexOf = sAssignField.lastIndexOf('}');
  if (leftIndexOf > -1 && rightIndexOf > -1) {
    const condition = sAssignField.substring(leftIndexOf, rightIndexOf + 1);
    const conditionStr = condition.toString();
    sOnChangeAssignFieldNew = sAssignField.replace(`${targetTbName}_filter_${condition}.`, '').replace('_sum', '').trim();
    console.log('sOnChangeAssignFieldNew', sOnChangeAssignFieldNew);
    let jsonObj = {};
    if (commonUtils.isNotEmptyObject(conditionStr) && commonUtils.isJSON(conditionStr)) {
      jsonObj = JSON.parse(conditionStr);
      const { ifdo: ifdoStr, condition, decimal = 0 } = jsonObj;
      let ifdoResult = true;
      if (ifdoStr) {
        try {
          let ifdoStrNew = "";
          const reg = new RegExp(`master\.`, "g");
          ifdoStrNew = ifdoStr.replace(reg, `masterData.`);
          const reg1 = new RegExp(`slave\.`, "g");
          ifdoStrNew = ifdoStrNew.replace(reg1, `tableDataRow.`);
          const evalStr = eval("`" + ifdoStrNew + "`");
          ifdoResult = eval(evalStr);
        } catch (error) {
          console.log('=====keyup ifdo 报错:', {
            ifdoStr, error
          })
        }
      }
      if (!ifdoResult) {
        return {};
      }
      if (commonUtils.isNotEmptyArr(condition)) {
        filterTableData = applyFilterConditions(condition, targetTableData, masterData, tableDataRow, baseFieldName, app);
      }
      let sumData = 0;
      if (commonUtils.isNotEmptyArr(filterTableData)) {
        sumData = calculateSum(sFieldName, filterTableData, tableDataRow, targetTableData, decimal, newCopyTo, masterData, name, targetTbName, app);
        if (commonUtils.isNotEmptyObject(targetRow)) {
          targetRow[sFieldName] = sumData;
        }
        if (targetSumName === 'master') {
          const targetDataNew = name === 'master' ? masterData : targetData;
          targetDataNew[targetFieldName] = sumData;
          targetDataNew.handleType = commonUtils.isEmpty(targetDataNew.handleType) ? 'update' : targetDataNew.handleType;
          addState[targetSumName] = targetDataNew;
          if(name === 'master') {
            addState[targetTbName] = targetTableData;
            return  addState;
          }
        } else if (commonUtils.isNotEmptyArr(targetData)) {
          targetRow[`${sFieldName}Sum`] = sumData;
          const iSlaveIndex = targetData.findIndex(item => item.sId === targetRow.sId);
          if (iSlaveIndex > -1) {
            targetData[iSlaveIndex][`${sFieldName}Sum`] = sumData;
            newCopyTo[targetSumName] = targetData[iSlaveIndex];
            targetData[iSlaveIndex].dTotal_waste_qty = sumData;
            targetData[iSlaveIndex].handleType = commonUtils.isEmpty(targetData[iSlaveIndex].handleType) ? 'update' : targetData[iSlaveIndex].handleType;
            addState[targetData] = targetData;
          }
        }
        addState[targetTbName] = targetTableData;
      }
    }
  }
  return addState;
}

// 辅助函数：应用过滤条件
function applyFilterConditions(conditions, targetTableData, masterData, tableDataRow, baseFieldName, app) {
  let filterTableData = [];
  for (const item of conditions) {
    const { bFilterName, bFilterCondition, bFilterValue } = item;
    if (bFilterCondition === '>') {
      if (bFilterValue.includes('.')) {
        const indexs = bFilterValue.indexOf('.');
        const sConditionTableName = bFilterValue.substring(0, indexs);
        const sConditionFieldName = bFilterValue.substring(indexs + 1, bFilterValue.length);
        if (sConditionFieldName === 'slave') {
          if (commonUtils.isNotEmptyArr(targetTableData)) {
            filterTableData = targetTableData.filter(item => item[bFilterName] > tableDataRow[sConditionTableName]);
          }
        }
      } else {
        filterTableData = targetTableData.filter(item => item[bFilterName] > bFilterValue);
      }
    } else if (bFilterCondition === '=') {
      if (bFilterValue.includes('.')) {
        const indexs = bFilterValue.indexOf('.');
        const sConditionTableName = bFilterValue.substring(0, indexs);
        const sConditionFieldName = bFilterValue.substring(indexs + 1, bFilterValue.length);
        if (sConditionTableName === 'slave') {
          if (commonUtils.isNotEmptyArr(targetTableData)) {
            filterTableData = targetTableData.filter(item => item[bFilterName] === tableDataRow[sConditionFieldName]);
          }
        } else if (sConditionTableName === 'master') {
          if (commonUtils.isNotEmptyArr(targetTableData)) {
            filterTableData = targetTableData.filter(item => item[bFilterName] === masterData[sConditionFieldName]);
          }
        }
      } else {
        filterTableData = targetTableData.filter(item => item[bFilterName] === bFilterValue);
      }
      if(baseFieldName !== sFieldName) {
        sFieldName = baseFieldName;
      }
    } else if (bFilterCondition === '<') {
      if (bFilterValue.includes('.')) {
        const indexs = bFilterValue.indexOf('.');
        const sConditionTableName = bFilterValue.substring(0, indexs);
        const sConditionFieldName = bFilterValue.substring(indexs + 1, bFilterValue.length);
        if (sConditionTableName === 'slave') {
          if (commonUtils.isNotEmptyArr(targetTableData)) {
            filterTableData = targetTableData.filter(item => item[bFilterName] < tableDataRow[sConditionFieldName]);
          }
        }
      } else {
        filterTableData = targetTableData.filter(item => item[bFilterName] < bFilterValue);
      }
    }
  }
  return filterTableData;
}

// 辅助函数：计算汇总值
function calculateSum(sFieldName, filterTableData, tableDataRow, targetTableData, decimal, newCopyTo, masterData, name, targetTbName, app) {
  let sumData = 0;
  filterTableData.forEach((item) => {
    const iIndex = targetTableData.findIndex(itemNew => itemNew.sId === item.sId);
    if (iIndex > -1) {
      newCopyTo.master = masterData;
      newCopyTo[targetTbName] = item;
      newCopyTo[name] = tableDataRow;
      newCopyTo.slaveInfo = item;
      newCopyTo.slaveChild = item;
      if (item.sId !== tableDataRow.sId) {
        sumData += commonUtils.isNull(item[sFieldName], 0);
      } else {
        sumData += commonUtils.isNull(tableDataRow[sFieldName], 0);
      }
    }
  });
  if (decimal) {
    sumData = Number(sumData.toFixed(decimal));
  }
  console.log('sumData', sumData);
  return sumData;
}

// 辅助函数：仅处理汇总
function handleSumOnly(tbName, sAssignField, tableData, tableDataRowNew, newCopyTo, masterData, tableDataRow, app) {
  const addState = {};
  const sOnChangeAssignFieldNew = sAssignField.replace(`${tbName}_sum.`, '').trim();
  newCopyTo.master = masterData;
  let sumData = 0;
  tableData.forEach((item, index) => {
    if (item.sId === tableDataRowNew.sId) {
      item = tableDataRowNew;
      tableData[index] = tableDataRowNew;
    }
    newCopyTo[name] = item;
    const tableRow = { ...item, ...commonFunc.getAssignFieldValue(sOnChangeAssignFieldNew, item, newCopyTo) };
    tableRow.handleType = commonUtils.isEmpty(tableRow.handleType) ? 'update' : tableRow.handleType;
    const tempName = `${sFieldName}temp_sum`;
    const tempData = tableRow[tempName];
    if (!commonUtils.isEmpty(tempData)) {
      sumData += tableRow[tempName];
      sumData += tableRow[sFieldName];
    }
    tableData[index] = tableRow;
  });
  addState[name] = tableData;
  masterData.handleType = commonUtils.isEmpty(masterData.handleType) ? 'update' : masterData.handleType;
  masterData[sumData] = sumData;
  addState.masterData = masterData;
  return addState;
}

// 辅助函数：仅处理过滤
function handleFilterOnly(sAssignField, sButtonParam, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataCollection, app) {
  const addState = {};
  let sOnChangeAssignFieldNew = sAssignField;
  const indexOf = sAssignField.indexOf('_');
  const targetTbName = sAssignField.substring(0, indexOf);
  const targetTableData = dataCollection[targetTbName];
  let filterTableData = [];
  const leftIndexOf = sAssignField.indexOf('{');
  const rightIndexOf = sAssignField.lastIndexOf('}');
  if (leftIndexOf > -1 && rightIndexOf > -1) {
    const condition = sAssignField.substring(leftIndexOf, rightIndexOf + 1);
    const conditionStr = condition.toString();
    sOnChangeAssignFieldNew = sAssignField.replace(`${targetTbName}_filter_${condition}.`, '').trim();
    let jsonObj = {};
    if (commonUtils.isNotEmptyObject(conditionStr)) {
      jsonObj = JSON.parse(conditionStr);
      const { condition } = jsonObj;
      if (commonUtils.isNotEmptyArr(condition)) {
        filterTableData = applyFilterConditions(condition, targetTableData, masterData, tableDataRow, sFieldName, app);
      }
      if (commonUtils.isNotEmptyArr(filterTableData)) {
        filterTableData.forEach((item) => {
          const iIndex = targetTableData.findIndex(itemNew => itemNew.sId === item.sId);
          if (iIndex > -1) {
            newCopyTo.master = masterData;
            newCopyTo[targetTbName] = item;
            newCopyTo[name] = tableDataRow;
            newCopyTo.slaveInfo = item;
            const tableRowAfter = { ...item, ...commonFunc.getAssignFieldValue(sOnChangeAssignFieldNew, tableDataRow, newCopyTo) };
            let tableNewRow = {};
            if (commonUtils.isNotEmptyObject(sButtonParam)) {
              newCopyTo[name] = tableRowAfter;
              tableNewRow = { ...tableRowAfter, ...commonFunc.getAssignFieldValue(sButtonParam, tableRowAfter, newCopyTo) };
            }
            tableRowAfter.handleType = commonUtils.isEmpty(tableRowAfter.handleType) ? 'update' : tableRowAfter.handleType;
            if (commonUtils.isNotEmptyObject(tableNewRow)) {
              targetTableData[iIndex] = { ...tableRowAfter, ...tableNewRow };
            } else {
              targetTableData[iIndex] = { ...item, ...tableRowAfter };
            }
          }
        });
        addState[targetTbName] = targetTableData;
      }
    }
  }
  return addState;
}

// 辅助函数：处理单表事件
function handleSingleTableEvent(name, sAssignField, tableDataRowNew, newCopyTo, masterData, tableDataRow, dataSelected, isWait) {
  const bCrossTable = isCrossTableAssignment(sAssignField);
  const tableNames = extractTableNames(sAssignField);

  const newTableSelectedMap = formatTableData(dataSelected);
  const datasetMap = bCrossTable ? newTableSelectedMap : newCopyTo;

  // 使用新的变量名来接收处理后的数据
  const processedData = {
    ...tableDataRowNew,
    ...commonFunc.getAssignFieldValue(sAssignField, tableDataRowNew, datasetMap, false, bCrossTable, tableNames)
  };

  let { handleType } = masterData ? masterData : { handleType: '' };
  if (name !== 'master') {
    handleType = processedData.handleType;
  }
  processedData.handleType = commonUtils.isEmpty(handleType) ? 'update' : handleType;

  if (isWait) {
    return processedData;
  }

  return processedData;
}

function extractTableNames(sAssignField) {
  const tableNames = [];
  const regex = /(\w+)\./g;
  let match;
  while ((match = regex.exec(sAssignField)) !== null) {
    if (!tableNames.includes(match[1])) {
      tableNames.push(match[1]);
    }
  }
  return tableNames;
}

// 辅助函数：格式化表格数据
function formatTableData(originObj) {
  // 添加空值检查，防止undefined或null导致Object.entries报错
  if (originObj == null || typeof originObj !== 'object') {
    return {};
  }

  const newObj = {};
  // 遍历原始对象的键值对
  Object.entries(originObj).forEach(([key, value]) => {
    // 去掉末尾的Row后缀
    const newKey = key.replace(/Row$/, '');
    // 赋值到新对象（键名更新，数据不变）
    newObj[newKey] = value;
  });
  return newObj;
}

// 辅助函数：获取子级数据（递归）
function getChildData(parentId, tableData, childData = []) {
  if (!commonUtils.isNotEmptyArr(tableData)) return childData;

  const children = tableData.filter(item => item.sParentId === parentId);
  if (commonUtils.isNotEmptyArr(children)) {
    childData.push(...children);
    children.forEach(child => {
      getChildData(child.sId, tableData, childData);
    });
  }
  return childData;
}



// 辅助函数：判断是否为跨表赋值
function isCrossTableAssignment(sAssignField) {
  const crossTableKeywords = ['slave.', 'control.', 'materials.', 'process.', 'master.'];
  return crossTableKeywords.some(keyword => sAssignField.includes(keyword));
}
