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
package cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService;
import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - MES 库区")
@RestController
@RequestMapping("/mes/wm/warehouse-location")
@Validated
public class MesWmWarehouseLocationController {
 
    @Resource
    private MesWmWarehouseLocationService locationService;
    @Resource
    private MesWmWarehouseService warehouseService;
    @Resource
    private MesWmWarehouseAreaService areaService;
 
    @PostMapping("/create")
    @Operation(summary = "创建库区")
    @PreAuthorize("@ss.hasPermission('mes:wm-warehouse:create')")
    public CommonResult<Long> createWarehouseLocation(@Valid @RequestBody MesWmWarehouseLocationSaveReqVO createReqVO) {
        return success(locationService.createWarehouseLocation(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新库区")
    @PreAuthorize("@ss.hasPermission('mes:wm-warehouse:update')")
    public CommonResult<Boolean> updateWarehouseLocation(@Valid @RequestBody MesWmWarehouseLocationSaveReqVO updateReqVO) {
        locationService.updateWarehouseLocation(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除库区")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:wm-warehouse:delete')")
    public CommonResult<Boolean> deleteWarehouseLocation(@RequestParam("id") Long id) {
        locationService.deleteWarehouseLocation(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得库区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MesWmWarehouseLocationRespVO> getWarehouseLocation(@RequestParam("id") Long id) {
        MesWmWarehouseLocationDO location = locationService.getWarehouseLocation(id);
        if (location == null) {
            return success(null);
        }
        return success(buildWarehouseLocationRespVOList(Collections.singletonList(location)).get(0));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得库区分页")
    public CommonResult<PageResult<MesWmWarehouseLocationRespVO>> getWarehouseLocationPage(@Valid MesWmWarehouseLocationPageReqVO pageReqVO) {
        PageResult<MesWmWarehouseLocationDO> pageResult = locationService.getWarehouseLocationPage(pageReqVO);
        return success(new PageResult<>(buildWarehouseLocationRespVOList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @GetMapping("/simple-list")
    @Operation(summary = "获得库区精简列表", description = "可按仓库过滤")
    public CommonResult<List<MesWmWarehouseLocationRespVO>> getWarehouseLocationSimpleList(
            @Parameter(name = "warehouseId", description = "仓库编号") @RequestParam(value = "warehouseId", required = false) Long warehouseId) {
        List<MesWmWarehouseLocationDO> list = locationService.getWarehouseLocationList(warehouseId);
        return success(buildWarehouseLocationRespVOList(list));
    }
 
    @PutMapping("/update-by-location-id")
    @Operation(summary = "批量设置库区下所有库位的混放规则")
    @PreAuthorize("@ss.hasPermission('mes:wm-warehouse:update')")
    public CommonResult<Boolean> updateAreaByLocationId(
            @RequestParam("locationId") Long locationId,
            @RequestParam(value = "allowItemMixing", required = false) Boolean allowItemMixing,
            @RequestParam(value = "allowBatchMixing", required = false) Boolean allowBatchMixing) {
        areaService.updateByLocationId(locationId, allowItemMixing, allowBatchMixing);
        return success(true);
    }
 
    @GetMapping("/get-import-template")
    @Operation(summary = "获得库区导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MesWmWarehouseLocationImportExcelVO> list = Collections.singletonList(
                MesWmWarehouseLocationImportExcelVO.builder()
                        .code("L001").name("示例库区").warehouseName("示例仓库")
                        .area(new java.math.BigDecimal("500.00")).frozen(false).remark("").build()
        );
        // 输出
        ExcelUtils.write(response, "库区导入模板.xls", "库区列表", MesWmWarehouseLocationImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入库区")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('mes:wm-warehouse:import')")
    public CommonResult<MesWmWarehouseLocationImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                                        @RequestParam(value = "updateSupport", required = false,
                                                                                defaultValue = "false") Boolean updateSupport) throws Exception {
        List<MesWmWarehouseLocationImportExcelVO> list = ExcelUtils.read(file, MesWmWarehouseLocationImportExcelVO.class);
        return success(locationService.importWarehouseLocationList(list, updateSupport));
    }
 
    // ==================== 拼接 VO ====================
 
    /**
     * 批量构建库区响应 VO 列表(填充仓库名称)
     *
     * @param list 库区列表
     * @return 响应 VO 列表
     */
    private List<MesWmWarehouseLocationRespVO> buildWarehouseLocationRespVOList(List<MesWmWarehouseLocationDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1. 获得仓库信息
        Map<Long, MesWmWarehouseDO> warehouseMap = warehouseService.getWarehouseMap(convertSet(list, MesWmWarehouseLocationDO::getWarehouseId));
        // 2. 构建结果
        return BeanUtils.toBean(list, MesWmWarehouseLocationRespVO.class, vo ->
                MapUtils.findAndThen(warehouseMap, vo.getWarehouseId(), warehouse -> vo.setWarehouseName(warehouse.getName())));
    }
 
}