package com.mh.user.controller; import com.mh.common.http.HttpResult; import com.mh.user.annotation.SysLogger; import com.mh.user.entity.BuildingEntity; import com.mh.user.model.BuildingModel; import com.mh.user.service.BuildingService; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellType; 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.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("building") public class BuildingController { @Autowired private BuildingService buildingService; //保存 @SysLogger(title="楼栋信息",optDesc = "保存楼栋信息") @PostMapping(value="/save") public HttpResult saveBuilding(@RequestBody BuildingEntity buildingEntity) { try{ return HttpResult.ok(buildingService.saveBuilding(buildingEntity)); }catch (Exception e){ e.printStackTrace(); return HttpResult.error(); } } //修改 @SysLogger(title="楼栋信息",optDesc = "修改楼栋信息") @PostMapping(value="/update") public HttpResult updateBuilding(@RequestBody BuildingEntity buildingEntity) { return HttpResult.ok("success",buildingService.updateBuilding(buildingEntity)); } //查询所有 @SysLogger(title="楼栋信息",optDesc = "查询楼栋信息") @PostMapping(value = "/query") public HttpResult queryBuilding(@RequestParam(value = "buildingId", required = false)String buildingId, @RequestParam(value= "page", required=true)Integer page, @RequestParam(value= "limit", required=true)Integer limit) { try{ int count=buildingService.getCount(buildingId, page,limit); List records=buildingService.queryBuilding(buildingId, page,limit); return HttpResult.ok(count,records); }catch (Exception e){ e.printStackTrace(); return HttpResult.error(); } } //查询楼栋名称 @PostMapping(value="/name") public HttpResult selectBuildingName() { try{ List list=buildingService.selectBuildingName(); return HttpResult.ok(list); }catch (Exception e){ e.printStackTrace(); return HttpResult.error(); } } // 删除多 @PostMapping(value="/deletes") public HttpResult deleteDevices(@RequestBody List records) { return HttpResult.ok(buildingService.deleteBuilding(records)); } // 删除单个 @SysLogger(title="楼栋信息",optDesc = "删除楼栋信息") @PostMapping(value="/delete") public HttpResult deleteDevice(@RequestParam String id ) { return HttpResult.ok(buildingService.deleteBuilding(id)); } // 查询编号 @PostMapping(value="/selectBuildingId") public HttpResult selectBuildingId(@RequestParam String buildingName ) { String id=buildingService.selectBuildingId(buildingName); return HttpResult.ok(id); } // 资料批量上传 @SysLogger(title="楼栋信息",optDesc = "批量导入楼栋信息") @PostMapping("/import_building") public HttpResult importExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest req) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); HttpResult httpResult=new HttpResult(); try { int is=0; //判断是否有重复 String msg=""; List a = new ArrayList(); InputStream inputStream = file.getInputStream(); //创建工作簿 //如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream); System.out.println("xssfWorkbook对象:" + hssfWorkbook); //读取第一个工作表 HSSFSheet sheet = hssfWorkbook.getSheetAt(0); System.out.println("sheet对象:" + sheet); //获取最后一行的num,即总行数。此处从0开始计数 int maxRow = sheet.getLastRowNum(); System.out.println("总行数为:" + maxRow); if (maxRow >= 500){ msg = "总行数不能超出500行"; httpResult.setMsg(msg); httpResult.setCode(500); return httpResult; } if (maxRow == 0){ msg = "请先录入数据到excel文件"; httpResult.setMsg(msg); httpResult.setCode(500); return httpResult; } // 创建数组集合 List uploadEntityList = new ArrayList<>(); List deviceList = new ArrayList<>(); for (int row = 1; row <= maxRow; row++) { //获取最后单元格num,即总单元格数 ***注意:此处从1开始计数*** int maxRol = sheet.getRow(row).getLastCellNum(); System.out.println("总列数为:" + maxRol); System.out.println("--------第" + row + "行的数据如下--------"); for (int rol = 0; rol < maxRol; rol++){ String sCell; if (sheet.getRow(row).getCell(rol) == null){ sCell=""; }else{ HSSFCell cell = sheet.getRow(row).getCell(rol); cell.setCellType(CellType.STRING); sCell = cell.getStringCellValue(); } sCell = sCell.trim(); //去首尾空格 sCell = sCell.replaceAll(" ", ""); //去掉所有空格,包括首尾、中间 sCell = sCell.replaceAll("\\s*", ""); //可以替换大部分空白字符, 不限于空格,\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 System.out.print(sCell + " "); deviceList.add(sCell); String rolName = ""; switch (rol){ case 1 : rolName = "楼栋名称"; break; case 2 : rolName = "楼层数"; break; case 3 : rolName = "起始楼层"; break; case 4 : rolName = "每层宿舍数"; break; case 5 : rolName = "床位数"; break; case 6 : rolName = "实际入住数"; break; case 7 : rolName = "水箱高度"; break; case 8 : rolName = "水箱高度(低区)"; break; case 9 : rolName = "热泵个数"; break; } if ((rol >= 1)&&(rol <= 4)&&(sCell.equals(""))){ msg = rolName + "不能为空" ; httpResult.setMsg(msg); httpResult.setCode(500); return httpResult; } } // 创建实体类 BuildingEntity uploadEntity = new BuildingEntity(); uploadEntity.setBuildingName(deviceList.get(0));//楼栋名称 uploadEntity.setLevelsCount(Integer.parseInt(deviceList.get(1))); //楼层数 uploadEntity.setBeginLevel(Integer.parseInt(deviceList.get(2))); //起始楼层 uploadEntity.setHouseCount(Integer.parseInt(deviceList.get(3))); //每层宿舍数 uploadEntity.setBedCount(Integer.parseInt(deviceList.get(4))); //床位数 uploadEntity.setCheckInCount(Integer.parseInt(deviceList.get(5))); //实际入住数 uploadEntity.setTankHeight(Double.parseDouble(deviceList.get(6))); //默认(高区)水箱高度 uploadEntity.setLowTankHeight(Double.parseDouble(deviceList.get(7))); //低区水箱高度 uploadEntity.setPumpCount(Integer.parseInt(deviceList.get(8))); //热泵个数 deviceList.clear(); uploadEntityList.add(uploadEntity); is=buildingService.selectCount(uploadEntity.getBuildingName()); if (is>0){ httpResult.setMsg("楼栋名称有重复!"); httpResult.setCode(500); // return httpResult; } } if (is==0){ for (BuildingEntity buildingEntity:uploadEntityList){ buildingService.saveBuilding(buildingEntity); } httpResult.setMsg("success"); httpResult.setCode(200); return httpResult; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return httpResult; } // 查询楼栋热泵数目 @PostMapping(value="/pumpCount") public HttpResult selectPumpCount(@RequestParam String buildingId ) { int count=buildingService.selectPumpCount(buildingId); return HttpResult.ok("success",count); } }