MonthReportService.java 9.0 KB

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