123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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.*;
- /**
- * <p>
- * 电力告警 服务实现类
- * </p>
- *
- * @author ya
- * @since 2021-07-07
- */
- @Service
- public class AlarmPowerServiceImpl extends AbstractCrudService<AlarmPowerMapper, AlarmPower> 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<CommonIcoVO> selectByDate(Integer days,Date endTime,Integer siteId) {
- List<Object> faultList = new ArrayList<>();
- List<Object> rushList = new ArrayList<>();
- List<Object> noDisposeList = new ArrayList<>();
- List<Object> dateList = new ArrayList<>();
- Integer userId = SessionLocal.getUserId();
- List<AlarmPower> 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<CommonIcoVO> 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<AlarmPower> page(Date startTime, Date endTime, Integer status, Integer siteId,Integer current,Integer size) {
- Page<AlarmPower> page = new Page<>(current, size);
- page = baseMapper.page(page,SessionLocal.getUserId(),siteId,status,startTime,endTime);
- return this.ToCommonPage(page);
- }
- @Override
- public List<Map<String, Object>> getLoopStatusList(Integer siteId) {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- List<Map<String, Object>> list = new ArrayList<>();
- List<AlarmPower> loopStatusListMap = baseMapper.getLoopStatusListMap(siteId);
- int int1 = 0;
- int int12= 0;
- Map<String,Object> 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<String,Object> map = new HashMap<>();
- // map.put("LoadLive",value);
- // map.put("day_count","day_count");
- // map.put("month_count","month_count");
- // list.add(map);
- // Iterator<String> 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<AlarmPower> 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<CommonIcoVO> runStatus(Date startTime, Date endTime, Integer siteId) {
- Integer userId = SessionLocal.getUserId();
- List<CommonIcoVO> list = new ArrayList<>();
- List<Object> offCounts = new ArrayList<>();
- List<Object> offDateS = new ArrayList<>();
- List<Object> runStatus = new ArrayList<>();
- List<Object> runDates = new ArrayList<>();
- List<AlarmPower> 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<AlarmPower> 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();
- }
- }
|