自动化立体仓库 - WMS系统
chen.llin
2 天以前 2816415f539ef54839e331657edae7055c243ad8
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.zy.asrs.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.zy.asrs.entity.AppVersion;
import com.zy.asrs.service.AppVersionService;
import com.zy.common.web.BaseController;
import com.zy.system.entity.Permission;
import com.zy.system.entity.RolePermission;
import com.zy.system.service.PermissionService;
import com.zy.system.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@RestController
public class AppVersionController extends BaseController {
 
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private AppVersionService appVersionService;
 
    @RequestMapping(value = "/appVersion/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") String id) {
        return R.ok(appVersionService.selectById(String.valueOf(id)));
    }
 
    @RequestMapping(value = "/appVersion/list/auth")
    @ManagerAuth
    public R list(@RequestParam(defaultValue = "1") Integer curr,
                  @RequestParam(defaultValue = "10") Integer limit,
                  @RequestParam(required = false) String orderByField,
                  @RequestParam(required = false) String orderByType,
                  @RequestParam(required = false) String condition,
                  @RequestParam Map<String, Object> param) {
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        excludeTrash(param);
        convert(param, wrapper);
        allLike(AppVersion.class, param.keySet(), wrapper, condition);
        if (!Cools.isEmpty(orderByField)) {
            wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));
        }
        return R.ok(appVersionService.selectPage(new Page<>(curr, limit), wrapper));
    }
 
    @RequestMapping(value = "/appVersion/checkUpdate/{version}/{type}")
    public R checkUpdate(@PathVariable("version") String version,
                         @PathVariable("type") Integer type) {
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        AppVersion appVersion = appVersionService.selectOne(wrapper);
        if (Cools.isEmpty(appVersion)) {
            return R.ok("已是最新版本");
        }
        AppVersion latestApp = appVersionService.getLatestApp(type);
        if (latestApp == null) {
            return R.error();
        }
 
        if (latestApp.getVersion().equals(version)) {
            return R.ok("已是最新版本");
        }
 
        return R.ok("有新版本,需要更新").add(latestApp);
    }
 
    private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String val = String.valueOf(entry.getValue());
            if (val.contains(RANGE_TIME_LINK)) {
                String[] dates = val.split(RANGE_TIME_LINK);
                wrapper.ge(entry.getKey(), DateUtils.convert(dates[0]));
                wrapper.le(entry.getKey(), DateUtils.convert(dates[1]));
            } else {
                wrapper.like(entry.getKey(), val);
            }
        }
    }
 
    @RequestMapping(value = "/appVersion/add/auth")
    @ManagerAuth
    public R add(AppVersion appVersion) {
        if (appVersion.getLatest() == 1) {
            //修改最新版本时,需要将其他版本设置为非最新版
            appVersionService.updateLatest(0, appVersion.getType());
        }
        appVersionService.insert(appVersion);
        return R.ok();
    }
 
    @RequestMapping(value = "/appVersion/update/auth")
    @ManagerAuth
    public R update(AppVersion appVersion) {
        if (Cools.isEmpty(appVersion) || null == appVersion.getId()) {
            return R.error();
        }
        if (appVersion.getLatest() == 1) {
            //修改最新版本时,需要将其他版本设置为非最新版
            appVersionService.updateLatest(0, appVersion.getType());
        }
        appVersionService.updateById(appVersion);
        return R.ok();
    }
 
    @RequestMapping(value = "/appVersion/delete/auth")
    @ManagerAuth
    public R delete(@RequestParam(value = "ids[]") Long[] ids) {
        for (Long id : ids) {
            appVersionService.deleteById(id);
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/appVersion/export/auth")
    @ManagerAuth
    public R export(@RequestBody JSONObject param) {
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
        Map<String, Object> map = excludeTrash(param.getJSONObject("appVersion"));
        convert(map, wrapper);
        List<AppVersion> list = appVersionService.selectList(wrapper);
        return R.ok(exportSupport(list, fields));
    }
 
    @RequestMapping(value = "/appVersionQuery/auth")
    @ManagerAuth
    public R query(String condition) {
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        wrapper.like("id", condition);
        Page<AppVersion> page = appVersionService.selectPage(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (AppVersion appVersion : page.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", appVersion.getId());
            map.put("value", appVersion.getId());
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping(value = "/appVersion/check/column/auth")
    @ManagerAuth
    public R query(@RequestBody JSONObject param) {
        Wrapper<AppVersion> wrapper = new EntityWrapper<AppVersion>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
        if (null != appVersionService.selectOne(wrapper)) {
            return R.parse(BaseRes.REPEAT).add(getComment(AppVersion.class, String.valueOf(param.get("key"))));
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/appVersion/uploadApp/auth")
    @ManagerAuth
    public R uploadApp(@RequestParam("id") Integer id,
                       @RequestParam("file") MultipartFile[] files) {
        AppVersion appVersion = appVersionService.selectById(id);
 
        MultipartFile file = files[0];
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String path = ClassUtils.getDefaultClassLoader().getResource("appVersion").getPath();
        //文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //上传文件名
        String filename = format.format(new Date()) + suffix;
        //最终文件路径
        String filepath = path + "/" + filename;
 
 
        //服务器端保存的文件对象
        File serverFile = new File(filepath);
        if (!serverFile.exists()) {
            try {
                //创建文件
                serverFile.createNewFile();
                //将上传的文件写入到服务器端文件内
                file.transferTo(serverFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        //保存文件名
        appVersion.setPath(filename);
        appVersionService.updateById(appVersion);
        return R.ok();
    }
 
    @RequestMapping("/appVersion/downloadApp/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) {
        try {
            ClassPathResource pathResource = new ClassPathResource("appVersion/" + filename);
            File file = pathResource.getFile();
            InputStream inputStream = pathResource.getInputStream();
            //输出文件
            InputStream fis = new BufferedInputStream(inputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
 
            //获取文件的名字再浏览器下载页面
            String name = file.getName();
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            out.write(buffer);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(404);
        }
    }
    @RequestMapping("/menu/pda/auth")
    @ManagerAuth
    public R menuPda(){
        // 直接从 sys_permission 表读取所有菜单,不检查权限分配
        // 查询所有状态为1(正常)的权限
        List<Permission> permissions = permissionService.selectList(
            new EntityWrapper<Permission>()
                .eq("status", 1)  // 只返回正常状态的权限
        );
        
        // 检查是否有层级结构(父菜单:action为空字符串,子菜单:resource_id指向父菜单的permission.id)
        // 查询所有父菜单(action为空字符串的权限)
        List<Permission> parentMenus = permissions.stream()
            .filter(p -> p.getAction() == null || p.getAction().isEmpty())
            .collect(Collectors.toList());
        
        // 收集所有父菜单ID(用于过滤独立菜单)
        java.util.Set<Long> parentMenuIds = parentMenus.stream()
            .map(Permission::getId)
            .collect(Collectors.toSet());
        
        if (!parentMenus.isEmpty()) {
            // 构建层级结构
            List<Map<String, Object>> result = new ArrayList<>();
            
            for (Permission parentMenu : parentMenus) {
                // 查找该父菜单下的子菜单
                // 方式1:resource_id指向父菜单的id
                // 方式2:resource_id为0或null,但根据action路径匹配父菜单
                List<Permission> children = permissions.stream()
                    .filter(p -> {
                        // 排除父菜单本身
                        if (p.getAction() == null || p.getAction().isEmpty()) {
                            return false;
                        }
                        // 方式1:resource_id指向父菜单的id
                        if (p.getResourceId() != null && p.getResourceId().equals(parentMenu.getId())) {
                            return true;
                        }
                        // 方式2:resource_id为0或null,根据action路径匹配
                        if ((p.getResourceId() == null || p.getResourceId() == 0)) {
                            String action = p.getAction();
                            String parentName = parentMenu.getName();
                            // 根据父菜单名称匹配action路径
                            if ("入库管理".equals(parentName)) {
                                return action != null && (action.startsWith("/pakin/") || action.startsWith("/order/"));
                            } else if ("AGV管理".equals(parentName)) {
                                return action != null && action.startsWith("/AGV/");
                            } else if ("库存管理".equals(parentName)) {
                                return action != null && action.startsWith("/stock/");
                            }
                        }
                        return false;
                    })
                    .collect(Collectors.toList());
                
                // 只有有子菜单的父菜单才返回
                if (!children.isEmpty()) {
                    Map<String, Object> parentMap = new HashMap<>();
                    parentMap.put("id", parentMenu.getId());
                    parentMap.put("name", parentMenu.getName());
                    parentMap.put("action", parentMenu.getAction());
                    parentMap.put("type", "parent"); // 标识为父菜单
                    
                    // 构建子菜单列表
                    List<Map<String, Object>> childrenList = new ArrayList<>();
                    for (Permission child : children) {
                        Map<String, Object> childMap = new HashMap<>();
                        childMap.put("id", child.getId());
                        childMap.put("name", child.getName());
                        childMap.put("action", child.getAction());
                        childMap.put("type", "child"); // 标识为子菜单
                        childrenList.add(childMap);
                    }
                    parentMap.put("children", childrenList);
                    result.add(parentMap);
                }
            }
            
            // 添加没有父菜单的独立权限(resource_id为NULL的权限)
            List<Permission> standalonePermissions = permissions.stream()
                .filter(p -> {
                    // 只返回有action的权限(排除父菜单)
                    if (p.getAction() == null || p.getAction().isEmpty()) {
                        return false;
                    }
                    // 如果resource_id为NULL,说明是独立菜单
                    return p.getResourceId() == null;
                })
                .collect(Collectors.toList());
            
            for (Permission permission : standalonePermissions) {
                Map<String, Object> item = new HashMap<>();
                item.put("id", permission.getId());
                item.put("name", permission.getName());
                item.put("action", permission.getAction());
                item.put("type", "standalone"); // 独立菜单
                result.add(item);
            }
            
            return R.ok().add(result);
        }
        
        // 如果没有层级结构,返回原来的平铺结构(兼容旧逻辑)
        return R.ok().add(permissions);
    }
}