Files
makemd/server/src/services/BondedWarehouseService.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

import db from '../config/database';
import { logger } from '../utils/logger';
import { AuditService } from './AuditService';
/**
* [BIZ_TRADE_17]
*
*/
export class BondedWarehouseService {
private static readonly BONDED_TABLE = 'cf_bonded_warehouse';
/**
*
*/
static async initTable() {
const hasTable = await db.schema.hasTable(this.BONDED_TABLE);
if (!hasTable) {
logger.info(`📦 Creating ${this.BONDED_TABLE} table...`);
await db.schema.createTable(this.BONDED_TABLE, (table) => {
table.increments('id').primary();
table.string('tenant_id', 64).notNullable();
table.string('warehouse_id', 64).notNullable();
table.string('bonded_area_code', 32).notNullable();
table.string('customs_reg_number', 64).notNullable();
table.string('status', 16).defaultTo('ACTIVE');
table.json('inventory_snapshot');
table.timestamp('created_at').defaultTo(db.fn.now());
table.timestamp('updated_at').defaultTo(db.fn.now());
table.index(['tenant_id', 'warehouse_id']);
});
logger.info(`✅ Table ${this.BONDED_TABLE} created`);
}
}
/**
*
*/
static async setupBondedWarehouse(
tenantId: string,
warehouseId: string,
bondedAreaCode: string,
customsRegNumber: string,
traceId: string
): Promise<void> {
await db.transaction(async (trx) => {
await trx('cf_bonded_warehouse').insert({
tenant_id: tenantId,
warehouse_id: warehouseId,
bonded_area_code: bondedAreaCode,
customs_reg_number: customsRegNumber,
status: 'ACTIVE'
});
// 审计记录
await AuditService.log({
tenant_id: tenantId,
action: 'BONDED_WAREHOUSE_SETUP',
target_type: 'WAREHOUSE',
target_id: warehouseId,
trace_id: traceId,
new_data: JSON.stringify({ bondedAreaCode, customsRegNumber }),
metadata: JSON.stringify({ status: 'ACTIVE' })
});
});
}
/**
*
*/
static async updateInventorySnapshot(tenantId: string, warehouseId: string, snapshot: any): Promise<void> {
await db('cf_bonded_warehouse')
.where({ tenant_id: tenantId, warehouse_id: warehouseId })
.update({
inventory_snapshot: JSON.stringify(snapshot),
updated_at: db.fn.now()
});
}
/**
*
*/
static async getBondedWarehouses(tenantId: string) {
return await db('cf_bonded_warehouse')
.where({ tenant_id: tenantId })
.orderBy('created_at', 'desc');
}
}