YearReportService.java 11 KB

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