2 天以前 1c1af9b0fc10778ae5ac13cc68fd4030affbcfe1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { getWarehouseAreaSimpleList } from '#/api/mes/wm/warehouse/area';
import { getWarehouseSimpleList } from '#/api/mes/wm/warehouse';
import { getWarehouseLocationSimpleList } from '#/api/mes/wm/warehouse/location';
 
export interface WmLocationMaps {
  warehouseNameMap: Record<number, string>;
  locationNameMap: Record<number, string>;
  areaNameMap: Record<number, string>;
}
 
let cached: WmLocationMaps | null = null;
 
/** 加载仓库/库区/库位 ID -> 名称映射(模块级缓存,仅首次加载) */
export async function getWmLocationMaps(): Promise<WmLocationMaps> {
  if (cached) {
    return cached;
  }
  const [warehouses, locations, areas] = await Promise.all([
    getWarehouseSimpleList(),
    getWarehouseLocationSimpleList(),
    getWarehouseAreaSimpleList(),
  ]);
  cached = {
    warehouseNameMap: Object.fromEntries(
      warehouses.map((w) => [w.id!, w.name || w.code || '']),
    ),
    locationNameMap: Object.fromEntries(
      locations.map((l) => [l.id!, l.name || l.code || '']),
    ),
    areaNameMap: Object.fromEntries(
      areas.map((a) => [a.id!, a.name || a.code || '']),
    ),
  };
  return cached;
}
 
/** 根据 ID 渲染 仓库/库区/库位 文本(ID 缺失时返回空串) */
export function formatWmLocation(
  maps: WmLocationMaps,
  warehouseId?: number,
  locationId?: number,
  areaId?: number,
): string {
  return [
    warehouseId ? maps.warehouseNameMap[warehouseId] : '',
    locationId ? maps.locationNameMap[locationId] : '',
    areaId ? maps.areaNameMap[areaId] : '',
  ]
    .filter(Boolean)
    .join(' / ');
}