#
vincentlu
2025-05-13 ebd2f4397a92c6a5096de1b86d59154363344720
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React, { useRef, useEffect, useState } from 'react';
import { useTranslate, useDataProvider, useRedirect } from 'react-admin';
import {
    Box,
    CircularProgress,
    Grid,
    Typography,
    Paper,
    Button,
    Divider,
    Stack,
} from '@mui/material';
import ShelfThree from './ShelfThree';
import { getLocGroup } from '../../http';
import { grey } from '@mui/material/colors';
import ConfirmButton from '../../../page/components/ConfirmButton';
 
const ShelfMain = (props) => {
    const { row, bay, curLocNo, curShelfInfo: info, setCurLocNo } = props;
    const translate = useTranslate();
    const dataProvider = useDataProvider();
    const redirect = useRedirect();
    const containerRef = useRef();
 
    const [selectedLoc, setSelectedLoc] = useState(null);   // loc obj
    const [loading, setLoading] = useState(true);
 
    const shelfThreeRef = useRef(null);
 
    const startThree = (dom) => {
        if (shelfThreeRef.current) {
            shelfThreeRef.current.destroy();
        }
        shelfThreeRef.current = new ShelfThree(dom);
        shelfThreeRef.current.startup();
    };
 
    const endThree = () => {
        if (shelfThreeRef.current) {
            shelfThreeRef.current.destroy();
            shelfThreeRef.current = null;
        }
    };
 
    const renderThree = (info, curLocNo) => {
        if (info && shelfThreeRef.current) {
            shelfThreeRef.current.generateMesh((loader, addObject) => {
                const promises = [];
                const singleHeight = 123;
                for (const item of info) {
                    const { lev } = item;
                    promises.push(new Promise((resolve) => {
                        loader.load('model/shelf.fbx', (mesh) => {
                            mesh.position.set(0, singleHeight * (lev - 1), 0);
                            mesh.scale.set(5, 5, 5);
                            mesh.name = item.locNo;
                            mesh.traverse((child) => {
                                if (child.isMesh) {
                                    switch (child.name) {
                                        case '货架':
                                            child.material.color.set(0x4680BF);
                                            break;
                                        case '托盘':
                                            child.visible = item.locStsEl === 'STOCK' || item.locStsEl === 'PAKOUT';
                                            child.material.color.set(0xBEBEBE);
                                            break;
                                        case '不规则':
                                            child.visible = item.locStsEl === 'STOCK' || item.locStsEl === 'PAKOUT';
                                            child.material.color.set(0xE8B67E);
                                            break;
                                        default:
                                            break;
                                    }
                                    child.name = item.locNo;
                                    child.castShadow = true;
                                    child.receiveShadow = true;
                                }
                            });
                            addObject(mesh);
                            resolve();
                        });
                    }));
                }
 
                Promise.all(promises).then(() => {
                    if (shelfThreeRef.current) {
                        if (typeof shelfThreeRef.current.setNewSelectedMesh === 'function') {
                            shelfThreeRef.current.setNewSelectedMesh(curLocNo);
                        }
                        if (typeof shelfThreeRef.current.rePerspective === 'function') {
                            shelfThreeRef.current.rePerspective(singleHeight * info.length, 500);
                        }
                    }
                }).catch(error => {
                    console.error(error);
                });
            });
        }
    };
 
    useEffect(() => {
        if (info) {
            setSelectedLoc(info[0]);
            setLoading(true);
            setTimeout(() => {
                startThree(containerRef.current);
                shelfThreeRef.current.handleClick = (objName) => {
                    setCurLocNo(objName);
                    setSelectedLoc(info.find(loc => objName === loc.locNo));
                };
                renderThree(info, curLocNo);
                setLoading(false);
            }, 200);
        }
        return endThree;
    }, [info]);
 
    const updateStatus = () => {
        dataProvider.update('loc', {
            id: info.agvId,
            data: {
                status: info.status === 0 ? 1 : 0
            }
        }).then(({ data }) => {
            fetchAgvInfo(curAgvNo);
            notify.success(translate('common.response.success'));
        }).catch((error) => {
            notify.success(translate('common.response.fail'));
            console.error(`Error: ${error.message}`);
        });
    };
 
    return (
        <Box display="flex" height="100%">
            <Box
                position="relative"
                width="50%"
                height="100%"
                ref={containerRef}
                style={{ backgroundColor: '#7a7a7a' }}
            >
                {loading && (
                    <Box
                        position="absolute"
                        top="50%"
                        left="50%"
                        style={{ transform: 'translate(-50%, -50%)' }}
                    >
                        <CircularProgress sx={{ color: grey[50] }} />
                    </Box>
                )}
            </Box>
            <Box width="50%" height="100%" overflow="auto" pl={1}>
                <Paper elevation={1} style={{
                    height: "100%",
                    padding: '16px',
                    display: 'flex',
                    flexDirection: 'column',
                    justifyContent: 'space-between',
                }}>
                    <Box sx={{
                        height: "70%",
                        overflowY: 'auto'
                    }}>
                        <Grid container spacing={1.5} style={{ marginTop: '0px' }}>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.locNo')} value={selectedLoc?.locNo || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.row')} value={selectedLoc?.row || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.bay')} value={selectedLoc?.bay || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.lev')} value={selectedLoc?.lev || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.offset')} value={selectedLoc?.offset || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.locSts')} value={selectedLoc?.locSts$ || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.zoneId')} value={selectedLoc?.zoneId$ || 'N/A'} />
                            </Grid>
                            <Grid item xs={12}>
                                <KeyValuePair label={translate('table.field.loc.code')} value={selectedLoc?.code$ || 'N/A'} />
                            </Grid>
                        </Grid>
                    </Box>
 
                    <Box sx={{
                        height: "30%",
                        display: 'flex',
                        flexDirection: 'column',
                        justifyContent: 'flex-end'
                    }}>
                        <Grid container spacing={2} style={{ marginTop: '0px' }}>
                            <Grid item xs={12}>
                                <Stack spacing={2} mt={2}>
                                    <Button
                                        color="primary"
                                        variant="contained"
                                        onClick={() => {
                                            const searchQuery = encodeURIComponent(JSON.stringify({ row, bay }));
                                            redirect(`/loc?filter=${searchQuery}`);
                                        }}
                                    >
                                        {translate('page.map.settings.map.more.title')}
                                    </Button>
                                    <Button
                                        color="primary" // inherit
                                        variant="contained"
                                        onClick={() => {
                                            redirect('edit', 'loc', selectedLoc?.id);
                                        }}
                                    >
                                        {translate('page.map.insight.shelf.updateStatus')}
                                    </Button>
                                    {/* <ConfirmButton
                                        label="page.map.insight.shelf.updateStatus"
                                        color="primary" // inherit
                                        variant="contained"
                                        onConfirm={() => {
                                            redirect('edit', 'loc', selectedLoc?.id);
                                        }}
                                    /> */}
                                    <Button variant="contained" color="error" disabled={selectedLoc?.locStsEl !== 'STOCK'} fullWidth>
                                        {translate('page.map.insight.shelf.outbound')}
                                    </Button>
                                </Stack>
                            </Grid>
                        </Grid>
                    </Box>
                </Paper>
            </Box>
        </Box>
    );
};
 
const KeyValuePair = ({ label, value }) => (
    <>
        <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 1 }}>
            <Typography
                variant="body2"
                sx={{ width: '80px', fontWeight: 'bold', color: 'text.secondary', textAlign: 'left' }}
            >
                {label}:
            </Typography>
            <Typography
                variant="body2"
                sx={{ flexGrow: 1, color: 'text.primary', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}
            >
                {value}
            </Typography>
        </Stack>
        <Divider />
    </>
);
 
export default ShelfMain;