| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.ManageAccount; |
| | | import com.moral.api.entity.MonitorPoint; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.SysDictData; |
| | | import com.moral.api.entity.SysDictType; |
| | | import com.moral.api.entity.Version; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.ManageAccountMapper; |
| | | import com.moral.api.mapper.MonitorPointMapper; |
| | | import com.moral.api.mapper.OrganizationMapper; |
| | | import com.moral.api.mapper.SysDictDataMapper; |
| | | import com.moral.api.mapper.SysDictTypeMapper; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.*; |
| | | import com.moral.api.pojo.vo.device.DeviceVO; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | import com.moral.api.util.LogUtils; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.redis.RedisUtil; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.ConvertUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | |
| | | @Autowired |
| | | private LogUtils logUtils; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private OrganizationUnitAlarmMapper organizationUnitAlarmMapper; |
| | | |
| | | @Autowired |
| | | private VersionSensorUnitMapper versionSensorUnitMapper; |
| | | |
| | | |
| | | |
| | | /* |
| | | * 从redis获取设备信息 |
| | | * */ |
| | | private Map<String, Object> getDeviceInfoFromRedis(String mac) { |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DEVICE + mac); |
| | | return deviceInfo; |
| | | } |
| | | |
| | | /* |
| | | * 设备信息存入redis |
| | | */ |
| | | private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) { |
| | | redisTemplate.opsForValue().set(getDeviceKey(mac), deviceInfo); |
| | | } |
| | | |
| | | /* |
| | | * 从redis删除设备信息 |
| | | */ |
| | | private void delDeviceInfoFromRedis(String mac) { |
| | | redisTemplate.delete(getDeviceKey(mac)); |
| | | } |
| | | |
| | | /* |
| | | * 获取设备信息在redis里的key |
| | | */ |
| | | private String getDeviceKey(String mac) { |
| | | return keysConnect(RedisConstants.DEVICE, mac); |
| | | } |
| | | |
| | | //redis key前缀 |
| | | private String keysConnect(String... keys) { |
| | | StringBuilder key = new StringBuilder(keys[0]); |
| | | for (int i = 1; i < keys.length; i++) { |
| | | key.append("_"); |
| | | key.append(keys[i]); |
| | | } |
| | | return key.toString().toLowerCase(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insert(Device device) { |
| | |
| | | device.setOrganizationId(orgId); |
| | | deviceMapper.insert(device); |
| | | Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId()); |
| | | //维护组织型号关系表 |
| | | insertOrganizationUnitAlarm(orgId,device.getDeviceVersionId()); |
| | | //新增设备信息存入redis |
| | | RedisUtil.set("device_" + device.getMac(), deviceInfo); |
| | | String mac = device.getMac(); |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | //设备信息存入redis |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("添加了设备:").append(device.getName()).append(";").append("mac:").append(device.getMac()); |
| | | content.append("添加了设备:").append(device.getName()).append(";").append("mac:").append(mac); |
| | | logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE); |
| | | } |
| | | |
| | |
| | | deviceMapper.update(null, updateWrapper); |
| | | Device device = deviceMapper.selectById(deviceId); |
| | | String mac = device.getMac(); |
| | | //清除redis |
| | | RedisUtil.del("device_" + mac); |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | //维护组织型号关系表 |
| | | Integer versionId = device.getDeviceVersionId(); |
| | | Integer orgId = device.getOrganizationId(); |
| | | deleteOrganizationUnitAlarm(orgId,versionId); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | |
| | | public void update(Device device) { |
| | | Integer deviceId = device.getId(); |
| | | Device oldDevice = deviceMapper.selectById(deviceId); |
| | | //判断是否更新了站点,如果更新了站点则查询对应站点的组织id进行更新 |
| | | if(!ObjectUtils.isEmpty(device.getMonitorPointId())){ |
| | | MonitorPoint monitorPoint = monitorPointMapper.selectById(device.getMonitorPointId()); |
| | | device.setOrganizationId(monitorPoint.getOrganizationId()); |
| | | } |
| | | deviceMapper.updateById(device); |
| | | String mac = deviceMapper.selectById(deviceId).getMac(); |
| | | //更新redis |
| | | RedisUtil.del("device_" + mac); |
| | | Device updateDevice = deviceMapper.selectById(deviceId); |
| | | String mac = updateDevice.getMac(); |
| | | //维护组织型号关系表 |
| | | Integer oldOrgId = oldDevice.getOrganizationId(); |
| | | Integer newOrgId = updateDevice.getOrganizationId(); |
| | | Integer oldVersionId = oldDevice.getDeviceVersionId(); |
| | | Integer newVersionId = updateDevice.getDeviceVersionId(); |
| | | if(!oldOrgId.equals(newOrgId)||!oldVersionId.equals(newVersionId)){ |
| | | deleteOrganizationUnitAlarm(oldOrgId,oldVersionId); |
| | | insertOrganizationUnitAlarm(newOrgId,newVersionId); |
| | | } |
| | | //从redis中删除设备信息 |
| | | delDeviceInfoFromRedis(mac); |
| | | Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId); |
| | | RedisUtil.set("device_" + mac, deviceInfo); |
| | | //设备信息存入redis |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | //操作日志记录 |
| | | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
| | | StringBuilder content = new StringBuilder(); |
| | |
| | | @Override |
| | | public Map<String, Object> selectDeviceInfoById(Integer deviceId) { |
| | | String mac = deviceMapper.selectById(deviceId).getMac(); |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) RedisUtil.get("device_" + mac); |
| | | Map<String, Object> deviceInfo = getDeviceInfoFromRedis(mac); |
| | | //先从redis中取 |
| | | if (deviceInfo != null) { |
| | | return deviceInfo; |
| | |
| | | deviceInfo.put("latitude", device.getLatitude()); |
| | | deviceInfo.put("createTime", DateUtils.dateToDateString(device.getCreateTime())); |
| | | deviceInfo.put("installTime", device.getInstallTime() == null ? null : DateUtils.dateToDateString(device.getInstallTime())); |
| | | |
| | | //扩展字段 |
| | | deviceInfo.put("extend", device.getExtend()); |
| | | |
| | | //行业 |
| | | deviceInfo.put("profession", device.getProfession()); |
| | |
| | | mpInfo.put("id", monitorPoint.getId()); |
| | | mpInfo.put("name", monitorPoint.getName()); |
| | | deviceInfo.put("monitorPoint", mpInfo); |
| | | RedisUtil.set("device_" + mac, deviceInfo); |
| | | |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | return deviceInfo; |
| | | } |
| | | |
| | |
| | | return monitorPointMapper.selectMaps(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getDeviceByMac(String mac) { |
| | | Map<String, Object> deviceInfo = getDeviceInfoFromRedis(mac); |
| | | if (deviceInfo == null) { |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("mac", mac).eq("is_delete", Constants.NOT_DELETE); |
| | | Device device = deviceMapper.selectOne(queryWrapper); |
| | | if (device != null) { |
| | | deviceInfo = selectDeviceInfoById(device.getId()); |
| | | setDeviceInfoToRedis(mac, deviceInfo); |
| | | } |
| | | } |
| | | return deviceInfo; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData, Map<String, Object> deviceInfo) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> judgeDeviceState(Map<String, Object> deviceData, Map<String, Object> deviceInfo) { |
| | | return null; |
| | | } |
| | | |
| | | private void insertOrganizationUnitAlarm(Integer orgId,Integer versionId){ |
| | | QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>(); |
| | | queryOrganizationVersionWrapper.eq("organization_id",orgId); |
| | | queryOrganizationVersionWrapper.eq("version_id",versionId); |
| | | queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper); |
| | | if(ObjectUtils.isEmpty(organizationUnitAlarms)){ |
| | | QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper =new QueryWrapper<>(); |
| | | queryVersionSensorUnitWrapper.eq("version_id",versionId); |
| | | queryVersionSensorUnitWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | List<VersionSensorUnit> versionSensorUnits = versionSensorUnitMapper.selectList(queryVersionSensorUnitWrapper); |
| | | if(!ObjectUtils.isEmpty(versionSensorUnits)){ |
| | | for (VersionSensorUnit versionSensorUnit : versionSensorUnits) { |
| | | OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm(); |
| | | organizationUnitAlarm.setOrganizationId(orgId); |
| | | organizationUnitAlarm.setVersionId(versionId); |
| | | organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode()); |
| | | organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey()); |
| | | organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey()); |
| | | organizationUnitAlarmMapper.insert(organizationUnitAlarm); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void deleteOrganizationUnitAlarm(Integer orgId,Integer versionId){ |
| | | QueryWrapper<Device> queryOrganizationVersionWrapper = new QueryWrapper<>(); |
| | | queryOrganizationVersionWrapper.eq("organization_id",orgId); |
| | | queryOrganizationVersionWrapper.eq("device_version_id",versionId); |
| | | queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | List<Device> devices = deviceMapper.selectList(queryOrganizationVersionWrapper); |
| | | if(ObjectUtils.isEmpty(devices)){//如果为空,则组织没有该型号的设备了。 |
| | | UpdateWrapper deleteWrapper = new UpdateWrapper(); |
| | | deleteWrapper.eq("organization_id",orgId); |
| | | deleteWrapper.eq("version_id",versionId); |
| | | deleteWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | deleteWrapper.set("is_delete",Constants.DELETE); |
| | | organizationUnitAlarmMapper.update(null,deleteWrapper); |
| | | } |
| | | } |
| | | } |