|  | @ -1,19 +1,27 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.endpoint;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.serializer.SerializerFeature;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DeviceDetailDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.DeviceService;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DeviceDetail;
 | 
	
		
			
				|  |  | import io.swagger.annotations.Api;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiOperation;
 | 
	
		
			
				|  |  | import io.swagger.annotations.ApiParam;
 | 
	
		
			
				|  |  | import jxl.Sheet;
 | 
	
		
			
				|  |  | import jxl.Workbook;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  | import org.springframework.web.multipart.MultipartFile;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.io.InputStream;
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created with IntelliJ IDEA.
 | 
	
	
		
			
				|  | @ -31,6 +39,121 @@ public class DeviceController {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceService deviceService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceDetailDao deviceDetailDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @RequestMapping(value = "/importDeviceFromExcel", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
 | 
	
		
			
				|  |  |     @ResponseBody
 | 
	
		
			
				|  |  |     public String importFromExcel(HttpServletRequest request,@ApiParam(value = "文件", required = true)
 | 
	
		
			
				|  |  |     @RequestParam(value = "file", required = true) MultipartFile file) {
 | 
	
		
			
				|  |  |         List errorLs = new ArrayList<>();
 | 
	
		
			
				|  |  |         List correctLs = new ArrayList<>();
 | 
	
		
			
				|  |  |         Map<String, String> errorMsgMap = new HashMap<>();
 | 
	
		
			
				|  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
	
		
			
				|  |  |         List<String> deviceCodes = new ArrayList<>();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             request.setCharacterEncoding("UTF-8");
 | 
	
		
			
				|  |  |             InputStream inputStream = file.getInputStream();
 | 
	
		
			
				|  |  |             Workbook rwb = Workbook.getWorkbook(inputStream);
 | 
	
		
			
				|  |  |             Sheet[] sheets = rwb.getSheets();
 | 
	
		
			
				|  |  |             int rows;
 | 
	
		
			
				|  |  |             int row;
 | 
	
		
			
				|  |  |             String manufacturer=null;//厂商名称
 | 
	
		
			
				|  |  |             String deviceName; //设备名称
 | 
	
		
			
				|  |  |             String deviceModel; //设备型号
 | 
	
		
			
				|  |  |             String deviceCode;  //设备SN
 | 
	
		
			
				|  |  |             String sim;  //sim卡号
 | 
	
		
			
				|  |  |             String bindingCount; //单人{"1":"0"}  多人{"1":"0", "2":"0"}
 | 
	
		
			
				|  |  |             String deviceType; //设备种类  0健康设备 1安防设备
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             Sheet sheet = sheets[0];    //第一张表
 | 
	
		
			
				|  |  |             rows = sheet.getRows();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             for (int j = 1; j < rows; j++) {
 | 
	
		
			
				|  |  |                 if (sheet.getRow(j).length == 0) {
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 DeviceDetail deviceDetail = new DeviceDetail();
 | 
	
		
			
				|  |  |                 row = j;
 | 
	
		
			
				|  |  |                 manufacturer = sheet.getCell(0, row).getContents().trim();    //0 厂商名称
 | 
	
		
			
				|  |  |                 deviceName = sheet.getCell(1, row).getContents().trim();  //1 设备名称
 | 
	
		
			
				|  |  |                 deviceModel = sheet.getCell(2, row).getContents().trim();   //2 设备型号
 | 
	
		
			
				|  |  |                 deviceCode = sheet.getCell(3, row).getContents().trim(); //3 设备SN
 | 
	
		
			
				|  |  |                 sim = sheet.getCell(4, row).getContents().trim();  //4 sim卡号
 | 
	
		
			
				|  |  |                 bindingCount = sheet.getCell(5, row).getContents().trim(); //5
 | 
	
		
			
				|  |  |                 deviceType = sheet.getCell(6, row).getContents().trim(); //6 设备种类
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(deviceCode)){
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (deviceCodes.contains(deviceCode)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"设备SN码重复");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 deviceCodes.add(deviceCode);
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(manufacturer)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"厂商名称不能为空");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(deviceName)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"设备名称不能为空");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(deviceModel)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"设备型号不能为空");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(bindingCount)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"设备类型不能为空");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (StringUtils.isBlank(deviceType)){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"设备种类不能为空");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }else {
 | 
	
		
			
				|  |  |                     if ("健康设备".equals(deviceType)){
 | 
	
		
			
				|  |  |                         deviceType = "1";
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     else if ("安防设备".equals(deviceType)){
 | 
	
		
			
				|  |  |                         deviceType= "0";
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     else {
 | 
	
		
			
				|  |  |                         errorMsgMap.put(deviceCode,"不支持该设备种类");
 | 
	
		
			
				|  |  |                         continue;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 if (deviceDetailDao.findByDeviceCode(deviceCode).size()>0){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"该设备SN码已存在");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (StringUtils.isNotBlank(sim) && deviceDetailDao.findBySim(sim).size()>0){
 | 
	
		
			
				|  |  |                     errorMsgMap.put(deviceCode,"该sim卡号已存在");
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 deviceDetail.setApplyDate(sdf.format(new Date()));
 | 
	
		
			
				|  |  |                 deviceDetail.setManufacturer(manufacturer);
 | 
	
		
			
				|  |  |                 deviceDetail.setDeviceName(deviceName);
 | 
	
		
			
				|  |  |                 deviceDetail.setDeviceModel(deviceModel);
 | 
	
		
			
				|  |  |                 deviceDetail.setDeviceCode(deviceCode);
 | 
	
		
			
				|  |  |                 deviceDetail.setSim(sim);
 | 
	
		
			
				|  |  |                 deviceDetail.setBindingCount("");
 | 
	
		
			
				|  |  |                 deviceDetail.setDeviceType(Integer.parseInt(deviceType));
 | 
	
		
			
				|  |  |                 correctLs.add(deviceDetail);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             deviceDetailDao.save(correctLs);
 | 
	
		
			
				|  |  |             //包装导入结果(导入成功数量、错误对象集合)
 | 
	
		
			
				|  |  |             Map<String, Object> map = new HashMap<>();
 | 
	
		
			
				|  |  |             map.put("successNum", correctLs.size());
 | 
	
		
			
				|  |  |             map.put("failedNum", rows-1 - correctLs.size() );
 | 
	
		
			
				|  |  |             map.put("errorData", JSON.toJSONString(errorMsgMap, SerializerFeature.WriteMapNullValue));
 | 
	
		
			
				|  |  |             System.out.println(map);
 | 
	
		
			
				|  |  |             return success();
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             return error(-1, "操作失败!");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @GetMapping("test")
 | 
	
		
			
				|  |  |     public String test(){
 |