This commit is contained in:
parent
08c9675bbf
commit
0f05ae0e39
|
|
@ -4,7 +4,7 @@ import { Importation, Exportation } from 'oak-domain/lib/types/Port';
|
|||
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
|
||||
export declare function registerPorts<ED extends EntityDict>(importations: Importation<ED, keyof ED, any>[], exportations: Exportation<ED, keyof ED, any>[]): void;
|
||||
export declare function clearPorts(): void;
|
||||
export declare function importEntity<ED extends EntityDict, Cxt extends AsyncContext<ED>>(params: FormData, context: Cxt): Promise<void>;
|
||||
export declare function importEntity<ED extends EntityDict, Cxt extends AsyncContext<ED>>(params: FormData, context: Cxt): Promise<NodeJS.ReadableStream | void>;
|
||||
export declare function exportEntity<ED extends EntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: {
|
||||
entity: T;
|
||||
id: string;
|
||||
|
|
|
|||
20
lib/port.js
20
lib/port.js
|
|
@ -31,6 +31,7 @@ function clearPorts() {
|
|||
}
|
||||
exports.clearPorts = clearPorts;
|
||||
function getImportation(id) {
|
||||
console.log(Importations);
|
||||
(0, assert_1.default)(Importations.hasOwnProperty(id), `id为[${id}]的importation不存在`);
|
||||
return Importations[id];
|
||||
}
|
||||
|
|
@ -42,13 +43,30 @@ async function importEntity(params, context) {
|
|||
const entity = params.get('entity');
|
||||
const file = params.get('file');
|
||||
const id = params.get('id');
|
||||
const option = JSON.parse(params.get('option'));
|
||||
const importation = getImportation(id);
|
||||
const { fn } = importation;
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const workbook = (0, xlsx_1.read)(arrayBuffer);
|
||||
const { SheetNames, Sheets } = workbook;
|
||||
const errorSheets = [];
|
||||
for (const sheetName of SheetNames) {
|
||||
const sheet = Sheets[sheetName];
|
||||
const dataList = xlsx_1.utils.sheet_to_json(sheet);
|
||||
console.log(dataList);
|
||||
const errorMessageList = await fn(dataList, context, option);
|
||||
if (errorMessageList.length > 0) {
|
||||
errorSheets.push({
|
||||
sheetName,
|
||||
worksheet: xlsx_1.utils.json_to_sheet(errorMessageList),
|
||||
});
|
||||
}
|
||||
}
|
||||
if (errorSheets.length > 0) {
|
||||
const errorWorkbook = xlsx_1.utils.book_new();
|
||||
for (const sheetData of errorSheets) {
|
||||
xlsx_1.utils.book_append_sheet(errorWorkbook, sheetData.worksheet, sheetData.sheetName);
|
||||
}
|
||||
return await (0, xlsx_1.write)(errorWorkbook, { type: 'buffer' });
|
||||
}
|
||||
// throw new Error('not implement yet');
|
||||
}
|
||||
|
|
|
|||
29
src/port.ts
29
src/port.ts
|
|
@ -2,7 +2,8 @@ import assert from 'assert';
|
|||
import { EntityDict, SelectOption } from 'oak-domain/lib/types/Entity';
|
||||
import { Importation, Exportation } from 'oak-domain/lib/types/Port';
|
||||
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
|
||||
import { read, utils } from 'xlsx';
|
||||
import { read, utils, write } from 'xlsx';
|
||||
import { buffer } from 'stream/consumers';
|
||||
const Importations: Record<string, any> = {};
|
||||
const Exportations: Record<string, any> = {};
|
||||
|
||||
|
|
@ -32,6 +33,7 @@ export function clearPorts() {
|
|||
}
|
||||
|
||||
function getImportation<ED extends EntityDict, T extends keyof ED>(id: string) {
|
||||
console.log(Importations);
|
||||
assert(Importations.hasOwnProperty(id), `id为[${id}]的importation不存在`);
|
||||
return Importations[id] as Importation<ED, T, any>;
|
||||
}
|
||||
|
|
@ -44,22 +46,39 @@ function getExportation<ED extends EntityDict, T extends keyof ED>(id: string) {
|
|||
export async function importEntity<
|
||||
ED extends EntityDict,
|
||||
Cxt extends AsyncContext<ED>
|
||||
>(params: FormData, context: Cxt): Promise<void> {
|
||||
>(params: FormData, context: Cxt): Promise<NodeJS.ReadableStream | void> {
|
||||
const entity = params.get('entity') as keyof ED;
|
||||
const file = params.get('file') as File;
|
||||
const id = params.get('id') as string;
|
||||
const option = params.get('option') as Object;
|
||||
const option = JSON.parse(params.get('option') as string);
|
||||
const importation = getImportation<ED, keyof ED>(id);
|
||||
const { fn } = importation;
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const workbook = read(arrayBuffer);
|
||||
const { SheetNames, Sheets } = workbook;
|
||||
const errorSheets = [];
|
||||
for (const sheetName of SheetNames) {
|
||||
const sheet = Sheets[sheetName];
|
||||
const dataList = utils.sheet_to_json(
|
||||
sheet
|
||||
);
|
||||
console.log(dataList);
|
||||
const errorMessageList = await fn(dataList as Record<string, string | number | boolean>[], context, option);
|
||||
if (errorMessageList.length > 0) {
|
||||
errorSheets.push(
|
||||
{
|
||||
sheetName,
|
||||
worksheet: utils.json_to_sheet(errorMessageList),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
if (errorSheets.length > 0) {
|
||||
const errorWorkbook = utils.book_new();
|
||||
for (const sheetData of errorSheets) {
|
||||
utils.book_append_sheet(errorWorkbook, sheetData.worksheet, sheetData.sheetName);
|
||||
}
|
||||
return await write(errorWorkbook, { type: 'buffer' });
|
||||
}
|
||||
|
||||
// throw new Error('not implement yet');
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue