Lius
2025-03-12 da82d77c6773f65aadfde810233615b602da655a
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.mdc.dto.MdcAlarmAnalyzeDto;
import org.jeecg.modules.mdc.dto.MdcAlarmDto;
import org.jeecg.modules.mdc.dto.MdcAlarmListDto;
import org.jeecg.modules.mdc.dto.MdcAlarmTrendDto;
import org.jeecg.modules.mdc.entity.MdcAlarmInfo;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
import org.jeecg.modules.mdc.service.IMdcAlarmInfoService;
import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.MdcEquipmentAlarmAnalyzeService;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcAlarmAnalyzeQueryVo;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * @author: LiuS
 * @create: 2023-09-06 17:31
 */
@Service
public class MdcEquipmentAlarmAnalyzeServiceImpl implements MdcEquipmentAlarmAnalyzeService {
 
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcAlarmInfoService mdcAlarmInfoService;
 
    @Override
    public List<MdcAlarmAnalyzeDto> alarmList(String userId, MdcAlarmAnalyzeQueryVo vo) {
        List<MdcAlarmAnalyzeDto> result = new ArrayList<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            // 单台设备信息
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            return result;
        } else {
            vo.setStartDate(DateUtils.format(DateUtils.toDate(vo.getStartDate(), DateUtils.STRDATE), DateUtils.STR_DATE) + " 00:00:00");
            vo.setEndDate(DateUtils.format(DateUtils.addDays(DateUtils.toDate(vo.getEndDate(), DateUtils.STRDATE), 1), DateUtils.STR_DATE) + " 00:00:00");
            // 查询
            List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.findAlarmList(vo);
            Map<String, MdcAlarmAnalyzeDto> map = new HashMap<>();
            for (MdcEquipmentRunningSection mdcEquipmentRunningSection : mdcEquipmentRunningSections) {
                if (map.containsKey(mdcEquipmentRunningSection.getAlarm())) {
                    MdcAlarmAnalyzeDto mdcAlarmAnalyzeDto = map.get(mdcEquipmentRunningSection.getAlarm());
                    mdcAlarmAnalyzeDto.setCount(mdcAlarmAnalyzeDto.getCount() + 1);
                    mdcAlarmAnalyzeDto.setTimeCount(mdcAlarmAnalyzeDto.getTimeCount().add(new BigDecimal(mdcEquipmentRunningSection.getDuration())));
                    map.put(mdcEquipmentRunningSection.getAlarm(), mdcAlarmAnalyzeDto);
                } else {
                    MdcAlarmAnalyzeDto mdcAlarmAnalyzeDto = new MdcAlarmAnalyzeDto();
                    mdcAlarmAnalyzeDto.setAlarmCode(mdcEquipmentRunningSection.getAlarm());
                    mdcAlarmAnalyzeDto.setCount(1);
                    mdcAlarmAnalyzeDto.setTimeCount(new BigDecimal(mdcEquipmentRunningSection.getDuration()));
                    //查询报警号内容
                    List<MdcEquipment> list = mdcEquipmentService.list(new LambdaQueryWrapper<MdcEquipment>().eq(MdcEquipment::getEquipmentId, mdcEquipmentRunningSection.getEquipmentId()));
                    List<MdcAlarmInfo> mdcAlarmInfo = mdcAlarmInfoService.list(new LambdaQueryWrapper<MdcAlarmInfo>().eq(MdcAlarmInfo::getDriveType, list.get(0).getDriveType()).eq(MdcAlarmInfo::getAlarmCode, mdcEquipmentRunningSection.getAlarm()).eq(MdcAlarmInfo::getIsUse, 0));
                    if (mdcAlarmInfo != null && !mdcAlarmInfo.isEmpty()) {
                        mdcAlarmAnalyzeDto.setAlarmContent(mdcAlarmInfo.get(0).getAlarmContent());
                    }
                    map.put(mdcEquipmentRunningSection.getAlarm(), mdcAlarmAnalyzeDto);
                }
            }
            if (!map.isEmpty()) {
                result = new ArrayList<>(map.values());
            }
        }
        return result;
    }
 
 
    @Override
    public MdcAlarmTrendDto alarmTrend(String userId, MdcAlarmAnalyzeQueryVo vo) {
        MdcAlarmTrendDto result = new MdcAlarmTrendDto();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            // 单台设备信息
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
        if (vo.getEquipmentIdList() != null && !vo.getEquipmentIdList().isEmpty()) {
            result.setEquipmentList(vo.getEquipmentIdList());
            List<MdcAlarmDto> equipmentCountList = new ArrayList<>();
            String startDate = DateUtils.format(DateUtils.toDate(vo.getStartDate(), DateUtils.STRDATE), DateUtils.STR_DATE) + " 00:00:00";
            String endDate = DateUtils.format(DateUtils.addDays(DateUtils.toDate(vo.getEndDate(), DateUtils.STRDATE), 1), DateUtils.STR_DATE) + " 00:00:00";
            /*List<MdcAlarmListDto> alarmList = mdcEquipmentRunningSectionService.selectAlarmList(vo, startDate, endDate);
            result.setAlarmList(alarmList);*/
            for (String equipmentId : result.getEquipmentList()) {
                MdcAlarmDto mdcAlarmDto = new MdcAlarmDto();
                mdcAlarmDto.setKey(equipmentId);
                Integer count = mdcEquipmentRunningSectionService.findAlarmCount(equipmentId, startDate, endDate, vo.getAlarmCode());
                mdcAlarmDto.setCount(count);
                if (count != 0) {
                    equipmentCountList.add(mdcAlarmDto);
                }
            }
            result.setEquipmentCountList(equipmentCountList);
        }
 
        Date start = DateUtils.toDate(vo.getStartDate(), DateUtils.STRDATE);
        Date end = DateUtils.toDate(vo.getEndDate(), DateUtils.STRDATE);
        List<String> dateList = DateUtils.getDatesStringLists(start, end);
        result.setDateList(dateList);
        List<String> datesStringList = DateUtils.getDatesStringList(start, end);
        List<MdcAlarmDto> dateCountList = new ArrayList<>();
        for (String date : datesStringList) {
            MdcAlarmDto mdcAlarmDto = new MdcAlarmDto();
            mdcAlarmDto.setKey(DateUtils.format(DateUtils.toDate(date, DateUtils.STR_DATE), DateUtils.STR_MMDD));
            String startDate = date + " 00:00:00";
            String endDate = DateUtils.format(DateUtils.addDays(DateUtils.toDate(date, DateUtils.STR_DATE), 1), DateUtils.STR_DATE) + " 00:00:00";
            Integer count = mdcEquipmentRunningSectionService.findAlarmCountByDate(startDate, endDate, vo);
            mdcAlarmDto.setCount(count);
            dateCountList.add(mdcAlarmDto);
        }
        result.setDateCountList(dateCountList);
        return result;
    }
 
    @Override
    public List<MdcAlarmListDto> equipmentAlarmList(String userId, MdcAlarmAnalyzeQueryVo vo) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            // 单台设备信息
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        String startDate = DateUtils.format(DateUtils.toDate(vo.getStartDate(), DateUtils.STRDATE), DateUtils.STR_DATE) + " 00:00:00";
        String endDate = DateUtils.format(DateUtils.addDays(DateUtils.toDate(vo.getEndDate(), DateUtils.STRDATE), 1), DateUtils.STR_DATE) + " 00:00:00";
        return mdcEquipmentRunningSectionService.selectAlarmList(vo, startDate, endDate);
    }
}