From 4e250e6a954ab426cc54dcf11caec806eb8df688 Mon Sep 17 00:00:00 2001
From: cjl <276999030@qq.com>
Date: Wed, 20 Sep 2023 17:21:02 +0800
Subject: [PATCH] feat:添加测试更新guid

---
 screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java |  612 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 587 insertions(+), 25 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..bfc1a41 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,47 @@
 package com.moral.api.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
 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.googlecode.aviator.AviatorEvaluator;
+import com.googlecode.aviator.Expression;
+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.constant.Constants;
 
+import com.moral.api.service.OrganizationService;
+import com.moral.api.service.SysDictDataService;
+import com.moral.api.util.CacheUtils;
+import com.moral.api.util.AdjustDataUtils;
+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 lombok.extern.slf4j.Slf4j;
 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.util.StringUtils;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import javax.servlet.http.HttpServletRequest;
 
 /**
  * <p>
@@ -31,6 +52,7 @@
  * @since 2021-05-11
  */
 @Service
+@Slf4j
 public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
 
     @Autowired
@@ -42,34 +64,574 @@
     @Autowired
     private OrganizationMapper organizationMapper;
 
+    @Autowired
+    private MonitorPointMapper monitorPointMapper;
+
+    @Autowired
+    private SysDictTypeMapper sysDictTypeMapper;
+
+    @Autowired
+    private SysDictDataMapper sysDictDataMapper;
+
+    @Autowired
+    private SysDictDataService sysDictDataService;
+
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Autowired
+    private OrganizationUnitAlarmMapper organizationUnitAlarmMapper;
+
+    @Autowired
+    private VersionSensorUnitMapper versionSensorUnitMapper;
+
+    @Autowired
+    private SpecialDeviceMapper specialDeviceMapper;
+
+    @Autowired
+    private AdjustDataUtils adjustDataUtils;
+
+    @Autowired
+    private OrganizationService organizationService;
+
+    /*
+     * ���redis������������������
+     * */
+    private Map<String, Object> getDeviceInfoFromRedis(String mac) {
+        return (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac);
+    }
+
+    /*
+     * ������������������redis
+     */
+    private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) {
+        redisTemplate.opsForHash().put(RedisConstants.DEVICE, mac, deviceInfo);
+    }
+
+    /*
+     * ���redis������������������
+     */
+    private void delDeviceInfoFromRedis(String mac) {
+        redisTemplate.opsForHash().delete(RedisConstants.DEVICE, mac);
+    }
+
     @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());
+        //���������������������������
+        insertOrganizationUnitAlarm(orgId, device.getDeviceVersionId());
+        //������������������������redis
+        String mac = device.getMac();
+        //���redis���������������������
+        delDeviceInfoFromRedis(mac);
+        //������������������redis
+        setDeviceInfoToRedis(mac, deviceInfo);
+        //������deviceInfo������
+        CacheUtils.refreshDeviceAlarmInfo();
+        //������������������
+        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) {
+        Device device = deviceMapper.selectById(deviceId);
+        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.eq("id", deviceId).set("is_delete", Constants.DELETE);
+        deviceMapper.update(null, updateWrapper);
+        String mac = device.getMac();
+        //���redis���������������������
+        delDeviceInfoFromRedis(mac);
+        //���������������������������
+        Integer versionId = device.getDeviceVersionId();
+        Integer orgId = device.getOrganizationId();
+        deleteOrganizationUnitAlarm(orgId, versionId);
+        //������deviceInfo������
+        CacheUtils.refreshDeviceAlarmInfo();
+        //������������������
+        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);
+        //���������������������������������������������������������������������������������id������������
+        if (!ObjectUtils.isEmpty(device.getMonitorPointId())) {
+            MonitorPoint monitorPoint = monitorPointMapper.selectById(device.getMonitorPointId());
+            device.setOrganizationId(monitorPoint.getOrganizationId());
+        }
+        deviceMapper.updateById(device);
+        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(oldDevice.getMac());
+        Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId);
+        //������������������redis
+        setDeviceInfoToRedis(mac, deviceInfo);
+        //������deviceInfo������
+        CacheUtils.refreshDeviceAlarmInfo();
+        //������������������
+        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+        StringBuilder content = new StringBuilder();
+        content.append("������������������" + oldDevice.getMac()).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) {
+        //���������������������
+        List<Organization> organizations = organizationService.getAllChildrenOrganization(orgId);
+        List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
+        orgIds.add(orgId);
+        QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("id", "name")
+                .eq("is_delete", Constants.NOT_DELETE)
+                .in("organization_id", orgIds);
+        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) {
+            //���������������������
+            List<Organization> organizations = organizationService.getAllChildrenOrganization(Integer.parseInt(orgId.toString()));
+            List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
+            orgIds.add(Integer.parseInt(orgId.toString()));
+            queryWrapper.in("organization_id", orgIds);
+        }
+
+        //������������������������������
+        if (mpId != null) {
+            queryWrapper.eq("monitor_point_id", mpId);
+        }
+
+        //���������������mac������������
+        if (name != null && mac != null) {
+            queryWrapper.like("name", name).or().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("town", device.getTown());
+
+        //������
+        deviceInfo.put("professions", device.getProfessions());
+
+        //������
+        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());
+        mpInfo.put("areaCode", monitorPoint.getAreaCode());
+        mpInfo.put("cityCode", monitorPoint.getCityCode());
+        mpInfo.put("provinceCode", monitorPoint.getProvinceCode());
+        deviceInfo.put("monitorPoint", mpInfo);
+
+        //������������
+        Map<String, Object> govMpInfo = new LinkedHashMap<>();
+
+        govMpInfo.put("guid", null);
+        govMpInfo.put("name", null);
+        if (device.getGovMonitorPoint() != null) {
+            govMpInfo.put("guid", device.getGovMonitorPoint().getGuid());
+            govMpInfo.put("name", device.getGovMonitorPoint().getName());
+        }
+        deviceInfo.put("govMonitorPoint", govMpInfo);
+        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 (ObjectUtils.isEmpty(deviceInfo)) {
+            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,String code) {
+//        String mac = deviceData.remove("mac").toString();
+        String mac = deviceData.get("mac").toString();
+        //���redis������������������
+        Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac);
+        if (!ObjectUtils.isEmpty(adjustFormula)) {
+            Map<String, Object> deviceInfo = getDeviceByMac(mac);
+            //������������������������������������
+            Map<String, Object> govMpInfo = (Map<String, Object>) deviceInfo.get("govMonitorPoint");
+            Map<String, Object> aqiMap = null;
+            if (govMpInfo.get("guid") != null) {
+                aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString());
+            }
+            return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,code);
+        }
+        deviceData.remove("DataTime");
+        return deviceData;
+    }
+
+    @Override
+    public Map<String, Object> adjustSpecialDeviceData(Map<String, Object> deviceData) {
+//        String mac = deviceData.remove("mac").toString();
+        String mac = deviceData.get("mac").toString();
+        //���redis������������������
+        Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac);
+        if (!ObjectUtils.isEmpty(adjustFormula)) {
+            Organization organization = specialDeviceMapper.selectOrgByMac(mac);
+            String areaCode = organization.getAreaCode().toString();
+            String cityCode = organization.getCityCode().toString();
+            Map<String, Object> aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, areaCode);
+            if (ObjectUtils.isEmpty(aqiMap)) {
+                aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, cityCode);
+            }
+            return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,"0");
+        }
+        deviceData.remove("DataTime");
+        return deviceData;
+    }
+
+    @Override
+    @Transactional
+    public void judgeDeviceState(Map<String, Object> deviceData) {
+        String mac = deviceData.remove("mac").toString();
+        Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac);
+        Version version = device.getVersion();
+        List<Sensor> sensors = version.getSensors();
+        Expression expression;
+        int state = 1;
+        for (Sensor sensor : sensors) {
+            //������������������
+            String alarmLevel = sensor.getAlarmLevel();
+            if (StringUtils.isEmpty(alarmLevel)) {
+                continue;
+            }
+            List<Object> list = JSONObject.parseObject(alarmLevel, List.class);
+            String sensorCode = sensor.getCode();
+            //������������
+            String formula = sensor.getFormula();
+
+            //���������������������������������������������������������������
+            if (deviceData.get(sensorCode) != null) {
+                String sensorValue = String.valueOf(deviceData.get(sensorCode));
+                double value = Double.parseDouble(sensorValue);
+                if (formula != null) {
+                    //������������������
+                    sensorValue = formula.replace("{0}", sensorValue);
+                    expression = AviatorEvaluator.compile(sensorValue);
+                    value = Double.parseDouble(expression.execute().toString());
+                }
+                int sensorState = judgeState(list, value);
+                if (sensorState > state) {
+                    state = sensorState;
+                }
+            }
+        }
+        //������������������
+        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.eq("id", device.getId()).set("state", String.valueOf(state));
+        deviceMapper.update(null, updateWrapper);
+    }
+
+    @Override
+    public List<Map<String, Object>> selectMonitorPiontAndDeviceByOrgId(int orgId) {
+        QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
+        monitorPointQueryWrapper.eq("is_delete", Constants.NOT_DELETE);
+        monitorPointQueryWrapper.eq("organization_id", orgId);
+        List<MonitorPoint> monitorPointList = monitorPointMapper.selectList(monitorPointQueryWrapper);
+        List<Map<String, Object>> resultList = new ArrayList<>();
+        for (MonitorPoint monitorPoint : monitorPointList) {
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap = JSON.parseObject(JSON.toJSONString(monitorPoint), Map.class);
+            int mp_id = monitorPoint.getId();
+            QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
+            deviceQueryWrapper.eq("is_delete", Constants.NOT_DELETE);
+            deviceQueryWrapper.eq("monitor_point_id", mp_id);
+            List<Device> devices = new ArrayList<>();
+            devices = deviceMapper.selectList(deviceQueryWrapper);
+            resultMap.put("devices", devices);
+            resultList.add(resultMap);
+        }
+        return resultList;
+    }
+
+    //���������������������������
+    private int judgeState(List<Object> levels, Double data) {
+        int state = 1;
+        for (int i = levels.size() - 1; i >= 0; i--) {
+            Double level = Double.parseDouble(levels.get(i).toString());
+            if (data >= level) {
+                state = i + 2;
+                break;
+            }
+        }
+        return state;
+    }
+
+    /**
+     * @Description: ���������������������������������������������������������������������������������������������
+     * @Param: [orgId, versionId]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/9/6
+     */
+    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);
+            //������������������������������������
+            Map<String, SysDictData> sensorAlarms = sysDictDataService.getDictDatasByType("defaultAlarm");
+            if (!ObjectUtils.isEmpty(versionSensorUnits)) {
+                for (VersionSensorUnit versionSensorUnit : versionSensorUnits) {
+                    OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm();
+                    String sensorCode = versionSensorUnit.getSensorCode();
+                    //���������������������
+                    SysDictData alarm = sensorAlarms.get(sensorCode);
+                    if (alarm != null)
+                        organizationUnitAlarm.setAlarmLevel(alarm.getDataValue());
+                    organizationUnitAlarm.setOrganizationId(orgId);
+                    organizationUnitAlarm.setVersionId(versionId);
+                    organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode());
+                    organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey());
+                    organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey());
+                    organizationUnitAlarmMapper.insert(organizationUnitAlarm);
+                }
+            }
+        }
+    }
+
+    /**
+     * @Description: ������������������������������������������������������������������������������������������������������������������������������������
+     * @Param: [orgId, versionId]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/9/6
+     */
+    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);
+        }
+    }
+
+    @Override
+    public void setRedisDevice() {
+        List<Device> list = lambdaQuery().eq(Device::getIsDelete,0).list();
+        for(Device d : list){
+           Map<String, Object> deviceInfo = selectDeviceInfoById(d.getId());
+           redisTemplate.opsForHash().delete(RedisConstants.DEVICE, d.getMac());
+           setDeviceInfoToRedis(d.getMac(), deviceInfo);
+        }
+    }
 }

--
Gitblit v1.8.0