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);
|
}
|
|
}
|