| | |
| | | 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.utils.ExcelUtil; |
| | | 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 javax.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.util.*; |
| | | |
| | | @RestController |
| | |
| | | @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)); |
| | | for (BasContainer container : page.getRecords()) { |
| | | if (!Cools.isEmpty(container.getAreas())) { |
| | | String content = container.getAreas().substring(1, container.getAreas().length() - 1); |
| | | String[] parts = content.split(","); |
| | | Long[] longArray = new Long[parts.length]; |
| | | for (int i = 0; i < parts.length; i++) { |
| | | longArray[i] = Long.parseLong(parts[i].trim()); |
| | | } |
| | | container.setAreaIds(longArray); |
| | | } |
| | | |
| | | } |
| | | return R.ok().add(page); |
| | | } |
| | | |
| | |
| | | @GetMapping("/basContainer/{id}") |
| | | public R get(@PathVariable("id") Long id) { |
| | | BasContainer basContainer = basContainerService.getById(id); |
| | | String content = basContainer.getAreas().substring(1, basContainer.getAreas().length() - 1); |
| | | String[] parts = content.split(","); |
| | | Long[] longArray = new Long[parts.length]; |
| | | for (int i = 0; i < parts.length; i++) { |
| | | longArray[i] = Long.parseLong(parts[i].trim()); |
| | | // 确保返回的areas按sort字段排序 |
| | | if (basContainer != null) { |
| | | basContainer.sortAreas(); |
| | | } |
| | | basContainer.setAreaIds(longArray); |
| | | return R.ok().add(basContainer); |
| | | } |
| | | |
| | |
| | | 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 (null !=basContainer.getAreaIds()){ |
| | | basContainer.setAreas(Arrays.toString(basContainer.getAreaIds())); |
| | | } |
| | | if (!basContainerService.save(basContainer)) { |
| | | return R.error("Save Fail"); |
| | |
| | | public R update(@RequestBody BasContainer basContainer) { |
| | | basContainer.setUpdateBy(getLoginUserId()); |
| | | basContainer.setUpdateTime(new Date()); |
| | | if (null !=basContainer.getAreaIds()){ |
| | | basContainer.setAreas(Arrays.toString(basContainer.getAreaIds())); |
| | | } |
| | | |
| | | // 确保areas按sort字段排序 |
| | | basContainer.sortAreas(); |
| | | |
| | | if (!basContainerService.updateById(basContainer)) { |
| | | return R.error("Update Fail"); |
| | | } |
| | |
| | | @PreAuthorize("hasAuthority('manager:basContainer:list')") |
| | | @PostMapping("/basContainer/export") |
| | | public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception { |
| | | ExcelUtil.build(ExcelUtil.create(basContainerService.list(), BasContainer.class), response); |
| | | 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); |
| | | } |
| | | |
| | | } |