1
18 小时以前 e711c834aec2293c53b07efe53e81e3573c289b6
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
package com.vincent.rsf.server.manager.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.server.common.annotation.OperationLog;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.KeyValVo;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.common.service.ListExportHandler;
import com.vincent.rsf.server.common.service.ListExportService;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.manager.entity.BasContainer;
import com.vincent.rsf.server.manager.entity.BasStation;
import com.vincent.rsf.server.manager.entity.WarehouseAreas;
import com.vincent.rsf.server.manager.service.BasContainerService;
import com.vincent.rsf.server.manager.service.WarehouseAreasService;
import com.vincent.rsf.server.system.controller.BaseController;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
public class BasContainerController extends BaseController {
 
    @Autowired
    private BasContainerService basContainerService;
 
    @Autowired
    private WarehouseAreasService warehouseAreasService;
 
    @Autowired
    private ListExportService listExportService;
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @PostMapping("/basContainer/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<BasContainer, BaseParam> pageParam = new PageParam<>(baseParam, BasContainer.class);
        PageParam<BasContainer, BaseParam> page = basContainerService.page(pageParam, pageParam.buildWrapper(true));
        return R.ok().add(page);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @PostMapping("/basContainer/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(basContainerService.list());
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @PostMapping({"/basContainer/many/{ids}", "/basContainers/many/{ids}"})
    public R many(@PathVariable Long[] ids) {
        return R.ok().add(basContainerService.listByIds(Arrays.asList(ids)));
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @GetMapping("/basContainer/{id}")
    public R get(@PathVariable("id") Long id) {
        BasContainer basContainer = basContainerService.getById(id);
        // 确保返回的areas按sort字段排序
        if (basContainer != null) {
            basContainer.sortAreas();
        }
        return R.ok().add(basContainer);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:save')")
    @OperationLog("Create 容器管理")
    @PostMapping("/basContainer/save")
    public R save(@RequestBody BasContainer basContainer) {
        basContainer.setCreateBy(getLoginUserId());
        basContainer.setCreateTime(new Date());
        basContainer.setUpdateBy(getLoginUserId());
        basContainer.setUpdateTime(new Date());
        
        // 确保areas按sort字段排序
        basContainer.sortAreas();
        
        BasContainer container = basContainerService.getOne(new LambdaQueryWrapper<BasContainer>().eq(BasContainer::getContainerType, basContainer.getContainerType()));
        if (null != container) {
            return R.error("该类型已被初始化");
        }
        if (!basContainerService.save(basContainer)) {
            return R.error("Save Fail");
        }
        return R.ok("Save Success").add(basContainer);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:update')")
    @OperationLog("Update 容器管理")
    @PostMapping("/basContainer/update")
    public R update(@RequestBody BasContainer basContainer) {
        basContainer.setUpdateBy(getLoginUserId());
        basContainer.setUpdateTime(new Date());
        
        // 确保areas按sort字段排序
        basContainer.sortAreas();
        
        if (!basContainerService.updateById(basContainer)) {
            return R.error("Update Fail");
        }
        return R.ok("Update Success").add(basContainer);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:remove')")
    @OperationLog("Delete 容器管理")
    @PostMapping("/basContainer/remove/{ids}")
    public R remove(@PathVariable Long[] ids) {
        if (!basContainerService.removeByIds(Arrays.asList(ids))) {
            return R.error("Delete Fail");
        }
        return R.ok("Delete Success").add(ids);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @PostMapping("/basContainer/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<BasContainer> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(BasContainer::getId, condition);
        }
        basContainerService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getId()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('manager:basContainer:list')")
    @PostMapping("/basContainer/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        Map<Long, String> areaNameMap = buildAreaNameMap();
        listExportService.export(
                map,
                exportMap -> buildParam(exportMap, BaseParam.class),
                new ListExportHandler<BasContainer, BaseParam>() {
                    @Override
                    public List<BasContainer> listByIds(List<Long> ids) {
                        List<BasContainer> records = basContainerService.listByIds(ids);
                        records.forEach(BasContainer::sortAreas);
                        return records;
                    }
 
                    @Override
                    public List<BasContainer> listByFilter(Map<String, Object> sanitizedMap, BaseParam baseParam) {
                        PageParam<BasContainer, BaseParam> pageParam = new PageParam<>(baseParam, BasContainer.class);
                        QueryWrapper<BasContainer> queryWrapper = pageParam.buildWrapper(true);
                        List<BasContainer> records = basContainerService.list(queryWrapper);
                        records.forEach(BasContainer::sortAreas);
                        return records;
                    }
 
                    @Override
                    public void fillExportFields(List<BasContainer> records) {
                        records.forEach(BasContainer::sortAreas);
                    }
 
                    @Override
                    public Map<String, Object> toExportRow(BasContainer record, List<ExcelUtil.ExportColumn> columns) {
                        return buildExportRow(record, columns, areaNameMap);
                    }
 
                    @Override
                    public String defaultReportTitle() {
                        return "容器规则报表";
                    }
                },
                response
        );
    }
 
    private Map<Long, String> buildAreaNameMap() {
        Map<Long, String> areaNameMap = new HashMap<>();
        for (WarehouseAreas area : warehouseAreasService.list()) {
            if (area != null && area.getId() != null) {
                areaNameMap.put(area.getId(), area.getName());
            }
        }
        return areaNameMap;
    }
 
    private Map<String, Object> buildExportRow(
            BasContainer record,
            List<ExcelUtil.ExportColumn> columns,
            Map<Long, String> areaNameMap
    ) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(record);
        Map<String, Object> row = new LinkedHashMap<>();
        for (ExcelUtil.ExportColumn column : columns) {
            Object value = resolveExportValue(record, column.getSource(), beanWrapper, areaNameMap);
            row.put(column.getSource(), value);
        }
        return row;
    }
 
    private Object resolveExportValue(
            BasContainer record,
            String source,
            BeanWrapper beanWrapper,
            Map<Long, String> areaNameMap
    ) {
        if ("containerTypeText".equals(source)) {
            return record.getContainerType$();
        }
        if ("areasText".equals(source)) {
            return buildAreasText(record, areaNameMap);
        }
        if ("status".equals(source)) {
            return record.getStatusBool() == null ? "" : (record.getStatusBool() ? "正常" : "冻结");
        }
        if ("createByText".equals(source)) {
            return record.getCreateBy$();
        }
        if ("createTimeText".equals(source)) {
            return record.getCreateTime$();
        }
        if ("updateByText".equals(source)) {
            return record.getUpdateBy$();
        }
        if ("updateTimeText".equals(source)) {
            return record.getUpdateTime$();
        }
        if (beanWrapper.isReadableProperty(source)) {
            return beanWrapper.getPropertyValue(source);
        }
        return null;
    }
 
    private String buildAreasText(BasContainer record, Map<Long, String> areaNameMap) {
        if (record == null || Cools.isEmpty(record.getAreas())) {
            return "";
        }
        List<String> labels = new ArrayList<>();
        for (Map<String, Object> area : record.getAreas()) {
            if (area == null) {
                continue;
            }
            Object idValue = area.get("id");
            if (idValue == null) {
                continue;
            }
            Long areaId = Long.valueOf(String.valueOf(idValue));
            String label = areaNameMap.get(areaId);
            if (Cools.isEmpty(label)) {
                label = String.valueOf(idValue);
            }
            labels.add(label);
        }
        return String.join("、", labels);
    }
 
}