#
zhou zhou
3 天以前 b5b400a615743a74e9d127261bd3785554aa06aa
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
package com.vincent.rsf.server.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.common.utils.Utils;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
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.manager.entity.MatnrGroup;
import com.vincent.rsf.server.manager.entity.Warehouse;
import com.vincent.rsf.server.manager.entity.WarehouseAreas;
import com.vincent.rsf.server.manager.service.WarehouseAreasService;
import com.vincent.rsf.server.manager.service.WarehouseService;
import com.vincent.rsf.server.system.controller.param.RoleScopeParam;
import com.vincent.rsf.server.system.entity.PdaRoleMenu;
import com.vincent.rsf.server.system.entity.WarehouseRoleMenu;
import com.vincent.rsf.server.system.service.WarehouseRoleMenuService;
import com.vincent.rsf.server.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
public class WarehouseRoleMenuController extends BaseController {
 
    @Autowired
    private WarehouseRoleMenuService warehouseRoleMenuService;
 
    @Autowired
    private WarehouseAreasService warehouseAreasService;
 
    @Autowired
    private WarehouseService warehouseService;
 
    @GetMapping("/roleWarehouse/scope/list")
    public R scopeList(@RequestParam Long roleId) {
        return R.ok().add(warehouseRoleMenuService.listStrictlyMenuByRoleId(roleId));
    }
 
    @PreAuthorize("hasAuthority('system:role:update')")
    @OperationLog("Assign Permissions")
    @PostMapping("/roleWarehouse/scope/update")
    @Transactional
    public R scopeUpdate(@RequestBody RoleScopeParam param) {
        Long roleId = param.getId();
        List<Long> menuIds = new ArrayList<>(param.getMenuIds().getChecked());
        menuIds.addAll(param.getMenuIds().getHalfChecked());
        warehouseRoleMenuService
                .remove(new LambdaQueryWrapper<WarehouseRoleMenu>().eq(WarehouseRoleMenu::getRoleId, roleId));
        for (Long menuId : menuIds) {
            if (!warehouseRoleMenuService.save(new WarehouseRoleMenu(roleId, menuId))) {
                throw new CoolException("Internal Server Error!");
            }
        }
        return R.ok("Assign Success");
    }
 
    @PostMapping("/menuWarehouse/tree")
    public R tree(@RequestBody Map<String, Object> map) {
        // 查询所有仓库
        List<Warehouse> warehouseList = warehouseService
                .list(new LambdaQueryWrapper<Warehouse>().orderByAsc(Warehouse::getId));
        // 查询所有库区
        List<WarehouseAreas> areasList = warehouseAreasService
                .list(new LambdaQueryWrapper<WarehouseAreas>().orderByAsc(WarehouseAreas::getId));
 
        // 按仓库ID分组库区
        Map<Long, List<WarehouseAreas>> areasMap = areasList.stream()
                .collect(java.util.stream.Collectors.groupingBy(WarehouseAreas::getWarehouseId));
 
        // 构建树形结构:将库区设置为仓库的children
        for (Warehouse warehouse : warehouseList) {
            List<WarehouseAreas> children = areasMap.getOrDefault(warehouse.getId(), new ArrayList<>());
            // 仓库使用100000+id,避免与库区id重复
            warehouse.setId(100000L + warehouse.getId());
            warehouse.setFlagWare(1); // 1表示仓库
            // 库区保持原id不变
            for (WarehouseAreas area : children) {
                area.setFlagWare(0); // 0表示库区
            }
            warehouse.setChildren(children);
        }
 
        // 条件过滤
        if (!Cools.isEmpty(map.get("condition"))) {
            String condition = String.valueOf(map.get("condition"));
            // 过滤仓库名称和库区名称
            warehouseList.removeIf(warehouse -> {
                // 先过滤库区
                if (warehouse.getChildren() != null) {
                    warehouse.getChildren()
                            .removeIf(area -> area.getName() != null && !area.getName().contains(condition));
                }
                // 如果仓库名称不匹配且没有匹配的库区,则移除该仓库
                boolean warehouseMatch = warehouse.getName() != null && warehouse.getName().contains(condition);
                boolean hasMatchingChildren = warehouse.getChildren() != null && !warehouse.getChildren().isEmpty();
                return !warehouseMatch && !hasMatchingChildren;
            });
        }
 
        return R.ok().add(warehouseList);
    }
 
}