DeviceMgrController.java
3.82 KB
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
package com.glsx.controller;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.com.glsx.openplatform.service.DeviceMgrService;
import cn.com.glsx.openplatform.vo.DeviceImportVo;
import cn.com.glsx.openplatform.vo.DeviceVo;
import cn.com.glsx.openplatform.vo.R;
import cn.hutool.db.Page;
import com.glsx.poi.util.ExcelUtil;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
//@Api
@RestController
@RequestMapping(value = "/devicemgr")
public class DeviceMgrController {
/**
* 日志
*/
private final static Logger LOGGER = LoggerFactory.getLogger(DeviceMgrController.class);
@Autowired
private DeviceMgrService deviceMgrService;
@RequestMapping(value = "/getDeviceMgrList", method = RequestMethod.GET)
@ResponseBody
public R getDeviceMgrList(Page page, DeviceVo deviceVo) {
return deviceMgrService.getDeviceMgrList(page,deviceVo);
}
@RequestMapping(value = "/getDropDownOptions", method = RequestMethod.GET)
@ResponseBody
public R getDropDownOptions() {
HashMap<String, List<String>> hashMap = new HashMap<>();
List<String> deviceTypeList = deviceMgrService.getDeviceTypeList();
hashMap.put("deviceTypeList", deviceTypeList);
return R.ok().data(hashMap);
}
/**
* 导入设备
*
* @param httpRequest
* @return
*/
@RequestMapping(value = "/importDeviceList", method = RequestMethod.POST)
@ResponseBody
public R importDeviceList(HttpServletRequest httpRequest){
Map<String, Object> map = new HashMap<>();
String message;
try {
//加载导入的文件
InputStream input = unloadFile(httpRequest, "file");
//加载导入数据
ExcelUtil<DeviceImportVo> excelUtil = new ExcelUtil<DeviceImportVo>(DeviceImportVo.class);
Collection<DeviceImportVo> list = excelUtil.importExcel(input);
Map<String, Object> desimap = deviceMgrService.importDevice(list);
int successCount = desimap.get("successCount") == null ? 0 : Integer.parseInt(desimap.get("successCount") + "");
map.put("successCount",successCount);
map.put("errorCount", list.size() - successCount);
map.put("errorMessage",desimap.get("errorMessage"));
return R.ok().data(map);
} catch (OfficeXmlFileException e) {
LOGGER.info("异常信息,{}",e);
message="上传文件出错,无法解析参数,请检查文件是否有误";
} catch (Exception e) {
LOGGER.info("异常信息,{}",e);
message="系统异常,请联系管理员处理";
}
return R.error(message);
}
/**
* 导入文件
*
* @param request
* @return
* @throws Exception
*/
public InputStream unloadFile(HttpServletRequest request, String fileName) throws Exception {
//加载导入文件
MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
MultipartFile file = mulRequest.getFile(fileName);
String filename = file.getOriginalFilename();
if (filename == null || "".equals(filename)) {
return null;
}
return file.getInputStream();
}
}