feat: 给AMF/MME对应的IMEI白名单添加批量导入和批量删除功能
This commit is contained in:
197
src/views/ne/neConfig/hooks/useArrayImport.ts
Normal file
197
src/views/ne/neConfig/hooks/useArrayImport.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { addNeConfigData, editNeConfigData } from '@/api/ne/neConfig';
|
||||
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
|
||||
import { readSheet } from '@/utils/execl-utils';
|
||||
import { message } from 'ant-design-vue';
|
||||
import { reactive, toRaw } from 'vue';
|
||||
import saveAs from 'file-saver';
|
||||
|
||||
/**
|
||||
* 导入文件加array
|
||||
* @param param 父级传入 { t, neTypeSelect, arrayState, fnActiveConfigNode }
|
||||
* @returns
|
||||
*/
|
||||
export default function useArrayImport({
|
||||
t,
|
||||
neTypeSelect,
|
||||
arrayState,
|
||||
fnActiveConfigNode,
|
||||
}: any) {
|
||||
/**网元导入模板解析 */
|
||||
const m: Record<string, any> = {
|
||||
AMF: {
|
||||
imeiWhitelist: {
|
||||
filename: 'import_amf_imeiWhitelist_template',
|
||||
fileetx: '.xlsx',
|
||||
item: (row: Record<string, any>) => {
|
||||
return {
|
||||
imeiPrefixValue: row['IMEI Prefix'],
|
||||
index: row['Index'],
|
||||
};
|
||||
},
|
||||
},
|
||||
whitelist: {
|
||||
filename: 'import_amf_whitelist_template',
|
||||
fileetx: '.xlsx',
|
||||
item: (row: Record<string, any>) => {
|
||||
return {
|
||||
imsiValue: row['IMSI Value'],
|
||||
imeiValue: row['IMEI Value/Prefix'],
|
||||
index: row['Index'],
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
MME: {
|
||||
white_list: {
|
||||
filename: 'import_mme_imeiWhitelist_template',
|
||||
fileetx: '.xlsx',
|
||||
item: (row: Record<string, any>) => {
|
||||
return {
|
||||
imei: row['IMEI'],
|
||||
index: row['Index'],
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/**状态属性 */
|
||||
const importState = reactive({
|
||||
open: false,
|
||||
msgArr: [] as string[],
|
||||
loading: false, //开始导入
|
||||
item: null as any,
|
||||
paramName: '',
|
||||
filename: '',
|
||||
fileetx: '',
|
||||
});
|
||||
|
||||
/**对话框表格信息导入弹出窗口 */
|
||||
function modalImportOpen(neType: string, paramName: string) {
|
||||
const tmpM = m[neType][paramName];
|
||||
importState.item = tmpM.item;
|
||||
importState.paramName = paramName;
|
||||
importState.filename = tmpM.filename;
|
||||
importState.fileetx = tmpM.fileetx;
|
||||
importState.open = true;
|
||||
}
|
||||
function modalImportClose() {
|
||||
if (importState.loading) {
|
||||
message.error({
|
||||
content: 'Import is in progress, please wait for it to complete',
|
||||
duration: 3,
|
||||
});
|
||||
return;
|
||||
}
|
||||
importState.open = false;
|
||||
importState.msgArr = [];
|
||||
importState.loading = false;
|
||||
fnActiveConfigNode('#');
|
||||
}
|
||||
|
||||
/**对话框表格信息导入上传 */
|
||||
async function modalImportUpload(file: File) {
|
||||
const hide = message.loading(t('common.loading'), 0);
|
||||
const [neType, neId] = neTypeSelect.value;
|
||||
importState.msgArr = [];
|
||||
|
||||
// 获取最大index
|
||||
let index = 0;
|
||||
if (arrayState.columnsData.length <= 0) {
|
||||
index = 1;
|
||||
} else {
|
||||
const last = arrayState.columnsData[arrayState.columnsData.length - 1];
|
||||
index = last.index.value + 1;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (e: any) {
|
||||
const arrayBuffer = e.target.result;
|
||||
readSheet(arrayBuffer).then(async rows => {
|
||||
if (rows.length <= 0) {
|
||||
hide();
|
||||
message.error({
|
||||
content: t('views.neData.baseStation.importDataEmpty'),
|
||||
duration: 3,
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 开始导入
|
||||
importState.loading = true;
|
||||
for (const row of rows) {
|
||||
const rowItem = importState.item(row);
|
||||
let result: any = null;
|
||||
// 检查index是否定义
|
||||
const hasIndex = arrayState.columnsData.find(
|
||||
(item: any) => item.index.value === rowItem.index
|
||||
);
|
||||
if (hasIndex) {
|
||||
result = await editNeConfigData({
|
||||
neType: neType,
|
||||
neId: neId,
|
||||
paramName: importState.paramName,
|
||||
paramData: rowItem,
|
||||
loc: `${rowItem.index}`,
|
||||
});
|
||||
let msg = `update ${rowItem.index} fail`;
|
||||
if (result.code === RESULT_CODE_SUCCESS) {
|
||||
msg = `update ${rowItem.index} success`;
|
||||
}
|
||||
importState.msgArr.push(msg);
|
||||
} else {
|
||||
// 未定义则新增
|
||||
result = await addNeConfigData({
|
||||
neType: neType,
|
||||
neId: neId,
|
||||
paramName: importState.paramName,
|
||||
paramData: Object.assign(rowItem, { index }),
|
||||
loc: `${index}`,
|
||||
});
|
||||
let msg = `add ${index} fail`;
|
||||
if (result.code === RESULT_CODE_SUCCESS) {
|
||||
index += 1;
|
||||
msg = `add ${index} success`;
|
||||
}
|
||||
importState.msgArr.push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
hide();
|
||||
importState.loading = false;
|
||||
});
|
||||
};
|
||||
reader.onerror = function (e) {
|
||||
hide();
|
||||
console.error('reader file error:', e);
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
/**对话框表格信息导入模板 */
|
||||
function modalImportTemplate() {
|
||||
const hide = message.loading(t('common.loading'), 0);
|
||||
|
||||
const baseUrl = import.meta.env.VITE_HISTORY_BASE_URL;
|
||||
const templateUrl = `${
|
||||
baseUrl.length === 1 && baseUrl.indexOf('/') === 0
|
||||
? ''
|
||||
: baseUrl.indexOf('/') === -1
|
||||
? '/' + baseUrl
|
||||
: baseUrl
|
||||
}/neDataImput`;
|
||||
saveAs(
|
||||
`${templateUrl}/${importState.filename}${importState.fileetx}`,
|
||||
`${importState.filename}_${Date.now()}${importState.fileetx}`
|
||||
);
|
||||
|
||||
hide();
|
||||
}
|
||||
|
||||
return {
|
||||
importState,
|
||||
modalImportOpen,
|
||||
modalImportClose,
|
||||
modalImportUpload,
|
||||
modalImportTemplate,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user