kaiyu
2021-06-21 59b3b2b0c7a55b8acd6f122dad4b2ff8df1b1bce
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -123,27 +123,7 @@
        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());
                    organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode());
                    organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey());
                    organizationUnitAlarmMapper.insert(organizationUnitAlarm);
                }
            }
        }
        insertOrganizationUnitAlarm(orgId,device.getDeviceVersionId());
        //新增设备信息存入redis
        String mac = device.getMac();
        //从redis中删除设备信息
@@ -169,20 +149,8 @@
        delDeviceInfoFromRedis(mac);
        //维护组织型号关系表
        Integer versionId = device.getDeviceVersionId();
        Integer organizationId = device.getOrganizationId();
        QueryWrapper<Device> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id",organizationId);
        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",organizationId);
            deleteWrapper.eq("version_id",versionId);
            deleteWrapper.eq("is_delete",Constants.NOT_DELETE);
            deleteWrapper.set("is_delete",Constants.DELETE);
            organizationUnitAlarmMapper.update(null,deleteWrapper);
        }
        Integer orgId = device.getOrganizationId();
        deleteOrganizationUnitAlarm(orgId,versionId);
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
@@ -196,7 +164,17 @@
        Integer deviceId = device.getId();
        Device oldDevice = deviceMapper.selectById(deviceId);
        deviceMapper.updateById(device);
        String mac = deviceMapper.selectById(deviceId).getMac();
        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);
@@ -442,5 +420,43 @@
        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());
                    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);
        }
    }
}