DeviceMgrController.java 3.82 KB
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();
    }
}