| | |
| | | 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; |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | |
| | | } |