YearReportService.java 9.8 KB

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