|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.moral.api.entity.Device; | 
 |  |  | import com.moral.api.entity.Sensor; | 
 |  |  | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  |  | 
 |  |  | 
 |  |  |         queryWrapper.select("mac", "name").in("mac", macs); | 
 |  |  |         List<Device> devices = deviceMapper.selectList(queryWrapper); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //所选时间 | 
 |  |  |         List<String> times = (List<String>) params.remove("times"); | 
 |  |  |         //因子code | 
 |  |  |         String sensorCode = params.get("sensorCode").toString(); | 
 |  |  |         String end; | 
 |  |  |         String timeUnits; | 
 |  |  |  | 
 |  |  |         String dateFormat; | 
 |  |  |         //返回结果集,time=data | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |  | 
 |  |  |         for (String start : times) { | 
 |  |  |             if ("day".equals(type)) { | 
 |  |  |             if ("hour".equals(type)) { | 
 |  |  |                 end = DateUtils.getDateAddDay(start, 1); | 
 |  |  |                 timeUnits = "hourly"; | 
 |  |  |             } else if ("month".equals(type)) { | 
 |  |  |                 String yearAndMonth = DateUtils.dateToDateString(DateUtils.getDate(start, DateUtils.yyyy_MM_dd_EN), DateUtils.yyyyMM_EN); | 
 |  |  |                 timeUnits = "hourly_" + yearAndMonth; | 
 |  |  |                 dateFormat = "%Y-%m-%d %H"; | 
 |  |  |             } else if ("day".equals(type)) { | 
 |  |  |                 end = DateUtils.getDateAddMonth(start, 1); | 
 |  |  |                 timeUnits = "daily"; | 
 |  |  |                 dateFormat = "%Y-%m-%d"; | 
 |  |  |             } else { | 
 |  |  |                 end = DateUtils.getDateAddYear(start, 1); | 
 |  |  |                 timeUnits = "monthly"; | 
 |  |  |                 dateFormat = "%Y-%m"; | 
 |  |  |             } | 
 |  |  |             params.put("timeUnits", timeUnits); | 
 |  |  |             params.put("start", start); | 
 |  |  |             params.put("end", end); | 
 |  |  |             params.put("macs", macs); | 
 |  |  |             params.put("dateFormat", "%Y-%m-%d %H:%i:%s"); | 
 |  |  |             params.put("dateFormat", dateFormat); | 
 |  |  |             //获取多设备指定因子数据 | 
 |  |  |             List<Map<String, Object>> list = deviceMapper.getTrendChartData(params); | 
 |  |  |  | 
 |  |  | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Device getDeviceByMac(String mac) { | 
 |  |  |         Map<String, Object> deviceMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); | 
 |  |  |         Device device = JSON.parseObject(JSON.toJSONString(deviceMap), Device.class); | 
 |  |  |         //从map获取organizationId和monitorPointId以及versionId | 
 |  |  |         Map<String, Object> organizationMap = (Map<String, Object>) deviceMap.get("organization"); | 
 |  |  |         Map<String, Object> monitorPointMap = (Map<String, Object>) deviceMap.get("monitorPoint"); | 
 |  |  |         Map<String, Object> versionMap = (Map<String, Object>) deviceMap.get("version"); | 
 |  |  |         device.setDeviceVersionId((Integer) versionMap.get("id")); | 
 |  |  |         device.setOrganizationId((Integer) organizationMap.get("id")); | 
 |  |  |         device.setMonitorPointId((Integer) monitorPointMap.get("id")); | 
 |  |  |         //如果缓存为空则查询数据库 | 
 |  |  |         if (ObjectUtils.isEmpty(device)) { | 
 |  |  |             return getDeviceByMacFromDB(mac); | 
 |  |  |         } | 
 |  |  |         return device; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private Device getDeviceByMacFromDB(String mac) { | 
 |  |  |         QueryWrapper<Device> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.eq("mac", mac); | 
 |  |  |         wrapper.eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         return deviceMapper.selectOne(wrapper); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |