List「Map「String, Object」」 返回 {key:“x“,value:“y“,...}

    技术2022-07-12  58

    /** * 统计本周异常数据 return ByDay 按每天返回 * @param strDate * @return */ @GetMapping("/get-aa-status-week") public ResponseEntity<Message> statisDeviceStatusByWeek(String strDate) { Message message = new Message(); try { if(null == strDate){ strDate = getPastDate(6); } List<Map<String, Object>> deviceStatusMap = tagDeviceService.statusDeviceStatusByDate(strDate,2); message.setData(deviceStatusMap); } catch (Exception e){ message.setCode(100001); message.setMessage("get-aa-status-week出错"); } return new ResponseEntity<>(message, HttpStatus.OK); } /** * 获取过去第几天的日期 * * @param past * @return */ public String getPastDate(int past) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past); Date today = calendar.getTime(); String result = sdf1.format(today); return result; } public List<Map<String, Object>> statusDeviceStatusByDate(String strDate,int dayType) { int time = 3600*24; String sdf = "yyyy-MM-dd"; if(1 == dayType){ time = 3600;//当天按小时计算 sdf = "yyyy-MM-dd HH:mm:ss"; }if (2 == dayType){ time = 3600*24;//本周按天计算 sdf = "yyyy-MM-dd"; } String nowTime1 = sdf1.format(new Date()); Long nowTime = Long.valueOf(dateTimeStamp(nowTime1, "yyyy-MM-dd HH:mm:ss")); Long startTime = Long.valueOf(dateTimeStamp(strDate, "yyyy-MM-dd HH:mm:ss")); List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>(); while (startTime <= nowTime) { Map<String, Object> m1 = new HashMap<String, Object>(); Map<String, Object> mapValue = new HashMap<>(); Map<String, Object> mapKey = new HashMap<>(); Long beforTime = startTime; Long afterTime = beforTime + time; String a = timeStampDate(String.valueOf(beforTime), "yyyy-MM-dd HH:mm:ss"); String b = timeStampDate(String.valueOf(afterTime), "yyyy-MM-dd HH:mm:ss"); // int normal = tagDeviceAllRepository.findDeviceAllRecordeByHour(0, a, b); // int exception = tagDeviceAllRepository.findDeviceAllRecordeByHour(1, a, b); int warning = tagDeviceAllRepository.findDeviceAllRecordeByHour(2, a, b); // mapValue.put("normal", normal); // mapValue.put("exception", exception); String outTime = timeStampDate(String.valueOf(beforTime), sdf); mapValue.put("value", warning); m1.put("key", outTime); m1.put("value", warning); list1.add(m1); startTime = afterTime; } return list1; } { "code": 100000, "data": [ { "value": 0, "key": "2020-06-26" }, { "value": 0, "key": "2020-06-27" }, { "value": 0, "key": "2020-06-28" }, { "value": 10, "key": "2020-06-29" }, { "value": 11, "key": "2020-06-30" }, { "value": 7, "key": "2020-07-01" }, { "value": 0, "key": "2020-07-02" } ], "message": "操作成功", "ret": 200 }
    Processed: 0.008, SQL: 9