YearReportService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 AbstractReportService<YearReportMapper, YearReportEntity> {
  28. @Resource
  29. private YearReportMapper yearReportMapper;
  30. /**
  31. * 查列表
  32. */
  33. public List<YearReportEntity> findList(YearReportEntity yearReportEntity) {
  34. return yearReportMapper.findList(yearReportEntity);
  35. }
  36. /**
  37. * 保存
  38. */
  39. public boolean addYearReport(YearReportEntity yearReport) {
  40. if (this.save(yearReport)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. /**
  46. * 根居ID获取对象
  47. */
  48. public YearReportEntity findYearReportById(Long id) {
  49. return yearReportMapper.findYearReportById(id);
  50. }
  51. public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
  52. LoginUser currentUser = UserUtil.getCurrentUser();
  53. Page<ReportDto> reportPage = new Page<>();
  54. //查询场景下的所有设备信息
  55. List<DeviceDto> devices = new ArrayList<>();
  56. DeviceDto deviceDto = new DeviceDto();
  57. for (Long id : ids) {
  58. deviceDto.setSceneIds(sceneService.findByParentIdsLike(id));
  59. if (deviceDto.getSceneIds().size() > 0) {
  60. devices.addAll(deviceService.selectList(deviceDto));
  61. }
  62. }
  63. if(devices.size()>0) {
  64. YearReportEntity yearReportEntity = new YearReportEntity();
  65. yearReportEntity.setYear(year);
  66. yearReportEntity.setTenantId(currentUser.getTenantId());
  67. yearReportEntity.setParentSceneIds(ids);
  68. yearReportEntity.setDeviceIds(devices);
  69. reportPage = yearReportMapper.findPage(page, yearReportEntity);
  70. //查询标题
  71. ReportDto reportDto = new ReportDto();
  72. reportDto.setYear(year);
  73. reportDto.setDeviceIds(devices);
  74. reportDto.setParentSceneIds(ids);
  75. List<ReportAttributeDto> reportAttributeDtos2 = deviceParmService.findAttributeNameList(reportDto);
  76. List<String> dataTitle = new ArrayList<>();
  77. Map<String, Integer> mapsTitle = new LinkedHashMap<>();
  78. int numCount = 0;
  79. for (ReportAttributeDto title : reportAttributeDtos2) {
  80. dataTitle.add(title.getAttributeName());
  81. if (!mapsTitle.containsKey(title.getAttributeName())) {
  82. mapsTitle.put(title.getAttributeName(), numCount++);
  83. }
  84. }
  85. //固定参数项
  86. List<ReportDto> reportDtos = yearReportMapper.findReport(reportDto);
  87. //单个属性值
  88. for (ReportDto item : reportPage.getRecords()) {
  89. item.setCollectDate(item.getYear() + "-" + String.format("%02d", item.getMonth()));
  90. item.setDeviceIds(devices);
  91. item.setDataTitle(dataTitle);
  92. //固定参数项
  93. calcUsage(item, reportDtos);
  94. //动态参数项
  95. List<ReportAttributeDto> reportAttributeDtos = yearReportMapper.findAttributeList(item);
  96. // item.setDataValues(reportAttributeDtos);
  97. Map<String, ReportAttributeDto> mapData = new LinkedHashMap<>();
  98. for (ReportAttributeDto itemData : reportAttributeDtos) {
  99. if (!mapData.containsKey(itemData.getAttributeName())) {
  100. mapData.put(itemData.getAttributeName(), itemData);
  101. }
  102. }
  103. List<Map<String, Object>> maps = new ArrayList<>();
  104. for (String key : mapsTitle.keySet()) {
  105. Map<String, Object> map = new LinkedHashMap<>();
  106. map.put("attributeName", mapData.get(key) == null ? key : mapData.get(key).getAttributeName());
  107. map.put("minValue" + mapsTitle.get(key), mapData.get(key) == null||mapData.get(key).getMinValue()==null ? "-" : (double)Math.round(mapData.get(key).getMinValue()*1000)/1000);
  108. map.put("maxValue" + mapsTitle.get(key), mapData.get(key) == null||mapData.get(key).getMaxValue()==null ? "-" : (double)Math.round(mapData.get(key).getMaxValue()*1000)/1000);
  109. map.put("avgValue" + mapsTitle.get(key), mapData.get(key) == null||mapData.get(key).getAvgValue()==null ? "-" : (double)Math.round(mapData.get(key).getAvgValue()*1000)/1000);
  110. map.put("sumValue" + mapsTitle.get(key), mapData.get(key) == null||mapData.get(key).getSumValue()==null ? "-" : (double)Math.round(mapData.get(key).getSumValue()*1000)/1000);
  111. map.put("latestValue" + mapsTitle.get(key), mapData.get(key) == null||mapData.get(key).getLatestValue()==null ? "-" : (double)Math.round(mapData.get(key).getLatestValue()*1000)/1000);
  112. maps.add(map);
  113. }
  114. item.setDataMapValues(maps);
  115. }
  116. }
  117. return reportPage;
  118. }
  119. //计算固定的参数项
  120. @Override
  121. public void calcUsage(ReportDto item, List<ReportDto> reportDtos) {
  122. if (item == null || reportDtos.size()==0 ) {
  123. return;
  124. }
  125. // 固定参数项
  126. int index = -1;
  127. for (int i = 0; i < reportDtos.size(); i++) {
  128. if (reportDtos.get(i).getMonth().equals(item.getMonth())){
  129. index = i;
  130. }
  131. }
  132. if (index == -1) {
  133. return;
  134. }
  135. Double power = reportDtos!=null&&reportDtos.get(index)!=null?reportDtos.get(index).getPowerUsage():0;
  136. Double water = reportDtos!=null&&reportDtos.get(index)!=null?reportDtos.get(index).getIntakeWaterUsage():0;
  137. Double yieldWaterUsage = reportDtos!=null&&reportDtos.get(index)!=null? reportDtos.get(index).getYieldWaterUsage():0;
  138. if (item.getSceneTypeName() != null && "水源水厂".indexOf(item.getSceneTypeName()) != -1){
  139. item.setPowerUsage(doubleDivideValue(power));
  140. item.setIntakeWaterUsage(doubleDivideValue(water));
  141. item.setYieldWaterUsage(doubleDivideValue(yieldWaterUsage));
  142. } else {
  143. item.setPowerUsage(power);
  144. item.setIntakeWaterUsage(water);
  145. item.setYieldWaterUsage(yieldWaterUsage);
  146. }
  147. item.setDrugUsage(reportDtos!=null&&reportDtos.get(index)!=null?reportDtos.get(index).getDrugUsage():0);
  148. }
  149. public Page<DevicePressReportAttributeDto> findDevicePressPage(IPage<DevicePressReportAttributeDto> page, Long[] ids, Integer year, Integer month, Integer day,Integer type) {
  150. LoginUser currentUser = UserUtil.getCurrentUser();
  151. Page<DevicePressReportAttributeDto> reportPage = new Page<>();
  152. //查询场景下的所有设备信息
  153. List<DeviceDto> devices = new ArrayList<>();
  154. for (Long id : ids) {
  155. DeviceDto deviceDto = new DeviceDto();
  156. deviceDto.setId(id);
  157. devices.add(deviceDto);
  158. }
  159. if (devices.size() > 0) {
  160. //根据设备ID查询报表测点信息
  161. YearReportEntity yearReportEntity = new YearReportEntity();
  162. yearReportEntity.setYear(year);
  163. yearReportEntity.setTenantId(currentUser.getTenantId());
  164. yearReportEntity.setType(type);
  165. yearReportEntity.setDeviceIds(devices);
  166. //查询到分页的行数
  167. reportPage = yearReportMapper.findDevicePressPage(page, yearReportEntity);
  168. }
  169. return reportPage;
  170. }
  171. public Page<DeviceWaterReportAttributeDto> findDeviceWaterPage(IPage<DeviceWaterReportAttributeDto> page, Long[] ids, Integer year, Integer month, Integer day) {
  172. LoginUser currentUser = UserUtil.getCurrentUser();
  173. Page<DeviceWaterReportAttributeDto> reportPage = new Page<>();
  174. //查询场景下的所有设备信息
  175. List<DeviceDto> devices = new ArrayList<>();
  176. for (Long id : ids) {
  177. DeviceDto deviceDto = new DeviceDto();
  178. deviceDto.setId(id);
  179. devices.add(deviceDto);
  180. }
  181. if (devices.size() > 0) {
  182. //根据设备ID查询报表测点信息
  183. YearReportEntity yearReportEntity = new YearReportEntity();
  184. yearReportEntity.setYear(year);
  185. yearReportEntity.setTenantId(currentUser.getTenantId());
  186. yearReportEntity.setDeviceIds(devices);
  187. //查询到分页的行数
  188. reportPage = yearReportMapper.findDeviceWaterPage(page, yearReportEntity);
  189. }
  190. return reportPage;
  191. }
  192. public List<YearReportEntity> findAmountTotalBySceneIds(YearReportEntity yearReportEntity) {
  193. return yearReportMapper.findAmountTotalBySceneIds(yearReportEntity);
  194. }
  195. @Override
  196. List<ReportDto> findReport(ReportDto reportDto) {
  197. return yearReportMapper.findReport(reportDto);
  198. }
  199. @Override
  200. List<ReportAttributeDto> findAttributeList(ReportDto item) {
  201. return yearReportMapper.findAttributeList(item);
  202. }
  203. public Page<ReportDto> findDevicePressPageReport(IPage<ReportDto> page, ReportQueryDto queryDto) {
  204. List<DeviceDto> devices = devicesToList(queryDto.getIds());
  205. if (devices.size() == 0 ){
  206. return new Page<>();
  207. }
  208. Page<ReportDto> reportPage = yearReportMapper.findPageReprot(page, getReportPageQuery(queryDto, devices, true));
  209. reportDataHandle(queryDto, devices, reportPage, true);
  210. return reportPage;
  211. }
  212. public Page<ReportDto> findPageReport(IPage<ReportDto> page, ReportQueryDto queryDto) {
  213. // 查询场景下的所有设备信息
  214. List<DeviceDto> devices = findDevices(queryDto.getIds());
  215. if (devices.size() == 0 ){
  216. return new Page<>();
  217. }
  218. Page<ReportDto> reportPage = yearReportMapper.findPageReprot(page, getReportPageQuery(queryDto, devices, false));
  219. reportDataHandle(queryDto, devices, reportPage, false);
  220. return reportPage;
  221. }
  222. }