package com.mh.user.controller; import com.mh.common.http.HttpResult; import com.mh.common.utils.StringUtils; import com.mh.user.annotation.BusinessType; import com.mh.user.annotation.SysLogger; import com.mh.user.entity.BuildingEntity; import com.mh.user.entity.DeviceInstallEntity; import com.mh.user.model.DeviceModel; import com.mh.user.service.BuildingService; import com.mh.user.service.DealDataService; import com.mh.user.service.DeviceInstallService; import com.mh.user.service.SummaryService; 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.*; @RestController @RequestMapping("device") public class DeviceInstallController { @Autowired private DeviceInstallService deviceInstallService; @Autowired BuildingService buildingService; private final DealDataService dealDataService; public DeviceInstallController(DealDataService dealDataService) { this.dealDataService = dealDataService; } //保存 @SysLogger(title="基表信息",optDesc = "保存基表信息") @PostMapping(value="/save") public HttpResult saveDevice(@RequestBody DeviceInstallEntity deviceInstallEntity) { return HttpResult.ok(deviceInstallService.saveDevice(deviceInstallEntity)); } //修改 @SysLogger(title="基表信息",optDesc = "修改基表信息") @PostMapping(value="/update") public HttpResult updateDevice(@RequestBody DeviceInstallEntity deviceInstallEntity) { // 根据id查询对应的deviceInstall DeviceInstallEntity oldEntity = deviceInstallService.selectDeviceById(deviceInstallEntity.getId()); // 删除全部的device_code_param值 deviceInstallService.deleteParamCode(oldEntity); // 设置校验位 if (StringUtils.isBlank(deviceInstallEntity.getParity())) { deviceInstallEntity.setParity(oldEntity.getParity()); } // 在创建新的device_code_param值 deviceInstallService.createParamCode(deviceInstallEntity); // 更新device_install deviceInstallService.updateDevice(deviceInstallEntity); String isUse=""; if (deviceInstallEntity.isUse()){ isUse="1"; }else{ isUse="0"; } String deviceAddr=deviceInstallEntity.getDeviceAddr(); String deviceType=deviceInstallEntity.getDeviceType(); String buildingId=deviceInstallEntity.getBuildingId(); deviceInstallService.updateIsUse(isUse,deviceAddr,deviceType,buildingId); deviceInstallService.updateIsUse2(isUse,deviceAddr,deviceType,buildingId); deviceInstallService.updateIsUse3(isUse,deviceAddr,deviceType,buildingId); return HttpResult.ok(); } //查询所有 @SysLogger(title="基表信息",optDesc = "查询基表信息") @PostMapping(value = "/getAll") public HttpResult getAllDevice(@RequestParam int page, @RequestParam int limit) { Map map=new HashMap<>(); map.put("count",deviceInstallService.getAllCount()); //记录数 map.put("data",deviceInstallService.getAllDevice(page,limit)); //数据集 return HttpResult.ok(map); } // 删除多 @PostMapping(value="/deletes") public HttpResult delete(@RequestBody List records) { return HttpResult.ok(deviceInstallService.deleteDevice(records)); } // 删除单个 @SysLogger(title="基表信息",optDesc = "删除基表信息") @PostMapping(value="/delete") public HttpResult delete(@RequestParam String id ) { return HttpResult.ok(deviceInstallService.deleteDevice(id)); } // 按条件查询 @SysLogger(title="基表信息",optDesc = "按条件查询基表信息") @PostMapping(value="/query") public HttpResult queryDevice( @RequestParam(value = "buildingId", required = false)String buildingId, @RequestParam(value = "deviceType", required = false)String deviceType, @RequestParam(value = "startDate", required = false)String startDate, @RequestParam(value = "endDate", required = false)String endDate, @RequestParam(value = "isOnline", required=false)String isOnline, @RequestParam(value = "isUse", required=false)String isUse, @RequestParam(value = "isFault", required=false)String isFault, @RequestParam(value = "page", required=true)Integer page, @RequestParam(value = "limit", required=true)Integer limit) { try{ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String curDate=sdf1.format(date); dealDataService.proDeviceState(curDate); //刷新统计设备状态数据 int count=deviceInstallService.getCount(buildingId,deviceType, startDate, endDate,isOnline,isUse,isFault, page,limit); List records=deviceInstallService.queryDevice(buildingId,deviceType, startDate, endDate,isOnline,isUse,isFault, page,limit); return HttpResult.ok(count,records); }catch (Exception e){ //e.printStackTrace(); return HttpResult.error(); } } //查询设备在线情况 @PostMapping(value="/getIsOnline") public HttpResult getIsOnlineCount(){ try { Map map = new HashMap<>(); int electCount = deviceInstallService.getIsOnlineCount("在线", "电表"); int wtCount = deviceInstallService.getIsOnlineCount("在线", "水表"); int pumpCount = deviceInstallService.getIsOnlineCount("在线", "热泵"); int pressCount = deviceInstallService.getIsOnlineCount("在线", "压变"); int deviceCount = deviceInstallService.getAllCount(); map.put("electCount", electCount); map.put("wtCount", wtCount); map.put("pumpCount", pumpCount); map.put("pressCount", pressCount); map.put("deviceCount", deviceCount); return HttpResult.ok(map); } catch(Exception e){ return HttpResult.error(); } } // 资料批量上传 @PostMapping("/import_devices") public HttpResult importExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest req) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); HttpResult httpResult=new HttpResult(); String token = req.getHeader("token"); try { int is=0; //判断是否有重复表号 String msg=""; List a = new ArrayList(); InputStream inputStream = file.getInputStream(); //创建工作簿 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; } //先清临时表 // deviceInstallService.deleteDevice_install_temp(); // 创建数组集合 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; case 10 : rolName = "所属楼栋"; break; } if ((rol >= 1)&&(rol <= 4)&&(sCell.equals(""))){ msg = rolName + "不能为空" ; httpResult.setMsg(msg); httpResult.setCode(500); return httpResult; } } // 创建实体类 DeviceInstallEntity uploadEntity=new DeviceInstallEntity(); uploadEntity.setDeviceAddr(deviceList.get(0));//通讯编号 uploadEntity.setDeviceName(deviceList.get(1));//设备名称 uploadEntity.setDeviceType(deviceList.get(2));//设备类型 uploadEntity.setDataCom(deviceList.get(3));//通讯端口 uploadEntity.setBaudRate(Integer.parseInt(deviceList.get(4)));//波特率 uploadEntity.setRatio(Double.parseDouble(deviceList.get(5)));//倍率 uploadEntity.setBrand(deviceList.get(6));//品牌 uploadEntity.setModel(deviceList.get(7));//型号 uploadEntity.setInstaller(deviceList.get(8));//安装人员 uploadEntity.setBuildingId(deviceList.get(9));//所属楼栋 deviceList.clear(); uploadEntityList.add(uploadEntity); is=deviceInstallService.selectDeviceCount(uploadEntity.getDeviceAddr(),uploadEntity.getDeviceType()); if (is>0){ httpResult.setMsg("通讯编号有重复!"); httpResult.setCode(500); // return httpResult; } } if (is==0){ for (DeviceInstallEntity deviceInstallEntity:uploadEntityList){ deviceInstallService.saveDevice(deviceInstallEntity); } httpResult.setMsg("success"); httpResult.setCode(200); return httpResult; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return httpResult; } // 修改使用状态 @PostMapping(value="/isUse") public HttpResult updateDeviceIsUse(@RequestParam(value = "isUse") String isUse, @RequestParam(value = "deviceAddr") String deviceAddr) { deviceInstallService.updateDeviceIsUse(isUse,deviceAddr); return HttpResult.ok(); } @PostMapping(value="/name") public HttpResult selectDevices(@RequestParam(value = "buildingId", required = false)String buildingId, @RequestParam(value = "deviceType", required = false)String deviceType) { try{ List list; if(deviceType.equals("热泵")){ list=deviceInstallService.selectDevices(buildingId,deviceType); if (list.size()==0){ String addr; String name; for(int i=1;i<8;i++){ DeviceModel d1=new DeviceModel(); addr=String.valueOf(i); name="热泵"+addr; d1.setDeviceAddr(addr); d1.setDeviceName(name); list.add(d1); } //System.out.println(list); // DeviceModel d2=new DeviceModel(); // d2.setDeviceAddr("2"); // d2.setDeviceName("热泵2"); // list.add(d2); // // DeviceModel d3=new DeviceModel(); // d3.setDeviceAddr("3"); // d3.setDeviceName("热泵3"); // list.add(d3); // // DeviceModel d4=new DeviceModel(); // d4.setDeviceAddr("4"); // d4.setDeviceName("热泵4"); // list.add(d4); // // DeviceModel d5=new DeviceModel(); // d5.setDeviceAddr("5"); // d5.setDeviceName("热泵5"); // list.add(d5); } }else{ list=deviceInstallService.selectDevices(buildingId,deviceType); } return HttpResult.ok(list); }catch (Exception e){ e.printStackTrace(); return HttpResult.error(); } } // 查询品牌 @PostMapping(value="/brand") public HttpResult selectBrand(@RequestParam(value = "buildingId") String buildingId, @RequestParam(value = "deviceAddr") String deviceAddr) { return HttpResult.ok(deviceInstallService.selectBrand(buildingId,deviceAddr)); } //判断楼栋是否有热泵设备 @PostMapping(value="/pump") public HttpResult judgePump(@RequestParam(value = "buildingId") String buildingId) { int data=deviceInstallService.judgePump(buildingId); return HttpResult.ok("success",data); } @PostMapping(value = "/calibration") public HttpResult calibration(@RequestParam(value = "buildingId") Integer buildingId, @RequestParam(value = "deviceType") Integer deviceType, @RequestParam(value = "param") Integer param, @RequestParam(value = "readValue") String readValue, @RequestParam(value = "realValue") String realValue) { boolean isUpdate = deviceInstallService.updateDeviation(buildingId,deviceType,param,readValue,realValue); if (isUpdate) { return HttpResult.ok("success"); } else { return HttpResult.error(); } } }