whyclxw
5 天以前 210efc8a9b3d3c5a6a3f453dd4ce14f735d63f9b
src/main/java/com/whyc/service/PwrdevHistorydataIdService.java
@@ -2,31 +2,40 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.constant.PwrCapperformanceEnum;
import com.whyc.dto.AnalysisAlm.ResAnalysis;
import com.whyc.dto.Real.CompareDto;
import com.whyc.dto.Real.PwrHisRealAcInDto;
import com.whyc.dto.Real.PwrHisRealDcoutInDto;
import com.whyc.dto.Real.QuarterDto;
import com.whyc.dto.Response;
import com.whyc.dto.Statistic.QuarterPwr7Res;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.db_data_history.BattRealdataId;
import com.whyc.pojo.db_data_history.PwrdevHistorydataId;
import com.whyc.pojo.db_param.AppParam;
import com.whyc.pojo.db_param.ApptimeParam;
import com.whyc.pojo.db_param.PowerPropertyParam;
import com.whyc.pojo.db_param.PowerheartParam;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 PwrdevHistorydataIdService {
@@ -47,32 +56,73 @@
    @Autowired
    private PowerPropertyParamService  propertyParamService;
    //系统概览获取半小时交流输入统计
    public Response getHalfHourPwrHisAcinData(Integer powerId,Integer granularity) {
        String dateTime = ActionUtil.sdfwithOutday.format(new Date());
        //获取前半个小时数据
        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 tableName ="db_data_history.tb_pwrdev_historydata_"+powerId+"_"+dateTime;
        String existTableName = commonMapper.existTable("db_data_history", "tb_pwrdev_historydata_"+powerId+"_"+dateTime);
        if(existTableName == null){
            return new Response().set(1,false,"当前电池组不存在前面小时数据");
    @Autowired
    private PowerheartParamService heartService;
    //系统概览前N笔交流输入统计
    public Response getPwrHisAcinData100(Integer powerId) throws ParseException, InterruptedException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
        }
        List<PwrHisRealAcInDto> datalist=subTablePageInfoService.getHalfHourPwrHisAcinData(tableName,granularity,halfHourAgoTime);
        return new Response().setII(1,datalist.size()>0,datalist,"获取半小时内交流输入统计");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        String halfHourAgoTime=halfHourAgo.format(formatter);
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<PwrHisRealAcInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealAcInDto> list = new ArrayList();
                if (tableNum > 0) {
                    list=subTablePageInfoService.getHalfHourPwrHisAcinData(tableName, finalGranularity,recordDatetime,recordDatetime1);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealAcInDto::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内交流输入统计");
    }
    //系统概览获取半小时直流输出统计
    public Response getHalfHourPwrHisDcoutData(Integer powerId,Integer granularity) {
    //系统概览获取前N笔直流输出统计
    public Response getHalfHourPwrHisDcoutData(Integer powerId) {
        String dateTime = ActionUtil.sdfwithOutday.format(new Date());
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
        }
        //获取前半个小时数据
        LocalDateTime now = LocalDateTime.now();
        // 计算半小时前的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(30);
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String halfHourAgoTime=halfHourAgo.format(formatter);
@@ -99,12 +149,9 @@
        List<String> datelist=ActionUtil.getDateListBetweenDates(apptimeParam.getParamValue(), new Date());
        //获取电源优良判断的标准参数值
        List<PowerPropertyParam> paramList=propertyParamService.getPwrStandardParam(powerType);
        /*Map<String,Float> paramValues=new HashMap<>();
        for (PowerPropertyParam param : paramList) {
            paramValues.put(param.getPropertyName()+"_upper", param.getBasisVal()*param.getAlarmLimithUpper());
            paramValues.put(param.getPropertyName()+"_lower", param.getBasisVal()*param.getAlarmLimitlLower());
            map.put(param.getPropertyName(), 0);
        }*/
        List<String> propertyNameList = paramList.stream()
                .map(param -> param.getPropertyName())
                .collect(Collectors.toList());
        for (int i=0;i<datelist.size();i++) {
            String date=datelist.get(i);
            String tableName ="db_data_history.tb_pwrdev_historydata_"+powerId+"_"+date;
@@ -113,33 +160,13 @@
            if(existTableName == null){
                continue;
            }
            List<QuarterPwr7Res> datalist=subTablePageInfoService.getPwrQuarter7(tableName);
            List<PwrdevHistorydataId> datalist=subTablePageInfoService.getPwrQuarter7(tableName,propertyNameList);
            // 遍历 dataList 并比较属性值
            for (QuarterPwr7Res data : datalist) {
                /*if(paramValues.get("acin1_vola_upper")!=null){
                    if ((data.getAcin1Vola() > paramValues.get("acin1_vola_upper"))||(data.getAcin1Vola() < paramValues.get("acin1_vola_lower"))) {
                        map.put("acin1_vola", map.get("acin1_vola") + 1);
                    }
                }
                if(paramValues.get("acout_curra_upper")!=null) {
                    if ((data.getAcoutCurra() > paramValues.get("acout_curra_upper")) || (data.getAcoutCurra() < paramValues.get("acout_curra_lower"))) {
                        map.put("acout_curra", map.get("acout_curra") + 1);
                    }
                }
                if(paramValues.get("dcout_vol_upper")!=null) {
                    if ((data.getDcoutVol() > paramValues.get("dcout_vol_upper")) || (data.getDcoutVol() < paramValues.get("dcout_vo_lower"))) {
                        map.put("dcout_vol", map.get("dcout_vol") + 1);
                    }
                }
                if(paramValues.get("dcout_curr_upper")!=null) {
                    if ((data.getDcoutCurr() > paramValues.get("dcout_curr_upper")) || (data.getDcoutCurr() < paramValues.get("dcout_curr_lower"))) {
                        map.put("dcout_curr", map.get("dcout_curr") + 1);
                    }
                }*/
            for (PwrdevHistorydataId data : datalist) {
                for (int j=0;j<paramList.size();j++) {
                    PowerPropertyParam param = paramList.get(j);
                    String propertyName = ActionUtil.toCamelCase(param.getPropertyName());
                    Field field = QuarterPwr7Res.class.getDeclaredField(propertyName);
                    Field field = PwrdevHistorydataId.class.getDeclaredField(propertyName);
                    field.setAccessible(true); // 如果字段是私有的,需要设置为可访问
                    Float value = (Float) field.get(data);
                    if ((value > (param.getBasisVal()*param.getAlarmLimithUpper()))||(value <(param.getBasisVal()*param.getAlarmLimitlLower()))) {