AbstractReportService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package com.huaxu.service;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.huaxu.common.StringUtils;
  7. import com.huaxu.dto.*;
  8. import com.huaxu.entity.DayReportEntity;
  9. import com.huaxu.util.UserUtil;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import java.util.*;
  12. import java.util.stream.Collectors;
  13. /**
  14. * 报表业务处理
  15. * @author lihui
  16. * @date 2020-3-26
  17. */
  18. public abstract class AbstractReportService<M extends BaseMapper<T>, T> extends ServiceImpl {
  19. @Autowired
  20. protected SceneService sceneService;
  21. @Autowired
  22. protected DeviceService deviceService;
  23. @Autowired
  24. protected DeviceParmService deviceParmService;
  25. /**
  26. * 报表数据处理
  27. * @param queryDto
  28. * @param devices
  29. * @param reportPage
  30. */
  31. public void reportDataHandle(ReportQueryDto queryDto, List<DeviceDto> devices, Page<ReportDto> reportPage, boolean isPipe){
  32. // 组装查询参数
  33. ReportDto reportDto = getQueryReportDto(queryDto, devices, isPipe);
  34. // 设置报表标题
  35. Map<String, Integer> mapsTitle = new LinkedHashMap<>();
  36. Map<String, ReportAttributeDto> mapsAttributeTitle = new LinkedHashMap<>();
  37. // 获取标题
  38. List<ReportTitleDto> reportTitleDtos = getReportTitle(reportDto, mapsTitle, mapsAttributeTitle);
  39. // 固定参数项
  40. List<ReportDto> reportList = findReport(reportDto);
  41. int total = 0;
  42. // 单个属性值
  43. for (ReportDto item : reportPage.getRecords()) {
  44. item.setCollectDate(formateCollectDate(queryDto, item));
  45. item.setDeviceIds(devices);
  46. if (total == 0){
  47. item.setReportTitle(reportTitleDtos);
  48. }
  49. // 固定参数项
  50. calcUsage(item, reportList);
  51. //动态参数项
  52. List<ReportAttributeDto> reportAttributeDtos = this.findAttributeList(item);
  53. Map<String, ReportAttributeDto> mapData = new LinkedHashMap<>();
  54. for (ReportAttributeDto itemData : reportAttributeDtos) {
  55. if (!mapData.containsKey(itemData.getAttributeName())){
  56. mapData.put(itemData.getAttributeName(), itemData);
  57. }
  58. }
  59. // 获取统计维度的值
  60. item.setDataMapValues(getStatisticalDimensionsValue(mapsTitle, mapData, mapsAttributeTitle));
  61. total ++;
  62. }
  63. }
  64. /**
  65. * 查询场景下的所有设备信息
  66. * @param ids 场景ID
  67. * @return
  68. */
  69. public List<DeviceDto> findDevices(Long[] ids){
  70. List<DeviceDto> devices = new ArrayList<>();
  71. DeviceDto deviceDto = new DeviceDto();
  72. for (Long id : ids) {
  73. deviceDto.setSceneIds(sceneService.findByParentIdsLike(id));
  74. if (deviceDto.getSceneIds().size() > 0){
  75. devices.addAll(deviceService.selectList(deviceDto));
  76. }
  77. }
  78. return devices;
  79. }
  80. /**
  81. * ids转换场景集合对象
  82. * @param ids 场景ID
  83. * @return
  84. */
  85. public List<DeviceDto> devicesToList(Long[] ids){
  86. List<DeviceDto> devices = new ArrayList<>();
  87. for (Long id : ids) {
  88. DeviceDto deviceDto = new DeviceDto();
  89. deviceDto.setId(id);
  90. devices.add(deviceDto);
  91. }
  92. return devices;
  93. }
  94. /**
  95. * 1000汇算
  96. * @param value
  97. * @return
  98. */
  99. public double doubleRoundValue(double value){
  100. return (double) Math.round(value * 1000) / 1000;
  101. }
  102. /**
  103. * 组装参数
  104. * @param queryDto
  105. * @param devices
  106. * @return
  107. */
  108. public ReportDto getQueryReportDto(ReportQueryDto queryDto, List<DeviceDto> devices, boolean isPipe){
  109. ReportDto reportDto = new ReportDto();
  110. reportDto.setYear(queryDto.getYear());
  111. reportDto.setMonth(queryDto.getMonth());
  112. reportDto.setDay(queryDto.getDay());
  113. if (isPipe){
  114. Integer[] parmType = null;
  115. if (queryDto.getType() == 1) {
  116. parmType = new Integer[]{13};
  117. } else if (queryDto.getType() == 2){
  118. parmType = new Integer[]{14};
  119. } else if (queryDto.getType() == 3){
  120. parmType = new Integer[]{7, 9, 11};
  121. }
  122. reportDto.setDeviceIds(devices);
  123. reportDto.setParmType(Arrays.asList(parmType));
  124. } else {
  125. reportDto.setDeviceIds(devices);
  126. reportDto.setParentSceneIds(queryDto.getIds());
  127. }
  128. reportDto.setUserId(UserUtil.getCurrentUser().getId());
  129. reportDto.setStartTime(queryDto.getStartTime());
  130. reportDto.setEndTime(queryDto.getEndTime());
  131. return reportDto;
  132. }
  133. //根据设备ID查询报表测点信息
  134. protected ReportPageQueryDto getReportPageQuery(ReportQueryDto queryDto, List<DeviceDto> devices, boolean isPipe){
  135. ReportPageQueryDto dto = new ReportPageQueryDto();
  136. dto.setYear(queryDto.getYear());
  137. dto.setMonth(queryDto.getMonth());
  138. dto.setDay(queryDto.getDay());
  139. dto.setTenantId(UserUtil.getCurrentUser().getTenantId());
  140. dto.setDeviceIds(devices);
  141. // 管网和其他有点不一样
  142. if (isPipe){
  143. Integer[] parmType = null;
  144. if (queryDto.getType() == 1) {
  145. parmType = new Integer[]{13};
  146. } else if (queryDto.getType() == 2){
  147. parmType = new Integer[]{14};
  148. } else if (queryDto.getType() == 3){
  149. parmType = new Integer[]{7, 9, 11};
  150. }
  151. dto.setTypeIds(Arrays.asList(parmType));
  152. dto.setType(queryDto.getType());
  153. } else {
  154. dto.setParentSceneIds(queryDto.getIds());
  155. }
  156. dto.setStartTime(queryDto.getStartTime());
  157. dto.setEndTime(queryDto.getEndTime());
  158. return dto;
  159. }
  160. /**
  161. * 获取报表标题
  162. * @param reportDto
  163. * @param mapsTitle
  164. * @param mapsAttributeTitle
  165. * @return
  166. */
  167. public List<ReportTitleDto> getReportTitle(ReportDto reportDto, Map<String, Integer> mapsTitle,
  168. Map<String, ReportAttributeDto> mapsAttributeTitle){
  169. // 获取设备参数属性
  170. List<ReportAttributeDto> reportAttributeDtos2 = deviceParmService.findAttributeNameList(reportDto);
  171. int numCount = 0;
  172. for (ReportAttributeDto title : reportAttributeDtos2) {
  173. if (!mapsTitle.containsKey(title.getAttributeName())){
  174. mapsTitle.put(title.getAttributeName(), numCount++);
  175. mapsAttributeTitle.put(title.getAttributeName(),title);
  176. }
  177. }
  178. // 设置标题
  179. Map<String, ReportTitleDto> deviceChildrenMap = new HashMap<>();
  180. mapsAttributeTitle.forEach((k, v) -> {
  181. // 判断开关是否开启
  182. if (v.isShowOnOff()){
  183. setReportTitle(v, deviceChildrenMap);
  184. }
  185. });
  186. // 转换list返回
  187. return deviceChildrenMap.values().stream().collect(Collectors.toList());
  188. }
  189. /**
  190. * 获取统计维度的值
  191. * @param mapsTitle
  192. * @param mapData
  193. * @param mapsAttributeTitle
  194. * @return
  195. */
  196. public List<Map<String, Object>> getStatisticalDimensionsValue( Map<String, Integer> mapsTitle, Map<String, ReportAttributeDto> mapData, Map<String, ReportAttributeDto> mapsAttributeTitle){
  197. // 判断是否有需要展示的字段
  198. boolean haveShowTitle = false;
  199. List<Map<String, Object>> dataMapValues = new ArrayList<>();
  200. for (String key : mapsTitle.keySet()) {
  201. ReportAttributeDto reportAttributeDto = mapsAttributeTitle.get(key);
  202. // 过滤掉没有开启开关的
  203. if (!reportAttributeDto.isShowOnOff()) {
  204. continue;
  205. }
  206. Map<String, Object> map = new LinkedHashMap<>();
  207. map.put("attributeName", mapData.get(key) == null ? key : mapData.get(key).getAttributeName());
  208. if (reportAttributeDto.isShowMaxValue()) {
  209. map.put("maxValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getMaxValue() == null ?
  210. "-" : doubleRoundValue(mapData.get(key).getMaxValue()));
  211. haveShowTitle = true;
  212. }
  213. if (reportAttributeDto.isShowMinValue()) {
  214. map.put("minValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getMinValue() == null ?
  215. "-" : doubleRoundValue(mapData.get(key).getMinValue()));
  216. haveShowTitle = true;
  217. }
  218. if (reportAttributeDto.isShowSumValue()) {
  219. map.put("sumValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getSumValue() == null ?
  220. "-" : doubleRoundValue(mapData.get(key).getSumValue()));
  221. haveShowTitle = true;
  222. }
  223. if (reportAttributeDto.isShowLatestValue()) {
  224. map.put("latestValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getLatestValue() == null ?
  225. "-" : doubleRoundValue(mapData.get(key).getLatestValue()));
  226. haveShowTitle = true;
  227. }
  228. if (reportAttributeDto.isShowAvgValue()) {
  229. map.put("avgValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getAvgValue() == null ?
  230. "-" : doubleRoundValue(mapData.get(key).getAvgValue()));
  231. haveShowTitle = true;
  232. }
  233. if (!haveShowTitle){
  234. continue;
  235. }
  236. haveShowTitle = false;
  237. dataMapValues.add(map);
  238. }
  239. return dataMapValues;
  240. }
  241. /**
  242. * 设置报表标题
  243. * @param v
  244. * @param deviceChildrenMap
  245. */
  246. private void setReportTitle(ReportAttributeDto v, Map<String, ReportTitleDto> deviceChildrenMap){
  247. String classify = StringUtils.isEmpty(v.getClassify()) ? v.getAttributeName() : v.getClassify();
  248. ReportTitleDto reportTitleDto = deviceChildrenMap.get(classify);
  249. if (reportTitleDto == null) {
  250. reportTitleDto = new ReportTitleDto();
  251. reportTitleDto.setClassify(classify);
  252. }
  253. List<ReportTitleDto.DeviceChildren> deviceChildrenList = reportTitleDto.getDeviceChildren();
  254. if (CollectionUtil.isEmpty(deviceChildrenList)){
  255. deviceChildrenList = new ArrayList<>();
  256. }
  257. ReportTitleDto.DeviceChildren deviceChildren = new ReportTitleDto.DeviceChildren();
  258. deviceChildren.setName(v.getAttributeName());
  259. // 检查是否有设置维度
  260. List<ReportTitleDto.StatisticalDimension> statisticalDimensions = new ArrayList<>();
  261. if (v.isShowMaxValue()){
  262. statisticalDimensions.add(getStatisticalDimension("showMaxValue", "最大值"));
  263. }
  264. if (v.isShowMinValue()){
  265. statisticalDimensions.add(getStatisticalDimension("showMinValue", "最小值"));
  266. }
  267. if (v.isShowSumValue()){
  268. statisticalDimensions.add(getStatisticalDimension("showSumValue", "合计值"));
  269. }
  270. if (v.isShowLatestValue()){
  271. statisticalDimensions.add(getStatisticalDimension("showLatestValue", "最新值"));
  272. }
  273. if (v.isShowAvgValue()){
  274. statisticalDimensions.add(getStatisticalDimension("showAvgValue", "平均值"));
  275. }
  276. // 有设置维度,添加相关信息
  277. if (v.isShowMaxValue() || v.isShowMinValue() || v.isShowSumValue() || v.isShowLatestValue()
  278. || v.isShowAvgValue()){
  279. deviceChildren.setDimensionList(statisticalDimensions);
  280. deviceChildrenList.add(deviceChildren);
  281. reportTitleDto.setDeviceChildren(deviceChildrenList);
  282. deviceChildrenMap.put(classify, reportTitleDto);
  283. }
  284. }
  285. /**
  286. * 获取统计维度
  287. * @param key
  288. * @param name
  289. * @return
  290. */
  291. private ReportTitleDto.StatisticalDimension getStatisticalDimension(String key, String name){
  292. ReportTitleDto.StatisticalDimension statisticalDimension = new ReportTitleDto.StatisticalDimension();
  293. statisticalDimension.setDimensionKey(key);
  294. statisticalDimension.setDimensionName(name);
  295. return statisticalDimension;
  296. }
  297. /**
  298. * @Author lihui
  299. * @Description 格式化日期
  300. * @Date 16:38 2021/4/15
  301. * @Param [queryDto, item]
  302. * @return java.lang.String
  303. **/
  304. private String formateCollectDate(ReportQueryDto queryDto, ReportDto item){
  305. // 0日 1月 2年
  306. if (queryDto.getReportType() == 0) {
  307. return item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()) + " " + String.format("%02d", item.getHour()) + ":00:00";
  308. }
  309. if (queryDto.getReportType() == 1 || queryDto.getReportType() == 3) {
  310. return item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay());
  311. }
  312. if (queryDto.getReportType() == 2) {
  313. return item.getYear() + "-" + String.format("%02d", item.getMonth());
  314. }
  315. return null;
  316. }
  317. /**
  318. * 查询报表
  319. * @param reportDto
  320. * @return
  321. */
  322. abstract List<ReportDto> findReport(ReportDto reportDto);
  323. /**
  324. * 计算值
  325. * @param item
  326. * @param reportDtos
  327. */
  328. abstract void calcUsage(ReportDto item, List<ReportDto> reportDtos);
  329. /**
  330. * 查询属性
  331. * @param item
  332. * @return
  333. */
  334. abstract List<ReportAttributeDto> findAttributeList(ReportDto item);
  335. }