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;
|
}
|
}
|