From 3689936045a84b2c29899bee8680b2129e8ef43a Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Tue, 25 May 2021 17:43:52 +0800
Subject: [PATCH] device操作日志

---
 screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java |  138 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 109 insertions(+), 29 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 57be6cd..c5573f4 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
@@ -2,7 +2,6 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.moral.api.entity.Device;
 import com.moral.api.entity.ManageAccount;
@@ -20,6 +19,8 @@
 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.util.ConvertUtils;
@@ -28,11 +29,19 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+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>
@@ -63,12 +72,23 @@
     @Autowired
     private SysDictDataMapper sysDictDataMapper;
 
+    @Autowired
+    private LogUtils logUtils;
+
     @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());
+        //������������������������redis
         RedisUtil.set("device_" + device.getMac(), deviceInfo);
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������").append(device.getName()).append("���").append("mac���").append(device.getMac());
+        logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
     }
 
     @Override
@@ -77,18 +97,57 @@
         UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
         updateWrapper.eq("id", deviceId).set("is_delete", Constants.DELETE);
         deviceMapper.update(null, updateWrapper);
-        String mac = deviceMapper.selectById(deviceId).getMac();
+        Device device = deviceMapper.selectById(deviceId);
+        String mac = device.getMac();
         //������redis
         RedisUtil.del("device_" + 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
     @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
-        Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
-        RedisUtil.set("device_" + deviceInfo.get("mac"), deviceInfo);
+        RedisUtil.del("device_" + mac);
+        Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId);
+        RedisUtil.set("device_" + 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
@@ -132,10 +191,10 @@
     }
 
     @Override
-    public IPage<Device> selectDevices(Map<String, Object> parameters) {
+    public Map<String, Object> selectDevices(Map<String, Object> parameters) {
         QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
-        Integer page = (Integer) parameters.get("page");
-        Integer size = (Integer) parameters.get("size");
+        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");
@@ -160,44 +219,64 @@
         queryWrapper.eq("is_delete", Constants.NOT_DELETE);
         Page<Device> devicePage = new Page<>(page, size);
         deviceMapper.selectPage(devicePage, queryWrapper);
-        return devicePage;
+        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) {
-        Map<String, Object> result = new LinkedHashMap<>();
+        String mac = deviceMapper.selectById(deviceId).getMac();
+        Map<String, Object> deviceInfo = (Map<String, Object>) RedisUtil.get("device_" + mac);
+        //������redis������
+        if (deviceInfo != null) {
+            return deviceInfo;
+        }
+        deviceInfo = new LinkedHashMap<>();
         DeviceVO device = deviceMapper.selectDeviceInfoById(deviceId);
         //������
-        result.put("id", device.getId());
-        result.put("name", device.getName());
-        result.put("mac", device.getMac());
-        result.put("address", device.getAddress());
-        result.put("longitude", device.getLongitude());
-        result.put("latitude", device.getLatitude());
-        result.put("createTime", DateUtils.dateToDateString(device.getCreateTime()));
+        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()));
 
         //������
-        result.put("profession", device.getProfession());
-        result.put("professionName", device.getProfessionName());
+        deviceInfo.put("profession", device.getProfession());
+        deviceInfo.put("professionName", device.getProfessionName());
 
         //������
-        result.put("tech", device.getTech());
-        result.put("techName", device.getTechName());
+        deviceInfo.put("tech", device.getTech());
+        deviceInfo.put("techName", device.getTechName());
 
         //���������
-        result.put("detector", device.getDetector());
-        result.put("detectorName", device.getDetectorName());
+        deviceInfo.put("detector", device.getDetector());
+        deviceInfo.put("detectorName", device.getDetectorName());
 
         //���������
-        result.put("purchaser", device.getPurchaser());
-        result.put("purchaserName", device.getPurchaserName());
+        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());
-        result.put("version", versionInfo);
+        deviceInfo.put("version", versionInfo);
 
         //���������
         List<Map<String, Object>> operatorsInfo = new ArrayList<>();
@@ -208,22 +287,23 @@
             operatorMap.put("name", operator.getUserName());
             operatorsInfo.add(operatorMap);
         }
-        result.put("operators", operatorsInfo);
+        deviceInfo.put("operators", operatorsInfo);
 
         //������
         Map<String, Object> orgInfo = new LinkedHashMap<>();
         Organization organization = device.getOrganization();
         orgInfo.put("id", organization.getId());
         orgInfo.put("name", organization.getName());
-        result.put("organization", orgInfo);
+        deviceInfo.put("organization", orgInfo);
 
         //������
         Map<String, Object> mpInfo = new LinkedHashMap<>();
         MonitorPoint monitorPoint = device.getMonitorPoint();
         mpInfo.put("id", monitorPoint.getId());
         mpInfo.put("name", monitorPoint.getName());
-        result.put("monitorPoint", mpInfo);
-        return result;
+        deviceInfo.put("monitorPoint", mpInfo);
+        RedisUtil.set("device_" + mac, deviceInfo);
+        return deviceInfo;
     }
 
     @Override

--
Gitblit v1.8.0