houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.device.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iailab.framework.common.constant.Constant;
import com.iailab.common.dto.echarts.BarLineDTO;
import com.iailab.common.dto.echarts.SeriesItem;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.common.utils.DateUtils;
import com.iailab.module.device.dao.DeviceHealthReportDao;
import com.iailab.module.device.dto.DeviceHealthAlarmDTO;
import com.iailab.module.device.dto.DeviceHealthAlarmDetailDTO;
import com.iailab.module.device.dto.DeviceHealthReportDTO;
import com.iailab.module.device.dto.DeviceHealthReportDataDTO;
import com.iailab.module.device.entity.DeviceHealthAlarmDetailEntity;
import com.iailab.module.device.entity.DeviceHealthAlarmEntity;
import com.iailab.module.device.entity.DeviceHealthReportEntity;
import com.iailab.module.device.service.DeviceHealthAlarmService;
import com.iailab.module.device.service.DeviceHealthAlarmDetailService;
import com.iailab.module.device.service.DeviceHealthReportDataService;
import com.iailab.module.device.service.DeviceHealthReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年06月28日
 */
@Slf4j
@Service
public class DeviceHealthReportServiceImpl extends BaseServiceImpl<DeviceHealthReportDao, DeviceHealthReportEntity>
        implements DeviceHealthReportService {
 
    @Resource
    private DeviceHealthReportDataService deviceHealthReportDataService;
    @Resource
    private DeviceHealthAlarmService deviceHealthAlarmService;
    @Resource
    private DeviceHealthAlarmDetailService deviceHealthAlarmDetailService;
 
    @Override
    public PageData<DeviceHealthReportDTO> page(Map<String, Object> params) {
        IPage<DeviceHealthReportEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, DeviceHealthReportDTO.class);
    }
 
    private QueryWrapper<DeviceHealthReportEntity> getWrapper(Map<String, Object> params) {
        String devNo = (String) params.get("devNo");
        String devName = (String) params.get("devName");
        String reportName = (String) params.get("reportName");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        QueryWrapper<DeviceHealthReportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(devNo), "dev_no", devNo)
                .like(StringUtils.isNotBlank(devName), "dev_name", devName)
                .like(StringUtils.isNotBlank(reportName), "report_name", reportName)
                .ge(StringUtils.isNotBlank(startTime), "report_date", startTime)
                .le(StringUtils.isNotBlank(endTime), "report_date", endTime);
        return wrapper;
    }
 
    @Override
    public DeviceHealthReportDTO get(String id) {
        DeviceHealthReportEntity entity = baseDao.selectById(id);
        DeviceHealthReportDTO result = ConvertUtils.sourceToTarget(entity, DeviceHealthReportDTO.class);
        List<DeviceHealthReportDataDTO> detList = deviceHealthReportDataService.listByReportId(id);
        result.setDetList(detList);
 
        List<BarLineDTO> chartList = new ArrayList<>();
        if (CollectionUtils.isEmpty(detList)) {
            result.setChartList(chartList);
            return result;
        }
        detList.forEach(item -> {
            BarLineDTO dto = new BarLineDTO();
            dto.setValueName(item.getDataName());
            List<String> legend = new ArrayList<>();
            List<SeriesItem> series = new ArrayList<>();
            List<String> categories = new ArrayList<>();
 
            legend.add(item.getDataName());
            SeriesItem seriesItem = new SeriesItem();
            seriesItem.setName(item.getDataName());
            List<Object[]> sData = new ArrayList<>();
            if (StringUtils.isNotBlank(item.getDataValue())) {
                sData = JSONArray.parseArray(item.getDataValue(), Object[].class);
            }
            seriesItem.setData(sData);
            series.add(seriesItem);
            series.get(0).getData().forEach(v -> {
                categories.add(v[0].toString());
            });
 
            dto.setLegend(legend);
            dto.setSeries(series);
            dto.setCategories(categories);
            chartList.add(dto);
        });
        result.setChartList(chartList);
        return result;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DeviceHealthReportDTO dto) {
        DeviceHealthReportEntity entity = ConvertUtils.sourceToTarget(dto, DeviceHealthReportEntity.class);
        entity.setCreateDate(new Date());
        insert(entity);
        if (!CollectionUtils.isEmpty(dto.getDetList())) {
            for (int i = 0; i < dto.getDetList().size(); i++) {
                DeviceHealthReportDataDTO det = dto.getDetList().get(i);
                det.setReportId(entity.getId());
                det.setSort(i);
                deviceHealthReportDataService.save(det);
            }
        }
 
        // 设备健康故障记录
        DeviceHealthAlarmDTO deviceHealthAlarm = dto.getDeviceHealthAlarm();
        if ( null != deviceHealthAlarm ){
            DeviceHealthAlarmEntity deviceHealthAlarmEntitie = ConvertUtils.sourceToTarget(deviceHealthAlarm, DeviceHealthAlarmEntity.class);
            deviceHealthAlarmService.insert(deviceHealthAlarmEntitie);
 
            // 设备健康故障详情记录
            List<DeviceHealthAlarmDetailDTO> deviceHealthAlarmDetails = dto.getDeviceHealthAlarmDetails();
            List<DeviceHealthAlarmDetailEntity> deviceHealthAlarmDetailEntities = ConvertUtils.sourceToTarget(deviceHealthAlarmDetails, DeviceHealthAlarmDetailEntity.class);
            deviceHealthAlarmDetailService.insertBatch(deviceHealthAlarmDetailEntities);
        }
 
    }
 
    @Override
    public void migrationDeviceHealthReport(Map<String, Date> tMap) {
        List<DeviceHealthReportEntity> list = baseDao.selectList(getDateWrapper(tMap));
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        baseDao.migration(list);
        baseDao.delete(getDateWrapper(tMap));
    }
 
    public QueryWrapper<DeviceHealthReportEntity> getDateWrapper(Map<String, Date> params) {
        String startDate = DateUtils.format(params.get("startdate"),DateUtils.DATE_TIME_PATTERN);
        String endDate = DateUtils.format(params.get("enddate"),DateUtils.DATE_TIME_PATTERN);
 
        QueryWrapper<DeviceHealthReportEntity> wrapper = new QueryWrapper<>();
        wrapper.ge(StringUtils.isNotBlank(startDate), "report_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "report_date", endDate);
        return wrapper;
    }
}