chen.lin
7 小时以前 bb69d7a4bdfbb90cde19b3d828f490ab10f2bb43
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { useState, useRef, useEffect, useMemo, useCallback } from 'react';
import { Stack, Chip, Dialog, DialogTitle, DialogContent, IconButton, CircularProgress } from '@mui/material';
import { useTranslate, useRecordContext } from 'react-admin';
import CloseIcon from '@mui/icons-material/Close';
import request from '@/utils/request';
 
const CrossZoneAreaField = () => {
    const translate = useTranslate();
    const record = useRecordContext();
    const [open, setOpen] = useState(false);
    const [areaNames, setAreaNames] = useState([]);
    const [loading, setLoading] = useState(false);
 
    const handleOpen = () => {
        setOpen(true);
    };
 
    const handleClose = () => {
        setOpen(false);
    };
 
    const fetchAreaNames = async () => {
        if (!record?.areas || record.areas.length === 0) return;
 
        setLoading(true);
        try {
            // 提取排序信息和ID
            // Old format: [1, 2, 3] (array of integers)
            // New format: [{id: 1, sort: 1}, {id: 2, sort: 2}] (array of objects)
            const isObjectArray = record.areas.length > 0 && 
                typeof record.areas[0] === 'object' && 
                record.areas[0] !== null && 
                'id' in record.areas[0];
            
            let areaIds = [];
            let sortMap = new Map(); // 存储 id -> sort 的映射
            
            if (isObjectArray) {
                // 对象数组格式,提取ID和排序信息
                areaIds = record.areas.map(area => {
                    const id = area.id;
                    sortMap.set(id, area.sort || 0);
                    return id;
                });
            } else {
                // 纯ID数组格式
                areaIds = record.areas.map(id => Number(id));
            }
            
            const res = await request.post(`/warehouseAreas/many/${areaIds.join(',')}`);
            if (res?.data?.code === 200) {
                let areas = res.data.data || [];
                
                // 如果有排序信息,按排序值排序
                if (sortMap.size > 0) {
                    areas = areas.sort((a, b) => {
                        const sortA = sortMap.get(a.id) || 0;
                        const sortB = sortMap.get(b.id) || 0;
                        return sortA - sortB;
                    });
                }
                
                setAreaNames(areas);
            }
        } catch (error) {
            console.error('获取区域名称失败:', error);
        } finally {
            setLoading(false);
        }
    };
 
    useEffect(() => {
        if (record?.areas && record.areas.length !== 0 && record.areas.length > 0) {
            fetchAreaNames();
        }
    }, [record]);
 
    if (loading) {
        return <CircularProgress size={20} />;
    }
 
    return (
        <>
            <Stack
                direction="row"
                gap={1}
                flexWrap="wrap"
                onClick={handleOpen}
                sx={{ cursor: 'pointer' }}
            >
                {areaNames.slice(0, 1).map((item, idx) => (
                    <Chip
                        size="small"
                        key={item.id}
                        label={item.name || item.id}
                    />
                ))}
                {areaNames.length > 1 && (
                    <Chip
                        size="small"
                        label={`+${areaNames.length - 1}`}
                    />
                )}
                {areaNames.length === 0 && record.areas && record.areas.length > 0 && (
                    <Chip
                        size="small"
                        label={`${record.areas.length} 个区域`}
                    />
                )}
            </Stack>
 
            <Dialog
                open={open}
                onClose={handleClose}
                maxWidth="md"
                fullWidth
            >
                <DialogTitle>
                    {translate('table.field.basStation.crossZoneArea')}
                    <IconButton
                        aria-label="close"
                        onClick={handleClose}
                        sx={{
                            position: 'absolute',
                            right: 8,
                            top: 8,
                        }}
                    >
                        <CloseIcon />
                    </IconButton>
                </DialogTitle>
                <DialogContent>
                    {loading ? (
                        <CircularProgress />
                    ) : (
                        <Stack direction="row" gap={1} flexWrap="wrap" sx={{ mt: 1 }}>
                            {areaNames.map((item) => (
                                <Chip
                                    size="small"
                                    key={item.id}
                                    label={item.name || item.id}
                                />
                            ))}
                        </Stack>
                    )}
                </DialogContent>
            </Dialog>
        </>
    );
};
 
export default CrossZoneAreaField;