package com.huaxu.service; 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.huaxu.dao.DayReportMapper; import com.huaxu.dao.MonthReportMapper; import com.huaxu.dto.*; import com.huaxu.entity.DayReportEntity; import com.huaxu.entity.MonthReportEntity; import com.huaxu.model.LoginUser; import com.huaxu.util.UserUtil; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; /** * 日报Service接口 * * @author: WYY * @date 2020-12-03 16:20 */ @Service public class MonthReportService extends ServiceImpl { @Resource private MonthReportMapper monthReportMapper; @Autowired private SceneService sceneService; @Autowired private DeviceService deviceService; @Autowired private DeviceParmService deviceParmService; /** * 查列表 */ public List findList(MonthReportEntity monthReportEntity) { return monthReportMapper.findList(monthReportEntity); } /** * 保存 */ public boolean addMonthReport(MonthReportEntity monthReport) { if (this.save(monthReport)) { return true; } return false; } /** * 根居ID获取对象 */ public MonthReportEntity findMonthReportById(Long id) { return monthReportMapper.findMonthReportById(id); } public Page findPage(IPage page, Long[] ids, Integer year, Integer month, Integer day) { LoginUser currentUser = UserUtil.getCurrentUser(); Page reportPage = new Page<>(); //查询场景下的所有设备信息 List devices = new ArrayList<>(); DeviceDto deviceDto = new DeviceDto(); for (Long id : ids) { deviceDto.setSceneIds(sceneService.findByParentIdsLike(id)); if (deviceDto.getSceneIds().size() > 0) devices.addAll(deviceService.selectList(deviceDto)); } if (devices.size() > 0) { MonthReportEntity monthReportEntity = new MonthReportEntity(); monthReportEntity.setYear(year); monthReportEntity.setMonth(month); monthReportEntity.setTenantId(currentUser.getTenantId()); monthReportEntity.setParentSceneIds(ids); monthReportEntity.setDeviceIds(devices); reportPage = monthReportMapper.findPage(page, monthReportEntity); //查询标题 ReportDto reportDto = new ReportDto(); reportDto.setYear(year); reportDto.setMonth(month); reportDto.setDeviceIds(devices); List reportAttributeDtos2 = deviceParmService.findAttributeNameList(reportDto); List dataTitle = new ArrayList<>(); Map mapsTitle = new LinkedHashMap<>(); int numCount = 0; for (ReportAttributeDto title : reportAttributeDtos2) { dataTitle.add(title.getAttributeName()); if (!mapsTitle.containsKey(title.getAttributeName())) mapsTitle.put(title.getAttributeName(), numCount++); } //固定参数项 List reportDtos = monthReportMapper.findReport(reportDto); //单个属性值 for (ReportDto item : reportPage.getRecords()) { item.setCollectDate(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay())); item.setDeviceIds(devices); item.setDataTitle(dataTitle); //固定参数项 calcUsage(item, reportDtos); //动态参数项 List reportAttributeDtos = monthReportMapper.findAttributeList(item); // item.setDataValues(reportAttributeDtos); Map mapData = new LinkedHashMap<>(); for (ReportAttributeDto itemData : reportAttributeDtos) { if (!mapData.containsKey(itemData.getAttributeName())) mapData.put(itemData.getAttributeName(), itemData); } List> maps = new ArrayList<>(); for (String key : mapsTitle.keySet()) { Map map = new LinkedHashMap<>(); map.put("attributeName", mapData.get(key) == null ? key : mapData.get(key).getAttributeName()); map.put("minValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getMinValue()); map.put("maxValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getMaxValue()); map.put("avgValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getAvgValue()); map.put("sumValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getSumValue()); map.put("latestValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getLatestValue()); maps.add(map); } item.setDataMapValues(maps); } } return reportPage; } //计算固定的参数项 private void calcUsage(ReportDto item, List reportDtos) { //固定参数项 int index = -1; for (int i = 0; i < reportDtos.size(); i++) { if (reportDtos.get(i).getDay().equals(item.getDay())) index = i; } if (index != -1) { if ((index + 2) <= reportDtos.size()) { item.setYieldWaterUsage(reportDtos.get(index).getYieldWaterUsage() - reportDtos.get(index + 1).getYieldWaterUsage()); item.setIntakeWaterUsage(reportDtos.get(index).getIntakeWaterUsage() - reportDtos.get(index + 1).getIntakeWaterUsage()); item.setPowerUsage(reportDtos.get(index).getPowerUsage() - reportDtos.get(index + 1).getPowerUsage()); item.setDrugUsage(reportDtos.get(index).getDrugUsage() - reportDtos.get(index + 1).getDrugUsage()); } else { item.setYieldWaterUsage(0d); item.setIntakeWaterUsage(0d); item.setPowerUsage(0d); item.setDrugUsage(0d); } } } public Page findDevicePressPage(IPage page, Long[] ids, Integer year, Integer month, Integer day,Integer type) { LoginUser currentUser = UserUtil.getCurrentUser(); Page reportPage = new Page<>(); //查询场景下的所有设备信息 List devices = new ArrayList<>(); for (Long id : ids) { DeviceDto deviceDto = new DeviceDto(); deviceDto.setId(id); devices.add(deviceDto); } //根据设备ID查询报表测点信息 MonthReportEntity monthReportEntity = new MonthReportEntity(); monthReportEntity.setYear(year); monthReportEntity.setMonth(month); monthReportEntity.setTenantId(currentUser.getTenantId()); monthReportEntity.setType(type); monthReportEntity.setDeviceIds(devices); //查询到分页的行数 reportPage = monthReportMapper.findDevicePressPage(page, monthReportEntity); return reportPage; } public Page findDeviceWaterPage(IPage page, Long[] ids, Integer year, Integer month, Integer day) { LoginUser currentUser = UserUtil.getCurrentUser(); Page reportPage = new Page<>(); //查询场景下的所有设备信息 List devices = new ArrayList<>(); for (Long id : ids) { DeviceDto deviceDto = new DeviceDto(); deviceDto.setId(id); devices.add(deviceDto); } //根据设备ID查询报表测点信息 MonthReportEntity monthReportEntity = new MonthReportEntity(); monthReportEntity.setYear(year); monthReportEntity.setMonth(month); monthReportEntity.setTenantId(currentUser.getTenantId()); monthReportEntity.setDeviceIds(devices); //查询到分页的行数 reportPage = monthReportMapper.findDeviceWaterPage(page, monthReportEntity); return reportPage; } public List findAmountBySceneIds(MonthReportEntity monthReportEntity) { return monthReportMapper.findAmountBySceneIds(monthReportEntity); } public List findAmountTotalBySceneIds(MonthReportEntity monthReportEntity) { return monthReportMapper.findAmountTotalBySceneIds(monthReportEntity); } }