From cc06e680134316be4655422d06056a916b8bd8e4 Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Fri, 18 Jun 2021 09:01:43 +0800
Subject: [PATCH] screen-api 修改型号查询bug
---
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java | 397 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 375 insertions(+), 22 deletions(-)
diff --git a/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java b/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
index bb7f828..76d4e8f 100644
--- a/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
+++ b/screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -1,26 +1,39 @@
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.Organization;
-import com.moral.api.entity.User;
-import com.moral.api.mapper.DeviceMapper;
-import com.moral.api.mapper.ManageAccountMapper;
-import com.moral.api.mapper.OrganizationMapper;
+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.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;
-import java.util.Date;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+
+import javax.servlet.http.HttpServletRequest;
/**
* <p>
@@ -42,34 +55,374 @@
@Autowired
private OrganizationMapper organizationMapper;
+ @Autowired
+ private MonitorPointMapper monitorPointMapper;
+
+ @Autowired
+ private SysDictTypeMapper sysDictTypeMapper;
+
+ @Autowired
+ private SysDictDataMapper sysDictDataMapper;
+
+ @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) {
+ Integer orgId = monitorPointMapper.selectById(device.getMonitorPointId()).getOrganizationId();
+ device.setOrganizationId(orgId);
deviceMapper.insert(device);
+ Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
+ //���������������������������
+ QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>();
+ queryOrganizationVersionWrapper.eq("organization_id",orgId);
+ queryOrganizationVersionWrapper.eq("version_id",device.getDeviceVersionId());
+ 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",device.getDeviceVersionId());
+ 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(device.getDeviceVersionId());
+
+ }
+ }
+ }
+ //������������������������redis
+ 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(mac);
+ logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
}
@Override
- public List<ManageAccount> selectAllOperator() {
- QueryWrapper<ManageAccount> queryWrapper = new QueryWrapper<>();
- queryWrapper.select("id", "user_name").eq("is_delete", Constants.NOT_DELETE);
- return manageAccountMapper.selectList(queryWrapper);
+ @Transactional
+ public void delete(Integer deviceId) {
+ UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
+ updateWrapper.eq("id", deviceId).set("is_delete", Constants.DELETE);
+ deviceMapper.update(null, updateWrapper);
+ Device device = deviceMapper.selectById(deviceId);
+ String mac = device.getMac();
+ //���redis���������������������
+ delDeviceInfoFromRedis(mac);
+ //������������������
+ HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+ StringBuilder content = new StringBuilder();
+ content.append("������������������").append(device.getName()).append(";").append("mac���").append(mac);
+ logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
}
@Override
- public List<Organization> selectAllOrganization() {
+ @Transactional
+ public void update(Device device) {
+ Integer deviceId = device.getId();
+ Device oldDevice = deviceMapper.selectById(deviceId);
+ deviceMapper.updateById(device);
+ String mac = deviceMapper.selectById(deviceId).getMac();
+ //���redis���������������������
+ delDeviceInfoFromRedis(mac);
+ Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId);
+ //������������������redis
+ setDeviceInfoToRedis(mac, deviceInfo);
+ //������������������
+ HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+ StringBuilder content = new StringBuilder();
+ content.append("������������������").append(mac).append("���");
+ Field[] fields = Device.class.getDeclaredFields();
+ for (Field field : fields) {
+ if (field.getName().equals("id")) {
+ continue;
+ }
+ if ("serialVersionUID".equals(field.getName())) {
+ continue;
+ }
+ String fieldName = field.getName();
+ PropertyDescriptor pd = null;
+ try {
+ pd = new PropertyDescriptor(fieldName, Device.class);
+ Method method = pd.getReadMethod();
+ Object o1 = method.invoke(oldDevice);
+ Object o2 = method.invoke(device);
+ if (o2 != null) {
+ content.append(fieldName).append("���").append(o1).append("-->").append(o2).append("���");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+ logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
+ }
+
+ @Override
+ public List<Map<String, Object>> selectAllOperators() {
+ //���������������
+ QueryWrapper<ManageAccount> operatorWrapper = new QueryWrapper<>();
+ operatorWrapper.select("id", "user_name").eq("is_delete", Constants.NOT_DELETE);
+ return manageAccountMapper.selectMaps(operatorWrapper);
+ }
+
+ //������������������������������������
+ @Override
+ public List<Map<String, Object>> selectDeviceDictData(String dictType) {
+ QueryWrapper<SysDictType> typeQueryWrapper = new QueryWrapper<>();
+ typeQueryWrapper.select("id").eq("name", dictType);
+ SysDictType sysDictType = sysDictTypeMapper.selectOne(typeQueryWrapper);
+ QueryWrapper<SysDictData> dataQueryWrapper = new QueryWrapper<>();
+ dataQueryWrapper.select("dataKey", "dataValue").eq("dict_type_id", sysDictType.getId()).eq("is_delete", Constants.NOT_DELETE);
+ return sysDictDataMapper.selectMaps(dataQueryWrapper);
+ }
+
+ @Override
+ public List<Map<String, Object>> selectMonitorsByOrgId(Integer orgId) {
+ QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>();
+ queryWrapper.select("id", "name").eq("organization_id", orgId).eq("is_delete", Constants.NOT_DELETE);
+ return monitorPointMapper.selectMaps(queryWrapper);
+ }
+
+ @Override
+ public Map<String, Object> selectDevicesByOrgId(Map<String, Object> parameters) {
+ return selectDevices(parameters);
+ }
+
+ @Override
+ public Map<String, Object> selectDevicesByMpId(Map<String, Object> parameters) {
+ return selectDevices(parameters);
+ }
+
+ @Override
+ public Map<String, Object> selectDevices(Map<String, Object> parameters) {
+ QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
+ int page = Integer.parseInt(parameters.get("page").toString());
+ int size = Integer.parseInt(parameters.get("size").toString());
+ Object order = parameters.get("order");
+ Object orderType = parameters.get("orderType");
+ Object name = parameters.get("name");
+ Object mac = parameters.get("mac");
+ Object orgId = parameters.get("organizationId");
+ Object mpId = parameters.get("monitorPointId");
+
+ //������������������������������
+ if (orgId != null) {
+ queryWrapper.eq("organization_id", orgId);
+ }
+
+ //������������������������������
+ if (mpId != null) {
+ queryWrapper.eq("monitor_point_id", mpId);
+ }
+
+ if (name != null) {
+ queryWrapper.like("name", name);
+ }
+ if (mac != null) {
+ queryWrapper.like("mac", mac);
+ }
+
+
+ //������������,������create_time������
+ if (order != null && orderType != null) {
+ if (Constants.ORDER_ASC.equals(orderType)) {
+ queryWrapper.orderByAsc(ConvertUtils.toLine(order.toString()));
+ } else {
+ queryWrapper.orderByDesc(ConvertUtils.toLine(order.toString()));
+ }
+ } else {
+ queryWrapper.orderByDesc("create_time");
+ }
+ queryWrapper.eq("is_delete", Constants.NOT_DELETE);
+ Page<Device> devicePage = new Page<>(page, size);
+ deviceMapper.selectPage(devicePage, queryWrapper);
+ List<Device> devices = devicePage.getRecords();
+ List<Map<String, Object>> items = new ArrayList<>();
+ for (Device device : devices) {
+ Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
+ items.add(deviceInfo);
+ }
+ Map<String, Object> result = new LinkedHashMap<>();
+ result.put("total", devicePage.getTotal());
+ result.put("totalPage", devicePage.getPages());
+ result.put("current", devicePage.getCurrent());
+ result.put("pageSize", devicePage.getSize());
+ result.put("item", items);
+ return result;
+ }
+
+
+ @Override
+ public Map<String, Object> selectDeviceInfoById(Integer deviceId) {
+ String mac = deviceMapper.selectById(deviceId).getMac();
+ Map<String, Object> deviceInfo = getDeviceInfoFromRedis(mac);
+ //������redis������
+ if (deviceInfo != null) {
+ return deviceInfo;
+ }
+ deviceInfo = new LinkedHashMap<>();
+ DeviceVO device = deviceMapper.selectDeviceInfoById(deviceId);
+ //������
+ deviceInfo.put("id", device.getId());
+ deviceInfo.put("name", device.getName());
+ deviceInfo.put("mac", device.getMac());
+ deviceInfo.put("address", device.getAddress());
+ deviceInfo.put("longitude", device.getLongitude());
+ 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());
+ deviceInfo.put("professionName", device.getProfessionName());
+
+ //������
+ deviceInfo.put("tech", device.getTech());
+ deviceInfo.put("techName", device.getTechName());
+
+ //���������
+ deviceInfo.put("detector", device.getDetector());
+ deviceInfo.put("detectorName", device.getDetectorName());
+
+ //���������
+ deviceInfo.put("purchaser", device.getPurchaser());
+ deviceInfo.put("purchaserName", device.getPurchaserName());
+
+ //������
+ Map<String, Object> versionInfo = new LinkedHashMap<>();
+ Version version = device.getVersion();
+ versionInfo.put("id", version.getId());
+ versionInfo.put("name", version.getName());
+ deviceInfo.put("version", versionInfo);
+
+ //���������
+ List<Map<String, Object>> operatorsInfo = new ArrayList<>();
+ List<ManageAccount> operators = device.getOperators();
+ for (ManageAccount operator : operators) {
+ Map<String, Object> operatorMap = new LinkedHashMap<>();
+ operatorMap.put("id", operator.getId());
+ operatorMap.put("name", operator.getUserName());
+ operatorsInfo.add(operatorMap);
+ }
+ deviceInfo.put("operators", operatorsInfo);
+
+ //������
+ Map<String, Object> orgInfo = new LinkedHashMap<>();
+ Organization organization = device.getOrganization();
+ orgInfo.put("id", organization.getId());
+ orgInfo.put("name", organization.getName());
+ deviceInfo.put("organization", orgInfo);
+
+ //������
+ Map<String, Object> mpInfo = new LinkedHashMap<>();
+ MonitorPoint monitorPoint = device.getMonitorPoint();
+ mpInfo.put("id", monitorPoint.getId());
+ mpInfo.put("name", monitorPoint.getName());
+ deviceInfo.put("monitorPoint", mpInfo);
+
+ setDeviceInfoToRedis(mac, deviceInfo);
+ return deviceInfo;
+ }
+
+ @Override
+ public List<Map<String, Object>> selectAllOrganization() {
QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
- queryWrapper.select("id", "name").ge("expire_time", new Date()).eq("is_delete", Constants.NOT_DELETE);
- return organizationMapper.selectList(queryWrapper);
+ queryWrapper.select("id", "name").eq("is_delete", Constants.NOT_DELETE);
+ return organizationMapper.selectMaps(queryWrapper);
}
@Override
- public Page<DeviceVO> selectDevices(Map<String, Object> parameters) {
- QueryWrapper<DeviceVO> queryWrapper = new QueryWrapper<>();
- Integer page = (Integer) parameters.get("page");
- Integer size = (Integer) parameters.get("size");
- Page<DeviceVO> pageData = new Page<>(page, size);
- deviceMapper.selectAllDeviceInfo(pageData, queryWrapper);
- return pageData;
+ public List<Map<String, Object>> selectAllMonitorPoint() {
+ QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>();
+ queryWrapper.select("id", "name").eq("is_delete", Constants.NOT_DELETE);
+ 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;
+ }
+
+
}
--
Gitblit v1.8.0