package com.bizmatics.service.impl; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import cn.afterturn.easypoi.handler.inter.IExcelExportServer; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.bizmatics.common.core.bean.CommonPage; import com.bizmatics.common.core.exception.BusinessException; import com.bizmatics.common.core.util.BeanMapperUtils; import com.bizmatics.common.core.util.DateUtils; import com.bizmatics.common.core.util.FileUtils; import com.bizmatics.common.spring.util.GlobalUtils; import com.bizmatics.model.AlarmPower; import com.bizmatics.persistence.mapper.AlarmPowerMapper; import com.bizmatics.service.AlarmPowerService; import com.bizmatics.common.mvc.base.AbstractCrudService; import com.bizmatics.service.util.SessionLocal; import com.bizmatics.service.vo.AlarmPowerExportVO; import com.bizmatics.service.vo.ApCountVO; import com.bizmatics.service.vo.CommonIcoVO; import net.sf.jsqlparser.expression.DateTimeLiteralExpression; import org.apache.ibatis.annotations.Param; import org.apache.poi.ss.usermodel.Workbook; import org.checkerframework.checker.units.qual.A; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.unit.DataUnit; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; /** *

* 电力告警 服务实现类 *

* * @author ya * @since 2021-07-07 */ @Service public class AlarmPowerServiceImpl extends AbstractCrudService implements AlarmPowerService { @Override public ApCountVO selectCount(Integer siteId) { Integer userId = SessionLocal.getUserId(); ApCountVO apCountVo = new ApCountVO(); apCountVo.setCount(baseMapper.selectCount(userId, siteId, null, null, null, null, null)); apCountVo.setUnCount(baseMapper.selectCount(userId, siteId, 0, null, null, null, null)); return apCountVo; } @Override public List selectByDate(Integer days,Date endTime,Integer siteId) { List faultList = new ArrayList<>(); List rushList = new ArrayList<>(); List noDisposeList = new ArrayList<>(); List dateList = new ArrayList<>(); Integer userId = SessionLocal.getUserId(); List alarmPowers = baseMapper.list(userId, siteId, null, DateUtils.addDays(endTime, -days), endTime, null, null); for (int i = 0; i < days; i++) { Date setDays = DateUtils.addDays(endTime, -i); Date dayStartTime = DateUtils.getDayStartTime(setDays); Date dayEndTime = DateUtils.getDayEndTime(setDays); faultList.add(getCount(alarmPowers,null,dayStartTime,dayEndTime,null,null)); rushList.add(getCount(alarmPowers,1,dayStartTime,dayEndTime,null,null)); noDisposeList.add(getCount(alarmPowers,0,dayStartTime,dayEndTime,null,null)); dateList.add(DateUtils.getMonth(setDays)+"-"+DateUtils.getDay(setDays)); } List list = new ArrayList<>(); list.add(CommonIcoVO.builder().name("故障数量").list(faultList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("抢修数量").list(rushList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("未处理数量").list(noDisposeList).listDate(dateList).build()); return list; } @Override public CommonPage page(Date startTime, Date endTime, Integer status, Integer siteId,Integer current,Integer size) { Page page = new Page<>(current, size); page = baseMapper.page(page,SessionLocal.getUserId(),siteId,status,startTime,endTime); return this.ToCommonPage(page); } @Override public List> getLoopStatusList(Integer siteId) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List> list = new ArrayList<>(); List loopStatusListMap = baseMapper.getLoopStatusListMap(siteId); int int1 = 0; int int12= 0; Map map = new HashMap<>(); loopStatusListMap.forEach(alarmPower -> { // if (alarmPower.getDigitalValue() == 0){ // String time1 = formatter.format(alarmPower.getSoeTime()); // }else if(alarmPower.getDigitalValue() == 1){ // String time2 = formatter.format(alarmPower.getSoeTime()); // } if (alarmPower.getDigitalValue() == 0){ // Duration differenceValue = Duration.between(time1 ,time2 ); // 获取的是两个时间相差的分钟数,如果想要相差小时数就调用toHours() // Long minutesTime = differenceValue .toMinutes(); } }); // Object value = LoopStatusList.get("LoadLive"); // int day_count = baseMapper.getOffCountDMap(siteId); // int month_count = baseMapper.getOffCountMMap(siteId); // Map map = new HashMap<>(); // map.put("LoadLive",value); // map.put("day_count","day_count"); // map.put("month_count","month_count"); // list.add(map); // Iterator it = radMap.keySet().iterator(); // int duration=0; // for (String name:LoopStatusList.keySet()) { //// String a =formatter.; //// LoopStatusList.get("sending_time"); // // } return list; } @Override public String export(Date startTime, Date endTime, Integer status, Integer siteId){ Integer userId = SessionLocal.getUserId(); Workbook workbook = null; File file = null; try { ExportParams params = new ExportParams(null, "电力告警"); workbook = ExcelExportUtil.exportBigExcel(params, AlarmPowerExportVO.class, (o, i) -> { Page page = new Page<>(i, 30); page = baseMapper.page(page, userId, siteId, status, startTime, endTime); return new ArrayList<>(BeanMapperUtils.mapList(page.getRecords(), AlarmPower.class, AlarmPowerExportVO.class)); },null); if (null != workbook) { file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "电力告警", System.currentTimeMillis() + "")); FileUtils.createFile(file.getAbsolutePath()); FileOutputStream allListingFileOutputStream = new FileOutputStream(file); workbook.write(allListingFileOutputStream); } else { throw new BusinessException("表格数据为空"); } } catch (Exception e) { log.error("导出文件失败", e); throw new BusinessException("导出文件失败"); } finally { if (workbook != null) { try { workbook.close(); } catch (IOException e) { log.error("===export spec=== 关闭workbook失败", e); } } } return file.getName(); } @Override public List runStatus(Date startTime, Date endTime, Integer siteId) { Integer userId = SessionLocal.getUserId(); List list = new ArrayList<>(); List offCounts = new ArrayList<>(); List offDateS = new ArrayList<>(); List runStatus = new ArrayList<>(); List runDates = new ArrayList<>(); List alarmPowers = baseMapper.list(userId, siteId, null, startTime, endTime, "LoadLive", null); if (!CollectionUtils.isEmpty(list)){ Date forEndTime; do { Date dayStartTime = DateUtils.getDayStartTime(startTime); Date dayEndTime = DateUtils.getDayEndTime(startTime); offCounts.add(getCount(alarmPowers,null,dayStartTime,dayEndTime,null,null)); forEndTime = dayEndTime; offDateS.add(DateUtils.format(endTime,DateUtils.PARSE_PATTERNS[8])); } while (forEndTime.before(endTime)); alarmPowers.forEach(alarmPower -> { runStatus.add(alarmPower.getDigitalValue()); runDates.add(DateUtils.format(alarmPower.getSendingTime(),DateUtils.PARSE_PATTERNS[5])); }); } list.add(CommonIcoVO.builder().name("offCount").list(offCounts).listDate(offDateS).build()); list.add(CommonIcoVO.builder().name("runStates").list(runStatus).listDate(runDates).build()); return list; } /** * 汇总 * @param list * @param handlingStatus * @param startTime * @param endTime * @param digitalStatus */ public Long getCount(List list,Integer handlingStatus, Date startTime, Date endTime, Integer digitalStatus,String measName){ return list.stream() .filter(alarmPower -> alarmPower.getSendingTime().after(startTime) && alarmPower.getSendingTime().before(endTime)) .filter(alarmPower -> Optional.ofNullable(handlingStatus).map(hand -> hand.equals(alarmPower.getHandlingStatus())).orElse(true)) .filter(alarmPower -> Optional.ofNullable(digitalStatus).map(dig -> dig.equals(alarmPower.getDigitalValue())).orElse(true)) .filter(alarmPower -> Optional.ofNullable(measName).map(means -> means.equals(alarmPower.getMeasName())).orElse(true)) .count(); } }