AlarmPowerServiceImpl.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.bizmatics.service.impl;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  5. import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.bizmatics.common.core.bean.CommonPage;
  8. import com.bizmatics.common.core.exception.BusinessException;
  9. import com.bizmatics.common.core.util.BeanMapperUtils;
  10. import com.bizmatics.common.core.util.DateUtils;
  11. import com.bizmatics.common.core.util.FileUtils;
  12. import com.bizmatics.common.spring.util.GlobalUtils;
  13. import com.bizmatics.model.AlarmPower;
  14. import com.bizmatics.persistence.mapper.AlarmPowerMapper;
  15. import com.bizmatics.service.AlarmPowerService;
  16. import com.bizmatics.common.mvc.base.AbstractCrudService;
  17. import com.bizmatics.service.util.SessionLocal;
  18. import com.bizmatics.service.vo.AlarmPowerExportVO;
  19. import com.bizmatics.service.vo.ApCountVO;
  20. import com.bizmatics.service.vo.CommonIcoVO;
  21. import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
  22. import org.apache.ibatis.annotations.Param;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.checkerframework.checker.units.qual.A;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.util.CollectionUtils;
  27. import org.springframework.util.unit.DataUnit;
  28. import java.text.SimpleDateFormat;
  29. import java.time.Duration;
  30. import java.util.*;
  31. import java.io.File;
  32. import java.io.FileOutputStream;
  33. import java.io.IOException;
  34. import java.util.*;
  35. /**
  36. * <p>
  37. * 电力告警 服务实现类
  38. * </p>
  39. *
  40. * @author ya
  41. * @since 2021-07-07
  42. */
  43. @Service
  44. public class AlarmPowerServiceImpl extends AbstractCrudService<AlarmPowerMapper, AlarmPower> implements AlarmPowerService {
  45. @Override
  46. public ApCountVO selectCount(Integer siteId) {
  47. Integer userId = SessionLocal.getUserId();
  48. ApCountVO apCountVo = new ApCountVO();
  49. apCountVo.setCount(baseMapper.selectCount(userId, siteId, null, null, null, null, null));
  50. apCountVo.setUnCount(baseMapper.selectCount(userId, siteId, 0, null, null, null, null));
  51. return apCountVo;
  52. }
  53. @Override
  54. public List<CommonIcoVO> selectByDate(Integer days,Date endTime,Integer siteId) {
  55. List<Object> faultList = new ArrayList<>();
  56. List<Object> rushList = new ArrayList<>();
  57. List<Object> noDisposeList = new ArrayList<>();
  58. List<Object> dateList = new ArrayList<>();
  59. Integer userId = SessionLocal.getUserId();
  60. List<AlarmPower> alarmPowers = baseMapper.list(userId, siteId, null, DateUtils.addDays(endTime, -days), endTime, null, null);
  61. for (int i = 0; i < days; i++) {
  62. Date setDays = DateUtils.addDays(endTime, -i);
  63. Date dayStartTime = DateUtils.getDayStartTime(setDays);
  64. Date dayEndTime = DateUtils.getDayEndTime(setDays);
  65. faultList.add(getCount(alarmPowers,null,dayStartTime,dayEndTime,null,null));
  66. rushList.add(getCount(alarmPowers,1,dayStartTime,dayEndTime,null,null));
  67. noDisposeList.add(getCount(alarmPowers,0,dayStartTime,dayEndTime,null,null));
  68. dateList.add(DateUtils.getMonth(setDays)+"-"+DateUtils.getDay(setDays));
  69. }
  70. List<CommonIcoVO> list = new ArrayList<>();
  71. list.add(CommonIcoVO.builder().name("故障数量").list(faultList).listDate(dateList).build());
  72. list.add(CommonIcoVO.builder().name("抢修数量").list(rushList).listDate(dateList).build());
  73. list.add(CommonIcoVO.builder().name("未处理数量").list(noDisposeList).listDate(dateList).build());
  74. return list;
  75. }
  76. @Override
  77. public CommonPage<AlarmPower> page(Date startTime, Date endTime, Integer status, Integer siteId,Integer current,Integer size) {
  78. Page<AlarmPower> page = new Page<>(current, size);
  79. page = baseMapper.page(page,SessionLocal.getUserId(),siteId,status,startTime,endTime);
  80. return this.ToCommonPage(page);
  81. }
  82. @Override
  83. public List<Map<String, Object>> getLoopStatusList(Integer siteId) {
  84. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  85. List<Map<String, Object>> list = new ArrayList<>();
  86. List<AlarmPower> loopStatusListMap = baseMapper.getLoopStatusListMap(siteId);
  87. int int1 = 0;
  88. int int12= 0;
  89. Map<String,Object> map = new HashMap<>();
  90. loopStatusListMap.forEach(alarmPower -> {
  91. // if (alarmPower.getDigitalValue() == 0){
  92. // String time1 = formatter.format(alarmPower.getSoeTime());
  93. // }else if(alarmPower.getDigitalValue() == 1){
  94. // String time2 = formatter.format(alarmPower.getSoeTime());
  95. // }
  96. if (alarmPower.getDigitalValue() == 0){
  97. // Duration differenceValue = Duration.between(time1 ,time2 );
  98. // 获取的是两个时间相差的分钟数,如果想要相差小时数就调用toHours()
  99. // Long minutesTime = differenceValue .toMinutes();
  100. }
  101. });
  102. // Object value = LoopStatusList.get("LoadLive");
  103. // int day_count = baseMapper.getOffCountDMap(siteId);
  104. // int month_count = baseMapper.getOffCountMMap(siteId);
  105. // Map<String,Object> map = new HashMap<>();
  106. // map.put("LoadLive",value);
  107. // map.put("day_count","day_count");
  108. // map.put("month_count","month_count");
  109. // list.add(map);
  110. // Iterator<String> it = radMap.keySet().iterator();
  111. // int duration=0;
  112. // for (String name:LoopStatusList.keySet()) {
  113. //// String a =formatter.;
  114. //// LoopStatusList.get("sending_time");
  115. //
  116. // }
  117. return list;
  118. }
  119. @Override
  120. public String export(Date startTime, Date endTime, Integer status, Integer siteId){
  121. Integer userId = SessionLocal.getUserId();
  122. Workbook workbook = null;
  123. File file = null;
  124. try {
  125. ExportParams params = new ExportParams(null, "电力告警");
  126. workbook = ExcelExportUtil.exportBigExcel(params, AlarmPowerExportVO.class,
  127. (o, i) -> {
  128. Page<AlarmPower> page = new Page<>(i, 30);
  129. page = baseMapper.page(page, userId, siteId, status, startTime, endTime);
  130. return new ArrayList<>(BeanMapperUtils.mapList(page.getRecords(), AlarmPower.class, AlarmPowerExportVO.class));
  131. },null);
  132. if (null != workbook) {
  133. file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "电力告警", System.currentTimeMillis() + ""));
  134. FileUtils.createFile(file.getAbsolutePath());
  135. FileOutputStream allListingFileOutputStream = new FileOutputStream(file);
  136. workbook.write(allListingFileOutputStream);
  137. } else {
  138. throw new BusinessException("表格数据为空");
  139. }
  140. } catch (Exception e) {
  141. log.error("导出文件失败", e);
  142. throw new BusinessException("导出文件失败");
  143. } finally {
  144. if (workbook != null) {
  145. try {
  146. workbook.close();
  147. } catch (IOException e) {
  148. log.error("===export spec=== 关闭workbook失败", e);
  149. }
  150. }
  151. }
  152. return file.getName();
  153. }
  154. @Override
  155. public List<CommonIcoVO> runStatus(Date startTime, Date endTime, Integer siteId) {
  156. Integer userId = SessionLocal.getUserId();
  157. List<CommonIcoVO> list = new ArrayList<>();
  158. List<Object> offCounts = new ArrayList<>();
  159. List<Object> offDateS = new ArrayList<>();
  160. List<Object> runStatus = new ArrayList<>();
  161. List<Object> runDates = new ArrayList<>();
  162. List<AlarmPower> alarmPowers = baseMapper.list(userId, siteId, null, startTime, endTime, "LoadLive", null);
  163. if (!CollectionUtils.isEmpty(list)){
  164. Date forEndTime;
  165. do {
  166. Date dayStartTime = DateUtils.getDayStartTime(startTime);
  167. Date dayEndTime = DateUtils.getDayEndTime(startTime);
  168. offCounts.add(getCount(alarmPowers,null,dayStartTime,dayEndTime,null,null));
  169. forEndTime = dayEndTime;
  170. offDateS.add(DateUtils.format(endTime,DateUtils.PARSE_PATTERNS[8]));
  171. } while (forEndTime.before(endTime));
  172. alarmPowers.forEach(alarmPower -> {
  173. runStatus.add(alarmPower.getDigitalValue());
  174. runDates.add(DateUtils.format(alarmPower.getSendingTime(),DateUtils.PARSE_PATTERNS[5]));
  175. });
  176. }
  177. list.add(CommonIcoVO.builder().name("offCount").list(offCounts).listDate(offDateS).build());
  178. list.add(CommonIcoVO.builder().name("runStates").list(runStatus).listDate(runDates).build());
  179. return list;
  180. }
  181. /**
  182. * 汇总
  183. * @param list
  184. * @param handlingStatus
  185. * @param startTime
  186. * @param endTime
  187. * @param digitalStatus
  188. */
  189. public Long getCount(List<AlarmPower> list,Integer handlingStatus, Date startTime, Date endTime, Integer digitalStatus,String measName){
  190. return list.stream()
  191. .filter(alarmPower -> alarmPower.getSendingTime().after(startTime) && alarmPower.getSendingTime().before(endTime))
  192. .filter(alarmPower -> Optional.ofNullable(handlingStatus).map(hand -> hand.equals(alarmPower.getHandlingStatus())).orElse(true))
  193. .filter(alarmPower -> Optional.ofNullable(digitalStatus).map(dig -> dig.equals(alarmPower.getDigitalValue())).orElse(true))
  194. .filter(alarmPower -> Optional.ofNullable(measName).map(means -> means.equals(alarmPower.getMeasName())).orElse(true))
  195. .count();
  196. }
  197. }