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 implements DeviceHealthReportService { @Resource private DeviceHealthReportDataService deviceHealthReportDataService; @Resource private DeviceHealthAlarmService deviceHealthAlarmService; @Resource private DeviceHealthAlarmDetailService deviceHealthAlarmDetailService; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, Constant.CREATE_DATE, false), getWrapper(params) ); return getPageData(page, DeviceHealthReportDTO.class); } private QueryWrapper getWrapper(Map 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 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 detList = deviceHealthReportDataService.listByReportId(id); result.setDetList(detList); List chartList = new ArrayList<>(); if (CollectionUtils.isEmpty(detList)) { result.setChartList(chartList); return result; } detList.forEach(item -> { BarLineDTO dto = new BarLineDTO(); dto.setValueName(item.getDataName()); List legend = new ArrayList<>(); List series = new ArrayList<>(); List categories = new ArrayList<>(); legend.add(item.getDataName()); SeriesItem seriesItem = new SeriesItem(); seriesItem.setName(item.getDataName()); List 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 deviceHealthAlarmDetails = dto.getDeviceHealthAlarmDetails(); List deviceHealthAlarmDetailEntities = ConvertUtils.sourceToTarget(deviceHealthAlarmDetails, DeviceHealthAlarmDetailEntity.class); deviceHealthAlarmDetailService.insertBatch(deviceHealthAlarmDetailEntities); } } @Override public void migrationDeviceHealthReport(Map tMap) { List list = baseDao.selectList(getDateWrapper(tMap)); if (CollectionUtils.isEmpty(list)){ return; } baseDao.migration(list); baseDao.delete(getDateWrapper(tMap)); } public QueryWrapper getDateWrapper(Map params) { String startDate = DateUtils.format(params.get("startdate"),DateUtils.DATE_TIME_PATTERN); String endDate = DateUtils.format(params.get("enddate"),DateUtils.DATE_TIME_PATTERN); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.ge(StringUtils.isNotBlank(startDate), "report_date", startDate); wrapper.le(StringUtils.isNotBlank(endDate), "report_date", endDate); return wrapper; } }