liding
2026-06-02 c0ce5324f128a09e2cd42eece8c3120acea20830
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.ruoyi.alarm.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.alarm.dto.AlarmCollectionInterfaceDto;
import com.ruoyi.alarm.dto.AlarmPointDto;
import com.ruoyi.alarm.mapper.AlarmCollectionInterfaceMapper;
import com.ruoyi.alarm.mapper.AlarmPointMapper;
import com.ruoyi.alarm.pojo.AlarmCollectionInterface;
import com.ruoyi.alarm.pojo.AlarmPoint;
import com.ruoyi.alarm.service.IAlarmCollectionService;
import com.ruoyi.alarm.vo.AlarmCollectionInterfaceVo;
import com.ruoyi.alarm.vo.AlarmPointLedgerVo;
import com.ruoyi.alarm.vo.AlarmStatisticsVo;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Service
@AllArgsConstructor
public class AlarmCollectionServiceImpl extends ServiceImpl<AlarmPointMapper, AlarmPoint> implements IAlarmCollectionService {
 
    private AlarmPointMapper alarmPointMapper;
    private AlarmCollectionInterfaceMapper alarmCollectionInterfaceMapper;
 
    @Override
    public AlarmStatisticsVo getStatistics() {
        AlarmStatisticsVo vo = new AlarmStatisticsVo();
        LambdaQueryWrapper<AlarmPoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmPoint::getStatus, 1);
        Long totalPoints = alarmPointMapper.selectCount(wrapper);
        vo.setTotalPoints(totalPoints.intValue());
        vo.setConnectedDevices((int) (totalPoints * 0.7));
        vo.setTodayDataVolume(245.6);
        vo.setParseSuccessRate(99.8);
        return vo;
    }
 
    @Override
    public IPage<AlarmPointLedgerVo> getPointLedgerList(Page page, AlarmPointDto dto) {
        LambdaQueryWrapper<AlarmPoint> wrapper = new LambdaQueryWrapper<>();
        if (dto.getPointName() != null && !dto.getPointName().isEmpty()) {
            wrapper.like(AlarmPoint::getPointName, dto.getPointName());
        }
        if (dto.getDeviceType() != null && !dto.getDeviceType().isEmpty()) {
            wrapper.eq(AlarmPoint::getDeviceType, dto.getDeviceType());
        }
        wrapper.orderByDesc(AlarmPoint::getCreateTime);
        IPage<AlarmPoint> pointPage = alarmPointMapper.selectPage(page, wrapper);
        return pointPage.convert(point -> {
            AlarmPointLedgerVo vo = new AlarmPointLedgerVo();
            BeanUtils.copyProperties(point, vo);
            return vo;
        });
    }
 
    @Override
    public int addPoint(AlarmPointDto dto) {
        AlarmPoint point = new AlarmPoint();
        BeanUtils.copyProperties(dto, point);
        point.setCreateTime(LocalDateTime.now());
        point.setUpdateTime(LocalDateTime.now());
        return alarmPointMapper.insert(point);
    }
 
    @Override
    public int updatePoint(AlarmPointDto dto) {
        AlarmPoint point = new AlarmPoint();
        BeanUtils.copyProperties(dto, point);
        point.setUpdateTime(LocalDateTime.now());
        return alarmPointMapper.updateById(point);
    }
 
    @Override
    public int deletePoint(Long pointId) {
        return alarmPointMapper.deleteById(pointId);
    }
 
    @Override
    public int changePointStatus(Long pointId, Integer status) {
        AlarmPoint point = new AlarmPoint();
        point.setPointId(pointId);
        point.setStatus(status);
        point.setUpdateTime(LocalDateTime.now());
        return alarmPointMapper.updateById(point);
    }
 
    @Override
    public List<AlarmPointLedgerVo> getPointOptions() {
        LambdaQueryWrapper<AlarmPoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmPoint::getStatus, 1);
        wrapper.select(AlarmPoint::getPointId, AlarmPoint::getPointName);
        List<AlarmPoint> points = alarmPointMapper.selectList(wrapper);
        return points.stream().map(point -> {
            AlarmPointLedgerVo vo = new AlarmPointLedgerVo();
            vo.setPointId(point.getPointId());
            vo.setPointName(point.getPointName());
            return vo;
        }).collect(java.util.stream.Collectors.toList());
    }
 
    @Override
    public List<AlarmCollectionInterfaceVo> getInterfaceList() {
        LambdaQueryWrapper<AlarmCollectionInterface> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AlarmCollectionInterface::getCreateTime);
        List<AlarmCollectionInterface> interfaces = alarmCollectionInterfaceMapper.selectList(wrapper);
        
        if (interfaces.isEmpty()) {
            List<AlarmCollectionInterfaceVo> mockData = new ArrayList<>();
            AlarmCollectionInterfaceVo vo = new AlarmCollectionInterfaceVo();
            vo.setInterfaceId(1L);
            vo.setInterfaceName("OPC UA数据采集");
            vo.setInterfaceType("OPC UA");
            vo.setServerAddress("opc.tcp://192.168.1.100:4840");
            vo.setCollectFreq("1秒");
            vo.setConnectStatus("connected");
            mockData.add(vo);
            return mockData;
        }
        
        return interfaces.stream().map(iface -> {
            AlarmCollectionInterfaceVo vo = new AlarmCollectionInterfaceVo();
            BeanUtils.copyProperties(iface, vo);
            return vo;
        }).collect(java.util.stream.Collectors.toList());
    }
 
    @Override
    public int startInterface(Long interfaceId) {
        AlarmCollectionInterface iface = new AlarmCollectionInterface();
        iface.setInterfaceId(interfaceId);
        iface.setConnectStatus("connected");
        iface.setUpdateTime(LocalDateTime.now());
        return alarmCollectionInterfaceMapper.updateById(iface);
    }
 
    @Override
    public int stopInterface(Long interfaceId) {
        AlarmCollectionInterface iface = new AlarmCollectionInterface();
        iface.setInterfaceId(interfaceId);
        iface.setConnectStatus("disconnected");
        iface.setUpdateTime(LocalDateTime.now());
        return alarmCollectionInterfaceMapper.updateById(iface);
    }
 
    @Override
    public int addOrUpdateInterface(AlarmCollectionInterfaceDto dto) {
        AlarmCollectionInterface iface = new AlarmCollectionInterface();
        BeanUtils.copyProperties(dto, iface);
        iface.setUpdateTime(LocalDateTime.now());
        
        if (dto.getInterfaceId() == null) {
            // 新增
            iface.setCreateTime(LocalDateTime.now());
            return alarmCollectionInterfaceMapper.insert(iface);
        } else {
            // 更新
            return alarmCollectionInterfaceMapper.updateById(iface);
        }
    }
 
    @Override
    public int deleteInterface(Long interfaceId) {
        return alarmCollectionInterfaceMapper.deleteById(interfaceId);
    }
}