| | |
| | | package com.whyc.service; |
| | | |
| | | import com.whyc.dto.Real.QuarterData; |
| | | import com.whyc.dto.Real.BattHisRealDto; |
| | | import com.whyc.dto.Real.CompareDto; |
| | | import com.whyc.dto.Real.QuarterDto; |
| | | import com.whyc.dto.Real.RealDateDTO; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.factory.ThreadPoolExecutorFactory; |
| | | import com.whyc.mapper.CommonMapper; |
| | | |
| | | import com.whyc.pojo.db_data_history.BattRealdataId; |
| | | import com.whyc.pojo.db_param.AlmAnalysisParam; |
| | | import com.whyc.pojo.db_param.AppParam; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.SubTablePageInfoUtils; |
| | | import com.whyc.util.DateUtil; |
| | | import com.whyc.util.ThreadLocalUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.text.ParseException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.CountDownLatch; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static java.lang.Thread.sleep; |
| | | |
| | | @Service |
| | | public class BattRealdataIdService { |
| | |
| | | @Resource |
| | | private CommonMapper commonMapper; |
| | | |
| | | @Autowired |
| | | private BattresdataInfService resInfService; |
| | | |
| | | @Autowired |
| | | private BattInfService binfService; |
| | | |
| | | @Autowired |
| | | private AlmAnalysisParamService almAnalysisParamService; |
| | | |
| | | @Autowired |
| | | private AppParamService appParamService; |
| | | |
| | | |
| | | //获取电池组最近一季度的单体数据 |
| | | public Response getBattQuarter(Integer battgroupId) { |
| | | public Response getBattQuarterVol(Integer battgroupId) { |
| | | HashMap<String,Object> map = new HashMap<>(); |
| | | Float maxData=0f; |
| | | Float minData=0f; |
| | | Float avgData=0f; |
| | | int num=0; |
| | | List<String> datelist = ActionUtil.getLastQuarterYearMonths(); |
| | | for (String date:datelist) { |
| | | for (int i=0;i<datelist.size();i++) { |
| | | String date=datelist.get(i); |
| | | String tableName ="db_data_history.tb_batt_realdata_"+battgroupId+"_"+date; |
| | | String existTableName = commonMapper.existTable("db_data_history", "tb_batt_realdata_"+battgroupId+"_"+date); |
| | | if(existTableName == null){ |
| | | continue; |
| | | } |
| | | //判断表是否存在 |
| | | List<QuarterData> list=subTablePageInfoService.getBattRealHis(tableName,"mon_vol"); |
| | | map.put(date,list); |
| | | List<QuarterDto> datalist=subTablePageInfoService.getBattRealHis(tableName,"mon_vol"); |
| | | map.put(date,datalist); |
| | | //获取表中最大,最小,平均数据值 |
| | | CompareDto compareData=subTablePageInfoService.getBattCompareHis(tableName,"mon_vol"); |
| | | if(num==0){ |
| | | maxData=compareData.getMaxData(); |
| | | minData=compareData.getMinData(); |
| | | } |
| | | if (compareData.getMaxData() >= maxData) { |
| | | maxData =compareData.getMaxData(); |
| | | } |
| | | if (compareData.getMinData() <= minData) { |
| | | minData= compareData.getMinData(); |
| | | } |
| | | avgData+= compareData.getAvgData(); |
| | | map.put("maxData",maxData); |
| | | map.put("minData",minData); |
| | | map.put("avgData",avgData/datelist.size()); |
| | | num++; |
| | | } |
| | | return new Response().setII(1,map.size()>0,map,"获取电池组最近一季度的数据"); |
| | | return new Response().setII(1,map.size()>0,map,"获取电池组最近一季度的电压数据"); |
| | | } |
| | | //获取电池组最近一季度的内阻数据 |
| | | public Response getBattQuarterRes(Integer battgroupId) { |
| | | HashMap<String,Object> map = new HashMap<>(); |
| | | Float maxData=0f; |
| | | Float minData=0f; |
| | | Float avgData=0f; |
| | | int num=0; |
| | | List<String> datelist = ActionUtil.getLastQuarterYearMonths(); |
| | | //获取内阻初始数据(第一次内阻测试的第一笔数据) |
| | | List<QuarterDto> list=resInfService.getFirstResData(battgroupId); |
| | | if(list==null){//当初始值为0时使用标称内阻 |
| | | list=binfService.getBinfResStd(battgroupId); |
| | | } |
| | | map.put("startRes",list); |
| | | for (int i=0;i<datelist.size();i++) { |
| | | String date=datelist.get(i); |
| | | String tableName ="db_data_history.tb_batt_realdata_"+battgroupId+"_"+date; |
| | | String existTableName = commonMapper.existTable("db_data_history", "tb_batt_realdata_"+battgroupId+"_"+date); |
| | | if(existTableName == null){ |
| | | continue; |
| | | } |
| | | //判断表是否存在 |
| | | List<QuarterDto> datalist=subTablePageInfoService.getBattRealHis(tableName,"mon_res"); |
| | | map.put(date,datalist); |
| | | //获取表中最大,最小,平均数据值 |
| | | CompareDto compareData=subTablePageInfoService.getBattCompareHis(tableName,"mon_res"); |
| | | if(num==0){ |
| | | maxData=compareData.getMaxData(); |
| | | minData=compareData.getMinData(); |
| | | } |
| | | if (compareData.getMaxData() >= maxData) { |
| | | maxData =compareData.getMaxData(); |
| | | } |
| | | if (compareData.getMinData() <= minData) { |
| | | minData= compareData.getMinData(); |
| | | } |
| | | avgData+= compareData.getAvgData(); |
| | | map.put("maxData",maxData); |
| | | map.put("minData",minData); |
| | | map.put("avgData",avgData/datelist.size()); |
| | | num++; |
| | | } |
| | | return new Response().setII(1,map.size()>0,map,"获取电池组最近一季度的内阻数据"); |
| | | } |
| | | //获取电池组最近一季度的温度数据 |
| | | public Response getBattQuarterTmp(Integer battgroupId) { |
| | | HashMap<String,Object> map = new HashMap<>(); |
| | | Float maxData=0f; |
| | | Float minData=0f; |
| | | Float avgData=0f; |
| | | int num=0; |
| | | List<String> datelist = ActionUtil.getLastQuarterYearMonths(); |
| | | for (int i=0;i<datelist.size();i++) { |
| | | String date=datelist.get(i); |
| | | String tableName ="db_data_history.tb_batt_realdata_"+battgroupId+"_"+date; |
| | | String existTableName = commonMapper.existTable("db_data_history", "tb_batt_realdata_"+battgroupId+"_"+date); |
| | | //判断表是否存在 |
| | | if(existTableName == null){ |
| | | continue; |
| | | } |
| | | List<QuarterDto> datalist=subTablePageInfoService.getBattRealHis(tableName,"mon_tmp"); |
| | | map.put(date,datalist); |
| | | //获取表中最大,最小,平均数据值 |
| | | CompareDto compareData=subTablePageInfoService.getBattCompareHis(tableName,"mon_tmp"); |
| | | if(num==0){ |
| | | maxData=compareData.getMaxData(); |
| | | minData=compareData.getMinData(); |
| | | } |
| | | if (compareData.getMaxData() >= maxData) { |
| | | maxData =compareData.getMaxData(); |
| | | } |
| | | if (compareData.getMinData() <= minData) { |
| | | minData= compareData.getMinData(); |
| | | } |
| | | avgData+= compareData.getAvgData(); |
| | | map.put("maxData",maxData); |
| | | map.put("minData",minData); |
| | | map.put("avgData",avgData/datelist.size()); |
| | | num++; |
| | | } |
| | | return new Response().setII(1,map.size()>0,map,"获取电池组最近一季度的温度数据"); |
| | | } |
| | | //系统概览获取半小时核容设备信息 |
| | | public Response getHalfHourBattDevData(Integer battgroupId,Integer granularity) { |
| | | String dateTime = ActionUtil.sdfwithOutday.format(new Date()); |
| | | String tableName ="db_data_history.tb_batt_realdata_"+battgroupId+"_"+dateTime; |
| | | //获取前半个小时数据 |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | // 计算半小时前的时间点 |
| | | LocalDateTime halfHourAgo = now.minusMinutes(30); |
| | | // 格式化输出 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | String halfHourAgoTime=halfHourAgo.format(formatter); |
| | | String existTableName = commonMapper.existTable("db_data_history", "tb_batt_realdata_"+battgroupId+"_"+dateTime); |
| | | if(existTableName == null){ |
| | | return new Response().set(1,false,"当前电池组不存在前面小时数据"); |
| | | } |
| | | List<BattHisRealDto> datalist=subTablePageInfoService.getHalfHourBattDevData(tableName,granularity,halfHourAgoTime); |
| | | return new Response().setII(1,datalist.size()>0,datalist,"获取半小时内核容设备信息"); |
| | | } |
| | | //历史实时数据 |
| | | public Response getBattRealDataHis(Integer battgroupId, Integer granularity,String startTime,String endTime) throws ParseException, InterruptedException { |
| | | List<RealDateDTO> dataList = new LinkedList<>(); |
| | | List<List<Date>> monthTimeList = DateUtil.getMonthTime(ThreadLocalUtil.parse(startTime,1), ThreadLocalUtil.parse(endTime,1)); |
| | | ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor(); |
| | | CountDownLatch latch = new CountDownLatch(monthTimeList.size()); |
| | | for (int i = 0; i < monthTimeList.size(); i++) { |
| | | int finalI = i; |
| | | pool.execute(() -> { |
| | | int finalII = finalI; |
| | | BattRealdataId realdata = new BattRealdataId(); |
| | | realdata.setRecordTime(monthTimeList.get(finalII).get(0)); |
| | | realdata.setRecordTime1(monthTimeList.get(finalII).get(1)); |
| | | String table = battgroupId + "_" + ThreadLocalUtil.format(realdata.getRecordTime(),2); |
| | | realdata.setTableName(table);//表名时间格式部分 |
| | | //判断表是否存在 |
| | | int tableNum = subTablePageInfoService.judgeTable_realdata(table); |
| | | List<RealDateDTO> list = new ArrayList(); |
| | | if (tableNum > 0) { |
| | | //获取指定时间段内最大最小recordNum确保数据的完整 |
| | | List recordNums= subTablePageInfoService.getMaxAndMinRecordNum(realdata); |
| | | Integer maxRecordNum= 0; |
| | | Integer minRecordNum= 0; |
| | | if(recordNums.size()>0){ |
| | | maxRecordNum=(Integer)recordNums.get(0); |
| | | minRecordNum=(Integer)recordNums.get(1); |
| | | } |
| | | list = subTablePageInfoService.getBattRealDataHis(realdata,granularity,maxRecordNum,minRecordNum); |
| | | } |
| | | dataList.addAll(list); |
| | | latch.countDown(); |
| | | }); |
| | | sleep(200); |
| | | } |
| | | latch.await(10, TimeUnit.MINUTES); |
| | | List dataListSorted = dataList.stream().sorted(Comparator.comparing(RealDateDTO::getRecordTime)).collect(Collectors.toList()); |
| | | return new Response().set(1, dataListSorted); |
| | | } |
| | | //电池告警点击具体告警信息查看从告警开始时间到现在的历史实时数据 |
| | | public Response getBattHisRealInAlm(Integer battgroupId, String startTime, Integer almId) throws ParseException, InterruptedException { |
| | | //获取告警almId对应的分析配置属性值 |
| | | AlmAnalysisParam almAnalysisParam= almAnalysisParamService.getAnalysisParam(almId); |
| | | List<RealDateDTO> dataList = new LinkedList<>(); |
| | | //获取预警分析周期阈值 |
| | | AppParam appParam = appParamService.getAlarmAnalysisCycle(); |
| | | Integer cycleTime=appParam.getParamValue().intValue(); |
| | | Date cyscleDate=ActionUtil.getDateAdd(ThreadLocalUtil.parse(startTime,1),cycleTime*(-1)); |
| | | //获取cyscleDate到现在所有的历史实时数据 |
| | | List<List<Date>> monthTimeList = DateUtil.getMonthTime(cyscleDate,new Date()); |
| | | ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor(); |
| | | CountDownLatch latch = new CountDownLatch(monthTimeList.size()); |
| | | for (int i = 0; i < monthTimeList.size(); i++) { |
| | | int finalI = i; |
| | | pool.execute(() -> { |
| | | int finalII = finalI; |
| | | BattRealdataId realdata = new BattRealdataId(); |
| | | realdata.setRecordTime(monthTimeList.get(finalII).get(0)); |
| | | realdata.setRecordTime1(monthTimeList.get(finalII).get(1)); |
| | | String table = battgroupId + "_" + ThreadLocalUtil.format(realdata.getRecordTime(),2); |
| | | realdata.setTableName(table);//表名时间格式部分 |
| | | //判断表是否存在 |
| | | int tableNum = subTablePageInfoService.judgeTable_realdata(table); |
| | | List<RealDateDTO> list = new ArrayList(); |
| | | if (tableNum > 0) { |
| | | list = subTablePageInfoService.getBattHisRealInAlm(realdata); |
| | | } |
| | | dataList.addAll(list); |
| | | latch.countDown(); |
| | | }); |
| | | sleep(200); |
| | | } |
| | | latch.await(10, TimeUnit.MINUTES); |
| | | List dataListSorted = dataList.stream().sorted(Comparator.comparing(RealDateDTO::getRecordTime)).collect(Collectors.toList()); |
| | | return new Response().setIII(1,true, dataListSorted,almAnalysisParam,"电池告警点击具体告警信息查看从告警开始时间到现在的历史实时数据"); |
| | | |
| | | } |
| | | } |