DayReportService.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.huaxu.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.huaxu.dao.DayReportMapper;
  4. import com.huaxu.dto.DeviceDto;
  5. import com.huaxu.dto.ReportAttributeDto;
  6. import com.huaxu.dto.ReportDto;
  7. import com.huaxu.entity.DayReportEntity;
  8. import com.huaxu.model.LoginUser;
  9. import com.huaxu.util.UserUtil;
  10. import org.apache.ibatis.annotations.Param;
  11. import org.apache.xmlbeans.impl.xb.xsdschema.BlockSet;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.mongodb.core.aggregation.DateOperators;
  14. import org.springframework.stereotype.Service;
  15. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  18. import javax.annotation.Resource;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.*;
  21. /**
  22. * 日报Service接口
  23. *
  24. * @author: WYY
  25. * @date 2020-12-03 16:20
  26. */
  27. @Service
  28. public class DayReportService extends ServiceImpl<DayReportMapper, DayReportEntity> {
  29. @Resource
  30. private DayReportMapper dayReportMapper;
  31. @Autowired
  32. private SceneService sceneService;
  33. @Autowired
  34. private DeviceService deviceService;
  35. @Autowired
  36. private DeviceParmService deviceParmService;
  37. /**
  38. * 查列表
  39. */
  40. public List<DayReportEntity> findList(DayReportEntity dayReportEntity) {
  41. return dayReportMapper.findList(dayReportEntity);
  42. }
  43. /**
  44. * 保存
  45. */
  46. public boolean addDayReport(DayReportEntity dayReport) {
  47. if (this.save(dayReport)) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * 根居ID获取对象
  54. */
  55. public DayReportEntity findDayReportById(Long id) {
  56. return dayReportMapper.findDayReportById(id);
  57. }
  58. public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
  59. LoginUser currentUser = UserUtil.getCurrentUser();
  60. Page<ReportDto> reportPage = new Page<>();
  61. //查询场景下的所有设备信息
  62. List<DeviceDto> devices = new ArrayList<>();
  63. DeviceDto deviceDto = new DeviceDto();
  64. for (Long id : ids) {
  65. deviceDto.setSceneIds(sceneService.findByParentIdsLike(id));
  66. if (deviceDto.getSceneIds().size() > 0)
  67. devices.addAll(deviceService.selectList(deviceDto));
  68. }
  69. if (devices.size() > 0) {
  70. //根据设备ID查询报表测点信息
  71. DayReportEntity dayReportEntity = new DayReportEntity();
  72. dayReportEntity.setYear(year);
  73. dayReportEntity.setMonth(month);
  74. dayReportEntity.setDay(day);
  75. dayReportEntity.setTenantId(currentUser.getTenantId());
  76. dayReportEntity.setParentSceneIds(ids);
  77. dayReportEntity.setDeviceIds(devices);
  78. //查询到分页的行数
  79. reportPage = dayReportMapper.findPage(page, dayReportEntity);
  80. //查询标题
  81. ReportDto reportDto = new ReportDto();
  82. reportDto.setYear(year);
  83. reportDto.setMonth(month);
  84. reportDto.setDay(day);
  85. reportDto.setDeviceIds(devices);
  86. List<ReportAttributeDto> reportAttributeDtos2 = deviceParmService.findAttributeNameList(reportDto);
  87. List<String> dataTitle = new ArrayList<>();
  88. Map<String, Integer> mapsTitle = new LinkedHashMap<>();
  89. int numCount = 0;
  90. for (ReportAttributeDto title : reportAttributeDtos2) {
  91. dataTitle.add(title.getAttributeName());
  92. if (!mapsTitle.containsKey(title.getAttributeName()))
  93. mapsTitle.put(title.getAttributeName(), numCount++);
  94. }
  95. //固定参数项
  96. List<ReportDto> reportDtos = dayReportMapper.findReport(reportDto);
  97. //单个属性值
  98. for (ReportDto item : reportPage.getRecords()) {
  99. item.setCollectDate(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()) + " " + String.format("%02d", item.getHour()) + ":00");
  100. item.setDeviceIds(devices);
  101. item.setDataTitle(dataTitle);
  102. //固定参数项
  103. calcUsage(item, reportDtos);
  104. //动态参数项
  105. List<ReportAttributeDto> reportAttributeDtos = dayReportMapper.findAttributeList(item);
  106. //item.setDataValues(reportAttributeDtos);
  107. Map<String, ReportAttributeDto> mapData = new LinkedHashMap<>();
  108. for (ReportAttributeDto itemData : reportAttributeDtos) {
  109. if (!mapData.containsKey(itemData.getAttributeName()))
  110. mapData.put(itemData.getAttributeName(), itemData);
  111. }
  112. List<Map<String, Object>> maps = new ArrayList<>();
  113. for (String key : mapsTitle.keySet()) {
  114. Map<String, Object> map = new LinkedHashMap<>();
  115. map.put("attributeName", mapData.get(key) == null ? key : mapData.get(key).getAttributeName());
  116. map.put("minValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getMinValue());
  117. map.put("maxValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getMaxValue());
  118. map.put("avgValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getAvgValue());
  119. map.put("sumValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getSumValue());
  120. map.put("latestValue" + mapsTitle.get(key), mapData.get(key) == null ? "-" : mapData.get(key).getLatestValue());
  121. maps.add(map);
  122. }
  123. item.setDataMapValues(maps);
  124. }
  125. }
  126. return reportPage;
  127. }
  128. //计算固定的参数项
  129. private void calcUsage(ReportDto item, List<ReportDto> reportDtos) {
  130. //固定参数项
  131. int index = -1;
  132. for (int i = 0; i < reportDtos.size(); i++) {
  133. if (reportDtos.get(i).getHour().equals(item.getHour()))
  134. index = i;
  135. }
  136. if (index != -1) {
  137. if ((index + 2) <= reportDtos.size()) {
  138. item.setYieldWaterUsage(reportDtos.get(index).getYieldWaterUsage() - reportDtos.get(index + 1).getYieldWaterUsage());
  139. item.setIntakeWaterUsage(reportDtos.get(index).getIntakeWaterUsage() - reportDtos.get(index + 1).getIntakeWaterUsage());
  140. item.setPowerUsage(reportDtos.get(index).getPowerUsage() - reportDtos.get(index + 1).getPowerUsage());
  141. item.setDrugUsage(reportDtos.get(index).getDrugUsage() - reportDtos.get(index + 1).getDrugUsage());
  142. } else {
  143. item.setYieldWaterUsage(0d);
  144. item.setIntakeWaterUsage(0d);
  145. item.setPowerUsage(0d);
  146. item.setDrugUsage(0d);
  147. }
  148. }
  149. }
  150. }